1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-24 07:39:39 +02:00
Maybe/app/models/family.rb

68 lines
2.1 KiB
Ruby
Raw Normal View History

2024-02-02 09:05:04 -06:00
class Family < ApplicationRecord
has_many :users, dependent: :destroy
has_many :accounts, dependent: :destroy
has_many :transactions, through: :accounts
def net_worth
accounts.sum("CASE WHEN classification = 'asset' THEN balance ELSE -balance END")
end
def assets
accounts.where(classification: "asset").sum(:balance)
end
def liabilities
accounts.where(classification: "liability").sum(:balance)
end
def net_worth_series(period = nil)
query = accounts.joins(:balances)
.select("account_balances.date, SUM(CASE WHEN accounts.classification = 'asset' THEN account_balances.balance ELSE -account_balances.balance END) AS balance, 'USD' as currency")
.group("account_balances.date")
.order("account_balances.date ASC")
if period && period.date_range
query = query.where("account_balances.date BETWEEN ? AND ?", period.date_range.begin, period.date_range.end)
end
MoneySeries.new(
query,
{ trend_type: "asset" }
)
end
def asset_series(period = nil)
query = accounts.joins(:balances)
.select("account_balances.date, SUM(account_balances.balance) AS balance, 'asset' AS classification, 'USD' AS currency")
.group("account_balances.date")
.order("account_balances.date ASC")
.where(classification: "asset")
if period && period.date_range
query = query.where("account_balances.date BETWEEN ? AND ?", period.date_range.begin, period.date_range.end)
end
MoneySeries.new(
query,
{ trend_type: "asset" }
)
end
def liability_series(period = nil)
query = accounts.joins(:balances)
.select("account_balances.date, SUM(account_balances.balance) AS balance, 'liability' AS classification, 'USD' AS currency")
.group("account_balances.date")
.order("account_balances.date ASC")
.where(classification: "liability")
if period && period.date_range
query = query.where("account_balances.date BETWEEN ? AND ?", period.date_range.begin, period.date_range.end)
end
MoneySeries.new(
query,
{ trend_type: "liability" }
)
end
2024-02-02 09:05:04 -06:00
end