fix: 合同付款预警优化
This commit is contained in:
1 parent
83e0bad22f
commit
fe5191b38a
15 files changed
+304
-97
No files matched your search
@@ -3,38 +3,60 @@
|
||||
* 用途:合同付款节点的到期/逾期判定,以及台账级“按节点聚合的付款状态”推导。
|
||||
* 依赖:contract-payment.api 的 PaymentNode 类型、浏览器 Date。
|
||||
* 逻辑:付款节点到期/逾期以节点 due_date 为准;台账状态按节点聚合,供列表页一行一眼看是否逾期。
|
||||
* 阶段格子的已逾期/即将到期/未到期也走这里,验收款和质保款与预付款、出货款共用同一比较。
|
||||
*/
|
||||
|
||||
import type { PaymentNode } from '../api/contract-payment.api';
|
||||
|
||||
/**
|
||||
* 把 YYYY-MM-DD 归一成 UTC 零点,供到期比较。
|
||||
* 核心逻辑:缺省取当前 UTC 日历日;测试传入 todayIso 时固定业务日,避免用例随真实日期漂移。
|
||||
* @param todayIso - 可选业务日。
|
||||
* @returns 可用于相减的日期;无法解析时返回 Invalid Date。
|
||||
*/
|
||||
function calendarDay(todayIso?: string): Date {
|
||||
const iso = todayIso || new Date().toISOString().slice(0, 10);
|
||||
return new Date(`${iso}T00:00:00Z`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算到期日相对业务日的天数。
|
||||
* 核心逻辑:两日都按 UTC 零点取值,负数表示已过期。
|
||||
* @param due - 到期日期(YYYY-MM-DD)。
|
||||
* @param todayIso - 可选业务日。
|
||||
* @returns 相差天数;日期无效时返回 null。
|
||||
*/
|
||||
function daysUntil(due: string, todayIso?: string): number | null {
|
||||
const target = new Date(`${due}T00:00:00Z`);
|
||||
const today = calendarDay(todayIso);
|
||||
if (Number.isNaN(target.getTime()) || Number.isNaN(today.getTime())) return null;
|
||||
return Math.round((target.getTime() - today.getTime()) / 86400000);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断某个日期是否临近到期(含今天起 7 天内)。
|
||||
* 核心逻辑:与 isNodeOverdue 共用 UTC 日期归一;范围内(0..7 天)判定为临近到期。
|
||||
* @param due - 到期日期(YYYY-MM-DD)。
|
||||
* @param todayIso - 可选业务日,缺省为当前 UTC 日历日。
|
||||
* @returns 是否临近到期。
|
||||
*/
|
||||
export function isNodeNearDue(due?: string | null): boolean {
|
||||
export function isNodeNearDue(due?: string | null, todayIso?: string): boolean {
|
||||
if (!due) return false;
|
||||
const target = new Date(`${due}T00:00:00Z`);
|
||||
if (Number.isNaN(target.getTime())) return false;
|
||||
const today = new Date(`${new Date().toISOString().slice(0, 10)}T00:00:00Z`);
|
||||
const days = Math.round((target.getTime() - today.getTime()) / 86400000);
|
||||
return days >= 0 && days <= 7;
|
||||
const days = daysUntil(due, todayIso);
|
||||
return days !== null && days >= 0 && days <= 7;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断某个日期是否已逾期(早于今天)。
|
||||
* 核心逻辑:与 isNodeNearDue 共用 UTC 日期归一;due_date 早于今天即判定为逾期。
|
||||
* @param due - 到期日期(YYYY-MM-DD)。
|
||||
* @param todayIso - 可选业务日,缺省为当前 UTC 日历日。
|
||||
* @returns 是否已逾期。
|
||||
*/
|
||||
export function isNodeOverdue(due?: string | null): boolean {
|
||||
export function isNodeOverdue(due?: string | null, todayIso?: string): boolean {
|
||||
if (!due) return false;
|
||||
const target = new Date(`${due}T00:00:00Z`);
|
||||
if (Number.isNaN(target.getTime())) return false;
|
||||
const today = new Date(`${new Date().toISOString().slice(0, 10)}T00:00:00Z`);
|
||||
const days = Math.round((target.getTime() - today.getTime()) / 86400000);
|
||||
return days < 0;
|
||||
const days = daysUntil(due, todayIso);
|
||||
return days !== null && days < 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,6 +70,43 @@ export function nodeDueReference(node: Pick<PaymentNode, 'due_date' | 'anchor_da
|
||||
return node.due_date || node.anchor_date || null;
|
||||
}
|
||||
|
||||
/** 台账阶段格子的状态:空格、已付、缺日期、已逾期、即将到期、未到期。 */
|
||||
export type LedgerStageCellKind = 'absent' | 'paid' | 'missing_date' | 'overdue' | 'due' | 'active';
|
||||
|
||||
/**
|
||||
* 判断台账里一个付款阶段格子该显示哪种状态。
|
||||
* 核心逻辑:无节点、不适用或金额比例都是 0 视为空格。只有预付款和出货款在已付时返回 paid。
|
||||
* 其余未付阶段没有日期依据则返回 missing_date;有日期则按业务日比较已逾期、7 日内即将到期或未到期。
|
||||
* 验收款、质保款与预付款、出货款共用这套日期比较,不因没有付款回写而省略已逾期。
|
||||
* @param node - 付款节点;该阶段不存在时传 null。
|
||||
* @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,
|
||||
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';
|
||||
const basis = nodeDueReference(node);
|
||||
if (!basis) return 'missing_date';
|
||||
if (isNodeOverdue(basis, todayIso)) return 'overdue';
|
||||
if (isNodeNearDue(basis, todayIso)) return 'due';
|
||||
return 'active';
|
||||
}
|
||||
|
||||
/** 台账级付款状态编码:未到期 / 即将到期 / 已逾期 / 已完成 / 待补充。 */
|
||||
export type ContractNodeStatusCode = 'active' | 'due' | 'overdue' | 'completed' | 'needs_completion';
|
||||
|
||||
|
||||
Reference in new issue
Block a user