<?php session_start(); include("../bd/conexion.php"); // Establecer zona horaria date_default_timezone_set('America/Bogota'); // Verificar sesión if (!isset($_SESSION['DIGITA'])) { header("Location: ../login.php"); exit(); } // Obtener listas para filtros $areas = []; $usuarios = []; $movimientos = ['Entrada', 'Salida']; // Consulta para obtener áreas y usuarios únicos de ambas tablas $query_filtros = "(SELECT AREA, userEntrega FROM regInventario WHERE AREA IS NOT NULL) UNION (SELECT AREA, userEntrega FROM regUsado WHERE AREA IS NOT NULL)"; $result_filtros = mysqli_query($conexion, $query_filtros); while ($row = mysqli_fetch_assoc($result_filtros)) { if (!in_array($row['AREA'], $areas)) { $areas[] = $row['AREA']; } if (!in_array($row['userEntrega'], $usuarios)) { $usuarios[] = $row['userEntrega']; } } sort($areas); sort($usuarios); // Procesar filtros del formulario $tabla_seleccionada = $_POST['tabla'] ?? 'regInventario'; $filtros = [ 'CODIGO' => $_POST['codigo'] ?? '', 'MOVIMIENTO' => $_POST['movimiento'] ?? '', 'AREA' => $_POST['area'] ?? '', 'userEntrega' => $_POST['usuario'] ?? '', 'fecha_inicio' => $_POST['fecha_inicio'] ?? '', 'fecha_fin' => $_POST['fecha_fin'] ?? date('Y-m-d') ]; // Procesar exportación a Excel if (isset($_POST['exportar'])) { header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="movimientos_' . $tabla_seleccionada . '_' . date('YmdHis') . '.xls"'); header('Cache-Control: max-age=0'); // Construir consulta con filtros $where = []; foreach ($filtros as $campo => $valor) { if (!empty($valor)) { if ($campo == 'fecha_inicio') { $where[] = "fechaIngreso >= '" . mysqli_real_escape_string($conexion, $valor) . "'"; } elseif ($campo == 'fecha_fin') { $where[] = "fechaIngreso <= '" . mysqli_real_escape_string($conexion, $valor) . " 23:59:59'"; } else { $where[] = "$campo = '" . mysqli_real_escape_string($conexion, $valor) . "'"; } } } $where_clause = !empty($where) ? 'WHERE ' . implode(' AND ', $where) : ''; $query = "SELECT * FROM $tabla_seleccionada $where_clause ORDER BY fechaIngreso DESC"; $result = mysqli_query($conexion, $query); // Generar Excel echo "<table border='1'>"; echo "<tr> <th>Fecha</th> <th>Codigo</th> <th>Descripcion</th> <th>Cantidad</th> <th>Movimiento</th> <th>Área</th> <th>Usuario</th> </tr>"; while ($row = mysqli_fetch_assoc($result)) { echo "<tr> <td>" . date('d/m/Y H:i', strtotime($row['fechaIngreso'])) . "</td> <td>{$row['CODIGO']}</td> <td>{$row['DESCRIPCION']}</td> <td>{$row['CANTIDAD']}</td> <td>{$row['MOVIMIENTO']}</td> <td>{$row['AREA']}</td> <td>{$row['userEntrega']}</td> </tr>"; } echo "</table>"; exit(); } // Consulta para mostrar resultados en pantalla $where = []; foreach ($filtros as $campo => $valor) { if (!empty($valor)) { if ($campo == 'fecha_inicio') { $where[] = "fechaIngreso >= '" . mysqli_real_escape_string($conexion, $valor) . "'"; } elseif ($campo == 'fecha_fin') { $where[] = "fechaIngreso <= '" . mysqli_real_escape_string($conexion, $valor) . " 23:59:59'"; } else { $where[] = "$campo = '" . mysqli_real_escape_string($conexion, $valor) . "'"; } } } $where_clause = !empty($where) ? 'WHERE ' . implode(' AND ', $where) : ''; $query = "SELECT * FROM $tabla_seleccionada $where_clause ORDER BY fechaIngreso DESC LIMIT 500"; $resultados = mysqli_query($conexion, $query); $total_resultados = mysqli_num_rows($resultados); ?> <!DOCTYPE html> <html lang="es"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Reporte de Movimientos</title> <link rel="icon" type="image/png" href="../img/icono.png"><!-- Cambia el icono de los titulos en las pestañas ico 16X16 --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> <style> body { background-image: linear-gradient(to right, #e2e2e2, #ffe5c9); padding: 20px; } .card { border-radius: 10px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); margin-bottom: 20px; } .card-header { background-color: #ff8a37; color: white; border-radius: 10px 10px 0 0 !important; } .table-container { overflow-x: auto; } .filter-section { background-color: #f1f1f1; padding: 15px; border-radius: 8px; margin-bottom: 20px; } .filter-title { font-weight: bold; margin-bottom: 10px; color: #ff8a37; } .btn-orange { background-color: #ff8a37; color: white; } .btn-orange:hover { background-color: #e07a2e; color: white; } .btn-success { background-color: #28a745; } .btn-success:hover { background-color: #218838; } .btn-red { background-color: #d04038; /* Color de fondo Rojo */ color: #fff; /* Color del texto */ font-size: 12px; padding: 10px 45px; border: 1px solid transparent; border-radius: 8px; font-weight: 600; letter-spacing: 0.5px; text-transform: uppercase; margin-top: 10px; cursor: pointer; text-decoration: none; /* Para eliminar el subrayado en enlaces */ display: inline-block; /* Para que el enlace se comporte como un botón */ } /* Mantener el color Rojo al pasar el mouse */ .btn-red:hover { background-color: #a73932; /* Un tono más oscuro de Rojo para efecto hover */ color: #fff; /* Color del texto */ } </style> </head> <body> <div class="container"> <div class="card"> <div class="card-header"> <h4><i class="fas fa-exchange-alt me-2"></i>Reporte de Movimientos de Inventario</h4> </div> <div class="card-body"> <form method="post" class="row g-3"> <div class="col-md-12 filter-section"> <div class="filter-title">Filtros de Búsqueda</div> <div class="row"> <div class="col-md-3"> <label class="form-label">Tabla</label> <select class="form-select" name="tabla" required> <option value="regInventario" <?= $tabla_seleccionada == 'regInventario' ? 'selected' : '' ?>>Inventario Nuevo</option> <option value="regUsado" <?= $tabla_seleccionada == 'regUsado' ? 'selected' : '' ?>>Inventario Usado</option> </select> </div> <div class="col-md-3"> <label class="form-label">Código</label> <input type="text" class="form-control" name="codigo" value="<?= htmlspecialchars($filtros['CODIGO']) ?>" placeholder="Código del item"> </div> <div class="col-md-3"> <label class="form-label">Movimiento</label> <select class="form-select" name="movimiento"> <option value="">Todos</option> <?php foreach ($movimientos as $mov): ?> <option value="<?= $mov ?>" <?= $filtros['MOVIMIENTO'] == $mov ? 'selected' : '' ?>><?= $mov ?></option> <?php endforeach; ?> </select> </div> <div class="col-md-3"> <label class="form-label">Área</label> <select class="form-select" name="area"> <option value="">Todas</option> <?php foreach ($areas as $area): ?> <option value="<?= htmlspecialchars($area) ?>" <?= $filtros['AREA'] == $area ? 'selected' : '' ?>><?= htmlspecialchars($area) ?></option> <?php endforeach; ?> </select> </div> </div> <div class="row mt-3"> <div class="col-md-3"> <label class="form-label">Usuario</label> <select class="form-select" name="usuario"> <option value="">Todos</option> <?php foreach ($usuarios as $user): ?> <option value="<?= htmlspecialchars($user) ?>" <?= $filtros['userEntrega'] == $user ? 'selected' : '' ?>><?= htmlspecialchars($user) ?></option> <?php endforeach; ?> </select> </div> <div class="col-md-3"> <label class="form-label">Fecha Inicio</label> <input type="date" class="form-control" name="fecha_inicio" value="<?= $filtros['fecha_inicio'] ?>"> </div> <div class="col-md-3"> <label class="form-label">Fecha Fin</label> <input type="date" class="form-control" name="fecha_fin" value="<?= $filtros['fecha_fin'] ?>"> </div> <div class="col-md-3 d-flex align-items-end"> <button type="submit" class="btn btn-orange w-100"> <i class="fas fa-search me-1"></i> Buscar </button> </div> </div> </div> </form> <?php if ($_SERVER['REQUEST_METHOD'] == 'POST' && !isset($_POST['exportar'])): ?> <div class="alert alert-info"> Mostrando resultados para <strong><?= $tabla_seleccionada ?></strong>. <?= $total_resultados ?> registros encontrados. </div> <form method="post"> <input type="hidden" name="tabla" value="<?= $tabla_seleccionada ?>"> <input type="hidden" name="codigo" value="<?= $filtros['CODIGO'] ?>"> <input type="hidden" name="movimiento" value="<?= $filtros['MOVIMIENTO'] ?>"> <input type="hidden" name="area" value="<?= $filtros['AREA'] ?>"> <input type="hidden" name="usuario" value="<?= $filtros['userEntrega'] ?>"> <input type="hidden" name="fecha_inicio" value="<?= $filtros['fecha_inicio'] ?>"> <input type="hidden" name="fecha_fin" value="<?= $filtros['fecha_fin'] ?>"> <button type="submit" name="exportar" class="btn btn-success mb-3"> <i class="fas fa-file-excel me-1"></i> Exportar a Excel </button> </form> <div class="table-container"> <table class="table table-striped table-bordered"> <thead class="table-dark"> <tr> <th>Fecha Movimiento</th> <th>Código</th> <th>Descripción</th> <th>Cantidad</th> <th>Movimiento</th> <th>Área</th> <th>Usuario</th> </tr> </thead> <tbody> <?php while ($row = mysqli_fetch_assoc($resultados)): ?> <tr> <td><?= date('d/m/Y H:i', strtotime($row['fechaIngreso'])) ?></td> <td><?= htmlspecialchars($row['CODIGO']) ?></td> <td><?= htmlspecialchars($row['DESCRIPCION']) ?></td> <td><?= $row['CANTIDAD'] ?></td> <td><?= htmlspecialchars($row['MOVIMIENTO']) ?></td> <td><?= htmlspecialchars($row['AREA']) ?></td> <td><?= htmlspecialchars($row['userEntrega']) ?></td> </tr> <?php endwhile; ?> </tbody> </table> </div> <?php endif; ?> <a class="btn-red" href="../dotacion/menup.PHP"><i class="fa-solid fa-circle-arrow-left"></i></a> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> <script> $(document).ready(function() { // Establecer fecha de hoy como valor por defecto para fecha_fin const today = new Date().toISOString().split('T')[0]; $('input[name="fecha_fin"]').val(today); // Restar 30 días a la fecha actual para fecha_inicio const date = new Date(); date.setDate(date.getDate() - 30); const thirtyDaysAgo = date.toISOString().split('T')[0]; $('input[name="fecha_inicio"]').val(thirtyDaysAgo); }); </script> </body> </html>