Simplified logic re: like_kind dates and treatment.
This commit is contained in:
parent
1c7209eb95
commit
36b114223e
|
@ -163,7 +163,7 @@ pub(crate) fn elect_like_kind_treatment(cutoff_date_arg: &Option<String>) -> Res
|
|||
Some(cutoff_date_arg) => {
|
||||
let provided_date = NaiveDate::parse_from_str(&cutoff_date_arg, "%y-%m-%d")
|
||||
.unwrap_or(NaiveDate::parse_from_str(&cutoff_date_arg, "%Y-%m-%d")
|
||||
.expect("Date entered as -c command line arg has an incorrect format."));
|
||||
.expect("Date entered as -l (like-kind cutoff date) command line arg has an incorrect format."));
|
||||
|
||||
println!("\nUse like-kind exchange treatment through {}? [Y/n/c] ('c' to 'change') ", provided_date);
|
||||
|
||||
|
|
|
@ -40,17 +40,14 @@ impl fmt::Display for InventoryCostingMethod {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct LikeKindSettings {
|
||||
pub like_kind_cutoff_date: NaiveDate,
|
||||
pub like_kind_basis_date_preserved: bool,
|
||||
}
|
||||
|
||||
pub struct ImportProcessParameters {
|
||||
pub export_path: PathBuf,
|
||||
pub home_currency: String,
|
||||
pub enable_like_kind_treatment: bool,
|
||||
pub lk_cutoff_date_string: String,
|
||||
pub lk_treatment_enabled: bool,
|
||||
/// NaiveDate either from "1-1-1" (default and not to be used) or the actual date chosen (or passed in via Cli option)
|
||||
pub lk_cutoff_date: NaiveDate,
|
||||
pub lk_basis_date_preserved: bool,
|
||||
pub costing_method: InventoryCostingMethod,
|
||||
pub input_file_date_separator: String,
|
||||
pub input_file_has_iso_date_style: bool,
|
||||
|
@ -65,7 +62,6 @@ pub(crate) fn import_and_process_final(
|
|||
HashMap<u16, RawAccount>,
|
||||
HashMap<u32, ActionRecord>,
|
||||
HashMap<u32, Transaction>,
|
||||
Option<LikeKindSettings>,
|
||||
), Box<dyn Error>> {
|
||||
|
||||
let mut transactions_map: HashMap<u32, Transaction> = HashMap::new();
|
||||
|
@ -121,22 +117,6 @@ pub(crate) fn import_and_process_final(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
let likekind_settings: Option<LikeKindSettings> = if settings.enable_like_kind_treatment {
|
||||
|
||||
let like_kind_cutoff_date = &settings.lk_cutoff_date_string;
|
||||
|
||||
Some(
|
||||
LikeKindSettings {
|
||||
like_kind_cutoff_date: NaiveDate::parse_from_str(&like_kind_cutoff_date, "%y-%m-%d")
|
||||
.unwrap_or(NaiveDate::parse_from_str(&like_kind_cutoff_date, "%Y-%m-%d")
|
||||
.expect("Found date string with improper format")),
|
||||
like_kind_basis_date_preserved: true,
|
||||
}
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
transactions_map = create_lots_mvmts::create_lots_and_movements(
|
||||
transactions_map,
|
||||
&action_records_map,
|
||||
|
@ -144,7 +124,9 @@ pub(crate) fn import_and_process_final(
|
|||
&mut account_map,
|
||||
&settings.home_currency,
|
||||
&settings.costing_method,
|
||||
&likekind_settings,
|
||||
settings.lk_treatment_enabled,
|
||||
settings.lk_cutoff_date,
|
||||
settings.lk_basis_date_preserved,
|
||||
&mut lot_map,
|
||||
)?;
|
||||
|
||||
|
@ -170,13 +152,11 @@ pub(crate) fn import_and_process_final(
|
|||
println!(" Successfully added proceeds to movements.");
|
||||
|
||||
|
||||
if let Some(lk_settings) = &likekind_settings {
|
||||
if settings.lk_treatment_enabled {
|
||||
|
||||
let cutoff_date = lk_settings.like_kind_cutoff_date;
|
||||
println!(" Applying like-kind treatment for cut-off date: {}.", cutoff_date);
|
||||
println!(" Applying like-kind treatment for cut-off date: {}.", settings.lk_cutoff_date);
|
||||
|
||||
import_cost_proceeds_etc::apply_like_kind_treatment(
|
||||
cutoff_date,
|
||||
&settings,
|
||||
&action_records_map,
|
||||
&raw_account_map,
|
||||
|
@ -185,7 +165,7 @@ pub(crate) fn import_and_process_final(
|
|||
)?;
|
||||
|
||||
println!(" Successfully applied like-kind treatment.");
|
||||
};
|
||||
}
|
||||
|
||||
Ok((account_map, raw_account_map, action_records_map, transactions_map, likekind_settings))
|
||||
Ok((account_map, raw_account_map, action_records_map, transactions_map))
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ use chrono::NaiveDate;
|
|||
|
||||
use crate::transaction::{Transaction, ActionRecord, TxType, Polarity, TxHasMargin};
|
||||
use crate::account::{Account, RawAccount, Lot, Movement};
|
||||
use crate::core_functions::{InventoryCostingMethod, LikeKindSettings};
|
||||
use crate::core_functions::{InventoryCostingMethod};
|
||||
use crate::decimal_utils::{round_d128_1e8};
|
||||
|
||||
pub(crate) fn create_lots_and_movements(
|
||||
|
@ -21,27 +21,13 @@ pub(crate) fn create_lots_and_movements(
|
|||
acct_map: &HashMap<u16, Account>,
|
||||
chosen_home_currency: &String,
|
||||
chosen_costing_method: &InventoryCostingMethod,
|
||||
likekind_settings: &Option<LikeKindSettings>,
|
||||
enable_lk_treatment: bool,
|
||||
like_kind_cutoff_date: NaiveDate,
|
||||
lk_basis_date_preserved: bool,
|
||||
lot_map: &HashMap<(RawAccount, u32), Lot>,
|
||||
) -> Result<HashMap<u32,Transaction>, Box<dyn Error>> {
|
||||
|
||||
// Set values to be referred to repeatedly, potentially, in Incoming Exchange transactions
|
||||
let multiple_incoming_mvmts_per_ar = match &likekind_settings {
|
||||
Some(likekind_settings) => {
|
||||
likekind_settings.like_kind_basis_date_preserved
|
||||
}
|
||||
None => {
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
let mut like_kind_cutoff_date: NaiveDate = NaiveDate::parse_from_str("1/1/1", "%m/%d/%y")
|
||||
.expect("NaiveDate string parsing failed. It shouldn't have. Try again.");
|
||||
// Above sets date never to be used. Below modifies date in case it will be used.
|
||||
if likekind_settings.is_some() {
|
||||
let likekind_settings_clone = likekind_settings.clone().unwrap();
|
||||
like_kind_cutoff_date = likekind_settings_clone.like_kind_cutoff_date;
|
||||
}
|
||||
let multiple_incoming_mvmts_per_ar = lk_basis_date_preserved;
|
||||
|
||||
// On with the creating of lots and movements.
|
||||
let length = txns_map.len();
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
use std::collections::{HashMap};
|
||||
use std::error::Error;
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use decimal::d128;
|
||||
|
||||
use crate::transaction::{Transaction, TxType, ActionRecord, Polarity};
|
||||
|
@ -263,7 +262,6 @@ pub(crate) fn add_proceeds_to_movements(
|
|||
}
|
||||
|
||||
pub(crate) fn apply_like_kind_treatment(
|
||||
cutoff_date: NaiveDate,
|
||||
settings: &ImportProcessParameters,
|
||||
ars: &HashMap<u32, ActionRecord>,
|
||||
raw_acct_map: &HashMap<u16, RawAccount>,
|
||||
|
@ -272,6 +270,7 @@ pub(crate) fn apply_like_kind_treatment(
|
|||
) -> Result<(), Box<dyn Error>> {
|
||||
|
||||
let length = txns_map.len();
|
||||
let cutoff_date = settings.lk_cutoff_date;
|
||||
for txn_num in 1..=length {
|
||||
|
||||
let txn_num = txn_num as u32;
|
||||
|
|
|
@ -123,7 +123,6 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
raw_acct_map,
|
||||
action_records_map,
|
||||
transactions_map,
|
||||
like_kind_settings
|
||||
) = core_functions::import_and_process_final(input_file_path, &settings)?;
|
||||
|
||||
let should_export = settings.should_export;
|
||||
|
|
17
src/setup.rs
17
src/setup.rs
|
@ -6,6 +6,8 @@ use std::path::PathBuf;
|
|||
use std::error::Error;
|
||||
use std::process;
|
||||
|
||||
use chrono::NaiveDate;
|
||||
|
||||
use crate::cli_user_choices;
|
||||
use crate::core_functions::{ImportProcessParameters, InventoryCostingMethod};
|
||||
use crate::skip_wizard;
|
||||
|
@ -48,13 +50,24 @@ pub (crate) fn run_setup(args: super::Cli) -> Result<(PathBuf, ImportProcessPara
|
|||
output_dir_path,
|
||||
) = wizard_or_not(args.flags.accept_args, wizard_or_not_args)?;
|
||||
|
||||
let like_kind_cutoff_date;
|
||||
|
||||
if like_kind_election {
|
||||
like_kind_cutoff_date = NaiveDate::parse_from_str(&like_kind_cutoff_date_string, "%y-%m-%d")
|
||||
.unwrap_or(NaiveDate::parse_from_str(&like_kind_cutoff_date_string, "%Y-%m-%d")
|
||||
.expect("Command line date (like-kind cutoff option) has an incorrect format. Program must abort."));
|
||||
} else {
|
||||
like_kind_cutoff_date = NaiveDate::parse_from_str(&"1-1-1", "%y-%m-%d").unwrap();
|
||||
}
|
||||
|
||||
let settings = ImportProcessParameters {
|
||||
home_currency: args.opts.home_currency.into_string().unwrap().to_uppercase(),
|
||||
input_file_has_iso_date_style: args.flags.iso_date,
|
||||
input_file_date_separator: date_separator.to_string(),
|
||||
costing_method: costing_method_choice,
|
||||
enable_like_kind_treatment: like_kind_election,
|
||||
lk_cutoff_date_string: like_kind_cutoff_date_string,
|
||||
lk_treatment_enabled: like_kind_election,
|
||||
lk_cutoff_date: like_kind_cutoff_date,
|
||||
lk_basis_date_preserved: true, // TODO
|
||||
should_export: should_export,
|
||||
export_path: output_dir_path,
|
||||
};
|
||||
|
|
|
@ -78,12 +78,12 @@ Home currency: {}
|
|||
Enable like-kind treatment: {}",
|
||||
settings.costing_method,
|
||||
settings.home_currency,
|
||||
settings.enable_like_kind_treatment
|
||||
settings.lk_treatment_enabled
|
||||
)?;
|
||||
|
||||
if settings.enable_like_kind_treatment {
|
||||
if settings.lk_treatment_enabled {
|
||||
writeln!(file, "Like-kind cut-off date: {}.",
|
||||
settings.lk_cutoff_date_string
|
||||
settings.lk_cutoff_date
|
||||
)?;
|
||||
}
|
||||
|
||||
|
@ -226,12 +226,12 @@ Home currency: {}
|
|||
Enable like-kind treatment: {}",
|
||||
settings.costing_method,
|
||||
settings.home_currency,
|
||||
settings.enable_like_kind_treatment
|
||||
settings.lk_treatment_enabled
|
||||
)?;
|
||||
|
||||
if settings.enable_like_kind_treatment {
|
||||
if settings.lk_treatment_enabled {
|
||||
writeln!(file, "Like-kind cut-off date: {}.",
|
||||
settings.lk_cutoff_date_string
|
||||
settings.lk_cutoff_date
|
||||
)?;
|
||||
}
|
||||
|
||||
|
@ -308,12 +308,12 @@ Home currency: {}
|
|||
Enable like-kind treatment: {}",
|
||||
settings.costing_method,
|
||||
settings.home_currency,
|
||||
settings.enable_like_kind_treatment
|
||||
settings.lk_treatment_enabled
|
||||
)?;
|
||||
|
||||
if settings.enable_like_kind_treatment {
|
||||
if settings.lk_treatment_enabled {
|
||||
writeln!(file, "Like-kind cut-off date: {}.",
|
||||
settings.lk_cutoff_date_string
|
||||
settings.lk_cutoff_date
|
||||
)?;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue