use crate::parsers::csv::CsvFile; use crate::banking::account::Account; use crate::banking::asset::Asset; use crate::parsers::ini::IniFile; use std::collections::HashMap; use rocket_contrib::templates::Template; use rocket::response::NamedFile; use std::path::{PathBuf, Path}; use rocket::request::Form; use rocket::http::RawStr; use regex::Regex; use chrono::{NaiveDate, Utc}; use chrono::Datelike; #[derive(Serialize)] struct ChartContext { account_name : String, groups : HashMap, total_sum : f32, total_chart : f32, date_start : String, date_end : String } #[get("/chart?&")] pub fn chart_handler(start : Option<&RawStr>, end : Option<&RawStr>) -> rocket_contrib::templates::Template { let date_start = match start { Some(s) => { let mut tmp = s.to_string(); tmp.push_str("-01"); chrono::NaiveDate::parse_from_str(&tmp, "%Y-%m-%d").unwrap() }, None => Utc::today().naive_utc() }; let date_end = match end { Some(s) => { let mut tmp = s.to_string(); tmp.push_str("-01"); chrono::NaiveDate::parse_from_str(&tmp, "%Y-%m-%d").unwrap() }, None => Utc::today().naive_utc() }; let date_range = crate::web_frontend::util::DateRange::new(date_start, date_end); // read group config let chart_file = "data/giro"; let chart_config = IniFile::from_file(chart_file); let ini_file; match chart_config { Ok(file) => ini_file = file, Err(e) => panic!("could not read group file {:?}", e) } let mut groups = HashMap::new(); let asset_ini = "data/asset.ini"; let asset = crate::banking::asset::Asset::from_ini_file(asset_ini); let transactions = asset.get_account_by_name("Girokonto"); let acc; match transactions { Some(trans) => acc = trans, None => panic!("could not read file") } let t = acc.transactions; // filter transaction to match only the specified timeframe println!("unfiltered number: {}", t.len()); let mut t_filtered = Vec::new(); for date in date_range { let mut tmp : Vec<_> = t.iter().filter(|x| x.date.month() == date.month()).collect(); t_filtered.append(& mut tmp); } println!("filtered number: {}", t_filtered.len()); let total_sum = t_filtered.iter().filter(|t| t.amount < 0.0 ) .fold(0.0, |acc, x| acc + x.amount).abs(); println!("total sum: {}", total_sum); let mut total_chart = 0.0; for (section_name, entries) in ini_file.sections { let mut complete = 0.0; for (key, values) in entries { for val in values { let mut t_filtered_cloned = t_filtered.clone(); if val.is_empty() || val.is_empty() { continue } let re = Regex::new(&val).unwrap(); let tmp = t_filtered_cloned.into_iter().filter(|transaction| re.is_match(&transaction.sender_name) ) .fold(0.0, |acc, x| acc + x.amount); complete = complete + tmp.abs(); } } groups.insert(section_name, complete); total_chart = total_chart + complete; // ALSO INSERT OTHER, AKA THE REST } let context = ChartContext { account_name : String::from("Girokonto"), groups : groups, total_sum : total_sum, total_chart : total_chart, date_start : date_start.to_string()[0..7].to_string(), date_end : date_end.to_string()[0..7].to_string() }; Template::render("chart", context) }