From 09ed117eb032b9b2620b3ad60d7f23697fd43c92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 2 Oct 2017 20:16:20 +0200 Subject: [PATCH] Add warning about different product between invoice line and stock move This allow to overcome the assert in update_anglo_saxon_quantity_product_cost. issue6803 review38801002 --- CHANGELOG | 2 ++ invoice.py | 26 +++++++++++++++++++++++++- stock.py | 3 +-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index b75483fd26..16c1ec4955 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Add warning about different product between invoice line and stock move + Version 4.4.0 - 2017-05-01 * Bug fixes (see mercurial logs for details) * Use stock_moves from account_invoice_stock diff --git a/invoice.py b/invoice.py index 8ae9e30326..696d2032df 100644 --- a/invoice.py +++ b/invoice.py @@ -11,6 +11,16 @@ class InvoiceLine: __metaclass__ = PoolMeta __name__ = 'account.invoice.line' + @classmethod + def __setup__(cls): + super(InvoiceLine, cls).__setup__() + cls._error_messages.update({ + 'stock_move_different_product': ( + "The invoice line '%(line)s' is linked to stock moves " + "for other products than '%(product)s'.\n" + "This may compute a wrong COGS."), + }) + def _get_anglo_saxon_move_lines(self, amount, type_): ''' Return account move for anglo-saxon stock accounting @@ -73,7 +83,21 @@ class InvoiceLine: return result # an empty list means we'll use the current cost price - moves = [move for move in self.stock_moves if move.state == 'done'] + moves = [] + for move in self.stock_moves: + if move.state != 'done': + continue + # remove move for different product + if move.product != self.product: + warning_name = '%s.stock.different_product' % self + self.raise_user_warning( + warning_name, 'stock_move_different_product', { + 'line': self.rec_name, + 'product': self.product.rec_name, + }) + else: + moves.append(move) + if self.invoice.type == 'in': type_ = 'in_supplier' elif self.invoice.type == 'out': diff --git a/stock.py b/stock.py index 7f883a83b3..8eab6eddb3 100644 --- a/stock.py +++ b/stock.py @@ -152,8 +152,7 @@ class Move: pool = Pool() Uom = pool.get('product.uom') - for move in moves: - assert move.product == product, 'wrong product' + assert all(m.product == product for m in moves), 'wrong product' assert type_.startswith('in_') or type_.startswith('out_'), \ 'wrong type'