changeset 617038b1be6a in modules/account_deposit:default
details: 
https://hg.tryton.org/modules/account_deposit?cmd=changeset;node=617038b1be6a
description:
        Add optional maximum to call_deposit method

        issue10125
        review316881002
diffstat:

 CHANGELOG  |   2 ++
 invoice.py |  20 ++++++++++++++------
 2 files changed, 16 insertions(+), 6 deletions(-)

diffs (64 lines):

diff -r 583667a3e6bb -r 617038b1be6a CHANGELOG
--- a/CHANGELOG Fri Mar 05 15:49:19 2021 +0100
+++ b/CHANGELOG Sat Mar 13 23:14:19 2021 +0100
@@ -1,3 +1,5 @@
+* Add optional maximum to call_deposit method
+
 Version 5.8.0 - 2020-11-02
 * Bug fixes (see mercurial logs for details)
 * Remove support for Python 3.5
diff -r 583667a3e6bb -r 617038b1be6a invoice.py
--- a/invoice.py        Fri Mar 05 15:49:19 2021 +0100
+++ b/invoice.py        Sat Mar 13 23:14:19 2021 +0100
@@ -1,5 +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 decimal import Decimal
+
 from trytond.i18n import gettext
 from trytond.pool import PoolMeta, Pool
 from trytond.model import ModelView, fields
@@ -32,7 +34,7 @@
     def recall_deposit(cls, invoices):
         pass
 
-    def call_deposit(self, account, description):
+    def call_deposit(self, account, description, maximum=None):
         pool = Pool()
         InvoiceLine = pool.get('account.invoice.line')
         Currency = pool.get('currency.currency')
@@ -40,14 +42,17 @@
         balance = self.party.get_deposit_balance(account)
         balance = Currency.compute(
             account.company.currency, balance, self.currency)
+        if maximum is None:
+            maximum = self.total_amount
+        total_amount = min(maximum, self.total_amount, key=abs)
 
-        amount = 0
+        amount = Decimal(0)
         if self.type.startswith('in'):
-            if balance > 0 and self.total_amount > 0:
-                amount = -min(balance, self.total_amount)
+            if balance > 0 and total_amount > 0:
+                amount = -min(balance, total_amount)
         else:
-            if balance < 0 and self.total_amount > 0:
-                amount = -min(-balance, self.total_amount)
+            if balance < 0 and total_amount > 0:
+                amount = -min(-balance, total_amount)
         to_delete = []
         for line in self.lines:
             if line.account == account:
@@ -61,8 +66,11 @@
             except ValueError:
                 pass
             line.save()
+        else:
+            amount = Decimal(0)
         if to_delete:
             InvoiceLine.delete(to_delete)
+        return amount
 
     def _get_deposit_recall_invoice_line(self, amount, account, description):
         pool = Pool()

Reply via email to