From fc0464427f268ed58d5f800dfcf40404640c6676 Mon Sep 17 00:00:00 2001 From: Laurent Smet Date: Thu, 15 Jun 2023 12:55:32 +0000 Subject: [PATCH] [IMP] account: Allow custom invoice PDF template/values for localizations in send & print The logic of the send & print wizard is to generate all EDI documents and PDF at the same time to ensure the consistency between all business documents. In some localizations, we need custom references to the generated EDI documents inside the PDF. The problem is the current PDF engine is not designed to easily extend such PDF template and provide custom values to render it. Also, you absolutely need an ir.actions.report to use the PDF rendering. To make such customizations less paintful, this commit adds a hook on the send & print wizard allowing to provide a custom template inheriting the standard one and custom values for the rendering. Task: 3069324 Part-of: odoo/odoo#128395 --- addons/account/models/account_move.py | 2 +- .../account/tests/test_account_move_send.py | 16 ++++++++++ addons/account/views/report_invoice.xml | 12 ++------ addons/account/wizard/account_move_send.py | 30 ++++++++++++++++++- addons/l10n_ar/views/report_invoice.xml | 16 ---------- addons/l10n_au/views/report_invoice.xml | 14 --------- addons/l10n_cl/views/report_invoice.xml | 16 ---------- .../l10n_gcc_invoice/views/report_invoice.xml | 15 ---------- addons/l10n_th/views/report_invoice.xml | 15 ---------- 9 files changed, 49 insertions(+), 87 deletions(-) diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index 5e692afcd7d..10061b7b223 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -4586,7 +4586,7 @@ class AccountMove(models.Model): """ This method need to be inherit by the localizations if they want to print a custom invoice report instead of the default one. For example please review the l10n_ar module """ self.ensure_one() - return 'account.report_invoice_document' + return self._context.get('force_report_invoice_template') or 'account.report_invoice_document' def _is_downpayment(self): ''' Return true if the invoice is a downpayment. diff --git a/addons/account/tests/test_account_move_send.py b/addons/account/tests/test_account_move_send.py index 9174deb39c3..a221246f993 100644 --- a/addons/account/tests/test_account_move_send.py +++ b/addons/account/tests/test_account_move_send.py @@ -755,3 +755,19 @@ class TestAccountMoveSend(TestAccountMoveSendCommon): # The PDF is generated even in case of error. self.assertTrue(invoice.invoice_pdf_report_id) + + def test_get_invoice_pdf_report_to_render(self): + invoice = self.init_invoice("out_invoice", amounts=[1000], post=True) + _get_name_invoice_report = invoice._get_name_invoice_report + wizard = self.create_send_and_print(invoice) + + def get_invoice_pdf_report_to_render(record, *args, **kwargs): + return 'account.report_invoice_document', {} + + def get_name_invoice_report(record, *args, **kwargs): + self.assertEqual(record._context.get('force_report_invoice_template'), 'account.report_invoice_document') + return _get_name_invoice_report(*args, **kwargs) + + with patch.object(type(wizard), '_get_invoice_pdf_report_to_render', get_invoice_pdf_report_to_render), \ + patch.object(type(invoice), '_get_name_invoice_report', get_name_invoice_report): + wizard.action_send_and_print(allow_fallback_pdf=True) diff --git a/addons/account/views/report_invoice.xml b/addons/account/views/report_invoice.xml index 66d79991498..c4fdeae7838 100644 --- a/addons/account/views/report_invoice.xml +++ b/addons/account/views/report_invoice.xml @@ -426,20 +426,14 @@ - + diff --git a/addons/account/wizard/account_move_send.py b/addons/account/wizard/account_move_send.py index 71e44767803..29f183089d1 100644 --- a/addons/account/wizard/account_move_send.py +++ b/addons/account/wizard/account_move_send.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +from unittest.mock import patch from odoo import _, api, fields, models, modules, tools, Command from odoo.exceptions import UserError @@ -306,6 +307,10 @@ class AccountMoveSend(models.Model): """ self.ensure_one() + def _get_invoice_pdf_report_to_render(self, invoice, invoice_data): + self.ensure_one() + return None, {} + def _prepare_invoice_pdf_report(self, invoice, invoice_data): """ Prepare the pdf report for the invoice passed as parameter. @@ -317,7 +322,30 @@ class AccountMoveSend(models.Model): if invoice.invoice_pdf_report_id: return - content, _report_format = self.env['ir.actions.report']._render('account.account_invoices', invoice.ids) + # Render the invoice PDF but allow to set a custom report_name and custom values. + # That way, all invoice reports are separated from the main one but don't need an + # extra report every time. + IrActionsReport = type(self.env['ir.actions.report']) + _render_template = IrActionsReport._render_template + inv_report, inv_report_values = self._get_invoice_pdf_report_to_render(invoice, invoice_data) + + def render_template(records, template, values=None): + return _render_template( + records, + template, + values={ + **(values or {}), + **inv_report_values, + }, + ) + + with patch.object(IrActionsReport, '_render_template', side_effect=render_template, autospec=True): + content, _report_format = self.env['ir.actions.report']\ + .with_context(force_report_invoice_template=inv_report)\ + ._render( + 'account.account_invoices', + invoice.ids, + ) invoice_data['pdf_attachment_values'] = { 'raw': content, diff --git a/addons/l10n_ar/views/report_invoice.xml b/addons/l10n_ar/views/report_invoice.xml index 350cd5353e3..7b2c30cf894 100644 --- a/addons/l10n_ar/views/report_invoice.xml +++ b/addons/l10n_ar/views/report_invoice.xml @@ -286,20 +286,4 @@ not o._l10n_ar_include_vat() - - - - - - diff --git a/addons/l10n_au/views/report_invoice.xml b/addons/l10n_au/views/report_invoice.xml index 3ef06edc2dd..51588b4669e 100644 --- a/addons/l10n_au/views/report_invoice.xml +++ b/addons/l10n_au/views/report_invoice.xml @@ -1,19 +1,5 @@ - - - - - - - - - - diff --git a/addons/l10n_gcc_invoice/views/report_invoice.xml b/addons/l10n_gcc_invoice/views/report_invoice.xml index 5c5eb395b19..381ac2a67c8 100644 --- a/addons/l10n_gcc_invoice/views/report_invoice.xml +++ b/addons/l10n_gcc_invoice/views/report_invoice.xml @@ -1,20 +1,5 @@ - - - - - - - - - - Commercial Invoice account.move