From e2a3c052f2d9bce4a2490f11d535453ec41e1bb7 Mon Sep 17 00:00:00 2001 From: FrankC013 Date: Tue, 17 Sep 2024 12:43:22 +0200 Subject: [PATCH] [IMP] account_invoice_batches: report intermediary service --- account_invoice_batches/__manifest__.py | 1 + account_invoice_batches/models/res_company.py | 5 ++ .../models/res_config_settings.py | 4 ++ .../views/res_config_settings_views.xml | 19 +++++++ .../wizard/account_invoice_batch_process.py | 57 +++++++++++++++++-- 5 files changed, 81 insertions(+), 5 deletions(-) diff --git a/account_invoice_batches/__manifest__.py b/account_invoice_batches/__manifest__.py index 3c5c322b5..79caaaef4 100644 --- a/account_invoice_batches/__manifest__.py +++ b/account_invoice_batches/__manifest__.py @@ -14,6 +14,7 @@ "l10n_es_facturae", "account_invoice_report_service", "sale_order_invoicing_grouping_criteria", + "account_move_service", "queue_job", ], "data": [ diff --git a/account_invoice_batches/models/res_company.py b/account_invoice_batches/models/res_company.py index 111964dcd..4a99c3381 100644 --- a/account_invoice_batches/models/res_company.py +++ b/account_invoice_batches/models/res_company.py @@ -13,3 +13,8 @@ class ResCompany(models.Model): comodel_name="mail.template", domain=[("model_id", "=", "account.move")], ) + report_intermediary_service_id = fields.Many2one( + string="Default Report for Intermediary Service Partner", + comodel_name="ir.actions.report", + domain=[("model", "=", "account.move")], + ) diff --git a/account_invoice_batches/models/res_config_settings.py b/account_invoice_batches/models/res_config_settings.py index e4741a8ef..0aca178d0 100644 --- a/account_invoice_batches/models/res_config_settings.py +++ b/account_invoice_batches/models/res_config_settings.py @@ -12,3 +12,7 @@ class ResConfigSettings(models.TransientModel): related="company_id.invoice_batch_sending_email_template_id", readonly=False, ) + report_intermediary_service_id = fields.Many2one( + related="company_id.report_intermediary_service_id", + readonly=False, + ) diff --git a/account_invoice_batches/views/res_config_settings_views.xml b/account_invoice_batches/views/res_config_settings_views.xml index ef4290829..f7cef6397 100644 --- a/account_invoice_batches/views/res_config_settings_views.xml +++ b/account_invoice_batches/views/res_config_settings_views.xml @@ -33,6 +33,25 @@ +
+
+
+
+
diff --git a/account_invoice_batches/wizard/account_invoice_batch_process.py b/account_invoice_batches/wizard/account_invoice_batch_process.py index c97541caa..5f7818a23 100644 --- a/account_invoice_batches/wizard/account_invoice_batch_process.py +++ b/account_invoice_batches/wizard/account_invoice_batch_process.py @@ -3,8 +3,11 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) import base64 +import io import logging +from PyPDF2 import PdfFileReader + from odoo import _, fields, models from odoo.exceptions import UserError @@ -152,8 +155,52 @@ def process_invoices(self): if invoices_pdf: if not self.company_id.report_service_id: raise UserError(_("There's no report defined on invoice company")) - report_action = self.company_id.report_service_id.report_action( - invoices_pdf - ) - invoices_pdf.write({"is_move_sent": True}) - return report_action + report_service = self.company_id.report_service_id + if invoices_pdf.filtered(lambda x: x.partner_id.service_intermediary): + if not self.company_id.report_intermediary_service_id: + raise UserError( + _( + "The report for service intermediary partners is not " + "defined in the configuration settings. Please go to " + "the billing configuration and specify which report " + "will be used." + ) + ) + report_intermediary = self.company_id.report_intermediary_service_id + pdf_streams = [] + for invoice in invoices_pdf: + if invoice.partner_id.service_intermediary: + if not report_intermediary: + raise UserError( + _( + "The report for service intermediary partners is not " + "defined in the configuration settings. Please go to " + "the billing configuration and specify which report " + "will be used." + ) + ) + report = report_intermediary + else: + report = report_service + + pdf_content, _p = report._render_qweb_pdf([invoice.id]) + pdf_stream = io.BytesIO(pdf_content) + pdf_streams.append(pdf_stream) + + if pdf_streams: + merged_pdf_stream = io.BytesIO() + pdf_merger = PdfFileReader(pdf_content) + for pdf_stream in pdf_streams: + pdf_merger.append(pdf_stream) + pdf_merger.write(merged_pdf_stream) + pdf_merger.close() + + return { + "type": "ir.actions.report", + "report_type": "qweb-pdf", + "data": {"pdf_content": merged_pdf_stream.getvalue()}, + "report_name": "Merged_Invoices", + "name": _("Merged Invoices"), + } + else: + raise UserError(_("No PDFs were generated for the selected invoices."))