class ESA::Transaction

Transactions are the recording of debits and credits to various accounts. This table can be thought of as a traditional accounting Journal.

Transactions are created from transitions in the corresponding Flag.

@author Lenno Nagel, Michael Bulat

Public Instance Methods

amounts_match_spec?(spec) click to toggle source
# File app/models/esa/transaction.rb, line 59
def amounts_match_spec?(spec)
  to_check = [
        [self.amounts.credits.all, spec[:credits]],
        [self.amounts.debits.all, spec[:debits]]
      ]

  to_check.map do |amounts,amount_spec|
    a = amounts.map{|a| [a.account, a.amount]}
    s = amount_spec.map{|a| [a[:account], a[:amount]]}
    (a - s).empty? and (s - a).empty?
  end.all?
end
credits=(*attributes) click to toggle source
# File app/models/esa/transaction.rb, line 32
def credits=(*attributes)
  attributes.flatten.each do |attrs|
    attrs[:transaction] = self
    self.amounts << ESA::Amounts::Credit.new(attrs)
  end
end
debits=(*attributes) click to toggle source
# File app/models/esa/transaction.rb, line 39
def debits=(*attributes)
  attributes.flatten.each do |attrs|
    attrs[:transaction] = self
    self.amounts << ESA::Amounts::Debit.new(attrs)
  end
end
matches_spec?(spec) click to toggle source
# File app/models/esa/transaction.rb, line 55
def matches_spec?(spec)
  self.description == spec[:description] and self.amounts_match_spec?(spec)
end
spec() click to toggle source
# File app/models/esa/transaction.rb, line 46
def spec
  {
    :time => self.time,
    :description => self.description,
    :credits => self.amounts.credits.map{|a| {:account => a.account, :amount => a.amount}},
    :debits => self.amounts.debits.map{|a| {:account => a.account, :amount => a.amount}},
  }
end

Private Instance Methods

accounts_of_the_same_chart?() click to toggle source
# File app/models/esa/transaction.rb, line 86
def accounts_of_the_same_chart?
  if self.new_record?
    chart_ids = self.amounts.map{|a| if a.account.present? then a.account.chart_id else nil end}
  else
    chart_ids = self.accounts.pluck(:chart_id)
  end

  if not chart_ids.all? or chart_ids.uniq.count != 1
    errors[:base] << "Transaction must take place between accounts of the same Chart " + chart_ids.to_s
  end
end
amounts_cancel?() click to toggle source
# File app/models/esa/transaction.rb, line 98
def amounts_cancel?
  balance = self.amounts.iterated_balance
  errors[:base] << "The credit and debit amounts are not equal" if balance.nil? or balance != 0
end
has_credit_amounts?() click to toggle source
# File app/models/esa/transaction.rb, line 78
def has_credit_amounts?
  errors[:base] << "Transaction must have at least one credit amount" if self.amounts.find{|a| a.is_credit?}.nil?
end
has_debit_amounts?() click to toggle source
# File app/models/esa/transaction.rb, line 82
def has_debit_amounts?
  errors[:base] << "Transaction must have at least one debit amount" if self.amounts.find{|a| a.is_debit?}.nil?
end
initialize_defaults() click to toggle source
# File app/models/esa/transaction.rb, line 74
def initialize_defaults
  self.time ||= Time.zone.now
end