diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 25cee640..b2a3bb98 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -3,9 +3,10 @@ class PagesController < ApplicationController before_action :authenticate_user! def dashboard - @asset_series = Current.family.asset_series(@period) - @liability_series = Current.family.liability_series(@period) - @net_worth_series = Current.family.net_worth_series(@period) + snapshot = Current.family.snapshot(@period) + @net_worth_series = snapshot[:net_worth_series] + @asset_series = snapshot[:asset_series] + @liability_series = snapshot[:liability_series] @account_groups = Current.family.accounts.by_group(@period) end end diff --git a/app/models/account.rb b/app/models/account.rb index ae1655c7..a882cf53 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -7,7 +7,11 @@ class Account < ApplicationRecord has_many :valuations has_many :transactions + enum :status, { ok: "ok", syncing: "syncing", error: "error" }, validate: true + scope :active, -> { where(is_active: true) } + scope :assets, -> { where(classification: "asset") } + scope :liabilities, -> { where(classification: "liability") } delegated_type :accountable, types: Accountable::TYPES, dependent: :destroy @@ -28,6 +32,10 @@ class Account < ApplicationRecord [ { name: "Manual accounts", accounts: all.order(balance: :desc).group_by(&:accountable_type) } ] end + def self.some_syncing? + exists?(status: "syncing") + end + # TODO: We will need a better way to encapsulate large queries & transformation logic, but leaving all in one spot until # we have a better understanding of the requirements def self.by_group(period = Period.all) @@ -70,50 +78,8 @@ class Account < ApplicationRecord total_liabilities = liabilities.sum(&:end_balance) { - asset: { - total: total_assets, - groups: assets.group_by(&:accountable_type).transform_values do |rows| - end_balance = rows.sum(&:end_balance) - start_balance = rows.sum(&:start_balance) - { - start_balance: start_balance, - end_balance: end_balance, - allocation: (end_balance / total_assets * 100).round(2), - trend: Trend.new(current: end_balance, previous: start_balance, type: "asset"), - accounts: rows.map do |account| - { - name: account.name, - start_balance: account.start_balance, - end_balance: account.end_balance, - allocation: (account.end_balance / total_assets * 100).round(2), - trend: Trend.new(current: account.end_balance, previous: account.start_balance, type: "asset") - } - end - } - end - }, - liability: { - total: total_liabilities, - groups: liabilities.group_by(&:accountable_type).transform_values do |rows| - end_balance = rows.sum(&:end_balance) - start_balance = rows.sum(&:start_balance) - { - start_balance: start_balance, - end_balance: end_balance, - allocation: (end_balance / total_liabilities * 100).round(2), - trend: Trend.new(current: end_balance, previous: start_balance, type: "liability"), - accounts: rows.map do |account| - { - name: account.name, - start_balance: account.start_balance, - end_balance: account.end_balance, - allocation: (account.end_balance / total_liabilities * 100).round(2), - trend: Trend.new(current: account.end_balance, previous: account.start_balance, type: "liability") - } - end - } - end - } + asset: build_group_summary(assets, "asset"), + liability: build_group_summary(liabilities, "liability") } end @@ -128,4 +94,34 @@ class Account < ApplicationRecord self.converted_currency = self.family.currency end end + + def self.build_group_summary(accounts, classification) + total_balance = accounts.sum(&:end_balance) + { + total: total_balance, + groups: accounts.group_by(&:accountable_type).transform_values do |rows| + build_account_summary(rows, total_balance, classification) + end + } + end + + def self.build_account_summary(accounts, total_balance, classification) + end_balance = accounts.sum(&:end_balance) + start_balance = accounts.sum(&:start_balance) + { + start_balance: start_balance, + end_balance: end_balance, + allocation: (end_balance / total_balance * 100).round(2), + trend: Trend.new(current: end_balance, previous: start_balance, type: classification), + accounts: accounts.map do |account| + { + name: account.name, + start_balance: account.start_balance, + end_balance: account.end_balance, + allocation: (account.end_balance / total_balance * 100).round(2), + trend: Trend.new(current: account.end_balance, previous: account.start_balance, type: classification) + } + end + } + end end diff --git a/app/models/account/syncable.rb b/app/models/account/syncable.rb index 45efe6d8..7e0b0d8b 100644 --- a/app/models/account/syncable.rb +++ b/app/models/account/syncable.rb @@ -6,13 +6,13 @@ module Account::Syncable end def sync - update!(status: "SYNCING") + update!(status: "syncing") synced_daily_balances = Account::BalanceCalculator.new(self).daily_balances self.balances.upsert_all(synced_daily_balances, unique_by: :index_account_balances_on_account_id_and_date) self.balances.where("date < ?", effective_start_date).delete_all - update!(status: "OK") + update!(status: "ok") rescue => e - update!(status: "ERROR") + update!(status: "error") Rails.logger.error("Failed to sync account #{id}: #{e.message}") end diff --git a/app/models/family.rb b/app/models/family.rb index e69620fc..3f81b93b 100644 --- a/app/models/family.rb +++ b/app/models/family.rb @@ -4,65 +4,41 @@ class Family < ApplicationRecord has_many :transactions, through: :accounts has_many :transaction_categories, dependent: :destroy, class_name: "Transaction::Category" + 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 + + { + asset_series: MoneySeries.new(query, { trend_type: :asset, amount_accessor: "assets" }), + liability_series: MoneySeries.new(query, { trend_type: :liability, amount_accessor: "liabilities" }), + net_worth_series: MoneySeries.new(query, { trend_type: :asset, amount_accessor: "net_worth" }) + } + end + + def effective_start_date + accounts.active.joins(:balances).minimum("account_balances.date") || Date.current + end + def net_worth accounts.active.sum("CASE WHEN classification = 'asset' THEN balance ELSE -balance END") end def assets - accounts.active.where(classification: "asset").sum(:balance) + accounts.active.assets.sum(:balance) end def liabilities - accounts.active.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" } - ) + accounts.active.liabilities.sum(:balance) end end diff --git a/app/views/accounts/show.html.erb b/app/views/accounts/show.html.erb index c7780643..11123b4c 100644 --- a/app/views/accounts/show.html.erb +++ b/app/views/accounts/show.html.erb @@ -21,7 +21,7 @@ <%= turbo_frame_tag "sync_message" do %> - <%= render partial: "accounts/sync_message", locals: { is_syncing: @account.status == "SYNCING" } %> + <%= render partial: "accounts/sync_message", locals: { is_syncing: @account.syncing? } %> <% end %>