feat(contract): 合同付款预警bug修复

This commit is contained in:
sophie committed 2026-10-28 09:09:53 +08:00
1 parent b02b776a97
commit f0334e5d22
16 files changed
+383 -104

No files matched your search

@@ -1,11 +1,14 @@
/**
* features/contract-payment/utils/paymentStatus.test.ts
* 用途:锁定台账阶段格子的逾期与缺日期判定,避免验收款有过期日期却不显示已逾期。
* 依赖:paymentStatus.classifyLedgerStageCell。
* 依赖:paymentStatus.classifyLedgerStageCell、firstRoundAlertDate、payableDate、isNothingToPay、aggregateContractNodeStatus。
* 逻辑:用固定业务日覆盖验收款已逾期、质保款未到期,以及零金额阶段显示为空格。
* 另断言详情页首轮预警日与第 1 次预警同一天,验收款不再把归档日当成预警日期。
* 质保款应付日是验收归档日加质保期,不能回退成验收日。
* 金额和比例都为 0 的阶段不参与合同逾期,也不再标已逾期。
*/
import { describe, expect, it } from 'vitest';
import { classifyLedgerStageCell } from './paymentStatus';
import { classifyLedgerStageCell, firstRoundAlertDate, aggregateContractNodeStatus, isNothingToPay, payableDate } from './paymentStatus';
const TODAY = '2026-10-07';
@@ -41,14 +44,15 @@ describe('classifyLedgerStageCell', () => {
}, 'acceptance', TODAY)).toBe('missing_date');
});
it('keeps paid prepayment as paid and a zero stage as an empty cell', () => {
it('keeps a paid acceptance as paid and a zero stage as an empty cell', () => {
// 验收款对上 ERP 付款后必须显示已付,不能因为归档日已过继续标已逾期。
expect(classifyLedgerStageCell({
status: 'paid',
percentage: '30',
amount: '6600.00',
due_date: '2026-08-12',
anchor_date: '2026-08-12',
}, 'prepayment', TODAY)).toBe('paid');
percentage: '20',
amount: '11160.00',
due_date: '2026-10-24',
anchor_date: '2026-10-24',
}, 'acceptance', TODAY)).toBe('paid');
expect(classifyLedgerStageCell({
status: 'active',
percentage: '0',
@@ -58,3 +62,116 @@ describe('classifyLedgerStageCell', () => {
}, 'acceptance', TODAY)).toBe('absent');
});
});
describe('firstRoundAlertDate', () => {
it('uses archive plus 30 days for acceptance so it matches the first alert', () => {
// 验收款 due_date 与归档日相同,预警日期必须取首轮而不是把 9 月 24 日再印一遍。
expect(firstRoundAlertDate({
node_type: 'acceptance',
anchor_date: '2026-09-24',
due_date: '2026-09-24',
})).toBe('2026-10-24');
});
it('keeps prepayment, shipment and warranty on their first-round offsets', () => {
expect(firstRoundAlertDate({
node_type: 'prepayment',
anchor_date: '2026-09-18',
due_date: '2026-09-18',
})).toBe('2026-09-25');
expect(firstRoundAlertDate({
node_type: 'shipment',
anchor_date: '2026-08-25',
due_date: '2026-08-25',
})).toBe('2026-08-18');
expect(firstRoundAlertDate({
node_type: 'warranty',
anchor_date: '2026-09-24',
due_date: '2027-09-24',
})).toBe('2027-09-17');
});
it('returns null when the stage has no date to count from', () => {
expect(firstRoundAlertDate({
node_type: 'acceptance',
anchor_date: null,
due_date: null,
})).toBeNull();
expect(firstRoundAlertDate({
node_type: 'warranty',
anchor_date: '2026-09-24',
due_date: null,
})).toBeNull();
});
});
describe('payableDate', () => {
it('uses acceptance plus the warranty term for warranty, not the acceptance date', () => {
// 质保款 anchor_date 是验收归档日,应付日必须取 due_date。
expect(payableDate({
node_type: 'warranty',
anchor_date: '2026-09-24',
due_date: '2027-03-23',
})).toBe('2027-03-23');
expect(payableDate({
node_type: 'warranty',
anchor_date: '2026-09-24',
due_date: null,
})).toBeNull();
});
it('keeps the existing due date for prepayment, shipment and acceptance', () => {
expect(payableDate({
node_type: 'prepayment',
anchor_date: '2026-09-02',
due_date: '2026-09-02',
})).toBe('2026-09-02');
expect(payableDate({
node_type: 'shipment',
anchor_date: '2026-08-25',
due_date: '2026-08-25',
})).toBe('2026-08-25');
expect(payableDate({
node_type: 'acceptance',
anchor_date: '2026-09-24',
due_date: '2026-09-24',
})).toBe('2026-09-24');
});
});
describe('isNothingToPay', () => {
it('does not treat a zero acceptance or warranty stage as overdue', () => {
// 金额和比例都是 0 时没有应付金额,合同状态不能被这个阶段拉成已逾期。
const acceptance = {
status: 'active',
percentage: '0',
amount: '0.00',
due_date: '2026-09-24',
anchor_date: '2026-09-24',
node_type: 'acceptance',
};
const warranty = {
status: 'active',
percentage: '0',
amount: '0.00',
due_date: '2027-03-23',
anchor_date: '2026-09-24',
node_type: 'warranty',
};
expect(isNothingToPay(acceptance)).toBe(true);
expect(classifyLedgerStageCell(acceptance, 'acceptance', TODAY)).toBe('absent');
expect(aggregateContractNodeStatus([
{ ...acceptance, id: 1 } as never,
{ ...warranty, id: 2 } as never,
{
id: 3,
status: 'paid',
percentage: '50',
amount: '4250.00',
due_date: '2026-09-02',
anchor_date: '2026-09-02',
node_type: 'prepayment',
} as never,
])).toBe('completed');
});
});
@@ -4,6 +4,8 @@
* 依赖:contract-payment.api 的 PaymentNode 类型、浏览器 Date。
* 逻辑:付款节点到期/逾期以节点 due_date 为准;台账状态按节点聚合,供列表页一行一眼看是否逾期。
* 阶段格子的已逾期/即将到期/未到期也走这里,验收款和质保款与预付款、出货款共用同一比较。
* 金额和比例都为 0 的阶段视为无需付款:详情不标已逾期,台账汇总也不把它算进逾期。
* 详情页「预警日期」用 firstRoundAlertDate,与下方第 1 次预警同一天,不直接展示 due_date。
*/
import type { PaymentNode } from '../api/contract-payment.api';
@@ -70,36 +72,92 @@ export function nodeDueReference(node: Pick<PaymentNode, 'due_date' | 'anchor_da
return node.due_date || node.anchor_date || null;
}
/**
* 返回详情页应展示的应付日期。
* 核心逻辑:质保款的应付日是验收归档日加上合同质保期,存在 due_date;没有质保期时不回退验收日。
* 预付款、出货款、验收款的 due_date 就是应付日,缺 due_date 时回退 anchor_date。
* @param node - 付款节点,至少含阶段类型和两个日期。
* @returns 应付日 YYYY-MM-DD;质保款未算出到期日时返回 null。
*/
export function payableDate(
node: Pick<PaymentNode, 'node_type' | 'anchor_date' | 'due_date'>,
): string | null {
const nodeType = String(node.node_type || '').trim().toLowerCase();
if (nodeType === 'warranty') return node.due_date || null;
return node.due_date || node.anchor_date || null;
}
/**
* 判断这一阶段是否没有应付金额。
* 核心逻辑:状态已是无此阶段,或比例和金额都是 0(含空金额)。已付节点不按 0 处理。
* @param node - 付款节点。
* @returns 无需付款时为 true。
*/
export function isNothingToPay(
node: Pick<PaymentNode, 'status' | 'percentage' | 'amount'>,
): boolean {
const status = String(node.status || '').trim().toLowerCase();
if (status === 'not_applicable') return true;
if (status === 'paid') return false;
const percentValue = Number(node.percentage);
const amountValue = Number(node.amount);
return Number.isFinite(percentValue)
&& percentValue === 0
&& (node.amount == null || node.amount === '' || amountValue === 0);
}
/** 各阶段首轮相对锚点的自然日偏移,与后端 alert_service._ALERT_RULES 第一轮一致。 */
const FIRST_ROUND_OFFSET_DAYS: Record<string, number> = {
prepayment: 7,
shipment: -7,
acceptance: 30,
warranty: -7,
};
/**
* 计算详情页应展示的首轮预警日期。
* 核心逻辑:预付款、验收款从 anchor_date 起算(+7 / +30 天);出货款、质保款从 due_date 起算(-7 天)。
* 验收款的 due_date 被写成归档日,不能拿来当预警日期,否则会和「第 1 次预警」相差 30 天。
* @param node - 付款节点,至少含阶段类型和两个日期。
* @returns 首轮计划日 YYYY-MM-DD;缺少对应锚点时返回 null。
*/
export function firstRoundAlertDate(
node: Pick<PaymentNode, 'node_type' | 'anchor_date' | 'due_date'>,
): string | null {
const nodeType = String(node.node_type || '').trim().toLowerCase();
const offset = FIRST_ROUND_OFFSET_DAYS[nodeType];
if (offset == null) return node.due_date || null;
const basis = nodeType === 'shipment' || nodeType === 'warranty'
? node.due_date
: (node.anchor_date || node.due_date);
if (!basis) return null;
const date = new Date(`${basis}T00:00:00Z`);
if (Number.isNaN(date.getTime())) return null;
date.setUTCDate(date.getUTCDate() + offset);
return date.toISOString().slice(0, 10);
}
/** 台账阶段格子的状态:空格、已付、缺日期、已逾期、即将到期、未到期。 */
export type LedgerStageCellKind = 'absent' | 'paid' | 'missing_date' | 'overdue' | 'due' | 'active';
/**
* 判断台账里一个付款阶段格子该显示哪种状态。
* 核心逻辑:无节点、不适用或金额比例都是 0 视为空格。只有预付款和出货款在已付时返回 paid。
* 核心逻辑:无节点、不适用或金额比例都是 0 视为空格。四个阶段在 ERP 对上付款后都返回 paid。
* 其余未付阶段没有日期依据则返回 missing_date;有日期则按业务日比较已逾期、7 日内即将到期或未到期。
* 验收款、质保款与预付款、出货款共用这套日期比较,不因没有付款回写而省略已逾期。
* @param node - 付款节点;该阶段不存在时传 null。
* @param nodeType - 阶段类型,用来区分能否标已付。
* @param _nodeType - 保留给列表调用;已付不再按阶段类型区分。
* @param todayIso - 可选业务日 YYYY-MM-DD。
* @returns 格子状态种类。
*/
export function classifyLedgerStageCell(
node: Pick<PaymentNode, 'status' | 'percentage' | 'amount' | 'due_date' | 'anchor_date'> | null | undefined,
nodeType: string,
_nodeType: string,
todayIso?: string,
): LedgerStageCellKind {
if (!node) return 'absent';
const status = String(node.status || '').trim().toLowerCase();
const percentValue = Number(node.percentage);
const amountValue = Number(node.amount);
const absent = status === 'not_applicable' || (
status !== 'paid'
&& Number.isFinite(percentValue)
&& percentValue === 0
&& (node.amount == null || node.amount === '' || amountValue === 0)
);
if (absent) return 'absent';
if ((nodeType === 'prepayment' || nodeType === 'shipment') && status === 'paid') return 'paid';
if (isNothingToPay(node)) return 'absent';
if (status === 'paid') return 'paid';
const basis = nodeDueReference(node);
if (!basis) return 'missing_date';
if (isNodeOverdue(basis, todayIso)) return 'overdue';
@@ -112,14 +170,17 @@ export type ContractNodeStatusCode = 'active' | 'due' | 'overdue' | 'completed'
/**
* 按付款节点聚合出合同台账的进度/逾期状态。
* 核心逻辑:先在合同层排除「待补充」(非激活或解析待确认);否则若存在未付且已过 due_date 的
* 节点 → 已逾期;存在 7 天内到期 → 即将到期;仍有未付且付款日在 7 日外 → 未到期;全部节点已付 → 已完成。
* 核心逻辑:金额和比例都为 0 的阶段不参与逾期判断。其余进行中节点已过 due_date → 已逾期;
* 存在 7 天内到期 → 即将到期;仍有未付且付款日在 7 日外 → 未到期;全部节点已付 → 已完成。
* @param nodes - 合同的付款节点列表。
* @returns 台账状态编码。
*/
export function aggregateContractNodeStatus(nodes: PaymentNode[]): ContractNodeStatusCode {
if (!nodes || nodes.length === 0) return 'needs_completion';
const active = nodes.filter((n) => String(n.status || '').trim().toLowerCase() === 'active');
const active = nodes.filter((n) => {
const status = String(n.status || '').trim().toLowerCase();
return status === 'active' && !isNothingToPay(n);
});
if (active.length === 0) {
// 无进行中的节点:存在已付则视为整个付款计划已完成,否则判为待补充。
const hasPaid = nodes.some((n) => String(n.status || '').trim().toLowerCase() === 'paid' || !!n.paid_at);