From 82cf4b34ac2d6210c4e7b9cb438e813953153258 Mon Sep 17 00:00:00 2001 From: Jakob Date: Mon, 23 Mar 2026 16:41:49 +0100 Subject: [PATCH] =?UTF-8?q?Fix=20summary=20amount=20calculation=20and=20ad?= =?UTF-8?q?d=20=C3=96resutj=C3=A4mning=20row?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Amount = round(volume, 2) * round(avg_price, 2) - Add Öresutjämning showing difference between transaction sum and calculated total --- src/main.rs | 14 +++++++++++--- templates/customer.html | 6 ++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2085f59..88b0b9d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,6 +86,7 @@ struct Summary { total_volume: String, grand_total: String, products: Vec, + oresutjamning: String, } #[derive(Clone)] @@ -150,10 +151,12 @@ impl PreparedCustomer { .collect(); let mut product_totals: HashMap = HashMap::new(); + let mut total_from_transactions: f64 = 0.0; for card in &cards { for tx in &card.transactions { let volume: f64 = tx.volume.parse().unwrap(); let amount: f64 = tx.amount.parse::().unwrap(); + total_from_transactions += amount; let entry = product_totals .entry(tx.quality_name.clone()) .or_insert((0.0, 0.0)); @@ -166,11 +169,13 @@ impl PreparedCustomer { .into_iter() .map(|(name, (volume, amount))| { let avg_price = if volume > 0.0 { amount / volume } else { 0.0 }; - let calculated_amount = volume * avg_price; + let rounded_volume = (volume * 100.0).round() / 100.0; + let rounded_avg_price = (avg_price * 100.0).round() / 100.0; + let calculated_amount = rounded_volume * rounded_avg_price; ProductSummary { name, - volume: fmt(volume), - avg_price: fmt(avg_price), + volume: fmt(rounded_volume), + avg_price: fmt(rounded_avg_price), amount: fmt(calculated_amount), } }) @@ -187,10 +192,13 @@ impl PreparedCustomer { .map(|p| p.amount.parse::().unwrap()) .sum(); + let oresutjamning = total_from_transactions - grand_total; + let summary = Summary { total_volume: fmt(total_volume), grand_total: fmt(grand_total), products, + oresutjamning: fmt(oresutjamning), }; PreparedCustomer { diff --git a/templates/customer.html b/templates/customer.html index 5dea08b..da26257 100644 --- a/templates/customer.html +++ b/templates/customer.html @@ -171,6 +171,12 @@ {{ customer.summary.grand_total }} Kr + + Öresutjämning + + + {{ customer.summary.oresutjamning }} Kr +