diff --git a/assets/js/app.js b/assets/js/app.js index 795f5fa..520dbb1 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -7203,13 +7203,20 @@ function syncRecipeUseQtyUnitBadge() { // Structured quantity display for inventory cards. // Returns { mainQty: '10', unitLabel: 'conf', packageDetail: 'da 36g', fraction: '¼' } -function formatQuantityParts(qty, unit, defaultQty, packageUnit) { - const n = parseFloat(qty) || 0; +function formatQuantityParts(qty, unit, defaultQty, packageUnit, displayUnitKey) { + let n = parseFloat(qty) || 0; const unitLabels = { 'pz': t('units.pz'), 'g': 'g', 'ml': 'ml', 'conf': t('units.conf') }; - const label = unitLabels[unit] || unit || t('units.pz'); + const customUnit = displayUnitKey ? CUSTOM_UNITS.find(u => u.key === displayUnitKey && u.base_unit === unit) : null; + let label; + if (customUnit) { + n = n / customUnit.factor; + label = `${customUnit.icon} ${customUnit.label}`.trim(); + } else { + label = unitLabels[unit] || unit || t('units.pz'); + } - // Special handling for conf with partial packages - if (unit === 'conf' && packageUnit && defaultQty > 0) { + // Special handling for conf with partial packages (custom units never apply to conf) + if (!customUnit && unit === 'conf' && packageUnit && defaultQty > 0) { const pkgLabel = unitLabels[packageUnit] || packageUnit; const wholeConf = Math.floor(n + 0.001); const fractionalConf = Math.round((n - wholeConf) * 1000) / 1000; @@ -7226,13 +7233,13 @@ function formatQuantityParts(qty, unit, defaultQty, packageUnit) { let mainQty; if (n === Math.floor(n)) mainQty = `${Math.floor(n)}`; - else if (unit === 'pz') mainQty = _pzFractionLabel(n); - else mainQty = `${n.toFixed(1)}`; - + else if (!customUnit && unit === 'pz') mainQty = _pzFractionLabel(n); + else mainQty = `${n.toFixed(customUnit ? 2 : 1)}`; + let packageDetail = ''; let fraction = ''; // pz = piece count only; default_quantity may hold legacy avg weight — ignore for display - if (unit !== 'conf' && unit !== 'pz' && defaultQty && defaultQty > 1) { + if (!customUnit && unit !== 'conf' && unit !== 'pz' && defaultQty && defaultQty > 1) { const d = parseFloat(defaultQty); const ratio = n / d; const remainder = ratio - Math.floor(ratio); @@ -7242,7 +7249,7 @@ function formatQuantityParts(qty, unit, defaultQty, packageUnit) { else fraction = '¾'; } } - + return { mainQty, unitLabel: label, packageDetail, fraction }; }