mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 23:59:40 +02:00
* Clean up time series models * Add value group rollup class for summarizing hierarchical data * Integrate new classes * Update UI to use new patterns * Update D3 charts to expect new data format * Clean up account model * More cleanup * Money improvements * Use new money fields * Remove invalid fixture data to avoid orphaned accountables * Update time series to work better with collections * Fix tests and UI bugs
50 lines
2 KiB
Ruby
50 lines
2 KiB
Ruby
class Family < ApplicationRecord
|
|
include Monetizable
|
|
|
|
has_many :users, dependent: :destroy
|
|
has_many :accounts, dependent: :destroy
|
|
has_many :transactions, through: :accounts
|
|
has_many :transaction_categories, dependent: :destroy, class_name: "Transaction::Category"
|
|
|
|
monetize :net_worth, :assets, :liabilities
|
|
|
|
def snapshot(period = Period.all)
|
|
query = accounts.active.joins(:balances)
|
|
.where("account_balances.currency = ?", self.currency)
|
|
.select(
|
|
"account_balances.currency",
|
|
"account_balances.date",
|
|
"SUM(CASE WHEN accounts.classification = 'liability' THEN account_balances.balance ELSE 0 END) AS liabilities",
|
|
"SUM(CASE WHEN accounts.classification = 'asset' THEN account_balances.balance ELSE 0 END) AS assets",
|
|
"SUM(CASE WHEN accounts.classification = 'asset' THEN account_balances.balance WHEN accounts.classification = 'liability' THEN -account_balances.balance ELSE 0 END) AS net_worth",
|
|
)
|
|
.group("account_balances.date, account_balances.currency")
|
|
.order("account_balances.date")
|
|
|
|
query = query.where("account_balances.date BETWEEN ? AND ?", period.date_range.begin, period.date_range.end) if period.date_range
|
|
|
|
result = query.to_a
|
|
|
|
{
|
|
asset_series: TimeSeries.new(result.map { |r| { date: r.date, value: Money.new(r.assets, r.currency) } }),
|
|
liability_series: TimeSeries.new(result.map { |r| { date: r.date, value: Money.new(r.liabilities, r.currency) } }),
|
|
net_worth_series: TimeSeries.new(result.map { |r| { date: r.date, value: Money.new(r.net_worth, r.currency) } })
|
|
}
|
|
end
|
|
|
|
def effective_start_date
|
|
accounts.active.joins(:balances).minimum("account_balances.date") || Date.current
|
|
end
|
|
|
|
def net_worth
|
|
Money.new(accounts.active.sum("CASE WHEN classification = 'asset' THEN balance ELSE -balance END"), currency)
|
|
end
|
|
|
|
def assets
|
|
accounts.active.assets.sum(:balance)
|
|
end
|
|
|
|
def liabilities
|
|
Money.new(accounts.active.liabilities.sum(:balance), currency)
|
|
end
|
|
end
|