- T1 资金/库存/报价/自采 P1(BIZ-1/2/3/4/10/13/14) - T2 主系统反向事件墓碑/改派/孤儿处理(BIZ-6/8/9) - T3 选分公司 ticket、trust proxy、角色权限收口(SEC-2/3/4) - T4 缺列/RLS/tenant 名单/FK/迁移执行器(DATA-1..8) - T5 401 登出/分页/映射口径/blob/ZIP 中文名(FE-1..4/BIZ-15) - T6 测试脚本目标守卫/config fail-fast/容器配置(OPS-2..8) - T7 前后端测试与 lint 门禁清零 - T8 应付卡片按关联订单日(口径 A) - T9 存储对象全链路归属校验、敏感列禁写、审计 target_id 超长修复
113 lines
4.4 KiB
TypeScript
113 lines
4.4 KiB
TypeScript
/**
|
||
* **订单头金额一次性对账**(2026-10-19 BIZ-16)。
|
||
*
|
||
* 背景:`orders.total_amount` 是冗余汇总,行被删/退货红冲后本应重算,但历史上只有
|
||
* 缺货删行 / 退货跟随 / 售后取消履约三条路径调 `recalcOrderHeaderTotals`;
|
||
* 其余路径会留下"头与明细不一致"的存量行
|
||
* (审计样本 `ORD_API_20261018_BR002_0001`:头 253.00 / 未退货明细 249.00,差 ¥4)。
|
||
*
|
||
* 口径:**完全复用**既有重算口径(金额 = 未退货行 `amount` 之和;重量 = `sumWeightInJin`),
|
||
* 明细金额一律不动 ⇒ 不改财务口径。
|
||
*
|
||
* 用法(在 `SupplierSystem/server` 下):
|
||
* npm run reconcile:order-header-totals -- --branch BR002 # dry-run:只报告
|
||
* npm run reconcile:order-header-totals -- --branch BR002 --apply # 写回
|
||
* npm run reconcile:order-header-totals -- --branch BR002 --limit 1000
|
||
*
|
||
* 退出码:0 = 完成(dry-run 发现不一致也算完成);1 = apply 后仍不一致。
|
||
*
|
||
* ⚠️ **写操作只允许本地库**(仓库硬规则:本地脚本不得写线上);脚本会对
|
||
* `DATABASE_URL` 做主机白名单校验。
|
||
*/
|
||
import { reconcileOrderHeaderTotals } from '../src/services/orderHeaderReconcileService';
|
||
import { pool } from '../src/db';
|
||
|
||
type Args = { branch: string; apply: boolean; limit: number };
|
||
|
||
function parseArgs(argv: string[]): Args {
|
||
const out: Args = { branch: '', apply: false, limit: 500 };
|
||
for (let i = 0; i < argv.length; i += 1) {
|
||
const arg = String(argv[i] || '');
|
||
if (arg === '--branch' && argv[i + 1]) out.branch = String(argv[(i += 1)]).toUpperCase();
|
||
else if (arg.startsWith('--branch=')) out.branch = arg.slice('--branch='.length).toUpperCase();
|
||
else if (arg === '--apply') out.apply = true;
|
||
else if (arg === '--limit' && argv[i + 1]) out.limit = Number(argv[(i += 1)]);
|
||
else if (arg.startsWith('--limit=')) out.limit = Number(arg.slice('--limit='.length));
|
||
}
|
||
return out;
|
||
}
|
||
|
||
/** 硬闸门:绝不连非本地库(本脚本 `--apply` 会写库) */
|
||
function assertLocalDatabase(url: string) {
|
||
const host = (() => {
|
||
try {
|
||
return new URL(url).hostname;
|
||
} catch {
|
||
return '';
|
||
}
|
||
})();
|
||
if (!['127.0.0.1', 'localhost', '::1'].includes(host)) {
|
||
throw new Error(`拒绝运行:DATABASE_URL 指向非本地库 (${host || 'unknown'})`);
|
||
}
|
||
}
|
||
|
||
const money = (n: number) => (Math.round((Number(n) || 0) * 100) / 100).toFixed(2);
|
||
|
||
async function main() {
|
||
const url = String(process.env.DATABASE_URL || '');
|
||
if (!url) {
|
||
throw new Error('缺少 DATABASE_URL(用 `npm run reconcile:order-header-totals` 会带 --env-file=.env)');
|
||
}
|
||
assertLocalDatabase(url);
|
||
|
||
const args = parseArgs(process.argv.slice(2));
|
||
if (!args.branch) {
|
||
console.error(
|
||
'用法:npm run reconcile:order-header-totals -- --branch BR002 [--apply] [--limit 500]'
|
||
);
|
||
process.exitCode = 2;
|
||
return;
|
||
}
|
||
|
||
const result = await reconcileOrderHeaderTotals({
|
||
branchCode: args.branch,
|
||
apply: args.apply,
|
||
limit: Number.isFinite(args.limit) ? args.limit : 500,
|
||
});
|
||
|
||
console.log(
|
||
`[reconcile] 分公司 ${result.branchCode}(${result.apply ? 'APPLY 写回' : 'DRY-RUN 只报告'},` +
|
||
`扫描上限 ${result.limit}):发现不一致 ${result.mismatchCount} 单,修后仍不一致 ${result.remaining} 单`
|
||
);
|
||
for (const mismatch of result.mismatches) {
|
||
console.log(
|
||
` · ${mismatch.orderCode}:头 ${money(mismatch.headerAmount)} / 明细 ${money(mismatch.itemAmount)}` +
|
||
`(差 ${mismatch.diff > 0 ? '+' : ''}${money(mismatch.diff)})`
|
||
);
|
||
}
|
||
for (const fixed of result.fixed) {
|
||
console.log(
|
||
` ✔ ${fixed.orderCode}:头 ${money(fixed.beforeAmount)} → ${money(fixed.afterAmount)},` +
|
||
`重量 → ${fixed.afterWeight} 斤`
|
||
);
|
||
}
|
||
if (!args.apply && result.mismatchCount > 0) {
|
||
console.log('[reconcile] dry-run 未改库;确认后用 `--apply` 写回。');
|
||
}
|
||
console.log(`[reconcile] JSON:${JSON.stringify(result)}`);
|
||
|
||
if (args.apply && result.remaining > 0) {
|
||
process.exitCode = 1;
|
||
}
|
||
}
|
||
|
||
main()
|
||
.catch((error) => {
|
||
console.error('[reconcile] 失败:', error instanceof Error ? error.message : error);
|
||
process.exitCode = 1;
|
||
})
|
||
.finally(async () => {
|
||
// 只用到一个连接池(src/db):脚本跑完主动收口,避免 tsx 挂住
|
||
await pool.end().catch(() => undefined);
|
||
});
|