Class: Borutus::Account Abstract

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
NoTenancy, Tenancy
Defined in:
app/models/borutus/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:

Borutus::Equity.create(name: "Drawing", contra: true)

At all times the balance of all accounts should conform to the “accounting equation” Borutus::Assets = Liabilties + Owner’s Equity

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

Contra Accounts

See Also:

Author:

  • Michael Bulat

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:

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

Returns:

  • (BigDecimal)

    The decimal value balance



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/models/borutus/account.rb', line 186

def self.balance(options = {})
  if new.class == Borutus::Account
    raise(NoMethodError, "undefined method 'balance'")
  end

  accounts_balance = BigDecimal("0")
  accounts = all
  accounts.each do ||
    if .contra
      accounts_balance -= .balance(options)
    else
      accounts_balance += .balance(options)
    end
  end
  accounts_balance
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



211
212
213
214
215
216
217
218
219
220
221
222
# File 'app/models/borutus/account.rb', line 211

def self.trial_balance
  if new.class != Borutus::Account
    raise NoMethodError, "undefined method 'trial_balance'"
  end

  Borutus::Asset.balance - (
    Borutus::Liability.balance +
    Borutus::Equity.balance +
    Borutus::Revenue.balance -
    Borutus::Expense.balance
  )
end

.typesObject



78
79
80
81
82
83
84
85
86
# File 'app/models/borutus/account.rb', line 78

def self.types
  [
    ::Borutus::Asset,
    ::Borutus::Equity,
    ::Borutus::Expense,
    ::Borutus::Liability,
    ::Borutus::Revenue,
  ]
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



118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/borutus/account.rb', line 118

def balance(options = {})
  if self.class == Borutus::Account
    raise NoMethodError, "undefined method 'balance'"
  end

  if normal_credit_balance ^ contra
    credits_balance(options) - debits_balance(options)
  else
    debits_balance(options) - credits_balance(options)
  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



145
146
147
# File 'app/models/borutus/account.rb', line 145

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



164
165
166
# File 'app/models/borutus/account.rb', line 164

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