117 lines
3.3 KiB
PHP
117 lines
3.3 KiB
PHP
<?php
|
|
session_start();
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
require_once 'db/config.php';
|
|
require_once 'vendor/autoload.php';
|
|
|
|
use Shuchkin\SimpleXLSXGen;
|
|
|
|
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, p.producto, p.cantidad
|
|
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);
|
|
|
|
$data = [];
|
|
// Header
|
|
$data[] = [
|
|
'DESTINATARIO (DOC)',
|
|
'TELF. DESTINATARIO',
|
|
'CONTACTO (DOC)',
|
|
'TELF. CONTACTO',
|
|
'NRO GRR',
|
|
'ORIGEN',
|
|
'DESTINO',
|
|
'MERCADERIA',
|
|
'ALTO',
|
|
'ANCHO',
|
|
'LARGO',
|
|
'PESO',
|
|
'CANTIDAD'
|
|
];
|
|
|
|
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);
|
|
}
|
|
|
|
$destino_raw = str_ireplace('shalom:', '', $pedido['sede_envio'] ?? '');
|
|
|
|
// Buscamos la última parte después del slash
|
|
$last_slash_pos = mb_strrpos($destino_raw, '/');
|
|
if ($last_slash_pos !== false) {
|
|
$destino = mb_substr($destino_raw, $last_slash_pos + 1);
|
|
} else {
|
|
$destino = $destino_raw;
|
|
}
|
|
|
|
// Solo quitamos UN espacio al inicio y UN espacio al final si existen,
|
|
// para respetar espacios dobles internos o intencionales.
|
|
if (str_starts_with($destino, ' ')) {
|
|
$destino = substr($destino, 1);
|
|
}
|
|
if (str_ends_with($destino, ' ')) {
|
|
$destino = substr($destino, 0, -1);
|
|
}
|
|
|
|
// Convertir a mayúsculas
|
|
$destino = mb_strtoupper($destino, 'UTF-8');
|
|
|
|
$data[] = [
|
|
(string)$dni,
|
|
(string)$celular,
|
|
'',
|
|
'',
|
|
'',
|
|
'AV MEXICO CO',
|
|
$destino,
|
|
'PAQUETE XXS', // MERCADERIA
|
|
'0.1', // ALTO
|
|
'0.1', // ANCHO
|
|
'0.1', // LARGO
|
|
'0.1', // PESO
|
|
'1' // CANTIDAD
|
|
];
|
|
}
|
|
|
|
$filename = "masivo_shalom";
|
|
if ($type === 'terrestre') $filename .= "_terrestre";
|
|
if ($type === 'aereo') $filename .= "_aereo";
|
|
$filename .= "_" . date('Y-m-d') . ".xlsx";
|
|
|
|
SimpleXLSXGen::fromArray($data)->downloadAs($filename);
|
|
exit;
|
|
|
|
} catch (Exception $e) {
|
|
header('HTTP/1.1 500 Internal Server Error');
|
|
echo 'Error al generar el reporte.';
|
|
}
|
|
?>
|