Class: Credere::Account Abstract

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
NoTenancy, Tenancy
Defined in:
app/models/credere/account.rb

Overview

This class is abstract.

An account must be a subclass to be saved to the database. The Account class has a singleton method Account.trial_balance to calculate the balance on all Accounts.

The Account class represents accounts in the system. Each account must be subclassed as one of the following types:

TYPE        | NORMAL BALANCE    | DESCRIPTION
--------------------------------------------------------------------------
Asset       | Debit             | Resources owned by the Business Entity
Liability   | Credit            | Debts owed to outsiders
Equity      | Credit            | Owners rights to the Assets
Revenue     | Credit            | Increases in owners equity
Expense     | Debit             | Assets or services consumed in the generation of revenue

Each account can also be marked as a “Contra Account”. A contra account will have it’s normal balance swapped. For example, to remove equity, a “Drawing” account may be created as a contra equity account as follows:

Credere::Equity.create(:name => "Drawing", contra => true)

At all times the balance of all accounts should conform to the “accounting equation”

Credere::Assets = Liabilties + Owner's Equity

Each sublclass account acts as it’s own ledger. See the individual subclasses for a description.

Direct Known Subclasses

Asset, Equity, Expense, Liability, Revenue

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.balance(options = {}) ⇒ BigDecimal

This class method is used to return the balance of all accounts for a given class and is intended for use only on account subclasses.

Contra accounts are automatically subtracted from the balance.

Takes an optional hash specifying :from_date and :to_date for calculating balances during periods. :from_date and :to_date may be strings of the form “yyyy-mm-dd” or Ruby Date objects

Examples:

>> Credere::Liability.balance({:from_date => "2000-01-01", :to_date => Date.today})
=> #<BigDecimal:103259bb8,'0.1E4',4(12)>
>> Credere::Liability.balance
=> #<BigDecimal:1030fcc98,'0.82875E5',8(20)>

Returns:

  • (BigDecimal)

    The decimal value balance



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/models/credere/account.rb', line 136

def self.balance(options={})
  if self.new.class == Credere::Account
    raise(NoMethodError, "undefined method 'balance'")
  else
    accounts_balance = BigDecimal.new('0')
    accounts = self.all
    accounts.each do ||
      if .contra
        accounts_balance -= .balance(options)
      else
        accounts_balance += .balance(options)
      end
    end
    accounts_balance
  end
end

.trial_balanceBigDecimal

The trial balance of all accounts in the system. This should always equal zero, otherwise there is an error in the system.

Examples:

>> Account.trial_balance.to_i
=> 0

Returns:

  • (BigDecimal)

    The decimal value balance of all accounts



161
162
163
164
165
166
167
# File 'app/models/credere/account.rb', line 161

def self.trial_balance
  if self.new.class == Credere::Account
    Credere::Asset.balance - (Credere::Liability.balance + Credere::Equity.balance + Credere::Revenue.balance - Credere::Expense.balance)
  else
    raise(NoMethodError, "undefined method 'trial_balance'")
  end
end

Instance Method Details

#balance(options = {}) ⇒ BigDecimal

The balance of the account. This instance method is intended for use only on instances of account subclasses.

If the account has a normal credit balance, the debits are subtracted from the credits unless this is a contra account, in which case credits are substracted from debits.

For a normal debit balance, the credits are subtracted from the debits unless this is a contra account, in which case debits are subtracted from credits.

Takes an optional hash specifying :from_date and :to_date for calculating balances during periods. :from_date and :to_date may be strings of the form “yyyy-mm-dd” or Ruby Date objects

Examples:

>> liability.balance({:from_date => "2000-01-01", :to_date => Date.today})
=> #<BigDecimal:103259bb8,'0.1E4',4(12)>
>> liability.balance
=> #<BigDecimal:103259bb8,'0.2E4',4(12)>

Returns:

  • (BigDecimal)

    The decimal value balance



71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/credere/account.rb', line 71

def balance(options={})
  if self.class == Credere::Account
    raise(NoMethodError, "undefined method 'balance'")
  else
    if self.normal_credit_balance ^ contra
      credits_balance(options) - debits_balance(options)
    else
      debits_balance(options) - credits_balance(options)
    end
  end
end

#credits_balance(options = {}) ⇒ BigDecimal

The credit balance for the account.

Takes an optional hash specifying :from_date and :to_date for calculating balances during periods. :from_date and :to_date may be strings of the form “yyyy-mm-dd” or Ruby Date objects

Examples:

>> asset.credits_balance({:from_date => "2000-01-01", :to_date => Date.today})
=> #<BigDecimal:103259bb8,'0.1E4',4(12)>
>> asset.credits_balance
=> #<BigDecimal:103259bb8,'0.1E4',4(12)>

Returns:

  • (BigDecimal)

    The decimal value credit balance



97
98
99
# File 'app/models/credere/account.rb', line 97

def credits_balance(options={})
  credit_amounts.balance(options)
end

#debits_balance(options = {}) ⇒ BigDecimal

The debit balance for the account.

Takes an optional hash specifying :from_date and :to_date for calculating balances during periods. :from_date and :to_date may be strings of the form “yyyy-mm-dd” or Ruby Date objects

Examples:

>> asset.debits_balance({:from_date => "2000-01-01", :to_date => Date.today})
=> #<BigDecimal:103259bb8,'0.1E4',4(12)>
>> asset.debits_balance
=> #<BigDecimal:103259bb8,'0.3E4',4(12)>

Returns:

  • (BigDecimal)

    The decimal value credit balance



115
116
117
# File 'app/models/credere/account.rb', line 115

def debits_balance(options={})
  debit_amounts.balance(options)
end