[FIX] account: chart template upgrade

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) <wan@odoo.com>
This commit is contained in:
agrabsi
2023-05-22 23:18:22 +02:00
parent 47cd29f33c
commit 30e25dac81
2 changed files with 40 additions and 0 deletions
+14
View File
@@ -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([
@@ -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")