From 1c5abe1b7d1ff8ec6a6ae65d9efa2a8536b9dd0d Mon Sep 17 00:00:00 2001 From: Julien Van Roy Date: Thu, 23 Mar 2023 11:36:25 +0000 Subject: [PATCH] [FIX] account_edi_ubl_cii: check validaty of taxes Invalid taxes could lead to invalid results from the `_prepare_edi_tax_details` fonction. For instance: a tax without tax repartition lines will be disregarded, which in turns leads to an invalid xml being generated. opw-3175701 closes odoo/odoo#116855 X-original-commit: 22b18a17348be6bd3982cbffd31da383442a6433 Signed-off-by: Nicolas Viseur (vin) Signed-off-by: Julien Van Roy --- .../account_edi_ubl_cii/i18n/account_edi_ubl_cii.pot | 6 ++++++ .../account_edi_ubl_cii/models/account_edi_common.py | 12 +++++++++++- .../models/account_edi_xml_cii_facturx.py | 3 +++ .../models/account_edi_xml_ubl_20.py | 3 +++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/addons/account_edi_ubl_cii/i18n/account_edi_ubl_cii.pot b/addons/account_edi_ubl_cii/i18n/account_edi_ubl_cii.pot index 016f2566e22..c9862476fc7 100644 --- a/addons/account_edi_ubl_cii/i18n/account_edi_ubl_cii.pot +++ b/addons/account_edi_ubl_cii/i18n/account_edi_ubl_cii.pot @@ -255,6 +255,12 @@ msgstr "" msgid "Report Action" msgstr "" +#. module: account_edi_ubl_cii +#: code:addons/account_edi_ubl_cii/models/account_edi_common.py:0 +#, python-format +msgid "Tax '%s' is invalid: %s" +msgstr "" + #. module: account_edi_ubl_cii #: model:ir.model,name:account_edi_ubl_cii.model_account_edi_xml_ubl_nl msgid "SI-UBL 2.0 (NLCIUS)" diff --git a/addons/account_edi_ubl_cii/models/account_edi_common.py b/addons/account_edi_ubl_cii/models/account_edi_common.py index fdd127b50f5..70e6242dc45 100644 --- a/addons/account_edi_ubl_cii/models/account_edi_common.py +++ b/addons/account_edi_ubl_cii/models/account_edi_common.py @@ -2,7 +2,7 @@ from odoo import _, models, Command from odoo.tools import float_repr -from odoo.exceptions import UserError +from odoo.exceptions import UserError, ValidationError from odoo.tools.float_utils import float_round from zeep import Client @@ -113,6 +113,16 @@ class AccountEdiCommon(models.AbstractModel): # TAXES # ------------------------------------------------------------------------- + def _validate_taxes(self, invoice): + """ Validate the structure of the tax repartition lines (invalid structure could lead to unexpected results) + """ + for tax in invoice.invoice_line_ids.tax_ids: + try: + tax._validate_repartition_lines() + except ValidationError as e: + error_msg = _("Tax '%s' is invalid: %s", tax.name, e.args[0]) # args[0] gives the error message + raise ValidationError(error_msg) + def _get_tax_unece_codes(self, invoice, tax): """ Source: doc of Peppol (but the CEF norm is also used by factur-x, yet not detailed) diff --git a/addons/account_edi_ubl_cii/models/account_edi_xml_cii_facturx.py b/addons/account_edi_ubl_cii/models/account_edi_xml_cii_facturx.py index 09b832b9c95..11671e21803 100644 --- a/addons/account_edi_ubl_cii/models/account_edi_xml_cii_facturx.py +++ b/addons/account_edi_ubl_cii/models/account_edi_xml_cii_facturx.py @@ -131,6 +131,9 @@ class AccountEdiXmlCII(models.AbstractModel): 'amount_type': tax.amount_type, } + # Validate the structure of the taxes + self._validate_taxes(invoice) + # Create file content. tax_details = invoice._prepare_edi_tax_details(grouping_key_generator=grouping_key_generator) diff --git a/addons/account_edi_ubl_cii/models/account_edi_xml_ubl_20.py b/addons/account_edi_ubl_cii/models/account_edi_xml_ubl_20.py index 1e9da992c4f..e8cda8b4e28 100644 --- a/addons/account_edi_ubl_cii/models/account_edi_xml_ubl_20.py +++ b/addons/account_edi_ubl_cii/models/account_edi_xml_ubl_20.py @@ -345,6 +345,9 @@ class AccountEdiXmlUBL20(models.AbstractModel): '_tax_category_vals_': tax_category_vals, } + # Validate the structure of the taxes + self._validate_taxes(invoice) + # Compute the tax details for the whole invoice and each invoice line separately. taxes_vals = invoice._prepare_edi_tax_details(grouping_key_generator=grouping_key_generator)