diff --git a/assets/js/app.js b/assets/js/app.js index 915b75f..9d4e69d 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -3206,6 +3206,8 @@ function showUseForm() { document.getElementById('use-quantity').value = 1; document.getElementById('use-location').value = 'dispensa'; document.getElementById('use-unit-switch').style.display = 'none'; + document.getElementById('use-move-group').style.display = 'none'; + document.getElementById('use-move-location').value = ''; // Reset location buttons document.querySelectorAll('#page-use .loc-btn').forEach(b => b.classList.remove('active')); @@ -3262,6 +3264,9 @@ async function loadUseInventoryInfo() { return ``; }).join(''); + // Show move-to section + updateUseMoveOptions(); + const unit = items[0].unit || 'pz'; const pkgSize = parseFloat(items[0].default_quantity) || 0; const pkgUnit = items[0].package_unit || ''; @@ -3364,15 +3369,60 @@ function selectUseLocation(btn, loc) { btn.parentElement.querySelectorAll('.loc-btn').forEach(b => b.classList.remove('active')); btn.classList.add('active'); document.getElementById('use-location').value = loc; + // Reset move location when source changes + updateUseMoveOptions(); +} + +function updateUseMoveOptions() { + const currentLoc = document.getElementById('use-location').value; + const moveGroup = document.getElementById('use-move-group'); + const moveSelector = document.getElementById('use-move-location-selector'); + // Show move section and disable the current source location + moveGroup.style.display = 'block'; + document.getElementById('use-move-location').value = ''; + moveSelector.querySelectorAll('.loc-btn').forEach(b => { + b.classList.remove('active'); + const btnLoc = b.getAttribute('onclick')?.match(/'([^']+)'/)?.[1]; + b.style.display = btnLoc === currentLoc ? 'none' : ''; + }); +} + +function selectUseMoveLocation(btn, loc) { + const moveSelector = document.getElementById('use-move-location-selector'); + const wasActive = btn.classList.contains('active'); + moveSelector.querySelectorAll('.loc-btn').forEach(b => b.classList.remove('active')); + if (!wasActive) { + btn.classList.add('active'); + document.getElementById('use-move-location').value = loc; + } else { + document.getElementById('use-move-location').value = ''; + } +} + +async function moveInventoryLocation(productId, fromLoc, toLoc) { + 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) return; + await api('inventory_update', {}, 'POST', { + id: item.id, + location: toLoc, + product_id: productId, + }); + } catch (e) { + console.error('Move inventory error:', e); + } } async function submitUseAll() { showLoading(true); try { + const moveTo = document.getElementById('use-move-location')?.value; + const moveFrom = document.getElementById('use-location').value; const result = await api('inventory_use', {}, 'POST', { product_id: currentProduct.id, use_all: true, - location: document.getElementById('use-location').value, + location: moveFrom, }); showLoading(false); if (result.success) { @@ -3415,6 +3465,13 @@ async function submitUse(e) { if (result.success) { const usedText = displayUnit ? `${displayQty}${displayUnit}` : displayQty; showToast(`📤 Usato ${usedText} di ${currentProduct.name}`, 'success'); + // Move remaining to new location if selected + const moveTo = document.getElementById('use-move-location')?.value; + const moveFrom = document.getElementById('use-location').value; + if (moveTo && moveTo !== moveFrom && result.remaining > 0) { + await moveInventoryLocation(currentProduct.id, moveFrom, moveTo); + setTimeout(() => showToast(`📦 Spostato in ${LOCATIONS[moveTo]?.label || moveTo}`, 'info'), 1200); + } if (result.added_to_bring) { setTimeout(() => showToast('🛒 Prodotto finito → aggiunto a Bring!', 'info'), 1500); } diff --git a/data/dispensa.db b/data/dispensa.db index eed68e5..7ad2c0f 100644 Binary files a/data/dispensa.db and b/data/dispensa.db differ diff --git a/index.html b/index.html index cef32fd..4ca0a66 100644 --- a/index.html +++ b/index.html @@ -275,6 +275,16 @@ + @@ -894,6 +904,6 @@ - +