The personal finance app for everyone
Calculates balance based on previous transaction on the same date (#1483)
Nico242eb5cea1f2
diff --git a/app/models/account/entry.rb b/app/models/account/entry.rbindex 09d19618..2e77b19c 100644--- a/app/models/account/entry.rb+++ b/app/models/account/entry.rb@@ -15,6 +15,7 @@ class Account::Entry < ApplicationRecordvalidates :date, comparison: { greater_than: -> { min_supported_date } }scope :chronological, -> { order(:date, :created_at) }+ scope :not_account_valuations, -> { where.not(entryable_type: "Account::Valuation") }scope :reverse_chronological, -> { order(date: :desc, created_at: :desc) }scope :without_transfers, -> { where(marked_as_transfer: false) }scope :with_converted_amount, ->(currency) {@@ -54,6 +55,13 @@ class Account::Entry < ApplicationRecordaccount.balances.find_by(date: date - 1)&.balance || 0end+ def prior_entry_balance+ entries_on_entry_date+ .not_account_valuations+ .last+ &.balance_after_entry || 0+ end+def balance_after_entryif account_valuation?Money.new(amount, currency)@@ -75,7 +83,7 @@ class Account::Entry < ApplicationRecorddef trendTimeSeries::Trend.new(current: balance_after_entry,- previous: Money.new(prior_balance, currency),+ previous: Money.new(prior_entry_balance, currency),favorable_direction: account.favorable_direction)enddiff --git a/test/models/account/entry_test.rb b/test/models/account/entry_test.rbindex e99f8db0..b5711d29 100644--- a/test/models/account/entry_test.rb+++ b/test/models/account/entry_test.rb@@ -110,4 +110,15 @@ class Account::EntryTest < ActiveSupport::TestCaseassert_equal Money.new(100), transaction.balance_after_entryend++ test "prior_entry_balance returns last transaction entry balance" do+ family = families(:empty)+ account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new++ new_valuation = create_valuation(account: account, amount: 1)+ transaction = create_transaction(date: new_valuation.date, account: account, amount: -100)+++ assert_equal Money.new(100), transaction.prior_entry_balance+ endend