fix(stats): estimate + budget now react to quantity changes
Estimeret total used each row's fixed line estimate, so changing a quantity (6 → 8) didn't move the price or the per-day budget unless the item matched a weight offer. Now scale an unmatched item's estimate by the quantity ratio (current number ÷ default number via qtyNum), so doubling the amount ~doubles its estimate. Combined with updateQty→recalcAll, price and budget update live on every quantity edit.
This commit is contained in:
@@ -642,6 +642,12 @@ function qtyToPacks(s) {
|
||||
const m = (s || '').match(/([\d.]+)\s*stk/i);
|
||||
return m ? parseFloat(m[1]) : 0;
|
||||
}
|
||||
// Leading number of a quantity string ("6 × ½ kg" -> 6, "8 dåser" -> 8), used to
|
||||
// scale a fixed line estimate when the quantity changes.
|
||||
function qtyNum(s) {
|
||||
const m = (s || '').replace(',', '.').replace(/~/g, '').replace(/½/g, '0.5').match(/([\d.]+)/);
|
||||
return m ? parseFloat(m[1]) : null;
|
||||
}
|
||||
function curQty(row) {
|
||||
const inp = row.querySelector('.item-qty input');
|
||||
return inp ? (inp.value.trim() || inp.placeholder) : '';
|
||||
@@ -702,7 +708,15 @@ function recalcAll() {
|
||||
let est = 0;
|
||||
document.querySelectorAll('[data-est]').forEach(r => {
|
||||
const offer = offerLineDkk(r); // real offer price if matched
|
||||
est += (offer != null) ? offer : (parseFloat(r.dataset.est) || 0);
|
||||
if (offer != null) { est += offer; return; }
|
||||
let base = parseFloat(r.dataset.est) || 0;
|
||||
const qi = r.querySelector('.item-qty input'); // scale estimate with quantity
|
||||
if (qi) {
|
||||
const cur = qtyNum(qi.value.trim() || qi.placeholder);
|
||||
const def = qtyNum(qi.placeholder);
|
||||
if (cur != null && def != null && def > 0) base = base * (cur / def);
|
||||
}
|
||||
est += base;
|
||||
});
|
||||
document.getElementById('stat-est').textContent = est > 0 ? Math.round(est).toLocaleString('da-DK') : '—';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user