Class: ESA::Transaction

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ESA::Traits::Extendable
Defined in:
app/models/esa/transaction.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#amounts_match_spec?(spec) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
70
# 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., a.amount]}
    s = amount_spec.map{|a| [a[:account], a[:amount]]}
    (a - s).empty? and (s - a).empty?
  end.all?
end

#credits=(*attributes) ⇒ Object



32
33
34
35
36
37
# 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) ⇒ Object



39
40
41
42
43
44
# 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) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/models/esa/transaction.rb', line 55

def matches_spec?(spec)
  self.description == spec[:description] and self.amounts_match_spec?(spec)
end

#specObject



46
47
48
49
50
51
52
53
# File 'app/models/esa/transaction.rb', line 46

def spec
  {
    :time => self.time,
    :description => self.description,
    :credits => self.amounts.credits.map{|a| {:account => a., :amount => a.amount}},
    :debits => self.amounts.debits.map{|a| {:account => a., :amount => a.amount}},
  }
end