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
|
pub struct Budget {
budget: ynab_api::models::BudgetDetail,
}
impl Budget {
pub fn new(budget: ynab_api::models::BudgetDetail) -> Self {
Self { budget }
}
pub fn name(&self) -> String {
self.budget.name.clone()
}
pub fn id(&self) -> String {
self.budget.id.clone()
}
pub fn reimbursables(&self) -> Vec<super::transaction::Transaction> {
let reimbursables_id =
if let Some(categories) = &self.budget.categories {
categories
.iter()
.find(|c| c.name == "Reimbursables")
.map(|c| c.id.clone())
.unwrap()
} else {
panic!("no categories found")
};
let mut reimbursables = vec![];
if let Some(payees) = &self.budget.payees {
let mut payee_map = std::collections::HashMap::new();
for p in payees {
payee_map.insert(p.id.clone(), p.name.clone());
}
let payee_map = payee_map;
let mut transaction_map = std::collections::HashMap::new();
if let Some(transactions) = &self.budget.transactions {
for t in transactions {
transaction_map.insert(t.id.clone(), t);
if let Some(category_id) = &t.category_id {
if category_id != &reimbursables_id {
continue;
}
} else {
continue;
}
let payee = t
.payee_id
.iter()
.flat_map(|payee_id| payee_map.get(payee_id).cloned())
.next()
.unwrap_or_else(|| "(none)".to_string());
let reimbursed = if let Some(color) = &t.flag_color {
color == "green"
} else {
false
};
reimbursables.push(super::transaction::Transaction {
date: t.date.clone(),
payee,
amount: t.amount,
total_amount: t.amount,
reimbursed,
})
}
}
let transaction_map = transaction_map;
if let Some(subtransactions) = &self.budget.subtransactions {
for st in subtransactions {
if let Some(category_id) = &st.category_id {
if category_id != &reimbursables_id {
continue;
}
} else {
continue;
}
let t = transaction_map[&st.transaction_id];
let payee = st
.payee_id
.iter()
.flat_map(|payee_id| payee_map.get(payee_id).cloned())
.next()
.unwrap_or_else(|| {
t.payee_id
.iter()
.flat_map(|payee_id| {
payee_map.get(payee_id).cloned()
})
.next()
.unwrap_or_else(|| "(none)".to_string())
});
let reimbursed = if let Some(color) = &t.flag_color {
color == "green"
} else {
false
};
reimbursables.push(super::transaction::Transaction {
date: t.date.clone(),
payee,
amount: st.amount,
total_amount: t.amount,
reimbursed,
})
}
}
} else {
panic!("no payees?");
}
reimbursables.sort_by_cached_key(|t| t.date.clone());
reimbursables
}
}
|