Class: Spree::Admin::StockManagementsController

Inherits:
ResourceController
  • Object
show all
Defined in:
app/controllers/spree/admin/stock_managements_controller.rb

Direct Known Subclasses

InventoryItemsController

Instance Method Summary collapse

Instance Method Details

#calendarObject

GET /products/:slug/stock_managements/calendar?year=&selected_variant_id=



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/spree/admin/stock_managements_controller.rb', line 44

def calendar
  @year = params[:year].present? ? params[:year].to_i : Time.zone.today.year

  from_date = Date.new(@year, 1, 1).beginning_of_year
  to_date = Date.new(@year, 1, 1).end_of_year

  @variants = @product.variants
  @inventory_items = if params[:selected_variant_id].present?
                       @product.inventory_items.includes(:variant, :prices).where(
                         inventory_date: from_date..to_date,
                         variant_id: params[:selected_variant_id]
                       ).to_a
                     else
                       @product.inventory_items.includes(:variant, :prices).where(
                         inventory_date: from_date..to_date
                       ).to_a
                     end

  @cached_inventory_items = ::SpreeCmCommissioner::RedisStock::CachedInventoryItemsBuilder.new(@inventory_items)
                                                                                          .call
                                                                                          .index_by(&:inventory_item_id)
  @events = SpreeCmCommissioner::CalendarEvent.from_inventory_items(@inventory_items)
end

#createObject



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

def create
  result = SpreeCmCommissioner::Stock::StockMovementCreator.call(
    variant_id: params[:variant_id],
    stock_location_id: params[:stock_location_id],
    current_store: current_store,
    stock_movement_params: stock_movement_params
  )

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

  redirect_back fallback_location: admin_product_stock_managements_path(@product)
end

#indexObject



14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/spree/admin/stock_managements_controller.rb', line 14

def index
  @variants = @product.variants.includes(:images, :default_price, stock_items: :stock_location, option_values: :option_type)
  @variants = [@product.master] if @variants.empty?

  vendor_stock_locations = @product.vendor&.stock_locations || []
  variant_stock_locations = @variants.flat_map(&:stock_locations).uniq

  @stock_locations = (variant_stock_locations + vendor_stock_locations).uniq

  load_inventories unless @product.permanent_stock?
end

#inventory_item_message(inventory_item, cached_inventory_item) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'app/controllers/spree/admin/stock_managements_controller.rb', line 68

def inventory_item_message(inventory_item, cached_inventory_item)
  synced = inventory_item.quantity_available == cached_inventory_item.quantity_available

  if synced
    "Synced: Quantity available matches in both DB and Redis (#{cached_inventory_item.quantity_available})."
  else
    "Out of sync: Redis shows #{cached_inventory_item.quantity_available} available, which doesn't match the database."
  end
end

#load_parentObject



10
11
12
# File 'app/controllers/spree/admin/stock_managements_controller.rb', line 10

def load_parent
  @product = Spree::Product.find_by(slug: params[:product_id])
end