From 94249317d0a9ebe6dd24b61d570bdf1678ae0a8e Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 5 Oct 2026 04:23:15 -0600 Subject: [PATCH] feat: Agregar opcion de nuevo producto rapido en caliente en mostrador POS --- public/js/pos.js | 114 +++++++++++++++++++++++++++++++++++++++++++++++ public/pos.html | 66 ++++++++++++++++++++++++--- server/server.js | 62 ++++++++++++++++++++++++++ 3 files changed, 237 insertions(+), 5 deletions(-) diff --git a/public/js/pos.js b/public/js/pos.js index 197a106..6a5873e 100644 --- a/public/js/pos.js +++ b/public/js/pos.js @@ -81,6 +81,7 @@ document.addEventListener('DOMContentLoaded', async () => { // La cámara se activa bajo demanda al cobrar o identificar, NO al entrar initSSE(); initPistolScanner(); + initQuickProductEvents(); }); // Cargar catálogo de productos @@ -110,7 +111,10 @@ function renderPosCategories() { `; } +let currentPosCatId = null; + function filterPosCat(catId, btn) { + currentPosCatId = catId; document.querySelectorAll('#posCategoriesBar .pos-tab-btn').forEach(b => b.classList.remove('active')); if (btn) btn.classList.add('active'); renderPosProducts(catId); @@ -137,6 +141,116 @@ function renderPosProducts(catId) { `).join(''); } +// ========================================== +// NUEVO PRODUCTO RÁPIDO EN CALIENTE (POS) +// ========================================== + +function openQuickProductModal() { + const modal = document.getElementById('modalProductoRapido'); + if (!modal) return; + modal.style.display = 'flex'; + const inputNom = document.getElementById('inputQuickProdNombre'); + const inputPre = document.getElementById('inputQuickProdPrecio'); + if (inputNom) { + inputNom.value = ''; + setTimeout(() => inputNom.focus(), 80); + } + if (inputPre) inputPre.value = ''; +} + +function closeQuickProductModal() { + const modal = document.getElementById('modalProductoRapido'); + if (modal) modal.style.display = 'none'; +} + +function initQuickProductEvents() { + const inputNom = document.getElementById('inputQuickProdNombre'); + const inputPre = document.getElementById('inputQuickProdPrecio'); + + if (inputNom) { + inputNom.addEventListener('keydown', (e) => { + if (e.key === 'Enter') { + e.preventDefault(); + if (inputPre) inputPre.focus(); + } + }); + } + + document.addEventListener('keydown', (e) => { + if (e.key === 'Escape') { + const modal = document.getElementById('modalProductoRapido'); + if (modal && modal.style.display !== 'none') { + closeQuickProductModal(); + } + } + }); +} + +async function saveQuickProduct(e) { + if (e) e.preventDefault(); + const inputNom = document.getElementById('inputQuickProdNombre'); + const inputPre = document.getElementById('inputQuickProdPrecio'); + const btn = document.getElementById('btnSubmitQuickProd'); + + const nombre = inputNom ? inputNom.value.trim() : ''; + const precio = inputPre ? parseInt(inputPre.value, 10) : 0; + + if (!nombre) { + alert('Ingresa el nombre del producto'); + if (inputNom) inputNom.focus(); + return; + } + if (!precio || isNaN(precio) || precio <= 0) { + alert('Ingresa un precio válido en colones'); + if (inputPre) inputPre.focus(); + return; + } + + if (btn) { + btn.disabled = true; + btn.innerHTML = '⏳ Guardando...'; + } + + try { + const res = await fetch('/api/productos/rapido', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ nombre, precio_colones: precio }) + }); + const data = await res.json(); + + if (!res.ok || !data.success || !data.producto) { + throw new Error(data.error || 'No se pudo guardar el producto'); + } + + const nuevoProd = data.producto; + + // Agregar al catálogo en memoria de la terminal de primero + const exists = posProducts.find(p => p.id === nuevoProd.id); + if (!exists) { + posProducts.unshift(nuevoProd); + } + + // Re-renderizar productos del mostrador + renderPosProducts(currentPosCatId); + + // Agregar de inmediato 1 unidad al carrito de cobro + addToPosCart(nuevoProd.id); + + // Cerrar modal + closeQuickProductModal(); + + if (window.sounds) window.sounds.playCoin(); + } catch (err) { + alert('Error al registrar producto: ' + err.message); + } finally { + if (btn) { + btn.disabled = false; + btn.innerHTML = '💾 Guardar y Cobrar ➔'; + } + } +} + // Carrito de mostrador function addToPosCart(prodId) { const prod = posProducts.find(p => p.id === prodId); diff --git a/public/pos.html b/public/pos.html index 4959881..3919114 100644 --- a/public/pos.html +++ b/public/pos.html @@ -100,8 +100,13 @@
-
- +
+
+ +
+
@@ -196,7 +201,12 @@
- Ítems en Mostrador +
+ Ítems en Mostrador + +
@@ -318,8 +328,54 @@
- + + + + - + diff --git a/server/server.js b/server/server.js index 97d3172..b47a540 100644 --- a/server/server.js +++ b/server/server.js @@ -1539,6 +1539,68 @@ app.get('/api/productos', (req, res) => { } }); +// Crear producto rápido en mostrador (en caliente) +app.post('/api/productos/rapido', (req, res) => { + try { + const { nombre, precio_colones, categoria_id } = req.body; + const cleanNombre = (nombre || '').trim(); + const precio = parseInt(precio_colones, 10); + + if (!cleanNombre) { + return res.status(400).json({ error: 'El nombre del producto es obligatorio' }); + } + if (isNaN(precio) || precio <= 0) { + return res.status(400).json({ error: 'El precio debe ser un monto válido mayor a ₡0' }); + } + + // Determinar categoría por defecto + let targetCatId = categoria_id; + if (!targetCatId) { + const catSnack = db.prepare("SELECT id FROM categorias WHERE LOWER(nombre) LIKE '%snack%' OR LOWER(nombre) LIKE '%fruta%' OR LOWER(nombre) LIKE '%vario%' ORDER BY id ASC LIMIT 1").get(); + if (catSnack) { + targetCatId = catSnack.id; + } else { + const catFirst = db.prepare("SELECT id FROM categorias ORDER BY id ASC LIMIT 1").get(); + targetCatId = catFirst ? catFirst.id : 1; + } + } + + const insertRes = db.prepare(` + INSERT INTO productos ( + categoria_id, nombre, descripcion, precio_colones, + icono, cumple_mep, disponible, permite_preorden, destacado + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) + `).run( + targetCatId, + cleanNombre, + 'Producto rápido registrado en mostrador', + precio, + '🥪', + 1, + 1, + 1, + 0 + ); + + const newId = insertRes.lastInsertRowid; + const nuevoProducto = db.prepare(` + SELECT p.*, c.nombre as categoria_nombre, c.icono as categoria_icono + FROM productos p + LEFT JOIN categorias c ON p.categoria_id = c.id + WHERE p.id = ? + `).get(newId); + + res.json({ + success: true, + mensaje: `Producto "${cleanNombre}" agregado al inventario`, + producto: nuevoProducto + }); + } catch (error) { + console.error('Error registrando producto rápido:', error); + res.status(500).json({ error: error.message }); + } +}); + // ========================================== // 3. ÓRDENES (MOSTRADOR Y PRE-ÓRDENES) // ==========================================