tktax_year_comparison/
year_comparison.rs1crate::ix!();
3
4#[derive(Debug)]
10pub struct YearComparison {
11 year_a: TrackedYear,
12 year_b: TrackedYear,
13 total_difference: MonetaryAmount,
15 }
17
18pub trait DifferentialAnalysis {
19 fn compare_years(&self, other: &Account) -> Option<YearComparison>;
20}
21
22impl DifferentialAnalysis for Account {
23 fn compare_years(&self, other: &Account) -> Option<YearComparison> {
24 if self.year().value() == other.year().value() {
25 return None;
27 }
28 let total_self = self.txns().iter().map(|tx| tx.amount()).sum::<MonetaryAmount>();
29 let total_other = other.txns().iter().map(|tx| tx.amount()).sum::<MonetaryAmount>();
30 Some(YearComparison {
31 year_a: *self.year(),
32 year_b: *other.year(),
33 total_difference: total_other - total_self,
34 })
35 }
36}
37
38#[derive(Debug)]
45pub struct MultiYearSummary {
46 yearly_totals: HashMap<TrackedYear, MonetaryAmount>,
48 }
50
51pub fn aggregate_multi_year(accounts: &[Account]) -> MultiYearSummary {
53 let mut result = MultiYearSummary {
54 yearly_totals: HashMap::new(),
55 };
56
57 for acct in accounts {
58 let yr = acct.year();
59 let total_for_year = acct.txns().iter().map(|tx| tx.amount()).sum::<MonetaryAmount>();
60 result.yearly_totals
61 .entry(*yr)
62 .and_modify(|existing| *existing += total_for_year)
63 .or_insert(total_for_year);
64 }
65
66 result
67}
68