From da329fc7c030bcb4f6066f39f671e423db2540b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 28 Jul 2021 00:35:24 +0200 Subject: [PATCH] Use domain method to select incoming and outgoing move to recompute cost price issue10586 review369311002 (grafted from 61b18217c99bf7195583345fc0f4b84d3d115e89) --HG-- branch : 6.0 --- product.py | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/product.py b/product.py index 0e386506cc..ae1fccdb94 100644 --- a/product.py +++ b/product.py @@ -299,6 +299,22 @@ class Product(StockMixin, object, metaclass=PoolMeta): ('state', '=', 'done'), ] + @classmethod + def _domain_in_moves_cost(cls): + "Return the domain for incoming moves in cost computation" + return [ + ('to_location.type', '=', 'storage'), + ('from_location.type', '!=', 'storage'), + ] + + @classmethod + def _domain_out_moves_cost(cls): + "Return the domain for outgoing moves in cost computation" + return [ + ('from_location.type', '=', 'storage'), + ('to_location.type', '!=', 'storage'), + ] + @classmethod def _domain_storage_quantity(cls): "Returns the domain for locations to use in cost computation" @@ -329,20 +345,22 @@ class Product(StockMixin, object, metaclass=PoolMeta): ('product', '=', self.id), self._domain_moves_cost(), ['OR', - [ - ('to_location.type', '=', 'storage'), - ('from_location.type', '!=', 'storage'), - ], [ - ('from_location.type', '=', 'storage'), - ('to_location.type', '!=', 'storage'), - ], - ], + self._domain_in_moves_cost(), + self._domain_out_moves_cost(), + ] ] if start: domain.append(('effective_date', '>=', start)) moves = Move.search( domain, order=[('effective_date', 'ASC'), ('id', 'ASC')]) + _in_moves = Move.search([ + ('product', '=', self.id), + self._domain_moves_cost(), + self._domain_in_moves_cost(), + ], order=[]) + _in_moves = set(m.id for m in _in_moves) + revisions = Revision.get_for_product(self) cost_price = Decimal(0) @@ -350,8 +368,7 @@ class Product(StockMixin, object, metaclass=PoolMeta): if start: domain.remove(('effective_date', '>=', start)) domain.append(('effective_date', '<', start)) - domain.append( - ('from_location.type', 'in', ['supplier', 'production'])) + domain.append(self._domain_in_moves_cost()) prev_moves = Move.search( domain, order=[('effective_date', 'DESC'), ('id', 'DESC')], @@ -363,6 +380,12 @@ class Product(StockMixin, object, metaclass=PoolMeta): date=start - datetime.timedelta(days=1)) quantity = Decimal(str(quantity)) + def in_move(move): + return move.id in _in_moves + + def out_move(move): + return not in_move(move) + current_moves = [] current_cost_price = cost_price for move in moves: @@ -380,10 +403,9 @@ class Product(StockMixin, object, metaclass=PoolMeta): 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': + if out_move(move): qty *= -1 - if (move.from_location.type in ['supplier', 'production'] - or move.to_location.type == 'supplier'): + if in_move(move): with Transaction().set_context(date=move.effective_date): unit_price = Currency.compute( move.currency, move.unit_price,