2024-06-20 07:26:25 -04:00
|
|
|
class Loan < ApplicationRecord
|
|
|
|
include Accountable
|
2024-10-08 17:16:37 -04:00
|
|
|
|
2025-05-23 18:58:22 -04:00
|
|
|
SUBTYPES = {
|
|
|
|
"mortgage" => { short: "Mortgage", long: "Mortgage" },
|
|
|
|
"student" => { short: "Student", long: "Student Loan" },
|
|
|
|
"auto" => { short: "Auto", long: "Auto Loan" },
|
|
|
|
"other" => { short: "Other", long: "Other Loan" }
|
|
|
|
}.freeze
|
|
|
|
|
2024-10-08 17:16:37 -04:00
|
|
|
def monthly_payment
|
|
|
|
return nil if term_months.nil? || interest_rate.nil? || rate_type.nil? || rate_type != "fixed"
|
2025-04-14 09:09:25 -04:00
|
|
|
return Money.new(0, account.currency) if account.loan.original_balance.amount.zero? || term_months.zero?
|
2024-10-08 17:16:37 -04:00
|
|
|
|
|
|
|
annual_rate = interest_rate / 100.0
|
|
|
|
monthly_rate = annual_rate / 12.0
|
|
|
|
|
2024-10-09 18:11:36 -04:00
|
|
|
if monthly_rate.zero?
|
2025-04-14 09:09:25 -04:00
|
|
|
payment = account.loan.original_balance.amount / term_months
|
2024-10-09 18:11:36 -04:00
|
|
|
else
|
2025-04-14 09:09:25 -04:00
|
|
|
payment = (account.loan.original_balance.amount * monthly_rate * (1 + monthly_rate)**term_months) / ((1 + monthly_rate)**term_months - 1)
|
2024-10-09 18:11:36 -04:00
|
|
|
end
|
2024-10-08 17:16:37 -04:00
|
|
|
|
|
|
|
Money.new(payment.round, account.currency)
|
|
|
|
end
|
2024-10-18 14:37:42 -04:00
|
|
|
|
2025-04-14 09:09:25 -04:00
|
|
|
def original_balance
|
|
|
|
Money.new(account.first_valuation_amount, account.currency)
|
|
|
|
end
|
|
|
|
|
2025-02-21 11:57:59 -05:00
|
|
|
class << self
|
|
|
|
def color
|
|
|
|
"#D444F1"
|
|
|
|
end
|
2024-11-04 20:27:31 -05:00
|
|
|
|
2025-02-21 11:57:59 -05:00
|
|
|
def icon
|
|
|
|
"hand-coins"
|
|
|
|
end
|
|
|
|
|
|
|
|
def classification
|
|
|
|
"liability"
|
|
|
|
end
|
2024-11-04 20:27:31 -05:00
|
|
|
end
|
2024-06-20 07:26:25 -04:00
|
|
|
end
|