From eb5ee60dd7025080c564c294b9f5b888462f8f51 Mon Sep 17 00:00:00 2001 From: dadaloop82 Date: Mon, 16 Mar 2026 06:48:48 +0000 Subject: [PATCH] Sposta prodotto dopo l'uso + fix prodotti aperti MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Dopo aver usato un prodotto, se rimane quantità mostra modal con opzione di spostarlo in un'altra posizione (es. dispensa→frigo dopo apertura) - La scadenza viene ricalcolata per la nuova posizione - Fix marmellata: default_quantity era 0 → non appariva tra prodotti aperti - Auto-set default_quantity al primo add per prodotti g/ml/kg/l senza pkg size - Versione: 20260316b --- api/index.php | 9 ++++++++ assets/js/app.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++- data/dispensa.db | Bin 176128 -> 180224 bytes index.html | 2 +- 4 files changed, 62 insertions(+), 2 deletions(-) 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 3a22ba3e5a67aef6f4aa0299c7688b022fdab294..78bcfb0a93c1db01fc14ecb003751cfc74032bc2 100644 GIT binary patch delta 1915 zcmZ`)U2IfE6uvWe+ikb)4%#2Ah)zrT11xphrGHCw3q+6@<5o}-HG%2f*~`G)JInnc zu?AXvFvMtN)0ja>L|+J16v(3cA}0KV^hK?j_(lYL@P!y85fg~UbGPlbCi=8@HPTr_(sMVLFtRqb6L+;|wZ+HCB z5+>JSt;NU#a*zB@Zj)chEpom1(8x7{*E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