March 11 2026: changed regular & special work mixing calculation issues 1

This commit is contained in:
2026-03-11 13:16:55 -04:00
parent 802f9cbf9d
commit b394a2559c
+94 -1
View File
@@ -9386,6 +9386,19 @@ def export_time_attendance_excel(records, project_name_for_filename, date_range_
else:
return None
# Enforce maximum 2-week (14-day) export window.
# If the selected range exceeds 14 days, cap end_date to start_date + 13 days.
MAX_EXPORT_DAYS = 14
if (end_date - start_date).days >= MAX_EXPORT_DAYS:
capped_end_date = start_date + timedelta(days=MAX_EXPORT_DAYS - 1)
logger_handler.logger.info(
f"TA Excel export: date range [{start_date} {end_date}] exceeds {MAX_EXPORT_DAYS} days; "
f"capping end_date to {capped_end_date}."
)
end_date = capped_end_date
# Drop records that fall outside the capped window
records = [r for r in records if r.attendance_date <= end_date]
# Import parse function at the beginning for work type detection
from working_hours_calculator import parse_employee_id_for_work_type
@@ -9976,8 +9989,51 @@ def export_time_attendance_excel(records, project_name_for_filename, date_range_
})
print(f" Record at {record.check_in_time}: action='{record.action_description}', is_out={is_out}")
# Create pairs based on IN -> OUT logic
# Odd-IN rule: if there are more INs than OUTs in this building group,
# discard all but the LATEST IN — those earlier INs become Missed Punch rows.
# The latest IN is then paired with the earliest OUT.
ins = [ri for ri in record_info if not ri['is_out']]
outs = [ri for ri in record_info if ri['is_out']]
pairs_to_write = []
if len(ins) > len(outs) and len(outs) > 0:
# Sort INs by time ascending; keep only the last one for pairing
ins_sorted = sorted(ins, key=lambda ri: ri['record'].check_in_time)
latest_in = ins_sorted[-1]
excess_ins = ins_sorted[:-1] # all earlier INs become Missed Punch
# Mark excess INs as used and emit Missed Punch rows
for ri in excess_ins:
ri['used'] = True
pairs_to_write.append({
'check_in': ri['record'],
'check_out': None,
'is_miss_punch': True
})
# Pair latest IN with earliest OUT
outs_sorted = sorted(outs, key=lambda ri: ri['record'].check_in_time)
earliest_out = outs_sorted[0]
latest_in['used'] = True
earliest_out['used'] = True
pairs_to_write.append({
'check_in': latest_in['record'],
'check_out': earliest_out['record'],
'is_miss_punch': False
})
# Any remaining unused OUTs after the first become Missed Punch
for ri in outs_sorted[1:]:
ri['used'] = True
pairs_to_write.append({
'check_in': None,
'check_out': ri['record'],
'is_miss_punch': True
})
else:
# Standard pairing: each IN finds the next available OUT
i = 0
while i < len(record_info):
if record_info[i]['used']:
@@ -10376,6 +10432,17 @@ def export_time_attendance_by_building_excel(records, project_name_for_filename,
else:
return None
# Enforce maximum 2-week (14-day) export window.
MAX_EXPORT_DAYS = 14
if (end_date - start_date).days >= MAX_EXPORT_DAYS:
capped_end_date = start_date + timedelta(days=MAX_EXPORT_DAYS - 1)
logger_handler.logger.info(
f"TA by-building Excel export: date range [{start_date} {end_date}] exceeds {MAX_EXPORT_DAYS} days; "
f"capping end_date to {capped_end_date}."
)
end_date = capped_end_date
records = [r for r in records if r.attendance_date <= end_date]
# Import parse function for work type detection
from working_hours_calculator import parse_employee_id_for_work_type
@@ -10638,6 +10705,32 @@ def export_time_attendance_by_building_excel(records, project_name_for_filename,
# Create pairs
pairs = []
ins = [ri for ri in record_info if not ri['is_out']]
outs = [ri for ri in record_info if ri['is_out']]
if len(ins) > len(outs) and len(outs) > 0:
# Odd-IN rule: discard all but the LATEST IN; pair it with the earliest OUT
ins_sorted = sorted(ins, key=lambda ri: ri['record'].check_in_time)
outs_sorted = sorted(outs, key=lambda ri: ri['record'].check_in_time)
latest_in = ins_sorted[-1]
excess_ins = ins_sorted[:-1]
earliest_out = outs_sorted[0]
for ri in excess_ins:
ri['used'] = True
pairs.append({'check_in': ri['record'], 'check_out': None, 'is_miss_punch': True})
latest_in['used'] = True
earliest_out['used'] = True
pairs.append({'check_in': latest_in['record'], 'check_out': earliest_out['record'], 'is_miss_punch': False})
for ri in outs_sorted[1:]:
ri['used'] = True
pairs.append({'check_in': None, 'check_out': ri['record'], 'is_miss_punch': True})
else:
# Standard pairing
i = 0
while i < len(record_info):
if record_info[i]['used']: