From 466d0188dbc6b7efeaa7d7afea49517db3d2062b Mon Sep 17 00:00:00 2001 From: "Anh Thao Pham (pta)" Date: Mon, 30 Oct 2023 12:46:40 +0000 Subject: [PATCH] [FIX] account: fix foreign taxes creation with group-type taxes Steps to reproduce: - Install Accounting - Create a fiscal position for a foreign country: * Country: [Any country that has group-type taxes] (e.g. Italy) * Foreign Tax ID: [any tax ID for that country] (e.g. IT12345670017) - Click on the "here" button appearing in the banner (Click "here" to create the taxes for this country.) => A ValueError is raised. "External ID not found in the system: account.1_22vsp" Cause: During the creation of the taxes, the xml_id of each tax is prefixed with the chart template code to avoid collision. However, when linking a group-type tax to its children taxes, it cannot find them because they are prefixed but the group-type tax is using their unprefixed value. opw-3518213 closes odoo/odoo#141448 X-original-commit: 3c22a2d4766e2401e25f2fe8eb0aadc0e9f77524 Signed-off-by: Brice Bartoletti (bib) Signed-off-by: Anh Thao Pham (pta) --- addons/account/models/chart_template.py | 7 +++++ addons/account/tests/test_multivat.py | 35 +++++++++++++++++++++---- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/addons/account/models/chart_template.py b/addons/account/models/chart_template.py index 564e8328e19..a69b991b853 100644 --- a/addons/account/models/chart_template.py +++ b/addons/account/models/chart_template.py @@ -812,6 +812,13 @@ class AccountChartTemplate(models.AbstractModel): } for model, templates in data.items() } + # add the prefix to the "children_tax_ids" value for group-type taxes + for tax_data in data['account.tax'].values(): + if tax_data.get('amount_type') == 'group': + children_taxes = tax_data['children_tax_ids'].split(',') + for idx, child_tax in enumerate(children_taxes): + children_taxes[idx] = f"{chart_template_code}_{child_tax}" + tax_data['children_tax_ids'] = ','.join(children_taxes) self._load_data(data) # -------------------------------------------------------------------------------- diff --git a/addons/account/tests/test_multivat.py b/addons/account/tests/test_multivat.py index e2f3a460709..4651b5e0ebe 100644 --- a/addons/account/tests/test_multivat.py +++ b/addons/account/tests/test_multivat.py @@ -98,11 +98,20 @@ def data_method_provider(chart_template_name, country_code): }, }, 'account.tax': { - xmlid: _tax_vals(name, amount, external_id_prefix) - for name, xmlid, amount in ( - ('Tax 1', 'test_tax_1_template', 15), - ('Tax 2', 'test_tax_2_template', 0), - ) + **{ + xmlid: _tax_vals(name, amount, external_id_prefix) + for name, xmlid, amount in ( + ('Tax 1', 'test_tax_1_template', 15), + ('Tax 2', 'test_tax_2_template', 0), + ) + }, + 'test_composite_tax_template': { + 'name': 'Tax Grouped', + 'amount_type': 'group', + 'type_tax_use': 'purchase', + 'tax_group_id': 'tax_group_taxes', + 'children_tax_ids': 'test_tax_1_template,test_tax_2_template', + } }, } return test_data_getter @@ -175,8 +184,10 @@ class TestMultiVAT(TransactionCase): # tax 'test_tax_1_template', 'test_tax_2_template', + 'test_composite_tax_template', 'foreign_test_tax_1_template', 'foreign_test_tax_2_template', + 'foreign_test_composite_tax_template' ): with self.subTest(xml_id=xml_id): record = self.env["account.chart.template"].ref(xml_id, raise_if_not_found=False) @@ -223,3 +234,17 @@ class TestMultiVAT(TransactionCase): _base_line, tax_line = tax.invoice_repartition_line_ids self.assertEqual(tax_line.account_id.code, '411001', "The previously created tax account should be reused for similar tax") + + def test_children_taxes(self): + # Ensure that group-type taxes are correctly linked to their children + composite_taxes = ['test_composite_tax_template', 'foreign_test_composite_tax_template'] + children_taxes = { + 'test_composite_tax_template': ['test_tax_1_template', 'test_tax_2_template'], + 'foreign_test_composite_tax_template': ['foreign_test_tax_1_template', 'foreign_test_tax_2_template'], + } + for xml_id in composite_taxes: + with self.subTest(xml_id=xml_id): + record = self.env["account.chart.template"].ref(xml_id, raise_if_not_found=False) + for i, child in enumerate(record.children_tax_ids): + child_tax = self.env["account.chart.template"].ref(children_taxes[xml_id][i], raise_if_not_found=False) + self.assertEqual(child.id, child_tax.id)