Class: CloverSandboxSimulator::Services::Clover::CashEventService

Inherits:
BaseService
  • Object
show all
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

#config, #logger

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

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)

Parameters:

  • employee_id (String)

    Employee ID

  • amount (Integer)

    Amount in cents

  • note (String, nil) (defaults to: nil)

    Reason for adding cash



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)

Parameters:

  • employee_id (String)

    Employee ID

  • final_amount (Integer)

    Final cash amount in cents



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

Note:

This may not work in sandbox environments where POST is not allowed

Create a cash event

Parameters:

  • type (String)

    Event type (OPEN, CLOSE, ADD, REMOVE, PAY, REFUND)

  • amount (Integer)

    Amount in cents

  • employee_id (String)

    Employee ID

  • note (String, nil) (defaults to: nil)

    Optional note



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.message.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)

Parameters:

  • employee_id (String)

    Employee ID

  • starting_cash (Integer) (defaults to: 20000)

    Starting cash amount in cents



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

Parameters:

  • employee_id (String)

    Employee ID

  • amount (Integer)

    Payment amount in cents



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

Parameters:

  • employee_id (String)

    Employee ID

  • amount (Integer)

    Refund amount in cents



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)

Parameters:

  • employee_id (String)

    Employee ID

  • amount (Integer)

    Amount in cents

  • note (String, nil) (defaults to: nil)

    Reason for removing cash



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