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
|
use parsers::csv::CsvFile;
use chrono::Datelike;
pub struct Account {
pub name : String,
pub iban : String,
pub transactions : Vec<Transaction>,
pub institute : String,
pub groupFile : String,
pub category : String,
pub riskCategory : String
}
impl Account {
pub fn new(name : String, iban : String, transactions : Vec<String>, institute : String, groupFile : String, category : String, riskCategory : String) -> Account {
let mut trans = Vec::new();
if institute == "SpecialParser" {
}
// default parser
else {
// TODO als function/lambda übergeben die konvertierung..
for trans_file in transactions {
let file = CsvFile::from_file(&trans_file, ";", true);
match file {
Ok(f) => { let mut tran = Transaction::from_sparkasse_csv_file(f);
trans.append(&mut tran);
},
Err(e) => panic!("account: new: error reading csv transaction file: {}", e)
}
}
}
Account { name : name, iban : iban, transactions : trans, institute : institute, groupFile : groupFile, category : category, riskCategory : riskCategory }
}
pub fn get_balance(&self, date : chrono::NaiveDate) -> f32 {
self.transactions.iter().filter(|transaction| Account::date_is_before(transaction.date, date)).fold(0.0, |acc, x| acc + x.amount).abs()
}
fn date_is_before(left: chrono::NaiveDate, right : chrono::NaiveDate) -> bool {
if left.year() < right.year() {
return true;
}
else if left.year() == right.year() {
if left.month() > right.month() {
return false;
}
return true;
}
return false;
}
}
#[derive(Serialize)]
#[derive(Clone)]
pub struct Transaction {
pub sender_name : String,
pub amount : f32,
pub reference : String,
pub date : chrono::NaiveDate
}
impl Transaction {
pub fn from_sparkasse_csv_file(file : CsvFile) -> Vec<Transaction> {
let mut ret = Vec::new();
for line in file.iter() {
let mut sender_name_f = String::from("");
let mut sender_iban_f = String::from("");
let mut amount_f : f32 = 0.0;
let mut reference_f = String::from("");
let mut date_f = chrono::NaiveDate::parse_from_str("01.01.2019", "%d.%m.%Y").unwrap();
match line.get(&String::from("Kontonummer")) {
Some(value) => sender_iban_f = value.to_string(),
None => println!("missing sender")
}
match line.get(&String::from("Beguenstigter/Zahlungspflichtiger")) {
Some(value) => sender_name_f = value.to_string(),
None => println!("missing sender")
}
match line.get(&String::from("Verwendungszweck")) {
Some(value) => reference_f = value.to_string(),
None => println!("missing refernce")
}
match line.get(&String::from("Betrag")) {
Some(value) => amount_f = value.parse().unwrap(),
None => println!("missing amount")
}
match line.get(&String::from("Valutadatum")) {
Some(value) => {
date_f = chrono::NaiveDate::parse_from_str(value, "%d.%m.%y").unwrap();} ,
None => println!("missing date")
}
ret.push(Transaction {
sender_name : sender_name_f,
amount : amount_f,
reference : reference_f,
date : date_f });
}
ret
}
}
|