feat(web-admin): add last-period due amount calc helpers
Pure functions for fq auto-fill of the final installment amount with override/lock gates, covered by unit tests. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
13ac45d10d
commit
5fd5550e0d
2 files changed
+73
-1
No files matched your search
@@ -409,3 +409,34 @@ export function applyExpandRemainingToInstallment<T extends Record<string, any>>
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/** 分期:最后一期应缴 = 合同 − 前 N−1 期;无法计算返回 null;负值钳为 0 */
|
||||
export function calcLastPeriodDueAmount(
|
||||
contractAmount: number | null | undefined,
|
||||
priorAmounts: Array<number | string | null | undefined>
|
||||
): number | null {
|
||||
// Number(null) === 0;须先排除空值,否则会误算尾期
|
||||
if (contractAmount == null) return null
|
||||
const contract = Number(contractAmount)
|
||||
if (!Number.isFinite(contract)) return null
|
||||
if (!priorAmounts.length) return null
|
||||
let anyFilled = false
|
||||
let sumCents = 0
|
||||
for (const raw of priorAmounts) {
|
||||
if (raw === null || raw === undefined || raw === '') continue
|
||||
const n = Number(raw)
|
||||
if (!Number.isFinite(n)) continue
|
||||
anyFilled = true
|
||||
sumCents += Math.round(n * 100)
|
||||
}
|
||||
if (!anyFilled) return null
|
||||
const last = Math.round(contract * 100 - sumCents) / 100
|
||||
return last < 0 ? 0 : last
|
||||
}
|
||||
|
||||
export function shouldAutoFillLastPeriodDue(opts: {
|
||||
lastManualOverride: boolean
|
||||
lastLocked: boolean
|
||||
}): boolean {
|
||||
return !opts.lastManualOverride && !opts.lastLocked
|
||||
}
|
||||
Reference in new issue
Block a user