Search code examples
pythonpython-2.7for-loopormodoo

why the computed method work on only one record?


i did this method which compute the late hours of an employee and convert it at days in dedication filed on payslip, it working well but when there is more than one payslip and i need to get the sumition of dedication field of all payslips for an employee it give me just one record and return other records = 0.00 , here the methode

@api.multi
@api.depends('date_to','date_to','employee_id')
def compute_amount(self):
    for rec in self:
        rec.attend = self.env['hr.attendance'].search(
            [('check_in', '>=', rec.date_from), ('check_out', '<=', rec.date_to),
             ('employee_id', '=', rec.employee_id.id), ])
        late=[float(s.late) for s in rec.attend]
        tot_late= float(sum(late))
    rec.dedication = tot_late / 8
    pass

dedication = fields.Float(string="Dedication",  required=False,compute=compute_amount )

if i try to print all record i get a list like this [0.0, 0.0, 1.7551388888888888] if there is 3 payslips the list return only one record with the real value and other record = 0

any help will be apriciated


Solution

  • It's probably because the rec.dedication = tot_late / 8 is outside the for rec in self loop. Which means the value is only set on the last record it computes. Also, the pass value seems unnecessary here.