Skip to Content
Menu
This question has been flagged
1 Reply
3751 Views

How could i modify this function to make the field overtime_hours computed field and it depends of check_in and check_out fields of another model (hr.attendance) . Any idea for help please ? i'm stuck with this problem

class BtHrOvertime(models.Model):
employee_id = fields.Many2one('hr.employee', string="Employee")
manager_id = fields.Many2one('hr.employee', string='Manager')
start_date = fields.Datetime('Date')
overtime_hours = fields.Float('Overtime Hours')
notes = fields.Text(string='Notes')
state = fields.Selection([('draft', 'Draft'), ('confirm', 'Waiting Approval'), ('refuse', 'Refused'),
       ('validate', 'Approved'), ('cancel', 'Cancelled')], default='draft', copy=False)
attendance_id = fields.Many2one('hr.attendance', string='Attendance')
@api.model
def run_overtime_scheduler(self):
    """ This Function is called by scheduler. """
    current_date = date.today()
    working_hours_empl = self.env['hr.contract']
    attend_signin_ids = self.env['hr.attendance'].search([('overtime_created', '=', False)])
    for obj in attend_signin_ids:
        if obj.check_in and obj.check_out:
            start_date = datetime.datetime.strptime(obj.check_in, DEFAULT_SERVER_DATETIME_FORMAT)
            end_date = datetime.datetime.strptime(obj.check_out, DEFAULT_SERVER_DATETIME_FORMAT) 
            difference = end_date - start_date
            hour_diff = str(difference).split(':')[0]
            min_diff = str(difference).split(':')[1]
            tot_diff = hour_diff + '.' + min_diff
            actual_working_hours = float(tot_diff)
            contract_obj = self.env['hr.contract'].search([('employee_id', '=', obj.employee_id.id),('work_hours','!=',0)])
            for contract in contract_obj:
                working_hours = contract.work_hours
                if actual_working_hours > working_hours:
                    overtime_hours = actual_working_hours - working_hours
                    vals = {
                        'employee_id':obj.employee_id and obj.employee_id.id or False,
                        'manager_id' : obj.employee_id and obj.employee_id.parent_id and obj.employee_id.parent_id.id or False,
                        'start_date' : obj.check_in,
                        'overtime_hours': round(overtime_hours,2),
                        'attendance_id': obj.id,
                        }
                    self.env['bt.hr.overtime'].create(vals)
                    obj.overtime_created = True


Avatar
Discard
Best Answer

Hello,

As per my knowledge, there is one field named "worked_hours" in hr.attendance which computes the working hours of an employee, so you can directly use that field for getting the working hours of employee. No need to do such code for retrieving the actual_working_hours

so you can simply create the "overtime_hours" field as a computed field and create one function like,

@api.depends('attendance_id')
def _compute_overtime_hours(self):

actual_working_hours = self.attendance_id.worked_hours

// after getting the actual_working_hours you can simply do your code here.



Avatar
Discard
Related Posts Replies Views Activity
2
Feb 24
13200
1
Dec 22
3863
2
Dec 22
12725
2
Jun 22
4688
2
Jun 22
3505