etradeTaxReturnHelper 0.7.5

Parses etrade and revolut financial documents for transaction details (income, tax paid, cost basis) and compute total income and total tax paid according to chosen tax residency (currency)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// SPDX-FileCopyrightText: 2022-2025 RustInFinance
// SPDX-License-Identifier: BSD-3-Clause

use clap::{Arg, Command};
use std::env;

mod de;
mod logging;
mod nbp;
mod pl;
mod us;

mod gui;

use etradeTaxReturnHelper::run_taxation;
use etradeTaxReturnHelper::TaxCalculationResult;
use logging::ResultExt;

// TODO: check if Tax from Terna company taken by IT goverment was taken into account
// TODO: Extend structure of TaxCalculationResult with country
// TODO: Make parsing of PDF start from first page not second so then reproduction of problem
// require one page not two
// TODO: remove support for account statement of investment account of revolut
// TODO: When there is no proxy (on intel account) there are problems (UT do not work
// getting_Exchange_rate)
// TODO: Make a parsing of incomplete date
// TODO:  async to get currency
// TODO: make UT using rounded vlaues of f32
// TODO: parse_gain_and_losses  expect ->  ?
// TODO: GUI : choosing residency
// TODO: Drag&Drop to work on MultiBrowser field
// TODO: taxation of EUR instruments in US

fn create_cmd_line_pattern(myapp: Command) -> Command {
    myapp
        .arg(
            Arg::new("residency")
                .long("residency")
                .help("Country of residence e.g. pl , us ...")
                .value_name("FILE")
                .default_value("pl"),
        )
        .arg(
            Arg::new("financial documents")
                .help("Account statement PDFs  and Gain & Losses xlsx documents\n\nAccount statements can be downloaded from:\n\thttps://edoc.etrade.com/e/t/onlinedocs/docsearch?doc_type=stmt\n\nGain&Losses documents can be downloaded from:\n\thttps://us.etrade.com/etx/sp/stockplan#/myAccount/gainsLosses\n")
                .num_args(1..)
                .required(true),
        )
        .arg(
            Arg::new("per-company")
                .long("per-company")
                .help("Enable per-company mode")
                .action(clap::ArgAction::SetTrue)
        )
        .arg(
            Arg::new("multiyear")
                .long("multiyear")
                .help("Allow processing documents across more than year")
                .action(clap::ArgAction::SetTrue)
        )
}

fn configure_dataframes_format() {
    // Make sure to show all raws
    if std::env::var("POLARS_FMT_MAX_ROWS").is_err() {
        std::env::set_var("POLARS_FMT_MAX_ROWS", "-1")
    }
}

fn main() {
    const VERSION: &str = env!("CARGO_PKG_VERSION");
    logging::init_logging_infrastructure();
    configure_dataframes_format();

    log::info!("Started etradeTaxHelper");
    // If there is no arguments then start GUI
    let args: Vec<String> = env::args().collect();
    if args.len() <= 1 {
        #[cfg(feature = "gui")]
        {
            gui::run_gui();
            return;
        }
    }

    let myapp = Command::new("etradeTaxHelper")
        .version(VERSION)
        .arg_required_else_help(true);
    let matches = create_cmd_line_pattern(myapp).get_matches_from(wild::args());

    let residency = matches
        .get_one::<String>("residency")
        .expect_and_log("error getting residency value");
    let rd: Box<dyn etradeTaxReturnHelper::Residency> = match residency.as_str() {
        "de" => Box::new(de::DE {}),
        "pl" => Box::new(pl::PL {}),
        "us" => Box::new(us::US {}),
        _ => panic!(
            "{}",
            &format!("Error: unimplemented residency: {}", residency)
        ),
    };

    let pdfnames = matches
        .get_many::<String>("financial documents")
        .expect_and_log("error getting brokarage statements pdfs names.\n\nBrokerege statements can be downloaded from:\n\nhttps://edoc.etrade.com/e/t/onlinedocs/docsearch?doc_type=stmt\n\n");

    let pdfnames: Vec<String> = pdfnames.map(|x| x.to_string()).collect();

    let TaxCalculationResult {
        gross_income: gross_div,
        tax: tax_div,
        gross_sold,
        cost_sold,
        ..
    } = match run_taxation(
        &rd,
        pdfnames,
        matches.get_flag("per-company"),
        matches.get_flag("multiyear"),
    ) {
        Ok(res) => res,
        Err(msg) => panic!("\nError: Unable to compute taxes. \n\nDetails: {msg}"),
    };

    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)]
mod tests {
    use super::*;
    use clap::Command;

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

        let mut dates: std::collections::HashMap<
            etradeTaxReturnHelper::Exchange,
            Option<(String, f32)>,
        > = std::collections::HashMap::new();

        dates.insert(
            etradeTaxReturnHelper::Exchange::USD("02/21/23".to_owned()),
            None,
        );

        rd.get_exchange_rates(&mut dates)?;

        let (exchange_rate_date, exchange_rate) = dates
            [&etradeTaxReturnHelper::Exchange::USD("02/21/23".to_owned())]
            .clone()
            .unwrap();

        assert_eq!(
            (exchange_rate_date, exchange_rate),
            ("2023-02-20".to_owned(), 0.9368559)
        );
        Ok(())
    }

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

        let mut dates: std::collections::HashMap<
            etradeTaxReturnHelper::Exchange,
            Option<(String, f32)>,
        > = std::collections::HashMap::new();

        dates.insert(
            etradeTaxReturnHelper::Exchange::USD("03/01/21".to_owned()),
            None,
        );

        rd.get_exchange_rates(&mut dates)?;

        let (exchange_rate_date, exchange_rate) = dates
            [&etradeTaxReturnHelper::Exchange::USD("03/01/21".to_owned())]
            .clone()
            .unwrap();

        assert_eq!(
            (exchange_rate_date, exchange_rate),
            ("2021-02-26".to_owned(), 3.7247)
        );
        Ok(())
    }

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

        let mut dates: std::collections::HashMap<
            etradeTaxReturnHelper::Exchange,
            Option<(String, f32)>,
        > = std::collections::HashMap::new();

        dates.insert(
            etradeTaxReturnHelper::Exchange::USD("03/01/21".to_owned()),
            None,
        );

        rd.get_exchange_rates(&mut dates)?;

        let (exchange_rate_date, exchange_rate) = dates
            [&etradeTaxReturnHelper::Exchange::USD("03/01/21".to_owned())]
            .clone()
            .unwrap();

        assert_eq!((exchange_rate_date, exchange_rate), ("N/A".to_owned(), 1.0));
        Ok(())
    }

    #[test]
    fn test_cmdline_de() -> Result<(), clap::Error> {
        // Init Transactions
        let myapp = Command::new("E-trade tax helper");
        let matches = create_cmd_line_pattern(myapp).get_matches_from(vec![
            "mytest",
            "--residency=de",
            "data/example.pdf",
        ]);
        let residency = matches
            .get_one::<String>("residency")
            .ok_or(clap::error::Error::new(
                clap::error::ErrorKind::InvalidValue,
            ))?;
        match residency.as_str() {
            "de" => return Ok(()),
            _ => clap::error::Error::<clap::error::DefaultFormatter>::new(
                clap::error::ErrorKind::InvalidValue,
            ),
        };
        Ok(())
    }
    #[test]
    fn test_cmdline_per_company() -> Result<(), clap::Error> {
        // Init Transactions
        let myapp = Command::new("E-trade tax helper");
        let matches =
            create_cmd_line_pattern(myapp).get_matches_from(vec!["mytest", "data/example.pdf"]);
        let per_company = matches.get_flag("per-company");
        match per_company {
            false => (),
            true => {
                return Err(clap::error::Error::<clap::error::DefaultFormatter>::new(
                    clap::error::ErrorKind::InvalidValue,
                ))
            }
        };
        let myapp = Command::new("E-trade tax helper");
        let matches = create_cmd_line_pattern(myapp).get_matches_from(vec![
            "mytest",
            "--per-company",
            "data/example.pdf",
        ]);
        let per_company = matches.get_flag("per-company");
        match per_company {
            true => (),
            false => {
                return Err(clap::error::Error::<clap::error::DefaultFormatter>::new(
                    clap::error::ErrorKind::InvalidValue,
                ))
            }
        };
        Ok(())
    }

    #[test]
    fn test_cmdline_multiyear() -> Result<(), clap::Error> {
        // Init Transactions
        let myapp = Command::new("E-trade tax helper");
        let matches =
            create_cmd_line_pattern(myapp).get_matches_from(vec!["mytest", "data/example.pdf"]);
        let multiyear = matches.get_flag("multiyear");
        match multiyear {
            false => (),
            true => {
                return Err(clap::error::Error::<clap::error::DefaultFormatter>::new(
                    clap::error::ErrorKind::InvalidValue,
                ))
            }
        };
        let myapp = Command::new("E-trade tax helper");
        let matches = create_cmd_line_pattern(myapp).get_matches_from(vec![
            "mytest",
            "--multiyear",
            "data/example.pdf",
        ]);
        let multiyear = matches.get_flag("multiyear");
        match multiyear {
            true => (),
            false => {
                return Err(clap::error::Error::<clap::error::DefaultFormatter>::new(
                    clap::error::ErrorKind::InvalidValue,
                ))
            }
        };
        Ok(())
    }

    #[test]
    fn test_cmdline_pl() -> Result<(), clap::Error> {
        // Init Transactions
        let myapp = Command::new("E-trade tax helper");
        let matches = create_cmd_line_pattern(myapp).get_matches_from(vec![
            "mytest",
            "--residency=pl",
            "data/example.pdf",
        ]);
        let residency = matches
            .get_one::<String>("residency")
            .ok_or(clap::error::Error::new(
                clap::error::ErrorKind::InvalidValue,
            ))?;
        match residency.as_str() {
            "pl" => return Ok(()),
            _ => clap::error::Error::<clap::error::DefaultFormatter>::new(
                clap::error::ErrorKind::InvalidValue,
            ),
        };
        Ok(())
    }
    #[test]
    fn test_cmdline_default() -> Result<(), clap::Error> {
        // Init Transactions
        let myapp = Command::new("E-trade tax helper");
        create_cmd_line_pattern(myapp).get_matches_from(vec!["mytest", "data/example.pdf"]);
        Ok(())
    }

    #[test]
    fn test_cmdline_us() -> Result<(), clap::Error> {
        // Init Transactions
        let myapp = Command::new("E-trade tax helper");
        let matches = create_cmd_line_pattern(myapp).get_matches_from(vec![
            "mytest",
            "--residency=us",
            "data/example.pdf",
        ]);
        let residency = matches
            .get_one::<String>("residency")
            .ok_or(clap::error::Error::new(
                clap::error::ErrorKind::InvalidValue,
            ))?;
        match residency.as_str() {
            "us" => return Ok(()),
            _ => clap::error::Error::<clap::error::DefaultFormatter>::new(
                clap::error::ErrorKind::InvalidValue,
            ),
        };
        Ok(())
    }

    #[test]
    fn test_unrecognized_file_taxation() -> Result<(), clap::Error> {
        // Get all brokerage with dividends only

        let myapp = Command::new("etradeTaxHelper").arg_required_else_help(true);

        let rd: Box<dyn etradeTaxReturnHelper::Residency> = Box::new(pl::PL {});
        // Check printed values or returned values?
        let matches = create_cmd_line_pattern(myapp)
            .get_matches_from(vec!["mytest", "unrecognized_file.txt"]);

        let pdfnames = matches
            .get_many::<String>("financial documents")
            .expect_and_log("error getting financial documents names");
        let pdfnames: Vec<String> = pdfnames.map(|x| x.to_string()).collect();

        match etradeTaxReturnHelper::run_taxation(&rd, pdfnames, false, false) {
            Ok(_) => panic!("Expected an error from run_taxation, but got Ok"),
            Err(_) => Ok(()), // Expected error, test passes
        }
    }

    #[test]
    fn test_revolut_dividends_pln() -> Result<(), clap::Error> {
        // Get all brokerage with dividends only
        let myapp = Command::new("etradeTaxHelper").arg_required_else_help(true);
        let rd: Box<dyn etradeTaxReturnHelper::Residency> = Box::new(pl::PL {});

        let matches = create_cmd_line_pattern(myapp).get_matches_from(vec![
            "mytest",
            "revolut_data/trading-pnl-statement_2024-01-01_2024-08-04_pl-pl_8e8783.csv",
        ]);
        let pdfnames = matches
            .get_many::<String>("financial documents")
            .expect_and_log("error getting brokarage statements pdfs names");
        let pdfnames: Vec<String> = pdfnames.map(|x| x.to_string()).collect();

        match etradeTaxReturnHelper::run_taxation(&rd, pdfnames, false, false) {
            Ok(TaxCalculationResult {
                gross_income: gross_div,
                tax: tax_div,
                gross_sold,
                cost_sold,
                ..
            }) => {
                assert_eq!(
                    (gross_div, tax_div, gross_sold, cost_sold),
                    (6331.29, 871.17993, 0.0, 0.0),
                );
                Ok(())
            }
            Err(x) => panic!("Error in taxation process: {x}"),
        }
    }

    #[test]
    fn test_revolut_sold_and_dividends() -> Result<(), clap::Error> {
        // Get all brokerage with dividends only
        let myapp = Command::new("etradeTaxHelper").arg_required_else_help(true);
        let rd: Box<dyn etradeTaxReturnHelper::Residency> = Box::new(pl::PL {});

        let matches = create_cmd_line_pattern(myapp).get_matches_from(vec![
            "mytest",
            "revolut_data/trading-pnl-statement_2022-11-01_2024-09-01_pl-pl_e989f4.csv",
        ]);
        let pdfnames = matches
            .get_many::<String>("financial documents")
            .expect_and_log("error getting brokarage statements pdfs names");
        let pdfnames: Vec<String> = pdfnames.map(|x| x.to_string()).collect();

        match etradeTaxReturnHelper::run_taxation(&rd, pdfnames, false, false) {
            Ok(TaxCalculationResult {
                gross_income: gross_div,
                tax: tax_div,
                gross_sold,
                cost_sold,
                ..
            }) => {
                assert_eq!(
                    (gross_div, tax_div, gross_sold, cost_sold),
                    (9142.319, 1207.08, 22988.617, 20163.5),
                );
                Ok(())
            }
            Err(x) => panic!("Error in taxation process: {x}"),
        }
    }

    #[test]
    fn test_revolut_interests_taxation_pln() -> Result<(), clap::Error> {
        // Get all brokerage with dividends only
        let myapp = Command::new("etradeTaxHelper").arg_required_else_help(true);
        let rd: Box<dyn etradeTaxReturnHelper::Residency> = Box::new(pl::PL {});

        let matches = create_cmd_line_pattern(myapp).get_matches_from(vec![
            "mytest",
            "revolut_data/Revolut_30cze2023_27lis2023.csv",
        ]);
        let pdfnames = matches
            .get_many::<String>("financial documents")
            .expect_and_log("error getting brokarage statements pdfs names");
        let pdfnames: Vec<String> = pdfnames.map(|x| x.to_string()).collect();

        match etradeTaxReturnHelper::run_taxation(&rd, pdfnames, false, false) {
            Ok(TaxCalculationResult {
                gross_income: gross_div,
                tax: tax_div,
                gross_sold,
                cost_sold,
                ..
            }) => {
                assert_eq!(
                    (gross_div, tax_div, gross_sold, cost_sold),
                    (86.93008, 0.0, 0.0, 0.0),
                );
                Ok(())
            }
            Err(x) => panic!("Error in taxation process: {x}"),
        }
    }

    #[test]
    #[ignore]
    fn test_sold_dividends_interests_taxation() -> Result<(), clap::Error> {
        // Get all brokerage with dividends only
        let myapp = Command::new("etradeTaxHelper").arg_required_else_help(true);
        let rd: Box<dyn etradeTaxReturnHelper::Residency> = Box::new(pl::PL {});
        let matches = create_cmd_line_pattern(myapp).get_matches_from(vec![
            "mytest",
            "etrade_data_2025/ClientStatements_010226.pdf",
            "etrade_data_2025/G&L_Collapsed.xlsx",
        ]);
        let pdfnames = matches
            .get_many::<String>("financial documents")
            .expect_and_log("error getting brokarage statements pdfs names");
        let pdfnames: Vec<String> = pdfnames.map(|x| x.to_string()).collect();

        match etradeTaxReturnHelper::run_taxation(&rd, pdfnames, false, false) {
            Ok(TaxCalculationResult {
                gross_income: gross_div,
                tax: tax_div,
                gross_sold,
                cost_sold,
                ..
            }) => {
                assert_eq!(
                    (gross_div, tax_div, gross_sold, cost_sold),
                    (219.34755, 0.0, 89845.65, 44369.938),
                );
                Ok(())
            }
            Err(x) => panic!("Error in taxation process: {x}"),
        }
    }

    #[test]
    #[ignore]
    fn test_interest_adjustment_taxation() -> Result<(), clap::Error> {
        // Get all brokerage with dividends only
        let myapp = Command::new("etradeTaxHelper").arg_required_else_help(true);
        let rd: Box<dyn etradeTaxReturnHelper::Residency> = Box::new(pl::PL {});
        let matches = create_cmd_line_pattern(myapp)
            .get_matches_from(vec!["mytest", "data/example-interest-adj.pdf"]);
        let pdfnames = matches
            .get_many::<String>("financial documents")
            .expect_and_log("error getting brokarage statements pdfs names");
        let pdfnames: Vec<String> = pdfnames.map(|x| x.to_string()).collect();

        match etradeTaxReturnHelper::run_taxation(&rd, pdfnames, false, false) {
            Ok(TaxCalculationResult {
                gross_income: gross_div,
                tax: tax_div,
                gross_sold,
                cost_sold,
                ..
            }) => {
                assert_eq!(
                    (gross_div, tax_div, gross_sold, cost_sold),
                    (0.66164804, 0.0, 0.0, 0.0),
                );
                Ok(())
            }
            Err(x) => panic!("Error in taxation process: {x}"),
        }
    }
}