264 lines
10 KiB
PHP
264 lines
10 KiB
PHP
<?php
|
|
$pageTitle = "Registro de Entrada por Unidad";
|
|
require_once 'layout_header.php';
|
|
require_once 'db/config.php';
|
|
|
|
$error_page_load = '';
|
|
|
|
// Obtener sedes para el dropdown
|
|
$sedes = [];
|
|
try {
|
|
$pdo = db();
|
|
$sedes_stmt = $pdo->query("SELECT id, nombre FROM sedes ORDER BY nombre ASC");
|
|
$sedes = $sedes_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
} catch (PDOException $e) {
|
|
$error_page_load = "Error al cargar las sedes: " . $e->getMessage();
|
|
}
|
|
?>
|
|
|
|
<div class="container mt-4">
|
|
<div class="row">
|
|
<div class="col-lg-6 mx-auto">
|
|
|
|
<!-- Contenedor para notificaciones (toasts) -->
|
|
<div id="notification-container" class="position-fixed top-0 end-0 p-3" style="z-index: 1100"></div>
|
|
|
|
<?php if (!empty($error_page_load)): ?>
|
|
<div class="alert alert-danger" role="alert">
|
|
<?php echo htmlspecialchars($error_page_load); ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<i class="fa fa-barcode"></i> Registro de Entrada por Unidad
|
|
</div>
|
|
<div class="card-body">
|
|
<form id="scan-form" onsubmit="return false;">
|
|
<div class="mb-3">
|
|
<label for="sede" class="form-label">Sede de Destino</label>
|
|
<select class="form-select" id="sede" name="sede_id" required>
|
|
<option value="">Seleccione una sede</option>
|
|
<?php foreach ($sedes as $sede): ?>
|
|
<option value="<?php echo htmlspecialchars($sede['id']); ?>">
|
|
<?php echo htmlspecialchars($sede['nombre']); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<hr>
|
|
<div class="mb-3">
|
|
<label for="barcode-input" class="form-label">Esperando código de barras...</label>
|
|
<div class="input-group">
|
|
<input type="text" class="form-control form-control-lg" id="barcode-input" placeholder="Escanee el producto aquí" autofocus>
|
|
<button class="btn btn-outline-secondary" type="button" data-bs-toggle="modal" data-bs-target="#camera-modal">
|
|
<i class="fa fa-camera"></i>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div id="scanned-product-info" class="mt-3" style="display: none;">
|
|
<p><strong>Producto escaneado:</strong> <span id="scanned-code"></span></p>
|
|
<div class="d-grid gap-2">
|
|
<button type="button" id="accept-btn" class="btn btn-success"> <i class="fa fa-check-circle"></i> Aceptar y Registrar Entrada</button>
|
|
<button type="button" id="cancel-btn" class="btn btn-danger"> <i class="fa fa-times-circle"></i> Cancelar</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal para la cámara -->
|
|
<div class="modal fade" id="camera-modal" tabindex="-1" aria-labelledby="camera-modal-label" aria-hidden="true">
|
|
<div class="modal-dialog modal-lg">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="camera-modal-label">Escanear Código de Barras</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div id="reader" width="100%"></div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cerrar</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://unpkg.com/html5-qrcode@2.3.8/html5-qrcode.min.js"></script>
|
|
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', (event) => {
|
|
// --- CONFIGURACIÓN INICIAL ---
|
|
if (typeof bootstrap === 'undefined') {
|
|
console.error('Bootstrap no está cargado.');
|
|
return;
|
|
}
|
|
|
|
const barcodeInput = document.getElementById('barcode-input');
|
|
const sedeSelect = document.getElementById('sede');
|
|
const scannedProductInfo = document.getElementById('scanned-product-info');
|
|
const scannedCodeSpan = document.getElementById('scanned-code');
|
|
const acceptBtn = document.getElementById('accept-btn');
|
|
const cancelBtn = document.getElementById('cancel-btn');
|
|
|
|
let processing = false;
|
|
let lastScannedCode = null;
|
|
|
|
// --- LÓGICA DE SONIDO (WEB AUDIO API) ---
|
|
let audioCtx;
|
|
function wakeUpAudio() {
|
|
if (!audioCtx) {
|
|
try {
|
|
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
|
} catch (e) { console.error("Web Audio API no es soportada.", e); return; }
|
|
}
|
|
if (audioCtx.state === 'suspended') {
|
|
audioCtx.resume();
|
|
}
|
|
}
|
|
document.body.addEventListener('click', wakeUpAudio, { once: true });
|
|
document.body.addEventListener('touchstart', wakeUpAudio, { once: true });
|
|
|
|
function playBeep() {
|
|
if (!audioCtx || audioCtx.state !== 'running') {
|
|
wakeUpAudio();
|
|
if (!audioCtx || audioCtx.state !== 'running') return;
|
|
}
|
|
const oscillator = audioCtx.createOscillator();
|
|
const gainNode = audioCtx.createGain();
|
|
oscillator.connect(gainNode);
|
|
gainNode.connect(audioCtx.destination);
|
|
gainNode.gain.value = 0.1;
|
|
oscillator.frequency.value = 880;
|
|
oscillator.type = 'sine';
|
|
oscillator.start();
|
|
setTimeout(() => oscillator.stop(), 150);
|
|
}
|
|
|
|
// --- INICIALIZACIÓN DE CÁMARA ---
|
|
const cameraModal = document.getElementById('camera-modal');
|
|
let html5QrCode;
|
|
|
|
function onScanSuccess(decodedText, decodedResult) {
|
|
const modal = bootstrap.Modal.getInstance(cameraModal);
|
|
if(modal) modal.hide();
|
|
|
|
barcodeInput.value = decodedText;
|
|
const changeEvent = new Event('change');
|
|
barcodeInput.dispatchEvent(changeEvent);
|
|
}
|
|
|
|
cameraModal.addEventListener('shown.bs.modal', function () {
|
|
wakeUpAudio();
|
|
html5QrCode = new Html5Qrcode("reader");
|
|
const config = { fps: 10, qrbox: { width: 250, height: 250 } };
|
|
html5QrCode.start({ facingMode: "environment" }, config, onScanSuccess, (e)=>{})
|
|
.catch(err => alert("Error al iniciar la cámara. Asegúrate de dar permisos."));
|
|
});
|
|
|
|
cameraModal.addEventListener('hidden.bs.modal', function () {
|
|
if (html5QrCode && html5QrCode.isScanning) {
|
|
html5QrCode.stop().catch(err => {});
|
|
}
|
|
});
|
|
|
|
// --- FUNCIONES DE AYUDA (Notificaciones) ---
|
|
function showNotification(message, isSuccess) {
|
|
const container = document.getElementById('notification-container');
|
|
if (!container) return;
|
|
const toastId = 'toast-' + Date.now();
|
|
const toastHTML = `
|
|
<div id="${toastId}" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
|
|
<div class="toast-header ${isSuccess ? 'bg-success text-white' : 'bg-danger text-white'}">
|
|
<strong class="me-auto">${isSuccess ? 'Éxito' : 'Error'}</strong>
|
|
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="toast" aria-label="Close"></button>
|
|
</div>
|
|
<div class="toast-body">${message}</div>
|
|
</div>`;
|
|
container.insertAdjacentHTML('beforeend', toastHTML);
|
|
const toast = new bootstrap.Toast(document.getElementById(toastId), { delay: 5000 });
|
|
toast.show();
|
|
document.getElementById(toastId).addEventListener('hidden.bs.toast', e => e.target.remove());
|
|
}
|
|
|
|
function resetScanner() {
|
|
lastScannedCode = null;
|
|
barcodeInput.value = '';
|
|
barcodeInput.disabled = false;
|
|
scannedProductInfo.style.display = 'none';
|
|
processing = false;
|
|
barcodeInput.focus();
|
|
}
|
|
|
|
// --- LÓGICA PRINCIPAL DEL ESCÁNER ---
|
|
barcodeInput.addEventListener('change', function() {
|
|
const barcodeValue = this.value.trim();
|
|
if (barcodeValue === '') return;
|
|
|
|
playBeep();
|
|
|
|
lastScannedCode = barcodeValue;
|
|
scannedCodeSpan.textContent = barcodeValue;
|
|
scannedProductInfo.style.display = 'block';
|
|
this.disabled = true;
|
|
acceptBtn.focus(); // Mover foco al botón de aceptar
|
|
});
|
|
|
|
// --- LÓGICA DE BOTONES ACEPTAR/CANCELAR ---
|
|
cancelBtn.addEventListener('click', function() {
|
|
resetScanner();
|
|
});
|
|
|
|
acceptBtn.addEventListener('click', function() {
|
|
if (processing || !lastScannedCode) return;
|
|
|
|
processing = true;
|
|
|
|
const sedeId = sedeSelect.value;
|
|
if (!sedeId) {
|
|
showNotification("Por favor, seleccione una sede de destino.", false);
|
|
processing = false;
|
|
return;
|
|
}
|
|
|
|
const formData = new FormData();
|
|
formData.append('codigo_unico', lastScannedCode);
|
|
formData.append('sede_id', sedeId);
|
|
|
|
fetch('registrar_entrada_api.php', {
|
|
method: 'POST',
|
|
body: formData
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
showNotification(data.message, true);
|
|
} else {
|
|
throw new Error(data.message || 'Error desconocido al registrar la entrada.');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
showNotification(error.message, false);
|
|
})
|
|
.finally(() => {
|
|
resetScanner();
|
|
});
|
|
});
|
|
|
|
// Mantener el foco en el input principal cuando no se interactúa con otros elementos
|
|
barcodeInput.focus();
|
|
document.body.addEventListener('click', (e) => {
|
|
// No re-enfocar si se hace clic en inputs, botones, selects, o dentro de un modal
|
|
if (!e.target.closest('input, button, select, .modal')) {
|
|
barcodeInput.focus();
|
|
}
|
|
});
|
|
});
|
|
</script>
|
|
|
|
<?php require_once 'layout_footer.php'; ?>
|