From dbd1749154ba9312033736f2cf236418076fa8ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Fri, 27 Sep 2024 19:28:03 +0200 Subject: [PATCH] Exclude the move which defer the invoice line amount The move to defer amount must be created before creating the period move in case we need to compute the remainder amount on the first run. Closes #13547 (grafted from e4758cb22bff649346d546ff0645c165b4b3c2b8) --HG-- branch : 6.0 --- modules/account_invoice_defer/account.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/modules/account_invoice_defer/account.py b/modules/account_invoice_defer/account.py index 1f0cc47fda..516c869e67 100644 --- a/modules/account_invoice_defer/account.py +++ b/modules/account_invoice_defer/account.py @@ -205,10 +205,8 @@ class InvoiceDeferred(Workflow, ModelSQL, ModelView): Period.find(deferral.company.id, deferral.start_date) # Set state before create moves and defer amount to pass assert cls.write(deferrals, {'state': 'running'}) - cls.create_moves(deferrals) - # defer_amount is called after create_moves to be sure that - # create_moves call get_move with the invoice period if needed. - cls.defer_amount(deferrals) + deferred_moves = cls.defer_amount(deferrals) + cls.create_moves(deferrals, _exclude=deferred_moves) cls.close_try(deferrals) @classmethod @@ -249,12 +247,14 @@ class InvoiceDeferred(Workflow, ModelSQL, ModelView): moves.append(deferral.get_move()) Move.save(moves) Move.post(moves) + return moves @classmethod - def create_moves(cls, deferrals): + def create_moves(cls, deferrals, _exclude=None): pool = Pool() Period = pool.get('account.period') Move = pool.get('account.move') + exclude = set(_exclude or []) moves = [] for deferral in deferrals: assert deferral.state == 'running' @@ -265,7 +265,8 @@ class InvoiceDeferred(Workflow, ModelSQL, ModelView): ('end_date', '>=', deferral.start_date), ]) for period in sorted( - set(periods) - {m.period for m in deferral.moves}, + set(periods) - { + m.period for m in deferral.moves if m not in exclude}, key=lambda p: p.start_date): moves.append(deferral.get_move(period)) Move.save(moves)