changeset 9df4acdef617 in modules/account_payment_clearing:default
details: 
https://hg.tryton.org/modules/account_payment_clearing?cmd=changeset&node=9df4acdef617
description:
        Reconcile payment clearing line with all statement lines

        issue11173
        review385851002
diffstat:

 CHANGELOG    |   1 +
 __init__.py  |   3 +++
 payment.py   |  53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 statement.py |  52 ++++++++++++++++++++++++++++++++++++++--------------
 4 files changed, 95 insertions(+), 14 deletions(-)

diffs (178 lines):

diff -r b79478a78f7a -r 9df4acdef617 CHANGELOG
--- a/CHANGELOG Wed Feb 23 00:01:17 2022 +0100
+++ b/CHANGELOG Sat Mar 19 22:16:29 2022 +0100
@@ -1,3 +1,4 @@
+* Reconcile payment clearing line with all statement lines
 * Use reference field for statement line relation
 * Add support for Python 3.10
 * Remove support for Python 3.6
diff -r b79478a78f7a -r 9df4acdef617 __init__.py
--- a/__init__.py       Wed Feb 23 00:01:17 2022 +0100
+++ b/__init__.py       Sat Mar 19 22:16:29 2022 +0100
@@ -9,11 +9,14 @@
     Pool.register(
         payment.Journal,
         payment.Payment,
+        payment.Group,
         payment.SucceedStart,
         account.Move,
         ir.Cron,
         module='account_payment_clearing', type_='model')
     Pool.register(
+        statement.Payment,
+        statement.PaymentGroup,
         statement.Statement,
         statement.StatementLine,
         module='account_payment_clearing', type_='model',
diff -r b79478a78f7a -r 9df4acdef617 payment.py
--- a/payment.py        Wed Feb 23 00:01:17 2022 +0100
+++ b/payment.py        Sat Mar 19 22:16:29 2022 +0100
@@ -192,6 +192,7 @@
                 if not sum(l.debit - l.credit for l in lines):
                     to_reconcile.append(lines)
         Line.reconcile(*to_reconcile)
+        cls.reconcile_clearing(payments)
 
     @property
     def clearing_account(self):
@@ -278,6 +279,34 @@
         super(Payment, cls).fail(payments)
 
     @classmethod
+    def reconcile_clearing(cls, payments):
+        pool = Pool()
+        MoveLine = pool.get('account.move.line')
+        Group = pool.get('account.payment.group')
+        to_reconcile = []
+        for payment in payments:
+            if not payment.clearing_move:
+                continue
+            clearing_account = payment.journal.clearing_account
+            if not clearing_account or not clearing_account.reconcile:
+                continue
+            lines = [l for l in payment.clearing_lines if not l.reconciliation]
+            if lines and not sum((l.debit - l.credit) for l in lines):
+                to_reconcile.append(lines)
+        if to_reconcile:
+            MoveLine.reconcile(*to_reconcile)
+        Group.reconcile_clearing(
+            Group.browse(list({p.group for p in payments if p.group})))
+
+    @property
+    def clearing_lines(self):
+        clearing_account = self.journal.clearing_account
+        if self.clearing_move:
+            for line in self.clearing_move.lines:
+                if line.account == clearing_account:
+                    yield line
+
+    @classmethod
     def copy(cls, payments, default=None):
         if default is None:
             default = {}
@@ -287,6 +316,30 @@
         return super(Payment, cls).copy(payments, default=default)
 
 
+class Group(metaclass=PoolMeta):
+    __name__ = 'account.payment.group'
+
+    @classmethod
+    def reconcile_clearing(cls, groups):
+        pool = Pool()
+        MoveLine = pool.get('account.move.line')
+        to_reconcile = []
+        for group in groups:
+            clearing_account = group.journal.clearing_account
+            if not clearing_account or not clearing_account.reconcile:
+                continue
+            lines = [l for l in group.clearing_lines if not l.reconciliation]
+            if lines and not sum((l.debit - l.credit) for l in lines):
+                to_reconcile.append(lines)
+        if to_reconcile:
+            MoveLine.reconcile(*to_reconcile)
+
+    @property
+    def clearing_lines(self):
+        for payment in self.payments:
+            yield from payment.clearing_lines
+
+
 class Succeed(Wizard):
     "Succeed Payment"
     __name__ = 'account.payment.succeed'
diff -r b79478a78f7a -r 9df4acdef617 statement.py
--- a/statement.py      Wed Feb 23 00:01:17 2022 +0100
+++ b/statement.py      Sat Mar 19 22:16:29 2022 +0100
@@ -8,13 +8,48 @@
 from trytond.transaction import Transaction
 
 
+class Payment(metaclass=PoolMeta):
+    __name__ = 'account.payment'
+
+    statement_lines = fields.One2Many(
+        'account.statement.line', 'related_to', "Statement Lines",
+        readonly=True)
+
+    @property
+    def clearing_lines(self):
+        clearing_account = self.journal.clearing_account
+        yield from super().clearing_lines
+        for statement_line in self.statement_lines:
+            if statement_line.move:
+                for line in statement_line.move.lines:
+                    if line.account == clearing_account:
+                        yield line
+
+
+class PaymentGroup(metaclass=PoolMeta):
+    __name__ = 'account.payment.group'
+
+    statement_lines = fields.One2Many(
+        'account.statement.line', 'related_to', "Statement Lines",
+        readonly=True)
+
+    @property
+    def clearing_lines(self):
+        clearing_account = self.journal.clearing_account
+        yield from super().clearing_lines
+        for statement_line in self.statement_lines:
+            if statement_line.move:
+                for line in statement_line.move.lines:
+                    if line.account == clearing_account:
+                        yield line
+
+
 class Statement(metaclass=PoolMeta):
     __name__ = 'account.statement'
 
     @classmethod
     def create_move(cls, statements):
         pool = Pool()
-        MoveLine = pool.get('account.move.line')
         Payment = pool.get('account.payment')
 
         moves = super(Statement, cls).create_move(statements)
@@ -46,19 +81,8 @@
                 with Transaction().set_context(clearing_date=date):
                     Payment.fail(Payment.browse(payments))
 
-        for move, statement, lines in moves:
-            assert len({l.payment for l in lines}) == 1
-            line = lines[0]
-            if line.payment and line.payment.clearing_move:
-                clearing_account = line.payment.journal.clearing_account
-                if clearing_account.reconcile:
-                    to_reconcile = []
-                    for line in move.lines + line.payment.clearing_move.lines:
-                        if (line.account == clearing_account
-                                and not line.reconciliation):
-                            to_reconcile.append(line)
-                    if not sum((l.debit - l.credit) for l in to_reconcile):
-                        MoveLine.reconcile(to_reconcile)
+        Payment.__queue__.reconcile_clearing(
+            list(set.union(*to_success.values(), *to_fail.values())))
         return moves
 
     def _group_key(self, line):

Reply via email to