34849-vm/download_shalom.php
2026-04-28 08:51:18 +00:00

91 lines
2.8 KiB
PHP

<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
require_once 'db/config.php';
try {
$pdo = db();
$type = $_GET['type'] ?? 'all';
// Filtramos solo los pedidos que están en estado 'ROTULADO 📦'
$query = "
SELECT p.dni_cliente, p.celular, p.sede_envio, p.agencia
FROM pedidos p
WHERE p.estado = 'ROTULADO 📦'
";
if ($type === 'terrestre') {
$query .= " AND p.sede_envio NOT LIKE '%IQUITOS%' ";
} elseif ($type === 'aereo') {
$query .= " AND p.sede_envio LIKE '%IQUITOS%' ";
}
$query .= " ORDER BY p.id DESC";
$stmt = $pdo->prepare($query);
$stmt->execute();
$pedidos = $stmt->fetchAll(PDO::FETCH_ASSOC);
$filename = "masivo_shalom";
if ($type === 'terrestre') $filename .= "_terrestre";
if ($type === 'aereo') $filename .= "_aereo";
$filename .= ".xls";
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Pragma: no-cache');
header('Expires: 0');
$output = '<html><head><meta charset="utf-8">
<style>
.text { mso-number-format:"\@"; }
</style>
</head><body>';
$output .= '<table border="1">';
$output .= '<thead>';
$output .= '<tr>';
$output .= '<th>DESTINATARIO (DOC)</th>';
$output .= '<th>TELF. DESTINATARIO</th>';
$output .= '<th>CONTACTO (DOC)</th>';
$output .= '<th>TELF. CONTACTO</th>';
$output .= '<th>NRO GRR</th>';
$output .= '<th>ORIGEN</th>';
$output .= '<th>DESTINO</th>';
$output .= '</tr>';
$output .= '</thead>';
$output .= '<tbody>';
foreach ($pedidos as $pedido) {
// Limpiar el DNI: solo números
$dni = preg_replace('/[^0-9]/', '', $pedido['dni_cliente'] ?? '');
$celular = htmlspecialchars($pedido['celular'] ?? '');
$destino_raw = str_ireplace('shalom:', '', $pedido['sede_envio'] ?? '');
$parts = explode('/', $destino_raw);
$destino = trim(end($parts));
$destino = htmlspecialchars($destino);
$output .= '<tr>';
$output .= '<td class="text">' . $dni . '</td>'; // Solo el número de DNI con formato texto
$output .= '<td class="text">' . $celular . '</td>'; // Teléfono con formato texto
$output .= '<td></td>'; // Contacto vacío
$output .= '<td></td>'; // Telf Contacto vacío
$output .= '<td></td>'; // NRO GRR vacío
$output .= '<td>AV MEXICO CO</td>'; // ORIGEN fijo
$output .= '<td>' . $destino . '</td>'; // Destino
$output .= '</tr>';
}
$output .= '</tbody>';
$output .= '</table>';
$output .= '</body></html>';
echo $output;
} catch (Exception $e) {
header('HTTP/1.1 500 Internal Server Error');
echo 'Error al generar el reporte.';
}
?>