Files
SupplierSystem/server/src/services/processingRawPurchaseConfigService.ts
T
Ubuntu 844a576252
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
feat(仓内加工): 移植 BOM、原料采购、完工分配与加工页面
适配供应商系统:商品 BOM、按加工单自动原料采购、成品免入库回写、加工分配预览/人工分配、订单 BOM 弹窗。
2026-11-03 13:49:38 +08:00

63 lines
2.2 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.
/**
* 仓内加工原料自动采购开关(阶段 1)
*
* 默认开启:只在成品配置了 processing_bom 时才触发,未配置的商品完全无行为变化。
* 关闭方式:system_configs.config_key='warehouse_processing_raw_purchase_enabled',
* 支持分公司级(branch_code)与全局(branch_code IS NULL)。
*/
import { pool } from '../db';
export const PROCESSING_RAW_PURCHASE_ENABLED_KEY = 'warehouse_processing_raw_purchase_enabled';
export interface ProcessingRawPurchaseConfig {
enabled: boolean;
}
export function parseProcessingRawPurchaseConfig(raw: unknown): ProcessingRawPurchaseConfig {
if (raw == null) return { enabled: true };
if (typeof raw === 'boolean') return { enabled: raw };
if (typeof raw === 'object') {
const obj = raw as Record<string, unknown>;
if (typeof obj.enabled === 'boolean') return { enabled: obj.enabled };
}
return { enabled: true };
}
export async function getProcessingRawPurchaseConfig(
branchCode?: string | null
): Promise<ProcessingRawPurchaseConfig> {
const code = String(branchCode || '').trim();
try {
if (code) {
const branchRow = await pool.query(
`SELECT config_value FROM system_configs
WHERE config_key = $1 AND branch_code = $2
LIMIT 1`,
[PROCESSING_RAW_PURCHASE_ENABLED_KEY, code]
);
if (branchRow.rows[0]?.config_value != null) {
return parseProcessingRawPurchaseConfig(branchRow.rows[0].config_value);
}
}
const globalRow = await pool.query(
`SELECT config_value FROM system_configs
WHERE config_key = $1 AND branch_code IS NULL
LIMIT 1`,
[PROCESSING_RAW_PURCHASE_ENABLED_KEY]
);
if (globalRow.rows[0]?.config_value != null) {
return parseProcessingRawPurchaseConfig(globalRow.rows[0].config_value);
}
} catch (error: any) {
// 配置表不可用时不阻断业务:按默认开启(有 BOM 才触发)
console.warn('[processing-raw] 读取自动采购开关失败,按默认开启处理', error?.message || error);
}
return { enabled: true };
}
export async function isProcessingRawPurchaseEnabled(
branchCode?: string | null
): Promise<boolean> {
return (await getProcessingRawPurchaseConfig(branchCode)).enabled;
}