来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
197 lines
6.1 KiB
TypeScript
197 lines
6.1 KiB
TypeScript
import type { PoolClient } from 'pg';
|
||
import { pool } from '../db';
|
||
import { notifyRoleUsers } from './notificationService';
|
||
|
||
export type StockMode = 'pass_through' | 'inventory';
|
||
|
||
/** 过手实收/订货超过该比例需二次确认 */
|
||
export const PASS_THROUGH_OVER_QTY_RATIO = 1.2;
|
||
|
||
type Db = Pick<PoolClient, 'query'>;
|
||
|
||
function trimText(value: unknown): string {
|
||
return String(value || '').trim();
|
||
}
|
||
|
||
function normalizeStockMode(raw: unknown): StockMode | null {
|
||
const v = trimText(raw).toLowerCase();
|
||
if (v === 'pass_through' || v === 'passthrough' || v === '过手') return 'pass_through';
|
||
if (v === 'inventory' || v === 'stock' || v === '管库存' || v === '库存') return 'inventory';
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 解析 SKU 库存策略:显式 stock_mode → 默认 inventory。
|
||
* 商品级过手 UI 已废弃(20260828);历史 pass_through 行仍只读兼容。
|
||
* 见 docs/design/20260828-supplier-direct-delivery-unified-switch.md
|
||
*/
|
||
export async function resolveStockMode(
|
||
input: {
|
||
skuCode?: string | null;
|
||
categoryCode?: string | null;
|
||
branchCode?: string | null;
|
||
},
|
||
db: Db = pool
|
||
): Promise<StockMode> {
|
||
const skuCode = trimText(input.skuCode);
|
||
const categoryCodeHint = trimText(input.categoryCode);
|
||
const branchCode = trimText(input.branchCode);
|
||
|
||
if (skuCode) {
|
||
const skuRow = await db.query(
|
||
`SELECT s.stock_mode AS sku_mode,
|
||
pc.stock_mode AS catalog_mode,
|
||
COALESCE(NULLIF(TRIM(s.category_code), ''), NULLIF(TRIM(pc.parent_code), ''), $2::text) AS category_code
|
||
FROM (SELECT $1::text AS sku_code) k
|
||
LEFT JOIN skus s ON s.sku_code = k.sku_code
|
||
LEFT JOIN product_catalog pc
|
||
ON pc.item_type = 'product' AND pc.code = k.sku_code
|
||
LIMIT 1`,
|
||
[skuCode, categoryCodeHint || null]
|
||
);
|
||
const row = skuRow.rows[0];
|
||
const explicit = normalizeStockMode(row?.sku_mode) || normalizeStockMode(row?.catalog_mode);
|
||
if (explicit) return explicit;
|
||
|
||
const catCode = trimText(row?.category_code) || categoryCodeHint;
|
||
if (catCode) {
|
||
return resolveCategoryDefaultStockMode(catCode, branchCode, db);
|
||
}
|
||
return 'inventory';
|
||
}
|
||
|
||
if (categoryCodeHint) {
|
||
return resolveCategoryDefaultStockMode(categoryCodeHint, branchCode, db);
|
||
}
|
||
return 'inventory';
|
||
}
|
||
|
||
export async function isPassThroughSku(
|
||
input: {
|
||
skuCode?: string | null;
|
||
categoryCode?: string | null;
|
||
branchCode?: string | null;
|
||
},
|
||
db: Db = pool
|
||
): Promise<boolean> {
|
||
return (await resolveStockMode(input, db)) === 'pass_through';
|
||
}
|
||
|
||
async function resolveCategoryDefaultStockMode(
|
||
_categoryCode: string,
|
||
_branchCode: string,
|
||
_db: Db
|
||
): Promise<StockMode> {
|
||
// 新建 SKU 无显式 stock_mode 时一律管库存;是否不入仓由供应商 skip_inbound 决定
|
||
return 'inventory';
|
||
}
|
||
|
||
function formatQty(n: number): string {
|
||
if (!Number.isFinite(n)) return '0';
|
||
return String(Math.round(n * 1000) / 1000);
|
||
}
|
||
|
||
/**
|
||
* 过手 SKU:实收/实供 > 订货×1.2 且未确认时阻断。
|
||
* 非过手或订货量为 0 时放行。
|
||
*/
|
||
export async function assertPassThroughOverQtyConfirmed(input: {
|
||
skuCode?: string | null;
|
||
categoryCode?: string | null;
|
||
branchCode?: string | null;
|
||
orderedQty: number;
|
||
supplyQty: number;
|
||
confirmOverQty?: boolean;
|
||
orderCode?: string | null;
|
||
itemLabel?: string | null;
|
||
/** 阻断时是否发站内通知(默认 true) */
|
||
notify?: boolean;
|
||
client?: PoolClient;
|
||
}): Promise<void> {
|
||
const db = input.client || pool;
|
||
const supply = Number(input.supplyQty) || 0;
|
||
const ordered = Number(input.orderedQty) || 0;
|
||
if (!(supply > 0) || !(ordered > 0)) return;
|
||
|
||
const passThrough = await isPassThroughSku(
|
||
{
|
||
skuCode: input.skuCode,
|
||
categoryCode: input.categoryCode,
|
||
branchCode: input.branchCode,
|
||
},
|
||
db
|
||
);
|
||
if (!passThrough) return;
|
||
|
||
const limit = ordered * PASS_THROUGH_OVER_QTY_RATIO;
|
||
if (supply + 1e-9 <= limit) return;
|
||
if (input.confirmOverQty === true) return;
|
||
|
||
const label = trimText(input.itemLabel) || trimText(input.skuCode) || '商品';
|
||
const message = `过手商品「${label}」实收 ${formatQty(supply)} 超过订货 ${formatQty(ordered)} 的 ${Math.round(PASS_THROUGH_OVER_QTY_RATIO * 100)}%,请确认后重试(传 confirm_over_qty=true)`;
|
||
|
||
if (input.notify !== false) {
|
||
try {
|
||
await notifyRoleUsers({
|
||
roles: ['operator', 'admin', 'super_admin', 'purchaser'],
|
||
notifyType: 'pass_through_over_qty',
|
||
title: '过手超量待确认',
|
||
content: message,
|
||
relatedOrderCode: input.orderCode || null,
|
||
branchCode: input.branchCode,
|
||
skipIfRecent: true,
|
||
dedupWithinHours: 6,
|
||
});
|
||
} catch {
|
||
// 通知失败不挡业务错误抛出
|
||
}
|
||
}
|
||
|
||
throw Object.assign(new Error(message), {
|
||
status: 409,
|
||
code: 'pass_through_over_qty',
|
||
ordered_qty: ordered,
|
||
supply_qty: supply,
|
||
max_auto_qty: limit,
|
||
ratio: PASS_THROUGH_OVER_QTY_RATIO,
|
||
});
|
||
}
|
||
|
||
/** 备货/订单行需求量合计(订货口径) */
|
||
export async function resolveOrderedQtyForSku(input: {
|
||
orderCode?: string | null;
|
||
skuCode?: string | null;
|
||
preparationId?: string | null;
|
||
fallbackQty?: number | null;
|
||
client?: PoolClient;
|
||
}): Promise<number> {
|
||
const db = input.client || pool;
|
||
const prepId = trimText(input.preparationId);
|
||
if (prepId) {
|
||
const r = await db.query(
|
||
`SELECT COALESCE(SUM(COALESCE(oi.ordered_quantity, oi.quantity)), 0)::float8 AS qty
|
||
FROM order_item_fulfillments f
|
||
JOIN order_items oi ON oi.id = f.order_item_id
|
||
WHERE f.supplier_preparation_id = $1::uuid`,
|
||
[prepId]
|
||
);
|
||
const q = Number(r.rows[0]?.qty) || 0;
|
||
if (q > 0) return q;
|
||
}
|
||
|
||
const orderCode = trimText(input.orderCode);
|
||
const skuCode = trimText(input.skuCode);
|
||
if (orderCode && skuCode) {
|
||
const r = await db.query(
|
||
`SELECT COALESCE(SUM(COALESCE(ordered_quantity, quantity)), 0)::float8 AS qty
|
||
FROM order_items
|
||
WHERE order_code = $1 AND sku_code IS NOT DISTINCT FROM $2`,
|
||
[orderCode, skuCode]
|
||
);
|
||
const q = Number(r.rows[0]?.qty) || 0;
|
||
if (q > 0) return q;
|
||
}
|
||
|
||
return Number(input.fallbackQty) || 0;
|
||
}
|