/** * 审计:库存「毛可用 / 在用预留 / 真实可领」三口径对照。 * * 背景:2026-09-22 真实缺陷 —— 需求单领料把已被预留占用的库存显示成"可用"并可领走 * (BR004 前夹精瘦肉 显示 15 / 真实可领 0)。修复见 * docs/design/20260922-processing-pick-respect-reservations.md; * 代码口径由 tests/processingPickRespectReservations.test.ts 钉住。 * * 本脚本(只读)给出两样东西: * 1. **影响面**:有多少 SKU 的"真实可领"小于"毛可用"(即修复后显示会变小的那些),其中多少已经是 0; * 2. **真异常**:`在用预留 > 毛可用`(超卖/重复预留)—— 这才是需要人工处理的数据问题(退出码 1)。 * * 用法: * npm run audit:stock-claimable * npm run audit:stock-claimable -- --branch BR002 */ import pg from 'pg'; const { Pool } = pg; function parseArgs(argv: string[]) { let branch = ''; for (let i = 0; i < argv.length; i += 1) { if (argv[i] === '--branch' && argv[i + 1]) branch = String(argv[i + 1]).toUpperCase(); } return { branch }; } (async () => { const url = String(process.env.DATABASE_URL || ''); if (!url) throw new Error('缺少 DATABASE_URL(用 npm run audit:stock-claimable 会带 --env-file=.env)'); const { branch } = parseArgs(process.argv.slice(2)); const pool = new Pool({ connectionString: url, max: 2 }); try { const schemas = ( await pool.query( `SELECT nspname FROM pg_namespace WHERE nspname LIKE 'tenant\\_%' ORDER BY 1` ) ).rows .map((r) => String(r.nspname)) .filter((s) => !branch || s === `tenant_${branch.toLowerCase()}`); let affected = 0; let blocked = 0; let overReserved = 0; for (const sch of schemas) { const exists = await pool.query( `SELECT 1 FROM information_schema.tables WHERE table_schema = $1 AND table_name = 'inventory'`, [sch] ); if (!exists.rowCount) continue; const rows = ( await pool.query( `WITH gross AS ( SELECT branch_code, sku_code, SUM(available_qty)::numeric AS gross FROM ${sch}.inventory WHERE available_qty > 0 GROUP BY 1, 2 ), reserved AS ( SELECT branch_code, sku_code, SUM(qty)::numeric AS reserved FROM ${sch}.inventory_reservations WHERE status = 10 GROUP BY 1, 2 ) SELECT g.branch_code, g.sku_code, g.gross, COALESCE(r.reserved, 0) AS reserved, GREATEST(0, g.gross - COALESCE(r.reserved, 0)) AS claimable FROM gross g LEFT JOIN reserved r ON r.sku_code = g.sku_code AND (r.branch_code IS NULL OR r.branch_code = g.branch_code) WHERE COALESCE(r.reserved, 0) > 0 ORDER BY g.branch_code, g.sku_code` ) ).rows; if (!rows.length) continue; const over = rows.filter((r) => Number(r.reserved) > Number(r.gross) + 1e-9); const zero = rows.filter((r) => Number(r.claimable) <= 1e-9); affected += rows.length; blocked += zero.length; overReserved += over.length; console.log( `${sch}: 受预留影响 ${rows.length} 个 SKU(真实可领为 0 的 ${zero.length} 个,超额预留 ${over.length} 个)` ); for (const r of rows.slice(0, 8)) { console.log( ` ${r.branch_code} ${r.sku_code} 毛可用=${r.gross} 预留=${r.reserved} 真实可领=${r.claimable}` ); } if (rows.length > 8) console.log(` …(其余 ${rows.length - 8} 个省略)`); } console.log( `\n影响面:受预留影响的 SKU 共 ${affected} 个,其中真实可领为 0 的 ${blocked} 个(修复后这些将显示 0)` ); if (overReserved) { console.log(`⚠ 真异常:${overReserved} 个 SKU 的「在用预留 > 毛可用」(超卖/重复预留),需人工核查。`); process.exitCode = 1; } else { console.log('✔ 未发现「在用预留 > 毛可用」的超卖/重复预留。'); } } finally { await pool.end(); } })().then( () => process.exit(process.exitCode || 0), (e) => { console.error('FATAL', (e as Error).message); process.exit(1); } );