Calculate total working hours for employee in odoo

Viewed 28
class AttendanceEmp(models.Model):
    _name = 'shift.log'
    _description = 'Attendance'

    sh_id = fields.Many2one(comodel_name='hr.employee',string='emp id')
    checkin_or_checkout = fields.Datetime(string="start or end time", required=True)
    check_in = fields.Selection([('1','True'),('2','False')])

    def log_details_calculate(self):
        pass

this is my model and in this model check-in that return true and checkout that return false and i have added some employee check-in and checkout manually now i want to create logic that calculate any particular employee working hours. how can i do that to calculate total working hours for employee

1 Answers

It would be a better idea to have 2 different fields for check_in and check_out and store the difference as a computed field:

   from odoo.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT, pycompat
   
   
   class AttendanceEmp(models.Model):
       _name = 'shift.log'
       _description = 'Attendance'
   
       sh_id = fields.Many2one(comodel_name='hr.employee',string='emp id')
       check_in = fields.Datetime(string="start time")
       check_out = fields.Datetime(string="end time")
       working_hours = fields.Float(string="Hours", compute='_compute_working_hours')
   
       #working_hours = fields.Integer(string="Hours", compute='_compute_working_hours')
   
       def _compute_working_hours(self):
           for record in self:
               work_start_dt = datetime.datetime.strptime(record.check_in, DEFAULT_SERVER_DATETIME_FORMAT)
               work_end_dt = datetime.datetime.strptime(record.check_out, DEFAULT_SERVER_DATETIME_FORMAT)
               record.working_hours = (work_end_dt - work_start_dt).minutes/60
               
               #record.working_hours = (work_end_dt - work_start_dt).hours

Related