2024-06-21 16:23:28 -04:00
|
|
|
class Account::Valuation < ApplicationRecord
|
2024-07-01 10:49:43 -04:00
|
|
|
include Account::Entryable
|
2024-06-21 16:23:28 -04:00
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
class << self
|
|
|
|
def search(_params)
|
|
|
|
all
|
2024-06-21 16:23:28 -04:00
|
|
|
end
|
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
def requires_search?(_params)
|
|
|
|
false
|
2024-06-21 16:23:28 -04:00
|
|
|
end
|
2024-07-01 10:49:43 -04:00
|
|
|
end
|
2024-11-04 20:27:31 -05:00
|
|
|
|
|
|
|
def name
|
2024-11-15 13:49:37 -05:00
|
|
|
entry.name || (oldest? ? "Initial balance" : "Balance update")
|
2024-11-04 20:27:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def trend
|
|
|
|
@trend ||= create_trend
|
|
|
|
end
|
|
|
|
|
|
|
|
def icon
|
|
|
|
oldest? ? "plus" : entry.trend.icon
|
|
|
|
end
|
|
|
|
|
|
|
|
def color
|
|
|
|
oldest? ? "#D444F1" : entry.trend.color
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def oldest?
|
|
|
|
@oldest ||= account.entries.where("date < ?", entry.date).empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def account
|
|
|
|
@account ||= entry.account
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_trend
|
|
|
|
TimeSeries::Trend.new(
|
|
|
|
current: entry.amount_money,
|
|
|
|
previous: prior_balance&.balance_money,
|
|
|
|
favorable_direction: account.favorable_direction
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def prior_balance
|
|
|
|
@prior_balance ||= account.balances
|
|
|
|
.where("date < ?", entry.date)
|
|
|
|
.order(date: :desc)
|
|
|
|
.first
|
|
|
|
end
|
2024-06-21 16:23:28 -04:00
|
|
|
end
|