Debian Codegraph @

addons/account_edi/models/ir_actions_report.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 import io
4
5 from odoo import models
6 from odoo.tools.pdf import OdooPdfFileReader, OdooPdfFileWriter
7
8
9 class IrActionsReport(models.Model):
10 _inherit = 'ir.actions.report'
11
12 def _render_qweb_pdf_prepare_streams(self, report_ref, data, res_ids=None):
13 # EXTENDS base
14 collected_streams = super()._render_qweb_pdf_prepare_streams(report_ref, data, res_ids=res_ids)
15
16 if collected_streams \
17 and res_ids \
18 and len(res_ids) == 1 \
19 and self._is_invoice_report(report_ref):
20 invoice = self.env['account.move'].browse(res_ids)
21 if invoice.is_sale_document() and invoice.state != 'draft':
22 to_embed = invoice.edi_document_ids
23 # Add the attachments to the pdf file
24 if to_embed:
25 pdf_stream = collected_streams[invoice.id]['stream']
26
27 # Read pdf content.
28 pdf_content = pdf_stream.getvalue()
29 reader_buffer = io.BytesIO(pdf_content)
30 reader = OdooPdfFileReader(reader_buffer, strict=False)
31
32 # Post-process and embed the additional files.
33 writer = OdooPdfFileWriter()
34 writer.cloneReaderDocumentRoot(reader)
35 for edi_document in to_embed:
36 # The attachements on the edi documents are only system readable
37 # because they don't have res_id and res_model, here we are sure that
38 # the user has access to the invoice and edi document
39 edi_document.edi_format_id._prepare_invoice_report(writer, edi_document)
40
41 # Replace the current content.
42 pdf_stream.close()
43 new_pdf_stream = io.BytesIO()
44 writer.write(new_pdf_stream)
45 collected_streams[invoice.id]['stream'] = new_pdf_stream
46
47 return collected_streams