diff --git a/scope-view.js b/scope-view.js index 0929af1..2271254 100644 --- a/scope-view.js +++ b/scope-view.js @@ -66,7 +66,29 @@ function anchoredGroup(anchors, dept, group) { return g ? { annual: round(g.value), h1: round(g.h1), h2: g.h2 === null ? null : round(g.h2) } : null; } -export function buildScopedView(model, anchors, permission, { year } = {}) { +// 部長看得到的人員異動:誰離開、誰加入、哪個月生效。 +// 刻意不分離職或轉調,也不顯示轉調去哪裡,部長只需要知道編制什麼時候變。 +// 只掛在看得到整個部門的人身上;只看單一組別的人不給,免得看到別組的人。 +export function changesFor(dept, model, plans) { + const name = (employeeNumber) => (model.people || []) + .find(p => String(p.employeeNumber || '') === String(employeeNumber))?.name || ''; + const out = []; + for (const person of model.people || []) { + if (String(person.dept || '').trim() !== dept) continue; + if (person.plannedLeave) out.push({ kind: 'leave', name: person.name || '', month: person.plannedLeave }); + // 拆成兩段時,前段掛在原部門,那是「離開」不是「加入」(下面用 fromDept 處理)。 + if (person.plannedTransfer && person.transferSplit !== 'before') out.push({ kind: 'join', name: person.name || '', month: person.plannedTransfer }); + if (person.plannedHire) out.push({ kind: 'join', name: person.name || '', month: person.plannedHire }); + } + for (const plan of plans?.transfers || []) { + if (String(plan.fromDept || '').trim() !== dept) continue; + out.push({ kind: 'leave', name: name(plan.employeeNumber), month: plan.effectiveMonth }); + } + return out.filter(change => change.name && change.month) + .sort((a, b) => String(a.month).localeCompare(String(b.month)) || a.name.localeCompare(b.name, 'zh-Hant')); +} + +export function buildScopedView(model, anchors, permission, { year, plans } = {}) { const allDepts = Object.keys(model.codaGroups || {}); const fullDepts = permission.admin || permission.company ? allDepts : allDepts.filter(d => permission.depts.includes(d)); const groupOnly = permission.groups.map(key => key.split('|')).filter(([d]) => !fullDepts.includes(d)); @@ -86,6 +108,7 @@ export function buildScopedView(model, anchors, permission, { year } = {}) { multiplier: multiplierFor(model, dept), headcount: headcountByKind(model.people.filter(p => (p.dept || '').trim() === dept)), totalCost: round(model.staffCost?.depts?.[dept]), + changes: changesFor(dept, model, plans), anchoredInvoice: anchoredInvoice ? { annual: round(anchoredInvoice.annual), h1: round(anchoredInvoice.h1), h2: anchoredInvoice.h2 === null ? null : round(anchoredInvoice.h2) } : null, groups: Object.entries(data.groups).map(([group, g]) => ({ group, actualCoda: round(g.coda), avgDailyCoda: round(g.avgDailyCoda), headcount: g.people.length,