Class: Spree::Admin::InventoryItemsController
- Inherits:
-
StockManagementsController
- Object
- ResourceController
- StockManagementsController
- Spree::Admin::InventoryItemsController
- Defined in:
- app/controllers/spree/admin/inventory_items_controller.rb
Instance Method Summary collapse
-
#bulk_update_prices ⇒ Object
PATCH /products/:slug/inventory_items/bulk_update_prices.
-
#bulk_update_stocks ⇒ Object
PATCH /products/:slug/inventory_items/bulk_update_stocks.
-
#prices ⇒ Object
GET /products/:slug/inventory_items/prices?inventory_ids=1,2,3.
-
#stocks ⇒ Object
GET /products/:slug/inventory_items/stocks?inventory_ids=1,2,3.
Methods inherited from StockManagementsController
#calendar, #create, #index, #inventory_item_message, #load_parent
Instance Method Details
#bulk_update_prices ⇒ Object
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_ids = params[:inventory_ids].to_s.split(',').map(&:to_i) inventory_items = @product.inventory_items.where(id: inventory_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_stocks ⇒ Object
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_ids = params[:inventory_ids].to_s.split(',').map(&:to_i) inventory_items = @product.inventory_items.where(id: inventory_ids) target_quantity = params[:quantity].to_i if target_quantity.zero? flash[:error] = "Target quantity (#{target_quantity}) must be greater than zero" 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) if quantity_change != 0 end flash[:success] = "Successfully updated stocks for #{inventory_items.count} items" redirect_back fallback_location: admin_product_stock_managements_path(@product) end |
#prices ⇒ Object
GET /products/:slug/inventory_items/prices?inventory_ids=1,2,3
7 8 9 10 |
# File 'app/controllers/spree/admin/inventory_items_controller.rb', line 7 def prices inventory_ids = params[:inventory_ids].to_s.split(',').map(&:to_i) @inventory_items = @product.inventory_items.where(id: inventory_ids) end |
#stocks ⇒ Object
GET /products/:slug/inventory_items/stocks?inventory_ids=1,2,3
13 14 15 16 |
# File 'app/controllers/spree/admin/inventory_items_controller.rb', line 13 def stocks inventory_ids = params[:inventory_ids].to_s.split(',').map(&:to_i) @inventory_items = @product.inventory_items.where(id: inventory_ids) end |