File "consulta.php"

Full Path: C:/wamp64/www/loteo/consulta.php
File size: 16.62 KB
MIME-type: text/x-php
Charset: utf-8

<?php
// Iniciar la sesión
session_start();
// Incluir conexión a la base de datos
require_once 'connection.php';

$conn = connection();

// Configuración de paginación
$registros_por_pagina = 40;
$pagina = isset($_GET['pagina']) ? (int)$_GET['pagina'] : 1;
$offset = ($pagina - 1) * $registros_por_pagina;

// Filtros
$beneficio   = isset($_GET['beneficio'])    ? $_GET['beneficio']    : '';
$fecha_inicio = isset($_GET['fecha_inicio']) ? $_GET['fecha_inicio'] : '';
$fecha_fin    = isset($_GET['fecha_fin'])    ? $_GET['fecha_fin']    : '';

// Construir la cláusula WHERE
$where_parts = [];
$params = [];
$types  = "";

if (!empty($fecha_inicio) && !empty($fecha_fin)) {
    $where_parts[] = "DATE(fecha) BETWEEN ? AND ?";
    $params[] = $fecha_inicio;
    $params[] = $fecha_fin;
    $types .= "ss";
} elseif (!empty($fecha_inicio)) {
    $where_parts[] = "DATE(fecha) >= ?";
    $params[] = $fecha_inicio;
    $types .= "s";
} elseif (!empty($fecha_fin)) {
    $where_parts[] = "DATE(fecha) <= ?";
    $params[] = $fecha_fin;
    $types .= "s";
}

if (!empty($beneficio)) {
    $where_parts[] = "beneficio = ?";
    $params[] = $beneficio;
    $types .= "s";
}

$where_clause = !empty($where_parts) ? " WHERE " . implode(" AND ", $where_parts) : "";

// Consulta para contar total de registros (con filtros)
$count_query = "SELECT COUNT(*) as total FROM entregas" . $where_clause;
$count_params = $params; // copia sin LIMIT/OFFSET
$count_stmt = $conn->prepare($count_query);
if (!empty($count_params)) {
    $count_stmt->bind_param($types, ...$count_params);
}
$count_stmt->execute();
$count_result = $count_stmt->get_result();
$total_registros = $count_result->fetch_assoc()['total'];
$total_paginas = ceil($total_registros / $registros_por_pagina);

// Consulta principal con paginación y filtros
$query = "SELECT * FROM entregas" . $where_clause . " ORDER BY fecha DESC LIMIT ? OFFSET ?";
$params[] = $registros_por_pagina;
$params[] = $offset;
$types .= "ii";

$stmt = $conn->prepare($query);
$stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();
?>

<!DOCTYPE html>
<html lang="es">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Registro de Entregas de Loteo</title>
    <link rel="icon" type="image/png" href="img/icono.png">
    <link rel="stylesheet" href="css/styles.css">
    <style>
        .principal {
            max-width: 1500px;
            margin: 0 auto;
            background: rgba(255, 255, 255, 0.95);
            padding: 25px;
            border-radius: 8px;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
        }

        .btn-exportar {
            background: #319426;
        }

        .btn-exportar:hover {
            background: #27701D;
        }

        .btn-devolucion {
            background: #E06043;
        }

        .btn-devolucion:hover {
            background: #D43C1E;
        }
		
	    .btn-crear {
            background: #006EF2;
        }

        .btn-crear:hover {
            background: #0055B5;
        }

        .logo {
            display: block;
            margin: 0 auto 1.5rem;
        }

        .footer {
            text-align: center;
            padding: 20px;
            margin-top: auto;
        }

        .copyright {
            display: inline-block;
            padding: 10px 25px;
            background-color: var(--primary-color);
            color: black;
            border-radius: 4px;
            font-size: 14px;
            font-weight: bold;
        }

        /* Estilos para filtros */
        .filtros {
            background: #f5f5f5;
            padding: 20px;
            border-radius: 6px;
            margin: 20px 0;
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            align-items: center;
        }

        .filtros label {
            font-weight: bold;
            color: #333;
        }

        .filtros input {
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }

        .filtros button {
            padding: 10px 20px;
            background: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }

        .filtros button:hover {
            background: #0056b3;
        }

        .btn-limpiar {
            background: #6c757d !important;
        }

        .btn-limpiar:hover {
            background: #545b62 !important;
        }

        /* Estilos para paginación */
        .paginacion {
            display: flex;
            justify-content: center;
            align-items: center;
            margin: 20px 0;
            flex-wrap: wrap;
            gap: 10px;
        }

        .paginacion a,
        .paginacion span {
            display: inline-block;
            padding: 8px 12px;
            margin: 0 2px;
            text-decoration: none;
            border: 1px solid #ddd;
            border-radius: 4px;
            color: #007bff;
        }

        .paginacion a:hover {
            background: #e9ecef;
        }

        .paginacion .actual {
            background: #007bff;
            color: white;
            border-color: #007bff;
        }

        .paginacion .disabled {
            color: #6c757d;
            cursor: not-allowed;
        }

        .info-registros {
            text-align: center;
            margin: 10px 0;
            color: #666;
            font-size: 14px;
        }

        /* Mejoras específicas para tablet/móvil */
        @media (max-width: 1324px) {
            table {
                display: block;
                overflow-x: auto;
                white-space: nowrap;
                width: 100%;
            }

            .principal {
                overflow-x: auto;
            }
        }

        .btn {
            padding: 10px 20px;
            font-size: 15px;
            margin: 6px;
        }

        table {
            font-size: 14px;
        }

        th,
        td {
            padding: 12px 10px;
        }

        .signature-pad {
            height: 180px;
        }

        @media (max-width: 768px) {
            .principal {
                padding: 15px;
            }

            h1 {
                font-size: 24px;
                margin-bottom: 20px;
            }

            .signature-pad {
                height: 160px;
            }

            table {
                font-size: 13px;
            }

            th,
            td {
                padding: 10px 8px;
            }

            .form-group {
                margin-bottom: 18px;
            }

            input[type="text"],
            input[type="number"],
            input[type="datetime-local"],
            textarea,
            select {
                padding: 10px;
                font-size: 15px;
            }

            .action-buttons {
                flex-direction: column;
                align-items: center;
            }

            .btn {
                width: 100%;
                max-width: 300px;
                margin: 5px 0;
            }

            .filtros {
                flex-direction: column;
                align-items: stretch;
            }

            .filtros > div {
                display: flex;
                flex-direction: column;
                gap: 5px;
            }

            .paginacion {
                justify-content: center;
                font-size: 12px;
            }

            .paginacion a,
            .paginacion span {
                padding: 6px 8px;
                font-size: 12px;
            }
        }

        @media (max-width: 480px) {
            body {
                padding: 10px;
            }

            .principal {
                padding: 12px;
            }

            table {
                display: block;
                overflow-x: auto;
                white-space: nowrap;
            }

            .signature-pad {
                height: 140px;
            }

            .firma {
                max-width: 120px;
                max-height: 60px;
            }
        }
    </style>
</head>

<body>
    <div class="principal">
        <img src="img/LOGO.png" alt="" width="37" height="37" class="logo" style="width: 250px; height: auto;" />
        <h1>Registro de Entregas de Loteo a Producción</h1>

        <div style="margin-bottom: 20px;">
            <a href="registrar.php" class="btn">Nueva Entrega</a>
            <a href="devolucion.php" class="btn btn-devolucion">Registrar Devolución</a>
			<a href="crearItems.php" class="btn btn-crear">Crear Item</a>
            <?php
            // Construir URL para exportar con filtros actuales
            $export_params = [];
            if (!empty($fecha_inicio)) $export_params[] = "fecha_inicio=" . urlencode($fecha_inicio);
            if (!empty($fecha_fin))    $export_params[] = "fecha_fin="    . urlencode($fecha_fin);
            if (!empty($beneficio))    $export_params[] = "beneficio="    . urlencode($beneficio);
            $export_url = "exportar.php" . (!empty($export_params) ? "?" . implode("&", $export_params) : "");
            ?>
            <a href="<?= $export_url ?>" class="btn btn-exportar">Exportar a Excel</a>
        </div>

        <!-- Filtros -->
        <div class="filtros">
            <form method="GET" style="display: flex; flex-wrap: wrap; gap: 15px; align-items: flex-end; width: 100%;">
                <div>
                    <label for="fecha_inicio">Fecha Inicio:</label>
                    <input type="date" id="fecha_inicio" name="fecha_inicio" value="<?= htmlspecialchars($fecha_inicio) ?>">
                </div>
                <div>
                    <label for="fecha_fin">Fecha Fin:</label>
                    <input type="date" id="fecha_fin" name="fecha_fin" value="<?= htmlspecialchars($fecha_fin) ?>">
                </div>
                <div>
                    <label for="beneficio">Tipo De Beneficio:</label>
                    <select id="beneficio" name="beneficio" style="padding:8px 12px; border:1px solid #ddd; border-radius:4px; font-size:14px;">
                        <option value="">-- Todos --</option>
                        <option value="Beneficio Pollo" <?= ($beneficio === 'Beneficio Pollo') ? 'selected' : '' ?>>Beneficio Pollo</option>
                        <option value="Beneficio Pavo"  <?= ($beneficio === 'Beneficio Pavo')  ? 'selected' : '' ?>>Beneficio Pavo</option>
                    </select>
                </div>
                <div>
                    <button type="submit">Filtrar</button>
                    <a href="consulta.php" class="btn btn-limpiar" style="text-decoration: none; display: inline-block; padding: 10px 20px; margin-left: 10px;">Limpiar</a>
                </div>
            </form>
        </div>

        <?php if (isset($_GET['success'])): ?>
            <div class="alert success">¡Entrega registrada correctamente!</div>
        <?php endif; ?>

        <!-- Información de registros -->
        <div class="info-registros">
            <?php
            $inicio_registro = $offset + 1;
            $fin_registro = min($offset + $registros_por_pagina, $total_registros);
            echo "Mostrando registros $inicio_registro - $fin_registro de $total_registros total";
            if (!empty($fecha_inicio) || !empty($fecha_fin) || !empty($beneficio)) {
                echo " (filtrados)";
            }
            ?>
        </div>

        <table>
            <thead>
                <tr>
                    <th>Fecha</th>
                    <th>Tipo Mov</th>
                    <th>Beneficio</th>
                    <th>Proceso</th>
                    <th>Ítem</th>
                    <th>Descripción</th>
                    <th>Cantidad</th>
                    <th>Área</th>
                    <th>Observaciones</th>
                    <th>Entrega</th>
                    <th>Recibe</th>
                    <th>Acciones</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($result->num_rows > 0): ?>
                    <?php while ($row = $result->fetch_assoc()): ?>
                        <tr>
                            <td><?= date('d/m/Y H:i', strtotime($row['fecha'])) ?></td>
                            <td><?= htmlspecialchars($row['tipoMV']) ?></td>
                            <td><?= htmlspecialchars($row['beneficio']) ?></td>
                            <td><?= htmlspecialchars($row['proceso']) ?></td>
                            <td><?= htmlspecialchars($row['item']) ?></td>
                            <td><?= htmlspecialchars($row['descripcion']) ?></td>
                            <td><?= htmlspecialchars($row['cantidad']) ?></td>
                            <td><?= htmlspecialchars($row['area']) ?></td>
                            <td><?= htmlspecialchars($row['observacion']) ?></td>
                            <td><img src="<?= htmlspecialchars($row['firma_entrega']) ?>" alt="Firma quien entrega" class="firma"></td>
                            <td><img src="<?= htmlspecialchars($row['firma_recibe']) ?>" alt="Firma quien recibe" class="firma"></td>
                            <td>
                                <a href="generar_pdf.php?id=<?= $row['id'] ?>" class="btn btn-pdf" target="_blank">PDF</a>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="11" style="text-align: center; padding: 20px; color: #666;">
                            No se encontraron registros con los filtros aplicados.
                        </td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>

        <!-- Paginación -->
        <?php if ($total_paginas > 1): ?>
            <div class="paginacion">
                <?php
                // Construir parámetros para mantener filtros en paginación
                $url_params = [];
                if (!empty($fecha_inicio)) $url_params[] = "fecha_inicio=" . urlencode($fecha_inicio);
                if (!empty($fecha_fin))    $url_params[] = "fecha_fin="    . urlencode($fecha_fin);
                if (!empty($beneficio))    $url_params[] = "beneficio="    . urlencode($beneficio);
                $base_url = "consulta.php" . (!empty($url_params) ? "?" . implode("&", $url_params) . "&" : "?");
                ?>

                <!-- Botón Anterior -->
                <?php if ($pagina > 1): ?>
                    <a href="<?= $base_url ?>pagina=<?= $pagina - 1 ?>">&laquo; Anterior</a>
                <?php else: ?>
                    <span class="disabled">&laquo; Anterior</span>
                <?php endif; ?>

                <!-- Números de página -->
                <?php
                $inicio_pag = max(1, $pagina - 2);
                $fin_pag = min($total_paginas, $pagina + 2);

                if ($inicio_pag > 1) {
                    echo '<a href="' . $base_url . 'pagina=1">1</a>';
                    if ($inicio_pag > 2) echo '<span>...</span>';
                }

                for ($i = $inicio_pag; $i <= $fin_pag; $i++) {
                    if ($i == $pagina) {
                        echo '<span class="actual">' . $i . '</span>';
                    } else {
                        echo '<a href="' . $base_url . 'pagina=' . $i . '">' . $i . '</a>';
                    }
                }

                if ($fin_pag < $total_paginas) {
                    if ($fin_pag < $total_paginas - 1) echo '<span>...</span>';
                    echo '<a href="' . $base_url . 'pagina=' . $total_paginas . '">' . $total_paginas . '</a>';
                }
                ?>

                <!-- Botón Siguiente -->
                <?php if ($pagina < $total_paginas): ?>
                    <a href="<?= $base_url ?>pagina=<?= $pagina + 1 ?>">Siguiente &raquo;</a>
                <?php else: ?>
                    <span class="disabled">Siguiente &raquo;</span>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </div>

    <!-- Footer con copyright -->
    <div class="footer">
        <div class="copyright">
            &#169; Avicampo <?php echo date('Y'); ?>
        </div>
    </div>
</body>

</html>