Class: Skr::GlTransaction

Inherits:
Model
  • Object
show all
Defined in:
lib/skr/models/gl_transaction.rb

Overview

A transaction is a record of a business event that has financial consequences. It consists of an at least one credit and at least one debit Transactions can be nested, with each level compacting all the entries that were made on it

require 'skr/core'
customer = Customer.find_by_code "MONEYBAGS"
GlTransaction.record( source: invoice, description: "Invoice Example" ) do | transaction |
  transaction.location = Location.default # <- could also specify in record's options
  Sku.where( code: ['HAT','STRING'] ).each do | sku |
      transaction.add_posting( amount: sku.default_price,
                                debit:  sku.,
                               credit: customer. )
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.currentGlTransaction

Returns the current transaction that’s in progress.

Returns:

  • (GlTransaction)

    the current transaction that’s in progress



80
81
82
83
# File 'lib/skr/models/gl_transaction.rb', line 80

def self.current
    glt = Thread.current[:gl_transaction]
    glt ? glt.last : nil
end

.push_or_save(owner: nil, amount: nil, debit: nil, credit: nil, options: {}) ⇒ Object

Parameters:

  • owner (Skr::Model) (defaults to: nil)
  • amount (BigDecimal) (defaults to: nil)
  • debit (GlAccount) (defaults to: nil)
  • credit (GlAccount) (defaults to: nil)
  • options (Hash) (defaults to: {})

    options to pass to the [GlTransaction] if one is created



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/skr/models/gl_transaction.rb', line 111

def self.push_or_save( owner: nil, amount: nil, debit:nil, credit:nil, options:{} )
    if (glt = self.current) # we push
        glt.add_posting( amount: amount, debit: debit, credit: credit )
    else
        options.merge!(source: owner, location: options[:location] || owner.location)
        glt = GlTransaction.new( options )
        glt.add_posting( amount: amount, debit: debit, credit: credit )
        glt.save!
    end
    glt
end

.record(attributes = {}) {|GlTransaction| ... } ⇒ GlTransaction

Start a new nested GlTransaction When a transaction is created, it can have

Yields:

Returns:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/skr/models/gl_transaction.rb', line 89

def self.record( attributes={} )
    Thread.current[:gl_transaction] ||= []
    glt = GlTransaction.new( attributes )
    Thread.current[:gl_transaction].push( glt )
    Lanes.logger.debug "B4 GlTransaction"
    results = yield glt
    Thread.current[:gl_transaction].pop
    if results
        if results.is_a?(Hash) && results.has_key?(:attributes)
            glt.assign_attributes( results[:attributes] )
        end
        glt._save_recorded
        Lanes.logger.debug "AF GlTransaction new=#{glt.new_record?} #{glt.errors.full_messages}"
    end
    return glt
end

Instance Method Details

#_save_recordedObject



124
125
126
127
128
129
# File 'lib/skr/models/gl_transaction.rb', line 124

def _save_recorded
    compact( 'debits'  )
    compact( 'credits' )
    self.save if self.credits.any? || self.debits.any?
    self
end

#add_posting(amount: nil, debit: nil, credit: nil) ⇒ Object

Add a debit/credit pair to the transaction with amount

Parameters:

  • amount (BigDecimal) (defaults to: nil)

    the amount to apply to each posting

  • debit (GlAccount) (defaults to: nil)
  • credit (GlAccount) (defaults to: nil)


56
57
58
59
60
61
62
# File 'lib/skr/models/gl_transaction.rb', line 56

def add_posting( amount: nil, debit: nil, credit: nil )
    Lanes.logger.debug "GlTransaction add_posting #{debit} : #{credit}"
    self.credits.build( location: @location, is_debit: false,
                        account: credit, amount: amount )
    self.debits.build(  location: @location, is_debit: true,
                        account: debit,  amount: amount * -1 )
end

#each_posting {|GlPosting| ... } ⇒ Object

Yields:

  • (GlPosting)

    each posting associated with the Transaction



74
75
76
77
# File 'lib/skr/models/gl_transaction.rb', line 74

def each_posting
    self.credits.each{ |posting| yield posting }
    self.debits.each{  |posting| yield posting }
end

#location=(location) ⇒ Object

Passes the location onto the postings.

Parameters:



66
67
68
69
70
71
# File 'lib/skr/models/gl_transaction.rb', line 66

def location=(location)
    @location = location
    each_posting do | posting |
        posting.location = location
    end
end