Class: Account

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/bookkeeper/account.rb,
lib/immutable-attribute-plugin/test/rails_root/app/models/account.rb

Direct Known Subclasses

Asset, Expense, Liability, Revenue, ImmutableAccount

Defined Under Namespace

Classes: Asset, Expense, Liability, Revenue

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.accounts_payableObject



60
# File 'lib/bookkeeper/account.rb', line 60

def accounts_payable; roots.find_by_name('Accounts Payable'); end

.accounts_receivableObject



61
# File 'lib/bookkeeper/account.rb', line 61

def accounts_receivable; roots.find_by_name('Accounts Receivable'); end

Instance Method Details

#account_typeObject



31
32
33
# File 'lib/bookkeeper/account.rb', line 31

def 
  self.class::ACCOUNT_TYPE
end

#balanceObject



44
45
46
47
48
49
# File 'lib/bookkeeper/account.rb', line 44

def balance
  postings.inject(0) { |balance, p| 
     = [:asset,:expense].include?() ? -p.amount : p.amount
    balance + 
  }
end

#credit(amount, options = {}) ⇒ Object



16
17
18
# File 'lib/bookkeeper/account.rb', line 16

def credit(amount, options = {})
  new_posting(:credit, amount, options)
end

#debit(amount, options = {}) ⇒ Object



12
13
14
# File 'lib/bookkeeper/account.rb', line 12

def debit(amount, options = {})
  new_posting(:debit, amount, options)
end

#deposit(transaction) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/bookkeeper/account.rb', line 20

def deposit(transaction)
  gross = transaction.notification.amount.cents.to_f / 100
  
  journal = Journal::Deposit.new(:transactable => transaction)
  
  journal.postings |= transaction.to_postings
  journal.postings << self.credit(gross)
  
  journal.save!
end

#destroyObject

Raises:

  • (ActiveRecord::IndestructibleRecord)


55
56
57
# File 'lib/bookkeeper/account.rb', line 55

def destroy
  raise ActiveRecord::IndestructibleRecord
end

#is_subaccountObject



51
52
53
# File 'lib/bookkeeper/account.rb', line 51

def is_subaccount
  self != self.root
end

#nameObject



35
36
37
# File 'lib/bookkeeper/account.rb', line 35

def name
  accountable_has_own_name ? accountable.name : self[:name]
end

#postings_with_recursionObject



39
40
41
# File 'lib/bookkeeper/account.rb', line 39

def postings_with_recursion
  (self_and_all_children.collect { || .postings_without_recursion }).flatten
end