[FIX] account: new line balance computation

Create a new Journal Entry
Add a 100$ debit line 1
Add a 100$ debit line 2
Add a new line: system will suggest 200$ credit line 3
Save
Delete line 3
Change line 2 amount into 50
Create a new line

Issue: the system will compute a credit amount of 50$
This occurs because in the onchange we work with a new record
representing the set of changes
Commands sent by the orm are:
- DELETE of line 3
- UPDATE of line 2
So in the new record we don't have information on line 1.
A solution is to recover the data from the stored record

opw-3757415

closes odoo/odoo#156875

Signed-off-by: de Wouters de Bouchout Jean-Benoît (jbw) <jbw@odoo.com>
This commit is contained in:
Andrea Grazioso (agr-odoo)
2024-03-14 08:06:22 +00:00
parent 58790d58e1
commit d7cfc3844c
+9 -2
View File
@@ -640,8 +640,15 @@ class AccountMoveLine(models.Model):
line.balance = False
elif not line.move_id.is_invoice(include_receipts=True):
# Only act as a default value when none of balance/debit/credit is specified
# balance is always the written field because of `_sanitize_vals`
line.balance = -sum((line.move_id.line_ids - line).mapped('balance'))
# balance is always the written field because of `_sanitize_vals`.
# Virtual record holds just the differences coming from the onchange
# so we need to recover balance of stored lines to calculate correctly the
# new line balance.
active_line_ids = [lid for lid in self.env.context.get('line_ids', []) if isinstance(lid, int)]
existing_lines = self.env['account.move.line'].browse(active_line_ids)
outdated_lines = line.move_id.line_ids._origin
new_lines = line.move_id.line_ids - line
line.balance = -sum((existing_lines - outdated_lines + new_lines).mapped('balance'))
else:
line.balance = 0