Files
37duAutomation/plugins/XQ0001-0100/XQ0033 门店管理及服务岗出勤积分更新/logic/rejoin_gap.py
T

112 lines
5.0 KiB
Python

# -*- coding: utf-8 -*-
"""XQ0033 插件 - 离职当月再入职人员「不操作段」判定(只跳过,不清空、不删除)。
业务口径(2026-10-29 业务确认,取代此前「再入职日之前全部踢除 + 生成删除行」方案):
离职当月再入职人员的「再入职日之前」积分既不导入、也不删除——该段是其离职前在职期间应得的
积分,离职结算时已随离职批次导入并生效,本轮再导入=重复给分,反向清段/生成删除行则会把
已生效的积分扣掉。因此:
- 台账工号(同工号再入职):[本月1日, 再入职日) 记为不操作段,报表原样保留、不补不删;
- 历史工号(换工号再入职):该工号整月都属于离职前段 → 整月不操作;
因工号可能被复用,要求「工号+姓名」同时匹配才跳过,避免误伤他人积分。
判定由 RejoinGuard 统一提供:新增段 / 修正补 / 修正删行过滤共用同一口径。
"""
from datetime import date
from .. import config
_CODE = "hr_staff_base.base_code"
_NAME = "hr_staff_base.staff_name"
_WHOLE_MONTH = 32 # 上界 32 = 整月(1~31 日)都不操作
def _clean(v):
if v is None:
return ""
return str(v).strip()
def _parse_date(v):
try:
return date.fromisoformat(str(v).strip()[:10])
except (TypeError, ValueError):
return None
class RejoinGuard:
"""再入职人员「不操作段」判定(新增段/修正补/修正删行过滤共用)。
anchor 传**积分日期 T-2**(不是运行日):月初运行 T-2 落在上个月时,
「本月1日~再入职日」必须按 T-2 所在月计算,否则 1~N 日的不操作段会整段失配。
"""
def __init__(self, rejoin_rows, old_codes_map=None, anchor=None):
anchor = anchor or date.today()
self.month_first = date(anchor.year, anchor.month, 1)
self.gaps = {} # 台账工号 -> (不操作段上界(不含), 再入职日, 姓名)
self.old_gaps = {} # 历史工号 -> (不操作段上界(不含), 再入职日, 姓名)
for r in rejoin_rows or ():
code = _clean(r.get(_CODE))
name = _clean(r.get(_NAME))
leave = _parse_date(r.get(f"{config.REJOIN_FIELD_PREFIX}.leave_date"))
join = _parse_date(r.get(f"{config.REJOIN_FIELD_PREFIX}.join_date"))
if not code or leave is None or join is None:
continue
until = self._until_day(join)
self.gaps[code] = (until, join, name)
for old_code in (old_codes_map or {}).get(code, ()):
old_code = _clean(old_code)
if old_code:
self.old_gaps[old_code] = (_WHOLE_MONTH, join, name)
def _until_day(self, join):
"""再入职日 → 本月不操作段上界(不含)。
本月再入职 → 再入职日;下月及以后再入职 → 整月(32);早于本月 → 无不操作段(1)。
"""
if (join.year, join.month) == (self.month_first.year, self.month_first.month):
return join.day
return _WHOLE_MONTH if join > self.month_first else 1
def __bool__(self):
return bool(self.gaps or self.old_gaps)
def invalid_until(self, code, name):
"""不操作段上界(不含):本月 1 日 ~ 该日之前均不导入、不删除;非再入职人员返回 None。"""
code = _clean(code)
hit = self.gaps.get(code)
if hit is None:
candidate = self.old_gaps.get(code)
if candidate and _clean(name) == candidate[2]:
hit = candidate
return hit[0] if hit else None
def is_invalid(self, code, name, day):
"""该员工某日(当月第几天)是否属于不操作段(不导入、不删除)。"""
until = self.invalid_until(code, name)
return until is not None and 1 <= int(day) < until
def allow(self, rows):
"""过滤 [(员工信息9列, date), ...],剔除不操作段的行(不导入、不删除)。"""
if not rows or not self:
return rows
return [x for x in rows if not self.is_invalid(x[0][0], x[0][8], x[1].day)]
def describe(self):
"""人员/段位描述,用于日志与运行摘要(例:何蕾(37037921) 再入职 10-18 → 1~17 日不操作)。"""
out = []
for code, (until, join, name) in self.gaps.items():
out.append(f"{name or '未知'}({code}) 再入职 {join:%m-%d} → "
f"{self._seg_text(until)}不导入/不删除")
for code, (until, join, name) in self.old_gaps.items():
out.append(f"{name or '未知'}({code}) 换工号再入职({join:%m-%d})→ "
f"{self._seg_text(until)}不导入/不删除")
return out
@staticmethod
def _seg_text(until):
if until >= _WHOLE_MONTH:
return "整月"
if until <= 1:
return "无(当月无需跳过)"
return f"1~{until - 1} 日"