1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-20 05:39:39 +02:00
Maybe/app/models/account.rb

54 lines
1.4 KiB
Ruby
Raw Normal View History

2024-02-02 09:05:04 -06:00
class Account < ApplicationRecord
include Syncable
broadcasts_refreshes
2024-02-02 09:05:04 -06:00
belongs_to :family
has_many :balances, class_name: "AccountBalance"
has_many :valuations
has_many :transactions
2024-02-02 11:09:31 -06:00
delegated_type :accountable, types: Accountable::TYPES, dependent: :destroy
2024-02-02 23:06:29 +00:00
delegate :type_name, to: :accountable
before_create :check_currency
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
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
2024-02-02 09:05:04 -06:00
end