mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-05 21:45:23 +02:00
Net worth calculation (#508)
* Add classification generated column to account * Add basic net worth calculation * Add net worth tests * Fix lint errors
This commit is contained in:
parent
19f15e9391
commit
facd74f733
12 changed files with 156 additions and 40 deletions
|
@ -2,4 +2,66 @@ 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
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue