Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
// - 預計新進:先用一個名稱(不一定是真名)、部門、組、職能、預估薪資、開始月份加進名單。
|
||||
// - 預計轉調:生效月份起算新部門/組。生效月份在報表年度中間就按月拆成兩段(前段原單位、後段新單位),
|
||||
// 在報表年度之前就整年算新單位,在之後則先不套用。目標部門可以是 HR 還沒有的新部門。
|
||||
// 主管職:預設轉調後卸任(keepManager 可保留);原單位可以指定接任者,模擬時就當成主管。
|
||||
// 這只影響數字與模擬,不影響看板權限(權限一律以 HR 的主管關係為準)。
|
||||
// 部長在 /my 看到的是套用預排之後的數字(不顯示原因);所有計算都用同一份資料,定錨的最高點才不會被墊高。
|
||||
// 自動核對(reconcilePlans):HR 名單一出現結果(離職的人不在名單上或有離職日、新人出現在名單上)就自動移除那一筆;
|
||||
// 時間過了 HR 還沒改,寬限 1 個月後也自動移除。移除紀錄留在 history 給最高管理者看。
|
||||
@@ -59,7 +61,10 @@ export function normalizePlans(input = {}) {
|
||||
const effectiveMonth = monthOf(item?.effectiveMonth);
|
||||
if (!/^[A-Za-z0-9_-]{1,40}$/.test(employeeNumber) || !toDept || !effectiveMonth || moved.has(employeeNumber)) throw new RangeError('invalid transfer');
|
||||
moved.add(employeeNumber);
|
||||
const successor = text(String(item?.successorEmployeeNumber ?? ''), 40);
|
||||
if (successor && (!/^[A-Za-z0-9_-]{1,40}$/.test(successor) || successor === employeeNumber)) throw new RangeError('invalid transfer');
|
||||
transfers.push({ employeeNumber, toDept, toGroup: text(item.toGroup), fromDept: text(item.fromDept), effectiveMonth,
|
||||
keepManager: item?.keepManager === true, successorEmployeeNumber: successor,
|
||||
note: text(item.note, 200), createdAt: text(item.createdAt, 40) || new Date().toISOString() });
|
||||
}
|
||||
const REASONS = ['hr_left', 'hr_leave_date', 'hr_joined', 'hr_transferred', 'expired', 'manual'];
|
||||
@@ -99,6 +104,13 @@ function matchHires(hires, hrPeople) {
|
||||
return matches;
|
||||
}
|
||||
|
||||
// 主管職是否已經照預排處理:本人的主管身分符合預排,指定的接任者在 HR 也已經是主管。
|
||||
function managerSettled(plan, person, hr) {
|
||||
if (Boolean(person?.isManager) !== Boolean(plan.keepManager)) return false;
|
||||
if (!plan.successorEmployeeNumber) return true;
|
||||
return Boolean(hr.get(String(plan.successorEmployeeNumber))?.isManager);
|
||||
}
|
||||
|
||||
// 拿目前 HR 名單核對預排,回傳要留下的預排和這次自動移除的紀錄。HR 名單是空的(例如 HR 暫時連不上)就不動。
|
||||
export function reconcilePlans(plans, people, now = new Date()) {
|
||||
const hrPeople = (people || []).filter(p => p.source === 'hr-sync');
|
||||
@@ -124,9 +136,12 @@ export function reconcilePlans(plans, people, now = new Date()) {
|
||||
// 轉調:HR 的部門(有填組就連組)變成目標單位就算完成;人不在名單上或過了寬限期也移除。
|
||||
const transfers = (plans.transfers || []).filter((plan) => {
|
||||
const person = hr.get(plan.employeeNumber);
|
||||
const arrived = Boolean(person) && String(person.dept || '').trim() === plan.toDept
|
||||
const movedDept = Boolean(person) && String(person.dept || '').trim() === plan.toDept
|
||||
&& (!plan.toGroup || String(person.group || '').trim() === plan.toGroup);
|
||||
const arrived = movedDept && managerSettled(plan, person, hr);
|
||||
// 部門對了但主管還沒處理:留著提醒,不讓它過期被默默移除。
|
||||
const reason = !person ? 'hr_left' : arrived ? 'hr_transferred'
|
||||
: movedDept ? null
|
||||
: thisMonth > addMonths(plan.effectiveMonth, GRACE_MONTHS) ? 'expired' : null;
|
||||
if (reason) {
|
||||
removed.push({ kind: 'transfer', reason, label: person?.name || plan.employeeNumber, month: plan.effectiveMonth,
|
||||
@@ -162,16 +177,34 @@ export function applyPlans(people, plans, { year, fullYearDefault = false } = {}
|
||||
if (!plans.transfers?.length) return out;
|
||||
const moving = new Map(plans.transfers.map(plan => [String(plan.employeeNumber), plan]));
|
||||
const splits = [];
|
||||
// 接任者:原單位由誰接主管(簡化為整年都算主管,一月生效的轉調本來就一致)。
|
||||
const successors = new Map(plans.transfers.filter(plan => plan.successorEmployeeNumber)
|
||||
.map(plan => [String(plan.successorEmployeeNumber), plan]));
|
||||
const managerBefore = new Map();
|
||||
const moved = out.map((person) => {
|
||||
if (person.source === 'hr-sync' && successors.has(String(person.employeeNumber))) {
|
||||
return { ...person, isManager: true, plannedManager: true };
|
||||
}
|
||||
const plan = person.source === 'hr-sync' ? moving.get(String(person.employeeNumber)) : null;
|
||||
if (!plan) return person;
|
||||
const [planYear, planMonth] = plan.effectiveMonth.split('-').map(Number);
|
||||
if (Number.isFinite(year) && planYear > year) return person;
|
||||
const month = !Number.isFinite(year) || planYear < year ? 1 : planMonth;
|
||||
if (month > 1) splits.push({ key: personKey(person), from: { dept: person.dept, group: person.group }, month });
|
||||
return { ...person, dept: plan.toDept, group: plan.toGroup, plannedTransfer: plan.effectiveMonth };
|
||||
if (month > 1) {
|
||||
splits.push({ key: personKey(person), from: { dept: person.dept, group: person.group }, month });
|
||||
managerBefore.set(personKey(person), { isManager: person.isManager, supervisorOf: person.supervisorOf });
|
||||
}
|
||||
const manager = plan.keepManager
|
||||
? { isManager: person.isManager, supervisorOf: person.supervisorOf }
|
||||
: { isManager: false, supervisorOf: [] };
|
||||
return { ...person, dept: plan.toDept, group: plan.toGroup, plannedTransfer: plan.effectiveMonth, ...manager };
|
||||
});
|
||||
// 拆成兩段時,生效月份之前還是原單位的主管。
|
||||
return splitTransfers(moved, splits).map((person) => {
|
||||
if (person.transferSplit !== 'before') return person;
|
||||
const before = managerBefore.get(personKey(person));
|
||||
return before ? { ...person, ...before } : person;
|
||||
});
|
||||
return splitTransfers(moved, splits);
|
||||
}
|
||||
|
||||
// 每一筆預排目前的狀態(給你看:等待中/HR 已完成/過期未完成)。
|
||||
@@ -192,13 +225,21 @@ export function planStatus(plans, people, now = new Date()) {
|
||||
const current = String(person?.dept || '').trim();
|
||||
const conflict = Boolean(person) && Boolean(plan.fromDept) && Boolean(current)
|
||||
&& current !== plan.fromDept && current !== plan.toDept;
|
||||
const status = conflict ? 'conflict' : thisMonth > plan.effectiveMonth ? 'overdue' : 'pending';
|
||||
const movedDept = Boolean(person) && current === plan.toDept
|
||||
&& (!plan.toGroup || String(person.group || '').trim() === plan.toGroup);
|
||||
const status = conflict ? 'conflict'
|
||||
: movedDept && !managerSettled(plan, person, hr) ? 'manager_pending'
|
||||
: thisMonth > plan.effectiveMonth ? 'overdue' : 'pending';
|
||||
const successor = plan.successorEmployeeNumber ? hr.get(String(plan.successorEmployeeNumber)) : null;
|
||||
return { ...plan, name: person?.name || '', dept: current, group: person?.group || '', status,
|
||||
isManager: Boolean(person?.isManager), leads: (person?.supervisorOf || []).map(unit => unit.deptName).filter(Boolean),
|
||||
successorName: successor?.name || '', successorIsManager: Boolean(successor?.isManager),
|
||||
autoRemoveAfter: addMonths(plan.effectiveMonth, GRACE_MONTHS) };
|
||||
});
|
||||
return { departures, hires, transfers, history: [...(plans.history || [])].reverse().slice(0, 30),
|
||||
overdue: [...departures, ...hires, ...transfers].filter(p => p.status === 'overdue').length,
|
||||
conflicts: transfers.filter(p => p.status === 'conflict').length };
|
||||
conflicts: transfers.filter(p => p.status === 'conflict').length,
|
||||
managerPending: transfers.filter(p => p.status === 'manager_pending').length };
|
||||
}
|
||||
|
||||
export function createPlanStore(source) {
|
||||
|
||||
Reference in New Issue
Block a user