From 03e100d2c5ea5bfc7267757ae62f2eccf8524b4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20De=20Caluw=C3=A9?= Date: Mon, 7 Aug 2023 13:15:01 +0000 Subject: [PATCH] [FIX] account: properly compute residual amounts during onchange MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The _compute_amount_residual method computes residual amounts on reconcilable account move lines. As it uses an sql query for an efficient computation it discerns stored records from new records (having a new id) by filtering out records with a falsy id field (new ids are always falsy). It then proceeds the computation for stored records only. The bug arises when the compute method is called on records during an onchange call, as the records get reassigned a new id as well. This can be seen when opening the total due followup view and toggling the blocked field on any of the unreconciled entries. The problem is resolved by including records during an onchange in the computation: in practice the computation can simply be carried out on all _origin records. opw-3388389 closes odoo/odoo#131465 X-original-commit: 3d52fca2979cc4bfe87540122ea7cc979040be4f Related: odoo/enterprise#45591 Signed-off-by: Habib Ayob (ayh) Signed-off-by: Tom De Caluwé (tdc) --- addons/account/models/account_move_line.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/addons/account/models/account_move_line.py b/addons/account/models/account_move_line.py index be6ad0cb831..26c3340fed8 100644 --- a/addons/account/models/account_move_line.py +++ b/addons/account/models/account_move_line.py @@ -706,7 +706,10 @@ class AccountMoveLine(models.Model): for unreconciled lines, and something in-between for partially reconciled lines. """ need_residual_lines = self.filtered(lambda x: x.account_id.reconcile or x.account_id.account_type in ('asset_cash', 'liability_credit_card')) - stored_lines = need_residual_lines.filtered('id') + # Run the residual amount computation on all lines stored in the db. By + # using _origin, new records (with a NewId) are excluded and the + # computation works automagically for virtual onchange records as well. + stored_lines = need_residual_lines._origin if stored_lines: self.env['account.partial.reconcile'].flush_model() @@ -753,8 +756,8 @@ class AccountMoveLine(models.Model): foreign_curr = line.currency_id or comp_curr # Retrieve the amounts in both foreign/company currencies. If the record is 'new', the amounts_map is empty. - debit_amount, debit_amount_currency = amounts_map.get((line.id, 'debit'), (0.0, 0.0)) - credit_amount, credit_amount_currency = amounts_map.get((line.id, 'credit'), (0.0, 0.0)) + debit_amount, debit_amount_currency = amounts_map.get((line._origin.id, 'debit'), (0.0, 0.0)) + credit_amount, credit_amount_currency = amounts_map.get((line._origin.id, 'credit'), (0.0, 0.0)) # Subtract the values from the account.partial.reconcile to compute the residual amounts. line.amount_residual = comp_curr.round(line.balance - debit_amount + credit_amount)