Class: Twobook::Corrections::SimulatedDifferenceAdjustment
- Defined in:
- lib/twobook/corrections.rb
Instance Attribute Summary
Attributes inherited from Handler
#data_in_process, #event_in_process
Class Method Summary collapse
Instance Method Summary collapse
- #accounts(corrected_events:, account_snapshots:) ⇒ Object
- #adjust_original_account_balance(original, correct) ⇒ Object
- #adjust_original_account_data(original, correct) ⇒ Object
- #handle(account_snapshots:, corrected_events:) ⇒ Object
Methods inherited from Handler
from_name, handler_name, #initialize, #name, #run, types
Methods included from Handler::BookingHelpers
#add_account, #book, #credit, #debit, #entry, #record
Methods included from Handler::QueryHelpers
#account_requirements, #existing, #labelled_account_requirements, #many, #method_missing, #one, #respond_to_missing?, #satisfy_requirement, #where
Constructor Details
This class inherits a constructor from Twobook::Handler
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Twobook::Handler::QueryHelpers
Class Method Details
.simulate_correction(events, accounts) ⇒ Object
102 103 104 105 106 |
# File 'lib/twobook/corrections.rb', line 102 def self.simulate_correction(events, accounts) deserialized_events = events.map { |e| Serialization.deserialize_event(e) } deserialized_accounts = accounts.map { |a| Serialization.deserialize_account(a) } Twobook.simulate(deserialized_events, deserialized_accounts) end |
Instance Method Details
#accounts(corrected_events:, account_snapshots:) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/twobook/corrections.rb', line 81 def accounts(corrected_events:, account_snapshots:) corrected_accounts = self.class.simulate_correction(corrected_events, account_snapshots) requirements = corrected_accounts.map do |account| query = AccountQuery.where( category: account.class.category, **account.data.slice(*account.class.name_includes), ) existing(query) end labelled_requirements = (0...requirements.count).map do |n| "requirement_#{n}_account".to_sym end.zip(requirements).to_h { buffer_account: one(where(category: 'twobook/corrections/correction_buffer')), **labelled_requirements, } end |
#adjust_original_account_balance(original, correct) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/twobook/corrections.rb', line 60 def adjust_original_account_balance(original, correct) correct_balance = correct.balance || Twobook.wrap_number(0) original_balance = original.balance || Twobook.wrap_number(0) diff = correct_balance - original_balance return if diff.zero? if original.class.account_type == :records record original, amount: diff else diff *= -1 if %i(revenue liabilities).include?(original.class.account_type) book diff, cr: buffer_account, dr: original if diff.positive? book (-1 * diff), cr: original, dr: buffer_account if diff.negative? end end |
#adjust_original_account_data(original, correct) ⇒ Object
75 76 77 78 79 |
# File 'lib/twobook/corrections.rb', line 75 def adjust_original_account_data(original, correct) diff = correct.data.to_a - original.data.to_a return if diff.empty? original << entry(0, data: diff.to_h) end |
#handle(account_snapshots:, corrected_events:) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/twobook/corrections.rb', line 50 def handle(account_snapshots:, corrected_events:) correct_accounts = self.class.simulate_correction(corrected_events, account_snapshots) correct_accounts.each do |correct| original = where(name: correct.name).on(@accounts_in_process).first adjust_original_account_balance(original, correct) adjust_original_account_data(original, correct) end end |