aboutsummaryrefslogtreecommitdiffstats
path: root/src/ynab/transaction.rs
blob: d9ce6bdb937e6b269e39c604882a2da471758ab6 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#[derive(Debug)]
pub struct Transaction {
    pub id: String,
    pub date: String,
    pub amount: i64,
    pub memo: Option<String>,
    pub cleared: ynab_api::models::transaction_summary::Cleared,
    pub approved: bool,
    pub flag_color: Option<ynab_api::models::transaction_summary::FlagColor>,
    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 == &ynab_api::models::transaction_summary::FlagColor::Green
        } else {
            false
        };
        Self {
            id: t.id.clone(),
            date: t.date.clone(),
            amount: t.amount,
            memo: t.memo.clone(),
            cleared: clone_cleared(&t.cleared),
            approved: t.approved,
            flag_color: t.flag_color.as_ref().map(clone_flag_color),
            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 == &ynab_api::models::transaction_summary::FlagColor::Green
        } else {
            false
        };
        Self {
            id: t.id.clone(),
            date: t.date.clone(),
            amount: st.amount,
            memo: t.memo.clone(),
            cleared: clone_cleared(&t.cleared),
            approved: t.approved,
            flag_color: t.flag_color.as_ref().map(clone_flag_color),
            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.id.clone(),
            self.account_id.clone(),
            self.date.clone(),
            self.amount,
        );
        ut.payee_id = self.payee_id.clone();
        ut.category_id = self.category_id.clone();
        ut.memo = self.memo.clone();
        ut.cleared = Some(cleared_to_cleared(&self.cleared));
        ut.approved = Some(self.approved);
        ut.flag_color =
            self.flag_color.as_ref().map(flag_color_to_flag_color);
        ut.import_id = self.import_id.clone();

        ut
    }
}

impl Clone for Transaction {
    fn clone(&self) -> Self {
        Self {
            id: self.id.clone(),
            date: self.date.clone(),
            amount: self.amount,
            memo: self.memo.clone(),
            cleared: clone_cleared(&self.cleared),
            approved: self.approved,
            flag_color: self.flag_color.as_ref().map(clone_flag_color),
            account_id: self.account_id.clone(),
            payee_id: self.payee_id.clone(),
            category_id: self.category_id.clone(),
            import_id: self.import_id.clone(),
            account: self.account.clone(),
            payee: self.payee.clone(),
            total_amount: self.total_amount,
            reimbursed: self.reimbursed,
            selected: self.selected,
        }
    }
}

fn cleared_to_cleared(
    cleared: &ynab_api::models::transaction_summary::Cleared,
) -> ynab_api::models::update_transaction::Cleared {
    use ynab_api::models::transaction_summary::Cleared as TSCleared;
    use ynab_api::models::update_transaction::Cleared as UTCleared;

    match cleared {
        TSCleared::Cleared => UTCleared::Cleared,
        TSCleared::Uncleared => UTCleared::Uncleared,
        TSCleared::Reconciled => UTCleared::Reconciled,
    }
}

fn flag_color_to_flag_color(
    flag_color: &ynab_api::models::transaction_summary::FlagColor,
) -> ynab_api::models::update_transaction::FlagColor {
    use ynab_api::models::transaction_summary::FlagColor as TSFlagColor;
    use ynab_api::models::update_transaction::FlagColor as UTFlagColor;

    match flag_color {
        TSFlagColor::Red => UTFlagColor::Red,
        TSFlagColor::Orange => UTFlagColor::Orange,
        TSFlagColor::Yellow => UTFlagColor::Yellow,
        TSFlagColor::Green => UTFlagColor::Green,
        TSFlagColor::Blue => UTFlagColor::Blue,
        TSFlagColor::Purple => UTFlagColor::Purple,
    }
}

fn clone_cleared(
    cleared: &ynab_api::models::transaction_summary::Cleared,
) -> ynab_api::models::transaction_summary::Cleared {
    use ynab_api::models::transaction_summary::Cleared as TSCleared;

    match cleared {
        TSCleared::Cleared => TSCleared::Cleared,
        TSCleared::Uncleared => TSCleared::Uncleared,
        TSCleared::Reconciled => TSCleared::Reconciled,
    }
}

fn clone_flag_color(
    flag_color: &ynab_api::models::transaction_summary::FlagColor,
) -> ynab_api::models::transaction_summary::FlagColor {
    use ynab_api::models::transaction_summary::FlagColor as TSFlagColor;

    match flag_color {
        TSFlagColor::Red => TSFlagColor::Red,
        TSFlagColor::Orange => TSFlagColor::Orange,
        TSFlagColor::Yellow => TSFlagColor::Yellow,
        TSFlagColor::Green => TSFlagColor::Green,
        TSFlagColor::Blue => TSFlagColor::Blue,
        TSFlagColor::Purple => TSFlagColor::Purple,
    }
}