mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-25 08:09:38 +02:00
* Add classification generated column to account * Add basic net worth calculation * Add net worth tests * Fix lint errors
37 lines
946 B
Ruby
37 lines
946 B
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
|
|
|
|
before_create :check_currency
|
|
|
|
def balance_series(period)
|
|
MoneySeries.new(
|
|
balances.in_period(period).order(:date),
|
|
{ trend_type: classification }
|
|
)
|
|
end
|
|
|
|
def valuation_series
|
|
MoneySeries.new(
|
|
valuations.order(:date),
|
|
{ trend_type: classification, amount_accessor: :value }
|
|
)
|
|
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
|