CI / build (push) Successful in 30s
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
70 lines
3.7 KiB
JavaScript
70 lines
3.7 KiB
JavaScript
// 伺服器端計算:在獨立的 node:vm 環境執行 calc-core.js(與網頁載入的是同一份程式碼),
|
|
// 所以伺服器算出來的數字和網頁上算出來的一模一樣。
|
|
import { readFileSync } from 'node:fs';
|
|
import vm from 'node:vm';
|
|
|
|
const source = readFileSync(new URL('./calc-core.js', import.meta.url), 'utf8');
|
|
const script = new vm.Script(`${source}
|
|
;({ initOrgStructure, calculateAll, calculateInvoiceSummary, calculateDeptAllocations,
|
|
codaPageByDept, codaShares, dashboardMultiplierUnits, activeMonths, deptCompanyMap,
|
|
personCompany, personInvoiceRole, invoiceDepts, isCostExcludedIntern, codaByGroup, shortCompanyName })`, { filename: 'calc-core.js' });
|
|
|
|
const clone = (value) => JSON.parse(JSON.stringify(value));
|
|
|
|
// 每次計算都用全新的環境,避免上一次的計算結果殘留。
|
|
export function loadCore({ baseData, globalParams }) {
|
|
const sandbox = { baseData: clone(baseData), globalParams: clone(globalParams), window: { IS_PUBLIC_VERSION: false }, console };
|
|
const context = vm.createContext(sandbox);
|
|
const core = script.runInContext(context);
|
|
return { core, baseData: sandbox.baseData, globalParams: sandbox.globalParams };
|
|
}
|
|
|
|
// 與網頁 updateDashboard 相同的計算順序,回傳整份模型(只有數字,不含畫面)。
|
|
export function computeModel(input) {
|
|
const { core, baseData, globalParams } = loadCore(input);
|
|
const { deptStats, studioTotalDT, studioRawTotalDT } = core.calculateAll(baseData);
|
|
core.initOrgStructure();
|
|
const invoice = core.calculateInvoiceSummary(baseData);
|
|
const allocations = core.calculateDeptAllocations(baseData, invoice.finalInvoiceTotal);
|
|
const coda = core.codaPageByDept(baseData);
|
|
const ownCoda = core.codaPageByDept(baseData, true);
|
|
const codaSharing = core.codaShares(baseData, ownCoda);
|
|
const codaGroups = core.codaByGroup(baseData);
|
|
// 主看板的倍率:部門內部成本(P+A+G,不含外包)÷ A 成本;顯示時再乘人薪倍率。夢境現實整間公司算一個。
|
|
const opMultiplier = Number(globalParams.opMultiplier) || 2.0;
|
|
const multiplierUnits = core.dashboardMultiplierUnits(baseData, Object.keys(globalParams.orgStructure), deptStats);
|
|
// 總人力成本(和主看板「全企業總成本」同一個算法):正職年薪 × 人薪倍率 + 常態型外包 × 外包倍率;不含實習生。
|
|
const contractorRate = globalParams.contractorMultiplier !== undefined && globalParams.contractorMultiplier !== ''
|
|
&& Number.isFinite(Number(globalParams.contractorMultiplier)) && Number(globalParams.contractorMultiplier) >= 0 ? Number(globalParams.contractorMultiplier) : 1.5;
|
|
const staffCost = { depts: {}, groups: {} };
|
|
for (const person of baseData) {
|
|
const dept = (person.dept || '').trim();
|
|
if (!dept) continue;
|
|
const contractor = String(person.role || '').startsWith('O');
|
|
if (!contractor && core.isCostExcludedIntern(person)) continue;
|
|
const cost = (Number(person.realSalaryTotal) || 0) * (contractor ? contractorRate : opMultiplier);
|
|
const key = `${dept}|${person.group || '未分組'}`;
|
|
staffCost.depts[dept] = (staffCost.depts[dept] || 0) + cost;
|
|
staffCost.groups[key] = (staffCost.groups[key] || 0) + cost;
|
|
}
|
|
return JSON.parse(JSON.stringify({
|
|
calcVersion: 2,
|
|
people: baseData,
|
|
orgStructure: globalParams.orgStructure,
|
|
deptStats,
|
|
studioTotalDT,
|
|
studioRawTotalDT,
|
|
invoice,
|
|
allocations,
|
|
coda,
|
|
ownCoda,
|
|
codaSharing,
|
|
codaGroups,
|
|
opMultiplier,
|
|
multiplierUnits,
|
|
staffCost,
|
|
// 每個部門所屬公司(簡稱,例如「夢想創造」),對外接口用來分公司。
|
|
deptCompany: Object.fromEntries([...core.deptCompanyMap(baseData)].map(([dept, company]) => [dept, core.shortCompanyName(company)])),
|
|
}));
|
|
}
|