File "exportar.php"

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

<?php
require_once 'connection.php';

// Configurar headers para descarga de Excel
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="entregas_loteo_' . date('Y-m-d_H-i-s') . '.xls"');
header('Cache-Control: max-age=0');

$conn = connection();

// Obtener filtros de fecha de los parámetros GET
$fecha_inicio = isset($_GET['fecha_inicio']) ? $_GET['fecha_inicio'] : '';
$fecha_fin    = isset($_GET['fecha_fin'])    ? $_GET['fecha_fin']    : '';
$beneficio    = isset($_GET['beneficio'])    ? $_GET['beneficio']    : '';

// 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) : "";

// Ejecutar consulta
$query = "SELECT * FROM entregas" . $where_clause . " ORDER BY fecha DESC";

$stmt = $conn->prepare($query);
if (!empty($params)) {
    $stmt->bind_param($types, ...$params);
}
$stmt->execute();
$result = $stmt->get_result();
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Exportar Entregas</title>
</head>
<body>
    <table border="1">
        <tr style="background-color: #f2f2f2; font-weight: bold;">
            <th>ID</th>
            <th>Fecha</th>
            <th>Tipo Movimiento</th>
            <th>Beneficio</th>
            <th>Proceso</th>
            <th>Ítem</th>
            <th>Descripción</th>
            <th>Cantidad</th>
            <th>Área</th>
            <th>Observaciones</th>
        </tr>
        <?php while ($row = $result->fetch_assoc()): ?>
        <tr>
            <td><?= $row['id'] ?></td>
            <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>
        </tr>
        <?php endwhile; ?>
    </table>
    
    <!-- Información del filtro aplicado -->
    <?php if (!empty($fecha_inicio) || !empty($fecha_fin) || !empty($beneficio)): ?>
    <br>
    <table>
        <tr>
            <td><strong>Filtro aplicado:</strong></td>
            <td>
                <?php
                $filtros_desc = [];
                if (!empty($fecha_inicio) && !empty($fecha_fin)) {
                    $filtros_desc[] = "Desde " . date('d/m/Y', strtotime($fecha_inicio)) . " hasta " . date('d/m/Y', strtotime($fecha_fin));
                } elseif (!empty($fecha_inicio)) {
                    $filtros_desc[] = "Desde " . date('d/m/Y', strtotime($fecha_inicio));
                } elseif (!empty($fecha_fin)) {
                    $filtros_desc[] = "Hasta " . date('d/m/Y', strtotime($fecha_fin));
                }
                if (!empty($beneficio)) {
                    $filtros_desc[] = "Beneficio: " . htmlspecialchars($beneficio);
                }
                echo implode(' | ', $filtros_desc);
                ?>
            </td>
        </tr>
        <tr>
            <td><strong>Fecha de exportación:</strong></td>
            <td><?= date('d/m/Y H:i:s') ?></td>
        </tr>
        <tr>
            <td><strong>Total de registros:</strong></td>
            <td><?= $result->num_rows ?></td>
        </tr>
    </table>
    <?php endif; ?>
</body>
</html>