fix: Corregir icono null en carrito, simplificar boton entregado en preordenes y sincronizar bloqueo de productos
This commit is contained in:
@@ -1575,6 +1575,15 @@ body.dark-mode .product-card.out-of-stock {
|
||||
box-shadow: 0 1px 3px rgba(239, 68, 68, 0.3);
|
||||
}
|
||||
|
||||
.pos-prod-card.out-of-stock-pos {
|
||||
opacity: 0.45 !important;
|
||||
filter: grayscale(85%) !important;
|
||||
border-color: rgba(239, 68, 68, 0.4) !important;
|
||||
background: rgba(239, 68, 68, 0.08) !important;
|
||||
cursor: not-allowed !important;
|
||||
position: relative !important;
|
||||
}
|
||||
|
||||
.banner-card-blocked {
|
||||
background: #fee2e2;
|
||||
border: 2px solid #ef4444;
|
||||
|
||||
+20
-6
@@ -628,7 +628,7 @@ function renderCategories() {
|
||||
for (const cat of categories) {
|
||||
html += `
|
||||
<button class="cat-pill ${activeCategoryId === cat.id ? 'active' : ''}" onclick="selectCategory(${cat.id})">
|
||||
<span>${cat.icono}</span> ${cat.nombre}
|
||||
<span>${cat.icono || '🍽️'}</span> ${cat.nombre}
|
||||
</button>
|
||||
`;
|
||||
}
|
||||
@@ -668,8 +668,8 @@ function renderProducts() {
|
||||
}
|
||||
|
||||
grid.innerHTML = filtered.map(prod => {
|
||||
const isOutOfStock = prod.control_stock === 1 && (prod.stock <= 0 || prod.disponible === 0);
|
||||
const stockBadge = (prod.control_stock === 1 && isOutOfStock)
|
||||
const isOutOfStock = prod.disponible === 0 || (prod.control_stock === 1 && prod.stock <= 0);
|
||||
const stockBadge = isOutOfStock
|
||||
? '<span class="badge-out-of-stock">AGOTADO</span>'
|
||||
: '';
|
||||
|
||||
@@ -710,8 +710,8 @@ function addToCart(productId) {
|
||||
const prod = products.find(p => p.id === productId);
|
||||
if (!prod) return;
|
||||
|
||||
if (prod.control_stock === 1 && (prod.stock <= 0 || prod.disponible === 0)) {
|
||||
alert(`El producto "${prod.nombre}" se encuentra agotado en la soda.`);
|
||||
if (prod.disponible === 0 || (prod.control_stock === 1 && prod.stock <= 0)) {
|
||||
alert(`El producto "${prod.nombre}" se encuentra agotado o no disponible en la soda.`);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -779,7 +779,7 @@ function renderCartModalItems() {
|
||||
return `
|
||||
<div style="display: flex; justify-content: space-between; align-items: center; padding: 8px 0; border-bottom: 1px solid #f1f5f9;">
|
||||
<div>
|
||||
<strong style="font-size: 0.9rem; color: #0f172a;">${item.product.icono} ${item.product.nombre}</strong>
|
||||
<strong style="font-size: 0.9rem; color: #0f172a;">${item.product.icono ? `${item.product.icono} ` : ''}${item.product.nombre}</strong>
|
||||
<div style="font-size: 0.75rem; color: #64748b;">₡${item.product.precio_colones.toLocaleString('es-CR')} c/u</div>
|
||||
</div>
|
||||
<div style="display: flex; align-items: center; gap: 8px;">
|
||||
@@ -1727,14 +1727,28 @@ function initStudentSSE() {
|
||||
// Actualizar en el catálogo de estudiantes
|
||||
const idx = products.findIndex(p => p.id === prod.id);
|
||||
if (idx !== -1) {
|
||||
if (prod.eliminado) {
|
||||
products.splice(idx, 1);
|
||||
} else {
|
||||
products[idx] = { ...products[idx], ...prod };
|
||||
}
|
||||
renderProducts();
|
||||
} else if (!prod.eliminado) {
|
||||
products.push(prod);
|
||||
renderProducts();
|
||||
}
|
||||
// Actualizar en admin
|
||||
if (currentUser && (currentUser.rol === 'admin' || currentUser.rol === 'cajero')) {
|
||||
const adminIdx = adminProducts.findIndex(p => p.id === prod.id);
|
||||
if (adminIdx !== -1) {
|
||||
if (prod.eliminado) {
|
||||
adminProducts.splice(adminIdx, 1);
|
||||
} else {
|
||||
adminProducts[adminIdx] = { ...adminProducts[adminIdx], ...prod };
|
||||
}
|
||||
filterAdminProducts(adminSearchQuery);
|
||||
} else if (!prod.eliminado) {
|
||||
adminProducts.unshift(prod);
|
||||
filterAdminProducts(adminSearchQuery);
|
||||
}
|
||||
}
|
||||
|
||||
+51
-24
@@ -105,7 +105,7 @@ function renderPosCategories() {
|
||||
<button class="pos-tab-btn active" style="padding: 6px 12px; font-size: 0.8rem;" onclick="filterPosCat(null, this)">Todos</button>
|
||||
${posCategories.map(c => `
|
||||
<button class="pos-tab-btn" style="padding: 6px 12px; font-size: 0.8rem;" onclick="filterPosCat(${c.id}, this)">
|
||||
${c.icono} ${c.nombre}
|
||||
${c.icono || '🍽️'} ${c.nombre}
|
||||
</button>
|
||||
`).join('')}
|
||||
`;
|
||||
@@ -127,18 +127,26 @@ function renderPosProducts(catId) {
|
||||
list = list.filter(p => p.categoria_id === catId);
|
||||
}
|
||||
|
||||
grid.innerHTML = list.map(prod => `
|
||||
<div class="pos-prod-card" onclick="addToPosCart(${prod.id})">
|
||||
grid.innerHTML = list.map(prod => {
|
||||
const isOutOfStock = prod.disponible === 0 || (prod.control_stock === 1 && prod.stock <= 0);
|
||||
|
||||
return `
|
||||
<div class="pos-prod-card ${isOutOfStock ? 'out-of-stock-pos' : ''}"
|
||||
style="${isOutOfStock ? 'opacity: 0.45; filter: grayscale(0.85); cursor: not-allowed; position: relative; border-color: #ef4444;' : ''}"
|
||||
onclick="${isOutOfStock ? `alert('El producto \\'${prod.nombre.replace(/'/g, "\\'")}\\' se encuentra bloqueado o agotado.')` : `addToPosCart(${prod.id})`}">
|
||||
${isOutOfStock ? '<div style="position: absolute; top: 8px; right: 8px; background: #dc2626; color: #ffffff; font-size: 0.65rem; font-weight: 900; padding: 2px 6px; border-radius: 4px; z-index: 2; letter-spacing: 0.5px;">BLOQUEADO</div>' : ''}
|
||||
<div style="font-size: 2.2rem; text-align: center;">${prod.icono || '🥪'}</div>
|
||||
<div>
|
||||
<div style="font-size: 0.88rem; font-weight: 800; color: #ffffff; line-height: 1.2;">${prod.nombre}</div>
|
||||
${prod.cumple_mep ? '<span style="font-size: 0.65rem; color: #34d399;">🌿 MEP Saludable</span>' : ''}
|
||||
${isOutOfStock ? '<span style="font-size: 0.65rem; color: #f87171; display: block; margin-top: 2px; font-weight: 700;">No disponible</span>' : ''}
|
||||
</div>
|
||||
<div style="font-size: 1.1rem; font-weight: 900; color: #38bdf8; margin-top: 6px;">
|
||||
<div style="font-size: 1.1rem; font-weight: 900; color: ${isOutOfStock ? '#94a3b8' : '#38bdf8'}; margin-top: 6px;">
|
||||
₡${prod.precio_colones.toLocaleString('es-CR')}
|
||||
</div>
|
||||
</div>
|
||||
`).join('');
|
||||
`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
@@ -256,7 +264,18 @@ function addToPosCart(prodId) {
|
||||
const prod = posProducts.find(p => p.id === prodId);
|
||||
if (!prod) return;
|
||||
|
||||
if (prod.disponible === 0 || (prod.control_stock === 1 && prod.stock <= 0)) {
|
||||
alert(`El producto "${prod.nombre}" no está disponible o se encuentra agotado.`);
|
||||
return;
|
||||
}
|
||||
|
||||
const existing = posCart.find(i => i.product.id === prodId);
|
||||
const currentInCart = existing ? existing.cantidad : 0;
|
||||
if (prod.control_stock === 1 && (currentInCart + 1) > prod.stock) {
|
||||
alert(`Solo quedan ${prod.stock} unidad(es) de "${prod.nombre}" en inventario.`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (existing) {
|
||||
existing.cantidad++;
|
||||
} else {
|
||||
@@ -301,7 +320,7 @@ function updatePosCartUI() {
|
||||
return `
|
||||
<div class="pos-cart-item">
|
||||
<div style="flex: 1; min-width: 0; word-break: break-word;">
|
||||
<strong style="color: #f8fafc; font-size: 0.85rem; display: block; line-height: 1.2;">${item.product.icono} ${item.product.nombre}</strong>
|
||||
<strong style="color: #f8fafc; font-size: 0.85rem; display: block; line-height: 1.2;">${item.product.icono ? `${item.product.icono} ` : ''}${item.product.nombre}</strong>
|
||||
<div style="font-size: 0.74rem; color: #94a3b8; margin-top: 2px;">₡${item.product.precio_colones.toLocaleString('es-CR')} c/u</div>
|
||||
</div>
|
||||
<div style="display: flex; align-items: center; gap: 6px; flex-shrink: 0;">
|
||||
@@ -1009,25 +1028,14 @@ async function loadPreOrders() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div style="display: flex; gap: 8px;">
|
||||
${ord.estado === 'pendiente' ? `
|
||||
<button onclick="updateOrderStatus(${ord.id}, 'en_preparacion')" style="padding: 6px 12px; background: #3b82f6; color: white; border: none; border-radius: 8px; font-weight: 700; cursor: pointer;">
|
||||
👨🍳 Preparar
|
||||
<div>
|
||||
${ord.estado !== 'entregado' ? `
|
||||
<button onclick="updateOrderStatus(${ord.id}, 'entregado')" style="padding: 8px 18px; background: #059669; color: white; border: none; border-radius: 8px; font-weight: 800; font-size: 0.88rem; cursor: pointer; display: flex; align-items: center; gap: 6px; box-shadow: 0 2px 4px rgba(0,0,0,0.2); transition: background 0.15s ease;" onmouseover="this.style.background='#047857'" onmouseout="this.style.background='#059669'">
|
||||
Entregado
|
||||
</button>
|
||||
` : ''}
|
||||
${ord.estado === 'en_preparacion' ? `
|
||||
<button onclick="updateOrderStatus(${ord.id}, 'listo')" style="padding: 6px 12px; background: #10b981; color: white; border: none; border-radius: 8px; font-weight: 700; cursor: pointer;">
|
||||
🛍️ Listo en Bolsa
|
||||
</button>
|
||||
` : ''}
|
||||
${ord.estado === 'listo' ? `
|
||||
<button onclick="updateOrderStatus(${ord.id}, 'entregado')" style="padding: 6px 14px; background: #059669; color: white; border: none; border-radius: 8px; font-weight: 800; cursor: pointer;">
|
||||
✅ Entregar
|
||||
</button>
|
||||
` : ''}
|
||||
${ord.estado === 'entregado' ? `
|
||||
<span style="font-size: 0.78rem; color: #34d399; font-weight: 800;">✓ Entregado</span>
|
||||
` : ''}
|
||||
` : `
|
||||
<span style="font-size: 0.82rem; color: #34d399; font-weight: 800; padding: 6px 12px; background: rgba(52, 211, 153, 0.1); border-radius: 6px; border: 1px solid rgba(52, 211, 153, 0.2);">✓ Entregado</span>
|
||||
`}
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
@@ -1096,6 +1104,25 @@ function initSSE() {
|
||||
loadPreOrders();
|
||||
});
|
||||
|
||||
sseSource.addEventListener('producto_actualizado', (e) => {
|
||||
try {
|
||||
const prod = JSON.parse(e.data);
|
||||
const idx = posProducts.findIndex(p => p.id === prod.id);
|
||||
if (idx !== -1) {
|
||||
if (prod.eliminado) {
|
||||
posProducts.splice(idx, 1);
|
||||
} else {
|
||||
posProducts[idx] = { ...posProducts[idx], ...prod };
|
||||
}
|
||||
} else if (!prod.eliminado) {
|
||||
posProducts.push(prod);
|
||||
}
|
||||
renderPosProducts(currentPosCatId);
|
||||
} catch (err) {
|
||||
console.error('Error procesando producto_actualizado en POS:', err);
|
||||
}
|
||||
});
|
||||
|
||||
sseSource.addEventListener('estudiante_actualizado', (e) => {
|
||||
try {
|
||||
const data = JSON.parse(e.data);
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
UPDATE productos SET icono = '🥟' WHERE id = 1 AND (icono IS NULL OR icono = '');
|
||||
UPDATE productos SET icono = '🥟' WHERE id = 2 AND (icono IS NULL OR icono = '');
|
||||
UPDATE productos SET icono = '🥪' WHERE id = 3 AND (icono IS NULL OR icono = '');
|
||||
UPDATE productos SET icono = '🌮' WHERE id = 4 AND (icono IS NULL OR icono = '');
|
||||
UPDATE productos SET icono = '🍳' WHERE id = 5 AND (icono IS NULL OR icono = '');
|
||||
UPDATE productos SET icono = '🍗' WHERE id = 6 AND (icono IS NULL OR icono = '');
|
||||
UPDATE productos SET icono = '🍛' WHERE id = 7 AND (icono IS NULL OR icono = '');
|
||||
UPDATE productos SET icono = '🍹' WHERE id = 8 AND (icono IS NULL OR icono = '');
|
||||
UPDATE productos SET icono = '🥤' WHERE id = 9 AND (icono IS NULL OR icono = '');
|
||||
UPDATE productos SET icono = '🧃' WHERE id = 10 AND (icono IS NULL OR icono = '');
|
||||
UPDATE productos SET icono = '🥛' WHERE id = 11 AND (icono IS NULL OR icono = '');
|
||||
UPDATE productos SET icono = '🍉' WHERE id = 12 AND (icono IS NULL OR icono = '');
|
||||
UPDATE productos SET icono = '🥣' WHERE id = 13 AND (icono IS NULL OR icono = '');
|
||||
UPDATE productos SET icono = '🌾' WHERE id = 14 AND (icono IS NULL OR icono = '');
|
||||
UPDATE productos SET icono = '🍮' WHERE id = 15 AND (icono IS NULL OR icono = '');
|
||||
UPDATE productos SET icono = '🥪' WHERE icono IS NULL OR icono = '';
|
||||
@@ -671,6 +671,7 @@ function crearOrdenCompleta({ estudianteId, tipoOrden, momentoEntrega, notas, it
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
`);
|
||||
|
||||
const productosActualizados = [];
|
||||
for (const det of detallesParaInsertar) {
|
||||
insertDetalle.run(ordenId, det.producto_id, det.cantidad, det.precio_unitario, det.subtotal);
|
||||
|
||||
@@ -680,6 +681,16 @@ function crearOrdenCompleta({ estudianteId, tipoOrden, momentoEntrega, notas, it
|
||||
const nuevoStock = Math.max(0, prod.stock - det.cantidad);
|
||||
const sigueDisponible = nuevoStock > 0 ? 1 : 0;
|
||||
db.prepare('UPDATE productos SET stock = ?, disponible = ? WHERE id = ?').run(nuevoStock, sigueDisponible, det.producto_id);
|
||||
|
||||
const prodActualizado = 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(det.producto_id);
|
||||
if (prodActualizado) {
|
||||
productosActualizados.push(prodActualizado);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -701,6 +712,7 @@ function crearOrdenCompleta({ estudianteId, tipoOrden, momentoEntrega, notas, it
|
||||
estado: estadoInicial,
|
||||
total_colones: totalColones,
|
||||
detalles: detallesParaInsertar,
|
||||
productosActualizados,
|
||||
financiero: resultadoDebito
|
||||
};
|
||||
});
|
||||
|
||||
@@ -1592,6 +1592,8 @@ app.post('/api/productos/rapido', (req, res) => {
|
||||
WHERE p.id = ?
|
||||
`).get(newId);
|
||||
|
||||
broadcastEvent('producto_actualizado', nuevoProducto);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
mensaje: `Producto "${cleanNombre}" agregado al inventario`,
|
||||
@@ -1634,6 +1636,13 @@ app.post('/api/ordenes', (req, res) => {
|
||||
// Notificar en tiempo real a la pantalla de cocina/caja de la soda
|
||||
broadcastEvent('nueva_orden', resultado);
|
||||
|
||||
// Notificar actualización de productos si su stock o disponibilidad cambió
|
||||
if (resultado && Array.isArray(resultado.productosActualizados)) {
|
||||
for (const prodAct of resultado.productosActualizados) {
|
||||
broadcastEvent('producto_actualizado', prodAct);
|
||||
}
|
||||
}
|
||||
|
||||
// Notificar actualización de estudiante (saldo y disponible) en tiempo real a clientes
|
||||
if (resultado && resultado.financiero && resultado.financiero.estudiante) {
|
||||
const fEst = resultado.financiero.estudiante;
|
||||
|
||||
Reference in New Issue
Block a user