diff --git a/templates/index.html b/templates/index.html index 0bca1c8..8b10d7c 100644 --- a/templates/index.html +++ b/templates/index.html @@ -199,18 +199,18 @@
Aftensmadsdage
-
~40
-
fra én tur
+
—
+
kød + pasta-retter
Kødportioner
-
24
+
—
à 500g i fryseren
Estimeret total
-
~4.400
-
kr. inkl. Rema
+
—
+
kr. for listen
Faktisk betalt
@@ -259,7 +259,7 @@
{% endfor %} {% for e in by_sec.get(sec.id, []) %} -
+
{{ e.name }} @@ -282,7 +282,7 @@
{% for o in picks %} {% set ps = saved.get(o.id, {}) %} -
+
{{ o.heading_da or o.heading }} @@ -317,7 +317,7 @@ {% set sk_extras = by_sec.get(sk, []) %} {% if sk_extras %} {% for e in sk_extras %} -
+
{{ e.name }}{% if e.note %}{{ e.note }}{% endif %}
{% if e.price %}{{ "{:.2f}".format(e.price).replace(".",",") }}{% else %}—{% endif %}
@@ -352,7 +352,7 @@
{% for e in loose %} -
+
{{ e.name }}{% if e.note %}{{ e.note }}{% endif %}
{% if e.price %}{{ "{:.2f}".format(e.price).replace(".",",") }}{% else %}—{% endif %}
@@ -523,10 +523,47 @@ function updateQty(input) { input.classList.toggle('modified', input.value.trim() !== '' && input.value.trim() !== defaultQty); const price = parseFloat(row.querySelector('.item-actual input').value); const checked = row.querySelector('input[type=checkbox]').checked ? 1 : 0; + computeMeals(); // meat portions / dinner days react to qty clearTimeout(debounceTimers[key + '_qty']); debounceTimers[key + '_qty'] = setTimeout(() => saveToServer(key, isNaN(price) ? null : price, checked), 600); } +// Parse a Danish quantity string ("6 × ½ kg", "3 kg", "~700g", "2 stk.") into +// kilograms / pack count, for the meat-portions and dinner-days estimates. +function qtyToKg(s) { + if (!s) return 0; + s = s.toLowerCase().replace(/~/g, '').replace(',', '.') + .replace(/½/g, '0.5').replace(/¼/g, '0.25').replace(/¾/g, '0.75'); + let m = s.match(/([\d.]+)\s*[×x]\s*([\d.]+)\s*kg/); + if (m) return parseFloat(m[1]) * parseFloat(m[2]); + m = s.match(/([\d.]+)\s*[×x]\s*([\d.]+)\s*g\b/); + if (m) return parseFloat(m[1]) * parseFloat(m[2]) / 1000; + m = s.match(/([\d.]+)\s*kg/); + if (m) return parseFloat(m[1]); + m = s.match(/([\d.]+)\s*g\b/); + if (m) return parseFloat(m[1]) / 1000; + return 0; +} +function qtyToPacks(s) { + const m = (s || '').match(/([\d.]+)\s*stk/i); + return m ? parseFloat(m[1]) : 0; +} +function curQty(row) { + const inp = row.querySelector('.item-qty input'); + return inp ? (inp.value.trim() || inp.placeholder) : ''; +} +function computeMeals() { + let meatKg = 0; + document.querySelectorAll('#sec-koed .item-row').forEach(r => { meatKg += qtyToKg(curQty(r)); }); + let packs = 0; + document.querySelectorAll('#sec-pasta .item-row').forEach(r => { packs += qtyToPacks(curQty(r)); }); + const portions = Math.round(meatKg / 0.5); + const dp = document.getElementById('stat-portions'); + const dd = document.getElementById('stat-days'); + if (dp) dp.textContent = portions > 0 ? portions : '—'; + if (dd) dd.textContent = (portions + Math.round(packs)) > 0 ? (portions + Math.round(packs)) : '—'; +} + function recalcAll() { const sections = document.querySelectorAll('.items-table'); let totalActual = 0; let hasAny = false; @@ -547,6 +584,14 @@ function recalcAll() { ['s-total-citti','s-total-all','stat-actual'].forEach(id => document.getElementById(id).textContent = '—'); document.getElementById('total-savings').textContent = '~1.800'; } + + // Estimeret total = whole current list (fixed catalog + added items + + // picked offers), live from each row's data-est. Reflects add/remove/pick. + let est = 0; + document.querySelectorAll('[data-est]').forEach(r => { est += parseFloat(r.dataset.est) || 0; }); + document.getElementById('stat-est').textContent = est > 0 ? Math.round(est).toLocaleString('da-DK') : '—'; + + computeMeals(); } // Item modal @@ -616,14 +661,17 @@ function appendExtraToSection(data) { const noteHtml = data.note ? `${data.note}` : ''; const row = document.createElement('div'); row.className = 'extra-item-row'; row.id = `extra-${data.id}`; + row.dataset.est = data.price || 0; row.innerHTML = `
${data.name}${noteHtml}
${priceStr}
`; table.appendChild(row); + recalcAll(); } async function deleteExtra(id, btn) { if (!confirm('Slet denne vare?')) return; await fetch(`/api/item/${id}`, {method: 'DELETE'}); document.getElementById(`extra-${id}`).remove(); + recalcAll(); } // Category modal @@ -697,6 +745,7 @@ async function removeOffer(id) { if (!document.querySelectorAll('#picks-table .item-row').length) { document.getElementById('sec-picks').style.display = 'none'; } + recalcAll(); showSaved(); } diff --git a/templates/tilbud.html b/templates/tilbud.html index 55e0809..5a71be0 100644 --- a/templates/tilbud.html +++ b/templates/tilbud.html @@ -117,14 +117,27 @@ {% endfor %}
+ + +