feat: lista spesa con tab Da comprare/In previsione, tag, frequenza, tap-to-scan

- Counter nei tab aggiornati dinamicamente
- Auto-aggiunta prodotti CRITICI a Bring! al caricamento (1x per sessione)
- Badge urgenza e frequenza sugli item in lista (cross-ref smart shopping)
- Tag locali per item (Urgente/Priorità/Verificare) con menu dropdown
- Ordinamento automatico per frequenza utilizzo (item più usati in cima)
- Tap su un item → scanner barcode, con banner 'Trovato! Rimuovi dalla lista'
- Fix pctLeft: usa max(1, qty) come fallback refQty per evitare falsi alert
- Fix daysLeft capped a 365gg per pulire stringhe di previsione
- Back button on action page → torna a shopping se aperto da lista
This commit is contained in:
dadaloop82
2026-03-29 14:12:37 +00:00
parent 5b11ab6493
commit a38a5d670f
6 changed files with 1571 additions and 51 deletions
+237
View File
@@ -147,4 +147,241 @@ function migrateDB(PDO $db): void {
if (!in_array('vacuum_sealed', $invColNames)) {
$db->exec("ALTER TABLE inventory ADD COLUMN vacuum_sealed INTEGER DEFAULT 0");
}
// Add opened_at column to inventory if missing
if (!in_array('opened_at', $invColNames)) {
$db->exec("ALTER TABLE inventory ADD COLUMN opened_at DATETIME DEFAULT NULL");
// Backfill: detect already-opened items and set opened_at + recalculate expiry
backfillOpenedItems($db);
}
// Migration v2: recalculate sealed fridge item expiry (fridge extends shelf life)
$migrated = $db->query("SELECT value FROM app_settings WHERE key = 'migration_fridge_expiry_v1'")->fetchColumn();
if (!$migrated) {
recalcSealedFridgeExpiry($db);
$db->exec("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('migration_fridge_expiry_v1', '1')");
}
}
/**
* Backfill opened_at for existing inventory items that appear to be opened.
* An item is considered opened if:
* - conf unit with fractional quantity
* - weight/volume unit (g,kg,ml,l) with quantity < default_quantity
* Uses updated_at as the approximate opened_at date.
* Recalculates expiry_date based on opened shelf life from opened_at.
*/
function backfillOpenedItems(PDO $db): void {
$stmt = $db->query("
SELECT i.id, i.quantity, i.location, i.updated_at, i.expiry_date, i.vacuum_sealed,
p.name, p.category, p.unit, p.default_quantity
FROM inventory i
JOIN products p ON i.product_id = p.id
WHERE i.quantity > 0
");
$rows = $stmt->fetchAll();
foreach ($rows as $row) {
$isOpened = false;
$unit = $row['unit'] ?: 'pz';
$qty = (float)$row['quantity'];
$defQty = (float)($row['default_quantity'] ?: 0);
if ($unit === 'conf') {
$frac = $qty - floor($qty + 0.001);
if ($frac > 0.001) $isOpened = true;
} elseif (in_array($unit, ['g','kg','ml','l']) && $defQty > 0 && $qty < $defQty - 0.001) {
$isOpened = true;
}
if (!$isOpened) continue;
$openedAt = $row['updated_at'];
$openedDays = estimateOpenedExpiryDaysPHP($row['name'], $row['category'], $row['location']);
if ($row['vacuum_sealed']) $openedDays = (int)round($openedDays * 1.5);
// Calculate new expiry from opened_at
$newExpiry = date('Y-m-d', strtotime($openedAt . " +{$openedDays} days"));
$upd = $db->prepare("UPDATE inventory SET opened_at = ?, expiry_date = ? WHERE id = ?");
$upd->execute([$openedAt, $newExpiry, $row['id']]);
}
}
/**
* Estimate shelf life in days for an opened product.
* Much shorter than sealed shelf life.
*/
function estimateOpenedExpiryDaysPHP(string $name, string $category, string $location): int {
$n = mb_strtolower($name);
$cat = mb_strtolower($category);
$loc = mb_strtolower($location);
// Freezer: opened items still last a long time
if ($loc === 'freezer') return 90;
// Dispensa: opened dry goods
if ($loc === 'dispensa') return 30;
// Specific product overrides (fridge)
if (preg_match('/latte\s+(fresco|intero|parzial|scremato)/', $n)) return 3;
if (preg_match('/latte\s+uht|latte\s+a\s+lunga/', $n)) return 5;
if (preg_match('/latte/', $n)) return 4;
if (preg_match('/yogurt/', $n)) return 3;
if (preg_match('/mozzarella|burrata|stracciatella/', $n)) return 2;
if (preg_match('/philadelphia|spalmabile/', $n)) return 7;
if (preg_match('/formaggio.*(fresco|ricotta|mascarpone|stracchino|crescenza)/', $n)) return 5;
if (preg_match('/parmigiano|grana|pecorino|provolone/', $n)) return 21;
if (preg_match('/formaggio/', $n)) return 10;
if (preg_match('/burro/', $n)) return 21;
if (preg_match('/panna/', $n)) return 3;
if (preg_match('/prosciutto\s+cotto|mortadella|wurstel/', $n)) return 3;
if (preg_match('/prosciutto\s+crudo|salame|bresaola|speck|pancetta|nduja/', $n)) return 7;
if (preg_match('/pollo|tacchino|maiale|manzo|vitello/', $n)) return 2;
if (preg_match('/salmone|tonno\s+fresco|pesce/', $n)) return 2;
if (preg_match('/passata|pelati|polpa|sugo/', $n)) return 5;
if (preg_match('/marmellata|confettura/', $n)) return 30;
if (preg_match('/miele/', $n)) return 180;
if (preg_match('/nutella/', $n)) return 60;
if (preg_match('/succo|spremuta/', $n)) return 4;
if (preg_match('/olio|aceto/', $n)) return 90;
if (preg_match('/vino|birra/', $n)) return 5;
if (preg_match('/limone|limmi/', $n)) return 21;
if (preg_match('/tonno\s+in\s+scatola|tonno\s+rio|sgombro\s+in/', $n)) return 3;
if (preg_match('/insalata|rucola|spinaci/', $n)) return 3;
// Category fallbacks
if (preg_match('/dairy|latticin|lait|dairies/', $cat)) return 5;
if (preg_match('/meat|carne|meats/', $cat)) return 3;
if (preg_match('/fish|pesce/', $cat)) return 2;
if (preg_match('/fruit|frutta/', $cat)) return 5;
if (preg_match('/verdur|vegetable|plant-based/', $cat)) return 5;
if (preg_match('/conserve/', $cat)) return 5;
if (preg_match('/condimenti|sauce/', $cat)) return 21;
if (preg_match('/bevand|beverage/', $cat)) return 4;
return 5; // safe default for fridge
}
/**
* Estimate sealed shelf life in days, with fridge/freezer extensions.
* Mirrors the JS estimateExpiryDays() function.
*/
function estimateSealedExpiryDaysPHP(string $name, string $category, string $location): int {
$n = mb_strtolower($name);
$cat = mb_strtolower($category);
$loc = mb_strtolower($location);
$days = null;
// Specific product overrides
if (preg_match('/latte\s+(fresco|intero|parzial|scremato)/', $n)) $days = 7;
elseif (preg_match('/latte\s+uht|latte\s+a\s+lunga/', $n)) $days = 90;
elseif (preg_match('/yogurt/', $n)) $days = 21;
elseif (preg_match('/mozzarella|burrata|stracciatella/', $n)) $days = 5;
elseif (preg_match('/formaggio\s+(fresco|ricotta|mascarpone|stracchino|crescenza)/', $n)) $days = 10;
elseif (preg_match('/parmigiano|grana|pecorino|provolone/', $n)) $days = 60;
elseif (preg_match('/burro/', $n)) $days = 60;
elseif (preg_match('/panna/', $n)) $days = 14;
elseif (preg_match('/prosciutto\s+cotto|mortadella|wurstel/', $n)) $days = 7;
elseif (preg_match('/prosciutto\s+crudo|salame|bresaola|speck/', $n)) $days = 30;
elseif (preg_match('/nduja/', $n)) $days = 90;
elseif (preg_match('/uova/', $n)) $days = 28;
elseif (preg_match('/pane\s+fresco|pane\s+in\s+cassetta/', $n)) $days = 5;
elseif (preg_match('/pane\s+confezionato|pan\s+carr|pancarrè/', $n)) $days = 14;
elseif (preg_match('/insalata|rucola|spinaci\s+freschi/', $n)) $days = 5;
elseif (preg_match('/pollo|tacchino|maiale|manzo|vitello|sovracosci|cosci/', $n)) $days = 3;
elseif (preg_match('/salmone|tonno\s+fresco|pesce/', $n) && !preg_match('/tonno\s+in\s+scatola|tonno\s+rio/', $n)) $days = 2;
elseif (preg_match('/tonno\s+in\s+scatola|tonno\s+rio|sgombro\s+in/', $n)) $days = 1095;
elseif (preg_match('/surgelat|frozen|findus|4\s*salti/', $n)) $days = 180;
elseif (preg_match('/gelato/', $n)) $days = 365;
elseif (preg_match('/succo|spremuta/', $n)) $days = 7;
elseif (preg_match('/birra|vino/', $n)) $days = 365;
elseif (preg_match('/acqua/', $n)) $days = 365;
elseif (preg_match('/mela|mele\b/', $n)) $days = 7;
elseif (preg_match('/arancia|arance|mandarini|agrumi/', $n)) $days = 7;
elseif (preg_match('/banana|banane/', $n)) $days = 5;
elseif (preg_match('/pera|pere\b|fragola|fragole|uva|kiwi/', $n)) $days = 5;
elseif (preg_match('/carota|carote|zucchina|zucchine|peperoni|melanzane/', $n)) $days = 7;
elseif (preg_match('/broccoli|cavolfiore|cavolo|spinaci|bietola/', $n)) $days = 5;
elseif (preg_match('/cipolla|cipolle/', $n)) $days = 10;
elseif (preg_match('/patata|patate/', $n)) $days = 14;
elseif (preg_match('/biscott|cracker|grissini|fette\s+biscott/', $n)) $days = 180;
elseif (preg_match('/nutella|marmellata|miele/', $n)) $days = 365;
elseif (preg_match('/passata|pelati|pomodor/', $n)) $days = 730;
elseif (preg_match('/olio|aceto/', $n)) $days = 548;
if ($days === null) {
// Category fallbacks
$catMap = [
'latticini' => 7, 'carne' => 4, 'pesce' => 3, 'frutta' => 7, 'verdura' => 7,
'pasta' => 730, 'pane' => 4, 'surgelati' => 180, 'bevande' => 365, 'condimenti' => 365,
'snack' => 180, 'conserve' => 730, 'cereali' => 365, 'igiene' => 1095, 'pulizia' => 1095,
];
$days = 180;
foreach ($catMap as $key => $d) {
if (strpos($cat, $key) !== false) { $days = $d; break; }
}
}
// Fridge extends shelf life for produce and short-lived items
if ($loc === 'frigo') {
if (preg_match('/mela|mele/', $n)) $days = max($days, 28);
elseif (preg_match('/arancia|arance|agrumi|mandarini|limone|limoni/', $n)) $days = max($days, 21);
elseif (preg_match('/carota|carote/', $n)) $days = max($days, 21);
elseif (preg_match('/cipolla/', $n)) $days = max($days, 14);
elseif (preg_match('/patata|patate/', $n)) $days = max($days, 21);
elseif (preg_match('/pera|pere/', $n)) $days = max($days, 21);
elseif (preg_match('/kiwi/', $n)) $days = max($days, 28);
elseif (preg_match('/uva/', $n)) $days = max($days, 14);
elseif (preg_match('/fragola|fragole/', $n)) $days = max($days, 7);
elseif (preg_match('/peperoni/', $n)) $days = max($days, 14);
elseif (preg_match('/zucchina|zucchine/', $n)) $days = max($days, 14);
elseif (preg_match('/melanzane/', $n)) $days = max($days, 14);
elseif (preg_match('/broccoli|cavolfiore|cavolo/', $n)) $days = max($days, 10);
elseif ($days <= 7 && preg_match('/frutta|fruit|verdur|vegetable|plant-based/', $cat)) {
$days = (int)round($days * 2);
}
}
// Freezer extends shelf life significantly
if ($loc === 'freezer' && $days < 180) {
if ($days <= 4) $days = 120;
elseif ($days <= 14) $days = 75;
elseif ($days <= 30) $days = 120;
else $days = max($days, 180);
}
return $days;
}
/**
* Recalculate expiry for sealed (non-opened) fridge items with new fridge-aware logic.
*/
function recalcSealedFridgeExpiry(PDO $db): void {
$stmt = $db->query("
SELECT i.id, i.added_at, i.vacuum_sealed, i.opened_at, i.expiry_date,
p.name, p.category
FROM inventory i
JOIN products p ON i.product_id = p.id
WHERE i.location = 'frigo' AND i.opened_at IS NULL AND i.quantity > 0
");
$rows = $stmt->fetchAll();
foreach ($rows as $row) {
$days = estimateSealedExpiryDaysPHP($row['name'], $row['category'], 'frigo');
if ($row['vacuum_sealed']) $days = getVacuumExpiryDaysPHP($days);
$newExpiry = date('Y-m-d', strtotime($row['added_at'] . " +{$days} days"));
// Only extend expiry, never shorten it
if ($row['expiry_date'] && $newExpiry <= $row['expiry_date']) continue;
$upd = $db->prepare("UPDATE inventory SET expiry_date = ? WHERE id = ?");
$upd->execute([$newExpiry, $row['id']]);
}
}
function getVacuumExpiryDaysPHP(int $baseDays): int {
if ($baseDays <= 7) return (int)round($baseDays * 3);
if ($baseDays <= 14) return (int)round($baseDays * 3);
if ($baseDays <= 30) return (int)round($baseDays * 2.5);
if ($baseDays <= 90) return (int)round($baseDays * 2.5);
return (int)round($baseDays * 1.5);
}