Class: Spree::Admin::InventoryItemsController

Inherits:
StockManagementsController show all
Defined in:
app/controllers/spree/admin/inventory_items_controller.rb

Instance Method Summary collapse

Methods inherited from StockManagementsController

#calendar, #create, #index, #inventory_item_message, #load_parent

Instance Method Details

#bulk_resetObject

PATCH /products/:slug/inventory_items/bulk_reset?inventory_item_ids=1,2,3



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/spree/admin/inventory_items_controller.rb', line 63

def bulk_reset
  inventory_item_ids = params[:inventory_item_ids]
  inventory_items = @product.inventory_items.where(id: inventory_item_ids)

  errors = []
  inventory_items.each do |inventory_item|
    result = SpreeCmCommissioner::InventoryItems::Reset.call(inventory_item: inventory_item)
    errors << result.error.to_s unless result.success?
  end

  if errors.empty?
    flash[:success] = "Successfully reset inventory items (#{inventory_items.count})"
  else
    flash[:error] = errors.join(', ')
  end

  redirect_back fallback_location: admin_product_stock_managements_path(@product)
end

#bulk_update_pricesObject

PATCH /products/:slug/inventory_items/bulk_update_prices



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/spree/admin/inventory_items_controller.rb', line 19

def bulk_update_prices # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  inventory_item_ids = params[:inventory_item_ids]
  inventory_items = @product.inventory_items.where(id: inventory_item_ids)

  inventory_items.each do |inventory_item|
    params[:prices]&.values&.each do |price_params|
      next if price_params[:price].empty?

      price = inventory_item.prices.find_or_initialize_by(currency: price_params[:currency])
      price.price = price_params[:price]
      price.compare_at_price = price_params[:compare_at_price].empty? ? nil : price_params[:compare_at_price]
      price.variant_id = inventory_item.variant_id
      price.save! if (price.new_record? && price.price) || (!price.new_record? && price.changed?)
    end
  end

  flash[:success] = "Successfully updated prices for #{inventory_items.count} items"
  redirect_back fallback_location: admin_product_stock_managements_path(@product)
end

#bulk_update_stocksObject

PATCH /products/:slug/inventory_items/bulk_update_stocks



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/spree/admin/inventory_items_controller.rb', line 40

def bulk_update_stocks
  inventory_item_ids = params[:inventory_item_ids]
  inventory_items = @product.inventory_items.where(id: inventory_item_ids)
  target_quantity = params[:quantity].to_i

  if target_quantity.negative?
    flash[:error] = "Target quantity (#{target_quantity}) must not be negative"
    return redirect_back(fallback_location: admin_product_stock_managements_path(@product))
  end

  inventory_items.each do |inventory_item|
    # Calculate the difference between target and current quantity
    quantity_change = target_quantity - inventory_item.quantity_available

    # Use adjust_quantity! to update max_capacity, quantity_available, and sync to Redis
    inventory_item.adjust_quantity!(quantity_change)
  end

  flash[:success] = "Successfully updated stocks for #{inventory_items.count} items"
  redirect_back fallback_location: admin_product_stock_managements_path(@product)
end

#pricesObject

GET /products/:slug/inventory_items/prices?inventory_item_ids=1,2,3



7
8
9
10
# File 'app/controllers/spree/admin/inventory_items_controller.rb', line 7

def prices
  inventory_item_ids = params[:inventory_item_ids]
  @inventory_items = @product.inventory_items.where(id: inventory_item_ids)
end

#stocksObject

GET /products/:slug/inventory_items/stocks?inventory_item_ids=1,2,3



13
14
15
16
# File 'app/controllers/spree/admin/inventory_items_controller.rb', line 13

def stocks
  inventory_item_ids = params[:inventory_item_ids]
  @inventory_items = @product.inventory_items.where(id: inventory_item_ids)
end