Fix smart shopping: skip one-time purchases, better frequency for short history

- Products bought only once (buy_count=1) and out of stock are skipped entirely
  (not enough history to predict repurchase need, e.g. piadelle)
- usesPerMonth for items tracked 7-30 days now normalized instead of using raw count
- usesPerMonth for items tracked <7 days halved to avoid inflation
- Critical urgency now requires buy_count >= 2
This commit is contained in:
dadaloop82
2026-03-29 19:58:43 +00:00
parent 4b3e5f2ce4
commit b954be4cac
2 changed files with 9 additions and 3 deletions
+9 -3
View File
@@ -2308,7 +2308,10 @@ function smartShopping(PDO $db): void {
// --- Frequency & recency metrics --- // --- Frequency & recency metrics ---
// Uses per month (30 days) — measures how frequently the product is actually used // Uses per month (30 days) — measures how frequently the product is actually used
$usesPerMonth = $daysSinceFirst >= 30 ? ($useCount / $daysSinceFirst) * 30 : $useCount; // For items tracked < 30 days, normalize over at least 14 days to avoid inflation
$usesPerMonth = $daysSinceFirst >= 30
? ($useCount / $daysSinceFirst) * 30
: ($daysSinceFirst >= 7 ? ($useCount / $daysSinceFirst) * 30 : $useCount * 0.5);
// Days since last use/purchase — measures recency // Days since last use/purchase — measures recency
$daysSinceLastUse = $lastOut ? ($now - $lastOut) / 86400 : ($lastIn ? ($now - $lastIn) / 86400 : 999); $daysSinceLastUse = $lastOut ? ($now - $lastOut) / 86400 : ($lastIn ? ($now - $lastIn) / 86400 : 999);
// Is this a frequently used product? (≥ 1.5 uses/month) // Is this a frequently used product? (≥ 1.5 uses/month)
@@ -2325,12 +2328,15 @@ function smartShopping(PDO $db): void {
// Out of stock // Out of stock
if ($qty <= 0) { if ($qty <= 0) {
if ($isFrequent && $isRecent) { if ($isFrequent && $isRecent && $buyCount >= 2) {
// Frequently used AND recently active → critical // Frequently used, recently active, AND bought multiple times → critical
$urgency = 'critical'; $urgency = 'critical';
$reasons[] = 'Esaurito'; $reasons[] = 'Esaurito';
$score += 100; $score += 100;
if ($useCount >= 5) { $score += 20; $reasons[] = "Uso frequente ({$useCount}x)"; } if ($useCount >= 5) { $score += 20; $reasons[] = "Uso frequente ({$useCount}x)"; }
} elseif ($isFrequent && $isRecent && $buyCount == 1) {
// Frequent use but only bought once — not yet a proven staple → skip
continue;
} elseif ($isRegular && $isRecent && ($useCount >= 4 || $buyCount >= 3)) { } elseif ($isRegular && $isRecent && ($useCount >= 4 || $buyCount >= 3)) {
// Regularly used, recently active → high // Regularly used, recently active → high
$urgency = 'high'; $urgency = 'high';
BIN
View File
Binary file not shown.