tktax_check/
checks_and_treasury.rs

1// ---------------- [ File: tktax-check/src/checks_and_treasury.rs ]
2crate::ix!();
3
4#[derive(Getters,Debug,Clone)]
5#[getset(get="pub")]
6pub struct CheckOrTreasuryTransaction<'a,TxCat:TransactionCategory + 'static> {
7    category: TxCat,
8    tx:       &'a Transaction,
9}
10
11impl<'a,TxCat:TransactionCategory + 'static> fmt::Display for CheckOrTreasuryTransaction<'a,TxCat> {
12
13    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14        write!(f, "{} - {}", self.category, self.tx)
15    }
16}
17
18impl<'a,TxCat:TransactionCategory + 'static> LineItem<TxCat> for CheckOrTreasuryTransaction<'a,TxCat> {
19
20    fn tag() -> &'static str {
21        "check-or-treasury"
22    }
23
24    fn quantity(&self) -> Decimal {
25        Decimal::ONE
26    }
27
28    fn price(&self) -> MonetaryAmount {
29        self.tx_amount()
30    }
31
32    fn category(&self) -> TxCat {
33        self.category
34    }
35
36    fn tx_amount(&self) -> MonetaryAmount {
37        self.tx.amount()
38    }
39
40    fn tx(&self) -> Option<&Transaction> {
41        Some(&self.tx)
42    }
43}
44
45pub trait GetTreasuryTransactionsAndChecks {
46
47    fn treasury_transactions_and_checks<TxCat:TransactionCategory + 'static>(&self, category_map: &CategoryMap<TxCat>) 
48        -> Vec<CheckOrTreasuryTransaction<TxCat>>;
49}
50
51impl GetTreasuryTransactionsAndChecks for Account {
52
53    fn treasury_transactions_and_checks<TxCat:TransactionCategory + 'static>(&self, category_map: &CategoryMap<TxCat>) 
54        -> Vec<CheckOrTreasuryTransaction<TxCat>> 
55    {
56        let mut line_items = vec![];
57
58        for tx in self.txns().iter() {
59
60            let predicted  = predict_category(&tx.description(), category_map);
61
62            if predicted.len() > 0 {
63
64                let prediction = &predicted[0];
65
66                let categories = TxCat::treasury_and_checks_categories();
67
68                if categories.contains(&prediction.category()) {
69
70                    line_items.push(CheckOrTreasuryTransaction { category: *prediction.category(), tx });
71                }
72            }
73
74        }
75
76        line_items.sort_by(|a,b| a.category.cmp(&b.category));
77
78        line_items
79    }
80}