From 3810ce75a078fef8ee16e663b9e3fb2e7c65878d Mon Sep 17 00:00:00 2001 From: paso-odoo Date: Thu, 22 Jun 2023 09:21:26 +0000 Subject: [PATCH] [FIX] l10n_es_edi_facturae: certificates deleted for facturae EDI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the user deletes the certificate of facturae EDI invoices on Spain and tries to send an invoice from the `Send & Print`, button error will be produced says, the local variable 'xml_content' referenced before the assignment. Steps to produce: - Install the 'l10n_es_edi_facturae' module and change the company to ES Company. - Accounting > Configuration > Spain Facturae EDI > Certificates. - Delete all the certificate(s). - Now create one Invoice > Confirm > Send & Print > Click on `Send & Print` a button from the wizard. - Error will be produced. Fix this issue by handling the error from the `_l10n_es_edi_facturae_render_facturae` method. If the certificate is not found, `userError` will be raised. Sentry-4265566923 closes odoo/odoo#127910 X-original-commit: 14e6954bf35c19f5415ad71e52591dd25794e47b Signed-off-by: William André (wan) Signed-off-by: Parth Solanki (paso) --- .../models/account_move.py | 5 ++-- .../models/account_move_send.py | 30 +++++++++---------- .../tests/test_edi_xml.py | 26 ++++++++++++---- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/addons/l10n_es_edi_facturae/models/account_move.py b/addons/l10n_es_edi_facturae/models/account_move.py index 7ca05ad8f57..9e7f1af87c3 100644 --- a/addons/l10n_es_edi_facturae/models/account_move.py +++ b/addons/l10n_es_edi_facturae/models/account_move.py @@ -359,8 +359,9 @@ class AccountMove(models.Model): xml_content = cleanup_xml_node(self.env['ir.qweb']._render('l10n_es_edi_facturae.account_invoice_facturae_export', template_values)) certificate = self.env['l10n_es_edi_facturae.certificate'].search([("company_id", '=', company.id)], limit=1) + errors = [] try: xml_content = certificate._sign_xml(xml_content, signature_values) except ValueError: - raise UserError(_('No valid certificate found for this company, Facturae EDI file will not be signed.\n')) - return xml_content + errors.append(_('No valid certificate found for this company, Facturae EDI file will not be signed.\n')) + return xml_content, errors diff --git a/addons/l10n_es_edi_facturae/models/account_move_send.py b/addons/l10n_es_edi_facturae/models/account_move_send.py index 1e27f322160..8881eaf214f 100644 --- a/addons/l10n_es_edi_facturae/models/account_move_send.py +++ b/addons/l10n_es_edi_facturae/models/account_move_send.py @@ -57,23 +57,23 @@ class AccountMoveSend(models.Model): super()._hook_invoice_document_before_pdf_report_render(invoice, invoice_data) if self.l10n_es_edi_facturae_checkbox_xml and invoice._l10n_es_edi_facturae_get_default_enable(): - try: - xml_content = invoice._l10n_es_edi_facturae_render_facturae() - except Exception as error: - invoice_data['error'] = "".join(( + xml_content, errors = invoice._l10n_es_edi_facturae_render_facturae() + if errors: + invoice_data['error'] = "".join([ _("Errors occured while creating the EDI document (format: %s):", "Facturae"), "\n", - str(error), - )) - - invoice_data['l10n_es_edi_facturae_attachment_values'] = { - 'name': invoice._l10n_es_edi_facturae_get_filename(), - 'raw': xml_content, - 'mimetype': 'application/xml', - 'res_model': invoice._name, - 'res_id': invoice.id, - 'res_field': 'l10n_es_edi_facturae_xml_file', # Binary field - } + "

  • " + "
  • ".join(errors) + "
  • " if self.mode == 'invoice_multi' \ + else "\n".join(errors) + ]) + else: + invoice_data['l10n_es_edi_facturae_attachment_values'] = { + 'name': invoice._l10n_es_edi_facturae_get_filename(), + 'raw': xml_content, + 'mimetype': 'application/xml', + 'res_model': invoice._name, + 'res_id': invoice.id, + 'res_field': 'l10n_es_edi_facturae_xml_file', # Binary field + } def _link_invoice_documents(self, invoice, invoice_data): # EXTENDS 'account' diff --git a/addons/l10n_es_edi_facturae/tests/test_edi_xml.py b/addons/l10n_es_edi_facturae/tests/test_edi_xml.py index 4c0b4528131..f79f7bff684 100644 --- a/addons/l10n_es_edi_facturae/tests/test_edi_xml.py +++ b/addons/l10n_es_edi_facturae/tests/test_edi_xml.py @@ -90,6 +90,16 @@ class TestEdiFacturaeXmls(AccountEdiTestCommon): ], }) + def create_send_and_print(self, invoices, **kwargs): + template = self.env.ref(invoices._get_mail_template()) + return self.env['account.move.send'].with_context( + active_model='account.move', + active_ids=invoices.ids, + ).create({ + 'mail_template_id': template.id, + **kwargs, + }) + def test_generate_signed_xml(self, date=None): random.seed(42) date = date or self.frozen_today @@ -109,7 +119,8 @@ class TestEdiFacturaeXmls(AccountEdiTestCommon): ], ) invoice.action_post() - generated_file = invoice._l10n_es_edi_facturae_render_facturae() + generated_file, errors = invoice._l10n_es_edi_facturae_render_facturae() + self.assertFalse(errors) self.assertTrue(generated_file) with file_open("l10n_es_edi_facturae/tests/data/expected_signed_document.xml", "rt") as f: @@ -125,7 +136,9 @@ class TestEdiFacturaeXmls(AccountEdiTestCommon): patch(f"{self.certificate_module}.sha1", lambda x: sha1()): invoice = self.create_invoice(partner_id=self.partner_a.id, move_type='out_invoice', invoice_line_ids=[{'price_unit': 100.0, 'tax_ids': [self.tax.id]},],) invoice.action_post() - self.assertRaises(UserError, invoice._l10n_es_edi_facturae_render_facturae) + wizard = self.create_send_and_print(invoice) + with self.assertRaises(UserError): + wizard.action_send_and_print() def test_tax_withheld(self): with freeze_time(self.frozen_today), \ @@ -155,7 +168,8 @@ class TestEdiFacturaeXmls(AccountEdiTestCommon): ], ) invoice.action_post() - generated_file = invoice._l10n_es_edi_facturae_render_facturae() + generated_file, errors = invoice._l10n_es_edi_facturae_render_facturae() + self.assertFalse(errors) self.assertTrue(generated_file) with file_open("l10n_es_edi_facturae/tests/data/expected_tax_withholding.xml", "rt") as f: expected_xml = lxml.etree.fromstring(f.read().encode()) @@ -179,7 +193,8 @@ class TestEdiFacturaeXmls(AccountEdiTestCommon): ], ) invoice.action_post() - generated_file = invoice._l10n_es_edi_facturae_render_facturae() + generated_file, errors = invoice._l10n_es_edi_facturae_render_facturae() + self.assertFalse(errors) self.assertTrue(generated_file) with file_open("l10n_es_edi_facturae/tests/data/expected_in_invoice_document.xml", "rt") as f: @@ -212,7 +227,8 @@ class TestEdiFacturaeXmls(AccountEdiTestCommon): }) reversal_wizard.modify_moves() refund = invoice.reversal_move_id - generated_file = refund._l10n_es_edi_facturae_render_facturae() + generated_file, errors = refund._l10n_es_edi_facturae_render_facturae() + self.assertFalse(errors) self.assertTrue(generated_file) with file_open("l10n_es_edi_facturae/tests/data/expected_refund_document.xml", "rt") as f: