feat: 實際 CODA 依調動月份拆分;快照與異動比對時合回一個人
CI / build (push) Successful in 30s

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
This commit is contained in:
2026-11-04 07:47:41 +00:00
co-authored by Claude Opus 5.5
parent e243513a7a
commit dd7c4dd2c0
+52 -14
View File
@@ -30,19 +30,32 @@ function invoiceMonths(person) {
};
}
// 調動後同一個人會拆成「調動前」「調動後」兩筆(splitTransfers);比對與快照時合回一個人:
// 金額相加,部門/組用調動後(現在)的。
export function mergedPeople(people) {
const byKey = new Map();
for (const p of people) {
const key = personKey(p);
const months = invoiceMonths(p);
const entry = byKey.get(key);
if (!entry) { byKey.set(key, { person: p, pa: months.pa, o: months.o, cost: (p.totalCost || 0) + (p.invoiceCost || 0) }); continue; }
entry.pa = entry.pa.map((v, i) => v + months.pa[i]);
entry.o = entry.o.map((v, i) => v + months.o[i]);
entry.cost += (p.totalCost || 0) + (p.invoiceCost || 0);
if (p.transferSplit !== 'before') entry.person = p;
}
return byKey;
}
// 快照:從伺服器算好的模型抓出定錨需要的數字。
export function buildSnapshot(model, { year, half, note, by, at }) {
const invoiceDeptNames = Object.keys(model.allocations?.deptData || {});
const people = model.people
.filter(p => (p.totalCost || 0) > 0 || (p.invoiceCost || 0) > 0)
.map(p => {
const months = invoiceMonths(p);
return {
key: personKey(p), name: p.name || '', dept: (p.dept || '').trim(), group: (p.group || '').trim(),
role: p.role || '', invoiceRole: p.invoiceRoleAtAnchor || null,
pa: months.pa, o: months.o,
};
});
const people = [...mergedPeople(model.people)]
.filter(([, e]) => e.cost > 0)
.map(([key, { person: p, pa, o }]) => ({
key, name: p.name || '', dept: (p.dept || '').trim(), group: (p.group || '').trim(),
role: p.role || '', invoiceRole: p.invoiceRoleAtAnchor || null, pa, o,
}));
const invoice = {};
for (const dept of invoiceDeptNames) {
const data = model.allocations.deptData[dept];
@@ -64,7 +77,7 @@ export function buildSnapshot(model, { year, half, note, by, at }) {
// 調動:快照時的部門與現在不同。effectiveMonth 由使用者確認,預設為偵測到的月份。
export function detectTransfers(snapshot, model, savedMonths = {}, detectedMonth = 1) {
const now = new Map(model.people.map(p => [personKey(p), p]));
const now = new Map([...mergedPeople(model.people)].map(([key, e]) => [key, e.person]));
const list = [];
for (const before of snapshot.people) {
const current = now.get(before.key);
@@ -181,7 +194,8 @@ export function halves(annual, half, h1Settled) {
export function detectChanges(snapshot, model) {
const margin = snapshot.grossMargin || 1;
const before = new Map(snapshot.people.map(p => [p.key, p]));
const now = new Map(model.people.map(p => [personKey(p), p]));
const merged = mergedPeople(model.people);
const now = new Map([...merged].map(([key, e]) => [key, e.person]));
const monthOf = (date) => { const m = /^\d{4}-(\d{2})/.exec(String(date || '')); return m ? Number(m[1]) : null; };
const list = [];
const push = (type, key, name, dept, group, beforeCost, afterCost, effectiveMonth, extra = {}) => {
@@ -191,8 +205,7 @@ export function detectChanges(snapshot, model) {
};
const zero = { pa: 0, o: 0 };
for (const [key, p] of now) {
const months = invoiceMonths(p);
const cost = { pa: sum(months.pa), o: sum(months.o) };
const cost = { pa: sum(merged.get(key).pa), o: sum(merged.get(key).o) };
const old = before.get(key);
const dept = (p.dept || '').trim();
const group = (p.group || '').trim();
@@ -217,3 +230,28 @@ export function detectChanges(snapshot, model) {
const order = { new: 0, leave: 1, cost: 2, role: 3, transfer: 4 };
return list.sort((a, b) => order[a.type] - order[b.type] || a.dept.localeCompare(b.dept, 'zh-Hant'));
}
// 實際 CODA 依調動月份拆分:生效月份之前算原部門/組,之後算新部門/組。
// splits:[{ key, from: { dept, group }, month }]。只拆在職月份,薪資與設定不變。
export function splitTransfers(people, splits) {
if (!splits?.length) return people;
const byKey = new Map(splits.map(s => [s.key, s]));
const clip = (p, first, last) => {
const out = { ...p };
for (const [a, b] of [['h1Start', 'h1End'], ['h2Start', 'h2End']]) {
if (out[a] === '' || out[b] === '' || out[a] === undefined || out[b] === undefined) continue;
const start = Math.max(Number(out[a]), first);
const end = Math.min(Number(out[b]), last);
if (start > end) { out[a] = ''; out[b] = ''; } else { out[a] = start; out[b] = end; }
}
return out;
};
const result = [];
for (const p of people) {
const split = byKey.get(personKey(p));
if (!split || split.month <= 1 || split.month > 12) { result.push(p); continue; }
result.push({ ...clip(p, 1, split.month - 1), dept: split.from.dept, group: split.from.group, transferSplit: 'before', transferMonth: split.month, row: `${p.row}-before` });
result.push({ ...clip(p, split.month, 12), transferSplit: 'after', transferMonth: split.month });
}
return result;
}