40097-vm/download_shalom.php
2026-05-14 22:38:03 +00:00

105 lines
3.4 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 📦' y son de la agencia 'SHALOM'
$query = "
SELECT p.dni_cliente, p.celular, p.sede_envio, p.agencia
FROM pedidos p
WHERE p.estado = 'ROTULADO 📦'
AND p.agencia LIKE '%SHALOM%'
";
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:"\@"; }
.preserve-space { white-space: pre; }
</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'] ?? '');
// Limpiar el celular: solo números
$celular = preg_replace('/[^0-9]/', '', $pedido['celular'] ?? '');
// Eliminar prefijo '51' si existe al inicio y el número es largo (ej. 51987654321)
if (str_starts_with($celular, '51') && strlen($celular) > 9) {
$celular = substr($celular, 2);
}
$celular = htmlspecialchars($celular);
$destino_raw = str_ireplace('shalom:', '', $pedido['sede_envio'] ?? '');
$parts = explode('/', $destino_raw);
$destino = trim(end($parts));
// Convertir a mayúsculas
$destino = mb_strtoupper($destino, 'UTF-8');
$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 class="preserve-space">' . $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.';
}
?>