[FIX] account: properly compute residual amounts during onchange

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) <ayh@odoo.com>
Signed-off-by: Tom De Caluwé (tdc) <tdc@odoo.com>
This commit is contained in:
Tom De Caluwé
2023-08-10 10:39:19 +02:00
parent f095b4c390
commit 03e100d2c5
+6 -3
View File
@@ -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)