40097-vm/imprimir_rotulos_grandes.php
2026-05-23 02:00:13 +00:00

272 lines
8.8 KiB
PHP

<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
require_once 'db/config.php';
$pdo = db();
// Fetch all products to have their prefixes ready
$stmt_products = $pdo->query("SELECT nombre, codigo_base FROM products");
$product_prefixes = [];
while ($row = $stmt_products->fetch(PDO::FETCH_ASSOC)) {
$product_prefixes[$row['nombre']] = $row['codigo_base'];
}
$user_id = $_SESSION['user_id'];
$user_role = $_SESSION['user_role'] ?? 'Asesor';
$selected_month = $_GET['mes'] ?? '';
$selected_year = $_GET['año'] ?? '';
$search_query = $_GET['q'] ?? '';
$is_today = isset($_GET['today']);
$sql = "SELECT p.*
FROM pedidos p
WHERE p.estado = 'ROTULADO 📦'";
$params = [];
if ($user_role === 'Asesor') {
$sql .= " AND p.asesor_id = ?";
$params[] = $user_id;
}
if (!empty($search_query)) {
$sql .= " AND (p.nombre_completo LIKE ? OR p.dni_cliente LIKE ? OR p.celular LIKE ?)";
$params[] = "%$search_query%";
$params[] = "%$search_query%";
$params[] = "%$search_query%";
}
if (!empty($selected_month)) {
$sql .= " AND MONTH(p.created_at) = ?";
$params[] = $selected_month;
}
if (!empty($selected_year)) {
$sql .= " AND YEAR(p.created_at) = ?";
$params[] = $selected_year;
}
if ($is_today) {
$sql .= " AND DATE(p.created_at) = CURDATE()";
}
$sql .= " ORDER BY p.created_at DESC";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$pedidos = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Imprimir Rótulos</title>
<style>
body {
margin: 0;
padding: 0;
font-family: 'Arial', sans-serif;
background-color: #f0f2f5;
}
.no-print {
padding: 20px;
text-align: center;
background: white;
border-bottom: 1px solid #ddd;
position: sticky;
top: 0;
z-index: 100;
}
.btn-print {
padding: 12px 24px;
background: #198754;
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 18px;
font-weight: bold;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}
.btn-print:hover {
background: #157347;
}
.label-page {
width: 96mm;
height: 75mm;
background: white;
margin: 10mm auto;
padding: 4mm;
box-sizing: border-box;
border: 1px solid #ccc;
display: flex;
flex-direction: column;
justify-content: space-between;
page-break-after: always;
color: black;
overflow: hidden;
}
.header {
text-align: center;
border-bottom: 3px solid black;
padding-bottom: 2mm;
}
.client-name {
font-size: 22pt;
font-weight: 900;
text-transform: uppercase;
margin: 0;
line-height: 1.1;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
.details {
font-size: 16pt;
line-height: 1.3;
font-weight: bold;
margin-top: 1mm;
}
.details-row {
display: flex;
justify-content: space-between;
margin-bottom: 1mm;
gap: 2mm;
}
.footer-msg {
text-align: center;
font-size: 14pt;
font-weight: 900;
border-top: 2px solid black;
padding-top: 2mm;
margin-top: auto;
}
@media print {
@page {
size: 96mm 75mm;
margin: 0;
}
body {
background: none;
margin: 0;
padding: 0;
}
.no-print {
display: none;
}
.label-page {
margin: 0;
border: none;
width: 96mm;
height: 75mm;
}
}
</style>
</head>
<body>
<div class="no-print">
<button class="btn-print" onclick="window.print()">IMPRIMIR RÓTULOS (<?php echo count($pedidos); ?>)</button>
<a href="pedidos.php" style="margin-left: 15px; text-decoration: none; color: #666; font-weight: bold;">← VOLVER</a>
</div>
<?php if (empty($pedidos)): ?>
<div style="text-align: center; margin-top: 50px;">
<h3>No hay pedidos rotulados para mostrar.</h3>
</div>
<?php endif; ?>
<?php foreach ($pedidos as $pedido):
$display_variants = [];
$found_in_notes = false;
// Try to parse from notas first
if (preg_match_all('/Detalle de productos: (.*)$/m', $pedido['notas'], $matches)) {
$last_match = end($matches[1]);
$details = explode(', ', $last_match);
foreach ($details as $detail) {
if (preg_match('/(.*) \(x(\d+)\)/', $detail, $d_matches)) {
$p_name = trim($d_matches[1]);
$p_qty = (int)$d_matches[2];
$prefix = $product_prefixes[$p_name] ?? 'S/V';
$display_variants[] = "($p_qty) $prefix";
$found_in_notes = true;
}
}
}
// Fallback if not found in notes
if (!$found_in_notes && !empty($pedido['producto'])) {
$names = explode(', ', $pedido['producto']);
$total_qty = (int)($pedido['cantidad'] ?: 1);
if (count($names) === 1) {
$prefix = $product_prefixes[trim($names[0])] ?? 'S/V';
$display_variants[] = "($total_qty) $prefix";
} else {
foreach ($names as $name) {
$prefix = $product_prefixes[trim($name)] ?? 'S/V';
$display_variants[] = "(1) $prefix";
}
}
}
$variante_text = !empty($display_variants) ? implode(' + ', $display_variants) : 'S/V';
?>
<div class="label-page">
<div class="header">
<h1 class="client-name"><?php
$full_name = trim($pedido['nombre_completo']);
$name_parts = array_filter(explode(' ', $full_name));
$display_name = implode(' ', array_slice($name_parts, 0, 2));
echo htmlspecialchars($display_name);
?></h1>
</div>
<div class="details">
<div class="details-row">
<span>VARIANTE: <?php echo htmlspecialchars($variante_text); ?></span>
</div>
<div class="details-row">
<span><?php
$dni_val = trim($pedido['dni_cliente'] ?: ($pedido['dni'] ?: 'N/A'));
// Remove existing DNI: prefix to standardize
$dni_clean = preg_replace('/^DNI:\s*/i', '', $dni_val);
echo 'DNI:' . htmlspecialchars($dni_clean);
?></span>
<span>CANT: <?php echo htmlspecialchars($pedido['cantidad'] ?: '1'); ?></span>
</div>
<div class="details-row">
<span><?php
$cel_val = trim($pedido['celular']);
// Remove CEL: prefix if exists, then remove +51, then trim again
$cel_clean = preg_replace('/^CEL:\s*/i', '', $cel_val);
$cel_clean = str_replace('+51', '', $cel_clean);
$cel_clean = trim($cel_clean);
echo 'CEL:' . htmlspecialchars($cel_clean);
?></span>
<span>AG: <?php echo htmlspecialchars($pedido['agencia'] ?: 'N/A'); ?></span>
</div>
<div class="details-row">
<span><?php
$sede_val = trim($pedido['sede_envio'] ?: 'N/A');
if ($sede_val !== 'N/A' && strpos($sede_val, ' / ') !== false) {
$parts = explode(' / ', $sede_val);
if (count($parts) > 1) {
$sede_val = trim($parts[0]) . ' / ' . trim(end($parts));
}
}
echo 'SEDE: ' . htmlspecialchars($sede_val);
?></span>
</div>
</div>
<div class="footer-msg">
MUCHAS GRACIAS POR SU COMPRA
</div>
</div>
<?php endforeach; ?>
</body>
</html>