changeset 97121a9ce873 in modules/account_invoice_stock:default
details: 
https://hg.tryton.org/modules/account_invoice_stock?cmd=changeset;node=97121a9ce873
description:
        Limit links between invoice line and stock moves

        issue9143
        review274981002
diffstat:

 CHANGELOG  |   2 ++
 account.py |  27 ++++++++++++++++++++++-----
 stock.py   |  35 +++++++++++++++++++++++++++++++++--
 3 files changed, 57 insertions(+), 7 deletions(-)

diffs (104 lines):

diff -r 4539f4758d35 -r 97121a9ce873 CHANGELOG
--- a/CHANGELOG Thu Mar 26 00:11:06 2020 +0100
+++ b/CHANGELOG Sat Mar 28 16:16:34 2020 +0100
@@ -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 -r 4539f4758d35 -r 97121a9ce873 account.py
--- a/account.py        Thu Mar 26 00:11:06 2020 +0100
+++ b/account.py        Sat Mar 28 16:16:34 2020 +0100
@@ -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 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 -r 4539f4758d35 -r 97121a9ce873 stock.py
--- a/stock.py  Thu Mar 26 00:11:06 2020 +0100
+++ b/stock.py  Sat Mar 28 16:16:34 2020 +0100
@@ -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):

Reply via email to