|
|
now = fields.Datetime.now()
now_utc = now.replace(tzinfo=datetime.UTC)
for timezone, employees in self.grouped('tz').items():
# start of day in the employee's timezone might be the previous day in utc
tz = ZoneInfo(timezone or 'UTC')
start_tz = now_utc.astimezone(tz).replace(hour=0, minute=0, second=0, microsecond=0)
start_naive = start_tz.astimezone(datetime.UTC).replace(tzinfo=None)
end_naive = (start_tz + relativedelta(days=1)).astimezone(datetime.UTC).replace(tzinfo=None)
start_local = start_tz.replace(tzinfo=None)
now_local = now_utc.astimezone(tz).replace(tzinfo=None)
today_interval = Intervals([(start_tz, now_utc, self.env['resource.calendar'])])
attendances_by_employee = dict(self.env['hr.attendance']._read_group(
[
('employee_id', 'in', employees.ids),
('check_in', '<', end_naive),
'|', ('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'])
employee.today_attendance_ids = attendances
worked_hours = 0
last_attendance_worked_hours = 0
for attendance in attendances:
check_in = attendance.check_in.replace(tzinfo=datetime.UTC)
check_out = (attendance.check_out or now).replace(tzinfo=datetime.UTC)
attendance_interval = Intervals([(check_in, check_out, attendance)]) & today_interval
attendance_worked_hours = (
sum_intervals(attendance_interval)
- attendance._get_break_duration_within_period(start_local, now_local)
)
worked_hours += attendance_worked_hours
if attendance == employee.last_attendance_id:
last_attendance_worked_hours = attendance_worked_hours
employee.last_attendance_worked_hours = last_attendance_worked_hours
employee.hours_previously_today = worked_hours - last_attendance_worked_hours
employee.hours_today = worked_hours
|
|