Class: DoubleEntry::Line
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- DoubleEntry::Line
- Defined in:
- lib/double_entry/line.rb
Overview
This is the table to end all tables!
Every financial transaction gets two entries in here: one for the source account, and one for the destination account. Normal double-entry accounting principles are followed.
This is a log table, and should (ideally) never be updated.
Indexes
The indexes on this table are carefully chosen, as it's both big and heavily loaded.
lines_scope_account_id_idx
ADD INDEX `lines_scope_account_id_idx` (scope, account, id)
This is the important one. It's used primarily for querying the current balance of an account. eg:
SELECT * FROM `lines` WHERE scope = ? AND account = ? ORDER BY id DESC LIMIT 1
lines_scope_account_created_at_idx
ADD INDEX `lines_scope_account_created_at_idx` (scope, account, created_at)
Used for querying historic balances:
SELECT * FROM `lines` WHERE scope = ? AND account = ? AND created_at < ? ORDER BY id DESC LIMIT 1
And for reporting on account changes over a time period:
SELECT SUM(amount) FROM `lines` WHERE scope = ? AND account = ? AND created_at BETWEEN ? AND ?
lines_account_created_at_idx and lines_account_code_created_at_idx
ADD INDEX `lines_account_created_at_idx` (account, created_at);
ADD INDEX `lines_account_code_created_at_idx` (account, code, created_at);
These two are used for generating reports, which need to sum things by account, or account and code, over a particular period.
Class Method Summary collapse
-
.find_id_and_created_at(options) ⇒ Object
Query out just the id and created_at fields for lines, without instantiating any ActiveRecord objects.
Instance Method Summary collapse
- #account ⇒ Object
- #account=(_account) ⇒ Object
- #amount ⇒ Object
- #amount=(money) ⇒ Object
- #balance ⇒ Object
- #balance=(money) ⇒ Object
- #code ⇒ Object
- #code=(code) ⇒ Object
- #currency ⇒ Object
- #decrease? ⇒ Boolean
- #increase? ⇒ Boolean
- #pair ⇒ Object
- #partner ⇒ Object
- #partner_account ⇒ Object
- #partner_account=(_partner_account) ⇒ Object
- #save ⇒ Object
- #save! ⇒ Object
Class Method Details
.find_id_and_created_at(options) ⇒ Object
Query out just the id and created_at fields for lines, without instantiating any ActiveRecord objects.
145 146 147 148 149 150 |
# File 'lib/double_entry/line.rb', line 145 def self.find_id_and_created_at() connection.select_rows <<-SQL SELECT id, created_at FROM #{Line.quoted_table_name} #{[:joins]} WHERE #{sanitize_sql_for_conditions([:conditions])} SQL end |
Instance Method Details
#account ⇒ Object
104 105 106 |
# File 'lib/double_entry/line.rb', line 104 def account DoubleEntry.account(self[:account].to_sym, :scope => scope) end |
#account=(_account) ⇒ Object
97 98 99 100 101 102 |
# File 'lib/double_entry/line.rb', line 97 def account=(_account) self[:account] = _account.identifier.to_s self.scope = _account.scope_identity raise "Missing Account" unless account _account end |
#amount ⇒ Object
62 63 64 |
# File 'lib/double_entry/line.rb', line 62 def amount self[:amount] && Money.new(self[:amount], currency) end |
#amount=(money) ⇒ Object
66 67 68 |
# File 'lib/double_entry/line.rb', line 66 def amount=(money) self[:amount] = (money && money.fractional) end |
#balance ⇒ Object
70 71 72 |
# File 'lib/double_entry/line.rb', line 70 def balance self[:balance] && Money.new(self[:balance], currency) end |
#balance=(money) ⇒ Object
74 75 76 |
# File 'lib/double_entry/line.rb', line 74 def balance=(money) self[:balance] = (money && money.fractional) end |
#code ⇒ Object
93 94 95 |
# File 'lib/double_entry/line.rb', line 93 def code self[:code].try(:to_sym) end |
#code=(code) ⇒ Object
88 89 90 91 |
# File 'lib/double_entry/line.rb', line 88 def code=(code) self[:code] = code.try(:to_s) code end |
#currency ⇒ Object
108 109 110 |
# File 'lib/double_entry/line.rb', line 108 def currency account.currency if self[:account] end |
#decrease? ⇒ Boolean
135 136 137 |
# File 'lib/double_entry/line.rb', line 135 def decrease? amount < Money.zero end |
#increase? ⇒ Boolean
139 140 141 |
# File 'lib/double_entry/line.rb', line 139 def increase? amount > Money.zero end |
#pair ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/double_entry/line.rb', line 127 def pair if decrease? [self, partner] else [partner, self] end end |
#partner ⇒ Object
123 124 125 |
# File 'lib/double_entry/line.rb', line 123 def partner self.class.find(partner_id) end |
#partner_account ⇒ Object
119 120 121 |
# File 'lib/double_entry/line.rb', line 119 def partner_account DoubleEntry.account(self[:partner_account].to_sym, :scope => partner_scope) end |
#partner_account=(_partner_account) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/double_entry/line.rb', line 112 def partner_account=(_partner_account) self[:partner_account] = _partner_account.identifier.to_s self.partner_scope = _partner_account.scope_identity raise "Missing Partner Account" unless partner_account _partner_account end |
#save ⇒ Object
78 79 80 81 |
# File 'lib/double_entry/line.rb', line 78 def save(*) check_balance_will_not_be_sent_negative super end |
#save! ⇒ Object
83 84 85 86 |
# File 'lib/double_entry/line.rb', line 83 def save!(*) check_balance_will_not_be_sent_negative super end |