details:   https://code.tryton.org/tryton/commit/36819c39122c
branch:    default
user:      Cédric Krier <[email protected]>
date:      Fri Aug 15 09:11:10 2025 +0200
description:
        Add support for refunding payments on sales

        Closes #14166
diffstat:

 modules/sale_payment/CHANGELOG |   1 +
 modules/sale_payment/sale.py   |  17 +++++++++++------
 2 files changed, 12 insertions(+), 6 deletions(-)

diffs (51 lines):

diff -r aa3f264dc2b2 -r 36819c39122c modules/sale_payment/CHANGELOG
--- a/modules/sale_payment/CHANGELOG    Sat Aug 16 14:10:56 2025 +0200
+++ b/modules/sale_payment/CHANGELOG    Fri Aug 15 09:11:10 2025 +0200
@@ -1,3 +1,4 @@
+* Add support for refunding payments on sales
 
 Version 7.6.0 - 2025-04-28
 --------------------------
diff -r aa3f264dc2b2 -r 36819c39122c modules/sale_payment/sale.py
--- a/modules/sale_payment/sale.py      Sat Aug 16 14:10:56 2025 +0200
+++ b/modules/sale_payment/sale.py      Fri Aug 15 09:11:10 2025 +0200
@@ -28,10 +28,6 @@
         'account.payment', 'origin', "Payments",
         domain=[
             ('company', '=', Eval('company', -1)),
-            If(Eval('total_amount', 0) >= 0,
-                ('kind', '=', 'receivable'),
-                ('kind', '=', 'payable'),
-                ),
             ['OR',
                 ('party', '=', If(Bool(Eval('invoice_party')),
                         Eval('invoice_party', -1), Eval('party', -1))),
@@ -69,7 +65,10 @@
     @property
     def payment_amount_authorized(self):
         "Total amount of the authorized payments"
-        return sum(p.amount for p in self.payments if p.is_authorized)
+        return (sum(
+                p.amount for p in self.payments
+                if p.kind == 'receivable' and p.is_authorized)
+            - sum(p.amount for p in self.payments if p.kind == 'payable'))
 
     @property
     def amount_to_pay(self):
@@ -86,9 +85,15 @@
                     ('payments', '!=', None),
                     ('company', '=', context.get('company')),
                     ])
+
+        def cover(authorized, amount):
+            return (
+                abs(authorized) >= abs(amount)
+                and (authorized * amount >= 0))
+
         to_confirm = []
         for sale in sales:
-            if sale.payment_amount_authorized >= sale.amount_to_pay:
+            if cover(sale.payment_amount_authorized, sale.amount_to_pay):
                 to_confirm.append(sale)
         if to_confirm:
             to_confirm = cls.browse(to_confirm)  # optimize cache

Reply via email to