fix: pkgUnit fallback for /kg+/L, fuzzy smart lookup by word-prefix
This commit is contained in:
+24
-3
@@ -6679,7 +6679,24 @@ function getAllShoppingPrices(PDO $db): void {
|
|||||||
foreach ($clientItems as $ci) {
|
foreach ($clientItems as $ci) {
|
||||||
$name = trim($ci['name'] ?? '');
|
$name = trim($ci['name'] ?? '');
|
||||||
if ($name === '') continue;
|
if ($name === '') continue;
|
||||||
|
|
||||||
|
// 1) Exact match by name or shopping_name
|
||||||
$si = $smartByName[mb_strtolower($name)] ?? null;
|
$si = $smartByName[mb_strtolower($name)] ?? null;
|
||||||
|
|
||||||
|
// 2) Prefix-word fallback: "Salame" → "Salame Paesano", "Penne" → "Penne rigate"
|
||||||
|
// Match when the Bring! name is a word-prefix of a smart key (case-insensitive).
|
||||||
|
if ($si === null) {
|
||||||
|
$nameLower = mb_strtolower($name);
|
||||||
|
foreach ($smartByName as $smartKey => $candidate) {
|
||||||
|
// smartKey starts with the Bring! name (exact word boundary)
|
||||||
|
if (str_starts_with($smartKey, $nameLower)
|
||||||
|
&& (strlen($smartKey) === strlen($nameLower) || $smartKey[strlen($nameLower)] === ' ')) {
|
||||||
|
$si = $candidate;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$items[] = [
|
$items[] = [
|
||||||
'name' => $name,
|
'name' => $name,
|
||||||
'quantity' => (float)(($si['suggested_qty'] ?? $si['buy_qty'] ?? null) ?? ($ci['quantity'] ?? 1)),
|
'quantity' => (float)(($si['suggested_qty'] ?? $si['buy_qty'] ?? null) ?? ($ci['quantity'] ?? 1)),
|
||||||
@@ -6827,14 +6844,15 @@ function _calcEstimatedTotal(float $pricePerUnit, string $priceUnitLabel, float
|
|||||||
$sub = strtolower($pkgUnit);
|
$sub = strtolower($pkgUnit);
|
||||||
if ($sub === 'g') $weightKg = $qty * $defQty / 1000.0;
|
if ($sub === 'g') $weightKg = $qty * $defQty / 1000.0;
|
||||||
elseif ($sub === 'kg') $weightKg = $qty * $defQty;
|
elseif ($sub === 'kg') $weightKg = $qty * $defQty;
|
||||||
|
} elseif (($unit === 'conf' || $unit === 'pz') && $defQty > 0 && empty($pkgUnit)) {
|
||||||
|
// pkgUnit not recorded in DB — for /kg prices assume defQty is in grams
|
||||||
|
// (vast majority of grocery packages: pancetta 80g, formaggio 200g, etc.)
|
||||||
|
$weightKg = $qty * $defQty / 1000.0;
|
||||||
} elseif ($unit === 'g') {
|
} elseif ($unit === 'g') {
|
||||||
$weightKg = $qty / 1000.0;
|
$weightKg = $qty / 1000.0;
|
||||||
} elseif ($unit === 'kg') {
|
} elseif ($unit === 'kg') {
|
||||||
$weightKg = $qty;
|
$weightKg = $qty;
|
||||||
}
|
}
|
||||||
// Cannot convert unit to kg → return null instead of misleadingly using
|
|
||||||
// price_per_unit as total (which would inflate estimates for items like
|
|
||||||
// "3 pz Salame" priced at €21.50/kg, returning €21.50 for just ~300g).
|
|
||||||
if ($weightKg <= 0) return null;
|
if ($weightKg <= 0) return null;
|
||||||
return round($pricePerUnit * $weightKg, 2);
|
return round($pricePerUnit * $weightKg, 2);
|
||||||
}
|
}
|
||||||
@@ -6846,6 +6864,9 @@ function _calcEstimatedTotal(float $pricePerUnit, string $priceUnitLabel, float
|
|||||||
$sub = strtolower($pkgUnit);
|
$sub = strtolower($pkgUnit);
|
||||||
if ($sub === 'ml') $volumeL = $qty * $defQty / 1000.0;
|
if ($sub === 'ml') $volumeL = $qty * $defQty / 1000.0;
|
||||||
elseif ($sub === 'l') $volumeL = $qty * $defQty;
|
elseif ($sub === 'l') $volumeL = $qty * $defQty;
|
||||||
|
} elseif (($unit === 'conf' || $unit === 'pz') && $defQty > 0 && empty($pkgUnit)) {
|
||||||
|
// pkgUnit not recorded — for /L prices assume defQty is in ml
|
||||||
|
$volumeL = $qty * $defQty / 1000.0;
|
||||||
} elseif ($unit === 'ml') {
|
} elseif ($unit === 'ml') {
|
||||||
$volumeL = $qty / 1000.0;
|
$volumeL = $qty / 1000.0;
|
||||||
} elseif ($unit === 'l') {
|
} elseif ($unit === 'l') {
|
||||||
|
|||||||
+1
-1
@@ -1462,6 +1462,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="assets/js/app.js?v=20260508b"></script>
|
<script src="assets/js/app.js?v=20260508c"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user