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=



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

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

  @inventory_items = @product.inventory_items.includes(:variant, :prices).where(inventory_date: from_date..to_date).to_a
  @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



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

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
# File 'app/controllers/spree/admin/stock_managements_controller.rb', line 14

def index
  @variants = @product.variants.includes(:images, stock_items: :stock_location, option_values: :option_type)
  @variants = [@product.master] if @variants.empty?
  @stock_locations = (@variants.flat_map(&:stock_locations) + @product.vendor.stock_locations).uniq

  load_inventories unless @product.permanent_stock?
end

#inventory_item_message(inventory_item, cached_inventory_item) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'app/controllers/spree/admin/stock_managements_controller.rb', line 53

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