changeset 20ec208741f4 in modules/account_statement:default
details: 
https://hg.tryton.org/modules/account_statement?cmd=changeset&node=20ec208741f4
description:
        Use reference field for statement line relation

        issue6854
        review367971002
diffstat:

 CHANGELOG                                    |    1 +
 statement.py                                 |  104 ++++++++++++++++++++------
 tests/scenario_account_statement.rst         |   22 ++--
 tests/scenario_statement_origin_invoices.rst |    4 +-
 view/statement_line_form.xml                 |    4 +-
 view/statement_line_tree.xml                 |    2 +-
 view/statement_line_tree_editable.xml        |    2 +-
 7 files changed, 96 insertions(+), 43 deletions(-)

diffs (369 lines):

diff -r ce77aff68339 -r 20ec208741f4 CHANGELOG
--- a/CHANGELOG Thu Feb 17 00:48:06 2022 +0100
+++ b/CHANGELOG Wed Feb 23 00:01:17 2022 +0100
@@ -1,3 +1,4 @@
+* Use reference field for statement line relation
 * Create default statement journal
 * Add support for Python 3.10
 * Remove support for Python 3.6
diff -r ce77aff68339 -r 20ec208741f4 statement.py
--- a/statement.py      Thu Feb 17 00:48:06 2022 +0100
+++ b/statement.py      Wed Feb 23 00:01:17 2022 +0100
@@ -7,6 +7,7 @@
 from sql import Null
 from sql.aggregate import Max, Sum
 from sql.conditionals import Coalesce
+from sql.operators import Concat
 
 from trytond.config import config
 from trytond.i18n import gettext
@@ -250,12 +251,12 @@
 
         invoices = set()
         for line in self.lines:
-            if (getattr(line, 'invoice', None)
+            if (line.invoice
                     and line.invoice.currency == self.company.currency):
                 invoices.add(line.invoice)
         for origin in self.origins:
             for line in origin.lines:
-                if (getattr(line, 'invoice', None)
+                if (line.invoice
                         and line.invoice.currency == self.company.currency):
                     invoices.add(line.invoice)
         invoice_id2amount_to_pay = {}
@@ -270,7 +271,8 @@
         for origin in origins:
             lines = list(origin.lines)
             for line in lines:
-                if (getattr(line, 'invoice', None) and line.id
+                if (line.invoice
+                        and line.id
                         and line.invoice.id in invoice_id2amount_to_pay):
                     amount_to_pay = invoice_id2amount_to_pay[line.invoice.id]
                     if (amount_to_pay
@@ -297,7 +299,7 @@
 
         invoices = set()
         for line in self.lines:
-            if (getattr(line, 'invoice', None)
+            if (line.invoice
                     and line.invoice.currency == self.company.currency):
                 invoices.add(line.invoice)
         invoice_id2amount_to_pay = {}
@@ -311,7 +313,7 @@
         lines = list(self.lines)
         line_offset = 0
         for index, line in enumerate(self.lines or []):
-            if getattr(line, 'invoice', None) and line.id:
+            if line.invoice and line.id:
                 if line.invoice.id not in invoice_id2amount_to_pay:
                     continue
                 amount_to_pay = invoice_id2amount_to_pay[line.invoice.id]
@@ -495,7 +497,7 @@
                     gettext('account_statement'
                         '.msg_statement_invoice_paid_cancelled'))
             Line.write(paid_cancelled_invoice_lines, {
-                    'invoice': None,
+                    'related_to': None,
                     })
 
         cls.create_move(statements)
@@ -504,9 +506,10 @@
                 'state': 'validated',
                 })
         common_lines = [l for l in Line.search([
-                ('statement.state', '=', 'draft'),
-                ('invoice.state', 'in', ['posted', 'paid']),
-                ])
+                    ('statement.state', '=', 'draft'),
+                    ('related_to.state', 'in', ['posted', 'paid'],
+                        'account.invoice'),
+                    ])
             if l.invoice.reconciled]
         if common_lines:
             warning_key = '_'.join(str(l.id) for l in common_lines)
@@ -515,7 +518,7 @@
                     gettext('account_statement'
                         '.msg_statement_paid_invoice_draft'))
             Line.write(common_lines, {
-                    'invoice': None,
+                    'related_to': None,
                     })
 
     @classmethod
@@ -757,15 +760,22 @@
             ('company', '=', Eval('company', -1)),
             ],
         depends=['company'])
-    invoice = fields.Many2One('account.invoice', 'Invoice',
-        domain=[
-            ('company', '=', Eval('company', -1)),
-            If(Bool(Eval('party')), [('party', '=', Eval('party'))], []),
-            If(Bool(Eval('account')), [('account', '=', Eval('account'))], []),
-            If(Eval('statement_state') == 'draft',
-                ('state', '=', 'posted'),
-                ('state', '!=', '')),
-            ],
+    related_to = fields.Reference(
+        "Related To", 'get_relations',
+        domain={
+            'account.invoice': [
+                ('company', '=', Eval('company', -1)),
+                If(Bool(Eval('party')),
+                    ('party', '=', Eval('party')),
+                    ()),
+                If(Bool(Eval('account')),
+                    ('account', '=', Eval('account')),
+                    ()),
+                If(Eval('statement_state') == 'draft',
+                    ('state', '=', 'posted'),
+                    ('state', '!=', '')),
+                ],
+            },
         states=_states,
         context={'with_payment': False},
         depends=['company', 'party', 'account'] + _depends)
@@ -796,11 +806,51 @@
                 'account_statement.msg_line_amount_non_zero'),
             ]
 
+    @classmethod
+    def __register__(cls, module):
+        table = cls.__table__()
+
+        super().__register__(module)
+
+        table_h = cls.__table_handler__(module)
+        cursor = Transaction().connection.cursor()
+        if table_h.column_exist('invoice'):
+            cursor.execute(*table.update(
+                    [table.related_to],
+                    [Concat('account.invoice,', table.invoice)],
+                    where=table.invoice != Null))
+            table_h.drop_column('invoice')
+
+    @property
+    @fields.depends('related_to')
+    def invoice(self):
+        pool = Pool()
+        Invoice = pool.get('account.invoice')
+        related_to = getattr(self, 'related_to', None)
+        if isinstance(related_to, Invoice) and related_to.id >= 0:
+            return related_to
+
+    @invoice.setter
+    def invoice(self, value):
+        self.related_to = value
+
     @staticmethod
     def default_amount():
         return Decimal(0)
 
-    @fields.depends('amount', 'party', 'invoice', 'date')
+    @classmethod
+    def _get_relations(cls):
+        "Return a list of Model names for related_to Reference"
+        return ['account.invoice']
+
+    @classmethod
+    def get_relations(cls):
+        Model = Pool().get('ir.model')
+        get_name = Model.get_name
+        models = cls._get_relations()
+        return [(None, '')] + [(m, get_name(m)) for m in models]
+
+    @fields.depends('amount', 'party', 'date', methods=['invoice'])
     def on_change_party(self):
         if self.party:
             if self.amount:
@@ -817,8 +867,10 @@
             else:
                 self.invoice = None
 
-    @fields.depends('amount', 'party', 'account', 'invoice', 'statement',
-        'date', '_parent_statement.journal')
+    @fields.depends(
+        'amount', 'party', 'account', 'date',
+        'statement', '_parent_statement.journal',
+        methods=['invoice'])
     def on_change_amount(self):
         Currency = Pool().get('currency.currency')
         if self.party:
@@ -845,7 +897,7 @@
             else:
                 self.invoice = None
 
-    @fields.depends('account', 'invoice')
+    @fields.depends('account', methods=['invoice'])
     def on_change_account(self):
         if self.invoice:
             if self.account:
@@ -854,8 +906,8 @@
             else:
                 self.invoice = None
 
-    @fields.depends('party', 'account', 'invoice')
-    def on_change_invoice(self):
+    @fields.depends('party', 'account', methods=['invoice'])
+    def on_change_related_to(self):
         if self.invoice:
             if not self.party:
                 self.party = self.invoice.party
@@ -915,7 +967,7 @@
         else:
             default = default.copy()
         default.setdefault('move', None)
-        default.setdefault('invoice', None)
+        default.setdefault('related_to', None)
         return super(Line, cls).copy(lines, default=default)
 
     @classmethod
diff -r ce77aff68339 -r 20ec208741f4 tests/scenario_account_statement.rst
--- a/tests/scenario_account_statement.rst      Thu Feb 17 00:48:06 2022 +0100
+++ b/tests/scenario_account_statement.rst      Wed Feb 23 00:01:17 2022 +0100
@@ -143,7 +143,7 @@
     >>> statement_line.party = customer
     >>> statement_line.account == receivable
     True
-    >>> statement_line.invoice = customer_invoice1
+    >>> statement_line.related_to = customer_invoice1
     >>> statement_line.amount
     Decimal('100.00')
     >>> statement_line = statement.lines[-1]
@@ -158,7 +158,7 @@
     >>> statement_line.account == receivable
     True
     >>> statement_line.description = 'other description'
-    >>> statement_line.invoice = customer_invoice2
+    >>> statement_line.related_to = customer_invoice2
     >>> statement_line.amount
     Decimal('80.00')
 
@@ -172,7 +172,7 @@
     >>> statement_line.amount = Decimal('-50')
     >>> statement_line.party = customer
     >>> statement_line.account = receivable
-    >>> statement_line.invoice = customer_credit_note
+    >>> statement_line.related_to = customer_credit_note
 
 Paid 50 to supplier::
 
@@ -183,7 +183,7 @@
     >>> statement_line.party = supplier
     >>> statement_line.account == payable
     True
-    >>> statement_line.invoice = supplier_invoice
+    >>> statement_line.related_to = supplier_invoice
     >>> statement_line.amount
     Decimal('-50.00')
     >>> statement_line = statement.lines.pop()
@@ -302,8 +302,8 @@
     >>> statement_line.party = customer
     >>> statement_line.account = receivable
     >>> statement_line.amount = Decimal(-120)
-    >>> statement_line.invoice = customer_invoice3
-    >>> statement_line.invoice.id == customer_invoice3.id
+    >>> statement_line.related_to = customer_invoice3
+    >>> statement_line.related_to.id == customer_invoice3.id
     True
 
     >>> statement_line = StatementLine()
@@ -312,7 +312,7 @@
     >>> statement_line.party = supplier
     >>> statement_line.account = payable
     >>> statement_line.amount = Decimal(50)
-    >>> statement_line.invoice = supplier_invoice2
+    >>> statement_line.related_to = supplier_invoice2
     >>> statement_line.amount
     Decimal('40.00')
     >>> len(statement.lines)
@@ -341,7 +341,7 @@
     >>> statement_line.party = customer
     >>> statement_line.account = receivable
     >>> statement_line.amount = Decimal(300)
-    >>> statement_line.invoice = customer_invoice4
+    >>> statement_line.related_to = customer_invoice4
     >>> statement1.save()
 
     >>> statement2 = Statement(name='2', journal=statement_journal)
@@ -351,7 +351,7 @@
     >>> statement_line.party = customer
     >>> statement_line.account = receivable
     >>> statement_line.amount = Decimal(300)
-    >>> statement_line.invoice = customer_invoice4
+    >>> statement_line.related_to = customer_invoice4
     >>> statement2.save()
 
     >>> statement1.click('validate_statement') # doctest: 
+IGNORE_EXCEPTION_DETAIL
@@ -366,10 +366,10 @@
     'validated'
 
     >>> statement1.reload()
-    >>> bool(statement1.lines[0].invoice)
+    >>> bool(statement1.lines[0].related_to)
     True
     >>> statement2.reload()
-    >>> bool(statement2.lines[0].invoice)
+    >>> bool(statement2.lines[0].related_to)
     False
 
 Testing balance validation::
diff -r ce77aff68339 -r 20ec208741f4 
tests/scenario_statement_origin_invoices.rst
--- a/tests/scenario_statement_origin_invoices.rst      Thu Feb 17 00:48:06 
2022 +0100
+++ b/tests/scenario_statement_origin_invoices.rst      Wed Feb 23 00:01:17 
2022 +0100
@@ -95,7 +95,7 @@
 
     >>> origin, = statement.origins
     >>> line = origin.lines.new()
-    >>> line.invoice = customer_invoice1
+    >>> line.related_to = customer_invoice1
     >>> line.amount
     Decimal('100.00')
     >>> line.party == customer
@@ -105,7 +105,7 @@
     >>> origin.pending_amount
     Decimal('80.00')
     >>> line = origin.lines.new()
-    >>> line.invoice = customer_invoice2
+    >>> line.related_to = customer_invoice2
     >>> line.amount
     Decimal('80.00')
     >>> line.party == customer
diff -r ce77aff68339 -r 20ec208741f4 view/statement_line_form.xml
--- a/view/statement_line_form.xml      Thu Feb 17 00:48:06 2022 +0100
+++ b/view/statement_line_form.xml      Wed Feb 23 00:01:17 2022 +0100
@@ -16,8 +16,8 @@
     <field name="party"/>
     <label name="account"/>
     <field name="account"/>
-    <label name="invoice"/>
-    <field name="invoice"/>
+    <label name="related_to"/>
+    <field name="related_to"/>
     <newline/>
     <label name="description"/>
     <field name="description" colspan="3"/>
diff -r ce77aff68339 -r 20ec208741f4 view/statement_line_tree.xml
--- a/view/statement_line_tree.xml      Thu Feb 17 00:48:06 2022 +0100
+++ b/view/statement_line_tree.xml      Wed Feb 23 00:01:17 2022 +0100
@@ -8,6 +8,6 @@
     <field name="amount"/>
     <field name="party" expand="1"/>
     <field name="account" expand="1"/>
-    <field name="invoice" expand="1"/>
+    <field name="related_to" expand="1"/>
     <field name="description" expand="1"/>
 </tree>
diff -r ce77aff68339 -r 20ec208741f4 view/statement_line_tree_editable.xml
--- a/view/statement_line_tree_editable.xml     Thu Feb 17 00:48:06 2022 +0100
+++ b/view/statement_line_tree_editable.xml     Wed Feb 23 00:01:17 2022 +0100
@@ -7,7 +7,7 @@
     <field name="date"/>
     <field name="amount" sum="Amount"/>
     <field name="party" expand="1"/>
-    <field name="invoice" expand="1"/>
+    <field name="related_to" expand="1"/>
     <field name="account" expand="1"/>
     <field name="description" expand="1"/>
 </tree>

Reply via email to