From 30e25dac81e18ea237df45c72f5527df74797158 Mon Sep 17 00:00:00 2001 From: agrabsi Date: Fri, 19 May 2023 14:32:42 +0000 Subject: [PATCH] [FIX] account: chart template upgrade MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Upgrading the chart template will point or create xmlid to existing records to avoid duplicate account codes. closes odoo/odoo#121967 X-original-commit: af1436482ebf04ac55a7947dfa4a3f789ce18afe Signed-off-by: William André (wan) --- addons/account/models/chart_template.py | 14 +++++++++++ addons/account/tests/test_chart_template.py | 26 +++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/addons/account/models/chart_template.py b/addons/account/models/chart_template.py index 0ec7c84ae97..8c25c15aea4 100644 --- a/addons/account/models/chart_template.py +++ b/addons/account/models/chart_template.py @@ -293,6 +293,20 @@ class AccountChartTemplate(models.AbstractModel): repartition_line.clear() if tags: repartition_line['tag_ids'] = tags + 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([ + ('code', '=', values['code']), + ('company_id', '=', company.id), + ]) + if existing_account: + self.env['ir.model.data']._update_xmlids([{ + 'xml_id': f"account.{company.id}_{xmlid}", + 'record': existing_account, + 'noupdate': True, + }]) if obsolete_xmlid: self.env['ir.model.data'].search([ diff --git a/addons/account/tests/test_chart_template.py b/addons/account/tests/test_chart_template.py index 925a8125f99..7e5966e3ff9 100644 --- a/addons/account/tests/test_chart_template.py +++ b/addons/account/tests/test_chart_template.py @@ -209,3 +209,29 @@ class TestChartTemplate(TransactionCase): tax_1_new = self.env['account.tax'].search([('company_id', '=', self.company_1.id), ('name', '=', "Tax 1")]) self.assertEqual(tax_1_old, tax_1_existing, "Old tax still exists but with a different name.") self.assertEqual(len(tax_1_new), 1, "New tax have been created with the original name.") + + def test_update_account_codes_conflict(self): + # Change code of an existing account to something else + standard_account = self.env['account.chart.template'].ref('test_account_income_template') + standard_account.code = '111111' + + # create a new account with the same code as an existing one + problematic_account = self.env['account.account'].create({ + 'code': '222221', + 'name': 'problematic_account', + 'company_id': self.company_1.id, + }) + + # remove an xmlid to see if it gets relinked and not duplicated + self.env['ir.model.data'].search([ + ('name', '=', f'{self.company_1.id}_test_account_expense_template'), + ('module', '=', 'account'), + ]).unlink() + + # reload chart template + with patch.object(AccountChartTemplate, '_get_chart_template_data', side_effect=test_get_data, autospec=True): + self.env['account.chart.template'].try_loading('test', company=self.company_1, install_demo=False) + + # check that xmlid is now pointing to problematic_account + xmlid_account = self.env.ref(f'account.{self.company_1.id}_test_account_income_template') + self.assertEqual(problematic_account, xmlid_account, "xmlid is not pointing to the right account")