From a113d573d686a259f3219f62fb0bf96e5f26f5e9 Mon Sep 17 00:00:00 2001 From: bruno costanzo <82187643+bruno-costanzo@users.noreply.github.com> Date: Fri, 8 Nov 2024 15:17:55 +0100 Subject: [PATCH] Skip account valuation on entry balance_after_entry (#1435) --- app/helpers/account/entries_helper.rb | 4 ++-- app/models/account/entry.rb | 4 +++- test/models/account/entry_test.rb | 11 +++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/app/helpers/account/entries_helper.rb b/app/helpers/account/entries_helper.rb index 360dae4a..148a497b 100644 --- a/app/helpers/account/entries_helper.rb +++ b/app/helpers/account/entries_helper.rb @@ -14,9 +14,9 @@ module Account::EntriesHelper def entries_by_date(entries, selectable: true, totals: false) entries.group_by(&:date).map do |date, grouped_entries| - # Valuations always go first, then sort by created_at + # Valuations always go first, then sort by created_at desc sorted_entries = grouped_entries.sort_by do |entry| - [ entry.account_valuation? ? 0 : 1, entry.created_at ] + [ entry.account_valuation? ? 0 : 1, -entry.created_at.to_i ] end content = capture do diff --git a/app/models/account/entry.rb b/app/models/account/entry.rb index 85a05850..b6692788 100644 --- a/app/models/account/entry.rb +++ b/app/models/account/entry.rb @@ -60,6 +60,8 @@ class Account::Entry < ApplicationRecord else new_balance = prior_balance entries_on_entry_date.each do |e| + next if e.account_valuation? + change = e.amount change = account.liability? ? change : -change new_balance += change @@ -79,7 +81,7 @@ class Account::Entry < ApplicationRecord end def entries_on_entry_date - account.entries.where(date: date).order(created_at: :desc) + account.entries.where(date: date).order(created_at: :asc) end class << self diff --git a/test/models/account/entry_test.rb b/test/models/account/entry_test.rb index f187ffa1..e99f8db0 100644 --- a/test/models/account/entry_test.rb +++ b/test/models/account/entry_test.rb @@ -99,4 +99,15 @@ class Account::EntryTest < ActiveSupport::TestCase assert create_transaction(amount: -10).inflow? assert create_transaction(amount: 10).outflow? end + + test "balance_after_entry skips account valuations" 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.balance_after_entry + end end