The personal finance app for everyone
Net worth calculation (#508)
Zach Gollwitzerfacd74f73378
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rbindex cba97434..0ef198be 100644--- a/app/helpers/application_helper.rb+++ b/app/helpers/application_helper.rb@@ -52,9 +52,9 @@ module ApplicationHelperdef trend_styles(trend)bg_class, text_class, symbol, icon = case trend.directionwhen "up"- trend.type == :liability ? [ "bg-red-500/5", "text-red-500", "+", "arrow-up" ] : [ "bg-green-500/5", "text-green-500", "+", "arrow-up" ]+ trend.type == "liability" ? [ "bg-red-500/5", "text-red-500", "+", "arrow-up" ] : [ "bg-green-500/5", "text-green-500", "+", "arrow-up" ]when "down"- trend.type == :liability ? [ "bg-green-500/5", "text-green-500", "-", "arrow-down" ] : [ "bg-red-500/5", "text-red-500", "-", "arrow-down" ]+ trend.type == "liability" ? [ "bg-green-500/5", "text-green-500", "-", "arrow-down" ] : [ "bg-red-500/5", "text-red-500", "-", "arrow-down" ]when "flat"[ "bg-gray-500/5", "text-gray-500", "", "minus" ]elsediff --git a/app/models/account.rb b/app/models/account.rbindex eb421166..ceabced7 100644--- a/app/models/account.rb+++ b/app/models/account.rb@@ -9,24 +9,8 @@ class Account < ApplicationRecorddelegated_type :accountable, types: Accountable::TYPES, dependent: :destroy- delegate :type_name, to: :accountablebefore_create :check_currency- def classification- classifications = {- "Account::Depository" => :asset,- "Account::Investment" => :asset,- "Account::Property" => :asset,- "Account::Vehicle" => :asset,- "Account::OtherAsset" => :asset,- "Account::Loan" => :liability,- "Account::Credit" => :liability,- "Account::OtherLiability" => :liability- }-- classifications[accountable_type]- end-def balance_series(period)MoneySeries.new(balances.in_period(period).order(:date),diff --git a/app/models/account/balance_calculator.rb b/app/models/account/balance_calculator.rbindex 435f4407..640ec640 100644--- a/app/models/account/balance_calculator.rb+++ b/app/models/account/balance_calculator.rb@@ -11,7 +11,7 @@ class Account::BalanceCalculatoroldest_entry = [ valuations.first, transactions.first ].compact.min_by(&:date)net_transaction_flows = transactions.sum(&:amount)- net_transaction_flows *= -1 if @account.classification == :liability+ net_transaction_flows *= -1 if @account.classification == "liability"implied_start_balance = oldest_entry.is_a?(Valuation) ? oldest_entry.value : @account.balance + net_transaction_flowsprior_balance = implied_start_balance@@ -22,7 +22,7 @@ class Account::BalanceCalculatorcurrent_balance = valuation.valueelsecurrent_day_net_transaction_flows = transactions.select { |t| t.date == date }.sum(&:amount)- current_day_net_transaction_flows *= -1 if @account.classification == :liability+ current_day_net_transaction_flows *= -1 if @account.classification == "liability"current_balance = prior_balance - current_day_net_transaction_flowsenddiff --git a/app/models/family.rb b/app/models/family.rbindex fca65cb0..32261214 100644--- a/app/models/family.rb+++ b/app/models/family.rb@@ -2,4 +2,66 @@ class Family < ApplicationRecordhas_many :users, dependent: :destroyhas_many :accounts, dependent: :destroyhas_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" }+ )+ endenddiff --git a/app/models/money_series.rb b/app/models/money_series.rbindex df493343..f9cea5d9 100644--- a/app/models/money_series.rb+++ b/app/models/money_series.rb@@ -1,6 +1,6 @@class MoneySeriesdef initialize(series, options = {})- @trend_type = options[:trend_type] || :asset # Defines whether a positive trend is good or bad+ @trend_type = options[:trend_type] || "asset" # Defines whether a positive trend is good or bad@accessor = options[:amount_accessor] || :balance@series = seriesenddiff --git a/app/models/trend.rb b/app/models/trend.rbindex d7a5c64f..abb89036 100644--- a/app/models/trend.rb+++ b/app/models/trend.rb@@ -1,10 +1,10 @@class Trendattr_reader :current, :previous, :type- def initialize(current:, previous: nil, type: :asset)+ def initialize(current:, previous: nil, type: "asset")@current = current@previous = previous- @type = type # :asset means positive trend is good, :liability means negative trend is good+ @type = type # asset means positive trend is good, liability means negative trend is goodenddef directiondiff --git a/db/migrate/20240302145715_add_classification_to_accounts.rb b/db/migrate/20240302145715_add_classification_to_accounts.rbnew file mode 100644index 00000000..6a3da5ec--- /dev/null+++ b/db/migrate/20240302145715_add_classification_to_accounts.rb@@ -0,0 +1,18 @@+class AddClassificationToAccounts < ActiveRecord::Migration[7.2]+ def change+ change_table :accounts do |t|+ t.virtual(+ :classification,+ type: :string,+ stored: true,+ as: <<-SQL+ CASE+ WHEN accountable_type IN ('Account::Loan', 'Account::Credit', 'Account::OtherLiability')+ THEN 'liability'+ ELSE 'asset'+ END+ SQL+ )+ end+ end+enddiff --git a/db/schema.rb b/db/schema.rbindex 01b212e0..564b9cf3 100644--- a/db/schema.rb+++ b/db/schema.rb@@ -10,7 +10,7 @@## It's strongly recommended that you check this file into your version control system.-ActiveRecord::Schema[7.2].define(version: 2024_02_27_142457) do+ActiveRecord::Schema[7.2].define(version: 2024_03_02_145715) do# These are extensions that must be enabled in order to support this databaseenable_extension "pgcrypto"enable_extension "plpgsql"@@ -79,6 +79,7 @@ ActiveRecord::Schema[7.2].define(version: 2024_02_27_142457) dot.decimal "converted_balance", precision: 19, scale: 4, default: "0.0"t.string "converted_currency", default: "USD"t.string "status", default: "OK"+ t.virtual "classification", type: :string, as: "\nCASE\n WHEN ((accountable_type)::text = ANY ((ARRAY['Account::Loan'::character varying, 'Account::Credit'::character varying, 'Account::OtherLiability'::character varying])::text[])) THEN 'liability'::text\n ELSE 'asset'::text\nEND", stored: truet.index ["accountable_type"], name: "index_accounts_on_accountable_type"t.index ["family_id"], name: "index_accounts_on_family_id"enddiff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rbindex bb493fee..8ef6db20 100644--- a/test/controllers/accounts_controller_test.rb+++ b/test/controllers/accounts_controller_test.rb@@ -3,7 +3,7 @@ require "test_helper"class AccountsControllerTest < ActionDispatch::IntegrationTestsetup dosign_in @user = users(:family_admin)- @account = accounts(:generic)+ @account = accounts(:checking)endtest "new" dodiff --git a/test/controllers/valuations_controller_test.rb b/test/controllers/valuations_controller_test.rbindex 478aaf75..c0f367ac 100644--- a/test/controllers/valuations_controller_test.rb+++ b/test/controllers/valuations_controller_test.rb@@ -3,7 +3,7 @@ require "test_helper"class ValuationsControllerTest < ActionDispatch::IntegrationTestsetup dosign_in @user = users(:family_admin)- @account = accounts(:generic)+ @account = accounts(:checking)endtest "new" dodiff --git a/test/fixtures/accounts.yml b/test/fixtures/accounts.ymlindex dffb3151..358358c2 100644--- a/test/fixtures/accounts.yml+++ b/test/fixtures/accounts.yml@@ -1,29 +1,27 @@-# No transactions, no valuations, just a generic account-generic:- family: dylan_family- name: No history, generic account- balance: 1200-# Account with only valuationscollectable:family: dylan_familyname: Collectable Accountbalance: 550+ accountable_type: Account::OtherAsset# Account with only transactionschecking:family: dylan_familyname: Checking Accountbalance: 5000+ accountable_type: Account::Depository# Account with both transactions and valuationssavings_with_valuation_overrides:family: dylan_familyname: Savings account with valuation overridesbalance: 20000+ accountable_type: Account::Depository# Liability accountcredit_card:family: dylan_familyname: Credit Cardbalance: 1000+ accountable_type: Account::Creditdiff --git a/test/models/family_test.rb b/test/models/family_test.rbindex d3e37007..83219a37 100644--- a/test/models/family_test.rb+++ b/test/models/family_test.rb@@ -2,27 +2,80 @@ require "test_helper"class FamilyTest < ActiveSupport::TestCasedef setup- @dylan_family = families(:dylan_family)+ @family = families(:dylan_family)++ @family.accounts.each do |account|+ account.accountable = account.classification == "asset" ? account_other_assets(:one) : account_other_liabilities(:one)+ account.sync+ endendtest "should have many users" do- assert @dylan_family.users.size > 0- assert @dylan_family.users.include?(users(:family_admin))+ assert @family.users.size > 0+ assert @family.users.include?(users(:family_admin))endtest "should have many accounts" do- assert @dylan_family.accounts.size > 0+ assert @family.accounts.size > 0endtest "should destroy dependent users" do- assert_difference("User.count", -@dylan_family.users.count) do- @dylan_family.destroy+ assert_difference("User.count", [email protected]) do+ @family.destroyendendtest "should destroy dependent accounts" do- assert_difference("Account.count", -@dylan_family.accounts.count) do- @dylan_family.destroy+ assert_difference("Account.count", [email protected]) do+ @family.destroyendend++ test "should calculate total assets" do+ assert_equal BigDecimal("25550"), @family.assets+ end++ test "should calculate total liabilities" do+ assert_equal BigDecimal("1000"), @family.liabilities+ end++ test "should calculate net worth" do+ assert_equal BigDecimal("24550"), @family.net_worth+ end++ test "calculates asset series" do+ # Sum of expected balances for all asset accounts in balance_calculator_test.rb+ expected_balances = [+ 25650, 26135, 26135, 26135, 26135, 25385, 25385, 25385, 26460, 26460,+ 26460, 26460, 24460, 24460, 24460, 24440, 24440, 24440, 25210, 25210,+ 25210, 25210, 25210, 25210, 25210, 25400, 25250, 26050, 26050, 26050,+ 25550+ ].map(&:to_d)++ assert_equal expected_balances, @family.asset_series.data.map { |b| b[:value].amount }+ end++ test "calculates liability series" do+ # Sum of expected balances for all liability accounts in balance_calculator_test.rb+ expected_balances = [+ 1040, 940, 940, 940, 940, 940, 940, 940, 940, 940,+ 940, 940, 940, 940, 940, 960, 960, 960, 990, 990,+ 990, 990, 990, 990, 990, 1000, 1000, 1000, 1000, 1000,+ 1000+ ].map(&:to_d)++ assert_equal expected_balances, @family.liability_series.data.map { |b| b[:value].amount }+ end++ test "calculates net worth" do+ # Net difference between asset and liability series above+ expected_balances = [+ 24610, 25195, 25195, 25195, 25195, 24445, 24445, 24445, 25520, 25520,+ 25520, 25520, 23520, 23520, 23520, 23480, 23480, 23480, 24220, 24220,+ 24220, 24220, 24220, 24220, 24220, 24400, 24250, 25050, 25050, 25050,+ 24550+ ].map(&:to_d)++ assert_equal expected_balances, @family.net_worth_series.data.map { |b| b[:value].amount }+ endend