diff --git a/api/index.php b/api/index.php index c2cddf9..fe2a62c 100644 --- a/api/index.php +++ b/api/index.php @@ -501,6 +501,15 @@ function addToInventory(PDO $db): void { if ($unit) { $stmt = $db->prepare("UPDATE products SET unit = ?, default_quantity = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?"); $stmt->execute([$unit, $quantity, $productId]); + } else { + // Auto-set default_quantity if product has none (first add sets package size) + $stmt = $db->prepare("SELECT default_quantity, unit FROM products WHERE id = ?"); + $stmt->execute([$productId]); + $prod = $stmt->fetch(); + if ($prod && (float)($prod['default_quantity'] ?? 0) == 0 && !in_array($prod['unit'], ['pz', 'conf'])) { + $stmt = $db->prepare("UPDATE products SET default_quantity = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?"); + $stmt->execute([$quantity, $productId]); + } } // Update package info if conf diff --git a/assets/js/app.js b/assets/js/app.js index a8d1188..008d56d 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -3410,6 +3410,51 @@ function selectUseLocation(btn, loc) { document.getElementById('use-location').value = loc; } +function showMoveAfterUseModal(product, fromLoc, remaining) { + const otherLocs = Object.entries(LOCATIONS).filter(([k]) => k !== fromLoc); + const locButtons = otherLocs.map(([k, v]) => + `` + ).join(''); + + document.getElementById('modal-content').innerHTML = ` + +
+

Vuoi spostare il resto di ${escapeHtml(product.name)} in un'altra posizione?

+
${locButtons}
+ +
+ `; + document.getElementById('modal-overlay').style.display = 'flex'; +} + +async function confirmMoveAfterUse(productId, fromLoc, toLoc) { + closeModal(); + showLoading(true); + try { + const data = await api('inventory_list'); + const item = (data.inventory || []).find(i => i.product_id == productId && i.location === fromLoc && parseFloat(i.quantity) > 0); + if (item) { + const product = { name: item.name || '', category: item.category || '' }; + let days = estimateExpiryDays(product, toLoc); + if (item.vacuum_sealed) days = getVacuumExpiryDays(days); + await api('inventory_update', {}, 'POST', { + id: item.id, + location: toLoc, + expiry_date: addDays(days), + product_id: productId, + }); + showToast(`📦 Spostato in ${LOCATIONS[toLoc]?.label || toLoc}`, 'success'); + } + } catch (e) { + console.error('Move error:', e); + } + showLoading(false); + showPage('dashboard'); +} + async function submitUseAll() { showLoading(true); try { @@ -3462,7 +3507,13 @@ async function submitUse(e) { if (result.added_to_bring) { setTimeout(() => showToast('🛒 Prodotto finito → aggiunto a Bring!', 'info'), 1500); } - showPage('dashboard'); + // If there's remaining quantity, offer to move to another location + const usedFrom = document.getElementById('use-location').value; + if (result.remaining > 0) { + showMoveAfterUseModal(currentProduct, usedFrom, result.remaining); + } else { + showPage('dashboard'); + } } else { showToast(result.error || 'Errore', 'error'); } diff --git a/data/dispensa.db b/data/dispensa.db index 3a22ba3..78bcfb0 100644 Binary files a/data/dispensa.db and b/data/dispensa.db differ diff --git a/index.html b/index.html index 3811bef..79655cb 100644 --- a/index.html +++ b/index.html @@ -895,6 +895,6 @@ - +