Class: Aloe::Account

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
AccountRepository
Defined in:
lib/aloe/account.rb

Instance Method Summary collapse

Methods included from AccountRepository

closed, currency, default_scope, owner, trial_balance

Instance Method Details

#balanceMoney

Return the balance of account

Returns:

  • (Money)

    The balance



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.

Parameters:

  • the (Time)

    date and time

Returns:

  • (Money)

    the past balance



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?

Parameters:

  • amount (Money, Fixnum)

    Amount in question in cents

  • option (NilClass, Symbol) (defaults to: nil)

    Option

Returns:

  • (true, false)


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.

Returns:

  • (true, false)


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.

Parameters:

  • cents_amount (Fixnum)

    Amount in cents

Returns:



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?

Parameters:

  • currency_in_question (Currency, Symbol, String)

Returns:

  • (true, false)


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?

Parameters:

  • amount (Money, Fixnum)

    Amount in question in cents

Returns:

  • (true, false)


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_idString

Return account owner type and it’s ID.

Returns:

  • (String)


156
157
158
# File 'lib/aloe/account.rb', line 156

def owner_type_and_id
  "#{owner_type} #{owner_id}" if owner.present?
end

#rollback_allObject

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_sString

Return string representation of account.

Returns:

  • (String)


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.

Parameters:

  • period (Range)

Returns:

  • (Money)


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