feat(stats): Estimeret total uses real offer prices for matched items
When a catalog item matches a current Lübeck offer, compute its line cost from the offer instead of the hardcoded estimate: offer price is per unit (e.g. per 100 g), so multiply by the item's quantity (qtyToKg × €/kg × rate). Only weight-based offers (g/kg) are converted — units that can't be matched cleanly keep the estimate, so the total never goes garbage. Data-offer-* attrs on matched rows; recalcAll prefers the offer line price. The total (and the budget-per-day) now move toward actual prices as matches land.
This commit is contained in:
@@ -255,7 +255,8 @@
|
||||
<div class="items-table" id="sec-{{ sec.id }}">
|
||||
{% for item in sec.varer %}
|
||||
{% set s = saved.get(item.key, {}) %}
|
||||
<div class="item-row{% if s.get('checked') %} checked{% endif %}" data-key="{{ item.key }}" data-est="{{ item.est }}">
|
||||
{% set od = deals.get(item.key) %}
|
||||
<div class="item-row{% if s.get('checked') %} checked{% endif %}" data-key="{{ item.key }}" data-est="{{ item.est }}"{% if od and od.unit %} data-offer-eur="{{ od.price_eur }}" data-offer-unit="{{ od.unit }}" data-offer-size="{{ od.size_from or '' }}"{% endif %}>
|
||||
<div class="item-check"><input type="checkbox" {% if s.get('checked') %}checked{% endif %} onchange="toggleRow(this)"></div>
|
||||
<div class="item-name">
|
||||
{{ item.name }}
|
||||
@@ -548,6 +549,23 @@
|
||||
<script>
|
||||
const SEC_IDS = ['koed','frost','mejeri','pasta','groent','konserves','indisk','mex','drikke','hus'];
|
||||
const DK_SAVINGS_RATIO = 0.35;
|
||||
const EUR_DKK = {{ eur_dkk }};
|
||||
|
||||
// For a catalog row that matches a current Lübeck offer, work out the real line
|
||||
// price from the offer: offer price is per unit (e.g. per 100 g), so multiply by
|
||||
// the item's quantity. Only weight-based offers (g/kg) are converted reliably;
|
||||
// everything else keeps the hardcoded estimate.
|
||||
function offerLineDkk(row) {
|
||||
const eur = parseFloat(row.dataset.offerEur);
|
||||
const unit = (row.dataset.offerUnit || '').toLowerCase();
|
||||
const size = parseFloat(row.dataset.offerSize);
|
||||
if (isNaN(eur) || !(unit === 'g' || unit === 'kg')) return null;
|
||||
const kg = qtyToKg(row.querySelector('.item-qty input')
|
||||
? (row.querySelector('.item-qty input').value.trim() || row.querySelector('.item-qty input').placeholder) : '');
|
||||
if (kg <= 0) return null;
|
||||
const offerKg = unit === 'kg' ? (size || 1) : (size || 100) / 1000; // offer priced per `size` units
|
||||
return kg * (eur / offerKg) * EUR_DKK;
|
||||
}
|
||||
let saveTimer = null;
|
||||
|
||||
function showSaved() {
|
||||
@@ -682,7 +700,10 @@ function recalcAll() {
|
||||
// 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.querySelectorAll('[data-est]').forEach(r => {
|
||||
const offer = offerLineDkk(r); // real offer price if matched
|
||||
est += (offer != null) ? offer : (parseFloat(r.dataset.est) || 0);
|
||||
});
|
||||
document.getElementById('stat-est').textContent = est > 0 ? Math.round(est).toLocaleString('da-DK') : '—';
|
||||
|
||||
// Estimeret besparelse = 35% of actual spend once entered, else of the estimate.
|
||||
|
||||
Reference in New Issue
Block a user