From 5e6c94b9be685d34a794a51d4680f0d01404b615 Mon Sep 17 00:00:00 2001 From: Julien Van Roy Date: Tue, 17 Oct 2023 13:37:41 +0200 Subject: [PATCH] [FIX] account: fix invoice_report_file_id if proforma pdf When triggering the Send & Print with the allow_fallback_pdf=True, if an error occurs (e.g.: no UNSCPC code on the product with CFDI), the proforma PDF is generated instead and is assigned to `invoice_report_file_id`. However, only a successful PDF should be bound to this field. task-3542881 closes odoo/odoo#138206 Related: odoo/enterprise#49137 Signed-off-by: Laurent Smet (las) --- addons/account/tests/test_account_move_send.py | 5 +++-- addons/account/wizard/account_move_send.py | 4 +--- addons/account_edi_ubl_cii/models/account_move_send.py | 10 ++++++---- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/addons/account/tests/test_account_move_send.py b/addons/account/tests/test_account_move_send.py index 12799885fee..49efb233a66 100644 --- a/addons/account/tests/test_account_move_send.py +++ b/addons/account/tests/test_account_move_send.py @@ -884,8 +884,9 @@ class TestAccountMoveSend(TestAccountMoveSendCommon): self.assertEqual(results['type'], 'ir.actions.act_url') self.assertRecordValues(wizard, [{'mode': 'done'}]) - # The PDF is generated even in case of error. - self.assertTrue(invoice.invoice_pdf_report_id) + # The PDF is generated even in case of error, but invoice_pdf_report_id is not set + self.assertFalse(invoice.invoice_pdf_report_id) + self.assertTrue(invoice.message_main_attachment_id) def test_with_unlink_invoices(self): invoice = self.init_invoice("out_invoice", amounts=[1000], post=True) diff --git a/addons/account/wizard/account_move_send.py b/addons/account/wizard/account_move_send.py index 4aca179eedd..2bc17e6e20e 100644 --- a/addons/account/wizard/account_move_send.py +++ b/addons/account/wizard/account_move_send.py @@ -535,7 +535,7 @@ class AccountMoveSend(models.Model): } for invoice, invoice_data in invoices_data_pdf.items(): form = invoice_data['_form'] - if form._need_invoice_document(invoice): + if form._need_invoice_document(invoice) and not invoice_data.get('error'): form._prepare_invoice_pdf_report(invoice, invoice_data) form._hook_invoice_document_after_pdf_report_render(invoice, invoice_data) @@ -548,8 +548,6 @@ class AccountMoveSend(models.Model): } if invoices_data_pdf_error: self._hook_if_errors(invoices_data_pdf_error, allow_fallback_pdf=allow_fallback_pdf) - for invoice_data in invoices_data_pdf_error.values(): - invoice_data.pop('error') # Web-service after the PDF generation. invoices_data_web_service = { diff --git a/addons/account_edi_ubl_cii/models/account_move_send.py b/addons/account_edi_ubl_cii/models/account_move_send.py index 4563142618a..401e6a3309f 100644 --- a/addons/account_edi_ubl_cii/models/account_move_send.py +++ b/addons/account_edi_ubl_cii/models/account_move_send.py @@ -144,7 +144,8 @@ class AccountMoveSend(models.Model): return # Read pdf content. - reader_buffer = io.BytesIO(invoice_data['pdf_attachment_values']['raw']) + pdf_values = invoice_data.get('pdf_attachment_values') or invoice_data['proforma_pdf_attachment_values'] + reader_buffer = io.BytesIO(pdf_values['raw']) reader = OdooPdfFileReader(reader_buffer, strict=False) # Post-process. @@ -174,7 +175,7 @@ class AccountMoveSend(models.Model): # Replace the current content. writer_buffer = io.BytesIO() writer.write(writer_buffer) - invoice_data['pdf_attachment_values']['raw'] = writer_buffer.getvalue() + pdf_values['raw'] = writer_buffer.getvalue() reader_buffer.close() writer_buffer.close() @@ -185,8 +186,9 @@ class AccountMoveSend(models.Model): if not anchor_elements: return - filename = invoice_data['pdf_attachment_values']['name'] - content = invoice_data['pdf_attachment_values']['raw'] + pdf_values = invoice_data.get('pdf_attachment_values') or invoice_data['proforma_pdf_attachment_values'] + filename = pdf_values['name'] + content = pdf_values['raw'] edi_model = invoice_data["ubl_cii_xml_options"]["builder"] doc_type_code_vals = edi_model._get_document_type_code_vals(invoice, invoice_data)