From d7cfc3844cbabb5d3877ef2c19e7855d0dca472c Mon Sep 17 00:00:00 2001 From: "Andrea Grazioso (agr-odoo)" Date: Thu, 7 Mar 2024 10:59:39 +0100 Subject: [PATCH] [FIX] account: new line balance computation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- addons/account/models/account_move_line.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/addons/account/models/account_move_line.py b/addons/account/models/account_move_line.py index bebceb0789d..b810e17cfed 100644 --- a/addons/account/models/account_move_line.py +++ b/addons/account/models/account_move_line.py @@ -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