Module: Twobook::Handler::BookingHelpers

Included in:
Twobook::Handler
Defined in:
lib/twobook/handler/booking_helpers.rb

Overview

Mixin for Handler with some booking shorthand. Expects @account_in_process and @event_in_process to be set.

Instance Method Summary collapse

Instance Method Details

#add_account(account) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/twobook/handler/booking_helpers.rb', line 56

def ()
  raise 'Cannot add accounts: not currently processing accounts' if @accounts_in_process.nil?
  if @accounts_in_process.include?()
    raise "Cannot add account #{.name}: was already processing one with the same name."
  end

  @accounts_in_process << 
  
end

#book(amount, debit: nil, dr: nil, credit: nil, cr: nil) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/twobook/handler/booking_helpers.rb', line 17

def book(amount, debit: nil, dr: nil, credit: nil, cr: nil)
  to_debit = debit || dr
  to_credit = credit || cr
  raise 'Must credit one account and debit one account' unless to_debit && to_credit
  transaction_id = SecureRandom.uuid
  debit amount, to_debit, transaction_id: transaction_id
  credit amount, to_credit, transaction_id: transaction_id
end

#credit(amount, account, **opts) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/twobook/handler/booking_helpers.rb', line 41

def credit(amount, , **opts)
  case .class.
  when :assets
     << entry(-amount, **opts)
  when :liabilities
     << entry(amount, **opts)
  when :revenue
     << entry(amount, **opts)
  when :expenses
     << entry(-amount, **opts)
  else
    raise "Invalid account type #{.}"
  end
end

#debit(amount, account, **opts) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/twobook/handler/booking_helpers.rb', line 26

def debit(amount, , **opts)
  case .class.
  when :assets
     << entry(amount, **opts)
  when :liabilities
     << entry(-amount, **opts)
  when :revenue
     << entry(-amount, **opts)
  when :expenses
     << entry(amount, **opts)
  else
    raise "Invalid account type #{.}"
  end
end

#entry(amount, transaction_id: nil, data: {}) ⇒ Object



6
7
8
9
10
11
# File 'lib/twobook/handler/booking_helpers.rb', line 6

def entry(amount, transaction_id: nil, data: {})
  raise 'Cannot create entry - not currently processing an event' if @event_in_process.blank?
  new_entry = Entry.new(amount, @event_in_process, transaction_id: transaction_id, data: data)
  @event_in_process.entries << new_entry
  new_entry
end

#record(account, amount: 0, **data) ⇒ Object



13
14
15
# File 'lib/twobook/handler/booking_helpers.rb', line 13

def record(, amount: 0, **data)
   << entry(amount, data: data)
end