Files
leadboard/settings-store.js
2026-11-25 01:33:47 +00:00

96 lines
4.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Board settings chosen by hand on the board (RD / CG Lead group flags and the
// 核心參數 values). They are not HR data, so HR syncs never change them. Persisted in
// COST_MODEL_DATA_DIR (board-settings.json), never in Git.
export const PARAM_KEYS = Object.freeze([
'opMultiplier', 'annualWorkDays', 'workingDays', 'utilizationP', 'utilizationA',
'invoiceWeightP', 'invoiceWeightA', 'invoiceWeightSurplus', 'targetAPRatio',
'externalProjectOutsourcing', 'employeeTripInvoices', 'grossMarginPct',
'rdReductionPct', 'pManagerReductionPct', 'pPCReductionPct', 'managerReductionPct', 'cgLeadReductionPct',
'contractorMultiplier', 'usdRate',
]);
const MAX_GROUPS = 300;
const MAX_SHARE_RULES = 20;
// Invoice role per company (default for its departments) and per department:
// bear = carries invoice, share = cost goes into the invoice pool but gets no allocation,
// exclude = outside the invoice (shown as cost × 人薪倍率).
export const INVOICE_ROLES = Object.freeze(['bear', 'share', 'exclude']);
function roleMap(input) {
if (input === undefined) return undefined;
if (!input || typeof input !== 'object' || Array.isArray(input)) throw new TypeError('role map must be an object');
const output = {};
for (const [name, role] of Object.entries(input).slice(0, MAX_GROUPS)) {
const key = String(name).trim().slice(0, 80);
if (key && INVOICE_ROLES.includes(role)) output[key] = role;
}
return output;
}
function groupKeys(list) {
if (list === undefined) return undefined;
if (!Array.isArray(list)) throw new TypeError('group list must be an array');
const keys = list
.filter(item => typeof item === 'string')
.map(item => item.trim().slice(0, 160))
.filter(item => /^[^|]+\|[^|]+$/.test(item));
return [...new Set(keys)].slice(0, MAX_GROUPS);
}
// CODA 分配規則:from 的部門整包 CODA 分給 to 的部門(依各自 CODA 比例)。
// 格式不對的整條跳過;已經在 from 的部門不會再被當成 to。
function shareRules(list) {
if (list === undefined) return undefined;
if (!Array.isArray(list)) throw new TypeError('codaShareRules must be an array');
const names = value => [...new Set((Array.isArray(value) ? value : [])
.filter(item => typeof item === 'string')
.map(item => item.trim().slice(0, 80))
.filter(Boolean))].slice(0, MAX_GROUPS);
const rules = [];
for (const rule of list.slice(0, MAX_SHARE_RULES)) {
if (!rule || typeof rule !== 'object' || Array.isArray(rule)) continue;
const from = names(rule.from);
const to = names(rule.to).filter(name => !from.includes(name));
if (!from.length || !to.length) continue;
rules.push({ company: String(rule.company || '').trim().slice(0, 80), from, to, noDeptP: Boolean(rule.noDeptP) });
}
return rules;
}
export function normalizeSettings(input = {}) {
if (!input || typeof input !== 'object' || Array.isArray(input)) throw new TypeError('settings must be an object');
const output = {};
const rdGroups = groupKeys(input.rdGroups);
const cgLeadGroups = groupKeys(input.cgLeadGroups);
if (rdGroups) output.rdGroups = rdGroups;
if (input.invoiceRoles !== undefined) {
if (!input.invoiceRoles || typeof input.invoiceRoles !== 'object' || Array.isArray(input.invoiceRoles)) throw new TypeError('invoiceRoles must be an object');
output.invoiceRoles = { companies: roleMap(input.invoiceRoles.companies) || {}, depts: roleMap(input.invoiceRoles.depts) || {} };
}
if (cgLeadGroups) output.cgLeadGroups = cgLeadGroups;
const codaShareRules = shareRules(input.codaShareRules);
if (codaShareRules) output.codaShareRules = codaShareRules;
if (input.params !== undefined) {
if (!input.params || typeof input.params !== 'object' || Array.isArray(input.params)) throw new TypeError('params must be an object');
output.params = {};
for (const key of PARAM_KEYS) {
const value = Number(input.params[key]);
if (input.params[key] !== undefined && input.params[key] !== null && input.params[key] !== '' && Number.isFinite(value) && value >= 0 && value <= 1e12) {
output.params[key] = value;
}
}
}
return output;
}
export function applySettings(globalParams, settings) {
if (!settings) return globalParams;
return {
...globalParams,
...(settings.params || {}),
...(settings.rdGroups ? { rdGroups: settings.rdGroups } : {}),
...(settings.cgLeadGroups ? { cgLeadGroups: settings.cgLeadGroups } : {}),
...(settings.invoiceRoles ? { invoiceRoles: settings.invoiceRoles } : {}),
...(settings.codaShareRules ? { codaShareRules: settings.codaShareRules } : {}),
};
}