From 2be6515fa6d318b37ad3e69fe64785c886d87203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 15 Jul 2023 01:05:48 +0200 Subject: [PATCH] Start remainder with all lines for amount to pay invoice This is the worst case before finding a better one with a lower remainder. Closes #12403 --- modules/account_invoice/invoice.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/modules/account_invoice/invoice.py b/modules/account_invoice/invoice.py index aa47928826..55ba89f01e 100644 --- a/modules/account_invoice/invoice.py +++ b/modules/account_invoice/invoice.py @@ -1396,16 +1396,17 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin): if not l.reconciliation and (not self.account.party_required or l.party == party)] - best = Result([], self.total_amount) - for n in range(len(lines), 0, -1): - for comb_lines in combinations(lines, n): - remainder = sum(map(balance, comb_lines)) - remainder -= amount - result = Result(list(comb_lines), remainder) - if currency.is_zero(remainder): - return result - if abs(remainder) < abs(best.remainder): - best = result + remainder = sum(map(balance, lines)) - amount + best = Result(lines, remainder) + if remainder: + for n in range(len(lines) - 1, 0, -1): + for comb_lines in combinations(lines, n): + remainder = sum(map(balance, comb_lines)) - amount + result = Result(list(comb_lines), remainder) + if currency.is_zero(remainder): + return result + if abs(remainder) < abs(best.remainder): + best = result return best def pay_invoice(