1
0
Fork 0
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:
Zach Gollwitzer 2024-03-11 16:32:13 -04:00 committed by GitHub
parent 1cdf5ea6a7
commit c60ddaec1d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 169 additions and 141 deletions

View file

@ -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