From 37b0ea801b1d01342b5b4db5b7373d2f034a8703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20Castro=20Bugallo?= Date: Fri, 23 Feb 2024 14:19:40 +0000 Subject: [PATCH] [FIX] account: check duplicate accounts by normalized code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- addons/account/models/chart_template.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/addons/account/models/chart_template.py b/addons/account/models/chart_template.py index f09a147308b..4bec1fbcbf5 100644 --- a/addons/account/models/chart_template.py +++ b/addons/account/models/chart_template.py @@ -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']}