feat(domain): loan amortization schedule
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1c7b1a53e4
commit
9fb4aab887
2 files changed
+228
No files matched your search
@@ -0,0 +1,133 @@
|
||||
import { addMonths, dateInMonth, monthOf } from "@/lib/dates";
|
||||
import type { Cents } from "./types";
|
||||
|
||||
export interface LoanInput {
|
||||
principal: Cents;
|
||||
annualRatePct: number;
|
||||
durationMonths: number;
|
||||
startDate: string;
|
||||
insuranceMonthly: Cents;
|
||||
paymentOverride: Cents | null;
|
||||
referenceDate: string | null;
|
||||
referenceRemainingCapital: Cents | null;
|
||||
}
|
||||
|
||||
export interface ScheduleRow {
|
||||
n: number;
|
||||
date: string;
|
||||
payment: Cents;
|
||||
interest: Cents;
|
||||
principal: Cents;
|
||||
insurance: Cents;
|
||||
remaining: Cents;
|
||||
/** Échéance antérieure au capital de référence saisi (affichée « avant suivi »). */
|
||||
beforeTracking: boolean;
|
||||
}
|
||||
|
||||
export interface LoanSummary {
|
||||
payment: Cents;
|
||||
insurance: Cents;
|
||||
endDate: string;
|
||||
remaining: Cents;
|
||||
interestPaid: Cents;
|
||||
interestLeft: Cents;
|
||||
}
|
||||
|
||||
const MAX_ROWS = 1200;
|
||||
|
||||
/** Mensualité constante hors assurance (amortissement à la française). */
|
||||
export function monthlyPayment(principal: Cents, annualRatePct: number, months: number): Cents {
|
||||
if (months <= 0) return principal;
|
||||
const r = annualRatePct / 100 / 12;
|
||||
if (r === 0) return Math.round(principal / months);
|
||||
return Math.round((principal * r) / (1 - Math.pow(1 + r, -months)));
|
||||
}
|
||||
|
||||
function rowDate(startDate: string, n: number): string {
|
||||
return dateInMonth(addMonths(monthOf(startDate), n - 1), Number(startDate.slice(8, 10)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Lignes d'amortissement à partir de `remaining`. Si `lastN` est fourni, la ligne `lastN`
|
||||
* solde le capital ; sinon on continue jusqu'à remboursement complet.
|
||||
*/
|
||||
function amortize(
|
||||
l: LoanInput,
|
||||
firstN: number,
|
||||
remaining: Cents,
|
||||
payment: Cents,
|
||||
lastN: number | null,
|
||||
): ScheduleRow[] {
|
||||
const rows: ScheduleRow[] = [];
|
||||
const rate = l.annualRatePct / 100 / 12;
|
||||
for (let n = firstN; remaining > 0 && rows.length < MAX_ROWS; n++) {
|
||||
const interest = Math.round(remaining * rate);
|
||||
let principal = payment - interest;
|
||||
if (principal <= 0) throw new Error("Mensualité insuffisante pour couvrir les intérêts");
|
||||
if (principal >= remaining || n === lastN) principal = remaining;
|
||||
remaining -= principal;
|
||||
rows.push({
|
||||
n,
|
||||
date: rowDate(l.startDate, n),
|
||||
payment: principal + interest,
|
||||
interest,
|
||||
principal,
|
||||
insurance: l.insuranceMonthly,
|
||||
remaining,
|
||||
beforeTracking: false,
|
||||
});
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
export function buildSchedule(l: LoanInput): ScheduleRow[] {
|
||||
const standardPayment = l.paymentOverride ?? monthlyPayment(l.principal, l.annualRatePct, l.durationMonths);
|
||||
if (l.referenceDate === null || l.referenceRemainingCapital === null) {
|
||||
return amortize(l, 1, l.principal, standardPayment, l.paymentOverride === null ? l.durationMonths : null);
|
||||
}
|
||||
|
||||
// Crédit en cours : l'historique théorique sert seulement à l'affichage,
|
||||
// le suivi repart du capital constaté à la date de référence.
|
||||
const original = amortize(
|
||||
l,
|
||||
1,
|
||||
l.principal,
|
||||
monthlyPayment(l.principal, l.annualRatePct, l.durationMonths),
|
||||
l.durationMonths,
|
||||
);
|
||||
const before = original
|
||||
.filter((r) => r.date <= l.referenceDate!)
|
||||
.map((r) => ({ ...r, beforeTracking: true }));
|
||||
const payment =
|
||||
l.paymentOverride ??
|
||||
monthlyPayment(l.referenceRemainingCapital, l.annualRatePct, l.durationMonths - before.length);
|
||||
const after = amortize(
|
||||
l,
|
||||
before.length + 1,
|
||||
l.referenceRemainingCapital,
|
||||
payment,
|
||||
l.paymentOverride === null ? l.durationMonths : null,
|
||||
);
|
||||
return [...before, ...after];
|
||||
}
|
||||
|
||||
/** Capital restant dû après la dernière échéance passée à `date`. */
|
||||
export function remainingAt(rows: ScheduleRow[], date: string): Cents {
|
||||
let remaining: Cents | null = null;
|
||||
for (const r of rows) if (r.date <= date) remaining = r.remaining;
|
||||
if (remaining !== null) return remaining;
|
||||
const first = rows[0];
|
||||
return first ? first.remaining + first.principal : 0;
|
||||
}
|
||||
|
||||
export function loanSummary(rows: ScheduleRow[], today: string): LoanSummary {
|
||||
const tracked = rows.filter((r) => !r.beforeTracking);
|
||||
return {
|
||||
payment: tracked[0]?.payment ?? 0,
|
||||
insurance: rows[0]?.insurance ?? 0,
|
||||
endDate: rows[rows.length - 1]?.date ?? "",
|
||||
remaining: remainingAt(rows, today),
|
||||
interestPaid: rows.filter((r) => r.date <= today).reduce((t, r) => t + r.interest, 0),
|
||||
interestLeft: rows.filter((r) => r.date > today).reduce((t, r) => t + r.interest, 0),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user