diff --git a/src/views/account/AccountView.vue b/src/views/account/AccountView.vue index b1f4e5e..7f4b69f 100644 --- a/src/views/account/AccountView.vue +++ b/src/views/account/AccountView.vue @@ -132,23 +132,46 @@ const repayTargetLoanWallet = computed(() => { const w = wallets.value.find((x) => x.id === form.toWalletId) return w?.type === 'LOAN' && w.loanRate ? w : null }) -// 납입총액 입력 → 원금/이자 자동분리 +// 납입금액 입력용 (원리금균등만 사용) const loanPaymentAmount = ref(null) + +function monthlyInterestOf(loan) { + return Math.round(Math.abs(loan.balance || 0) * (Number(loan.loanRate) / 100 / 12)) +} + +// 상환방식별 자동계산: 원금균등·만기일시는 납입금액 입력 불필요 +const loanAutoResult = computed(() => { + const loan = repayTargetLoanWallet.value + if (!loan) return null + const interest = monthlyInterestOf(loan) + if (loan.loanMethod === 'BULLET') { + return { interest, principal: 0, total: interest, needsInput: false } + } + if (loan.loanMethod === 'EQUAL_PRINCIPAL' && loan.loanAmount && loan.loanMonths) { + const principal = Math.round(Number(loan.loanAmount) / loan.loanMonths) + return { interest, principal, total: principal + interest, needsInput: false } + } + return { interest: null, principal: null, total: null, needsInput: true } +}) + +watch(loanAutoResult, (r) => { + if (r && !r.needsInput) { + form.interest = r.interest + form.principal = r.principal + loanPaymentAmount.value = r.total + } +}) + function calcLoanRepayment(payment) { const loan = repayTargetLoanWallet.value if (!loan || !payment || payment <= 0) return - const monthlyRate = Number(loan.loanRate) / 100 / 12 - const remainingDebt = Math.abs(loan.balance || 0) - const monthlyInterest = Math.round(remainingDebt * monthlyRate) - if (loan.loanMethod === 'BULLET') { - form.interest = payment - form.principal = 0 - } else { - form.interest = Math.min(monthlyInterest, payment) - form.principal = Math.max(0, payment - form.interest) - } + const interest = monthlyInterestOf(loan) + form.interest = Math.min(interest, payment) + form.principal = Math.max(0, payment - form.interest) } -watch(loanPaymentAmount, (val) => { if (val) calcLoanRepayment(Number(val)) }) +watch(loanPaymentAmount, (val) => { + if (loanAutoResult.value?.needsInput && val) calcLoanRepayment(Number(val)) +}) watch(() => form.toWalletId, () => { loanPaymentAmount.value = null form.principal = null @@ -1162,15 +1185,26 @@ onMounted(async () => { - + -