Class: Borutus::Account Abstract
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Borutus::Account
- Defined in:
- app/models/borutus/account.rb
Overview
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
Class Method Summary collapse
-
.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.
-
.trial_balance ⇒ BigDecimal
The trial balance of all accounts in the system.
- .types ⇒ Object
Instance Method Summary collapse
-
#balance(options = {}) ⇒ BigDecimal
The balance of the account.
-
#credits_balance(options = {}) ⇒ BigDecimal
The credit balance for the account.
-
#debits_balance(options = {}) ⇒ BigDecimal
The debit balance for the account.
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
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( = {}) if new.class == Borutus::Account raise(NoMethodError, "undefined method 'balance'") end accounts_balance = BigDecimal("0") accounts = all accounts.each do |account| if account.contra accounts_balance -= account.balance() else accounts_balance += account.balance() end end accounts_balance end |
.trial_balance ⇒ BigDecimal
The trial balance of all accounts in the system. This should always equal zero, otherwise there is an error in the system.
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 |
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
118 119 120 121 122 123 124 125 126 127 128 |
# File 'app/models/borutus/account.rb', line 118 def balance( = {}) if self.class == Borutus::Account raise NoMethodError, "undefined method 'balance'" end if normal_credit_balance ^ contra credits_balance() - debits_balance() else debits_balance() - credits_balance() 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
145 146 147 |
# File 'app/models/borutus/account.rb', line 145 def credits_balance( = {}) credit_amounts.balance() 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
164 165 166 |
# File 'app/models/borutus/account.rb', line 164 def debits_balance( = {}) debit_amounts.balance() end |