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
- #add_account(account) ⇒ Object
- #book(amount, debit: nil, dr: nil, credit: nil, cr: nil) ⇒ Object
- #credit(amount, account, **opts) ⇒ Object
- #debit(amount, account, **opts) ⇒ Object
- #entry(amount, transaction_id: nil, data: {}) ⇒ Object
- #record(account, amount: 0, **data) ⇒ Object
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 add_account(account) raise 'Cannot add accounts: not currently processing accounts' if @accounts_in_process.nil? if @accounts_in_process.include?(account) raise "Cannot add account #{account.name}: was already processing one with the same name." end @accounts_in_process << account account 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, account, **opts) case account.class.account_type when :assets account << entry(-amount, **opts) when :liabilities account << entry(amount, **opts) when :revenue account << entry(amount, **opts) when :expenses account << entry(-amount, **opts) else raise "Invalid account type #{account.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, account, **opts) case account.class.account_type when :assets account << entry(amount, **opts) when :liabilities account << entry(-amount, **opts) when :revenue account << entry(-amount, **opts) when :expenses account << entry(amount, **opts) else raise "Invalid account type #{account.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(account, amount: 0, **data) account << entry(amount, data: data) end |