The personal finance app for everyone (by everyone)
Ruby
fix(accounts): keep the entered current balance when it differs from the opening one (#3301)
Nate Mendes61c1e0d7e1bf
diff --git a/app/models/account.rb b/app/models/account.rbindex 772f6a29c2..ffbc23ce26 100644--- a/app/models/account.rb+++ b/app/models/account.rb@@ -175,7 +175,10 @@ class Account < ApplicationRecordattrs = attributes.dupattrs[:cash_balance] = attrs[:balance] unless attrs.key?(:cash_balance)account = new(attrs)- initial_balance = attributes.dig(:accountable_attributes, :initial_balance)&.to_d+ # Presence is read from the raw value: a blank form field arrives as ""+ # and would convert to a very present-looking 0.+ raw_initial_balance = attributes.dig(:accountable_attributes, :initial_balance)+ initial_balance = raw_initial_balance.to_d if raw_initial_balance.present?transaction doaccount.save!@@ -187,6 +190,29 @@ class Account < ApplicationRecord)raise result.error if result.error+ # When the opening balance differs from the entered current balance+ # (a loan created with its original principal), the opening anchor is+ # the account's only entry — the initial sync would recalculate+ # today's balance back to it, silently discarding what the user just+ # typed. Anchor today's balance too so both survive.+ #+ # Only when the opening anchor is on an earlier day: the opening date+ # is user-supplied and may be today, and a same-day reconciliation+ # would be matched to the opening anchor by date and overwrite it.+ # On its own date the opening balance wins.+ if initial_balance && initial_balance != account.balance && manager.opening_date < Date.current+ # An explicit reconciliation, not CurrentBalanceManager: for cash+ # accounts its transaction-adjustment strategy computes a zero delta+ # here (account.balance already holds the entered value) and would+ # only rewrite the opening anchor, leaving today's balance unanchored+ # for the first sync.+ reconciliation = Account::ReconciliationManager.new(account).reconcile_balance(+ balance: account.balance,+ date: Date.current+ )+ raise reconciliation.error_message unless reconciliation.success?+ end+account.auto_share_with_family! if account.family.share_all_by_default?enddiff --git a/test/models/account_test.rb b/test/models/account_test.rbindex 4b03520c65..9360c7beee 100644--- a/test/models/account_test.rb+++ b/test/models/account_test.rb@@ -95,6 +95,95 @@ class AccountTest < ActiveSupport::TestCaseassert_equal 1000, opening_anchor.entry.amountend+ test "create_and_sync keeps the entered current balance when it differs from the opening balance" do+ Account.any_instance.stubs(:sync_later)++ account = Account.create_and_sync(+ {+ family: @family,+ owner: @admin,+ name: "Student Loan",+ balance: 8_000,+ currency: "USD",+ accountable_type: "Loan",+ accountable_attributes: { initial_balance: 20_000, rate_type: "fixed", interest_rate: 4.5, term_months: 120 }+ },+ skip_initial_sync: true+ )++ assert_equal 20_000, account.valuations.opening_anchor.first.entry.amount+ # Without a today anchor, the initial sync would walk forward from the+ # opening valuation and overwrite the entered 8,000 with 20,000.+ today_valuation = account.entries.valuations.find_by(date: Date.current)+ assert_not_nil today_valuation+ assert_equal 8_000, today_valuation.amount+ end++ test "create_and_sync leaves the opening anchor alone when the opening balance date is today" do+ Account.any_instance.stubs(:sync_later)++ account = Account.create_and_sync(+ {+ family: @family,+ owner: @admin,+ name: "Student Loan",+ balance: 8_000,+ currency: "USD",+ accountable_type: "Loan",+ accountable_attributes: { initial_balance: 20_000, rate_type: "fixed", interest_rate: 4.5, term_months: 120 }+ },+ skip_initial_sync: true,+ opening_balance_date: Date.current+ )++ # Both balances land on the same day, so today's balance cannot be+ # anchored without reusing (and overwriting) the opening anchor.+ valuations = account.entries.valuations.where(date: Date.current)+ assert_equal 1, valuations.count+ assert_equal 20_000, valuations.first.amount+ assert_equal "opening_anchor", valuations.first.entryable.kind+ end++ test "create_and_sync treats a blank initial balance as absent" do+ Account.any_instance.stubs(:sync_later)++ account = Account.create_and_sync(+ {+ family: @family,+ owner: @admin,+ name: "Student Loan",+ balance: 8_000,+ currency: "USD",+ accountable_type: "Loan",+ accountable_attributes: { initial_balance: "", rate_type: "fixed", interest_rate: 4.5, term_months: 120 }+ },+ skip_initial_sync: true+ )++ assert_equal 8_000, account.valuations.opening_anchor.first.entry.amount+ assert_nil account.entries.valuations.find_by(date: Date.current)+ end++ test "create_and_sync treats a zero initial balance as a real opening balance" do+ Account.any_instance.stubs(:sync_later)++ account = Account.create_and_sync(+ {+ family: @family,+ owner: @admin,+ name: "Student Loan",+ balance: 8_000,+ currency: "USD",+ accountable_type: "Loan",+ accountable_attributes: { initial_balance: 0, rate_type: "fixed", interest_rate: 4.5, term_months: 120 }+ },+ skip_initial_sync: true+ )++ assert_equal 0, account.valuations.opening_anchor.first.entry.amount+ assert_equal 8_000, account.entries.valuations.find_by(date: Date.current)&.amount+ end+test "create_and_sync uses provided opening balance date" doAccount.any_instance.stubs(:sync_later)opening_date = Time.zone.today