Files
37duAutomation/plugins/xq0010/logic/check2.py
T

84 lines
4.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""XQ0010 EHR系统月考勤异常处理 插件 - 检查2「休息日半天加班 × 打卡校验」(由影刀版移植)。
休息日加班(类型3)且加班天数为 0.5 天时,校验当日打卡是否按考勤班段正常打卡。
参考班段优先级:加班计划时段(PLAN_* 优先 FINAL_*,跨天 +1440)> 排班班段;
休息日半天放宽窗口(覆盖常规上/下半段 09:00~12:30 / 13:30~18:00)+ 打卡跨度 4 小时兜底;
判定四档:正常 / 提前打卡 / 晚到 / 漏打卡。
数据来源(按工号实时拉取,排班按工号缓存):
- 打卡:data.hr_api.fetch_all_kq_data(加班日 ~ 次日,跨天 0~6 点归入加班日)
- 排班:data.hr_api.fetch_all_shift(缓存整月,取加班日班段)
"""
from datetime import datetime, timedelta
from ..config import logger, HALF_DAY_RELAX_MORNING, HALF_DAY_RELAX_AFTERNOON
from ..data.hr_api import fetch_all_kq_data, fetch_all_shift
from .abnormal_utils import (
_get, _to_date_str, _staff_name, _is_half_day_ot, _punch_analysis,
_extract_day_shift, _ot_plan_min_range, _judge_punch_by_shift,
)
# 输出列(对应需求文档检查2)
OUTPUT_COLUMNS = [
"员工姓名", "工号", "加班日期", "加班天数", "排班班次", "排班班段时间", "计划时段",
"上班打卡", "下班打卡", "打卡次数", "判定",
]
def run_check2(session, user_id, treecode, rest_overtime, begin, end):
"""检查2:休息日半天加班当日打卡校验 → 返回校验行列表。
Args:
session / user_id / treecode: 底座注入的 HR 会话与用户信息。
rest_overtime: 已备案的休息日加班记录列表(检查2 只认 BPM_STATUS=002)。
begin / end: 排班查询月份范围 "YYYY-MM-DD"。
Returns:
校验行列表(每行为 OUTPUT_COLUMNS 对应字段的 dict)。
"""
rows = []
half_ots = [ot for ot in rest_overtime if _is_half_day_ot(ot)]
logger.info("休息日半天加班 %d 条,逐一校验打卡(结合排班班段)", len(half_ots))
# 按工号缓存排班记录,避免重复请求
shift_cache = {}
for ot in half_ots:
code = str(_get(ot, "base_code")).strip()
ot_date = _to_date_str(_get(ot, "OT_DATE", "ot_date"))
if not code or not ot_date:
continue
# 打卡查询范围扩到次日:加班可能跨天干到第二天凌晨(0~4点),
# 次日凌晨的打卡需纳入「次日4点前有卡即正常」判定。
next_day = (datetime.strptime(ot_date, "%Y-%m-%d") + timedelta(days=1)).strftime("%Y-%m-%d")
kq = fetch_all_kq_data(session, user_id, code, ot_date, next_day, treecode)
# 拉取该员工排班(缓存),取加班日的班段
if code not in shift_cache:
shift_cache[code] = fetch_all_shift(session, user_id, code, begin, end, treecode)
shift_info = _extract_day_shift(shift_cache.get(code, []), ot_date)
p = _punch_analysis(kq, base_date=ot_date)
# 打卡覆盖基准:优先加班计划时段(实际加班时段),排班班段仅兜底/展示
plan_range = _ot_plan_min_range(ot, ot_date)
# 休息日半天放宽窗口:不必严格按计划时段,覆盖常规上/下半段任一窗口即正常
relax_ranges = [HALF_DAY_RELAX_MORNING, HALF_DAY_RELAX_AFTERNOON]
judge = _judge_punch_by_shift(p, shift_info, plan_min_range=plan_range,
base_date=ot_date, relax_ranges=relax_ranges)
rows.append({
"员工姓名": _staff_name(ot),
"工号": code,
"加班日期": ot_date,
"加班天数": _get(ot, "ot_days", "sys_ot_days", "plan_ot_days", default="0.5"),
"排班班次": shift_info["bc_name"],
"排班班段时间": shift_info["bc_time"],
"计划时段": f"{_get(ot, 'PLAN_START_TIME') or _get(ot, 'FINAL_START_TIME')} ~ "
f"{_get(ot, 'PLAN_END_TIME') or _get(ot, 'FINAL_END_TIME')}",
"上班打卡": p["first"],
"下班打卡": p["last"],
"打卡次数": p["count"],
"判定": judge,
})
return rows