Files
SupplierSystem/server/scripts/audit-stock-claimable.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

107 lines
4.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.
/**
* 审计:库存「毛可用 / 在用预留 / 真实可领」三口径对照。
*
* 背景: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);
}
);