1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-04 21:15:19 +02:00

loan: Set the first valuation as the original principal. (#2088)

Fix: #1645.
This commit is contained in:
Joseph Ho 2025-04-14 09:09:25 -04:00 committed by GitHub
parent 5cb2183bdf
commit f181ba941f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 59 additions and 13 deletions

View file

@ -3,20 +3,24 @@ class Loan < ApplicationRecord
def monthly_payment
return nil if term_months.nil? || interest_rate.nil? || rate_type.nil? || rate_type != "fixed"
return Money.new(0, account.currency) if account.original_balance.amount.zero? || term_months.zero?
return Money.new(0, account.currency) if account.loan.original_balance.amount.zero? || term_months.zero?
annual_rate = interest_rate / 100.0
monthly_rate = annual_rate / 12.0
if monthly_rate.zero?
payment = account.original_balance.amount / term_months
payment = account.loan.original_balance.amount / term_months
else
payment = (account.original_balance.amount * monthly_rate * (1 + monthly_rate)**term_months) / ((1 + monthly_rate)**term_months - 1)
payment = (account.loan.original_balance.amount * monthly_rate * (1 + monthly_rate)**term_months) / ((1 + monthly_rate)**term_months - 1)
end
Money.new(payment.round, account.currency)
end
def original_balance
Money.new(account.first_valuation_amount, account.currency)
end
class << self
def color
"#D444F1"