tktax_year_comparison/
year_comparison.rs

1// ---------------- [ File: tktax-year-comparison/src/year_comparison.rs ]
2crate::ix!();
3
4//-------------------------------------------
5// Differential (Comparative) Analysis
6//-------------------------------------------
7
8/// Stores differential data comparing two years.
9#[derive(Debug)]
10pub struct YearComparison {
11    year_a: TrackedYear,
12    year_b: TrackedYear,
13    // The difference in total amounts: year_b - year_a
14    total_difference: MonetaryAmount,
15    // If needed, break down by debits, credits, checks, etc.
16}
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            // same year - can't do a meaningful cross-year comparison
26            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//-------------------------------------------
39// Multi-Year Aggregator
40//-------------------------------------------
41
42/// Aggregates data across multiple `Account`s spanning different years.
43/// Could be extended to unify them in one cohesive summary.
44#[derive(Debug)]
45pub struct MultiYearSummary {
46    /// Each year mapped to a total
47    yearly_totals: HashMap<TrackedYear, MonetaryAmount>,
48    // You can add more detail as needed, e.g. total checks, total debits, etc.
49}
50
51/// Summarizes multiple years or multiple `Account` instances.
52pub 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