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 +