mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 15:49:39 +02:00
Multi-currency part 1 (#542)
* Add family snapshots table * Add snapshot method, clean up family expected results * Remove old sync trigger
This commit is contained in:
parent
1cdf5ea6a7
commit
c60ddaec1d
9 changed files with 169 additions and 141 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
</div>
|
||||
</div>
|
||||
<%= 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 %>
|
||||
<div class="bg-white shadow-xs rounded-xl border border-alpha-black-25 rounded-lg">
|
||||
<div class="p-4 flex justify-between">
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue