# Account Invoice: Slow combinations when reconcile lines for amount

**URL:** https://discuss.tryton.org/t/account-invoice-slow-combinations-when-reconcile-lines-for-amount/4444
**Category:** Developer
**Tags:** accounting
**Created:** [July 22, 2021, 6:14am UTC](https://discuss.tryton.org/t/account-invoice-slow-combinations-when-reconcile-lines-for-amount/4444 "2021-07-22T06:14:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![resteve](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/resteve/32/823_2.png) [@resteve](https://discuss.tryton.org/u/resteve)
#### Post date: [July 22, 2021, 6:14am UTC](https://discuss.tryton.org/t/account-invoice-slow-combinations-when-reconcile-lines-for-amount/4444/1 "2021-07-22T06:14:11Z")

</div>

An invoice is related with a move that has 45 lines (account.move.line). Each line has same amount.

When try validate a stament, start do combinations [1] (from itertools). Start do 45, 44, 43, …41, 40… and not end.

**Proposal**

A- Set new variable to do MAX combinations and abort.  
B- iterate over combinations with a recursion, not using combinations() from itertools.  
C- …

[1] [account\_invoice/invoice.py at 5.4 · tryton/account\_invoice · GitHub](https://github.com/tryton/account_invoice/blob/5.4/invoice.py#L1216)

---

<div class="post-metadata">

### Author: ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)
#### Post date: [July 22, 2021, 6:30am UTC](https://discuss.tryton.org/t/account-invoice-slow-combinations-when-reconcile-lines-for-amount/4444/2 "2021-07-22T06:30:55Z")

</div>

> [@resteve](#):
>
> When try validate a stament,

I guess you mean when try validate a statement. But there is no reconciliation when validating a statement (it is triggered manualy by the user).

> [@resteve](#):
>
> An invoice is related with a move that has 45 lines (account.move.line). Each line has same amount.

So why not setting the invoice directly on the statement?

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [July 22, 2021, 6:50am UTC](https://discuss.tryton.org/t/account-invoice-slow-combinations-when-reconcile-lines-for-amount/4444/3 "2021-07-22T06:50:57Z")

</div>

The reconciliation is a [Knapsack problem - Wikipedia](https://en.wikipedia.org/wiki/Knapsack_problem) the current implementation is a naive brute force because we expected only small number of payment terms (certainly not 45 terms). Some heuristics exits that may find a solution faster.  
But I think the problem is that having a payment terms of 45 lines is the real problem. With a term per month, it spreads over almost 4 years. I do not know business that can take such risk.

> [@resteve](#):
>
> Set new variable to do MAX combinations and abort.

That can not be a solution because the problem will still be there: no reconciliation or payment.

> [@resteve](#):
>
> iterate over combinations with a recursion, not using combinations() from itertools.

I do not see how this could solve the problem. Indeed with recursion you will probably reach recursion limit.

Maybe an option would be to raise a warning when calling `Invoice.get_reconcile_lines_for_amount` with a list of lines to big (biggest than `reconciliation_chunk`). So the user will have to remove the invoice (or not use the pay wizard) and make the reconciliation manually (create the payment move manually).
