[FIX] l10n_be_edi: pdf extracted from e-fff file have correct extension

The text of the node 'cac:AdditionalDocumentReference/cbc:ID', which should contain the name of the pdf file in an e-fff document is incorrectly set by some software, and may contain the path to the file and/or the name of the xml instead of the pdf. Based on this information, we extract the stem only and append the .pdf extension to form a valid filename for the pdf.

TASK ID: 2339062

X-original-commit: f998d96d68e2e153e6061a273eb65ec3699501e4
This commit is contained in:
Benjamin Frantzen (bfr)
2021-02-08 08:17:20 +00:00
parent fbc77a6ecd
commit 548d08bdc6
@@ -7,6 +7,8 @@ from odoo.exceptions import UserError
from odoo.osv import expression
from datetime import datetime
from pathlib import PureWindowsPath
from xml.sax.saxutils import unescape
import logging
@@ -99,12 +101,17 @@ class AccountEdiFormat(models.Model):
attachment_data = element.xpath('cac:Attachment//cbc:EmbeddedDocumentBinaryObject', namespaces=namespaces)
if attachment_name and attachment_data:
text = attachment_data[0].text
# Normalize the name of the file : some e-fff emitters put the full path of the file
# (Windows or Linux style) and/or the name of the xml instead of the pdf.
# Get only the filename with a pdf extension.
name = PureWindowsPath(attachment_name[0].text).stem + '.pdf'
attachments |= self.env['ir.attachment'].create({
'name': attachment_name[0].text,
'name': name,
'res_id': invoice.id,
'res_model': 'account.move',
'datas': text + '=' * (len(text) % 3), # Fix incorrect padding
'type': 'binary',
'mimetype': 'application/pdf',
})
if attachments:
invoice.with_context(no_new_invoice=True).message_post(attachment_ids=attachments.ids)