来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
123 lines
7.0 KiB
TypeScript
123 lines
7.0 KiB
TypeScript
/**
|
|
* 归集依据消失 → 清 stale 供应商明细(改供应商/改自采后旧行留死的修复)。
|
|
*
|
|
* 为什么:`runUpsertSupplierStatementForDay` 原先在「当日无桶」或「桶金额 0 且
|
|
* 允许成本价为 0 开关关闭」时**直接早退**,存量待确认单上的旧供应商明细永远留着
|
|
* (审计 §0.7.7 存量 ¥831.50;其中 ¥531.50 两单就死在这两条早退上)。
|
|
*
|
|
* 实测(零写入干跑,事务内模拟后回滚,scripts/repair-orphan-supplier-payables.ts):
|
|
* - SS20260924003(¥27.50,无桶) -> 清明细 + 删除空壳待确认单
|
|
* - SS20260924001(br003)(¥252+¥252,无桶)-> 清明细 + 删除空壳待确认单
|
|
* - SS20260830004(¥180 stale,另有真实行)-> 'updated' 重建后 750.00 → 570.00
|
|
* - SS20260924002(¥120 stale,另有真实行)-> 'updated' 重建后 404.80 → 284.80
|
|
* 库存量:5 行 ¥831.50 → 0 行
|
|
*
|
|
* 口径约束:已确认/已收付(statement_status >= 20)一律不动 —— 与 `applyStatementRecalcs`
|
|
* 的重算守卫(tests/statementRecalcLockedGuard.test.ts)同口径;手工扣款不得被覆盖。
|
|
*/
|
|
import { readFileSync } from 'node:fs';
|
|
import path from 'node:path';
|
|
import { describe, expect, it } from 'vitest';
|
|
|
|
const src = readFileSync(
|
|
path.join(__dirname, '..', 'src/services/financeReconciliationService.ts'),
|
|
'utf8'
|
|
);
|
|
|
|
const runStart = src.indexOf('async function runUpsertSupplierStatementForDay');
|
|
const helperStart = src.indexOf('async function clearSupplierStatementWithoutBasis');
|
|
const runBody = src.slice(runStart, src.indexOf('/**\n * 归集依据消失'));
|
|
|
|
describe('归集为 0 / 无桶:清 stale 供应商明细', () => {
|
|
it('存量单查询在「无桶」判断之前(否则无桶分支拿不到单号,清不掉)', () => {
|
|
const body = src.slice(runStart, runStart + 3000);
|
|
const existingIdx = body.indexOf('FROM supplier_statements');
|
|
const noBucketIdx = body.indexOf('if (!bucket)');
|
|
expect(existingIdx).toBeGreaterThan(-1);
|
|
expect(noBucketIdx).toBeGreaterThan(existingIdx);
|
|
});
|
|
|
|
it('无桶且有存量单时走清理,而不是早退', () => {
|
|
const body = src.slice(runStart, runStart + 3000);
|
|
const noBucketBlock = body.slice(body.indexOf('if (!bucket)'), body.indexOf('const total ='));
|
|
expect(noBucketBlock).toContain("if (!existing.rows.length) return 'empty';");
|
|
expect(noBucketBlock).toContain('clearSupplierStatementWithoutBasis(');
|
|
});
|
|
|
|
it('方案 B:金额 0 照写(有订单行就建/更新),不再因 0 早退或清行', () => {
|
|
const body = src.slice(runStart, runStart + 3600);
|
|
// 生成器不再出现「0 当缺失」的两条早退
|
|
expect(body).not.toContain('if (total <= 0 && !existing.rows.length) return');
|
|
expect(body).not.toContain('if (!allowZero) {');
|
|
expect(body).not.toContain('isZeroSupplyCostAllowed');
|
|
// 有桶即落到按桶重建(写入 0 金额行)
|
|
expect(body).toContain('DELETE FROM statement_lines WHERE statement_no = $1');
|
|
});
|
|
|
|
it('清理函数:先做 >=20 冻结判断,再删明细/改头/删空壳', () => {
|
|
const helper = src.slice(helperStart, runStart);
|
|
const guardIdx = helper.indexOf('if (status >= 20)');
|
|
const deleteIdx = helper.indexOf('DELETE FROM statement_lines');
|
|
const updateIdx = helper.indexOf('UPDATE supplier_statements');
|
|
const shellIdx = helper.indexOf('tryDeletePendingZeroStatement');
|
|
expect(guardIdx).toBeGreaterThan(-1);
|
|
expect(deleteIdx).toBeGreaterThan(guardIdx);
|
|
expect(updateIdx).toBeGreaterThan(guardIdx);
|
|
expect(shellIdx).toBeGreaterThan(updateIdx);
|
|
// 冻结分支必须早退,不能落到删除
|
|
expect(helper.slice(guardIdx, deleteIdx)).toContain("return 'empty';");
|
|
});
|
|
|
|
it('只清「无供货依据」的行(按行判据,不整表清空)——避免归集日口径不一致时误伤', () => {
|
|
const helper = src.slice(helperStart, runStart);
|
|
const deleteSql = helper.slice(helper.indexOf('DELETE FROM statement_lines'), helper.indexOf('if (!cleaned.rowCount)'));
|
|
// 方案 B 孤儿判据:该行已无「仍属本供应商、有效、非仓内加工」的订单行支撑
|
|
expect(deleteSql).toContain('NOT EXISTS');
|
|
expect(deleteSql).toContain('COALESCE(oi.is_returned, false) = false');
|
|
expect(deleteSql).toContain('UPPER(TRIM(oi.supplier_code)) = $2');
|
|
expect(deleteSql).toContain('o.order_status >= 20');
|
|
expect(deleteSql).toContain('warehouse_processing');
|
|
// 不得退化成整表清空
|
|
expect(deleteSql).not.toMatch(/DELETE FROM statement_lines\s+WHERE statement_no = \$1 AND statement_type = 'supplier'\s*`/);
|
|
});
|
|
|
|
it('只清生成器写的行:售后调整行(after_sale)与退货冲销行(order_item_return)不得被顺手删掉', () => {
|
|
const helper = src.slice(helperStart, runStart);
|
|
const deleteSql = helper.slice(helper.indexOf('DELETE FROM statement_lines'), helper.indexOf('if (!cleaned.rowCount)'));
|
|
// 生成器行标记:price_source='prep' 且无 reference_type
|
|
expect(deleteSql).toContain("COALESCE(sl.price_source, '') IN ('prep', 'order')");
|
|
expect(deleteSql).toContain("COALESCE(sl.reference_type, '') = ''");
|
|
// 这两类标记值不得出现在"允许删除"的条件里(只允许出现在注释说明中)
|
|
const sqlOnly = deleteSql
|
|
.split('\n')
|
|
.filter((line) => !line.trim().startsWith('--'))
|
|
.join('\n');
|
|
expect(sqlOnly).not.toContain('after_sale');
|
|
expect(sqlOnly).not.toContain('order_item_return');
|
|
});
|
|
|
|
it('无变化才原样返回;**头表与剩余明细不一致时必须归位**(含"明细空但头表有钱"的历史单);空壳删除只在合计为 0 时发生', () => {
|
|
const helper = src.slice(helperStart, runStart);
|
|
// 不能只判「没删到行」就早退:2026-07 BR002 93 张「明细空、头表有金额」正是没有行可删的情形
|
|
expect(helper).not.toContain('if (!cleaned.rowCount) return');
|
|
expect(helper).toContain('if (!cleaned.rowCount && Math.abs(prevTotal - total) < 0.005) return');
|
|
const nothingIdx = helper.indexOf('if (!cleaned.rowCount && Math.abs(prevTotal - total) < 0.005) return');
|
|
const updateIdx = helper.indexOf('UPDATE supplier_statements');
|
|
expect(nothingIdx).toBeGreaterThan(-1);
|
|
expect(nothingIdx).toBeLessThan(updateIdx);
|
|
expect(helper).toContain('total <= 0 ? await tryDeletePendingZeroStatement');
|
|
// 放错归集日的遗留行必须清掉,否则真实日期单会重复计
|
|
expect(helper).toContain("to_char((o.created_at AT TIME ZONE 'Asia/Shanghai')::date, 'YYYYMMDD') = $3");
|
|
expect(helper).toContain('[statementNo, supplierCode, cycleDay]');
|
|
});
|
|
|
|
it('清理函数保留手工扣款、且写回为 0 时四列同写', () => {
|
|
const helper = src.slice(helperStart, runStart);
|
|
expect(helper).toContain('manualDeduction: Number(statementRow.deduction_amount) || 0');
|
|
expect(helper).toContain('paid: Number(statementRow.paid_amount) || 0');
|
|
for (const col of ['total_amount', 'payable_amount', 'deduction_amount', 'unpaid_amount']) {
|
|
expect(helper).toContain(col);
|
|
}
|
|
});
|
|
});
|