[FIX] account_edi_ubl: extracting the pdf from the imported xml doesn't raise an exception any more when creating a new invoice

Before this commit, when an attachment was present in the xml (pdf) and the import was to create a new invoice, it crashed when trying to post the pdf on the not-yet existant invoice.

This commit also fixes:
- In the tests, `create_invoice_from_file` didn't handle the subfolder parameter correctly
- `create_invoice_from_file` now returns the created invoice

closes odoo/odoo#69722

X-original-commit: e81457501f4815a4d9add7a56a9dfe66a7656269
Signed-off-by: Laurent Smet <smetl@users.noreply.github.com>
Signed-off-by: bfr-o <bfr-o@users.noreply.github.com>
This commit is contained in:
Benjamin Frantzen (bfr)
2021-04-22 16:57:12 +00:00
parent 08bb6e2e50
commit b27614b136
4 changed files with 45 additions and 28 deletions
+3 -3
View File
@@ -157,7 +157,7 @@ class AccountEdiTestCommon(AccountTestInvoicingCommon):
invoice.message_post(attachment_ids=[attachment.id])
def create_invoice_from_file(self, module_name, subfolder, filename):
file_path = get_module_resource(module_name, 'test_file', filename)
file_path = get_module_resource(module_name, subfolder, filename)
file = open(file_path, 'rb').read()
attachment = self.env['ir.attachment'].create({
@@ -165,9 +165,9 @@ class AccountEdiTestCommon(AccountTestInvoicingCommon):
'datas': base64.encodebytes(file),
'res_model': 'account.move',
})
journal_id = self.company_data['default_journal_sale']
journal_id.with_context(default_move_type='in_invoice').create_invoice_from_attachment(attachment.ids)
action_vals = journal_id.with_context(default_move_type='in_invoice').create_invoice_from_attachment(attachment.ids)
return self.env['account.move'].browse(action_vals['res_id'])
def assert_generated_file_equal(self, invoice, expected_values, applied_xpath=None):
invoice.action_post()
@@ -104,29 +104,6 @@ class AccountEdiFormat(models.Model):
vat=_find_value('//cac:AccountingSupplierParty/cac:Party//cbc:CompanyID'),
)
# Regenerate PDF
attachments = self.env['ir.attachment']
elements = tree.xpath('//cac:AdditionalDocumentReference', namespaces=namespaces)
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:
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': 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)
# Lines
lines_elements = tree.xpath('//cac:InvoiceLine', namespaces=namespaces)
for eline in lines_elements:
@@ -170,8 +147,32 @@ class AccountEdiFormat(models.Model):
)
if tax:
invoice_line_form.tax_ids.add(tax)
invoice = invoice_form.save()
return invoice_form.save()
# Regenerate PDF
attachments = self.env['ir.attachment']
elements = tree.xpath('//cac:AdditionalDocumentReference', namespaces=namespaces)
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:
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': 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)
return invoice
####################################################
# Export
File diff suppressed because one or more lines are too long
+9 -1
View File
@@ -181,7 +181,7 @@ class TestL10nBeEdi(AccountEdiTestCommon):
# Test import
####################################################
def test_invoice_edi_xml(self):
def test_invoice_edi_xml_update(self):
invoice = self._create_empty_vendor_bill()
invoice_count = len(self.env['account.move'].search([]))
self.update_invoice_from_file('l10n_be_edi', 'test_xml_file', 'efff_test.xml', invoice)
@@ -190,3 +190,11 @@ class TestL10nBeEdi(AccountEdiTestCommon):
self.assertEqual(invoice.amount_total, 666.50)
self.assertEqual(invoice.amount_tax, 115.67)
self.assertEqual(invoice.partner_id, self.partner_a)
def test_invoice_edi_xml_create(self):
invoice_count = len(self.env['account.move'].search([]))
invoice = self.create_invoice_from_file('l10n_be_edi', 'test_xml_file', 'efff_test.xml')
self.assertEqual(len(self.env['account.move'].search([])), invoice_count + 1)
self.assertEqual(invoice.amount_total, 666.50)
self.assertEqual(invoice.amount_tax, 115.67)
self.assertEqual(invoice.partner_id, self.partner_a)