Class: Aloe::Account
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Aloe::Account
- Extended by:
- AccountRepository
- Defined in:
- lib/aloe/account.rb
Instance Method Summary collapse
-
#balance ⇒ Money
Return the balance of account.
-
#balance_at(time) ⇒ Money
Computes the balance of the account at the requested date and time.
-
#balance_of?(amount, option = nil) ⇒ true, false
Does account have minimum given balance?.
-
#closeable? ⇒ true, false
Can the account be closed?.
-
#create_entry(cents_amount) ⇒ Aloe::Entry
Creates entry in the account.
-
#currency?(currency_in_question) ⇒ true, false
Is account in given currency?.
-
#debit_possible?(amount) ⇒ true, false
Is the debit of given amount possible?.
-
#owner_type_and_id ⇒ String
Return account owner type and it’s ID.
-
#rollback_all ⇒ Object
Rolls back all transactions on this account.
-
#to_s ⇒ String
Return string representation of account.
-
#turnover(period) ⇒ Money
Return account turnover over given period of time.
Methods included from AccountRepository
closed, currency, default_scope, owner, trial_balance
Instance Method Details
#balance ⇒ Money
Return the balance of account
62 63 64 |
# File 'lib/aloe/account.rb', line 62 def balance Money.new read_attribute(:balance), currency end |
#balance_at(time) ⇒ Money
Computes the balance of the account at the requested date and time. Returns nil if the account did not exist at that time.
71 72 73 74 75 76 |
# File 'lib/aloe/account.rb', line 71 def balance_at(time) return nil unless created_at <= time amount = entries.where('created_at >= ?', time).sum(&:amount) offset = amount == 0 ? Money.new(0, currency) : amount balance - offset end |
#balance_of?(amount, option = nil) ⇒ true, false
Does account have minimum given balance?
83 84 85 86 87 |
# File 'lib/aloe/account.rb', line 83 def balance_of?(amount, option = nil) reload if option == :reload cents_amount = amount.respond_to?(:cents) ? amount.cents : amount read_attribute(:balance) >= cents_amount end |
#closeable? ⇒ true, false
Can the account be closed?
An account can be closed only if the balance is 0.
94 95 96 |
# File 'lib/aloe/account.rb', line 94 def closeable? balance.zero? end |
#create_entry(cents_amount) ⇒ Aloe::Entry
Creates entry in the account.
Creates new entry and modified the balance.
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/aloe/account.rb', line 104 def create_entry(cents_amount) with_lock(true) do if cents_amount < 0 && !debit_possible?(-cents_amount) raise Aloe::InsufficientBalanceError.new(self, -cents_amount) end entry = entries.create! amount: cents_amount increment! :balance, cents_amount entry end end |
#currency?(currency_in_question) ⇒ true, false
Is account in given currency?
119 120 121 |
# File 'lib/aloe/account.rb', line 119 def currency?(currency_in_question) currency.to_s == currency_in_question.to_s end |
#debit_possible?(amount) ⇒ true, false
Is the debit of given amount possible?
127 128 129 |
# File 'lib/aloe/account.rb', line 127 def debit_possible?(amount) allow_negative_balance ? true : balance_of?(amount, :reload) end |
#owner_type_and_id ⇒ String
Return account owner type and it’s ID.
156 157 158 |
# File 'lib/aloe/account.rb', line 156 def owner_type_and_id "#{owner_type} #{owner_id}" if owner.present? end |
#rollback_all ⇒ Object
Rolls back all transactions on this account.
132 133 134 135 |
# File 'lib/aloe/account.rb', line 132 def rollback_all transactions = entries.map &:transaction transactions.map &:rollback end |
#to_s ⇒ String
Return string representation of account.
140 141 142 |
# File 'lib/aloe/account.rb', line 140 def to_s name? ? name : owner_type_and_id end |
#turnover(period) ⇒ Money
Return account turnover over given period of time.
148 149 150 151 |
# File 'lib/aloe/account.rb', line 148 def turnover(period) turnover = entries.where(created_at: period).sum &:amount turnover == 0 ? Money.new(0, currency) : turnover end |