[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) <oco@odoo.com>
Signed-off-by: Claire Bretton <clbr@odoo.com>
This commit is contained in:
Claire Bretton (clbr)
2023-01-30 09:39:24 +01:00
parent 10f9460d2b
commit 280a0433fd
2 changed files with 18 additions and 0 deletions
+7
View File
@@ -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):
@@ -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.")