From ebcde726f8b04cf3b4e65a554fae5660aa8e4785 Mon Sep 17 00:00:00 2001 From: "Pierrot (prro)" Date: Mon, 25 Sep 2023 14:10:39 +0200 Subject: [PATCH] [FIX] account: handle unsigned with signed tax tags MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a tax is created using an unsigned tax tags AND a signed tax tag, the mapper in the chart template model was crashing, skipping the needed mapping for the signed tax (the unsigned tax doesn't need to be mapped as it comes from a template). task-3520182 closes odoo/odoo#138840 Signed-off-by: Olivier Colson (oco) Signed-off-by: Pierre-Rodéric Roose (prro) --- addons/account/models/chart_template.py | 4 +- addons/account/tests/test_chart_template.py | 61 +++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/addons/account/models/chart_template.py b/addons/account/models/chart_template.py index 79d176c53f4..5aee23ac5e0 100644 --- a/addons/account/models/chart_template.py +++ b/addons/account/models/chart_template.py @@ -941,7 +941,7 @@ class AccountChartTemplate(models.AbstractModel): ('applicability', '=', 'taxes'), ('country_id', '=', self._get_chart_template_mapping()[template_code]['country_id']), ])} - return lambda *args: [tags[re.sub(r'\s+', ' ', x.strip())] for x in args] + return lambda *args: [tags[re.sub(r'\s+', ' ', x.strip())] if not re.match(r"^\w+\.\w+$", x) else x for x in args] def _deref_account_tags(self, template_code, tax_data): mapper = self._get_tag_mapper(template_code) @@ -950,7 +950,7 @@ class AccountChartTemplate(models.AbstractModel): if tax.get(fname): for _command, _id, repartition in tax[fname]: tags = repartition.get('tag_ids') - if isinstance(tags, str) and not re.match(r"^(\w+\.\w+,)*\w+\.\w+$", tags): + if isinstance(tags, str): repartition['tag_ids'] = [Command.set(mapper(*tags.split(TAX_TAG_DELIMITER)))] def _parse_csv(self, template_code, model, module=None): diff --git a/addons/account/tests/test_chart_template.py b/addons/account/tests/test_chart_template.py index 20b5e69a891..50984685cb2 100644 --- a/addons/account/tests/test_chart_template.py +++ b/addons/account/tests/test_chart_template.py @@ -146,6 +146,67 @@ class TestChartTemplate(TransactionCase): with patch.object(AccountChartTemplate, '_get_chart_template_data', side_effect=test_get_data, autospec=True): cls.env['account.chart.template'].try_loading('test', company=cls.company_1, install_demo=False) + def test_signed_and_unsigned_tags_tax(self): + tax_report = self.env['account.report'].create({ + 'name': "Tax report 1", + 'country_id': None, + 'column_ids': [ + Command.create({ + 'name': "Balance", + 'expression_label': 'balance', + }), + ], + }) + self.env['account.report.line'].create({ + 'name': "[SIGNED_TAG] Signed tag line", + 'report_id': tax_report.id, + 'sequence': max(tax_report.mapped('line_ids.sequence') or [0]) + 1, + 'expression_ids': [ + Command.create({ + 'label': 'balance', + 'engine': 'tax_tags', + 'formula': 'SIGNED_TAG', + }), + ], + }) + signed_tag = self.env['account.account.tag'].search([ + ('applicability', '=', 'taxes'), + ('name', '=', '+SIGNED_TAG'), + ]) + self.env['account.account.tag']._load_records([ + { + 'xml_id': 'account.unsigned_tax_tag', + 'noupdate': True, + 'values': { + 'name': "unsigned tax tag", + 'applicability': 'taxes', + }, + }, + ]) + tax_to_load = { + 'name': 'Mixed Tags Tax', + 'amount': 30, + 'amount_type': 'percent', + 'tax_group_id': 'tax_group_taxes', + 'active': True, + 'repartition_line_ids': [ + Command.create({'document_type': 'invoice', 'factor_percent': 100, 'repartition_type': 'base', 'tag_ids': 'account.unsigned_tax_tag||+SIGNED_TAG'}), + Command.create({'document_type': 'invoice', 'factor_percent': 100, 'repartition_type': 'tax'}), + Command.create({'document_type': 'refund', 'factor_percent': 100, 'repartition_type': 'base'}), + Command.create({'document_type': 'refund', 'factor_percent': 100, 'repartition_type': 'tax'}), + ] + } + self.env['account.chart.template']._deref_account_tags('test', {'tax1': tax_to_load}) + self.assertEqual( + tax_to_load['repartition_line_ids'][0], + Command.create({ + 'document_type': 'invoice', + 'factor_percent': 100, + 'repartition_type': 'base', + 'tag_ids': [Command.set(['account.unsigned_tax_tag', signed_tag.id])], + }) + ) + def test_update_taxes_creation(self): """ Tests that adding a new tax and a fiscal position tax creates new records when updating. """ def local_get_data(self, template_code):