This repository has been archived by the owner on Jul 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
extern crate clap; | ||
extern crate rust_decimal; | ||
|
||
use clap::Parser; | ||
use rust_decimal::Decimal; | ||
use rust_decimal_macros::dec; | ||
|
||
use crate::{ | ||
config::Config, | ||
db::{self, TaxDb}, | ||
}; | ||
|
||
#[derive(Parser, Debug)] | ||
pub struct InsertArgs { | ||
#[clap( | ||
short, | ||
long, | ||
help = "Decimal income value in BAM (will be rounded to 2 decimals) after deduction" | ||
)] | ||
deduced_income: Option<Decimal>, | ||
#[clap( | ||
short, | ||
long, | ||
help = "Decimal income value in BAM (will be rounded to 2 decimals)" | ||
)] | ||
income: Option<Decimal>, | ||
#[clap( | ||
long, | ||
help = "Tax deduction percentage (20 default, 30 for income from authored work). Applied only when income is used and not deduced income", | ||
default_value_t = dec!(20) | ||
)] | ||
deduction_percentage: Decimal, | ||
#[clap(long, help = "Invoice date (YYYY-MM-DD)")] | ||
invoice_date: String, | ||
} | ||
|
||
pub fn handle_command(config: Config, args: &InsertArgs) { | ||
let income = match &args.income { | ||
Some(inc) => { | ||
let deduction_factor: Decimal = | ||
dec!(1) - (args.deduction_percentage.round_dp(2) * dec!(0.01)); | ||
inc * deduction_factor | ||
} | ||
None => match &args.deduced_income { | ||
Some(deduced_income) => deduced_income.clone(), | ||
None => panic!("Provide either --income or --deduced_income!"), | ||
}, | ||
}; | ||
// TODO: extract tax calculations into a common module | ||
let health_insurance = income * dec!(0.04); | ||
let tax_base = income - health_insurance; | ||
let tax_amount: Decimal = tax_base * dec!(0.10); | ||
let mut tax_db: TaxDb = db::parse_db_with_default(config.db_location.as_str()); | ||
tax_db.add_ams_info( | ||
db::AmsInfo { | ||
income_total: income, | ||
tax_paid: tax_amount, | ||
}, | ||
args.invoice_date.clone(), | ||
); | ||
tax_db.write_to_file(config.db_location.as_str()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
extern crate clap; | ||
|
||
use clap::Parser; | ||
|
||
use crate::{ | ||
config::Config, | ||
db::{self, TaxDb}, | ||
forms::amsform::{self, FormField}, | ||
}; | ||
|
||
#[derive(Parser, Debug)] | ||
pub struct LoadArgs { | ||
#[clap(short, long)] | ||
file: String, | ||
} | ||
|
||
pub fn handle_command(config: Config, args: &LoadArgs) { | ||
let form = amsform::load_ams_form(args.file.clone()); | ||
|
||
let total_paid = form.get_number_field_value(FormField::TaxToPayTotal); | ||
let income = form.get_number_field_value(FormField::TaxBaseTotal) | ||
+ form.get_number_field_value(FormField::HealthInsuranceTotal); | ||
|
||
let invoice_date = [ | ||
form.get_text_field_value(FormField::PaymentDateYear), | ||
"-".to_string(), | ||
form.get_text_field_value(FormField::PaymentDateMonth), | ||
"-".to_string(), | ||
form.get_text_field_value(FormField::PaymentDateDay), | ||
] | ||
.concat(); | ||
|
||
let mut tax_db: TaxDb = db::parse_db_with_default(config.db_location.as_str()); | ||
tax_db.add_ams_info( | ||
db::AmsInfo { | ||
income_total: income, | ||
tax_paid: total_paid, | ||
}, | ||
invoice_date, | ||
); | ||
tax_db.write_to_file(config.db_location.as_str()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
extern crate clap; | ||
extern crate rust_decimal; | ||
|
||
mod insert; | ||
mod load; | ||
|
||
use crate::config::Config; | ||
use clap::{AppSettings, Parser, Subcommand}; | ||
|
||
use self::{insert::InsertArgs, load::LoadArgs}; | ||
|
||
#[derive(Parser, Debug)] | ||
#[clap(setting(AppSettings::SubcommandRequiredElseHelp))] | ||
pub struct DbArgs { | ||
#[clap(subcommand)] | ||
command: DbCommands, | ||
} | ||
|
||
#[derive(Subcommand, Debug)] | ||
enum DbCommands { | ||
Load(LoadArgs), | ||
Insert(InsertArgs), | ||
} | ||
|
||
pub fn handle_command(config: Config, args: &DbArgs) { | ||
match &args.command { | ||
DbCommands::Load(load_args) => load::handle_command(config, load_args), | ||
DbCommands::Insert(insert_args) => insert::handle_command(config, insert_args), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod ams; | ||
pub mod db; | ||
pub mod gpd; | ||
pub mod taxbreakdown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters