mirror of
https://github.com/bringout/oca-ocb-hr.git
synced 2026-04-25 17:52:08 +02:00
19.0 vanilla
This commit is contained in:
parent
a1137a1456
commit
e1d89e11e3
2789 changed files with 1093187 additions and 605897 deletions
|
|
@ -0,0 +1,54 @@
|
|||
import io
|
||||
from odoo import models, _
|
||||
from odoo.tools import pdf
|
||||
from odoo.tools.pdf import OdooPdfFileReader, OdooPdfFileWriter, PdfReadError, DependencyError
|
||||
|
||||
|
||||
class IrActionsReport(models.Model):
|
||||
_inherit = 'ir.actions.report'
|
||||
|
||||
def _render_qweb_pdf_prepare_streams(self, report_ref, data, res_ids=None):
|
||||
# OVERRIDE
|
||||
res = super()._render_qweb_pdf_prepare_streams(report_ref, data, res_ids)
|
||||
if not res_ids:
|
||||
return res
|
||||
report = self._get_report(report_ref)
|
||||
if report.report_name == 'hr_expense.report_expense':
|
||||
for expense in self.env['hr.expense'].browse(res_ids):
|
||||
# Will contains the expense
|
||||
stream_list = []
|
||||
stream = res[expense.id]['stream']
|
||||
stream_list.append(stream)
|
||||
attachments = self.env['ir.attachment'].search([('res_id', 'in', expense.ids), ('res_model', '=', 'hr.expense')])
|
||||
expense_report = OdooPdfFileReader(stream, strict=False)
|
||||
output_pdf = OdooPdfFileWriter()
|
||||
output_pdf.appendPagesFromReader(expense_report)
|
||||
for attachment in self._prepare_local_attachments(attachments):
|
||||
if attachment.mimetype == 'application/pdf':
|
||||
attachment_stream = pdf.to_pdf_stream(attachment)
|
||||
else:
|
||||
# In case the attachment is not a pdf we will create a new PDF from the template "report_expense_img"
|
||||
# And then append to the stream. By doing so, the attachment is put on a new page with the name of the expense
|
||||
# associated to the attachment
|
||||
data['attachment'] = attachment
|
||||
attachment_prep_stream = self._render_qweb_pdf_prepare_streams('hr_expense.report_expense_img', data, res_ids=res_ids)
|
||||
attachment_stream = attachment_prep_stream[expense.id]['stream']
|
||||
attachment_reader = OdooPdfFileReader(attachment_stream, strict=False)
|
||||
try:
|
||||
output_pdf.appendPagesFromReader(attachment_reader)
|
||||
except (PdfReadError, DependencyError) as e:
|
||||
expense._message_log(body=_(
|
||||
"The attachment (%(attachment_name)s) has not been added to the report due to the following error: '%(error)s'",
|
||||
attachment_name=attachment.name,
|
||||
error=e
|
||||
))
|
||||
continue
|
||||
stream_list.append(attachment_stream)
|
||||
|
||||
new_pdf_stream = io.BytesIO()
|
||||
output_pdf.write(new_pdf_stream)
|
||||
res[expense.id]['stream'] = new_pdf_stream
|
||||
|
||||
for stream in stream_list:
|
||||
stream.close()
|
||||
return res
|
||||
Loading…
Add table
Add a link
Reference in a new issue