From 23bace99bb7d49e8f81fb2c8c8986f7f0fd2bd0d Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Thu, 12 Dec 2024 12:37:56 +0100 Subject: [PATCH] optimize generation of pdf if its not stored to db --- wizard_runreport.py | 18 +++------------- xreport.py | 52 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/wizard_runreport.py b/wizard_runreport.py index 183126b..dd5b0bf 100644 --- a/wizard_runreport.py +++ b/wizard_runreport.py @@ -118,21 +118,10 @@ class RunXRechnungReport(Wizard): 'invoice_report_cache_id': None, 'invoice_report_format': None}]) - to_generate = [x.id for x in invoices if not x.invoice_report_cache] + to_generate = [x for x in invoices if not x.invoice_report_cache] if to_generate: - report_action = XReport.get_used_report() - - # run selected report on invoices w/o stored report-data - data2 = {} - data2.update(data) - data2['action_id'] = report_action.id - data2['model'] = report_action.model - data2['id'] = to_generate[0] - data2['ids'] = to_generate - - RepInvoice = pool.get(report_action.report_name, type='report') - with Transaction().set_context({'with_rec_name': False}): - RepInvoice.execute(to_generate, data2) + # the pdf should be stored in db... + XReport.generate_pdf_data(to_generate) def do_export(self, action): """ run export @@ -151,7 +140,6 @@ class RunXRechnungReport(Wizard): if data['edocument'] == 'edocument.facturxext.invoice-ferd': # pdf is stored to db self.generate_invoice_reports(data) - return action, data # end RunXRechnungReport diff --git a/xreport.py b/xreport.py index 596afc4..d104558 100644 --- a/xreport.py +++ b/xreport.py @@ -11,6 +11,7 @@ from trytond.report import Report from trytond.pool import Pool from trytond.exceptions import UserError from trytond.i18n import gettext +from trytond.transaction import Transaction from .wizard_runreport import edoc_versions @@ -112,6 +113,36 @@ class XReport(Report): 'account_invoice_xrechnung.msg_no_report_found')) return act_report + @classmethod + def generate_pdf_data(cls, records): + """ generate pdf of invoice using defined report + + Args: + records (list): records of account.invoice + + Returns: + list: dicts - {'content': , 'fname': 'file-name'} + """ + report_action = cls.get_used_report() + RepInvoice = Pool().get(report_action.report_name, type='report') + + # run selected report on invoices w/o stored report-data + data2 = {} + data2['action_id'] = report_action.id + data2['model'] = report_action.model + result = [] + for record in records: + data2['id'] = record.id + data2['ids'] = [record.id] + + with Transaction().set_context({'with_rec_name': False}): + (ext, content, print1, fname) = RepInvoice.execute( + [record.id], data2) + result.append({ + 'content': content, + 'fname': fname}) + return result + @classmethod def get_zugferd_pdf(cls, invoice, invoice_xml): """ generate ZugFeRD-PDF @@ -120,15 +151,22 @@ class XReport(Report): invoice (record): model account.invoice invoice_xml (str): xml-data """ - # pdf was already stored to db - if not (invoice.invoice_report_cache and ( - invoice.invoice_report_format == 'pdf')): - raise UserError(gettext( - 'account_invoice_xrechnung.msg_invalid_cachecontent', - invoice_name=invoice.rec_name)) + # pdf was already stored to db, must be pdf + if invoice.invoice_report_cache: + if invoice.invoice_report_format != 'pdf': + raise UserError(gettext( + 'account_invoice_xrechnung.msg_invalid_cachecontent', + invoice_name=invoice.rec_name)) + pdf_data = invoice.invoice_report_cache + else: + # run report, could fail because we are in + # readonly-transaction + report_data = cls.generate_pdf_data([invoice]) + if report_data: + pdf_data = report_data[0]['content'] zugferd_pdf = generate_from_binary( - pdf_file=invoice.invoice_report_cache, + pdf_file=pdf_data, xml=invoice_xml, check_xsd=True, pdf_metadata={