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
This commit is contained in:
Cédric Krier committed 2023-07-15 01:05:48 +02:00
1 parent 852741ae54
commit 2be6515fa6
1 file changed
+11 -10
+11 -10
View File
@@ -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(