diff --git a/src/utils/repayment.js b/src/utils/repayment.js index 756a945..c44608c 100644 --- a/src/utils/repayment.js +++ b/src/utils/repayment.js @@ -70,20 +70,18 @@ export function loanSchedule(w, now = currentYm(), cap = 600) { return { rows, partial: !nRem } } - // EQUAL_PAYMENT 원리금균등: 매월 상환액 고정 - // 남은 개월수는 '현재 잔액 + 최초 상환액'으로 역산 → loanStart 없어도 정확(기존 대출 대응) + // EQUAL_PAYMENT 원리금균등 + // (1) 남은 개월수 n 산출: 최초 상환액으로 역산(loanStart 없어도 정확) → 안되면 loanStart 경과 let n = null - let payment = null if (w.loanAmount && w.loanMonths && r > 0) { const origPay = (Number(w.loanAmount) * r) / (1 - Math.pow(1 + r, -Number(w.loanMonths))) - payment = Math.round(origPay) // bal = origPay·(1-(1+r)^-n)/r → n = -ln(1 - bal·r/origPay) / ln(1+r) const ratio = 1 - (bal * r) / origPay if (ratio > 0 && ratio < 1) { n = Math.min(Number(w.loanMonths), Math.max(1, Math.ceil(-Math.log(ratio) / Math.log(1 + r) - 1e-9))) } } - if (n == null) n = nRem // 잔액 역산 불가 시 loanStart 경과 기반 + if (n == null) n = nRem if (n == null) { // 개월·원금 정보 부족 → 이번 달 이자만 안내(원금 분리 불가) const interest = Math.round(bal * r) @@ -92,10 +90,12 @@ export function loanSchedule(w, now = currentYm(), cap = 600) { partial: true, } } - if (payment == null) payment = r > 0 ? Math.round((bal * r) / (1 - Math.pow(1 + r, -n))) : Math.round(bal / n) + // (2) 스케줄 상환액은 '현재 잔액'을 n개월에 상각 → 원금 항상 양수, 잔액이 0으로 수렴 + // (최초원금 기준을 쓰면 잔액>원금 등 데이터 불일치 시 원금이 음수가 되어 잔액이 증가함) + const payment = r > 0 ? Math.round((bal * r) / (1 - Math.pow(1 + r, -n))) : Math.round(bal / n) for (let i = 0; i < n && bal > 0 && rows.length < cap; i++) { const interest = Math.round(bal * r) - let principal = payment - interest + let principal = Math.max(0, payment - interest) if (i === n - 1 || principal > bal) principal = bal // 마지막 회차 잔액 정산 bal = Math.max(0, bal - principal) rows.push({ label: label(y, m), principal, interest, total: principal + interest, balance: bal })