File "exportar.php"

Full Path: C:/wamp64/www/loteo1/exportar.php
File size: 3.57 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'] : '';

// Construir la consulta con filtros
$where_clause = "";
$params = [];
$types = "";

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

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

if (!empty($params)) {
    $stmt = $conn->prepare($query);
    $stmt->bind_param($types, ...$params);
    $stmt->execute();
    $result = $stmt->get_result();
} else {
    $result = $conn->query($query);
}
?>
<!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>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['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)): ?>
    <br>
    <table>
        <tr>
            <td><strong>Filtro aplicado:</strong></td>
            <td>
                <?php
                if (!empty($fecha_inicio) && !empty($fecha_fin)) {
                    echo "Desde " . date('d/m/Y', strtotime($fecha_inicio)) . " hasta " . date('d/m/Y', strtotime($fecha_fin));
                } elseif (!empty($fecha_inicio)) {
                    echo "Desde " . date('d/m/Y', strtotime($fecha_inicio));
                } elseif (!empty($fecha_fin)) {
                    echo "Hasta " . date('d/m/Y', strtotime($fecha_fin));
                }
                ?>
            </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>