mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-23 07:09:39 +02:00
* Rename account balance field for clarity `original_balance` and `original_currency` may infer that these values are "original" to the account. In reality, they represent the "current" balance and currency on the account. * Prepare fixture data for account sync testing * Update to new field * Fix conflicts * Remove local schema change
48 lines
1.4 KiB
Ruby
48 lines
1.4 KiB
Ruby
class Account < ApplicationRecord
|
|
broadcasts_refreshes
|
|
belongs_to :family
|
|
has_many :balances, class_name: "AccountBalance"
|
|
has_many :valuations
|
|
has_many :transactions
|
|
|
|
delegated_type :accountable, types: Accountable::TYPES, dependent: :destroy
|
|
|
|
delegate :type_name, to: :accountable
|
|
before_create :check_currency
|
|
|
|
def balance_series(period)
|
|
filtered_balances = balances.in_period(period).order(:date)
|
|
return nil if filtered_balances.empty?
|
|
|
|
series_data = [ nil, *filtered_balances ].each_cons(2).map do |previous, current|
|
|
trend = current&.trend(previous)
|
|
{ data: current, trend: { amount: trend&.amount, direction: trend&.direction, percent: trend&.percent } }
|
|
end
|
|
|
|
last_balance = series_data.last[:data]
|
|
|
|
{
|
|
series_data: series_data,
|
|
last_balance: last_balance.balance,
|
|
trend: last_balance.trend(series_data.first[:data])
|
|
}
|
|
end
|
|
|
|
def valuation_series
|
|
series_data = [ nil, *valuations.order(:date) ].each_cons(2).map do |previous, current|
|
|
{ value: current, trend: current&.trend(previous) }
|
|
end
|
|
|
|
series_data.reverse_each
|
|
end
|
|
|
|
def check_currency
|
|
if self.currency == self.family.currency
|
|
self.converted_balance = self.balance
|
|
self.converted_currency = self.currency
|
|
else
|
|
self.converted_balance = ExchangeRate.convert(self.currency, self.family.currency, self.balance)
|
|
self.converted_currency = self.family.currency
|
|
end
|
|
end
|
|
end
|