From 4c06b3c1180048b1c0db5067adf0c0bef5c02785 Mon Sep 17 00:00:00 2001
From: oco-odoo <oco@odoo.com>
Date: Mon, 13 Sep 2021 12:20:52 +0000
Subject: [PATCH] [FIX] account: Don't generate pdf at all when trying to print
 a misc operation from the 'print invoices' actions

Before, clicking on 'print invoices' or 'print invoices without payment' on a posted misc operation raised an error message saying that only invoices could be printed. However, the pdf file still got generated and could be found as attachement on the move. With this commit, we don't generate the file anymore in this case.

closes odoo/odoo#76419

Signed-off-by: Laurent Smet <smetl@users.noreply.github.com>
---
 addons/account/models/account_move.py      |  2 --
 addons/account/models/ir_actions_report.py | 12 ++++++++++++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py
index 2e981c209790..92873a4b0ecb 100644
--- a/addons/account/models/account_move.py
+++ b/addons/account/models/account_move.py
@@ -2543,8 +2543,6 @@ class AccountMove(models.Model):
             move.write({'invoice_line_ids' : new_invoice_line_ids})
 
     def _get_report_base_filename(self):
-        if any(not move.is_invoice() for move in self):
-            raise UserError(_("Only invoices could be printed."))
         return self._get_move_display_name()
 
     def preview_invoice(self):
diff --git a/addons/account/models/ir_actions_report.py b/addons/account/models/ir_actions_report.py
index 78b21f0a9f9e..c607a8fe8000 100644
--- a/addons/account/models/ir_actions_report.py
+++ b/addons/account/models/ir_actions_report.py
@@ -33,3 +33,15 @@ class IrActionsReport(models.Model):
             if attachment:
                 attachment.register_as_main_attachment(force=False)
         return res
+
+    def render_qweb_pdf(self, res_ids=None, data=None):
+        # Overridden so that the print > invoices actions raises an error
+        # when trying to print a miscellaneous operation instead of an invoice.
+        if self.model == 'account.move' and res_ids:
+            invoice_reports = (self.env.ref('account.account_invoices_without_payment'), self.env.ref('account.account_invoices'))
+            if self in invoice_reports:
+                moves = self.env['account.move'].browse(res_ids)
+                if any(not move.is_invoice(include_receipts=True) for move in moves):
+                    raise UserError(_("Only invoices could be printed."))
+
+        return super().render_qweb_pdf(res_ids=res_ids, data=None)
-- 
GitLab