From def8e0f9e8566536b1549a486f61a103fa896d6f Mon Sep 17 00:00:00 2001 From: Sergi Almacellas Abellana Date: Mon, 24 Mar 2014 16:02:19 +0100 Subject: [PATCH] Fix cost calculation with diferent uoms in bom --- plan.py | 25 +++++++++++++++++-------- tests/scenario_product_cost_plan.rst | 11 +++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/plan.py b/plan.py index e108742..7df00e0 100644 --- a/plan.py +++ b/plan.py @@ -159,10 +159,10 @@ class Plan(Workflow, ModelSQL, ModelView): self.product_cost) def on_change_with_product_cost(self, name=None): - return sum(p.total for p in self.products if p.total) + return sum(p.total for p in self.products if p.total) def on_change_with_cost_price(self, name=None): - return sum(c.cost for c in self.costs if c.cost) + return sum(c.cost for c in self.costs if c.cost) def on_change_with_product_uom_category(self, name=None): if self.product: @@ -290,15 +290,26 @@ class Plan(Workflow, ModelSQL, ModelView): *factor*: The factor to calculate the quantity """ pool = Pool() + Uom = pool.get('product.uom') Input = pool.get('production.bom.input') + ProductLine = pool.get('product.cost.plan.product_line') quantity = Input.compute_quantity(input_, factor) + cost_factor = Decimal(Uom.compute_qty(input_.product.default_uom, 1, + input_.uom)) + digits = ProductLine.product_cost_price.digits[1] + product_cost_price = (input_.product.cost_price / + cost_factor).quantize(Decimal(str(10 ** -digits))) + digits = ProductLine.cost_price.digits[1] + cost_price = (input_.product.cost_price / + cost_factor).quantize(Decimal(str(10 ** -digits))) + return { 'name': input_.product.rec_name, 'product': input_.product.id, 'quantity': quantity, 'uom': input_.uom.id, - 'product_cost_price': input_.product.cost_price, - 'cost_price': input_.product.cost_price, + 'product_cost_price': product_cost_price, + 'cost_price': cost_price, } @classmethod @@ -378,7 +389,7 @@ class PlanProductLine(ModelSQL, ModelView): res['uom'] = self.product.default_uom.id res['uom.rec_name'] = self.product.default_uom.rec_name res['product_cost_price'] = self.product.cost_price - res['cost_price'] = self.product.cost_price + res['cost_price'] = self.product.cost_price else: res['name'] = None res['uom'] = None @@ -400,9 +411,7 @@ class PlanProductLine(ModelSQL, ModelView): return cost.quantize(Decimal(str(10 ** -digits))) def on_change_with_total(self, name=None): - pool = Pool() - Uom = pool.get('product.uom') - quantity = self.quantity + quantity = self.quantity if not quantity: return Decimal('0.0') total = Decimal(str(quantity)) * (self.cost_price or Decimal('0.0')) diff --git a/tests/scenario_product_cost_plan.rst b/tests/scenario_product_cost_plan.rst index 47e44c5..502b81c 100644 --- a/tests/scenario_product_cost_plan.rst +++ b/tests/scenario_product_cost_plan.rst @@ -183,6 +183,7 @@ Create a cost plan for product (without child boms):: True >>> plan.boms[0].bom != None True + >>> plan.boms[0].bom = None >>> plan.save() >>> plan.state u'draft' @@ -195,9 +196,13 @@ Create a cost plan for product (without child boms):: >>> c1, = plan.products.find([ ... ('product', '=', component1.id), ... ], limit=1) + >>> c1.quantity == 5.0 + True >>> c2, = plan.products.find([ ... ('product', '=', component2.id), ... ], limit=1) + >>> c2.quantity == 150.0 + True >>> cA = plan.products.find([ ... ('product', '=', componentA.id), ... ], limit=1) @@ -252,11 +257,17 @@ Create a cost plan for product (with child boms):: >>> cA, = plan.products.find([ ... ('product', '=', componentA.id), ... ], limit=1) + >>> cA.quantity == 5.0 + True >>> cB, = plan.products.find([ ... ('product', '=', componentB.id), ... ], limit=1) + >>> cB.quantity == 5.0 + True >>> c2, = plan.products.find([ ... ('product', '=', component2.id), ... ], limit=1) + >>> c2.quantity == 150.0 + True >>> plan.cost_price == Decimal('17.5') True