Fix ricalcolo scadenza per freezer e sotto vuoto

- selectPurchaseType: non usa più date hardcoded, ricalcola sempre in base
  alla posizione e stato sotto vuoto corrente
- Edit inventario: cambiare posizione o toggle sotto vuoto ricalcola la scadenza
- Spostamento prodotto: ricalcola scadenza per la nuova posizione
- Fix DB: fusi di pollo freezer 120gg, Bel Paese vacuum 21gg, Speck vacuum 75gg
- Versione: 20260315j
This commit is contained in:
dadaloop82
2026-03-15 19:15:22 +00:00
parent dc9785c160
commit 68dd7810f0
3 changed files with 42 additions and 9 deletions
+41 -8
View File
@@ -1367,6 +1367,18 @@ async function deleteInventoryItem(id) {
} }
} }
function recalcEditExpiry(locInputId, vacuumInputId, expiryInputId) {
const product = window._editingProduct;
if (!product) return;
const loc = document.getElementById(locInputId)?.value || '';
const isVacuum = document.getElementById(vacuumInputId)?.checked;
let days = estimateExpiryDays(product, loc);
if (isVacuum) days = getVacuumExpiryDays(days);
const newDate = addDays(days);
const expiryInput = document.getElementById(expiryInputId);
if (expiryInput) expiryInput.value = newDate;
}
function editInventoryItem(id) { function editInventoryItem(id) {
const item = currentInventory.find(i => i.id === id); const item = currentInventory.find(i => i.id === id);
if (!item) { if (!item) {
@@ -1379,6 +1391,8 @@ function editInventoryItem(id) {
const confSizeVal = (isConf && item.default_quantity > 0) ? item.default_quantity : ''; const confSizeVal = (isConf && item.default_quantity > 0) ? item.default_quantity : '';
const confUnitVal = (isConf && item.package_unit) ? item.package_unit : 'g'; const confUnitVal = (isConf && item.package_unit) ? item.package_unit : 'g';
window._editingProduct = { name: item.name, category: item.category || '' };
// Rebuild modal content for editing (don't close and reopen - just replace content) // Rebuild modal content for editing (don't close and reopen - just replace content)
document.getElementById('modal-content').innerHTML = ` document.getElementById('modal-content').innerHTML = `
<div class="modal-header"> <div class="modal-header">
@@ -1414,7 +1428,7 @@ function editInventoryItem(id) {
<div class="location-selector"> <div class="location-selector">
${Object.entries(LOCATIONS).map(([k, v]) => ` ${Object.entries(LOCATIONS).map(([k, v]) => `
<button type="button" class="loc-btn ${item.location === k ? 'active' : ''}" <button type="button" class="loc-btn ${item.location === k ? 'active' : ''}"
onclick="this.parentElement.querySelectorAll('.loc-btn').forEach(b=>b.classList.remove('active'));this.classList.add('active');document.getElementById('edit-loc').value='${k}'">${v.icon} ${v.label}</button> onclick="this.parentElement.querySelectorAll('.loc-btn').forEach(b=>b.classList.remove('active'));this.classList.add('active');document.getElementById('edit-loc').value='${k}';recalcEditExpiry('edit-loc','edit-vacuum','edit-expiry')">${v.icon} ${v.label}</button>
`).join('')} `).join('')}
</div> </div>
<input type="hidden" id="edit-loc" value="${item.location}"> <input type="hidden" id="edit-loc" value="${item.location}">
@@ -1427,7 +1441,7 @@ function editInventoryItem(id) {
<label class="toggle-row"> <label class="toggle-row">
<span>🫙 Sotto vuoto</span> <span>🫙 Sotto vuoto</span>
<span class="toggle-switch"> <span class="toggle-switch">
<input type="checkbox" id="edit-vacuum" ${item.vacuum_sealed ? 'checked' : ''}> <input type="checkbox" id="edit-vacuum" ${item.vacuum_sealed ? 'checked' : ''} onchange="recalcEditExpiry('edit-loc','edit-vacuum','edit-expiry')">
<span class="toggle-slider"></span> <span class="toggle-slider"></span>
</span> </span>
</label> </label>
@@ -2578,6 +2592,8 @@ function editActionInventoryItem(inventoryId) {
const confSizeVal = (isConf && item.default_quantity > 0) ? item.default_quantity : ''; const confSizeVal = (isConf && item.default_quantity > 0) ? item.default_quantity : '';
const confUnitVal = (isConf && item.package_unit) ? item.package_unit : 'g'; const confUnitVal = (isConf && item.package_unit) ? item.package_unit : 'g';
window._editingProduct = { name: item.name || currentProduct.name, category: item.category || currentProduct.category || '' };
document.getElementById('modal-content').innerHTML = ` document.getElementById('modal-content').innerHTML = `
<div class="modal-header"> <div class="modal-header">
<h3>Modifica ${escapeHtml(item.name || currentProduct.name)}</h3> <h3>Modifica ${escapeHtml(item.name || currentProduct.name)}</h3>
@@ -2612,7 +2628,7 @@ function editActionInventoryItem(inventoryId) {
<div class="location-selector"> <div class="location-selector">
${Object.entries(LOCATIONS).map(([k, v]) => ` ${Object.entries(LOCATIONS).map(([k, v]) => `
<button type="button" class="loc-btn ${item.location === k ? 'active' : ''}" <button type="button" class="loc-btn ${item.location === k ? 'active' : ''}"
onclick="this.parentElement.querySelectorAll('.loc-btn').forEach(b=>b.classList.remove('active'));this.classList.add('active');document.getElementById('action-edit-loc').value='${k}'">${v.icon} ${v.label}</button> onclick="this.parentElement.querySelectorAll('.loc-btn').forEach(b=>b.classList.remove('active'));this.classList.add('active');document.getElementById('action-edit-loc').value='${k}';recalcEditExpiry('action-edit-loc','action-edit-vacuum','action-edit-expiry')">${v.icon} ${v.label}</button>
`).join('')} `).join('')}
</div> </div>
<input type="hidden" id="action-edit-loc" value="${item.location}"> <input type="hidden" id="action-edit-loc" value="${item.location}">
@@ -2625,7 +2641,7 @@ function editActionInventoryItem(inventoryId) {
<label class="toggle-row"> <label class="toggle-row">
<span>🫙 Sotto vuoto</span> <span>🫙 Sotto vuoto</span>
<span class="toggle-switch"> <span class="toggle-switch">
<input type="checkbox" id="action-edit-vacuum" ${item.vacuum_sealed ? 'checked' : ''}> <input type="checkbox" id="action-edit-vacuum" ${item.vacuum_sealed ? 'checked' : ''} onchange="recalcEditExpiry('action-edit-loc','action-edit-vacuum','action-edit-expiry')">
<span class="toggle-slider"></span> <span class="toggle-slider"></span>
</span> </span>
</label> </label>
@@ -2937,10 +2953,10 @@ function showAddForm() {
expirySection.innerHTML = ` expirySection.innerHTML = `
<label>🛒 Questo prodotto è...</label> <label>🛒 Questo prodotto è...</label>
<div class="purchase-type-selector"> <div class="purchase-type-selector">
<button type="button" class="purchase-type-btn active" onclick="selectPurchaseType(this, 'new', '${estimatedDate}', '${escapeHtml(estimateLabel)}')"> <button type="button" class="purchase-type-btn active" onclick="selectPurchaseType(this, 'new')">
🆕 Appena comprato 🆕 Appena comprato
</button> </button>
<button type="button" class="purchase-type-btn" onclick="selectPurchaseType(this, 'existing', '', '')"> <button type="button" class="purchase-type-btn" onclick="selectPurchaseType(this, 'existing')">
📦 Ce l'avevo già 📦 Ce l'avevo già
</button> </button>
</div> </div>
@@ -3085,7 +3101,7 @@ function adjustAddQty(delta) {
qtyInput.value = val; qtyInput.value = val;
} }
function selectPurchaseType(btn, type, estimatedDate, estimateLabel) { function selectPurchaseType(btn, type) {
btn.parentElement.querySelectorAll('.purchase-type-btn').forEach(b => b.classList.remove('active')); btn.parentElement.querySelectorAll('.purchase-type-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active'); btn.classList.add('active');
@@ -3095,9 +3111,21 @@ function selectPurchaseType(btn, type, estimatedDate, estimateLabel) {
const currentQty = document.getElementById('add-quantity').value; const currentQty = document.getElementById('add-quantity').value;
if (type === 'new') { if (type === 'new') {
// Recalculate fresh expiry based on current location/vacuum
const loc = document.getElementById('add-location')?.value || '';
const isVacuum = document.getElementById('add-vacuum-sealed')?.checked;
let days = estimateExpiryDays(currentProduct, loc);
if (isVacuum) days = getVacuumExpiryDays(days);
const estimatedDate = addDays(days);
const estimateLabel = formatEstimatedExpiry(days);
let suffix = '';
if (loc === 'freezer' && isVacuum) suffix = ' (freezer + sotto vuoto)';
else if (loc === 'freezer') suffix = ' (freezer)';
else if (isVacuum) suffix = ' (sotto vuoto)';
detailDiv.innerHTML = ` detailDiv.innerHTML = `
<div class="expiry-estimate"> <div class="expiry-estimate">
<span class="expiry-estimate-label">Scadenza stimata: <strong>${estimateLabel}</strong></span> <span class="expiry-estimate-label">Scadenza stimata: <strong>${estimateLabel}${suffix}</strong></span>
<span class="expiry-estimate-date">${formatDate(estimatedDate)}</span> <span class="expiry-estimate-date">${formatDate(estimatedDate)}</span>
</div> </div>
<div class="expiry-input-row"> <div class="expiry-input-row">
@@ -3418,9 +3446,14 @@ async function moveInventoryLocation(productId, fromLoc, toLoc) {
const data = await api('inventory_list'); const data = await api('inventory_list');
const item = (data.inventory || []).find(i => i.product_id == productId && i.location === fromLoc && parseFloat(i.quantity) > 0); const item = (data.inventory || []).find(i => i.product_id == productId && i.location === fromLoc && parseFloat(i.quantity) > 0);
if (!item) return; if (!item) return;
// Recalculate expiry for new location
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', { await api('inventory_update', {}, 'POST', {
id: item.id, id: item.id,
location: toLoc, location: toLoc,
expiry_date: addDays(days),
product_id: productId, product_id: productId,
}); });
} catch (e) { } catch (e) {
BIN
View File
Binary file not shown.
+1 -1
View File
@@ -904,6 +904,6 @@
</div> </div>
</div> </div>
<script src="assets/js/app.js?v=20260315i"></script> <script src="assets/js/app.js?v=20260315j"></script>
</body> </body>
</html> </html>