|
|
att_progress_values = dict()
negative_overtime_attendances = defaultdict(lambda: False)
if self.employee_id:
self.env['hr.attendance'].flush_model(['worked_hours'])
self.env['hr.attendance.overtime'].flush_model(['duration'])
self.env.cr.execute('''
WITH employee_time_zones AS (
SELECT employee.id AS employee_id,
calendar.tz AS timezone
FROM hr_employee employee
INNER JOIN resource_calendar calendar
ON calendar.id = employee.resource_calendar_id
)
SELECT att.id AS att_id,
att.worked_hours AS att_wh,
ot.id AS ot_id,
ot.duration AS ot_d,
ot.date AS od,
att.check_in AS ad
FROM hr_attendance att
INNER JOIN employee_time_zones etz
ON att.employee_id = etz.employee_id
INNER JOIN hr_attendance_overtime ot
ON date_trunc('day',
CAST(att.check_in
AT TIME ZONE 'utc'
AT TIME ZONE etz.timezone
as date)) = date_trunc('day', ot.date)
AND att.employee_id = ot.employee_id
AND att.employee_id IN %s
AND ot.adjustment IS false
ORDER BY att.check_in DESC
''', (tuple(self.employee_id.ids),))
a = self.env.cr.dictfetchall()
grouped_dict = dict()
for row in a:
if row['ot_id'] and row['att_wh']:
if row['ot_id'] not in grouped_dict:
grouped_dict[row['ot_id']] = {'attendances': [(row['att_id'], row['att_wh'])], 'overtime_duration': row['ot_d']}
else:
grouped_dict[row['ot_id']]['attendances'].append((row['att_id'], row['att_wh']))
for overtime in grouped_dict:
overtime_reservoir = grouped_dict[overtime]['overtime_duration']
if overtime_reservoir > 0:
for attendance in grouped_dict[overtime]['attendances']:
if overtime_reservoir > 0:
sub_time = attendance[1] - overtime_reservoir
if sub_time < 0:
att_progress_values[attendance[0]] = 0
overtime_reservoir -= attendance[1]
else:
att_progress_values[attendance[0]] = float(((attendance[1] - overtime_reservoir) / attendance[1]) * 100)
overtime_reservoir = 0
else:
att_progress_values[attendance[0]] = 100
elif overtime_reservoir < 0 and grouped_dict[overtime]['attendances']:
att_id = grouped_dict[overtime]['attendances'][0][0]
att_progress_values[att_id] = overtime_reservoir
negative_overtime_attendances[att_id] = True
for attendance in self:
if negative_overtime_attendances[attendance.id]:
attendance.overtime_hours = att_progress_values.get(attendance.id, 0)
else:
attendance.overtime_hours = attendance.worked_hours * ((100 - att_progress_values.get(attendance.id, 100)) / 100)
|
|