Commit dd0f98d4 authored by Andrew Danger Lyon's avatar Andrew Danger Lyon

ok, tying resource tagging into costs, and adding rudimentary test. this should be done. tracker#31

parent aa4518b7
......@@ -41,6 +41,7 @@ pub fn calculate_costs(orders_incoming: &Vec<Order>, orders_outgoing: &Vec<Order
// holds product_id -> average_costs for products we bought for inventory
let mut avg_input_costs: HashMap<String, Costs> = HashMap::new();
// labor is an operating cost
{
let op_costs = sum_costs.entry(CostCategory::Operating).or_insert(Default::default());
for entry in labor {
......@@ -56,10 +57,18 @@ pub fn calculate_costs(orders_incoming: &Vec<Order>, orders_outgoing: &Vec<Order
let cat = order.cost_category.clone();
let current = sum_costs.entry(cat).or_insert(Default::default());
for prod in &order.products {
*current = current.clone() + (prod.costs.clone() * prod.quantity);
let mut prod_costs = prod.costs.clone() * prod.quantity;
// if this product is a resource, add its id and quantity to the
// cost list
if prod.is_resource() {
let mut tmp_costs = Costs::new();
tmp_costs.track(&prod.product_id, prod.quantity);
prod_costs = prod_costs + tmp_costs;
}
*current = current.clone() + prod_costs.clone();
if cat == CostCategory::Inventory {
let prod_inp_costs = sum_inventory_costs.entry(prod.product_id.clone()).or_insert(vec![]);
prod_inp_costs.push(prod.costs.clone() * prod.quantity);
prod_inp_costs.push(prod_costs);
}
}
}
......@@ -182,7 +191,7 @@ pub mod tests {
// TODO: write tests
#[test]
fn calculates() {
calculate_costs(&vec![], &vec![], &HashMap::new(), &HashMap::new()).expect("costs failed");
calculate_costs(&vec![], &vec![], &vec![], &HashMap::new(), &HashMap::new()).expect("costs failed");
}
}
......@@ -102,7 +102,7 @@ impl Transaction for TxCreate {
}
}
}
schema.orders_create(&self.id, &self.company_id_from, &self.company_id_to, &self.cost_category, &self.products, &self.created, &hash);
schema.orders_create(&self.id, &self.company_id_from, &self.company_id_to, &self.cost_category, &products, &self.created, &hash);
Ok(())
}
}
......@@ -250,6 +250,16 @@ pub mod tests {
);
testkit.create_block_with_transactions(txvec![tx_prod]);
let tag_id = gen_uuid();
let tx_tag = transactions::resource_tag::TxCreate::sign(
&tag_id,
&prod_id,
&util::time::now(),
&root_pub,
&root_sec
);
testkit.create_block_with_transactions(txvec![tx_tag]);
let labor_id = gen_uuid();
let tx_labor1 = transactions::labor::TxCreate::sign(
&labor_id,
......@@ -316,6 +326,10 @@ pub mod tests {
assert_eq!(idx_from.keys().count(), 0);
assert_eq!(idx_to.keys().count(), 0);
// test for resource tagging
let order = Schema::new(&snapshot).get_order(&ord1_id).unwrap();
assert!(order.products[0].is_resource());
let ord1_date: DateTime<Utc> = "2018-01-01T00:00:00Z".parse().unwrap();
let ord2_date: DateTime<Utc> = "2018-07-01T00:00:00Z".parse().unwrap();
let ord3_date: DateTime<Utc> = "2019-03-01T00:00:00Z".parse().unwrap();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment