From 280a0433fd3c7a21dee8fe17c8271531881b156f Mon Sep 17 00:00:00 2001 From: "Claire Bretton (clbr)" Date: Fri, 20 Jan 2023 09:38:45 +0000 Subject: [PATCH] [FIX] account: fix tax update name collision Update of taxes could create name collision due to uniqueness of (name, company, scope, type) on taxes. We add "[old]" before the tax name to avoid this problem. closes odoo/odoo#111124 Related: #108571 X-original-commit: e50be0ed9d8ae32f85380b9239de7f3c93d71a93 Signed-off-by: Olivier Colson (oco) Signed-off-by: Claire Bretton --- addons/account/models/chart_template.py | 7 +++++++ addons/account/tests/test_chart_template.py | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/addons/account/models/chart_template.py b/addons/account/models/chart_template.py index 6810b94a3a6..7041865cbd4 100644 --- a/addons/account/models/chart_template.py +++ b/addons/account/models/chart_template.py @@ -43,12 +43,19 @@ def update_taxes_from_templates(cr, chart_template_xmlid): module, name = xml_id.split(".", 1) env['ir.model.data'].search([('module', '=', module), ('name', '=', name)]).unlink() + def _avoid_name_conflict(): + conflict_tax = env['account.tax'].search([('name', '=', template.name), ('company_id', '=', company.id), + ('type_tax_use', '=', template.type_tax_use), ('tax_scope', '=', template.tax_scope)]) + if conflict_tax: + conflict_tax.name = "[old] " + conflict_tax.name + template_vals = template._get_tax_vals_complete(company) chart_template = env["account.chart.template"].with_context(default_company_id=company.id) if old_tax: xml_id = old_tax.get_external_id().get(old_tax.id) if xml_id: _remove_xml_id(xml_id) + _avoid_name_conflict() chart_template.create_record_with_xmlid(company, template, "account.tax", template_vals) def _update_tax_from_template(template, tax): diff --git a/addons/account/tests/test_chart_template.py b/addons/account/tests/test_chart_template.py index ca47ffcbcbc..d0d59e62bb8 100644 --- a/addons/account/tests/test_chart_template.py +++ b/addons/account/tests/test_chart_template.py @@ -208,3 +208,14 @@ class TestChartTemplate(TransactionCase): # if only the fiscal position mapping has been removed, it won't be recreated self.assertEqual(len(fiscal_position.tax_ids), 0) + + def test_update_taxes_conflict_name(self): + chart_template_xml_id = self.chart_template.get_external_id()[self.chart_template.id] + template_vals = self.tax_1_template._get_tax_vals_complete(self.company_1) + template_vals['amount'] = 20 + self.chart_template.create_record_with_xmlid(self.company_1, self.tax_1_template, "account.tax", template_vals) + update_taxes_from_templates(self.env.cr, chart_template_xml_id) + tax_1_old = self.env['account.tax'].search([('company_id', '=', self.company_1.id), ('name', '=', "[old] " + self.tax_1_template.name)]) + tax_1_new = self.env['account.tax'].search([('company_id', '=', self.company_1.id), ('name', '=', self.tax_1_template.name)]) + self.assertEqual(len(tax_1_old), 1, "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.")