[FIX] account: avoid unneeded updates during chart data reload

During upgrade, l10n migration scripts that call `try_loading()` can cause an
exception _You cannot switch an account to prevent the reconciliation_ in
`_toggle_reconcile_to_false()` of model `account.account`.

This is caused by new chart template data containing a _False_ value for the
_reconcile_ field, while the already existing record has this field set to
_True_. Under this constellation, the code path leads into
`_toggle_reconcile_to_false()`, which checks for partial reconciliation and
throws the exception when the record is under partial reconciliation.
Also, the update of existing account.account data from the default data
triggers a lot of superfluous re-computes.

To avoid both the update of the reconcile field as well as other unnecessary
updates that trigger superfluous re-computes, filter the default data in the
update case to only update `tag_ids`. This is done after the code that fixes
XMLIDs to be sure about whether the record is being updated or created.

closes odoo/odoo#127099

X-original-commit: 2a8100e9ee8d49a957c1aab21a5a533a28dc2060
Signed-off-by: Carsten Wolff (cawo) <cawo@odoo.com>
Signed-off-by: William André (wan) <wan@odoo.com>
This commit is contained in:
Carsten Wolff (cawo)
2023-07-04 07:55:21 +02:00
parent 960eb211f0
commit 0778891cc9
+12 -1
View File
@@ -249,6 +249,7 @@ class AccountChartTemplate(models.AbstractModel):
)
obsolete_xmlid = set()
skip_update = set()
for model_name, records in data.items():
_fields = self.env[model_name]._fields
for xmlid, values in records.items():
@@ -313,6 +314,15 @@ class AccountChartTemplate(models.AbstractModel):
'record': existing_account,
'noupdate': True,
}])
account = existing_account
# on existing accounts, only tag_ids are to be updated using default data
if account and 'tag_ids' in data[model_name][xmlid]:
data[model_name][xmlid] = {'tag_ids': data[model_name][xmlid]['tag_ids']}
elif account:
skip_update.add((model_name, xmlid))
for skip_model, skip_xmlid in skip_update:
data[skip_model].pop(skip_xmlid, None)
if obsolete_xmlid:
self.env['ir.model.data'].search([
@@ -353,7 +363,8 @@ class AccountChartTemplate(models.AbstractModel):
# Normalize the code_digits of the accounts
code_digits = int(template_data.get('code_digits', 6))
for key, account_data in data.get('account.account', {}).items():
data['account.account'][key]['code'] = f'{account_data["code"]:<0{code_digits}}'
if 'code' in account_data:
data['account.account'][key]['code'] = f'{account_data["code"]:<0{code_digits}}'
for model in ('account.fiscal.position', 'account.reconcile.model'):
if model in data: