Files
cz-web/src/utils/billVo.ts
2026-10-01 17:24:57 +08:00

92 lines
2.9 KiB
TypeScript

import type { Bill, BillStatus, BillVo } from '@/types/business/finance'
const STATUS_TEXT: Record<string, BillStatus> = {
UNSTARTED: '未到期',
PENDING: '待支付',
PAID: '已支付',
CANCELLED: '已取消',
}
export function toBillStatus(code?: string): BillStatus {
if (!code) return '未到期'
return STATUS_TEXT[code] ?? '未到期'
}
export function toBill(vo: BillVo): Bill {
return {
id: String(vo.id),
planName: vo.periodName ?? '',
billNo: vo.billNo ?? '',
tradeNo: vo.transactionNo,
projectName: vo.projectName ?? '',
amount: Number(vo.amount ?? 0),
status: toBillStatus(vo.status),
createTime: vo.createTime ?? '',
payChannel: vo.payChannel,
payTime: vo.payTime,
}
}
export function toBills(list: BillVo[] | null | undefined): Bill[] {
if (!list || list.length === 0) return []
return list.filter((item) => item != null && item.id != null).map(toBill)
}
export function toPlanStatus(code?: string | null): BillStatus {
const value = (code ?? '').toUpperCase()
if (value === 'DONE' || value === 'PAID' || value === 'COMPLETED') return '已支付'
if (value === 'ACTIVE' || value === 'PENDING') return '待支付'
if (value === 'CLOSED' || value === 'CANCELLED') return '已取消'
return '未到期'
}
export interface PlanLike {
id?: number | string | null
projectName?: string | null
totalAmount?: number | string | null
periods?: number | null
currentPeriod?: number | null
periodRule?: string | null
status?: string | null
orderNo?: string | null
transactionNo?: string | null
payChannel?: string | null
payStatus?: string | null
payTime?: string | null
}
function periodLabel(period: number, periods: number): string {
if (period <= 1) return '预付款'
if (period >= periods) return '尾款'
return '进度款'
}
export function toPlanBill(vo: PlanLike): Bill {
const period = vo.currentPeriod ?? 0
const periods = vo.periods ?? 0
const rule = (vo.periodRule ?? '').split(',').map((part) => Number(part.trim()))
const segment = rule[period - 1]
const ratio = Number.isFinite(segment) ? segment : periods > 0 ? 100 / periods : 0
const total = Number(vo.totalAmount ?? 0)
const paid = (vo.payStatus ?? '').toUpperCase() === 'SUCCESS'
return {
id: String(vo.id ?? ''),
planName: period > 0 && periods > 0
? `第${period}期·${periodLabel(period, periods)}`
: (vo.periodRule ?? '付款计划'),
billNo: vo.orderNo ?? '',
tradeNo: vo.transactionNo ?? undefined,
projectName: vo.projectName ?? '',
amount: Math.round(total * ratio) / 100,
status: paid ? '已支付' : toPlanStatus(vo.status),
createTime: '',
payChannel: vo.payChannel ?? undefined,
payTime: vo.payTime ?? undefined,
}
}
export function toPlanBills(list: readonly PlanLike[] | null | undefined): Bill[] {
if (!list || list.length === 0) return []
return list.filter((item) => item != null && item.id != null).map(toPlanBill)
}