281 lines
12 KiB
PHP
281 lines
12 KiB
PHP
<?php
|
|
$adminSection = 'qrs';
|
|
$pageTitle = 'Códigos QR';
|
|
$pageHeading = 'Códigos QR';
|
|
|
|
require_once __DIR__ . '/../includes/admin_bootstrap.php';
|
|
require_once __DIR__ . '/../config/database.php';
|
|
|
|
$db = getDB();
|
|
$outputDir = qrOutputDir();
|
|
$moduloFiltro = (int) ($_GET['modulo_id'] ?? 0);
|
|
$buscar = trim($_GET['q'] ?? '');
|
|
$soloPendientes = isset($_GET['pendientes']);
|
|
|
|
$sql = '
|
|
SELECT u.id, u.codigo, u.nombre AS ubicacion, u.modulo_id,
|
|
m.nombre AS modulo
|
|
FROM ubicaciones u
|
|
JOIN modulos m ON m.id = u.modulo_id
|
|
WHERE u.activo = 1
|
|
';
|
|
$params = [];
|
|
if ($moduloFiltro > 0) {
|
|
$sql .= ' AND u.modulo_id = ?';
|
|
$params[] = $moduloFiltro;
|
|
}
|
|
if ($buscar !== '') {
|
|
$sql .= ' AND (u.nombre LIKE ? OR u.codigo LIKE ? OR m.nombre LIKE ?)';
|
|
$params[] = '%' . $buscar . '%';
|
|
$params[] = '%' . $buscar . '%';
|
|
$params[] = '%' . $buscar . '%';
|
|
}
|
|
$sql .= ' ORDER BY u.codigo';
|
|
$stmt = $db->prepare($sql);
|
|
$stmt->execute($params);
|
|
$ubicaciones = $stmt->fetchAll();
|
|
|
|
$allForMap = $db->query('
|
|
SELECT u.codigo, m.nombre AS modulo, u.nombre AS ubicacion
|
|
FROM ubicaciones u JOIN modulos m ON m.id = u.modulo_id
|
|
WHERE u.activo = 1 ORDER BY u.codigo
|
|
')->fetchAll();
|
|
$filenameMap = buildQrFilenameMap($allForMap);
|
|
|
|
$qrList = [];
|
|
$qrGenerados = 0;
|
|
foreach ($ubicaciones as $u) {
|
|
$filename = $filenameMap[$u['codigo']] ?? null;
|
|
$path = $filename ? qrFilePathForUbicacion($u['modulo'], $u['ubicacion'], $u['codigo'], $filenameMap) : null;
|
|
$exists = $path !== null;
|
|
if ($exists) {
|
|
$qrGenerados++;
|
|
}
|
|
if ($soloPendientes && $exists) {
|
|
continue;
|
|
}
|
|
$qrList[] = [
|
|
'id' => $u['id'],
|
|
'codigo' => $u['codigo'],
|
|
'modulo' => $u['modulo'],
|
|
'ubicacion'=> $u['ubicacion'],
|
|
'filename' => $filename,
|
|
'url' => $path ? BASE_URL . '/output/' . rawurlencode($filename) : null,
|
|
'exists' => $exists,
|
|
];
|
|
}
|
|
|
|
$totalUbicaciones = (int) $db->query('SELECT COUNT(*) FROM ubicaciones WHERE activo = 1')->fetchColumn();
|
|
$totalQrFiles = 0;
|
|
if (is_dir($outputDir)) {
|
|
$totalQrFiles = count(array_filter(scandir($outputDir), fn($f) => str_ends_with($f, '.png')));
|
|
}
|
|
$modulos = $db->query('SELECT id, nombre FROM modulos WHERE activo = 1 ORDER BY nombre')->fetchAll();
|
|
$qrDataExists = file_exists(dirname(__DIR__) . '/qr_data.csv');
|
|
$logoExists = file_exists(dirname(__DIR__) . '/NEM.png');
|
|
$flash = getFlash();
|
|
|
|
$bulkUrl = 'download_qr.php?bulk=1' . ($moduloFiltro ? '&modulo_id=' . $moduloFiltro : '');
|
|
$filterQuery = http_build_query(array_filter([
|
|
'modulo_id' => $moduloFiltro ?: null,
|
|
'q' => $buscar ?: null,
|
|
'pendientes'=> $soloPendientes ? '1' : null,
|
|
]));
|
|
|
|
$topbarActions = '
|
|
<a href="export_qr.php" class="btn btn-outline-secondary btn-sm">
|
|
<span class="material-symbols-outlined align-middle" style="font-size:18px">table</span> CSV
|
|
</a>
|
|
<a href="' . h($bulkUrl) . '" class="btn btn-outline-primary btn-sm">
|
|
<span class="material-symbols-outlined align-middle" style="font-size:18px">folder_zip</span> ZIP masivo
|
|
</a>
|
|
<form method="post" action="generate_qrs.php" class="d-inline" onsubmit="return confirm(\'¿Generar todos los QR? Puede tardar varios minutos.\');">
|
|
<button type="submit" class="btn btn-primary btn-sm">
|
|
<span class="material-symbols-outlined align-middle" style="font-size:18px">qr_code_2</span> Generar todos
|
|
</button>
|
|
</form>
|
|
';
|
|
|
|
$extraJs = 'admin-qrs.js';
|
|
require_once __DIR__ . '/../includes/admin_header.php';
|
|
?>
|
|
|
|
<?php if ($flash): ?>
|
|
<div class="alert alert-<?= h($flash['type']) ?> alert-dismissible fade show">
|
|
<?= h($flash['message']) ?>
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="row g-3 mb-4">
|
|
<div class="col-md-3">
|
|
<div class="card stat-card h-100">
|
|
<div class="card-body">
|
|
<div class="text-muted small">Ubicaciones activas</div>
|
|
<div class="h4 mb-0"><?= number_format($totalUbicaciones) ?></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<div class="card stat-card h-100">
|
|
<div class="card-body">
|
|
<div class="text-muted small">QR generados</div>
|
|
<div class="h4 mb-0 <?= $qrGenerados >= count($ubicaciones) && count($ubicaciones) > 0 ? 'text-success' : 'text-warning' ?>">
|
|
<?= number_format($qrGenerados) ?> <span class="fs-6 text-muted">/ <?= count($ubicaciones) ?> filtradas</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<div class="card stat-card h-100">
|
|
<div class="card-body">
|
|
<div class="text-muted small">Archivos en output/</div>
|
|
<div class="h4 mb-0"><?= number_format($totalQrFiles) ?></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<div class="card stat-card h-100">
|
|
<div class="card-body d-flex flex-column justify-content-center gap-1">
|
|
<span><?= $qrDataExists ? '<span class="badge bg-success">CSV listo</span>' : '<span class="badge bg-danger">CSV falta</span>' ?></span>
|
|
<span><?= $logoExists ? '<span class="badge bg-success">Logo NEM.png</span>' : '<span class="badge bg-warning">Sin logo</span>' ?></span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card mb-4">
|
|
<div class="card-body">
|
|
<form method="get" class="row g-3 align-items-end">
|
|
<div class="col-md-4">
|
|
<label class="form-label">Módulo</label>
|
|
<select name="modulo_id" class="form-select">
|
|
<option value="">Todos los módulos</option>
|
|
<?php foreach ($modulos as $m): ?>
|
|
<option value="<?= (int) $m['id'] ?>" <?= $moduloFiltro === (int) $m['id'] ? 'selected' : '' ?>>
|
|
<?= h($m['nombre']) ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label">Buscar</label>
|
|
<input type="text" name="q" class="form-control" value="<?= h($buscar) ?>" placeholder="Código, habitación o módulo">
|
|
</div>
|
|
<div class="col-md-2">
|
|
<div class="form-check mt-4">
|
|
<input class="form-check-input" type="checkbox" name="pendientes" id="pendientes" value="1" <?= $soloPendientes ? 'checked' : '' ?>>
|
|
<label class="form-check-label" for="pendientes">Solo pendientes</label>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-2 d-flex gap-2">
|
|
<button type="submit" class="btn btn-primary flex-grow-1">Filtrar</button>
|
|
<?php if ($filterQuery): ?>
|
|
<a href="qrs.php" class="btn btn-outline-secondary" title="Limpiar filtros">
|
|
<span class="material-symbols-outlined" style="font-size:20px">close</span>
|
|
</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-header d-flex flex-wrap justify-content-between align-items-center gap-2">
|
|
<span><?= count($qrList) ?> ubicación(es)</span>
|
|
<div class="d-flex flex-wrap gap-2 align-items-center">
|
|
<button type="button" class="btn btn-sm btn-outline-secondary" id="btnSelectAll">Seleccionar todos</button>
|
|
<button type="button" class="btn btn-sm btn-primary" id="btnDownloadSelected" disabled>
|
|
<span class="material-symbols-outlined align-middle" style="font-size:18px">download</span>
|
|
Descargar seleccionados (ZIP)
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle mb-0 qr-table">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th style="width:40px"><input type="checkbox" class="form-check-input" id="checkAll" title="Seleccionar todos"></th>
|
|
<th>Código</th>
|
|
<th>Módulo</th>
|
|
<th>Habitación</th>
|
|
<th>Estado</th>
|
|
<th class="text-end">Acciones</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($qrList)): ?>
|
|
<tr><td colspan="6" class="text-center text-muted py-5">No hay ubicaciones con estos filtros.</td></tr>
|
|
<?php else: ?>
|
|
<?php foreach ($qrList as $qr): ?>
|
|
<tr class="<?= $qr['exists'] ? '' : 'qr-row-pending' ?>">
|
|
<td>
|
|
<?php if ($qr['exists']): ?>
|
|
<input type="checkbox" class="form-check-input qr-check" value="<?= h($qr['codigo']) ?>">
|
|
<?php endif; ?>
|
|
</td>
|
|
<td><code class="badge-codigo"><?= h($qr['codigo']) ?></code></td>
|
|
<td><?= h($qr['modulo']) ?></td>
|
|
<td><?= h($qr['ubicacion']) ?></td>
|
|
<td>
|
|
<?php if ($qr['exists']): ?>
|
|
<span class="badge bg-success-subtle text-success border border-success-subtle">Generado</span>
|
|
<?php else: ?>
|
|
<span class="badge bg-warning-subtle text-warning-emphasis border border-warning-subtle">Pendiente</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td class="text-end text-nowrap">
|
|
<?php if ($qr['exists']): ?>
|
|
<button type="button" class="btn btn-sm btn-outline-primary btn-qr-preview"
|
|
data-codigo="<?= h($qr['codigo']) ?>"
|
|
data-modulo="<?= h($qr['modulo']) ?>"
|
|
data-ubicacion="<?= h($qr['ubicacion']) ?>"
|
|
data-url="<?= h($qr['url']) ?>"
|
|
data-filename="<?= h($qr['filename']) ?>">
|
|
<span class="material-symbols-outlined align-middle" style="font-size:18px">visibility</span>
|
|
</button>
|
|
<a href="download_qr.php?codigo=<?= urlencode($qr['codigo']) ?>" class="btn btn-sm btn-primary" title="Descargar PNG">
|
|
<span class="material-symbols-outlined align-middle" style="font-size:18px">download</span>
|
|
</a>
|
|
<?php else: ?>
|
|
<span class="text-muted small">Genere QR primero</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="modal fade" id="qrPreviewModal" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="qrPreviewTitle">Vista previa QR</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body text-center">
|
|
<div class="qr-preview-box mb-3">
|
|
<img id="qrPreviewImg" src="" alt="QR" class="img-fluid">
|
|
</div>
|
|
<p class="mb-1"><code id="qrPreviewCodigo"></code></p>
|
|
<p class="text-muted small mb-0" id="qrPreviewCaption"></p>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cerrar</button>
|
|
<a href="#" id="qrPreviewDownload" class="btn btn-primary">
|
|
<span class="material-symbols-outlined align-middle" style="font-size:18px">download</span>
|
|
Descargar PNG
|
|
</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<form id="formBulkSelected" method="get" action="download_qr.php" class="d-none">
|
|
<input type="hidden" name="bulk" value="1">
|
|
</form>
|
|
|
|
<?php require_once __DIR__ . '/../includes/admin_footer.php'; ?>
|