diff --git a/CHANGELOG b/CHANGELOG index 4430aab7ce..48efd2592e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Limit links between invoice line and stock moves + Version 5.4.0 - 2019-11-04 * Bug fixes (see mercurial logs for details) diff --git a/account.py b/account.py index 1617196df5..a6acfd7b21 100644 --- a/account.py +++ b/account.py @@ -2,7 +2,7 @@ # this repository contains the full copyright notices and license terms. from trytond.model import ModelSQL, fields from trytond.pool import Pool, PoolMeta -from trytond.pyson import Eval +from trytond.pyson import Eval, If class InvoiceLineStockMove(ModelSQL): @@ -17,12 +17,29 @@ class InvoiceLineStockMove(ModelSQL): class InvoiceLine(metaclass=PoolMeta): __name__ = 'account.invoice.line' - stock_moves = fields.Many2Many('account.invoice.line-stock.move', - 'invoice_line', 'stock_move', 'Stock Moves', + stock_moves = fields.Many2Many( + 'account.invoice.line-stock.move', 'invoice_line', 'stock_move', + "Stock Moves", + domain=[ + ('product.default_uom_category', + '=', Eval('product_uom_category', -1)), + If((Eval('_parent_invoice', {}).get('type') == 'out') + | (Eval('invoice_type') == 'out'), + ['OR', + ('to_location.type', '=', 'customer'), + ('from_location.type', '=', 'customer'), + ], + ['OR', + ('from_location.type', '=', 'supplier'), + ('to_location.type', '=', 'supplier'), + ]), + ], states={ - 'invisible': Eval('type') != 'line', + 'invisible': ( + (Eval('type') != 'line') + | ~Eval('product')), }, - depends=['type']) + depends=['type', 'product_uom_category', 'invoice', 'invoice_type']) @property def moved_quantity(self): diff --git a/stock.py b/stock.py index da29e8ecc8..f6a4dd348c 100644 --- a/stock.py +++ b/stock.py @@ -3,12 +3,43 @@ from trytond.model import fields from trytond.pool import Pool, PoolMeta from trytond.transaction import Transaction +from trytond.pyson import Eval class Move(metaclass=PoolMeta): __name__ = 'stock.move' - invoice_lines = fields.Many2Many('account.invoice.line-stock.move', - 'stock_move', 'invoice_line', 'Invoice Lines') + invoice_lines = fields.Many2Many( + 'account.invoice.line-stock.move', 'stock_move', 'invoice_line', + "Invoice Lines", + domain=[ + ('product.default_uom_category', + '=', Eval('product_uom_category', -1)), + ['OR', + ('invoice.type', 'in', Eval('invoice_types', [])), + ('invoice_type', 'in', Eval('invoice_types', [])), + ], + ], + depends=['product_uom_category', 'invoice_types']) + invoice_types = fields.Function( + fields.MultiSelection('get_invoice_types', "Invoice Types"), + 'on_change_with_invoice_types') + + @classmethod + def get_invoice_types(cls): + pool = Pool() + Invoice = pool.get('account.invoice') + return Invoice.fields_get(['type'])['type']['selection'] + [(None, '')] + + @fields.depends('from_location', 'to_location') + def on_change_with_invoice_types(self, name=None): + types = set() + for location in [self.from_location, self.to_location]: + if location: + if location.type == 'customer': + types.add('out') + elif location.type == 'supplier': + types.add('in') + return list(types) @property def invoiced_quantity(self):