diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/l10n_be_edi/models | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/l10n_be_edi/models')
| -rw-r--r-- | addons/l10n_be_edi/models/__init__.py | 5 | ||||
| -rw-r--r-- | addons/l10n_be_edi/models/account_edi_format.py | 53 | ||||
| -rw-r--r-- | addons/l10n_be_edi/models/account_move.py | 22 | ||||
| -rw-r--r-- | addons/l10n_be_edi/models/ir_actions_report.py | 52 |
4 files changed, 132 insertions, 0 deletions
diff --git a/addons/l10n_be_edi/models/__init__.py b/addons/l10n_be_edi/models/__init__.py new file mode 100644 index 00000000..ae085e47 --- /dev/null +++ b/addons/l10n_be_edi/models/__init__.py @@ -0,0 +1,5 @@ +# -*- encoding: utf-8 -*- + +from . import account_edi_format +from . import account_move +from . import ir_actions_report diff --git a/addons/l10n_be_edi/models/account_edi_format.py b/addons/l10n_be_edi/models/account_edi_format.py new file mode 100644 index 00000000..2b0566c3 --- /dev/null +++ b/addons/l10n_be_edi/models/account_edi_format.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- + +from odoo import models + +import base64 + + +class AccountEdiFormat(models.Model): + _inherit = 'account.edi.format' + + def _is_efff(self, filename, tree): + return self.code == 'efff_1' and tree.tag == '{urn:oasis:names:specification:ubl:schema:xsd:Invoice-2}Invoice' + + def _create_invoice_from_xml_tree(self, filename, tree): + self.ensure_one() + if self._is_efff(filename, tree): + return self._create_invoice_from_ubl(tree) + return super()._create_invoice_from_xml_tree(filename, tree) + + def _update_invoice_from_xml_tree(self, filename, tree, invoice): + self.ensure_one() + if self._is_efff(filename, tree): + return self._update_invoice_from_ubl(tree, invoice) + return super()._update_invoice_from_xml_tree(filename, tree, invoice) + + def _is_compatible_with_journal(self, journal): + self.ensure_one() + res = super()._is_compatible_with_journal(journal) + if self.code != 'efff_1': + return res + return journal.type == 'sale' and journal.country_code == 'BE' + + def _post_invoice_edi(self, invoices, test_mode=False): + self.ensure_one() + if self.code != 'efff_1': + return super()._post_invoice_edi(invoices, test_mode=test_mode) + res = {} + for invoice in invoices: + attachment = self._export_efff(invoice) + res[invoice] = {'attachment': attachment} + return res + + def _export_efff(self, invoice): + self.ensure_one() + # Create file content. + xml_content = b"<?xml version='1.0' encoding='UTF-8'?>" + xml_content += self.env.ref('account_edi_ubl.export_ubl_invoice')._render(invoice._get_ubl_values()) + xml_name = '%s.xml' % invoice._get_efff_name() + return self.env['ir.attachment'].create({ + 'name': xml_name, + 'datas': base64.encodebytes(xml_content), + 'mimetype': 'application/xml', + }) diff --git a/addons/l10n_be_edi/models/account_move.py b/addons/l10n_be_edi/models/account_move.py new file mode 100644 index 00000000..6a3dd240 --- /dev/null +++ b/addons/l10n_be_edi/models/account_move.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- + +from odoo import models + +import re + +class AccountMove(models.Model): + _inherit = 'account.move' + + def _get_ubl_values(self): + values = super(AccountMove, self)._get_ubl_values() + + # E-fff uses ubl_version 2.0, account_edi_ubl supports ubl_version 2.1 but generates 2.0 UBL + # so we only need to override the version to be compatible with E-FFF + values['ubl_version'] = 2.0 + + return values + + def _get_efff_name(self): + self.ensure_one() + vat = self.company_id.partner_id.commercial_partner_id.vat + return 'efff_%s%s%s' % (vat or '', '_' if vat else '', re.sub('[\W_]', '', self.name)) # official naming convention diff --git a/addons/l10n_be_edi/models/ir_actions_report.py b/addons/l10n_be_edi/models/ir_actions_report.py new file mode 100644 index 00000000..0ba1ce4c --- /dev/null +++ b/addons/l10n_be_edi/models/ir_actions_report.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import models + +from lxml import etree +import base64 +from xml.sax.saxutils import escape, quoteattr + + +class IrActionsReport(models.Model): + _inherit = 'ir.actions.report' + + def _postprocess_pdf_report(self, record, buffer): + '''Add the pdf report in the e-fff XML as base64 string. + ''' + result = super()._postprocess_pdf_report(record, buffer) + + if record._name == 'account.move': + edi_attachment = record.edi_document_ids.filtered(lambda d: d.edi_format_id.code == 'efff_1').attachment_id + if edi_attachment: + old_xml = base64.b64decode(edi_attachment.with_context(bin_size=False).datas, validate=True) + tree = etree.fromstring(old_xml) + document_currency_code_elements = tree.xpath("//*[local-name()='DocumentCurrencyCode']") + additional_document_elements = tree.xpath("//*[local-name()='AdditionalDocumentReference']") + if document_currency_code_elements and not additional_document_elements: + pdf = base64.b64encode(buffer.getvalue()).decode() + pdf_name = '%s.pdf' % record._get_efff_name() + to_inject = ''' + <cac:AdditionalDocumentReference + xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" + xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" + xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"> + <cbc:ID>%s</cbc:ID> + <cac:Attachment> + <cbc:EmbeddedDocumentBinaryObject mimeCode="application/pdf" filename=%s> + %s + </cbc:EmbeddedDocumentBinaryObject> + </cac:Attachment> + </cac:AdditionalDocumentReference> + ''' % (escape(pdf_name), quoteattr(pdf_name), pdf) + + document_currency_code_elements[0].addnext(etree.fromstring(to_inject)) + new_xml = etree.tostring(tree, pretty_print=True) + edi_attachment.write({ + 'res_model': 'account.move', + 'res_id': record.id, + 'datas': base64.b64encode(new_xml), + 'mimetype': 'application/xml', + }) + + return result |
