diff --git a/settings-store.js b/settings-store.js index 17a7c9a..937cb51 100644 --- a/settings-store.js +++ b/settings-store.js @@ -9,6 +9,7 @@ export const PARAM_KEYS = Object.freeze([ '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 × 人薪倍率). @@ -35,6 +36,26 @@ function groupKeys(list) { 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 = {}; @@ -46,6 +67,8 @@ export function normalizeSettings(input = {}) { 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 = {}; @@ -67,5 +90,6 @@ export function applySettings(globalParams, settings) { ...(settings.rdGroups ? { rdGroups: settings.rdGroups } : {}), ...(settings.cgLeadGroups ? { cgLeadGroups: settings.cgLeadGroups } : {}), ...(settings.invoiceRoles ? { invoiceRoles: settings.invoiceRoles } : {}), + ...(settings.codaShareRules ? { codaShareRules: settings.codaShareRules } : {}), }; }