2024-02-02 23:09:35 +00:00
|
|
|
module Accountable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2024-06-20 07:26:25 -04:00
|
|
|
ASSET_TYPES = %w[ Depository Investment Crypto Property Vehicle OtherAsset ]
|
|
|
|
LIABILITY_TYPES = %w[ CreditCard Loan OtherLiability ]
|
2024-03-19 09:10:40 -04:00
|
|
|
TYPES = ASSET_TYPES + LIABILITY_TYPES
|
2024-02-09 14:26:54 +00:00
|
|
|
|
|
|
|
def self.from_type(type)
|
2024-06-20 07:26:25 -04:00
|
|
|
return nil unless TYPES.include?(type)
|
|
|
|
type.constantize
|
2024-02-09 14:26:54 +00:00
|
|
|
end
|
|
|
|
|
2024-03-19 09:10:40 -04:00
|
|
|
def self.by_classification
|
|
|
|
{ assets: ASSET_TYPES, liabilities: LIABILITY_TYPES }
|
|
|
|
end
|
|
|
|
|
2024-02-02 23:09:35 +00:00
|
|
|
included do
|
|
|
|
has_one :account, as: :accountable, touch: true
|
|
|
|
end
|
2024-08-02 17:09:25 -04:00
|
|
|
|
|
|
|
def value
|
|
|
|
account.balance_money
|
|
|
|
end
|
|
|
|
|
|
|
|
def series(period: Period.all, currency: account.currency)
|
|
|
|
balance_series = account.balances.in_period(period).where(currency: currency)
|
|
|
|
|
|
|
|
if balance_series.empty? && period.date_range.end == Date.current
|
|
|
|
TimeSeries.new([ { date: Date.current, value: account.balance_money.exchange_to(currency) } ])
|
|
|
|
else
|
|
|
|
TimeSeries.from_collection(balance_series, :balance_money)
|
|
|
|
end
|
|
|
|
rescue Money::ConversionError
|
|
|
|
TimeSeries.new([])
|
|
|
|
end
|
2024-02-02 23:09:35 +00:00
|
|
|
end
|