diff --git a/api/index.php b/api/index.php index e0a1024..5662030 100644 --- a/api/index.php +++ b/api/index.php @@ -2681,6 +2681,10 @@ function smartShopping(PDO $db): void { : ($daysSinceFirst >= 7 ? ($useCount / $daysSinceFirst) * 30 : $useCount * 0.5); // Days since last use/purchase — measures recency $daysSinceLastUse = $lastOut ? ($now - $lastOut) / 86400 : ($lastIn ? ($now - $lastIn) / 86400 : 999); + // Days since last PURCHASE specifically + $daysSinceLastBuy = $lastIn ? ($now - $lastIn) / 86400 : 999; + // Product was restocked very recently (within 3 days) — suppress non-expiry urgency + $justRestocked = $daysSinceLastBuy <= 3; // Is this a frequently used product? (≥ 1.5 uses/month) $isFrequent = $usesPerMonth >= 1.5; // Is it a regular product? (≥ 0.5 uses/month = at least once every 2 months) @@ -2766,9 +2770,10 @@ function smartShopping(PDO $db): void { $score += 20; } - // Absolute minimum stock fallback: flag items with very low stock regardless of usage frequency. - // Applies to products bought at least once, even if rarely used. - if ($urgency === 'none' && $buyCount >= 1 && $qty > 0) { + // Absolute minimum stock fallback: flag items with critically low stock. + // Requires: product is regularly consumed (isRegular), bought ≥2 times (proven staple), + // and stock is clearly depleted relative to normal purchase (pctLeft < 80). + if ($urgency === 'none' && $isRegular && $buyCount >= 2 && $qty > 0 && $pctLeft < 80) { if ($unit === 'conf') { if ($qty <= 1) { $urgency = 'high'; @@ -2805,6 +2810,12 @@ function smartShopping(PDO $db): void { // Is already on Bring? (fuzzy token match — mirrors JS _findSimilarItem logic) $onBring = _productOnBring($p['name'], $bringItems); + // "Just restocked" suppression: if bought in the last 3 days AND stock is above 50% + // of reference qty, skip non-expiry urgency flags. The product doesn't need rebuying yet. + if ($justRestocked && $pctLeft >= 50 && !$isExpired && !$isExpiringSoon) { + continue; + } + $items[] = [ 'product_id' => $pid, 'name' => $p['name'], diff --git a/assets/js/app.js b/assets/js/app.js index 2020033..e8ce9c9 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -4075,7 +4075,7 @@ function setPzFraction(frac) { // ===== LOW STOCK → BRING! PROMPT ===== function isLowStock(totalRemaining, unit, defaultQty) { if (totalRemaining <= 0) return true; // fully depleted → definitely needs restocking - if (unit === 'pz') return totalRemaining <= 2; + if (unit === 'pz') return totalRemaining <= 1; // only 1 piece left if (unit === 'conf') return totalRemaining <= 1; // Weight/volume: use percentage of default_qty or fixed threshold if (defaultQty > 0) return totalRemaining <= defaultQty * 0.25;