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_option_value_ids=



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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

  @variant_kind_option_types = Spree::Variants::OptionTypesFinder.new(variant_ids: @product.variants.ids)
                                                                 .execute.select(&:variant?)
  @selected_option_value_ids = Array(params[:selected_option_value_ids]).map(&:to_i)

  inventory_items = @product.inventory_items.includes(:variant, :prices).where(
    inventory_date: from_date..to_date
  )

  if @selected_option_value_ids.present?
    variant_ids = SpreeCmCommissioner::Variants::FindByOptionValueIds.new(
      product: @product, option_value_ids: @selected_option_value_ids
    ).execute.ids
    inventory_items = inventory_items.where(variant_id: variant_ids)
    @matched_variants_count = variant_ids.size
  else
    @matched_variants_count = @product.variants.count
  end

  @inventory_items = inventory_items.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



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_hold_message(inventory_item, cached_inventory_item) ⇒ Object



86
87
88
89
90
91
92
93
# File 'app/controllers/spree/admin/stock_managements_controller.rb', line 86

def inventory_item_hold_message(inventory_item, cached_inventory_item)
  synced = inventory_item.quantity_on_hold == cached_inventory_item.quantity_on_hold
  if synced
    "Synced: Quantity on hold matches in both DB and Redis (#{cached_inventory_item.quantity_on_hold})."
  else
    "Out of sync: Redis shows #{cached_inventory_item.quantity_on_hold} on hold, which doesn't match the database."
  end
end

#inventory_item_message(inventory_item, cached_inventory_item) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'app/controllers/spree/admin/stock_managements_controller.rb', line 76

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