Files
SupplierSystem/server/src/services/zeroSupplyCostConfigService.ts
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

131 lines
4.9 KiB
TypeScript
Raw 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.
import { pool } from '../db';
import { writeAuditLog } from './auditService';
/**
* 「允许成本价为 0」开关(分公司级,2026-09-23)。
*
* 背景:部分租户不愿把真实成本放进系统(只用系统看销售价)。开启后,
* **自采(实采价)、备货(采购价/供货价)、小程序采购端、供应商端、独立入库(无订单进仓)**
* 都允许填 0 并正常保存/推进。
*
* 设计:docs/design/20260923-allow-zero-supply-cost.md
*
* 铁律:
* - **默认关闭**(`DEFAULT_CONFIG.enabled = false`)→ 关闭时全部校验与今天逐字节一致;
* - 语义:**0 是真实值 0**(开发者 2026-09-23 决定:财务展示按真实数据,不改任何口径);
* - 仍然拒绝负数 / NaN;
* - 存储复用 `system_configs`(分公司行 → 全局行 → 代码默认),与 `block_zero_sale_on_submit`、
* `independent_inbound_require_inspection` 完全同构;
* - 只有**分公司管理员**能改(路由层 `requireAdminRole`,超管不参与本开关)。
*/
export const ALLOW_ZERO_SUPPLY_COST_KEY = 'allow_zero_supply_cost';
export interface AllowZeroSupplyCostConfig {
enabled: boolean;
}
const DEFAULT_CONFIG: AllowZeroSupplyCostConfig = { enabled: false };
const DESCRIPTION =
'允许成本价为 0:enabled=开启时,自采/备货/供应商端/独立入库可按 0 成本保存与推进(默认关闭)';
function parseConfigValue(raw: unknown): AllowZeroSupplyCostConfig {
if (!raw || typeof raw !== 'object') return { ...DEFAULT_CONFIG };
const obj = raw as Record<string, unknown>;
if (obj.enabled === true) return { enabled: true };
if (obj.enabled === false) return { enabled: false };
return { ...DEFAULT_CONFIG };
}
export type AllowZeroSupplyCostSource = 'branch' | 'global' | 'default';
export async function getAllowZeroSupplyCostConfig(
branchCode?: string | null
): Promise<AllowZeroSupplyCostConfig & { source: AllowZeroSupplyCostSource; branch_code: string | null }> {
const code = String(branchCode || '').trim();
if (code) {
const branchRow = await pool.query(
`SELECT config_value FROM system_configs
WHERE config_key = $1 AND branch_code = $2
LIMIT 1`,
[ALLOW_ZERO_SUPPLY_COST_KEY, code]
);
if (branchRow.rows[0]?.config_value != null) {
return { ...parseConfigValue(branchRow.rows[0].config_value), source: 'branch', branch_code: code };
}
}
const globalRow = await pool.query(
`SELECT config_value FROM system_configs
WHERE config_key = $1 AND branch_code IS NULL
LIMIT 1`,
[ALLOW_ZERO_SUPPLY_COST_KEY]
);
if (globalRow.rows[0]?.config_value != null) {
return { ...parseConfigValue(globalRow.rows[0].config_value), source: 'global', branch_code: code || null };
}
return { ...DEFAULT_CONFIG, source: 'default', branch_code: code || null };
}
export async function isZeroSupplyCostAllowed(branchCode?: string | null): Promise<boolean> {
const cfg = await getAllowZeroSupplyCostConfig(branchCode);
return cfg.enabled === true;
}
export async function setAllowZeroSupplyCostConfig(
branchCode: string,
enabled: boolean,
updatedBy?: string | null
): Promise<AllowZeroSupplyCostConfig> {
const code = String(branchCode || '').trim();
if (!code) {
throw Object.assign(new Error('请先选择操作分公司'), {
status: 400,
code: 'operating_branch_required',
});
}
const value: AllowZeroSupplyCostConfig = { enabled: enabled === true };
const before = await getAllowZeroSupplyCostConfig(code);
const existing = await pool.query(
`SELECT id FROM system_configs
WHERE config_key = $1 AND branch_code = $2
LIMIT 1`,
[ALLOW_ZERO_SUPPLY_COST_KEY, code]
);
if (existing.rows[0]?.id) {
await pool.query(
`UPDATE system_configs
SET config_value = $1::jsonb, updated_at = NOW(), updated_by = $2
WHERE id = $3`,
[JSON.stringify(value), updatedBy || null, existing.rows[0].id]
);
} else {
await pool.query(
`INSERT INTO system_configs (
config_key, config_value, config_group, description, branch_code, updated_at, updated_by
) VALUES (
$1, $2::jsonb, 'purchase', $3, $4, NOW(), $5
)`,
[ALLOW_ZERO_SUPPLY_COST_KEY, JSON.stringify(value), DESCRIPTION, code, updatedBy || null]
);
}
// 审计留痕:这是会改变成本校验口径的策略开关,谁在哪个分公司、何时开/关都要可追溯
try {
await writeAuditLog({
operatorId: updatedBy || null,
operationType: 'config_update',
targetTable: 'system_configs',
targetId: ALLOW_ZERO_SUPPLY_COST_KEY,
oldValue: { enabled: before.enabled, source: before.source },
newValue: { enabled: value.enabled },
branchCode: code,
actionCategory: 'purchase',
});
} catch (error) {
// 审计失败不回滚配置(与其它配置写入同口径),但要能看见
console.warn('[zero-supply-cost] 审计写入失败', error);
}
return value;
}