Search code examples
pythonpython-3.xodooodoo-12

Is it possible to print a report from another model?


I'm developing a module to calculate salaries.

Once all salaries are calculated, in 'hr.contract' form, I will have an option to print a report.

This report has to be the same report that I can print in 'hr.payslip' form.

So, the question is if it's possible to do this. I've tried but with bad results.

I've tried this:

class HrContract(models.Model):
    _inherit = 'hr.contract'

    def print_nominee_report(self):
        # I get this values from another methods,
        # I put 1 and 20 just to avoid confution in the question.
        run_id = 1
        indicador_id = 20

        # this method generate a payslip from which I want the report.
        payslip = self.generate_payslip(run_id, self.employee_id.id, indicador_id, self.id) 
        ids = [payslip.id]
        data = {
            'ids': ids,
            'model': 'hr.payslip',
            'form': self.env['hr.payslip'].search([('id', '=', payslip.id)])
        }
        return self.env.ref('hr_payroll.action_report_payslip').report_action(self, data=data)

but the result is an empty PDF.


Solution

  • Figure it out myself.

    First of all, I made the fuction to print the report on the original model 'hr.payslip':

    class HrPayslip(models.Model):
        _inherit = 'hr.payslip'
    
        def print_nominee_report(self):
            return self.env.ref('hr_payroll.action_report_payslip').report_action(self)
    

    Then, from hr.contract, I call the function to print the report using the payslip instance:

    class HrContract(models.Model):
        _inherit = 'hr.contract'
    
        def print_nominee_report(self):
            # I get this values from another methods,
            # I put 1 and 20 just to avoid confution in the question.
            run_id = 1
            indicador_id = 20
    
            payslip = self.generate_payslip(run_id, self.employee_id.id, indicador_id, self.id) 
            return payslip.print_nominee_report()