Files
SupplierSystem/src/utils/statementInvoiceStatus.test.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

316 lines
14 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import {
attachCustomerInvoiceStatus,
attachSupplierInvoiceStatus,
invoicesInDateRange,
isStatementInvoicePartial,
isStatementInvoiced,
sumCustomerInvoicePoolKpis,
sumStatementInvoiceKpiCards,
sumSupplierInvoicePoolKpis,
} from './statementInvoiceStatus';
import { filterStatementsByStatusGroup } from './statements';
describe('statement invoice status', () => {
it('treats invoiced case-insensitively and does not treat partial as invoiced', () => {
expect(isStatementInvoiced('invoiced')).toBe(true);
expect(isStatementInvoiced('pending')).toBe(false);
expect(isStatementInvoiced('partial')).toBe(false);
expect(isStatementInvoiced(null)).toBe(false);
expect(isStatementInvoicePartial('partial')).toBe(true);
});
it('filters list by pending / uninvoiced / invoiced; partial counts as uninvoiced', () => {
const rows = [
{ statement_status: 10, invoice_status: 'pending' },
{ statement_status: 30, invoice_status: 'invoiced' },
{ statement_status: 20, invoice_status: 'pending' },
{ statement_status: 10, invoice_status: 'partial' },
];
expect(filterStatementsByStatusGroup(rows, 'pending')).toHaveLength(3);
expect(filterStatementsByStatusGroup(rows, 'uninvoiced')).toHaveLength(3);
expect(filterStatementsByStatusGroup(rows, 'invoiced')).toHaveLength(1);
expect(filterStatementsByStatusGroup(rows, '')).toHaveLength(4);
});
it('keeps invoices inside the date window', () => {
const rows = invoicesInDateRange(
[
{ invoice_date: '2026-09-01', amount: 1 },
{ invoice_date: '2026-08-31', amount: 2 },
{ created_at: '2026-09-08T01:00:00Z', amount: 3 },
],
'2026-09-01',
'2026-09-08'
);
expect(rows.map((row) => row.amount)).toEqual([1, 3]);
});
it('covers statements FIFO by cycle_code: 2000+3000 with 4500', () => {
const attached = attachSupplierInvoiceStatus(
[
{ statement_no: 'SS1', supplier_code: 'S1', cycle_code: '20260801', payable_amount: 2000 },
{ statement_no: 'SS2', supplier_code: 'S1', cycle_code: '20260802', payable_amount: 3000 },
],
[{ supplier_code: 'S1', amount: 4500, invoice_no: 'INV-1' }]
);
expect(attached[0].invoice_status).toBe('invoiced');
expect(attached[0].invoice_amount).toBe(2000);
expect(attached[0].invoice_remaining).toBe(0);
expect(attached[1].invoice_status).toBe('partial');
expect(attached[1].invoice_amount).toBe(2500);
expect(attached[1].invoice_remaining).toBe(500);
});
it('ignores statement_no pins and puts all amounts in the supplier pool', () => {
const attached = attachSupplierInvoiceStatus(
[
{ statement_no: 'SS1', supplier_code: 'S1', cycle_code: '20260801', payable_amount: 100 },
{ statement_no: 'SS2', supplier_code: 'S1', cycle_code: '20260802', payable_amount: 50 },
],
[{ statement_no: 'SS2', supplier_code: 'S1', amount: 100, invoice_no: 'INV-1' }]
);
expect(attached[0].invoice_status).toBe('invoiced');
expect(attached[1].invoice_status).toBe('pending');
});
it('covers same-day statements as one payable total, not per statement', () => {
const attached = attachSupplierInvoiceStatus(
[
{ statement_no: 'SS1a', supplier_code: 'S1', cycle_code: '20260801', payable_amount: 800 },
{ statement_no: 'SS1b', supplier_code: 'S1', cycle_code: '20260801', payable_amount: 1200 },
{ statement_no: 'SS2a', supplier_code: 'S1', cycle_code: '20260802', payable_amount: 1000 },
{ statement_no: 'SS2b', supplier_code: 'S1', cycle_code: '20260802', payable_amount: 2000 },
],
[{ supplier_code: 'S1', amount: 4500 }]
);
expect(attached.filter((row) => row.cycle_code === '20260801').every((row) => row.invoice_status === 'invoiced')).toBe(
true
);
const day2 = attached.filter((row) => row.cycle_code === '20260802');
expect(day2.every((row) => row.invoice_status === 'partial')).toBe(true);
expect(day2.every((row) => row.invoice_remaining === 500)).toBe(true);
expect(day2.every((row) => row.invoice_amount === 2500)).toBe(true);
expect(sumSupplierInvoicePoolKpis(attached)).toEqual({ invoiced: 4500, uninvoiced: 500 });
});
it('uses earlier allocate rows so a later visible day stays partial', () => {
const visible = [
{ statement_no: 'SS2', supplier_code: 'S1', cycle_code: '20260802', payable_amount: 3000 },
];
const allocate = [
{ statement_no: 'SS1', supplier_code: 'S1', cycle_code: '20260801', payable_amount: 2000 },
...visible,
];
const attached = attachSupplierInvoiceStatus(
visible,
[{ supplier_code: 'S1', amount: 4500 }],
allocate
);
expect(attached).toHaveLength(1);
expect(attached[0].invoice_status).toBe('partial');
expect(attached[0].invoice_remaining).toBe(500);
});
it('ignores marked-only occupancy; pool is uploaded invoice amounts', () => {
const attached = attachCustomerInvoiceStatus(
[
{ statement_no: 'O803', customer_code: 'C1', cycle_code: '20260803', total_amount: 2000, sales_invoice_status: 'pending' },
{ statement_no: 'O809', customer_code: 'C1', cycle_code: '20260809', total_amount: 3000, sales_invoice_status: 'invoiced' },
],
[]
);
expect(attached.every((row) => row.invoice_status === 'pending')).toBe(true);
expect(sumCustomerInvoicePoolKpis(attached)).toEqual({ invoiced: 0, uninvoiced: 5000 });
});
it('covers FIFO from uploaded customer invoices only', () => {
const attached = attachCustomerInvoiceStatus(
[
{ statement_no: 'O801', customer_code: 'C1', cycle_code: '20260801', total_amount: 2000, sales_invoice_status: 'invoiced' },
{ statement_no: 'O802', customer_code: 'C1', cycle_code: '20260802', total_amount: 3000, sales_invoice_status: 'pending' },
],
[{ customer_code: 'C1', amount: 4500 }]
);
expect(attached[0].invoice_status).toBe('invoiced');
expect(attached[1].invoice_status).toBe('partial');
expect(attached[1].invoice_remaining).toBe(500);
expect(sumCustomerInvoicePoolKpis(attached)).toEqual({ invoiced: 4500, uninvoiced: 500 });
});
it('skips CS* statements from the customer waterline', () => {
const attached = attachCustomerInvoiceStatus(
[
{ statement_no: 'CS202608', customer_code: 'C1', cycle_code: '20260801', total_amount: 9000 },
{ statement_no: 'O801', customer_code: 'C1', cycle_code: '20260801', total_amount: 100 },
],
[{ customer_code: 'C1', amount: 100 }]
);
expect(attached[0].invoice_status).toBe('pending');
expect(attached[0].invoice_amount).toBe(0);
expect(attached[1].invoice_status).toBe('invoiced');
});
it('covers same-day customer statements as one payable total', () => {
const attached = attachCustomerInvoiceStatus(
[
{ statement_no: 'O1a', customer_code: 'C1', cycle_code: '20260801', total_amount: 800 },
{ statement_no: 'O1b', customer_code: 'C1', cycle_code: '20260801', total_amount: 1200 },
],
[{ customer_code: 'C1', amount: 800 }]
);
expect(attached.every((row) => row.invoice_status === 'partial')).toBe(true);
expect(attached.every((row) => row.invoice_amount === 800)).toBe(true);
expect(attached.every((row) => row.invoice_remaining === 1200)).toBe(true);
});
it('skips uploaded rows with no parsed amount', () => {
const attached = attachCustomerInvoiceStatus(
[{ statement_no: 'O801', customer_code: 'C1', cycle_code: '20260801', total_amount: 100 }],
[{ customer_code: 'C1', amount: 0 }]
);
expect(attached[0].invoice_status).toBe('pending');
});
});
// 上传即绑定(设计 20260923-invoice-statement-binding):
// 绑定票额先按单占掉应付,池只吃剩余额;池里只放「无绑定的历史票」。
describe('bound invoice coverage (20260923)', () => {
it('客户:绑定 2000 到 8/1、2500 到 8/2 → 8/1 已开、8/2 部分开还差 500', () => {
const statements = [
{ statement_no: 'O801', customer_code: 'C1', cycle_code: '20260801', total_amount: 2000 },
{ statement_no: 'O802', customer_code: 'C1', cycle_code: '20260802', total_amount: 3000 },
];
const bound = new Map([
['O801', 2000],
['O802', 2500],
]);
const attached = attachCustomerInvoiceStatus(statements, [], statements, bound);
expect(attached[0].invoice_status).toBe('invoiced');
expect(attached[0].invoice_amount).toBe(2000);
expect(attached[1].invoice_status).toBe('partial');
expect(attached[1].invoice_amount).toBe(2500);
expect(attached[1].invoice_remaining).toBe(500);
});
it('客户:换一个时间窗口看不到别的窗口的绑定票', () => {
const bound = new Map([['O801', 2000]]);
// 只看 8/2:8/1 的绑定不进这个窗口
const attached = attachCustomerInvoiceStatus(
[{ statement_no: 'O802', customer_code: 'C1', cycle_code: '20260802', total_amount: 3000 }],
[],
[{ statement_no: 'O802', customer_code: 'C1', cycle_code: '20260802', total_amount: 3000 }],
bound
);
expect(attached[0].invoice_status).toBe('pending');
expect(attached[0].invoice_remaining).toBe(3000);
});
it('客户:历史票池只吃绑定后的剩余额,互不重复', () => {
const statements = [
{ statement_no: 'O801', customer_code: 'C1', cycle_code: '20260801', total_amount: 2000 },
{ statement_no: 'O802', customer_code: 'C1', cycle_code: '20260802', total_amount: 3000 },
];
const bound = new Map([['O801', 2000]]);
// 历史池 1000:8/1 已被绑定占满,只能盖 8/2
const attached = attachCustomerInvoiceStatus(
statements,
[{ customer_code: 'C1', amount: 1000 }],
statements,
bound
);
expect(attached[0].invoice_status).toBe('invoiced');
expect(attached[1].invoice_status).toBe('partial');
expect(attached[1].invoice_amount).toBe(1000);
expect(attached[1].invoice_remaining).toBe(2000);
});
it('客户:绑定超过应付按应付封顶,不出现负数', () => {
const statements = [
{ statement_no: 'O801', customer_code: 'C1', cycle_code: '20260801', total_amount: 2000 },
];
const attached = attachCustomerInvoiceStatus(
statements,
[],
statements,
new Map([['O801', 9999]])
);
expect(attached[0].invoice_status).toBe('invoiced');
expect(attached[0].invoice_amount).toBe(2000);
expect(attached[0].invoice_remaining).toBe(0);
});
it('供应商:绑定 2000 到 8/1、2500 到 8/2 → 8/1 已开、8/2 部分开还差 500', () => {
const statements = [
{ statement_no: 'SS1', supplier_code: 'S1', cycle_code: '20260801', payable_amount: 2000 },
{ statement_no: 'SS2', supplier_code: 'S1', cycle_code: '20260802', payable_amount: 3000 },
];
const bound = new Map([
['SS1', 2000],
['SS2', 2500],
]);
const attached = attachSupplierInvoiceStatus(statements, [], statements, bound);
expect(attached[0].invoice_status).toBe('invoiced');
expect(attached[1].invoice_status).toBe('partial');
expect(attached[1].invoice_remaining).toBe(500);
expect(sumSupplierInvoicePoolKpis(attached)).toEqual({ invoiced: 4500, uninvoiced: 500 });
});
it('不传绑定时行为与旧版一致(历史票走池)', () => {
const statements = [
{ statement_no: 'O801', customer_code: 'C1', cycle_code: '20260801', total_amount: 2000 },
{ statement_no: 'O802', customer_code: 'C1', cycle_code: '20260802', total_amount: 3000 },
];
const attached = attachCustomerInvoiceStatus(statements, [{ customer_code: 'C1', amount: 4500 }]);
expect(attached[0].invoice_status).toBe('invoiced');
expect(attached[1].invoice_status).toBe('partial');
expect(attached[1].invoice_remaining).toBe(500);
});
});
// 卡片口径(2026-09-22 开发者决定):已开 = min(已上传票额合计, 应付合计);
// 未开 = 应付 − 已开。保证「已开 + 未开 = 应付」且「已开 ≤ 应付」。
describe('sumStatementInvoiceKpiCards', () => {
it('票额小于应付:已开就是上传的票面合计', () => {
// 实测场景:BR002 供应商应付 268,417.69 / 已上传一张 ¥10,630 的票
expect(sumStatementInvoiceKpiCards({ payableTotal: 268417.69, invoicePoolTotal: 10630 })).toEqual({
invoiced: 10630,
uninvoiced: 257787.69,
});
});
it('票额超过应付:已开以上限收口,未开为 0(不出现「已开 > 应付」)', () => {
expect(sumStatementInvoiceKpiCards({ payableTotal: 1000, invoicePoolTotal: 3000 })).toEqual({
invoiced: 1000,
uninvoiced: 0,
});
});
it('没有票 / 没有应付时都为 0,且容忍脏值', () => {
expect(sumStatementInvoiceKpiCards({ payableTotal: 500, invoicePoolTotal: 0 })).toEqual({
invoiced: 0,
uninvoiced: 500,
});
expect(sumStatementInvoiceKpiCards({ payableTotal: 0, invoicePoolTotal: 100 })).toEqual({
invoiced: 0,
uninvoiced: 0,
});
expect(sumStatementInvoiceKpiCards({ payableTotal: NaN, invoicePoolTotal: -5 })).toEqual({
invoiced: 0,
uninvoiced: 0,
});
});
it('四舍五入到分,且「已开 + 未开 = 应付」在分位成立', () => {
expect(sumStatementInvoiceKpiCards({ payableTotal: 10, invoicePoolTotal: 3.333 })).toEqual({
invoiced: 3.33,
uninvoiced: 6.67,
});
// 亚分位输入也不破坏恒等式
const odd = sumStatementInvoiceKpiCards({ payableTotal: 10.005, invoicePoolTotal: 3.333 });
expect(Math.round((odd.invoiced + odd.uninvoiced) * 100) / 100).toBe(
Math.round(Math.max(0, 10.005) * 100) / 100
);
});
});