feat: Agregar opcion de nuevo producto rapido en caliente en mostrador POS

This commit is contained in:
unknown
2026-10-05 04:23:15 -06:00
parent fe7dffbcdb
commit 94249317d0
3 changed files with 237 additions and 5 deletions
+114
View File
@@ -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 = '<span>⏳</span> 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 = '<span>💾</span> Guardar y Cobrar ➔';
}
}
}
// Carrito de mostrador
function addToPosCart(prodId) {
const prod = posProducts.find(p => p.id === prodId);
+59 -3
View File
@@ -100,9 +100,14 @@
<!-- VISTA 1: CATÁLOGO TÁCTIL DE PRODUCTOS (MOSTRADOR) -->
<div id="viewPosMostrador">
<div style="display: flex; gap: 8px; margin-bottom: 14px; overflow-x: auto;" id="posCategoriesBar">
<div style="display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 14px; flex-wrap: wrap;">
<div style="display: flex; gap: 8px; overflow-x: auto; flex: 1; min-width: 200px;" id="posCategoriesBar">
<!-- Filtros de categorías -->
</div>
<button type="button" onclick="openQuickProductModal()" style="background: linear-gradient(135deg, #10b981, #059669); color: white; border: none; padding: 7px 14px; border-radius: 10px; font-weight: 800; font-size: 0.8rem; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; box-shadow: 0 2px 10px rgba(16, 185, 129, 0.35); flex-shrink: 0; white-space: nowrap;">
<span>⚡</span> + Nuevo Rápido
</button>
</div>
<div class="pos-products-grid" id="posProductsGrid">
<!-- Tarjetas táctiles de productos -->
@@ -196,7 +201,12 @@
<!-- LISTA DE PRODUCTOS SELECCIONADOS EN CAJA -->
<div style="margin-top: 14px;">
<div style="display: flex; justify-content: space-between; align-items: center; font-size: 0.82rem; font-weight: 700; color: #94a3b8; border-bottom: 1px solid #334155; padding-bottom: 6px;">
<div style="display: flex; align-items: center; gap: 8px;">
<span>Ítems en Mostrador</span>
<button type="button" onclick="openQuickProductModal()" style="background: #064e3b; color: #34d399; border: 1px solid #059669; border-radius: 6px; font-size: 0.72rem; font-weight: 800; padding: 2px 8px; cursor: pointer; display: inline-flex; align-items: center; gap: 4px;">
<span>⚡</span> + Rápido
</button>
</div>
<button onclick="clearPosCart()" style="background: none; border: none; color: #ef4444; font-size: 0.75rem; font-weight: 700; cursor: pointer; padding: 2px 6px;">Limpiar</button>
</div>
<div class="pos-cart-list" id="posCartList">
@@ -318,8 +328,54 @@
</div>
</div>
<script src="/js/sounds.js?v=8.5"></script>
<!-- MODAL: NUEVO PRODUCTO RÁPIDO EN CALIENTE -->
<div id="modalProductoRapido" style="display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.82); z-index: 9999; justify-content: center; align-items: center; padding: 16px; backdrop-filter: blur(5px);">
<div style="background: #0f172a; border: 2px solid #10b981; border-radius: 18px; max-width: 420px; width: 100%; padding: 22px; box-shadow: 0 20px 50px rgba(0,0,0,0.8); position: relative;">
<button type="button" onclick="closeQuickProductModal()" style="position: absolute; top: 14px; right: 14px; background: none; border: none; color: #94a3b8; font-size: 1.4rem; cursor: pointer; padding: 4px 8px; line-height: 1;" title="Cerrar (Esc)">✕</button>
<div style="display: flex; align-items: center; gap: 10px; margin-bottom: 16px;">
<span style="font-size: 1.8rem; background: #064e3b; padding: 8px; border-radius: 12px; border: 1px solid #10b981; display: inline-flex;">⚡</span>
<div>
<h3 style="font-size: 1.15rem; font-weight: 900; color: #ffffff; margin: 0;">Nuevo Producto Rápido</h3>
<span style="font-size: 0.76rem; color: #94a3b8;">Registro instantáneo para cobrar al vuelo</span>
</div>
</div>
<form id="formProductoRapido" onsubmit="saveQuickProduct(event)">
<div style="margin-bottom: 14px;">
<label style="display: block; font-size: 0.78rem; font-weight: 700; color: #cbd5e1; margin-bottom: 6px;">
Nombre del Producto:
</label>
<input type="text" id="inputQuickProdNombre" placeholder="Ej: Empanada de Pollo, Jugo Naranja..." required autocomplete="off" style="width: 100%; padding: 11px 13px; border-radius: 10px; border: 1.5px solid #334155; background: #1e293b; color: #ffffff; font-size: 0.95rem; font-weight: 700; box-sizing: border-box; outline: none;">
</div>
<div style="margin-bottom: 18px;">
<label style="display: block; font-size: 0.78rem; font-weight: 700; color: #cbd5e1; margin-bottom: 6px;">
Precio de Venta (₡ Colones):
</label>
<div style="position: relative;">
<span style="position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: #38bdf8; font-weight: 900; font-size: 1.1rem;">₡</span>
<input type="number" id="inputQuickProdPrecio" placeholder="1500" required min="1" step="50" style="width: 100%; padding: 11px 13px 11px 32px; border-radius: 10px; border: 1.5px solid #334155; background: #1e293b; color: #ffffff; font-size: 1.15rem; font-weight: 900; box-sizing: border-box; outline: none;">
</div>
<span style="font-size: 0.72rem; color: #34d399; margin-top: 6px; display: block;">
🌿 Sello MEP Saludable asignado automáticamente (sin bloqueo parental)
</span>
</div>
<div style="display: flex; gap: 10px;">
<button type="button" onclick="closeQuickProductModal()" style="flex: 1; padding: 12px; background: #334155; color: #cbd5e1; border: none; border-radius: 10px; font-weight: 700; font-size: 0.85rem; cursor: pointer;">
Cancelar (Esc)
</button>
<button type="submit" id="btnSubmitQuickProd" style="flex: 2; padding: 12px; background: linear-gradient(135deg, #10b981, #059669); color: white; border: none; border-radius: 10px; font-weight: 800; font-size: 0.92rem; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 6px; box-shadow: 0 4px 14px rgba(16, 185, 129, 0.4);">
<span>💾</span> Guardar y Cobrar ➔
</button>
</div>
</form>
</div>
</div>
<script src="/js/sounds.js?v=8.6"></script>
<script src="/js/jsQR.js"></script>
<script src="/js/pos.js?v=8.5"></script>
<script src="/js/pos.js?v=8.6"></script>
</body>
</html>
+62
View File
@@ -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)
// ==========================================