From a9a512e014aec5ab6e23471b8232458ca76e298b Mon Sep 17 00:00:00 2001 From: dadaloop82 Date: Mon, 11 May 2026 17:19:00 +0000 Subject: [PATCH] fix: clear opened_at on sealed packages row during split MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When usage splits a row into 'whole sealed packages' + 'opened fraction', the sealed row was updated without clearing opened_at — if it had been opened previously, the stale flag would persist and wrongly show 'aperto da N giorni' on intact packages. Now all 3 split paths (conf early-split, conf post-split, g/ml/l split) explicitly set opened_at = NULL on the sealed row. --- api/index.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/index.php b/api/index.php index e4c5f57..f8af61c 100644 --- a/api/index.php +++ b/api/index.php @@ -1227,7 +1227,7 @@ function useFromInventory(PDO $db): void { // Has both whole and fractional, and we're using less than or equal to the fractional part if ($wholeConfs >= 1 && $fraction > 0.001 && $quantity <= $fraction + 0.001) { // Split: keep whole confs in original row, create new row for opened part - $stmt3 = $db->prepare("UPDATE inventory SET quantity = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?"); + $stmt3 = $db->prepare("UPDATE inventory SET quantity = ?, opened_at = NULL, updated_at = CURRENT_TIMESTAMP WHERE id = ?"); $stmt3->execute([$wholeConfs, $existing['id']]); // Get expiry and vacuum_sealed from original row @@ -1305,7 +1305,7 @@ function useFromInventory(PDO $db): void { $newFrac = round($newQty - $newWhole, 6); if ($newFrac > 0.001 && $newWhole >= 1) { // Keep whole confs in original row (no opened_at, sealed expiry unchanged) - $stmt = $db->prepare("UPDATE inventory SET quantity = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?"); + $stmt = $db->prepare("UPDATE inventory SET quantity = ?, opened_at = NULL, updated_at = CURRENT_TIMESTAMP WHERE id = ?"); $stmt->execute([$newWhole, $existing['id']]); // New row for the opened fraction with short shelf-life expiry $stmt = $db->prepare("INSERT INTO inventory (product_id, location, quantity, expiry_date, vacuum_sealed, opened_at) VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)"); @@ -1321,7 +1321,7 @@ function useFromInventory(PDO $db): void { $newRemainder = round($newQty - $newWholePkgs * $defQty, 6); if ($newRemainder > $defQty * 0.01 && $newWholePkgs >= 1) { // Keep whole packages in original row (no opened_at, sealed expiry unchanged) - $stmt = $db->prepare("UPDATE inventory SET quantity = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?"); + $stmt = $db->prepare("UPDATE inventory SET quantity = ?, opened_at = NULL, updated_at = CURRENT_TIMESTAMP WHERE id = ?"); $stmt->execute([$newWholePkgs * $defQty, $existing['id']]); // New row for the opened partial package with short shelf-life expiry $stmt = $db->prepare("INSERT INTO inventory (product_id, location, quantity, expiry_date, vacuum_sealed, opened_at) VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)");