refactor(overtime-approval): 重构加班审批弹性兑换规则
1. 新增弹性扣减午餐时长配置项,优化晚到扣减逻辑 2. 统一排班开始时间作为上班时间优先级,替代硬编码09:00 3. 替换总自然时长校验为实际在岗时长计算 4. 更新文档与代码注释,对齐业务规则变更
This commit is contained in:
@@ -82,6 +82,10 @@ PUNCH_RECORD_URL = f"{HR_BASE_URL}/RedseaPlatform/redmagicapi/rf_s_kq_data_query
|
||||
WORK_START_TIME = "09:00"
|
||||
WORK_END_TIME = "18:00"
|
||||
FLEX_TRIGGER_TIME = "23:00"
|
||||
# 次日弹性上班扣减:次日打卡晚于该时刻(13:00)视为跨过中午休息,扣除午餐时长 LUNCH_BREAK_HOURS
|
||||
FLEX_LATE_LUNCH_TRIGGER_TIME = "13:00"
|
||||
# 次日弹性上班扣减时扣除的中午休息时长(小时)
|
||||
LUNCH_BREAK_HOURS = 1
|
||||
OT_MIN_HOURS = 1
|
||||
# 跨天打卡判定:次日首条打卡时间早于该时刻(6点),视为归属加班当天的跨天有效打卡
|
||||
CROSS_DAY_BEFORE_HOUR = 6
|
||||
@@ -1134,7 +1138,7 @@ def _check_punch_records(session, staff_id, ot_date):
|
||||
if not ot_date or not staff_id:
|
||||
return {"当日最早打卡": None, "当日最晚打卡": None, "次日最早打卡": None,
|
||||
"前一天最晚打卡": None, "班次开始时间": None, "班次结束时间": None,
|
||||
"打卡备注": "缺少加班日期或员工ID"}
|
||||
"次日班次开始时间": None, "打卡备注": "缺少加班日期或员工ID"}
|
||||
|
||||
start = datetime.strptime(ot_date, "%Y-%m-%d")
|
||||
prev_day = (start - timedelta(days=1)).strftime("%Y-%m-%d")
|
||||
@@ -1148,7 +1152,7 @@ def _check_punch_records(session, staff_id, ot_date):
|
||||
if not punches:
|
||||
return {"当日最早打卡": None, "当日最晚打卡": None, "次日最早打卡": None,
|
||||
"前一天最晚打卡": None, "班次开始时间": None, "班次结束时间": None,
|
||||
"打卡备注": "当天及次日无打卡记录"}
|
||||
"次日班次开始时间": None, "打卡备注": "当天及次日无打卡记录"}
|
||||
# 按考勤归属日 bc_date 分组;time_order:1=上班打卡、2=下班打卡
|
||||
arrivals = {}
|
||||
checkouts = {}
|
||||
@@ -1166,6 +1170,8 @@ def _check_punch_records(session, staff_id, ot_date):
|
||||
# 班次起止时间仍由考勤逐日接口提供;未获取到时由上层回退默认 09:00/18:00
|
||||
daily = get_attendance_daily(session, staff_id, prev_day, next_day)
|
||||
shift_begin, shift_end = _extract_shift_times(daily, ot_date)
|
||||
# 次日班次开始时间:弹性到岗兑换计算晚到时长时,次日上班时间优先取排班开始时间
|
||||
next_shift_begin, _ = _extract_shift_times(daily, next_day)
|
||||
|
||||
def _sorted(date_str):
|
||||
return sorted(all_by_date.get(date_str, []))
|
||||
@@ -1190,13 +1196,14 @@ def _check_punch_records(session, staff_id, ot_date):
|
||||
"前一天最晚打卡": prev_last,
|
||||
"班次开始时间": shift_begin,
|
||||
"班次结束时间": shift_end,
|
||||
"次日班次开始时间": next_shift_begin,
|
||||
"打卡备注": "",
|
||||
}
|
||||
except Exception as e:
|
||||
logger.warning("获取打卡明细失败: %s", str(e))
|
||||
return {"当日最早打卡": None, "当日最晚打卡": None, "次日最早打卡": None,
|
||||
"前一天最晚打卡": None, "班次开始时间": None, "班次结束时间": None,
|
||||
"打卡备注": f"获取失败: {e}"}
|
||||
"次日班次开始时间": None, "打卡备注": f"获取失败: {e}"}
|
||||
|
||||
|
||||
def _check_leave_covers_morning(session, staff_id, date_str):
|
||||
@@ -1207,7 +1214,15 @@ def _check_leave_covers_morning(session, staff_id, date_str):
|
||||
logger.warning("获取请假记录失败: %s", str(e))
|
||||
return False
|
||||
|
||||
morning = datetime.strptime(f"{date_str} {WORK_START_TIME}", "%Y-%m-%d %H:%M")
|
||||
# 上班时间优先取排班开始时间,未获取到排班时段时回退默认 09:00
|
||||
shift_begin = None
|
||||
try:
|
||||
daily = get_attendance_daily(session, staff_id, date_str, date_str)
|
||||
shift_begin, _ = _extract_shift_times(daily, date_str)
|
||||
except Exception as e:
|
||||
logger.warning("获取排班时段失败(%s): %s", date_str, str(e))
|
||||
morning_time = shift_begin or WORK_START_TIME
|
||||
morning = datetime.strptime(f"{date_str} {morning_time}", "%Y-%m-%d %H:%M")
|
||||
for r in records:
|
||||
start = _parse_dt(r.get("start_time"))
|
||||
end = _parse_dt(r.get("end_time"))
|
||||
@@ -1320,7 +1335,9 @@ def _validate_delayed(info, punch, session, staff_id):
|
||||
elif _check_leave_covers_morning(session, staff_id, next_day):
|
||||
flex_note = f"次日({next_day})请假覆盖早上上班时间,不触发弹性兑换"
|
||||
else:
|
||||
work_start = datetime.strptime(f"{next_day} {WORK_START_TIME}", "%Y-%m-%d %H:%M")
|
||||
# 次日上班时间优先取排班开始时间,未获取到排班时段时回退默认 09:00
|
||||
next_shift_begin = punch.get("次日班次开始时间") or WORK_START_TIME
|
||||
work_start = datetime.strptime(f"{next_day} {next_shift_begin}", "%Y-%m-%d %H:%M")
|
||||
p = _parse_dt(punch.get("次日最早打卡"))
|
||||
|
||||
if p is None:
|
||||
@@ -1328,28 +1345,41 @@ def _validate_delayed(info, punch, session, staff_id):
|
||||
elif p <= work_start:
|
||||
flex_note = f"次日打卡({p:%H:%M})准时或提前,无需兑换"
|
||||
else:
|
||||
total_natural = (p - start).total_seconds() / 3600
|
||||
# 有晚到,校验:加班时长 + 晚到 <= 实际在岗时长(按系统取整)
|
||||
# 实际在岗时长 = 加班开始->实际下班打卡 按延时取整(1h起算0.5h累加)
|
||||
actual_work = _calc_delayed_hours(start, actual_checkout)
|
||||
lunch_trigger = datetime.strptime(
|
||||
f"{next_day} {FLEX_LATE_LUNCH_TRIGGER_TIME}", "%Y-%m-%d %H:%M"
|
||||
)
|
||||
|
||||
if p <= work_start + timedelta(hours=d_hours):
|
||||
late_hours = max(1, math.ceil((p - work_start).total_seconds() / 3600))
|
||||
if hours + late_hours > total_natural:
|
||||
info["弹性扣减(小时)"] = 0
|
||||
info["有效加班时长(小时)"] = hours
|
||||
info["弹性额度D(小时)"] = d_hours
|
||||
return "驳回", (
|
||||
f"弹性兑换校验驳回:申请加班{hours:g}h + 晚到{late_hours}h "
|
||||
f"> 总自然时长{total_natural:.1f}h"
|
||||
)
|
||||
deduction = float(late_hours)
|
||||
flex_note = f"次日打卡({p:%H:%M})晚到,兑换{late_hours}小时"
|
||||
# T < P <= T+D:晚到时长 = P-T,次日打卡超过13:00时
|
||||
# 再扣1小时午餐,按1小时起算向上取整(不足1小时不扣)
|
||||
late_raw = (p - work_start).total_seconds() / 3600
|
||||
if p > lunch_trigger:
|
||||
late_raw -= LUNCH_BREAK_HOURS
|
||||
late_hours = math.ceil(late_raw) if late_raw >= OT_MIN_HOURS else 0
|
||||
if late_hours < 1:
|
||||
flex_note = f"次日打卡({p:%H:%M})晚到不足1小时,不触发弹性兑换"
|
||||
else:
|
||||
if hours + late_hours > actual_work:
|
||||
info["弹性扣减(小时)"] = 0
|
||||
info["有效加班时长(小时)"] = hours
|
||||
info["弹性额度D(小时)"] = d_hours
|
||||
return "驳回", (
|
||||
f"弹性兑换校验驳回:申请加班{hours:g}h + 晚到{late_hours}h "
|
||||
f"> 实际在岗时长{actual_work:g}h"
|
||||
)
|
||||
deduction = float(late_hours)
|
||||
flex_note = f"次日打卡({p:%H:%M})晚到,兑换{late_hours}小时"
|
||||
else:
|
||||
if hours + d_hours > total_natural:
|
||||
if hours + d_hours > actual_work:
|
||||
info["弹性扣减(小时)"] = 0
|
||||
info["有效加班时长(小时)"] = hours
|
||||
info["弹性额度D(小时)"] = d_hours
|
||||
return "驳回", (
|
||||
f"弹性兑换校验驳回:申请加班{hours:g}h + 兑换额度{d_hours:g}h "
|
||||
f"> 总自然时长{total_natural:.1f}h"
|
||||
f"> 实际在岗时长{actual_work:g}h"
|
||||
)
|
||||
deduction = float(d_hours)
|
||||
flex_note = (
|
||||
|
||||
Reference in New Issue
Block a user