mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 15:49:39 +02:00
* Sketch out sync interface * Add basic account sync algorithm * Update logic for final balance in series * Remove start_date concept * Clean up tests * Improve clarity of test * Update app/models/account.rb Co-authored-by: Rob Zolkos <rob@zolkos.com> Signed-off-by: Zach Gollwitzer <zach.gollwitzer@gmail.com> * Update app/models/transaction.rb Co-authored-by: Rob Zolkos <rob@zolkos.com> Signed-off-by: Zach Gollwitzer <zach.gollwitzer@gmail.com> * Update app/models/valuation.rb Co-authored-by: Rob Zolkos <rob@zolkos.com> Signed-off-by: Zach Gollwitzer <zach.gollwitzer@gmail.com> * Re-organize code, simplify job interface * Consolidate balance calculations * More cleanup --------- Signed-off-by: Zach Gollwitzer <zach.gollwitzer@gmail.com> Co-authored-by: Rob Zolkos <rob@zolkos.com>
50 lines
1.5 KiB
Ruby
50 lines
1.5 KiB
Ruby
class Account < ApplicationRecord
|
|
include Syncable
|
|
|
|
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
|