Class: SpreeCmCommissioner::Stock::OrderAvailabilityChecker

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree_cm_commissioner/stock/order_availability_checker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ OrderAvailabilityChecker

Returns a new instance of OrderAvailabilityChecker.



8
9
10
# File 'app/models/spree_cm_commissioner/stock/order_availability_checker.rb', line 8

def initialize(order)
  @order = order
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order.



6
7
8
# File 'app/models/spree_cm_commissioner/stock/order_availability_checker.rb', line 6

def order
  @order
end

Instance Method Details

#cached_inventory_items_group_by_line_item_idObject

1: [ {inventory_key: "inventory:1", active: true, quantity_available: 5, inventory_item_id: 1, variant_id: 1],
2: [ "inventory:2", active: true, quantity_available: 5, inventory_item_id: 2, variant_id: 2],

}



27
28
29
30
31
32
# File 'app/models/spree_cm_commissioner/stock/order_availability_checker.rb', line 27

def 
   ||=
    ::SpreeCmCommissioner::RedisStock::LineItemsCachedInventoryItemsBuilder.new(
      line_item_ids: order.line_items.pluck(:id)
    ).call
end

#can_supply_all?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'app/models/spree_cm_commissioner/stock/order_availability_checker.rb', line 12

def can_supply_all?
  insufficient_stock_lines.empty?
end

#insufficient_stock_linesObject



16
17
18
19
20
21
# File 'app/models/spree_cm_commissioner/stock/order_availability_checker.rb', line 16

def insufficient_stock_lines
  .map do |line_item_id, cached_inventory_items|
    line_item = order.line_items.find { |item| item.id == line_item_id }
    line_item unless sufficient_stock_for?(line_item, cached_inventory_items)
  end.compact
end

#sufficient_stock_for?(line_item, cached_inventory_items) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
# File 'app/models/spree_cm_commissioner/stock/order_availability_checker.rb', line 34

def sufficient_stock_for?(line_item, cached_inventory_items)
  return false unless line_item.variant.available?
  return true unless line_item.variant.should_track_inventory?
  return true if line_item.variant.backorderable?
  return true if line_item.variant.need_confirmation?

  cached_inventory_items.all? { |item| item.active? && item.quantity_available >= line_item.quantity }
end