Skip to Content
Menu
This question has been flagged
2 Replies
5321 Views

I have python function which I want it to fill one2many field for all payslip records. Onchange method works only when I change employee_id for already created records, doesn't work for when I create new record. I need to create, save and then edit.

How can I make it work for also new records(payslips)?

all_project_hours = fields.One2many('hr.payslip.projects', 'slip_id', "Project Hours")

@api.onchange('employee_id')
def _calculate_project_hours(self):
    all_project_hours = []
    value = {}
    projects = self.env['project.project'].search([])
    for project in projects:
        domain = [('employee_id', '=', self.employee_id.name), ('date', '>=', self.date_from),
                  ('date', '<=', self.date_to),
                  ('validated', '=', True), ('is_bonus_eligible', '=', True), ('project_id', '=', project.name)]
        domain_ot = [('employee_id', '=', self.employee_id.name), ('date', '>=', self.date_from),
                     ('date', '<=', self.date_to),
                     ('validated', '=', True), ('is_bonus_eligible', '=', True), ('project_id', '=', project.name),
                     ('task_id', '=', 'Overtime')]
        all_timesheets = self.env["account.analytic.line"].search(domain)
        ot_timesheets = self.env["account.analytic.line"].search(domain_ot)
        sum_all = 0.0
        sum_ot = 0.0
        for unit in all_timesheets:
            sum_all += unit.unit_amount
        for ot in ot_timesheets:
            sum_ot += ot.unit_amount
        if self.total_project_hours > 0.0:
            split = sum_all / self.total_project_hours * 100
        else:
            split = 0.0
            return split
        all_project_hours.append(
            (0, 0, {'project_id': project.id, 'project_hours': sum_all, 'overtime_hours': sum_ot, 'project_split': split}))
    value.update(all_project_hours=all_project_hours)
    return {'value': value}
Avatar
Discard
Best Answer

Hi,

You should try to add more fields in @api.onchange() declaration, for example:

@api.onchange('employee_id', 'employee_id.name', 'date_from', 'date_to')

Avatar
Discard
Related Posts Replies Views Activity
1
Mar 20
3056
2
Jun 22
16489
0
Mar 22
1284
1
Jan 22
2139
5
Jun 20
30128