Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Date a new loan's opening anchor on its start date (#4174)

jaysbeekay529e8b1256a9
diff --git a/app/models/account.rb b/app/models/account.rb
index c9e573a31d..d80e08c20e 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -233,6 +233,17 @@ class Account < ApplicationRecord
transaction do
account.save!
+ # A loan's original principal was its balance on the day it was drawn
+ # down, so that is where its opening anchor belongs. The form always
+ # sends an opening balance date (defaulting to two years ago), so the
+ # server cannot tell a deliberate one from the default: the loan's
+ # start date wins. Loan validates start_date as on or before today, so
+ # the anchor never lands after the reconciliation below. Entry rejects
+ # a date on or before Entry.min_supported_date, so a loan started that
+ # long ago keeps the form's date; its schedule still uses start_date.
+ loan_start_date = account.accountable.start_date if initial_balance && account.loan?
+ opening_balance_date = loan_start_date if loan_start_date && loan_start_date > Entry.min_supported_date
+
manager = Account::OpeningBalanceManager.new(account)
result = manager.set_opening_balance(
balance: initial_balance || account.balance,
diff --git a/config/locales/views/loans/en.yml b/config/locales/views/loans/en.yml
index 9ec2a754fb..5554bee34b 100644
--- a/config/locales/views/loans/en.yml
+++ b/config/locales/views/loans/en.yml
@@ -24,7 +24,7 @@ en:
rate_change_add: Add rate change
rate_change_remove: Remove
start_date: Origination date
- start_date_hint: When the loan was drawn down. Leave blank to use the account's opening date.
+ start_date_hint: When the loan was drawn down. A new loan's balance history starts on this date, not the opening balance date. Leave blank to use the opening balance date.
down_payment: Down payment
insurance_rate: Insurance rate
insurance_rate_placeholder: '0.36'
diff --git a/test/controllers/loans_controller_test.rb b/test/controllers/loans_controller_test.rb
index 4234e7cea6..44934f0c01 100644
--- a/test/controllers/loans_controller_test.rb
+++ b/test/controllers/loans_controller_test.rb
@@ -179,6 +179,79 @@ class LoansControllerTest < ActionDispatch::IntegrationTest
assert_enqueued_with(job: SyncJob)
end
+ # #3383: the opening anchor holds the original principal, so
+ # it belongs on the day the loan was drawn down, not on the form's separate
+ # opening balance date (which defaults to today minus two years).
+ test "a new loan's opening anchor is dated on its start date" do
+ account = create_loan(start_date: Date.new(2020, 1, 15))
+
+ anchor = account.valuations.opening_anchor.first.entry
+ assert_equal Date.new(2020, 1, 15), anchor.date
+ assert_equal 400_000, anchor.amount
+ end
+
+ test "a new loan's recorded balance opens on its principal at its start date" do
+ start_date = Date.new(2020, 1, 15)
+ account = create_loan(start_date: start_date)
+ Balance::Materializer.new(account, strategy: :forward).materialize_balances
+
+ series = account.balance_series(
+ period: Period.custom(start_date: start_date, end_date: Date.current),
+ interval: "1 month"
+ )
+ assert_equal 400_000, series.values.first.value.amount
+ assert_equal start_date, account.balances.minimum(:date)
+ end
+
+ test "a new loan keeps its entered current balance" do
+ account = create_loan(start_date: Date.new(2020, 1, 15))
+ Balance::Materializer.new(account, strategy: :forward).materialize_balances
+
+ today = account.entries.valuations.find_by(date: Date.current)
+ assert_not_nil today
+ assert_equal 399_600, today.amount
+ assert_equal 399_600, account.balances.find_by(date: Date.current).balance
+ end
+
+ test "a loan without a start date keeps the form's opening date" do
+ opening_balance_date = Date.current - 3.years # not the manager's own two-year default
+ account = create_loan(start_date: nil, opening_balance_date: opening_balance_date)
+
+ assert_equal opening_balance_date, account.valuations.opening_anchor.first.entry.date
+ end
+
+ test "a loan with a start date but no original balance keeps the form's opening date" do
+ # Without a principal the anchor holds the entered current balance, which
+ # was not the balance on the start date.
+ opening_balance_date = Date.current - 3.years
+ account = create_loan(start_date: Date.new(2020, 1, 15), initial_balance: nil,
+ opening_balance_date: opening_balance_date)
+
+ anchor = account.valuations.opening_anchor.first.entry
+ assert_equal opening_balance_date, anchor.date
+ assert_equal 399_600, anchor.amount
+ end
+
+ # An anchor on or before Entry.min_supported_date would fail Entry's own
+ # validation and the form would re-render; the loan is created instead.
+ test "a loan started on the earliest supported entry date is created with the form's opening date" do
+ opening_balance_date = Date.current - 3.years
+ assert_difference "Account.count", 1 do
+ account = create_loan(start_date: Entry.min_supported_date, opening_balance_date: opening_balance_date)
+ assert_equal opening_balance_date, account.valuations.opening_anchor.first.entry.date
+ end
+ end
+
+ test "a loan started today has its opening anchor today and no reconciliation" do
+ account = create_loan(start_date: Date.current)
+
+ valuations = account.entries.valuations
+ assert_equal 1, valuations.count
+ assert_equal Date.current, valuations.first.date
+ assert_equal 400_000, valuations.first.amount
+ assert_equal "opening_anchor", valuations.first.entryable.kind
+ end
+
test "updates with loan details" do
assert_no_difference [ "Account.count", "Loan.count" ] do
patch loan_path(@account), params: {
@@ -880,6 +953,31 @@ class LoansControllerTest < ActionDispatch::IntegrationTest
end
private
+ # Posts the new-loan form as a browser does: the "Opening balance date"
+ # field is always sent, defaulting to today minus two years.
+ def create_loan(start_date:, initial_balance: 400_000, opening_balance_date: Date.current - 2.years)
+ post loans_path, params: {
+ account: {
+ name: "Mortgage",
+ balance: 399_600,
+ currency: "USD",
+ accountable_type: "Loan",
+ opening_balance_date: opening_balance_date.iso8601,
+ accountable_attributes: {
+ subtype: "mortgage",
+ interest_rate: 5,
+ term_months: 360,
+ rate_type: "fixed",
+ initial_balance: initial_balance,
+ start_date: start_date&.iso8601
+ }
+ }
+ }
+ account = Account.order(:created_at).last
+ assert_redirected_to account
+ account
+ end
+
# The payment cells of the Schedule tab's table, the only table on the page.
def schedule_table_cells
css_select("table tbody td").map { |cell| cell.text.strip }
diff --git a/test/models/account_test.rb b/test/models/account_test.rb
index 5678823aa4..4b8ffeaf3b 100644
--- a/test/models/account_test.rb
+++ b/test/models/account_test.rb
@@ -375,6 +375,114 @@ class AccountTest < ActiveSupport::TestCase
assert_equal 8_000, account.entries.valuations.find_by(date: Date.current)&.amount
end
+ # #3383: only a loan's start date moves the opening anchor.
+ test "create_and_sync dates a loan's opening anchor on its start date" do
+ Account.any_instance.stubs(:sync_later)
+ opening_balance_date = Date.current - 2.years
+
+ account = Account.create_and_sync(
+ {
+ family: @family,
+ owner: @admin,
+ name: "Mortgage",
+ balance: 399_600,
+ currency: "USD",
+ accountable_type: "Loan",
+ accountable_attributes: { initial_balance: 400_000, start_date: Date.new(2020, 1, 15),
+ rate_type: "fixed", interest_rate: 5, term_months: 360 }
+ },
+ skip_initial_sync: true,
+ opening_balance_date: opening_balance_date
+ )
+
+ assert_equal Date.new(2020, 1, 15), account.valuations.opening_anchor.first.entry.date
+ assert_equal 399_600, account.entries.valuations.find_by(date: Date.current)&.amount
+ end
+
+ # The anchor is dated on the start date only because Loan rejects a future
+ # one: an anchor after today would skip today's reconciliation and leave the
+ # account with no balance for today.
+ # Entry rejects a date on or before Entry.min_supported_date, so a loan drawn
+ # down that long ago keeps the form's opening date, as before the start date
+ # moved the anchor. Both sides of the boundary.
+ test "a loan whose start date is on the earliest supported entry date keeps the form's opening date" do
+ Account.any_instance.stubs(:sync_later)
+ opening_balance_date = Date.current - 2.years
+ start_date = Entry.min_supported_date
+
+ account = create_loan_with_start_date(start_date, opening_balance_date)
+
+ assert_equal start_date, account.accountable.start_date
+ assert_equal opening_balance_date, account.valuations.opening_anchor.first.entry.date
+ end
+
+ test "a loan started the day after the earliest supported date is anchored on its start date" do
+ Account.any_instance.stubs(:sync_later)
+ start_date = Entry.min_supported_date + 1.day
+
+ account = create_loan_with_start_date(start_date, Date.current - 2.years)
+
+ assert_equal start_date, account.valuations.opening_anchor.first.entry.date
+ end
+
+ test "create_and_sync rejects a loan with a future start date and writes no anchor" do
+ Account.any_instance.stubs(:sync_later)
+
+ assert_no_difference [ "Account.count", "Entry.count" ] do
+ assert_raises ActiveRecord::RecordInvalid do
+ Account.create_and_sync(
+ {
+ family: @family,
+ owner: @admin,
+ name: "Mortgage",
+ balance: 399_600,
+ currency: "USD",
+ accountable_type: "Loan",
+ accountable_attributes: { initial_balance: 400_000, start_date: Date.current + 1.month,
+ rate_type: "fixed", interest_rate: 5, term_months: 360 }
+ },
+ skip_initial_sync: true,
+ opening_balance_date: Date.current - 3.years
+ )
+ end
+ end
+ end
+
+ test "create_and_sync keeps a depository account's opening balance date" do
+ Account.any_instance.stubs(:sync_later)
+ opening_balance_date = Date.new(2020, 1, 15)
+
+ account = Account.create_and_sync(
+ {
+ family: @family,
+ owner: @admin,
+ name: "Checking",
+ balance: 1_000,
+ currency: "USD",
+ accountable_type: "Depository",
+ accountable_attributes: {}
+ },
+ skip_initial_sync: true,
+ opening_balance_date: opening_balance_date
+ )
+
+ assert_equal opening_balance_date, account.valuations.opening_anchor.first.entry.date
+ end
+
+ test "a provider-created loan keeps the default opening date" do
+ item = SimplefinItem.create!(family: @family, name: "SF Conn", access_url: "https://example.com/access")
+ simplefin_account = SimplefinAccount.create!(
+ simplefin_item: item, name: "My Loan", account_id: "acct_424",
+ currency: "USD", account_type: "loan", current_balance: 5_000
+ )
+
+ account = Account.create_from_simplefin_account(simplefin_account, "Loan")
+
+ assert_nil account.accountable.start_date
+ assert_equal 2.years.ago.to_date, account.valuations.opening_anchor.first.entry.date
+ assert_equal 1, account.entries.valuations.count
+ end
+
test "create_and_sync uses provided opening balance date" do
Account.any_instance.stubs(:sync_later)
opening_date = Time.zone.today
@@ -1091,4 +1199,22 @@ class AccountTest < ActiveSupport::TestCase
accounts(:credit_card).account_shares.find_by!(user: member).update!(permission: "read_write")
assert_includes Account.annotatable_by(member).pluck(:id), accounts(:credit_card).id, "read_write share"
end
+
+ private
+ def create_loan_with_start_date(start_date, opening_balance_date)
+ Account.create_and_sync(
+ {
+ family: @family,
+ owner: @admin,
+ name: "Mortgage",
+ balance: 399_600,
+ currency: "USD",
+ accountable_type: "Loan",
+ accountable_attributes: { initial_balance: 400_000, start_date: start_date,
+ rate_type: "fixed", interest_rate: 5, term_months: 360 }
+ },
+ skip_initial_sync: true,
+ opening_balance_date: opening_balance_date
+ )
+ end
end