[FIX] l10n_es_edi_facturae: certificates deleted for facturae EDI

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) <wan@odoo.com>
Signed-off-by: Parth Solanki (paso) <paso@odoo.com>
This commit is contained in:
paso-odoo
2023-07-10 16:59:36 +02:00
parent b970caef22
commit 3810ce75a0
3 changed files with 39 additions and 22 deletions
@@ -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
@@ -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
}
"<p><li>" + "</li><li>".join(errors) + "</li></p>" 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'
@@ -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: