1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 21:29:38 +02:00

Skip account valuation on entry balance_after_entry (#1435)

This commit is contained in:
bruno costanzo 2024-11-08 15:17:55 +01:00 committed by GitHub
parent 3b928775a8
commit a113d573d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 3 deletions

View file

@ -14,9 +14,9 @@ module Account::EntriesHelper
def entries_by_date(entries, selectable: true, totals: false) def entries_by_date(entries, selectable: true, totals: false)
entries.group_by(&:date).map do |date, grouped_entries| 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| 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 end
content = capture do content = capture do

View file

@ -60,6 +60,8 @@ class Account::Entry < ApplicationRecord
else else
new_balance = prior_balance new_balance = prior_balance
entries_on_entry_date.each do |e| entries_on_entry_date.each do |e|
next if e.account_valuation?
change = e.amount change = e.amount
change = account.liability? ? change : -change change = account.liability? ? change : -change
new_balance += change new_balance += change
@ -79,7 +81,7 @@ class Account::Entry < ApplicationRecord
end end
def entries_on_entry_date def entries_on_entry_date
account.entries.where(date: date).order(created_at: :desc) account.entries.where(date: date).order(created_at: :asc)
end end
class << self class << self

View file

@ -99,4 +99,15 @@ class Account::EntryTest < ActiveSupport::TestCase
assert create_transaction(amount: -10).inflow? assert create_transaction(amount: -10).inflow?
assert create_transaction(amount: 10).outflow? assert create_transaction(amount: 10).outflow?
end 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 end