From 97e420df2f2ba4be9eb175f587deac1ef5495654 Mon Sep 17 00:00:00 2001 From: Henrik Jess Nielsen Date: Fri, 30 Oct 2026 13:58:26 +0100 Subject: [PATCH] fix(stats): estimate + budget now react to quantity changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- templates/index.html | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/templates/index.html b/templates/index.html index 9dd87b8..f0855a3 100644 --- a/templates/index.html +++ b/templates/index.html @@ -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') : '—';