From 76f03540bf76f110f3da81e5c88aa43f7d74c9e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Fri, 30 Oct 2015 12:35:39 +0100 Subject: [PATCH] Correctly round the production cost - quantity of inputs should not be rounded at unit conversion - round the cost at the end of the computation loop - use Move unit_price digits to round unit_price issue5064 review21501002 --- production.py | 34 +++++++++++++++------------------- tests/scenario_production.rst | 2 +- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/production.py b/production.py index f8ca33685f..da148523b2 100644 --- a/production.py +++ b/production.py @@ -250,8 +250,7 @@ class Production(Workflow, ModelSQL, ModelView): def explode_bom(self): pool = Pool() Uom = pool.get('product.uom') - Template = pool.get('product.template') - Product = pool.get('product.product') + Move = pool.get('stock.move') if not (self.bom and self.product and self.uom): return self.cost = Decimal(0) @@ -271,15 +270,15 @@ class Production(Workflow, ModelSQL, ModelView): if move: inputs.append(move) quantity = Uom.compute_qty(input_.uom, quantity, - input_.product.default_uom) + input_.product.default_uom, round=False) self.cost += (Decimal(str(quantity)) * input_.product.cost_price) self.inputs = inputs + digits = self.__class__.cost.digits + self.cost = self.cost.quantize(Decimal(str(10 ** -digits[1]))) - if hasattr(Product, 'cost_price'): - digits = Product.cost_price.digits - else: - digits = Template.cost_price.digits + digits = Move.unit_price.digits + digit = Decimal(str(10 ** -digits[1])) outputs = [] for output in self.bom.outputs: quantity = output.compute_quantity(factor) @@ -289,8 +288,7 @@ class Production(Workflow, ModelSQL, ModelView): move.unit_price = Decimal(0) if output.product == move.product and quantity: move.unit_price = Decimal( - self.cost / Decimal(str(quantity)) - ).quantize(Decimal(str(10 ** -digits[1]))) + self.cost / Decimal(str(quantity))).quantize(digit) outputs.append(move) self.outputs = outputs @@ -373,8 +371,7 @@ class Production(Workflow, ModelSQL, ModelView): def set_moves(self): pool = Pool() - Template = pool.get('product.template') - Product = pool.get('product.product') + Move = pool.get('stock.move') storage_location = self.warehouse.storage_location location = self.location @@ -401,13 +398,13 @@ class Production(Workflow, ModelSQL, ModelView): if move: move.production_input = self move.save() - cost += Decimal(str(quantity)) * product.cost_price - - if hasattr(Product, 'cost_price'): - digits = Product.cost_price.digits - else: - digits = Template.cost_price.digits + cost += (Decimal(str(move.internal_quantity)) * + product.cost_price) + digits = self.__class__.cost.digits + cost = cost.quantize(Decimal(str(10 ** -digits[1]))) + digits = Move.unit_price.digits + digit = Decimal(str(10 ** -digits[1])) for output in self.bom.outputs: quantity = output.compute_quantity(factor) product = output.product @@ -417,8 +414,7 @@ class Production(Workflow, ModelSQL, ModelView): move.production_output = self if product == self.product: move.unit_price = Decimal( - cost / Decimal(str(quantity)) - ).quantize(Decimal(str(10 ** -digits[1]))) + cost / Decimal(str(quantity))).quantize(digit) move.save() self._set_move_planned_date() diff --git a/tests/scenario_production.rst b/tests/scenario_production.rst index b0a8d1db3f..6165f5f797 100644 --- a/tests/scenario_production.rst +++ b/tests/scenario_production.rst @@ -139,7 +139,7 @@ Make a production:: >>> output.quantity == 2 True >>> production.cost - Decimal('25.0') + Decimal('25.0000') >>> production.click('wait') >>> production.state u'waiting'