The personal finance app for everyone (by everyone)
Ruby
<%# locals: (account:, as_of: Date.current) %>
<%# ONE reference date for every figure on this tab, supplied by the caller
where it has one (the account page passes the same date to the Schedule
tab). %>
<% today = as_of %>
<div class="grid grid-cols-3 gap-2">
<%= summary_card title: t(".original_principal") do %>
<%= format_money account.loan.original_balance %>
<% end %>
<%= summary_card title: t(".remaining_principal") do %>
<%= format_money account.balance_money %>
<% end %>
<%# The rate in force today, not the one the loan was written at. On a
variable loan with a recorded change the Schedule tab beside this card
re-amortises at that change; quoting `interest_rate` here showed the
origination rate next to a schedule that no longer used it. For a fixed
loan, or before any change applies, this is the column unchanged. %>
<%= summary_card title: t(".interest_rate") do %>
<% current_rate = account.loan.current_variable_rate(today) %>
<% if current_rate.present? %>
<%= number_to_percentage(current_rate, precision: 3) %>
<% else %>
<%= t(".unknown") %>
<% end %>
<% end %>
<%# A fixed loan's contracted repayment. A variable loan has no single one, so
the card quotes the repayment in force today -- the next scheduled payment,
sized at every rate change recorded before it -- and N/A only when there
is no schedule or it has run out. %>
<%= summary_card title: t(".monthly_payment") do %>
<% if account.loan.variable_rate_type? %>
<% payment_in_force = account.loan.amortization_schedule&.payment_in_force(today) %>
<%= payment_in_force ? format_money(payment_in_force) : t(".not_applicable") %>
<% elsif account.loan.monthly_payment.present? %>
<%= format_money(account.loan.monthly_payment) %>
<% else %>
<%= t(".unknown") %>
<% end %>
<% end %>
<%= summary_card title: t(".term") do %>
<% if account.loan.term_months.present? %>
<% if account.loan.term_months < 12 %>
<%= pluralize(account.loan.term_months, "month") %>
<% else %>
<%= pluralize(account.loan.term_months / 12, "year") %>
<% end %>
<% else %>
<%= t(".unknown") %>
<% end %>
<% end %>
<%# When the contract's last payment falls due. A variable loan keeps this
date through its rate changes -- the schedule resizes the repayment
instead -- so it is the loan's original payoff date either way. %>
<%= summary_card title: t(".original_payoff_date") do %>
<% original_payoff_date = account.loan.amortization_schedule&.payoff_date %>
<%= original_payoff_date ? l(original_payoff_date, format: :long) : t(".unknown") %>
<% end %>
<%= summary_card title: t(".type") do %>
<%= account.loan.rate_type&.titleize || t(".unknown") %>
<% end %>
<%# What the loan costs in total: the Schedule tab's "Total Cost" (principal
and interest) plus any premium charged beside it. With a premium the
title says so, because both tabs render on one page and the same title
over two different amounts reads as a mistake. Unknown without a schedule
to read interest from, the same condition the payoff date answers with. %>
<%= summary_card title: account.loan.insurance.present? ? t(".total_cost_with_insurance") : t(".total_cost") do %>
<% total_cost = account.loan.total_cost %>
<%= total_cost ? format_money(total_cost) : t(".unknown") %>
<% end %>
<%# Only for a loan carrying a premium. A card reading zero would say the
borrower has insurance costing nothing rather than none recorded. A rate
saved on a loan with no schedule to project it against still shows, as
the rate, rather than vanishing as though the save had been lost. %>
<% if account.loan.insurance.present? %>
<%= summary_card title: t(".total_insurance") do %>
<%= format_money(account.loan.total_insurance) %>
<% end %>
<% elsif account.loan.insurance_rate&.positive? %>
<%= summary_card title: t(".total_insurance") do %>
<%= t(".insurance_rate_only", rate: number_to_percentage(account.loan.insurance_rate, strip_insignificant_zeros: true)) %>
<% end %>
<% end %>
<%# Leverage needs a down payment to be a ratio at all, and a loan with none
recorded is not unleveraged -- it is one nobody has told us about. %>
<% if account.loan.initial_leverage_ratio.present? %>
<%= summary_card title: t(".leverage") do %>
<span class="<%= loan_leverage_band_class(account.loan.leverage_band) %>">
<%= t(".leverage_multiple", ratio: number_with_precision(account.loan.initial_leverage_ratio, precision: 1)) %>
<span class="text-sm"><%= t(".leverage_band_#{account.loan.leverage_band}") %></span>
</span>
<% end %>
<% end %>
</div>
<%= render "loans/tabs/repayment_progress", loan: account.loan, as_of: today %>
<div class="flex justify-center py-8">
<%= render DS::Link.new(
text: t(".edit_loan_details"),
variant: "ghost",
href: edit_loan_path(account),
frame: :modal
) %>
</div>