[FIX] account: check duplicate accounts by normalized code

Function `_pre_reload_data` checks for the existance of accounts with the same
code to avoid creating a duplicate. It does so however comparing with the code
before normalizing it with the length from the template, failing to find
possible duplicates with a normalized code.

closes odoo/odoo#156498

X-original-commit: d60a6d25ad31d85fd3737b0a96ef9958fbed9c70
Signed-off-by: William André (wan) <wan@odoo.com>
This commit is contained in:
Agustín Castro Bugallo
2024-03-05 18:06:27 +00:00
committed by william-andre
parent 52c986ed19
commit 37b0ea801b
+9 -8
View File
@@ -307,18 +307,19 @@ class AccountChartTemplate(models.AbstractModel):
elif model_name == 'account.account':
# Point or create xmlid to existing record to avoid duplicate code
account = self.ref(xmlid, raise_if_not_found=False)
if not account or (account and account.code != values['code']):
existing_account = self.env['account.account'].search([
*self.env['account.account']._check_company_domain(company),
('code', '=', values['code']),
])
if existing_account:
normalized_code = f'{values["code"]:<0{int(template_data.get("code_digits", 6))}}'
if not account or not re.match(f'^{values["code"]}0*$', account.code):
query = self.env['account.account']._search(self.env['account.account']._check_company_domain(company))
query.add_where("account_account.code SIMILAR TO %s", [f'{values["code"]}0*'])
accounts = self.env['account.account'].browse(query)
account = accounts.sorted(key=lambda x: x.code != normalized_code)[0] if accounts else None
if account:
self.env['ir.model.data']._update_xmlids([{
'xml_id': f"account.{company.id}_{xmlid}",
'record': existing_account,
'record': 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']}