mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-19 21:29:38 +02:00
Calculates balance based on previous transaction on the same date (#1483)
This commit is contained in:
parent
6996a225ba
commit
242eb5cea1
2 changed files with 20 additions and 1 deletions
|
@ -15,6 +15,7 @@ class Account::Entry < ApplicationRecord
|
||||||
validates :date, comparison: { greater_than: -> { min_supported_date } }
|
validates :date, comparison: { greater_than: -> { min_supported_date } }
|
||||||
|
|
||||||
scope :chronological, -> { order(:date, :created_at) }
|
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 :reverse_chronological, -> { order(date: :desc, created_at: :desc) }
|
||||||
scope :without_transfers, -> { where(marked_as_transfer: false) }
|
scope :without_transfers, -> { where(marked_as_transfer: false) }
|
||||||
scope :with_converted_amount, ->(currency) {
|
scope :with_converted_amount, ->(currency) {
|
||||||
|
@ -54,6 +55,13 @@ class Account::Entry < ApplicationRecord
|
||||||
account.balances.find_by(date: date - 1)&.balance || 0
|
account.balances.find_by(date: date - 1)&.balance || 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def prior_entry_balance
|
||||||
|
entries_on_entry_date
|
||||||
|
.not_account_valuations
|
||||||
|
.last
|
||||||
|
&.balance_after_entry || 0
|
||||||
|
end
|
||||||
|
|
||||||
def balance_after_entry
|
def balance_after_entry
|
||||||
if account_valuation?
|
if account_valuation?
|
||||||
Money.new(amount, currency)
|
Money.new(amount, currency)
|
||||||
|
@ -75,7 +83,7 @@ class Account::Entry < ApplicationRecord
|
||||||
def trend
|
def trend
|
||||||
TimeSeries::Trend.new(
|
TimeSeries::Trend.new(
|
||||||
current: balance_after_entry,
|
current: balance_after_entry,
|
||||||
previous: Money.new(prior_balance, currency),
|
previous: Money.new(prior_entry_balance, currency),
|
||||||
favorable_direction: account.favorable_direction
|
favorable_direction: account.favorable_direction
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
|
@ -110,4 +110,15 @@ class Account::EntryTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
assert_equal Money.new(100), transaction.balance_after_entry
|
assert_equal Money.new(100), transaction.balance_after_entry
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue