Class: ActsAsAccount::Journal

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/acts_as_account/journal.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear_currentObject



17
18
19
# File 'lib/acts_as_account/journal.rb', line 17

def clear_current
  Thread.current[:acts_as_account_current] = nil
end

.currentObject



13
14
15
# File 'lib/acts_as_account/journal.rb', line 13

def current
  Thread.current[:acts_as_account_current] ||= create!
end

Instance Method Details

#transfer(amount, from_account, to_account, reference = nil, valuta = Time.now) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/acts_as_account/journal.rb', line 26

def transfer(amount, , , reference = nil, valuta = Time.now)
  transaction do
    if (amount < 0)
      # change order if amount is negative
      amount, ,  = -amount, , 
    end

    if logger
      logger.debug(
        [
          "ActsAsAccount::Journal.transfer",
          "amount: #{amount}",
          "from: #{.id}",
          "to: #{.id}",
          "reference: #{reference.class.name}(#{reference&.id})",
          "valuta: #{valuta}",
        ].join(' ')
      )
    end

    # To avoid possible deadlocks we need to ensure that the locking order
    # is always the same therfore the sort by id.
    if ActsAsAccount.configuration.
      [, ].sort_by(&:id).each(&:lock!)
    end

    posting1 = build_posting(-amount,  ,   , reference, valuta)
    posting2 = build_posting( amount,    , , reference, valuta)

    result = postings.model.insert_all([ posting1.attributes.compact, posting2.attributes.compact ])

    update_attributes_on(, -amount)
    update_attributes_on(,    amount)

    !!result
  end
end

#transfersObject



22
23
24
# File 'lib/acts_as_account/journal.rb', line 22

def transfers
  postings.each_slice(2).map { |postings| Transfer.new(*postings) }
end