Files
odoo_source/addons/hr_payroll/controllers/main.py
T
jbm-odoo dbf7257971 [IMP] hr_payroll: Add a multi-report printing mechanism
Purpose
=======

We can't have only one report template for all the payslips. Some things
are specific to belgium, or another country. (For example, display the
amount on meal vouchers on the payslip). We should add an multi action,
and a button on the payslip form to print it.

Add a report field on a salary structure. This report is used on printing.

That way we can specify another report for a given salary structure and
print all the document of a employee with the correct report on each
payslip.

closes odoo/odoo#29674
2019-01-03 10:12:36 +00:00

51 lines
1.8 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import io
import re
from PyPDF2 import PdfFileReader, PdfFileWriter
from odoo.http import request, route, Controller
from odoo.tools.safe_eval import safe_eval
class HrPayroll(Controller):
@route(["/print/payslips"], type='http', auth='user')
def get_payroll_report_print(self, list_ids='', **post):
if not request.env.user.has_group('hr_payroll.group_hr_payroll_user') or not list_ids or re.search("[^0-9|,]", list_ids):
return request.not_found()
ids = [int(s) for s in list_ids.split(',')]
payslips = request.env['hr.payslip'].browse(ids)
pdf_writer = PdfFileWriter()
for payslip in payslips:
if not payslip.struct_id or not payslip.struct_id.report_id:
continue
pdf_content, _ = payslip.struct_id.report_id.render_qweb_pdf(payslip.id)
reader = PdfFileReader(io.BytesIO(pdf_content), strict=False, overwriteWarnings=False)
for page in range(reader.getNumPages()):
pdf_writer.addPage(reader.getPage(page))
_buffer = io.BytesIO()
pdf_writer.write(_buffer)
merged_pdf = _buffer.getvalue()
_buffer.close()
if len(payslips) == 1 and payslips.struct_id.report_id.print_report_name:
report_name = safe_eval(payslips.struct_id.report_id.print_report_name, {'object': payslips})
else:
report_name = "Payslips"
pdfhttpheaders = [
('Content-Type', 'application/pdf'),
('Content-Length', len(merged_pdf)),
('Content-Disposition', 'attachment; filename=' + report_name + '.pdf;')
]
return request.make_response(merged_pdf, headers=pdfhttpheaders)