From 41046ffda9686192c19717d8a4df58440daec5ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 13 Mar 2021 23:14:19 +0100 Subject: [PATCH] Add optional maximum to call_deposit method issue10125 review316881002 --- CHANGELOG | 2 ++ invoice.py | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 4c58a3363a..52911250d4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 --git a/invoice.py b/invoice.py index 7c7e4897fe..7b20a8a9fa 100644 --- a/invoice.py +++ b/invoice.py @@ -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 @@ class Invoice(metaclass=PoolMeta): 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 @@ class Invoice(metaclass=PoolMeta): 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 @@ class Invoice(metaclass=PoolMeta): 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()