Class: DoubleDouble::Transaction

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/double_double/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.

Posting to a Ledger can be considered to happen automatically, since Accounts have the reverse ‘has_many’ relationship to either it’s credit or debit transactions

Examples:

cash = DoubleDouble::Asset.named('Cash')
accounts_receivable = DoubleDouble::Asset.named('Accounts Receivable')

debit_amount = DoubleDouble::DebitAmount.new(account: 'cash', amount: 1000)
credit_amount = DoubleDouble::CreditAmount.new(account: 'accounts_receivable', amount: 1000)

transaction = DoubleDouble::Transaction.new(description: "Receiving payment on an invoice")
transaction.debit_amounts << debit_amount
transaction.credit_amounts << credit_amount
transaction.save

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(args) ⇒ DoubleDouble::Transaction

Simple API for building a transaction and associated debit and credit amounts

Examples:

transaction = DoubleDouble::Transaction.build(
  description: "Sold some widgets",
  debits: [
    {account: "Accounts Receivable", amount: 50, context: @some_active_record_object}], 
  credits: [
    {account: "Sales Revenue",       amount: 45},
    {account: "Sales Tax Payable",   amount:  5}])

Returns:



56
57
58
59
60
61
62
63
64
65
# File 'lib/double_double/transaction.rb', line 56

def self.build args
  args.merge!({credits: args[:debits], debits: args[:credits]}) if args[:reversed]
  t = Transaction.new()
  t.description      = args[:description]
  t.transaction_type = args[:transaction_type] if args.has_key? :transaction_type
  t.initiator        = args[:initiator]        if args.has_key? :initiator
  add_amounts_to_transaction(args[:debits],  t, true) 
  add_amounts_to_transaction(args[:credits], t, false)
  t
end

.create!(args) ⇒ Object



67
68
69
70
# File 'lib/double_double/transaction.rb', line 67

def self.create! args
  t = build args
  t.save!
end

Instance Method Details

#transaction_typeObject



72
73
74
# File 'lib/double_double/transaction.rb', line 72

def transaction_type
  self.transaction_type_id.nil? ? UnassignedTransactionType : TransactionType.find(self.transaction_type_id)
end