ir_actions_report.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # -*- coding: utf-8 -*-
  2. from collections import OrderedDict
  3. from zlib import error as zlib_error
  4. try:
  5. from PyPDF2.errors import PdfStreamError, PdfReadError
  6. except ImportError:
  7. from PyPDF2.utils import PdfStreamError, PdfReadError
  8. from odoo import models, _
  9. from odoo.exceptions import UserError
  10. from odoo.tools import pdf
  11. class IrActionsReport(models.Model):
  12. _inherit = 'ir.actions.report'
  13. def _render_qweb_pdf_prepare_streams(self, report_ref, data, res_ids=None):
  14. # Custom behavior for 'account.report_original_vendor_bill'.
  15. if self._get_report(report_ref).report_name != 'account.report_original_vendor_bill':
  16. return super()._render_qweb_pdf_prepare_streams(report_ref, data, res_ids=res_ids)
  17. invoices = self.env['account.move'].browse(res_ids)
  18. original_attachments = invoices.message_main_attachment_id
  19. if not original_attachments:
  20. raise UserError(_("No original purchase document could be found for any of the selected purchase documents."))
  21. collected_streams = OrderedDict()
  22. for invoice in invoices:
  23. attachment = invoice.message_main_attachment_id
  24. if attachment:
  25. stream = pdf.to_pdf_stream(attachment)
  26. if stream:
  27. record = self.env[attachment.res_model].browse(attachment.res_id)
  28. try:
  29. stream = pdf.add_banner(stream, record.name, logo=True)
  30. except (ValueError, PdfStreamError, PdfReadError, TypeError, zlib_error, NotImplementedError):
  31. record._message_log(body=_(
  32. "There was an error when trying to add the banner to the original PDF.\n"
  33. "Please make sure the source file is valid."
  34. ))
  35. collected_streams[invoice.id] = {
  36. 'stream': stream,
  37. 'attachment': attachment,
  38. }
  39. return collected_streams
  40. def _render_qweb_pdf(self, report_ref, res_ids=None, data=None):
  41. # Check for reports only available for invoices.
  42. # + append context data with the display_name_in_footer parameter
  43. if self._get_report(report_ref).report_name in ('account.report_invoice_with_payments', 'account.report_invoice'):
  44. invoices = self.env['account.move'].browse(res_ids)
  45. if self.env['ir.config_parameter'].sudo().get_param('account.display_name_in_footer'):
  46. data = data and dict(data) or {}
  47. data.update({'display_name_in_footer': True})
  48. if any(x.move_type == 'entry' for x in invoices):
  49. raise UserError(_("Only invoices could be printed."))
  50. return super()._render_qweb_pdf(report_ref, res_ids=res_ids, data=data)