summaryrefslogtreecommitdiff
path: root/src/banking/account.rs
blob: 1783e6357e1554b50ca65202b0490d2197a01f50 (plain)
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
use parsers::csv::CsvFile;

pub struct Account {
	pub name : String,
	pub iban : String,
	pub transactions : Vec<Transaction>,
	pub institute : String,
	pub groupFile : String
}

impl Account {
	pub fn new(name : String, iban : String, transactions : Vec<String>, institute : String, groupFile : String) -> Account {
		let mut trans = Vec::new();
		if institute == "Sparkasse" {
			// 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 }
	}
}

#[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
	}
}