Files
sure/app/models/loan.rb
Sure Admin (bot) ae37c2495f Fix loan account subtype not persisting on create (#1491)
* Fix loan account subtype not persisting on create

The LoansController was missing :subtype in permitted_accountable_attributes,
causing form submissions with account[subtype] to be silently ignored.

This is the same bug that was fixed for Investment accounts in PR #1039
and Crypto accounts in PR #1022.

Fixes: loan account subtype not saving (v0.7.0-alpha.4)

* Validate loan subtype values

Agent-Logs-Url: https://github.com/we-promise/sure/sessions/54bc6874-2cc0-43aa-ac44-9acd50316be3

Co-authored-by: jjmata <187772+jjmata@users.noreply.github.com>

---------

Co-authored-by: SureBot <sure-bot@we-promise.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: jjmata <187772+jjmata@users.noreply.github.com>
2026-04-18 00:06:24 +02:00

47 lines
1.2 KiB
Ruby

class Loan < ApplicationRecord
include Accountable
SUBTYPES = {
"mortgage" => { short: "Mortgage", long: "Mortgage" },
"student" => { short: "Student Loan", long: "Student Loan" },
"auto" => { short: "Auto Loan", long: "Auto Loan" },
"other" => { short: "Other Loan", long: "Other Loan" }
}.freeze
validates :subtype, inclusion: { in: SUBTYPES.keys }, allow_blank: true
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.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.loan.original_balance.amount / term_months
else
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"
end
def icon
"hand-coins"
end
def classification
"liability"
end
end
end