# -*- coding: utf-8 -*- # This file is part of the account-invoice-xrechnung-module # from m-ds for Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. import zipfile from facturx import generate_from_binary from io import BytesIO from trytond.report import Report from trytond.pool import Pool from slugify import slugify from .wizard_runreport import edoc_versions class XReport(Report): 'eDocument Export' __name__ = 'account_invoice_xrechnung.export' @classmethod def compress_as_zip(cls, fname, data): """ compress """ content = BytesIO() with zipfile.ZipFile(content, 'w') as content_zip: content_zip.writestr(fname, data) return content.getvalue() @classmethod def execute(cls, ids, data): """ skip export-engine, run edocument-xml-convert """ pool = Pool() IrDate = pool.get('ir.date') Invoice = pool.get('account.invoice') document_para = pool.get(data['edocument'].split('-')[0]) EDocument = document_para[0] document_var = document_para[1] if len(document_para) > 1 else None invoice, = Invoice.browse([data['invoice']]) template = EDocument(invoice) invoice_xml = template.render(edoc_versions[data['edocument']]) file_name = slugify('%(date)s-%(descr)s' % { 'date': IrDate.today().isoformat().replace('-', ''), 'descr': invoice.rec_name}, max_length=100, word_boundary=True, save_order=True) # if document_var and ( # document_var == 'ferd') and ( # EDocument.__name__ == 'edocument.facturxext.invoice'): # # convert to zugferd # pdf_data = generate_from_binary( # pdf_file='pdf_content', # xml=invoice_xml, # check_xsd=True, # pdf_metadata={ # 'author': invoice.company.rec_name, # 'keywords': 'Factur-X, Invoice', # 'title': invoice.number, # 'subject': invoice.description}, # lang='de-DE') if data['as_zip'] is True: return ( 'zip', cls.compress_as_zip('%(fname)s.%(ext)s' % { 'fname': file_name, 'ext': 'xml', }, invoice_xml), False, file_name) else: return ('xml', invoice_xml, False, file_name) # end XReport