changeset 1b4c816e37ae in modules/account_payment_clearing:default
details: 
https://hg.tryton.org/modules/account_payment_clearing?cmd=changeset;node=1b4c816e37ae
description:
        Allow going back to processing state

        This allows to avoid to use the failed state when updating a succeeded 
payment
        as specific workflow may be plugged to the fail transition.

        issue9525
        review302001002
diffstat:

 payment.py |  89 ++++++++++++++++++++++++++++++++++++-------------------------
 1 files changed, 52 insertions(+), 37 deletions(-)

diffs (118 lines):

diff -r f12cdce20a8b -r 1b4c816e37ae payment.py
--- a/payment.py        Thu Jul 09 10:21:06 2020 +0100
+++ b/payment.py        Sat Aug 29 18:17:51 2020 +0200
@@ -1,6 +1,7 @@
 # This file is part of Tryton.  The COPYRIGHT file at the top level of
 # this repository contains the full copyright notices and license terms.
 from collections import defaultdict
+from functools import wraps
 
 from trytond.pool import PoolMeta, Pool
 from trytond.model import ModelView, Workflow, fields
@@ -54,6 +55,50 @@
         Move.post(moves)
 
 
+def cancel_clearing_move(func):
+    @wraps(func)
+    def wrapper(cls, payments, *args, **kwargs):
+        pool = Pool()
+        Move = pool.get('account.move')
+        Line = pool.get('account.move.line')
+        Reconciliation = pool.get('account.move.reconciliation')
+
+        func(cls, payments, *args, **kwargs)
+
+        to_delete = []
+        to_reconcile = defaultdict(lambda: defaultdict(list))
+        to_unreconcile = []
+        for payment in payments:
+            if payment.clearing_move:
+                if payment.clearing_move.state == 'draft':
+                    to_delete.append(payment.clearing_move)
+                    for line in payment.clearing_move.lines:
+                        if line.reconciliation:
+                            to_unreconcile.append(line.reconciliation)
+                else:
+                    cancel_move = payment.clearing_move.cancel()
+                    for line in (payment.clearing_move.lines
+                            + cancel_move.lines):
+                        if line.reconciliation:
+                            to_unreconcile.append(line.reconciliation)
+                        if line.account.reconcile:
+                            to_reconcile[payment.party][line.account].append(
+                                line)
+
+        # Remove clearing_move before delete
+        # in case reconciliation triggers use it.
+        cls.write(payments, {'clearing_move': None})
+
+        if to_unreconcile:
+            Reconciliation.delete(to_unreconcile)
+        if to_delete:
+            Move.delete(to_delete)
+        for party in to_reconcile:
+            for lines in to_reconcile[party].values():
+                Line.reconcile(lines)
+    return wrapper
+
+
 class Payment(metaclass=PoolMeta):
     __name__ = 'account.payment'
     account = fields.Many2One(
@@ -200,48 +245,18 @@
         return move
 
     @classmethod
+    @Workflow.transition('processing')
+    @cancel_clearing_move
+    def proceed(cls, payments):
+        super().proceed(payments)
+
+    @classmethod
     @ModelView.button
     @Workflow.transition('failed')
+    @cancel_clearing_move
     def fail(cls, payments):
-        pool = Pool()
-        Move = pool.get('account.move')
-        Line = pool.get('account.move.line')
-        Reconciliation = pool.get('account.move.reconciliation')
-
         super(Payment, cls).fail(payments)
 
-        to_delete = []
-        to_reconcile = defaultdict(lambda: defaultdict(list))
-        to_unreconcile = []
-        for payment in payments:
-            if payment.clearing_move:
-                if payment.clearing_move.state == 'draft':
-                    to_delete.append(payment.clearing_move)
-                    for line in payment.clearing_move.lines:
-                        if line.reconciliation:
-                            to_unreconcile.append(line.reconciliation)
-                else:
-                    cancel_move = payment.clearing_move.cancel()
-                    for line in (payment.clearing_move.lines
-                            + cancel_move.lines):
-                        if line.reconciliation:
-                            to_unreconcile.append(line.reconciliation)
-                        if line.account.reconcile:
-                            to_reconcile[payment.party][line.account].append(
-                                line)
-
-        # Remove clearing_move before delete in case reconciliation triggers
-        # would use it.
-        cls.write(payments, {'clearing_move': None})
-
-        if to_unreconcile:
-            Reconciliation.delete(to_unreconcile)
-        if to_delete:
-            Move.delete(to_delete)
-        for party in to_reconcile:
-            for lines in to_reconcile[party].values():
-                Line.reconcile(lines)
-
     @classmethod
     def copy(cls, payments, default=None):
         if default is None:

Reply via email to