From 540e4c778da92cb16ad7d6794e4bd3cbcd01c57a Mon Sep 17 00:00:00 2001 From: michaelpivato Date: Thu, 9 Mar 2023 12:51:10 +1030 Subject: [PATCH] Ignore ccs with no area, use hashset for tracking overhead ccs --- src/overhead_allocation.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/overhead_allocation.rs b/src/overhead_allocation.rs index 7ecabf5..1930011 100644 --- a/src/overhead_allocation.rs +++ b/src/overhead_allocation.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, io::Read, path::Path}; +use std::{collections::{HashMap, HashSet}, io::Read, path::Path}; use itertools::Itertools; use nalgebra::{DMatrix, Dynamic, LU}; @@ -199,6 +199,9 @@ where let cost_centre: HashMap = cost_centre?; let name = cost_centre.get("Code").unwrap(); let area = cost_centre.get("Area").unwrap(); + if area.is_empty() { + continue; + } area_ccs .entry(area.clone()) .or_insert(Vec::new()) @@ -224,7 +227,7 @@ where let mut overhead_other_total: Vec<(String, String, f64)> = Vec::new(); // Save overhead ccs, so we later know whether a to cc is overhead or operating - let mut overhead_ccs: Vec = Vec::new(); + let mut overhead_ccs: HashSet = HashSet::new(); // overhead department -> total (summed limit to costs) let mut overhead_cc_totals: HashMap = HashMap::new(); // For each overhead area, get the cost centres in the area (overhead cost centres), and get all cost centres @@ -246,7 +249,9 @@ where } let mut current_area_ccs = current_area_ccs.unwrap().clone(); - overhead_ccs.append(&mut current_area_ccs); + for cc in current_area_ccs { + overhead_ccs.insert(cc); + } let overhead_ccs = area_ccs.get(area_name).unwrap(); // TODO: This depends on the area limit criteria. For now just doing any limit criteria let mut limited_ccs: Vec = Vec::new(); @@ -325,7 +330,7 @@ where from_overhead_department: from_overhead_department.clone(), to_department: to_department.clone(), percent: percent / overhead_cc_totals.get(from_overhead_department).unwrap(), - to_department_type: if overhead_ccs.contains(&to_department) { + to_department_type: if overhead_ccs.contains(to_department) { DepartmentType::Overhead } else { DepartmentType::Operating @@ -422,7 +427,11 @@ fn split_allocation_statistic_range( // Removes quotes and padding from accounts int he allocation statistic account range. // e.g. "'100' " becomes "100" fn remove_quote_and_padding(s: &str) -> String { - s.trim()[1..s.trim().len() - 1].to_owned() + if s.contains('\'') { + s.trim()[1..s.trim().len() - 1].to_owned() + } else { + s.trim().to_owned() + } } // Perform the reciprocal allocation (matrix) method to allocate servicing departments (indirect) costs