136 lines
4.9 KiB
JavaScript
136 lines
4.9 KiB
JavaScript
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
import { formatIdReference } from '../../../helpers/safeDisplay';
|
|
|
|
// 与后端 model/org_fund.go 常量一一对应
|
|
export const FUND_STATE = { NEVER: 0, ON: 1, OFF: 2 };
|
|
export const FUND_ACCOUNT_TYPE = { POOL: 0, UNALLOCATED: 1, DEPT: 2 };
|
|
export const FUND_ACCOUNT_STATUS = { ACTIVE: 1, DISABLED: 2, ARCHIVED: 3 };
|
|
export const FUND_LEDGER_PENDING = 1;
|
|
|
|
export const FUND_ENTRY_TYPES = [
|
|
'allocate_in',
|
|
'allocate_out',
|
|
'recall_in',
|
|
'recall_out',
|
|
'consume',
|
|
'consume_adjust',
|
|
'recharge',
|
|
'platform_add',
|
|
'platform_sub',
|
|
'baseline',
|
|
];
|
|
|
|
export const fundEntryLabel = (t, type) =>
|
|
({
|
|
baseline: t('首次开启基线'),
|
|
allocate_in: t('分配转入'),
|
|
allocate_out: t('分配转出'),
|
|
recall_in: t('回收转入'),
|
|
recall_out: t('回收转出'),
|
|
consume: t('消费'),
|
|
consume_adjust: t('消费调账'),
|
|
recharge: t('充值入账'),
|
|
platform_add: t('平台调增'),
|
|
platform_sub: t('平台调减'),
|
|
})[type] || type;
|
|
|
|
// 扣费账户展示值(F-36):企业资金池 / 企业未分配户 / 部门账户 · {名称快照}
|
|
export const fundAccountDisplay = (t, accountType, deptName) => {
|
|
if (accountType === FUND_ACCOUNT_TYPE.UNALLOCATED) return t('企业未分配户');
|
|
if (accountType === FUND_ACCOUNT_TYPE.DEPT)
|
|
return `${t('部门账户')} · ${deptName || ''}`;
|
|
return t('企业资金池');
|
|
};
|
|
|
|
// 归属部门展示值:未快照(历史待补齐)为 “—”,部门为 0 是“未归属部门”
|
|
export const fundDeptDisplay = (t, deptId, deptName) => {
|
|
if (deptId === null || deptId === undefined) return '—';
|
|
if (!deptId) return t('未归属部门');
|
|
return deptName || formatIdReference(deptId);
|
|
};
|
|
|
|
// 一次分配/回收的幂等键:打开弹窗时生成,重试沿用(F-12)。
|
|
// 旧内核(Chromium < 92)没有 crypto.randomUUID,退回 getRandomValues 拼装。
|
|
export const newFundOpId = () => {
|
|
const c = typeof window !== 'undefined' ? window.crypto : undefined;
|
|
if (c?.randomUUID) return c.randomUUID();
|
|
const bytes = new Uint8Array(16);
|
|
if (c?.getRandomValues) c.getRandomValues(bytes);
|
|
else for (let i = 0; i < 16; i++) bytes[i] = Math.floor(Math.random() * 256);
|
|
return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
|
|
};
|
|
|
|
// 手动金额校验(F-13):正数、最多两位小数;TOKENS 展示模式只收整数。
|
|
// 不接受负数、0、科学计数法、非数字文本。返回 i18n 键,合法返回 ''。
|
|
export const validateFundAmountInput = (raw, isTokenUnit) => {
|
|
const s = String(raw ?? '').trim();
|
|
if (!s) return '请输入金额';
|
|
const pattern = isTokenUnit ? /^\d+$/ : /^\d+(\.\d{1,2})?$/;
|
|
if (!pattern.test(s) || !(Number(s) > 0))
|
|
return '请输入大于 0、最多两位小数的金额';
|
|
if (Number(s) > 1e9) return '金额超出可操作范围';
|
|
return '';
|
|
};
|
|
|
|
// 按 parent_id 组装树(同级按 sort、id 排序),供 Semi Table 树形展示
|
|
export const buildFundTree = (rows, rootParentId = 0) => {
|
|
const byParent = new Map();
|
|
rows.forEach((r) => {
|
|
const p = r.parent_id || 0;
|
|
if (!byParent.has(p)) byParent.set(p, []);
|
|
byParent.get(p).push(r);
|
|
});
|
|
const ids = new Set(rows.map((r) => r.department_id));
|
|
const sortRows = (list) =>
|
|
[...list].sort(
|
|
(a, b) =>
|
|
(a.sort || 0) - (b.sort || 0) || a.department_id - b.department_id,
|
|
);
|
|
const build = (parentId, trail) =>
|
|
sortRows(byParent.get(parentId) || []).map((r) => {
|
|
const path = [...trail, r.name];
|
|
const children = build(r.department_id, path);
|
|
return {
|
|
...r,
|
|
key: r.department_id,
|
|
pathLabel: path.join(' / '),
|
|
children: children.length ? children : undefined,
|
|
};
|
|
});
|
|
// 根:rootParentId 的直接子节点;另外把父节点不在列表里的行也当作根(下级视图里的顶层)
|
|
const roots = build(rootParentId, []);
|
|
const orphanRoots = rows.filter(
|
|
(r) => (r.parent_id || 0) !== rootParentId && !ids.has(r.parent_id),
|
|
);
|
|
return [
|
|
...roots,
|
|
...sortRows(orphanRoots).map((r) => {
|
|
const children = build(r.department_id, [r.name]);
|
|
return {
|
|
...r,
|
|
key: r.department_id,
|
|
pathLabel: r.name,
|
|
children: children.length ? children : undefined,
|
|
};
|
|
}),
|
|
];
|
|
};
|