From 3827d940d065ce00f3654cd8809ddd01f756092f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 5 Jul 2017 14:51:46 +0200 Subject: [PATCH] Move cost price to product level instead of template issue6515 review41321002 --- move.py | 2 +- product.py | 21 ++++++++++----------- tests/scenario_product_cost_fifo.rst | 17 ++++++++--------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/move.py b/move.py index 93d9c7bb71..e44a66969b 100644 --- a/move.py +++ b/move.py @@ -44,7 +44,7 @@ class Move: total_qty = Uom.compute_qty(self.uom, self.quantity, self.product.default_uom, round=False) - fifo_moves = self.product.template.get_fifo_move(total_qty) + fifo_moves = self.product.get_fifo_move(total_qty) cost_price = Decimal("0.0") consumed_qty = 0.0 diff --git a/product.py b/product.py index 4ef19e8ad8..316057a95b 100644 --- a/product.py +++ b/product.py @@ -16,7 +16,12 @@ class Template: super(Template, cls).__setup__() new_sel = ('fifo', 'FIFO') if new_sel not in cls.cost_price_method.selection: - cls.cost_price_method.selection.append(new_sel) + cls.cost_price_method._field.selection.append(new_sel) + + +class Product: + __metaclass__ = PoolMeta + __name__ = 'product.product' def get_fifo_move(self, quantity=0.0): ''' @@ -29,7 +34,6 @@ class Template: Move = pool.get('stock.move') Uom = pool.get('product.uom') Location = pool.get('stock.location') - Template = pool.get('product.template') locations = Location.search([ ('type', '=', 'storage'), @@ -38,15 +42,15 @@ class Template: location_ids = [l.id for l in locations] with Transaction().set_context(locations=location_ids, stock_date_end=stock_date_end): - template = Template(self.id) + product = self.__class__(self.id) offset = 0 limit = Transaction().database.IN_MAX - avail_qty = template.quantity + avail_qty = product.quantity fifo_moves = [] while avail_qty > 0.0: moves = Move.search([ - ('product.template.id', '=', template.id), + ('product', '=', product.id), ('state', '=', 'done'), ('from_location.type', '=', 'supplier'), ], offset=offset, limit=limit, @@ -58,7 +62,7 @@ class Template: for move in moves: qty = Uom.compute_qty(move.uom, move.quantity - move.fifo_quantity, - template.default_uom, round=False) + product.default_uom, round=False) avail_qty -= qty if avail_qty <= quantity: @@ -73,10 +77,5 @@ class Template: fifo_moves.reverse() return fifo_moves - -class Product: - __metaclass__ = PoolMeta - __name__ = 'product.product' - def recompute_cost_price_fifo(self): return self.recompute_cost_price_average() diff --git a/tests/scenario_product_cost_fifo.rst b/tests/scenario_product_cost_fifo.rst index 77dad4be88..f332dbbffd 100644 --- a/tests/scenario_product_cost_fifo.rst +++ b/tests/scenario_product_cost_fifo.rst @@ -26,19 +26,18 @@ Create product:: >>> ProductUom = Model.get('product.uom') >>> ProductTemplate = Model.get('product.template') - >>> Product = Model.get('product.product') >>> unit, = ProductUom.find([('name', '=', 'Unit')]) - >>> product = Product() + >>> template = ProductTemplate() >>> template.name = 'Product' >>> template.default_uom = unit >>> template.type = 'goods' >>> template.list_price = Decimal('300') - >>> template.cost_price = Decimal('80') >>> template.cost_price_method = 'fifo' + >>> product, = template.products + >>> product.cost_price = Decimal('80') >>> template.save() - >>> product.template = template - >>> product.save() + >>> product, = template.products Get stock locations:: @@ -65,7 +64,7 @@ Make 4 units of the product available @ 10 :: Check Cost Price is 10:: >>> product.reload() - >>> product.template.cost_price + >>> product.cost_price Decimal('10.0000') Add 2 more units @ 25:: @@ -85,7 +84,7 @@ Add 2 more units @ 25:: Check Cost Price FIFO is 15:: >>> product.reload() - >>> product.template.cost_price + >>> product.cost_price Decimal('15.0000') Sell 3 units @ 50:: @@ -105,7 +104,7 @@ Sell 3 units @ 50:: Check Cost Price FIFO is 20:: >>> product.reload() - >>> product.template.cost_price + >>> product.cost_price Decimal('20.0000') Sell twice 1 more units @ 50:: @@ -142,5 +141,5 @@ Sell twice 1 more units @ 50:: Check Cost Price FIFO is 25:: >>> product.reload() - >>> product.template.cost_price + >>> product.cost_price Decimal('25.0000')