aboutsummaryrefslogtreecommitdiffstats
path: root/src/ynab/transaction.rs
blob: be31019b8e907ae5cb5aeb175b9a36c4d0648981 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#[derive(Clone, Debug)]
pub struct Transaction {
    pub id: String,
    pub date: String,
    pub amount: i64,
    pub memo: Option<String>,
    pub cleared: String,
    pub approved: bool,
    pub flag_color: Option<String>,
    pub account_id: String,
    pub payee_id: Option<String>,
    pub category_id: Option<String>,
    pub import_id: Option<String>,

    pub account: Option<String>,
    pub payee: Option<String>,
    pub total_amount: i64,
    pub reimbursed: bool,
    pub selected: bool,
}

impl Transaction {
    pub fn from_transaction(
        t: &ynab_api::models::TransactionSummary,
    ) -> Self {
        let reimbursed = if let Some(color) = &t.flag_color {
            color == "green"
        } else {
            false
        };
        Self {
            id: t.id.clone(),
            date: t.date.clone(),
            amount: t.amount,
            memo: t.memo.clone(),
            cleared: t.cleared.clone(),
            approved: t.approved,
            flag_color: t.flag_color.clone(),
            account_id: t.account_id.clone(),
            payee_id: t.payee_id.clone(),
            category_id: t.category_id.clone(),
            import_id: t.import_id.clone(),

            account: None,
            payee: None,
            total_amount: t.amount,
            reimbursed,
            selected: false,
        }
    }

    pub fn from_sub_transaction(
        t: &ynab_api::models::TransactionSummary,
        st: &ynab_api::models::SubTransaction,
    ) -> Self {
        let reimbursed = if let Some(color) = &t.flag_color {
            color == "green"
        } else {
            false
        };
        Self {
            id: t.id.clone(),
            date: t.date.clone(),
            amount: st.amount,
            memo: t.memo.clone(),
            cleared: t.cleared.clone(),
            approved: t.approved,
            flag_color: t.flag_color.clone(),
            account_id: t.account_id.clone(),
            payee_id: t.payee_id.clone(),
            category_id: t.category_id.clone(),
            import_id: t.import_id.clone(),

            account: None,
            payee: None,
            total_amount: t.amount,
            reimbursed,
            selected: false,
        }
    }

    pub fn to_update_transaction(
        &self,
    ) -> ynab_api::models::UpdateTransaction {
        let mut ut = ynab_api::models::UpdateTransaction::new(
            self.account_id.clone(),
            self.date.clone(),
            self.amount,
        );
        ut.id = Some(self.id.clone());
        ut.payee_id = self.payee_id.clone();
        ut.category_id = self.category_id.clone();
        ut.memo = self.memo.clone();
        ut.cleared = Some(self.cleared.clone());
        ut.approved = Some(self.approved);
        ut.flag_color = self.flag_color.clone();
        ut.import_id = self.import_id.clone();

        ut
    }
}