Class: Spree::Admin::InventoryHoldsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/spree/admin/inventory_holds_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/spree/admin/inventory_holds_controller.rb', line 4

def index
  authorize! :manage, SpreeCmCommissioner::InventoryHold

  q = params.fetch(:q, {})
  q = q.respond_to?(:to_unsafe_h) ? q.to_unsafe_h.deep_dup : q.deep_dup
  q['s'] ||= 'created_at desc'
  q['created_at_gt'] = parse_time_or_nil(q['created_at_gt'], :beginning_of_day) if q['created_at_gt'].present?
  q['created_at_lt'] = parse_time_or_nil(q['created_at_lt'], :end_of_day) if q['created_at_lt'].present?

  @search = scope.ransack(q)
  result_scope = @search.result(distinct: true)

  @holds = result_scope
           .includes(order: [:user, { line_items: { variant: { product: :vendor } } }])
           .page(params[:page])
           .per(params[:per_page] || Spree::Backend::Config[:admin_orders_per_page])

  @status_counts = result_scope.reorder(nil).group(:status).count
end

#releaseObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/spree/admin/inventory_holds_controller.rb', line 24

def release
  authorize! :manage, SpreeCmCommissioner::InventoryHold

  hold = SpreeCmCommissioner::InventoryHold.find(params[:id])

  if hold.finalized?
    flash[:error] = "Hold ##{hold.id} is already #{hold.status} and cannot be released."
    return redirect_to admin_inventory_holds_path(back_params)
  end

  result = SpreeCmCommissioner::InventoryHolds::Release.call(hold: hold, reason: :user_canceled)

  if result.success?
    flash[:success] = "Hold ##{hold.id} for order #{hold.order.number} has been released."
  else
    flash[:error] = "Failed to release hold ##{hold.id}: #{result.error}"
  end

  redirect_to admin_inventory_holds_path(back_params)
end