diff --git a/assets/js/app.js b/assets/js/app.js index 1528f70..aa36c35 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -1001,20 +1001,40 @@ function showAlertItemDetail(inventoryId, productId) { }); } +function formatSubRemainder(amt, pkgUnit) { + const uL = { 'kg': 'kg', 'g': 'g', 'l': 'L', 'ml': 'ml' }; + if (pkgUnit === 'l' && amt < 1) return `${Math.round(amt * 1000)}ml`; + if (pkgUnit === 'kg' && amt < 1) return `${Math.round(amt * 1000)}g`; + if (pkgUnit === 'ml' || pkgUnit === 'g') return `${Math.round(amt)}${uL[pkgUnit] || pkgUnit}`; + return `${Math.round(amt * 10) / 10}${uL[pkgUnit] || pkgUnit}`; +} + function formatQuantity(qty, unit, defaultQty, packageUnit) { if (!qty && qty !== 0) return ''; const n = parseFloat(qty); const unitLabels = { 'pz': 'pz', 'kg': 'kg', 'g': 'g', 'l': 'L', 'ml': 'ml', 'conf': 'conf' }; const label = unitLabels[unit] || unit || 'pz'; - let result; - if (n === Math.floor(n)) result = `${Math.floor(n)} ${label}`; - else if (unit === 'pz' || unit === 'conf') result = `${Math.round(n)} ${label}`; - else result = `${n.toFixed(1)} ${label}`; - // Add package info for conf + + // Special handling for conf with partial packages if (unit === 'conf' && packageUnit && defaultQty > 0) { const pkgLabel = unitLabels[packageUnit] || packageUnit; - result += ` (da ${defaultQty}${pkgLabel})`; + const wholeConf = Math.floor(n + 0.001); + const fractionalConf = Math.round((n - wholeConf) * 1000) / 1000; + + if (fractionalConf < 0.01) { + return `${wholeConf} conf (da ${defaultQty}${pkgLabel})`; + } + const remainderText = formatSubRemainder(fractionalConf * defaultQty, packageUnit); + if (wholeConf > 0) { + return `${wholeConf} conf (da ${defaultQty}${pkgLabel}) + ${remainderText}`; + } + return remainderText; } + + let result; + if (n === Math.floor(n)) result = `${Math.floor(n)} ${label}`; + else if (unit === 'pz') result = `${Math.round(n)} ${label}`; + else result = `${n.toFixed(1)} ${label}`; return result; } @@ -1024,19 +1044,30 @@ function formatQuantityParts(qty, unit, defaultQty, packageUnit) { const n = parseFloat(qty) || 0; const unitLabels = { 'pz': 'pz', 'kg': 'kg', 'g': 'g', 'l': 'L', 'ml': 'ml', 'conf': 'conf' }; const label = unitLabels[unit] || unit || 'pz'; + + // Special handling for conf with partial packages + if (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; + + if (fractionalConf < 0.01) { + return { mainQty: `${wholeConf}`, unitLabel: 'conf', packageDetail: `da ${defaultQty}${pkgLabel}`, fraction: '' }; + } + const remainderText = formatSubRemainder(fractionalConf * defaultQty, packageUnit); + if (wholeConf > 0) { + return { mainQty: `${wholeConf}`, unitLabel: 'conf', packageDetail: `da ${defaultQty}${pkgLabel}`, fraction: `+ ${remainderText}` }; + } + return { mainQty: remainderText, unitLabel: '', packageDetail: '', fraction: '' }; + } + let mainQty; if (n === Math.floor(n)) mainQty = `${Math.floor(n)}`; - else if (unit === 'pz' || unit === 'conf') mainQty = `${Math.round(n)}`; + else if (unit === 'pz') mainQty = `${Math.round(n)}`; else mainQty = `${n.toFixed(1)}`; let packageDetail = ''; - if (unit === 'conf' && packageUnit && defaultQty > 0) { - const pkgLabel = unitLabels[packageUnit] || packageUnit; - packageDetail = `da ${defaultQty}${pkgLabel}`; - } - let fraction = ''; - // For conf, defaultQty is the package SIZE (e.g. 300g), not a count; fraction doesn't apply if (unit !== 'conf' && defaultQty && defaultQty > 1) { const d = parseFloat(defaultQty); const ratio = n / d; diff --git a/data/dispensa.db b/data/dispensa.db index ce272dc..cc49dc5 100644 Binary files a/data/dispensa.db and b/data/dispensa.db differ diff --git a/index.html b/index.html index af40356..65651b1 100644 --- a/index.html +++ b/index.html @@ -878,6 +878,6 @@ - +