Class: Skr::GlTransaction
- Defined in:
- lib/skr/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.gl_asset_account,
credit: customer.gl_receivables_account )
end
end
Class Method Summary collapse
-
.current ⇒ GlTransaction
The current transaction that’s in progress.
- .push_or_save(owner: nil, amount: nil, debit: nil, credit: nil, options: {}) ⇒ Object
-
.record(attributes = {}) {|GlTransaction| ... } ⇒ GlTransaction
Start a new nested GlTransaction When a transaction is created, it can have.
Instance Method Summary collapse
- #_save_recorded ⇒ Object
-
#add_posting(amount: nil, debit: nil, credit: nil) ⇒ Object
Add a debit/credit pair to the transaction with amount.
- #each_posting {|GlPosting| ... } ⇒ Object
-
#location=(location) ⇒ Object
Passes the location onto the postings.
Class Method Details
.current ⇒ GlTransaction
Returns the current transaction that’s in progress.
71 72 73 74 |
# File 'lib/skr/gl_transaction.rb', line 71 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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/skr/gl_transaction.rb', line 102 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 .merge!({ source: owner, location: [:location] || owner.location }) glt = GlTransaction.new( ) 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
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/skr/gl_transaction.rb', line 80 def self.record( attributes={} ) Thread.current[:gl_transaction] ||= [] glt = GlTransaction.new( attributes ) Thread.current[:gl_transaction].push( glt ) Skr::Core.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 Skr::Core.logger.debug "AF GlTransaction new=#{glt.new_record?} #{glt.errors.}" end return glt end |
Instance Method Details
#_save_recorded ⇒ Object
118 119 120 121 122 123 |
# File 'lib/skr/gl_transaction.rb', line 118 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
47 48 49 50 51 52 53 |
# File 'lib/skr/gl_transaction.rb', line 47 def add_posting( amount: nil, debit: nil, credit: nil ) Skr::Core.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
65 66 67 68 |
# File 'lib/skr/gl_transaction.rb', line 65 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.
57 58 59 60 61 62 |
# File 'lib/skr/gl_transaction.rb', line 57 def location=(location) @location = location each_posting do | posting | posting.location = location end end |