Skip to content

Commit

Permalink
- Implemented warnings from presentation and remove all clears all gui (
Browse files Browse the repository at this point in the history
#86)

windows
  • Loading branch information
jczaja authored Nov 10, 2023
1 parent 2cde065 commit 54474f7
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "etradeTaxReturnHelper"
version = "0.3.1"
version = "0.3.2"
edition = "2021"
description = "Parses etrade financial documents for transaction details (income, tax paid, cost basis) and compute total income and total tax paid according to chosen tax residency (currency)"
license = "BSD-3-Clause"
Expand Down
6 changes: 3 additions & 3 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl etradeTaxReturnHelper::Residency for DE {
tax_div: f32,
gross_sold: f32,
cost_sold: f32,
) -> Vec<String> {
) -> (Vec<String>, Option<String>) {
let mut presentation: Vec<String> = vec![];
presentation.push(format!("===> (DIVIDENDS) INCOME: {:.2} EUR", gross_div));
presentation.push(format!("===> (DIVIDENDS) TAX PAID: {:.2} EUR", tax_div));
Expand All @@ -64,7 +64,7 @@ impl etradeTaxReturnHelper::Residency for DE {
"===> (SOLD STOCK) TAX DEDUCTIBLE COST: {:.2} EUR",
cost_sold
));
presentation
(presentation, None)
}
}

Expand All @@ -87,7 +87,7 @@ mod tests {
"===> (SOLD STOCK) TAX DEDUCTIBLE COST: 10.00 EUR".to_string(),
];

let results = rd.present_result(gross_div, tax_div, gross_sold, cost_sold);
let (results, _) = rd.present_result(gross_div, tax_div, gross_sold, cost_sold);

results
.iter()
Expand Down
36 changes: 33 additions & 3 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,31 @@ pub mod gui {
docs.iter().for_each(|x| browser.add(x));
}

fn create_clear_documents(browser: Rc<RefCell<MultiBrowser>>, clear_button: &mut Button) {
fn create_clear_documents(
browser: Rc<RefCell<MultiBrowser>>,
tdisplay: Rc<RefCell<TextDisplay>>,
sdisplay: Rc<RefCell<TextDisplay>>,
ndisplay: Rc<RefCell<TextDisplay>>,
clear_button: &mut Button,
) {
clear_button.set_callback(move |_| {
let mut buffer = sdisplay
.borrow()
.buffer()
.expect_and_log("Error: No buffer assigned to Summary TextDisplay");
let mut nbuffer = ndisplay
.borrow()
.buffer()
.expect_and_log("Error: No buffer assigned to Notes TextDisplay");
let mut tbuffer = tdisplay
.borrow()
.buffer()
.expect_and_log("Error: No buffer assigned to Transactions TextDisplay");
let mut filelist = browser.borrow_mut();
filelist.clear();
buffer.set_text("");
tbuffer.set_text("");
nbuffer.set_text("");
});
}

Expand Down Expand Up @@ -107,8 +128,11 @@ pub mod gui {
panic!("Error: unable to perform taxation");
}
};
let presentation = rd.present_result(gross_div, tax_div, gross_sold, cost_sold);
let (presentation,warning) = rd.present_result(gross_div, tax_div, gross_sold, cost_sold);
buffer.set_text(&presentation.join("\n"));
if let Some(warn_msg) = warning {
nbuffer.set_text(&warn_msg);
}
let mut transactions_strings: Vec<String> = vec![];
div_transactions
.iter()
Expand Down Expand Up @@ -260,7 +284,13 @@ pub mod gui {
uberpack.end();

create_choose_documents_dialog(browser.clone(), &mut load_button);
create_clear_documents(browser.clone(), &mut clear_button);
create_clear_documents(
browser.clone(),
tdisplay.clone(),
sdisplay.clone(),
ndisplay.clone(),
&mut clear_button,
);
create_execute_documents(
browser.clone(),
tdisplay.clone(),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub trait Residency {
tax_div: f32,
gross_sold: f32,
cost_sold: f32,
) -> Vec<String>;
) -> (Vec<String>, Option<String>);
fn get_exchange_rates(
&self,
dates: &mut std::collections::HashMap<String, Option<(String, f32)>>,
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod gui;
use etradeTaxReturnHelper::run_taxation;
use logging::ResultExt;

// TODO: GUI : Error messages section (Redirecting to GUI the errors)
// TODO: GUI : choosing residency
// TODO: Drag&Drop to work on MultiBrowser field

Expand Down Expand Up @@ -78,8 +77,12 @@ fn main() {
Err(msg) => panic!("\nError: Unable to compute taxes. \n\nDetails: {msg}"),
};

let presentation = rd.present_result(gross_div, tax_div, gross_sold, cost_sold);
let (presentation, warning) = rd.present_result(gross_div, tax_div, gross_sold, cost_sold);
presentation.iter().for_each(|x| println!("{x}"));

if let Some(warn_msg) = warning {
println!("\n\nWARNING: {warn_msg}");
}
}

#[cfg(test)]
Expand Down
47 changes: 43 additions & 4 deletions src/pl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,16 @@ impl etradeTaxReturnHelper::Residency for PL {
tax_div: f32,
gross_sold: f32,
cost_sold: f32,
) -> Vec<String> {
) -> (Vec<String>, Option<String>) {
let mut presentation: Vec<String> = vec![];
let tax_pl = 0.19 * gross_div;
presentation.push(format!(
"(DYWIDENDY) PRZYCHOD Z ZAGRANICY: {:.2} PLN",
gross_div
));
presentation.push(format!(
"===> (DYWIDENDY) ZRYCZALTOWANY PODATEK: {:.2} PLN",
(0.19 * gross_div)
tax_pl
));
presentation.push(format!(
"===> (DYWIDENDY) PODATEK ZAPLACONY ZAGRANICA: {:.2} PLN",
Expand All @@ -127,7 +128,11 @@ impl etradeTaxReturnHelper::Residency for PL {
"===> (SPRZEDAZ AKCJI) KOSZT UZYSKANIA PRZYCHODU: {:.2} PLN",
cost_sold
));
presentation
if tax_div > tax_pl {
(presentation,Some(format!("Warning: Tax paid in US({tax_div} PLN) is higher than the tax that you are to pay in Poland({tax_pl} PLN). This usually means that there was a problem with declaration of your residency to avoid double taxation")))
} else {
(presentation, None)
}
}
}

Expand All @@ -151,7 +156,7 @@ mod tests {
"===> (SPRZEDAZ AKCJI) KOSZT UZYSKANIA PRZYCHODU: 10.00 PLN".to_string(),
];

let results = rd.present_result(gross_div, tax_div, gross_sold, cost_sold);
let (results, _) = rd.present_result(gross_div, tax_div, gross_sold, cost_sold);

results
.iter()
Expand All @@ -160,4 +165,38 @@ mod tests {

Ok(())
}

#[test]
fn test_present_result_double_taxation_warning_pl() -> Result<(), String> {
let rd: Box<dyn etradeTaxReturnHelper::Residency> = Box::new(PL {});

let gross_div = 100.0f32;
let tax_div = 30.0f32;
let gross_sold = 1000.0f32;
let cost_sold = 10.0f32;

let ref_results: Vec<String> = vec![
"(DYWIDENDY) PRZYCHOD Z ZAGRANICY: 100.00 PLN".to_string(),
"===> (DYWIDENDY) ZRYCZALTOWANY PODATEK: 19.00 PLN".to_string(),
"===> (DYWIDENDY) PODATEK ZAPLACONY ZAGRANICA: 30.00 PLN".to_string(),
"===> (SPRZEDAZ AKCJI) PRZYCHOD Z ZAGRANICY: 1000.00 PLN".to_string(),
"===> (SPRZEDAZ AKCJI) KOSZT UZYSKANIA PRZYCHODU: 10.00 PLN".to_string(),
];

let (results, warning) = rd.present_result(gross_div, tax_div, gross_sold, cost_sold);

results
.iter()
.zip(&ref_results)
.for_each(|(a, b)| assert_eq!(a, b));

let ref_msg = "Warning: Tax paid in US(30 PLN) is higher than the tax that you are to pay in Poland(19 PLN). This usually means that there was a problem with declaration of your residency to avoid double taxation".to_string();

match (warning) {
Some(msg) => assert_eq!(msg, ref_msg),
None => return Err("Error: expected information on to high tax".to_string()),
}

Ok(())
}
}
6 changes: 3 additions & 3 deletions src/us.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl etradeTaxReturnHelper::Residency for US {
tax_div: f32,
gross_sold: f32,
cost_sold: f32,
) -> Vec<String> {
) -> (Vec<String>, Option<String>) {
let mut presentation: Vec<String> = vec![];
presentation.push(format!("===> (DIVIDENDS) INCOME: ${:.2}", gross_div));
presentation.push(format!("===> (DIVIDENDS) TAX PAID: ${:.2}", tax_div));
Expand All @@ -25,7 +25,7 @@ impl etradeTaxReturnHelper::Residency for US {
"===> (SOLD STOCK) TAX DEDUCTIBLE COST: ${:.2}",
cost_sold
));
presentation
(presentation, None)
}
}

Expand All @@ -48,7 +48,7 @@ mod tests {
"===> (SOLD STOCK) TAX DEDUCTIBLE COST: $10.00".to_string(),
];

let results = rd.present_result(gross_div, tax_div, gross_sold, cost_sold);
let (results, _) = rd.present_result(gross_div, tax_div, gross_sold, cost_sold);

results
.iter()
Expand Down

0 comments on commit 54474f7

Please sign in to comment.