1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-24 15:49:39 +02:00

Calculates balance based on previous transaction on the same date (#1483)

This commit is contained in:
Nico 2024-11-22 11:38:41 -03:00 committed by GitHub
parent 6996a225ba
commit 242eb5cea1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 1 deletions

View file

@ -15,6 +15,7 @@ class Account::Entry < ApplicationRecord
validates :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 < ApplicationRecord
account.balances.find_by(date: date - 1)&.balance || 0
end
def prior_entry_balance
entries_on_entry_date
.not_account_valuations
.last
&.balance_after_entry || 0
end
def balance_after_entry
if account_valuation?
Money.new(amount, currency)
@ -75,7 +83,7 @@ class Account::Entry < ApplicationRecord
def trend
TimeSeries::Trend.new(
current: balance_after_entry,
previous: Money.new(prior_balance, currency),
previous: Money.new(prior_entry_balance, currency),
favorable_direction: account.favorable_direction
)
end