Odoo Mindscape. Understand Odoo faster. Explore modules, fields, dependencies and version differences in one place instead of digging through the source code.

Module: Attendances

Entity: hr.employee

Compute Method: _compute_hours_today

View 17.0 View 19.0
Inputs Method Outputs
  • tz
  • id
  • check_out
  • check_in
now = fields.Datetime.now()
now_utc = pytz.utc.localize(now)
- for employee in self:
+ for timezone, employees in self.grouped('tz').items():
            # start of day in the employee's timezone might be the previous day in utc
-             tz = pytz.timezone(employee.tz)
-             now_tz = now_utc.astimezone(tz)
-             start_tz = now_tz + relativedelta(hour=0, minute=0)  # day start in the employee's timezone
+             tz = pytz.timezone(timezone or 'UTC')
+             start_tz = now_utc.astimezone(tz) + relativedelta(hour=0, minute=0)  # day start in the employee's timezone
            start_naive = start_tz.astimezone(pytz.utc).replace(tzinfo=None)

-             attendances = self.env['hr.attendance'].search([
-                 ('employee_id', '=', employee.id),
-                 ('check_in', '<=', now),
-                 '|', ('check_out', '>=', start_naive), ('check_out', '=', False),
-             ], order='check_in asc')
-             hours_previously_today = 0
-             worked_hours = 0
-             attendance_worked_hours = 0
-             for attendance in attendances:
-                 delta = (attendance.check_out or now) - max(attendance.check_in, start_naive)
-                 attendance_worked_hours = delta.total_seconds() / 3600.0
-                 worked_hours += attendance_worked_hours
-                 hours_previously_today += attendance_worked_hours
-             employee.last_attendance_worked_hours = attendance_worked_hours
-             hours_previously_today -= attendance_worked_hours
-             employee.hours_previously_today = hours_previously_today
-             employee.hours_today = worked_hours
+             attendances_by_employee = dict(self.env['hr.attendance']._read_group(
+                 [
+                     ('employee_id', 'in', employees.ids),
+                     ('check_in', '<=', now),
+                     '|', ('check_out', '>=', start_naive), ('check_out', '=', False),
+                 ],
+                 ['employee_id'],
+                 ['id:recordset'],
+             ))
+ 
+             for employee in employees:
+                 attendances = attendances_by_employee.get(employee, self.env['hr.attendance'])
+                 hours_previously_today = 0
+                 worked_hours = 0
+                 attendance_worked_hours = 0
+                 for attendance in attendances:
+                     delta = (attendance.check_out or now) - max(attendance.check_in, start_naive)
+                     attendance_worked_hours = delta.total_seconds() / 3600.0
+                     worked_hours += attendance_worked_hours
+                     hours_previously_today += attendance_worked_hours
+                 employee.last_attendance_worked_hours = attendance_worked_hours
+                 hours_previously_today -= attendance_worked_hours
+                 employee.hours_previously_today = hours_previously_today
+                 employee.hours_today = worked_hours
  • last_attendance_worked_hours
  • hours_previously_today
  • hours_today

Back to entity