Skip to content
Snippets Groups Projects
Commit 2174e406 authored by Nicolas Martinelli's avatar Nicolas Martinelli
Browse files

[FIX] l10n_be_edi: error at PDF generation


An UBL invoice can contain several `AdditionalDocumentReference`, some
of them without `Attachment`. For example:

```
  <cac:AdditionalDocumentReference>
    <cbc:ID>UBL.BE</cbc:ID>
    <cbc:DocumentDescription>BOB50  6.07.0</cbc:DocumentDescription>
  </cac:AdditionalDocumentReference>
  <cac:AdditionalDocumentReference>
    <cbc:ID>Facture N°19090118 - 30-09-2019.pdf</cbc:ID>
    <cbc:DocumentDescription>CommercialInvoice</cbc:DocumentDescription>
    <cac:Attachment>
      <cbc:EmbeddedDocumentBinaryObject filename="Facture.pdf" mimeCode="application/pdf">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#40698

Signed-off-by: default avatarNicolas Martinelli (nim) <nim@odoo.com>
parent 9a20896e
Branches
Tags
No related merge requests found
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment