Finish implementing move money

This commit is contained in:
Piv
2023-01-28 23:13:49 +10:30
parent a2091c13b4
commit ba279c8c9b
3 changed files with 194 additions and 79 deletions

View File

@@ -1,7 +1,7 @@
use std::{collections::HashMap, error::Error, io::Write, path::PathBuf};
use clap::{builder::PathBufValueParser, Parser, Subcommand};
use coster_rs::Unit;
use clap::{Parser, Subcommand};
use coster_rs::{CsvCost, Unit};
use serde::Deserialize;
#[derive(Parser)]
@@ -26,6 +26,9 @@ enum Commands {
#[clap(short, long, parse(from_os_str), value_name = "FILE")]
output: Option<PathBuf>,
#[clap(short, long, default_value_t = false)]
use_numeric_accounts: bool,
},
/// Combines rules to the minimum set required
smush_rules {
@@ -56,7 +59,8 @@ fn main() -> anyhow::Result<()> {
rules,
lines,
output,
} => move_money(rules, lines, output),
use_numeric_accounts,
} => move_money(rules, lines, output, use_numeric_accounts),
Commands::smush_rules { rules, output } => smush_rules(rules, output),
Commands::allocate_overheads {
rules,
@@ -66,58 +70,18 @@ fn main() -> anyhow::Result<()> {
}
}
fn move_money(rules: PathBuf, lines: PathBuf, output: Option<PathBuf>) -> anyhow::Result<()> {
let mut rdr = csv::Reader::from_path(lines)?;
let headers = rdr.headers()?;
let mut account_index = 0;
let mut department_index = 0;
for (index, field) in headers.iter().enumerate() {
if field == "Account" {
account_index = index;
} else if field == "Department" {
department_index = index;
}
}
let lines: HashMap<Unit, f64> = rdr
.records()
.map(|record| {
let record = record.unwrap();
let account = record.get(account_index).unwrap();
let department = record.get(department_index).unwrap();
let sum = record
.iter()
.enumerate()
.filter(|(i, _)| *i != account_index && *i != department_index)
.map(|(_, f)| f.parse::<f64>().unwrap())
.sum();
(
Unit {
account: account.into(),
department: department.into(),
},
sum,
)
})
.collect();
// read rules into required struct (basically map each line into an array). Need to figure out the input format...
let mut rdr = csv::Reader::from_path(rules)?;
for result in rdr.deserialize() {
let record: CsvMovementRule = result?;
// TODO: Problem is the from/to are ranges, that rely on the gl data, so need to get a list of
//
}
// Read gl lines data (all of it). For each line, sum the periods, and insert the summed
// line into a hashmap (need to read the whole gl in as we can move between every line)
// We use a record, everything that's not an account/department is assumed to be a period
// Then run move_moeny
// Ouput the list moved moneys
Ok(())
fn move_money(
rules: PathBuf,
lines: PathBuf,
output: Option<PathBuf>,
use_numeric_accounts: bool,
) -> anyhow::Result<()> {
coster_rs::move_money(
csv::Reader::from_path(rules)?,
csv::Reader::from_path(lines)?,
csv::Writer::from_path(output.unwrap_or(PathBuf::from("output.csv")))?,
use_numeric_accounts,
)
}
fn smush_rules(rules_path: PathBuf, output: Option<PathBuf>) -> anyhow::Result<()> {
@@ -144,23 +108,6 @@ fn allocate_overheads(
Ok(())
}
#[derive(Debug, Deserialize)]
struct CsvMovementRule {
#[serde(rename = "FromCC")]
// Need strings to further split later
from_departments: String,
to_departments: String,
all_from_departments: bool,
all_to_departments: bool,
from_accounts: String,
to_accounts: String,
all_from_accounts: bool,
all_to_accounts: bool,
amount: f64,
is_percent: Option<bool>,
is_separator: Option<bool>,
}
#[derive(Debug, Deserialize)]
struct CsvOverheadAllocationRule {
from_overhead_department: String,
@@ -168,10 +115,3 @@ struct CsvOverheadAllocationRule {
percent: f64,
to_department_type: String,
}
#[derive(Debug, Deserialize)]
struct CsvCost {
account: String,
department: String,
value: f64,
}