2024-06-20 07:26:25 -04:00
|
|
|
class Investment < ApplicationRecord
|
2024-02-02 23:09:35 +00:00
|
|
|
include Accountable
|
2024-02-06 17:45:17 -08:00
|
|
|
|
|
|
|
SUBTYPES = [
|
|
|
|
[ "Brokerage", "brokerage" ],
|
|
|
|
[ "Pension", "pension" ],
|
|
|
|
[ "Retirement", "retirement" ],
|
|
|
|
[ "401(k)", "401k" ],
|
|
|
|
[ "529 plan", "529_plan" ],
|
|
|
|
[ "Health Savings Account", "hsa" ],
|
|
|
|
[ "Mutual Fund", "mutual_fund" ],
|
|
|
|
[ "Roth IRA", "roth_ira" ],
|
2024-03-06 05:14:25 -08:00
|
|
|
[ "Roth 401k", "roth_401k" ],
|
|
|
|
[ "Angel", "angel" ]
|
2024-02-06 17:45:17 -08:00
|
|
|
].freeze
|
2024-08-02 17:09:25 -04:00
|
|
|
|
|
|
|
def value
|
|
|
|
account.balance_money + holdings_value
|
|
|
|
end
|
|
|
|
|
|
|
|
def holdings_value
|
|
|
|
account.holdings.current.known_value.sum(&:amount) || Money.new(0, account.currency)
|
|
|
|
end
|
|
|
|
|
|
|
|
def series(period: Period.all, currency: account.currency)
|
|
|
|
balance_series = account.balances.in_period(period).where(currency: currency)
|
|
|
|
holding_series = account.holdings.known_value.in_period(period).where(currency: currency)
|
|
|
|
|
|
|
|
holdings_by_date = holding_series.group_by(&:date).transform_values do |holdings|
|
|
|
|
holdings.sum(&:amount)
|
|
|
|
end
|
|
|
|
|
|
|
|
combined_series = balance_series.map do |balance|
|
|
|
|
holding_amount = holdings_by_date[balance.date] || 0
|
|
|
|
|
|
|
|
{ date: balance.date, value: Money.new(balance.balance + holding_amount, currency) }
|
|
|
|
end
|
|
|
|
|
|
|
|
if combined_series.empty? && period.date_range.end == Date.current
|
|
|
|
TimeSeries.new([ { date: Date.current, value: self.value.exchange_to(currency) } ])
|
|
|
|
else
|
|
|
|
TimeSeries.new(combined_series)
|
|
|
|
end
|
|
|
|
rescue Money::ConversionError
|
|
|
|
TimeSeries.new([])
|
|
|
|
end
|
2024-02-02 23:09:35 +00:00
|
|
|
end
|