CI / build (push) Successful in 37s
Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
148 lines
7.8 KiB
JavaScript
148 lines
7.8 KiB
JavaScript
// 依權限從伺服器算好的模型裁出「這個人可以看的數字」。看不到的部門、組、個人薪資一律不送出。
|
|
const round = (value) => Math.round(Number(value) || 0);
|
|
|
|
function personRow(p) {
|
|
const isO = String(p.role || '').startsWith('O');
|
|
return {
|
|
employeeNumber: p.source === 'hr-sync' ? String(p.employeeNumber || '') : null,
|
|
name: p.name || '', title: p.title || '', role: p.role || '', group: p.group || '未分組',
|
|
months: { h1Start: p.h1Start || '', h1End: p.h1End || '', h2Start: p.h2Start || '', h2End: p.h2End || '' },
|
|
salaryType: p.salaryType || '',
|
|
salary: isO ? null : round(p.salaryType === 'hourly' ? p.hourlyRate : p.salary),
|
|
dayrate: isO ? round(p.dayrate) : null,
|
|
annualSalary: round(p.realSalaryTotal),
|
|
cost: round(p.totalCost),
|
|
invoiceCost: round(p.invoiceCost),
|
|
partTime: Boolean(p.partTime),
|
|
intern: Boolean(p.invoiceCost),
|
|
isManager: Boolean(p.isManager),
|
|
issue: p.calculationIssue || '',
|
|
};
|
|
}
|
|
|
|
// 部門倍率(和主看板一樣):夢境現實整間公司一個倍率,其他部門各自一個;已乘人薪倍率。沒有 A 成本就是 null。
|
|
export function multiplierFor(model, dept) {
|
|
const units = model.multiplierUnits || [];
|
|
const company = model.deptCompany?.[dept];
|
|
const own = units.find(u => !u.company && u.label === dept);
|
|
const unit = own || units.find(u => u.company && u.label === company);
|
|
if (!unit || !Number.isFinite(unit.multiplier)) return null;
|
|
return { value: Math.round(unit.multiplier * (model.opMultiplier || 2) * 100) / 100, company: unit.company ? unit.label : null };
|
|
}
|
|
|
|
// 發票佔比與三個指標(只有出發票的部門有)。
|
|
export function invoiceBreakdown(model, dept) {
|
|
const alloc = model.allocations?.deptData?.[dept];
|
|
if (!alloc) return null;
|
|
const annual = (model.invoice?.finalInvoiceTotal || 0) * (alloc.finalPct || 0);
|
|
const ratio = v => (Number.isFinite(v) ? Math.round(v * 100) / 100 : null);
|
|
return {
|
|
annual: round(annual), share: alloc.finalPct || 0, h1: round(annual / 2), h2: round(annual - round(annual / 2)),
|
|
indicators: {
|
|
p: { value: alloc.m1 || 0, weight: model.allocations.wP },
|
|
a: { value: alloc.m2 || 0, weight: model.allocations.wA },
|
|
s: { value: alloc.m3 || 0, weight: model.allocations.wS },
|
|
apRatio: ratio(alloc.costRatio), overallApRatio: ratio(model.allocations.overallRatio),
|
|
},
|
|
};
|
|
}
|
|
|
|
// 人數依職能分(實際人數,不是 FTE)。
|
|
function headcountByKind(people) {
|
|
const out = { A: 0, P: 0, G: 0, O: 0, parttime: 0, intern: 0, other: 0 };
|
|
for (const p of people) {
|
|
const role = String(p.role || '');
|
|
if (p.invoiceCost || (!role.startsWith('O') && /\bintern\b/i.test(String(p.title || '')))) out.intern += 1;
|
|
else if (role.startsWith('O')) out[p.partTime ? 'parttime' : 'O'] += 1;
|
|
else if (/^[APG]/.test(role)) out[role[0]] += 1;
|
|
else out.other += 1;
|
|
}
|
|
out.total = people.length;
|
|
return out;
|
|
}
|
|
|
|
function anchoredGroup(anchors, dept, group) {
|
|
const g = anchors?.coda?.[dept]?.groups?.[group];
|
|
return g ? { annual: round(g.value), h1: round(g.h1), h2: g.h2 === null ? null : round(g.h2) } : null;
|
|
}
|
|
|
|
// 部長看得到的人員異動:誰離開、誰加入、哪個月生效。
|
|
// 刻意不分離職或轉調,也不顯示轉調去哪裡,部長只需要知道編制什麼時候變。
|
|
// 只掛在看得到整個部門的人身上;只看單一組別的人不給,免得看到別組的人。
|
|
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));
|
|
const invoiceTotal = model.invoice?.finalInvoiceTotal || 0;
|
|
const depts = [];
|
|
for (const dept of fullDepts) {
|
|
const data = model.codaGroups[dept];
|
|
if (!data) continue;
|
|
const alloc = model.allocations?.deptData?.[dept];
|
|
const anchoredDept = anchors?.coda?.[dept];
|
|
const anchoredInvoice = anchors?.invoice?.[dept];
|
|
const row = {
|
|
dept, full: true,
|
|
actualCoda: round(data.coda), avgDailyCoda: round(data.avgDailyCoda),
|
|
anchoredCoda: anchoredDept ? { annual: round(anchoredDept.value), h1: round(anchoredDept.h1), h2: anchoredDept.h2 === null ? null : round(anchoredDept.h2) } : null,
|
|
invoice: invoiceBreakdown(model, dept),
|
|
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,
|
|
anchoredCoda: anchoredGroup(anchors, dept, group), totalCost: round(model.staffCost?.groups?.[`${dept}|${group}`]),
|
|
})).sort((a, b) => b.actualCoda - a.actualCoda),
|
|
};
|
|
if (permission.personDetail) {
|
|
row.people = model.people.filter(p => (p.dept || '').trim() === dept).map(personRow)
|
|
.sort((a, b) => a.group.localeCompare(b.group, 'zh-Hant') || b.cost - a.cost);
|
|
}
|
|
depts.push(row);
|
|
}
|
|
for (const [dept, group] of groupOnly) {
|
|
const g = model.codaGroups?.[dept]?.groups?.[group];
|
|
let row = depts.find(d => d.dept === dept && !d.full);
|
|
if (!row) { row = { dept, full: false, groups: [] }; depts.push(row); }
|
|
row.groups.push(g
|
|
? { group, actualCoda: round(g.coda), avgDailyCoda: round(g.avgDailyCoda), headcount: g.people.length, anchoredCoda: anchoredGroup(anchors, dept, group), totalCost: round(model.staffCost?.groups?.[`${dept}|${group}`]) }
|
|
: { group, missing: true });
|
|
if (permission.personDetail && g) {
|
|
row.people = [...(row.people || []), ...model.people.filter(p => (p.dept || '').trim() === dept && (p.group || '未分組') === group).map(personRow)];
|
|
}
|
|
}
|
|
return {
|
|
year,
|
|
me: {
|
|
employeeNumber: permission.employeeNumber,
|
|
admin: permission.admin, company: permission.company,
|
|
personDetail: permission.personDetail, simulate: permission.simulate,
|
|
sources: permission.sources || [],
|
|
},
|
|
anchor: anchors?.active ? { half: anchors.active.half, at: anchors.active.at } : null,
|
|
company: permission.admin || permission.company ? { invoiceTotal: round(invoiceTotal) } : null,
|
|
depts,
|
|
};
|
|
}
|