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, #index, #inventory_item_message, #load_parent

Instance Method Details

#createObject

POST /products/:slug/variants/:variant_id/inventory_items



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 22

def create
  variant = @product.variants.find(params[:variant_id])
  inventory_item = variant.create_default_non_permanent_inventory_item!

  if inventory_item.present?
    result = SpreeCmCommissioner::Stock::InventoryItemResetter.call(inventory_item: inventory_item)

    if result.success?
      flash[:success] = flash_message_for(result.inventory_item, :successfully_created)
    else
      flash[:error] = result.message
    end
  end

  redirect_back fallback_location: admin_product_stock_managements_path(@product)
end

#delete_pricesObject

PATCH /products/:slug/variants/:variant_id/inventory_items/:id/delete_prices



70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/spree/admin/inventory_items_controller.rb', line 70

def delete_prices
  inventory_item = @product.inventory_items.find(params[:id])

  if inventory_item.prices.destroy_all
    flash[:success] = flash_message_for(inventory_item, :successfully_removed)
  else
    flash[:error] = inventory_item.errors.full_messages.join(', ')
  end

  redirect_back fallback_location: admin_product_stock_managements_path(@product)
end

#resetObject

PATCH /products/:slug/variants/:variant_id/inventory_items/:id/reset



40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/spree/admin/inventory_items_controller.rb', line 40

def reset
  inventory_item = @product.inventory_items.find(params[:id])
  result = SpreeCmCommissioner::Stock::InventoryItemResetter.call(inventory_item: inventory_item)

  if result.success?
    flash[:success] = flash_message_for(result.inventory_item, :successfully_updated)
  else
    flash[:error] = result.message
  end

  redirect_back fallback_location: admin_product_stock_managements_path(@product)
end

#showObject

GET /products/:slug/variants/:variant_id/inventory_items/:id



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/spree/admin/inventory_items_controller.rb', line 7

def show
  @inventory_item = @product.inventory_items.find(params[:id])
  @default_prices = @inventory_item.variant.prices.to_a.index_by(&:currency)

  @prices = @inventory_item.prices.to_a
  supported_currencies_for_all_stores.each do |currency|
    @prices << @inventory_item.prices.new(currency: currency) if @prices.none? { |price| price.currency == currency }
  end

  @cached_inventory_item = ::SpreeCmCommissioner::RedisStock::CachedInventoryItemsBuilder.new([@inventory_item])
                                                                                         .call
                                                                                         .index_by(&:inventory_item_id)[@inventory_item.id]
end

#update_pricesObject

PATCH /products/:slug/variants/:variant_id/inventory_items/:id/update_prices



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/spree/admin/inventory_items_controller.rb', line 54

def update_prices
  inventory_item = @product.inventory_items.find(params[:id])

  params[:prices]&.values&.each do |price_params|
    price = inventory_item.prices.find_or_initialize_by(currency: price_params[:currency])
    price.price = price_params[:price].empty? ? nil : 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

  flash[:success] = flash_message_for(inventory_item, :successfully_updated)
  redirect_back fallback_location: admin_product_stock_managements_path(@product)
end