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,
|
total_volume: String,
|
||||||
grand_total: String,
|
grand_total: String,
|
||||||
products: Vec<ProductSummary>,
|
products: Vec<ProductSummary>,
|
||||||
|
oresutjamning: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -150,10 +151,12 @@ impl PreparedCustomer {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let mut product_totals: HashMap<String, (f64, f64)> = HashMap::new();
|
let mut product_totals: HashMap<String, (f64, f64)> = HashMap::new();
|
||||||
|
let mut total_from_transactions: f64 = 0.0;
|
||||||
for card in &cards {
|
for card in &cards {
|
||||||
for tx in &card.transactions {
|
for tx in &card.transactions {
|
||||||
let volume: f64 = tx.volume.parse().unwrap();
|
let volume: f64 = tx.volume.parse().unwrap();
|
||||||
let amount: f64 = tx.amount.parse::<f64>().unwrap();
|
let amount: f64 = tx.amount.parse::<f64>().unwrap();
|
||||||
|
total_from_transactions += amount;
|
||||||
let entry = product_totals
|
let entry = product_totals
|
||||||
.entry(tx.quality_name.clone())
|
.entry(tx.quality_name.clone())
|
||||||
.or_insert((0.0, 0.0));
|
.or_insert((0.0, 0.0));
|
||||||
@@ -166,11 +169,13 @@ impl PreparedCustomer {
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(name, (volume, amount))| {
|
.map(|(name, (volume, amount))| {
|
||||||
let avg_price = if volume > 0.0 { amount / volume } else { 0.0 };
|
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 {
|
ProductSummary {
|
||||||
name,
|
name,
|
||||||
volume: fmt(volume),
|
volume: fmt(rounded_volume),
|
||||||
avg_price: fmt(avg_price),
|
avg_price: fmt(rounded_avg_price),
|
||||||
amount: fmt(calculated_amount),
|
amount: fmt(calculated_amount),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -187,10 +192,13 @@ impl PreparedCustomer {
|
|||||||
.map(|p| p.amount.parse::<f64>().unwrap())
|
.map(|p| p.amount.parse::<f64>().unwrap())
|
||||||
.sum();
|
.sum();
|
||||||
|
|
||||||
|
let oresutjamning = total_from_transactions - grand_total;
|
||||||
|
|
||||||
let summary = Summary {
|
let summary = Summary {
|
||||||
total_volume: fmt(total_volume),
|
total_volume: fmt(total_volume),
|
||||||
grand_total: fmt(grand_total),
|
grand_total: fmt(grand_total),
|
||||||
products,
|
products,
|
||||||
|
oresutjamning: fmt(oresutjamning),
|
||||||
};
|
};
|
||||||
|
|
||||||
PreparedCustomer {
|
PreparedCustomer {
|
||||||
|
|||||||
@@ -171,6 +171,12 @@
|
|||||||
<td></td>
|
<td></td>
|
||||||
<td>{{ customer.summary.grand_total }} Kr</td>
|
<td>{{ customer.summary.grand_total }} Kr</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Öresutjämning</td>
|
||||||
|
<td></td>
|
||||||
|
<td></td>
|
||||||
|
<td>{{ customer.summary.oresutjamning }} Kr</td>
|
||||||
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user