来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
121 lines
5.0 KiB
TypeScript
121 lines
5.0 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
||
import path from 'node:path';
|
||
import { describe, expect, it } from 'vitest';
|
||
import { computeSupplierStatementAmounts } from '../src/services/financeReconciliationService';
|
||
|
||
/**
|
||
* 供应商结算头金额口径(2026-09-22 修复):
|
||
* total = Σ 明细行 line_amount(该列已是「结算量 × 单价」净额)
|
||
* payable = total − 手工扣款(deduction_amount 只表示财务手工扣款)
|
||
* unpaid = payable − paid
|
||
*
|
||
* 回归背景:表头 deduction_amount 曾被写成「行级扣量金额合计」,
|
||
* 导致应付/未开票/已开票被重复扣一次,并出现「应付总额 < 待付金额」。
|
||
*/
|
||
describe('computeSupplierStatementAmounts', () => {
|
||
it('应付 = 总额 − 手工扣款;待付 = 应付 − 已付', () => {
|
||
expect(computeSupplierStatementAmounts({ total: 1000, manualDeduction: 200, paid: 300 })).toEqual({
|
||
total: 1000,
|
||
deduction: 200,
|
||
payable: 800,
|
||
unpaid: 500,
|
||
});
|
||
});
|
||
|
||
it('无手工扣款时应付等于总额(行级扣量金额不得再减一次)', () => {
|
||
// SS20260805003:备货 565.20 / 结算净额 544.48 / 行级扣量 20.72 → 应付必须是 544.48
|
||
expect(computeSupplierStatementAmounts({ total: 544.48, manualDeduction: 0, paid: 0 })).toEqual({
|
||
total: 544.48,
|
||
deduction: 0,
|
||
payable: 544.48,
|
||
unpaid: 544.48,
|
||
});
|
||
});
|
||
|
||
it('已付超过应付时待付不为负;扣款超过总额时应付不为负', () => {
|
||
expect(computeSupplierStatementAmounts({ total: 100, paid: 150 }).unpaid).toBe(0);
|
||
expect(computeSupplierStatementAmounts({ total: 100, manualDeduction: 300 }).payable).toBe(0);
|
||
expect(computeSupplierStatementAmounts({ total: 100, manualDeduction: 300 }).unpaid).toBe(0);
|
||
});
|
||
|
||
it('容忍空值与脏值,且四舍五入到分', () => {
|
||
expect(computeSupplierStatementAmounts({ total: NaN, manualDeduction: null, paid: undefined })).toEqual({
|
||
total: 0,
|
||
deduction: 0,
|
||
payable: 0,
|
||
unpaid: 0,
|
||
});
|
||
expect(computeSupplierStatementAmounts({ total: 10.005, manualDeduction: 0.005 }).payable).toBe(10);
|
||
});
|
||
});
|
||
|
||
describe('生成/重算口径不再重复扣减(源码棘轮)', () => {
|
||
const source = readFileSync(
|
||
path.join(__dirname, '..', 'src', 'services', 'financeReconciliationService.ts'),
|
||
'utf8'
|
||
);
|
||
|
||
it('生成器的「行级扣量合计」变量已移除', () => {
|
||
expect(source).not.toContain('lineDeduction');
|
||
expect(source).not.toContain('lineDeductionForInsert');
|
||
});
|
||
|
||
it('生成器两处(更新/新建)都走 computeSupplierStatementAmounts', () => {
|
||
const calls = source.match(/computeSupplierStatementAmounts\(/g) || [];
|
||
// 1 处定义 + 更新分支 + 新建分支
|
||
expect(calls.length).toBeGreaterThanOrEqual(3);
|
||
expect(source).toContain('manualDeduction: existing.rows[0].deduction_amount');
|
||
expect(source).toContain('manualDeduction: 0');
|
||
});
|
||
|
||
it('生成器更新分支读取存量手工扣款,不覆盖财务填的扣款', () => {
|
||
expect(source).toContain('COALESCE(deduction_amount, 0)::numeric AS deduction_amount');
|
||
});
|
||
|
||
it('applyStatementRecalcs 的供应商分支同步写 unpaid_amount', () => {
|
||
const start = source.indexOf('async function applyStatementRecalcs');
|
||
expect(start).toBeGreaterThan(-1);
|
||
const block = source.slice(start, start + 6000);
|
||
const supplierBranch = block.slice(block.indexOf("UPDATE supplier_statements"));
|
||
expect(supplierBranch).toContain('unpaid_amount');
|
||
expect(supplierBranch).toContain('COALESCE(paid_amount, 0)');
|
||
});
|
||
|
||
it('客户分支只写 total/unpaid(客户表没有 payable_amount 列)', () => {
|
||
const start = source.indexOf('async function applyStatementRecalcs');
|
||
const block = source.slice(start, start + 6000);
|
||
const customerStart = block.indexOf('UPDATE customer_statements');
|
||
const supplierStart = block.indexOf('UPDATE supplier_statements');
|
||
expect(customerStart).toBeGreaterThan(-1);
|
||
expect(supplierStart).toBeGreaterThan(customerStart);
|
||
const customerSql = block
|
||
.slice(customerStart, supplierStart)
|
||
.split('\n')
|
||
.filter((line) => !line.trim().startsWith('//'))
|
||
.join('\n');
|
||
expect(customerSql).not.toContain('payable_amount');
|
||
});
|
||
});
|
||
|
||
describe('数据回填迁移', () => {
|
||
const sql = readFileSync(
|
||
path.join(__dirname, '..', '..', 'migrations', '20260922_supplier_statement_amount_caliber.sql'),
|
||
'utf8'
|
||
);
|
||
|
||
it('摘出纯行级扣量:只命中「表头扣款 == 行扣款合计」且未结清的行', () => {
|
||
expect(sql).toContain('deduction_amount = 0');
|
||
expect(sql).toContain('ABS(COALESCE(ss.deduction_amount, 0) - line_agg.line_ded) < 0.005');
|
||
expect(sql).toContain('COALESCE(ss.statement_status, 0) < 30');
|
||
});
|
||
|
||
it('重算 unpaid_amount 且幂等', () => {
|
||
expect(sql).toContain('unpaid_amount IS DISTINCT FROM');
|
||
});
|
||
|
||
it('缺列的 schema 会跳过而不是报错中断迁移', () => {
|
||
expect(sql).toContain('information_schema.columns');
|
||
expect(sql).toContain('跳过');
|
||
});
|
||
});
|