Fix summary amount calculation and add Öresutjämning row
- Amount = round(volume, 2) * round(avg_price, 2) - Add Öresutjämning showing difference between transaction sum and calculated total
This commit is contained in:
14
src/main.rs
14
src/main.rs
@@ -86,6 +86,7 @@ struct Summary {
|
||||
total_volume: String,
|
||||
grand_total: String,
|
||||
products: Vec<ProductSummary>,
|
||||
oresutjamning: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -150,10 +151,12 @@ impl PreparedCustomer {
|
||||
.collect();
|
||||
|
||||
let mut product_totals: HashMap<String, (f64, f64)> = 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::<f64>().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::<f64>().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 {
|
||||
|
||||
Reference in New Issue
Block a user