From 220b918dfa57ea07c78e82ae9d67ecb950bca085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sun, 26 Jan 2020 21:10:40 +0100 Subject: [PATCH] Store product cost price revision The "Modify Cost Price" wizard create a revision record for each template or product. The cost price re-computation methods apply those revisions at the beginning of their date. The stock accounting move is created and posted when the cost price is modified by the re-computation. issue8930 review260751002 --- product.py | 14 +++++++++----- ...ario_product_cost_fifo_recompute_cost_price.rst | 4 ++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/product.py b/product.py index 9eaceac2e0..af1b9c67bf 100644 --- a/product.py +++ b/product.py @@ -95,6 +95,7 @@ class Product(metaclass=PoolMeta): Move = pool.get('stock.move') Currency = pool.get('currency.currency') Uom = pool.get('product.uom') + Revision = pool.get('product.cost_price.revision') digits = self.__class__.cost_price.digits domain = [ @@ -116,6 +117,8 @@ class Product(metaclass=PoolMeta): moves = Move.search( domain, order=[('effective_date', 'ASC'), ('id', 'ASC')]) + revisions = Revision.get_for_product(self) + cost_price = Decimal(0) quantity = 0 if start: @@ -197,6 +200,8 @@ class Product(metaclass=PoolMeta): current_out_qty = 0 current_moves.append(move) + cost_price = Revision.apply_up_to( + revisions, cost_price, move.effective_date) qty = Uom.compute_qty(move.uom, move.quantity, self.default_uom) qty = Decimal(str(qty)) if move.from_location.type == 'storage': @@ -237,12 +242,11 @@ class Product(metaclass=PoolMeta): dict(cost_price=fifo_cost_price)) if quantity: cost_price = ( - ((current_cost_price * ( - quantity + current_out_qty)) + ((cost_price * (quantity + current_out_qty)) - (fifo_cost_price * current_out_qty)) / quantity) else: cost_price = Decimal(0) - current_cost_price = cost_price.quantize( - Decimal(str(10.0 ** -digits[1]))) - return current_cost_price + for revision in revisions: + cost_price = revision.get_cost_price(cost_price) + return cost_price diff --git a/tests/scenario_product_cost_fifo_recompute_cost_price.rst b/tests/scenario_product_cost_fifo_recompute_cost_price.rst index f8b952fd15..c6e028991a 100644 --- a/tests/scenario_product_cost_fifo_recompute_cost_price.rst +++ b/tests/scenario_product_cost_fifo_recompute_cost_price.rst @@ -110,7 +110,7 @@ Recompute cost price:: >>> product.reload() >>> product.cost_price - Decimal('99.9999') + Decimal('99.9998') Recompute cost price from a date:: @@ -123,4 +123,4 @@ Recompute cost price from a date:: >>> product.reload() >>> product.cost_price - Decimal('99.9999') + Decimal('99.9998')