2024-02-02 09:05:04 -06:00
|
|
|
class Account < ApplicationRecord
|
2024-02-29 08:32:52 -05:00
|
|
|
include Syncable
|
|
|
|
|
2024-02-24 02:18:30 +00:00
|
|
|
broadcasts_refreshes
|
2024-02-02 09:05:04 -06:00
|
|
|
belongs_to :family
|
2024-02-20 09:07:55 -05:00
|
|
|
has_many :balances, class_name: "AccountBalance"
|
|
|
|
has_many :valuations
|
2024-02-23 21:34:33 -05:00
|
|
|
has_many :transactions
|
2024-02-02 11:09:31 -06:00
|
|
|
|
2024-02-09 14:26:54 +00:00
|
|
|
delegated_type :accountable, types: Accountable::TYPES, dependent: :destroy
|
2024-02-02 23:06:29 +00:00
|
|
|
|
2024-02-03 15:38:52 -05:00
|
|
|
delegate :type_name, to: :accountable
|
2024-02-10 16:18:56 -06:00
|
|
|
before_create :check_currency
|
|
|
|
|
2024-02-29 16:35:54 -05:00
|
|
|
def classification
|
|
|
|
classifications = {
|
|
|
|
"Account::Depository" => :asset,
|
|
|
|
"Account::Investment" => :asset,
|
|
|
|
"Account::Property" => :asset,
|
|
|
|
"Account::Vehicle" => :asset,
|
|
|
|
"Account::OtherAsset" => :asset,
|
|
|
|
"Account::Loan" => :liability,
|
|
|
|
"Account::Credit" => :liability,
|
|
|
|
"Account::OtherLiability" => :liability
|
|
|
|
}
|
|
|
|
|
|
|
|
classifications[accountable_type]
|
|
|
|
end
|
|
|
|
|
2024-02-22 11:35:06 -05:00
|
|
|
def balance_series(period)
|
2024-03-01 17:17:34 -05:00
|
|
|
MoneySeries.new(
|
|
|
|
balances.in_period(period).order(:date),
|
|
|
|
{ trend_type: classification }
|
|
|
|
)
|
2024-02-20 09:07:55 -05:00
|
|
|
end
|
|
|
|
|
2024-02-22 11:35:06 -05:00
|
|
|
def valuation_series
|
2024-03-01 17:17:34 -05:00
|
|
|
MoneySeries.new(
|
|
|
|
valuations.order(:date),
|
|
|
|
{ trend_type: classification, amount_accessor: :value }
|
|
|
|
)
|
2024-02-20 09:07:55 -05:00
|
|
|
end
|
|
|
|
|
2024-02-10 16:18:56 -06:00
|
|
|
def check_currency
|
2024-02-27 12:43:49 -05:00
|
|
|
if self.currency == self.family.currency
|
|
|
|
self.converted_balance = self.balance
|
|
|
|
self.converted_currency = self.currency
|
2024-02-10 16:18:56 -06:00
|
|
|
else
|
2024-02-27 12:43:49 -05:00
|
|
|
self.converted_balance = ExchangeRate.convert(self.currency, self.family.currency, self.balance)
|
2024-02-10 16:18:56 -06:00
|
|
|
self.converted_currency = self.family.currency
|
|
|
|
end
|
|
|
|
end
|
2024-02-02 09:05:04 -06:00
|
|
|
end
|