Class: CloverSandboxSimulator::Services::Clover::CashEventService
- Inherits:
-
BaseService
- Object
- BaseService
- CloverSandboxSimulator::Services::Clover::CashEventService
- Defined in:
- lib/clover_sandbox_simulator/services/clover/cash_event_service.rb
Overview
Manages Clover cash events (drawer operations, cash tracking)
Constant Summary collapse
- EVENT_TYPES =
Cash event types
%w[OPEN CLOSE ADD REMOVE PAY REFUND].freeze
Instance Attribute Summary
Attributes inherited from BaseService
Instance Method Summary collapse
-
#add_cash(employee_id:, amount:, note: nil) ⇒ Object
Add cash to drawer (cash drop/deposit).
-
#calculate_drawer_total(events = nil) ⇒ Object
Calculate expected drawer total from events.
-
#close_drawer(employee_id:, final_amount:) ⇒ Object
Close cash drawer (end of shift/day).
-
#create_cash_event(type:, amount:, employee_id:, note: nil) ⇒ Object
Create a cash event.
-
#get_cash_events(limit: 100) ⇒ Object
Fetch cash events.
-
#open_drawer(employee_id:, starting_cash: 20000) ⇒ Object
Open cash drawer (start of shift/day).
-
#record_cash_payment(employee_id:, amount:) ⇒ Object
Record a cash payment received.
-
#record_cash_refund(employee_id:, amount:) ⇒ Object
Record a cash refund given.
-
#remove_cash(employee_id:, amount:, note: nil) ⇒ Object
Remove cash from drawer (paid out, deposit).
Methods inherited from BaseService
Constructor Details
This class inherits a constructor from CloverSandboxSimulator::Services::BaseService
Instance Method Details
#add_cash(employee_id:, amount:, note: nil) ⇒ Object
Add cash to drawer (cash drop/deposit)
75 76 77 78 |
# File 'lib/clover_sandbox_simulator/services/clover/cash_event_service.rb', line 75 def add_cash(employee_id:, amount:, note: nil) logger.info "Adding $#{'%.2f' % (amount / 100.0)} to drawer" create_cash_event(type: "ADD", amount: amount, employee_id: employee_id, note: note || "Cash added") end |
#calculate_drawer_total(events = nil) ⇒ Object
Calculate expected drawer total from events
104 105 106 107 |
# File 'lib/clover_sandbox_simulator/services/clover/cash_event_service.rb', line 104 def calculate_drawer_total(events = nil) events ||= get_cash_events events.sum { |e| e["amountChange"] || 0 } end |
#close_drawer(employee_id:, final_amount:) ⇒ Object
Close cash drawer (end of shift/day)
66 67 68 69 |
# File 'lib/clover_sandbox_simulator/services/clover/cash_event_service.rb', line 66 def close_drawer(employee_id:, final_amount:) logger.info "Closing cash drawer with $#{'%.2f' % (final_amount / 100.0)}" create_cash_event(type: "CLOSE", amount: final_amount, employee_id: employee_id, note: "Drawer closed") end |
#create_cash_event(type:, amount:, employee_id:, note: nil) ⇒ Object
This may not work in sandbox environments where POST is not allowed
Create a cash event
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/clover_sandbox_simulator/services/clover/cash_event_service.rb', line 26 def create_cash_event(type:, amount:, employee_id:, note: nil) unless EVENT_TYPES.include?(type.upcase) raise ArgumentError, "Invalid cash event type: #{type}. Must be one of: #{EVENT_TYPES.join(', ')}" end logger.info "Creating cash event: #{type} for $#{'%.2f' % (amount / 100.0)}" payload = { "type" => type.upcase, "amountChange" => amount, "employee" => { "id" => employee_id }, "timestamp" => (Time.now.to_f * 1000).to_i } payload["note"] = note if note begin request(:post, endpoint("cash_events"), payload: payload) rescue ApiError => e if e..include?("405") logger.warn "Cash event creation not supported in this environment (405 Method Not Allowed)" logger.warn "Cash events may need to be created through the Clover device/dashboard" # Return a simulated response for tracking purposes { "type" => type.upcase, "amountChange" => amount, "simulated" => true } else raise end end end |
#get_cash_events(limit: 100) ⇒ Object
Fetch cash events
12 13 14 15 16 17 18 |
# File 'lib/clover_sandbox_simulator/services/clover/cash_event_service.rb', line 12 def get_cash_events(limit: 100) logger.info "Fetching cash events..." response = request(:get, endpoint("cash_events"), params: { limit: limit }) events = response&.dig("elements") || [] logger.info "Found #{events.size} cash events" events end |
#open_drawer(employee_id:, starting_cash: 20000) ⇒ Object
Open cash drawer (start of shift/day)
58 59 60 61 |
# File 'lib/clover_sandbox_simulator/services/clover/cash_event_service.rb', line 58 def open_drawer(employee_id:, starting_cash: 20000) # Default $200 starting cash logger.info "Opening cash drawer with $#{'%.2f' % (starting_cash / 100.0)}" create_cash_event(type: "OPEN", amount: starting_cash, employee_id: employee_id, note: "Drawer opened") end |
#record_cash_payment(employee_id:, amount:) ⇒ Object
Record a cash payment received
92 93 94 |
# File 'lib/clover_sandbox_simulator/services/clover/cash_event_service.rb', line 92 def record_cash_payment(employee_id:, amount:) create_cash_event(type: "PAY", amount: amount, employee_id: employee_id) end |
#record_cash_refund(employee_id:, amount:) ⇒ Object
Record a cash refund given
99 100 101 |
# File 'lib/clover_sandbox_simulator/services/clover/cash_event_service.rb', line 99 def record_cash_refund(employee_id:, amount:) create_cash_event(type: "REFUND", amount: -amount.abs, employee_id: employee_id) end |
#remove_cash(employee_id:, amount:, note: nil) ⇒ Object
Remove cash from drawer (paid out, deposit)
84 85 86 87 |
# File 'lib/clover_sandbox_simulator/services/clover/cash_event_service.rb', line 84 def remove_cash(employee_id:, amount:, note: nil) logger.info "Removing $#{'%.2f' % (amount / 100.0)} from drawer" create_cash_event(type: "REMOVE", amount: -amount.abs, employee_id: employee_id, note: note || "Cash removed") end |