Class: DoubleEntry::Line

Inherits:
ActiveRecord::Base
  • Object
show all
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

Instance Method Summary collapse

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.



144
145
146
147
148
149
# File 'lib/double_entry/line.rb', line 144

def self.find_id_and_created_at(options)
  connection.select_rows <<-SQL
    SELECT id, created_at FROM #{Line.quoted_table_name} #{options[:joins]}
     WHERE #{sanitize_sql_for_conditions(options[:conditions])}
  SQL
end

Instance Method Details

#accountObject



103
104
105
# File 'lib/double_entry/line.rb', line 103

def 
  DoubleEntry.(self[:account].to_sym, :scope => scope)
end

#account=(account) ⇒ Object



96
97
98
99
100
101
# File 'lib/double_entry/line.rb', line 96

def account=()
  self[:account] = .identifier.to_s
  self.scope = .scope_identity
  fail 'Missing Account' unless self.
  
end

#amountObject



61
62
63
# File 'lib/double_entry/line.rb', line 61

def amount
  self[:amount] && Money.new(self[:amount], currency)
end

#amount=(money) ⇒ Object



65
66
67
# File 'lib/double_entry/line.rb', line 65

def amount=(money)
  self[:amount] = (money && money.fractional)
end

#balanceObject



69
70
71
# File 'lib/double_entry/line.rb', line 69

def balance
  self[:balance] && Money.new(self[:balance], currency)
end

#balance=(money) ⇒ Object



73
74
75
# File 'lib/double_entry/line.rb', line 73

def balance=(money)
  self[:balance] = (money && money.fractional)
end

#codeObject



92
93
94
# File 'lib/double_entry/line.rb', line 92

def code
  self[:code].try(:to_sym)
end

#code=(code) ⇒ Object



87
88
89
90
# File 'lib/double_entry/line.rb', line 87

def code=(code)
  self[:code] = code.try(:to_s)
  code
end

#currencyObject



107
108
109
# File 'lib/double_entry/line.rb', line 107

def currency
  .currency if self[:account]
end

#decrease?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/double_entry/line.rb', line 134

def decrease?
  amount < Money.zero
end

#increase?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/double_entry/line.rb', line 138

def increase?
  amount > Money.zero
end

#pairObject



126
127
128
129
130
131
132
# File 'lib/double_entry/line.rb', line 126

def pair
  if decrease?
    [self, partner]
  else
    [partner, self]
  end
end

#partnerObject



122
123
124
# File 'lib/double_entry/line.rb', line 122

def partner
  self.class.find(partner_id)
end

#partner_accountObject



118
119
120
# File 'lib/double_entry/line.rb', line 118

def 
  DoubleEntry.(self[:partner_account].to_sym, :scope => partner_scope)
end

#partner_account=(partner_account) ⇒ Object



111
112
113
114
115
116
# File 'lib/double_entry/line.rb', line 111

def partner_account=()
  self[:partner_account] = .identifier.to_s
  self.partner_scope = .scope_identity
  fail 'Missing Partner Account' unless self.
  
end

#saveObject



77
78
79
80
# File 'lib/double_entry/line.rb', line 77

def save(*)
  check_balance_will_remain_valid
  super
end

#save!Object



82
83
84
85
# File 'lib/double_entry/line.rb', line 82

def save!(*)
  check_balance_will_remain_valid
  super
end