来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
98 lines
5.0 KiB
TypeScript
98 lines
5.0 KiB
TypeScript
/**
|
|
* 客户结算 = **订单的投影**:任何改动订单行销售数据(数量/单价/金额/sku/供应商)的入口,
|
|
* 都必须同步重建结算单(客户 + 供应商投影)。
|
|
*
|
|
* 开发者口径(2026-09-24):「要是订单的销售价修改了,那么客户结算这块也该同步变化的,
|
|
* 相当于数据源就是订单的数据呀」。
|
|
*
|
|
* 实测漏接的 4 条(都在结案后改价/改量,导致结算停在旧值):
|
|
* ① 供货单位同步 `syncOrderItemsFromSupplyCompletion` / `syncOrderItemFromPickingComplete`
|
|
* ② 换品替代确认 `v9CompletionService`
|
|
* ③ 重新分配(备货路由)`preparationRoutes`
|
|
* ④ 补录/单位同步等历史路径 → 见 financeReconciliationService.syncStatementsForOrderSafe 注释
|
|
* 9 月因此出现 2 行陈旧单价(差额 ¥31)。
|
|
*
|
|
* 本测试**静态枚举**所有 `UPDATE order_items SET ...price/quantity/amount/locked_price` 的语句,
|
|
* 要求所在文件调用 `syncStatementsForOrderSafe`(或已接入的 `upsertCustomerStatementForOrder`)。
|
|
* 新增写入点若忘了接刷新,这里立刻失败。
|
|
*/
|
|
import { readFileSync, readdirSync } from 'node:fs';
|
|
import path from 'node:path';
|
|
import { describe, expect, it } from 'vitest';
|
|
|
|
const SRC = path.join(__dirname, '..', 'src');
|
|
const SALE_COLUMNS = ['price', 'locked_price', 'quantity', 'amount'] as const;
|
|
|
|
function collectTsFiles(dir: string, out: string[] = []): string[] {
|
|
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
if (entry.name === 'node_modules' || entry.name.startsWith('.')) continue;
|
|
const full = path.join(dir, entry.name);
|
|
if (entry.isDirectory()) collectTsFiles(full, out);
|
|
else if (entry.name.endsWith('.ts')) out.push(full);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/** 找出真正写入销售字段的 (文件, 行号) */
|
|
function findSaleWriters(): Array<{ file: string; line: number; cols: string[] }> {
|
|
const hits: Array<{ file: string; line: number; cols: string[] }> = [];
|
|
for (const file of collectTsFiles(SRC)) {
|
|
const text = readFileSync(file, 'utf8');
|
|
for (const m of text.matchAll(/UPDATE\s+order_items\b/gi)) {
|
|
const seg = text.slice(m.index!, m.index! + 800);
|
|
const cut = Math.min(...[seg.indexOf('`'), seg.indexOf(';')].filter((x) => x > 0).concat([seg.length]));
|
|
const stmt = seg.slice(0, cut);
|
|
const parts = stmt.split(/\bset\b/i);
|
|
if (parts.length < 2) continue;
|
|
const cols = SALE_COLUMNS.filter((c) => new RegExp(`\\b${c}\\b`).test(parts[1]));
|
|
if (cols.length) hits.push({ file, line: text.slice(0, m.index!).split('\n').length, cols: [...cols] });
|
|
}
|
|
}
|
|
return hits;
|
|
}
|
|
|
|
describe('订单销售数据变更 → 结算投影必须同步', () => {
|
|
it('所有写销售字段的入口都接入了同步助手', () => {
|
|
const writers = findSaleWriters();
|
|
expect(writers.length).toBeGreaterThanOrEqual(5);
|
|
const missing: string[] = [];
|
|
for (const w of writers) {
|
|
const text = readFileSync(w.file, 'utf8');
|
|
const ok = text.includes('syncStatementsForOrderSafe') || text.includes('upsertCustomerStatementForOrder');
|
|
if (!ok) missing.push(`${path.relative(SRC, w.file)}:${w.line} (${w.cols.join(',')})`);
|
|
}
|
|
expect(missing, `以下写入点未接结算同步:${missing.join(' ; ')}`).toEqual([]);
|
|
});
|
|
|
|
it('已接入的四个入口逐个点名(防被删/改名后断言空转)', () => {
|
|
const expectHas = (rel: string) => {
|
|
const text = readFileSync(path.join(SRC, rel), 'utf8');
|
|
expect(text, `${rel} 必须调用结算同步`).toContain('syncStatementsForOrderSafe');
|
|
};
|
|
expectHas('services/orderItemSupplyUnitSync.ts');
|
|
expectHas('services/v9CompletionService.ts');
|
|
expectHas('modules/workflow/routes/preparationRoutes.ts');
|
|
// 原有路径(订单保存 / 联动改价 / 退货)保持
|
|
expect(readFileSync(path.join(SRC, 'services/orderManageItemsService.ts'), 'utf8')).toContain(
|
|
'upsertCustomerStatementForOrder'
|
|
);
|
|
expect(readFileSync(path.join(SRC, 'services/purchaseLinkedSalePriceService.ts'), 'utf8')).toContain(
|
|
'upsertCustomerStatementForOrder'
|
|
);
|
|
});
|
|
|
|
it('同步助手:客户结算整表重建、供应商结算按订单行重建、失败不阻断、≥20 由生成器 skipped', () => {
|
|
const fin = readFileSync(path.join(SRC, 'services/financeReconciliationService.ts'), 'utf8');
|
|
const start = fin.indexOf('export async function syncStatementsForOrderSafe');
|
|
expect(start).toBeGreaterThan(-1);
|
|
const body = fin.slice(start, start + 1400);
|
|
expect(body).toContain('await upsertCustomerStatementForOrder(code, client || undefined)');
|
|
expect(body).toContain('await ensureSupplierStatementsForOrder(code, client || undefined)');
|
|
expect(body).toContain('catch (error)');
|
|
// 已确认/已收款不得被静默改额:生成器对 >=20 返回 skipped
|
|
const gen = fin.slice(fin.indexOf('async function runUpsertCustomerStatementForOrder'));
|
|
expect(gen).toContain('>= 20');
|
|
expect(gen).toContain("return 'skipped';");
|
|
});
|
|
});
|