From c52f91a5883ef7009efcaa029531b49be23cec24 Mon Sep 17 00:00:00 2001 From: Nicolas Martinelli Date: Fri, 22 Nov 2019 12:10:41 +0000 Subject: [PATCH] [FIX] l10n_be_edi: error at PDF generation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An UBL invoice can contain several `AdditionalDocumentReference`, some of them without `Attachment`. For example: ``` UBL.BE BOB50 6.07.0 Facture N°19090118 - 30-09-2019.pdf CommercialInvoice JVBERi0xLjMK... ... ``` In this case, a crash occurs since: ``` element.xpath('cac:Attachment//cbc:EmbeddedDocumentBinaryObject', namespaces=namespaces) ``` is an empty list. To avoid this, we loop on all elements and generate all PDFs. opw-2128083 closes odoo/odoo#40721 X-original-commit: 2174e4066c5c8f87ed45aa564c184d1ce7063758 Signed-off-by: Nicolas Martinelli (nim) --- addons/l10n_be_edi/models/account_invoice.py | 25 +++++++++++--------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/addons/l10n_be_edi/models/account_invoice.py b/addons/l10n_be_edi/models/account_invoice.py index 66b82756ada..d6d755e71e1 100644 --- a/addons/l10n_be_edi/models/account_invoice.py +++ b/addons/l10n_be_edi/models/account_invoice.py @@ -108,18 +108,21 @@ class AccountMove(models.Model): invoice_form.partner_id = self.env['res.partner'] # Regenerate PDF + attachments = self.env['ir.attachment'] elements = tree.xpath('//cac:AdditionalDocumentReference', namespaces=namespaces) - if elements: - attachment_name = elements[0].xpath('cbc:ID', namespaces=namespaces)[0].text - attachment_data = elements[0].xpath('cac:Attachment//cbc:EmbeddedDocumentBinaryObject', namespaces=namespaces)[0].text - attachment_id = self.env['ir.attachment'].create({ - 'name': attachment_name, - 'res_id': self.id, - 'res_model': 'account.move', - 'datas': attachment_data, - 'type': 'binary', - }) - self.with_context(no_new_invoice=True).message_post(attachment_ids=[attachment_id.id]) + for element in elements: + attachment_name = element.xpath('cbc:ID', namespaces=namespaces) + attachment_data = element.xpath('cac:Attachment//cbc:EmbeddedDocumentBinaryObject', namespaces=namespaces) + if attachment_name and attachment_data: + attachments |= self.env['ir.attachment'].create({ + 'name': attachment_name[0].text, + 'res_id': self.id, + 'res_model': 'account.move', + 'datas': attachment_data[0].text, + 'type': 'binary', + }) + if attachments: + self.with_context(no_new_invoice=True).message_post(attachment_ids=attachments.ids) # Lines lines_elements = tree.xpath('//cac:InvoiceLine', namespaces=namespaces)