Class: SpreeCmCommissioner::Stock::AvailabilityChecker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variant, options = {}) ⇒ AvailabilityChecker

options checks the operator's withheld allotment instead of public stock — set for a line item whose LineItem#locked_stock? is true.



8
9
10
11
12
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 8

def initialize(variant, options = {})
  @variant = variant
  @options = options
  @error_message = nil
end

Instance Attribute Details

#error_messageObject (readonly)

Returns the value of attribute error_message.



4
5
6
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 4

def error_message
  @error_message
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 4

def options
  @options
end

#variantObject (readonly)

Returns the value of attribute variant.



4
5
6
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 4

def variant
  @variant
end

Instance Method Details

#builder_klassObject



52
53
54
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 52

def builder_klass
  ::SpreeCmCommissioner::RedisStock::VariantCachedInventoryItemsBuilder
end

#cached_inventory_itemsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 37

def cached_inventory_items
  return @cached_inventory_items if defined?(@cached_inventory_items)

  if variant.permanent_stock?
    return [] if options[:from_date].blank? || options[:to_date].blank?

    dates = options[:from_date].to_date..options[:to_date].to_date
    @cached_inventory_items = builder_klass.new(variant_id: variant.id, dates: dates).call
    @cached_inventory_items = [] if @cached_inventory_items.size != dates.count
    @cached_inventory_items
  else
    @cached_inventory_items = builder_klass.new(variant_id: variant.id).call
  end
end

#can_supply?(quantity = 1, include_locked: false) ⇒ Boolean

Can quantity be supplied? By default from THIS line's pool — which is what add-to-cart gates on, so it must never quietly include stock the caller cannot buy.

include_locked: true adds the locked pool to the total instead, for callers asking "is this worth showing at all" rather than "may this order have it". See Variant#in_stock?.

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 19

def can_supply?(quantity = 1, include_locked: false)
  return false unless variant.available?
  return true unless variant.should_track_inventory?
  return true if variant.backorderable?
  return true if variant.need_confirmation?
  return false if cached_inventory_items.empty?

  cached_inventory_items.all? { |item| item.active? && quantity_in_pool(item, include_locked: include_locked) >= quantity }
end

#quantity_in_pool(item, include_locked: false) ⇒ Object

The cache row carries both pools, so choosing between them is a read, not another Redis trip.



30
31
32
33
34
35
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 30

def quantity_in_pool(item, include_locked: false)
  return item.quantity_available + item.quantity_locked if include_locked
  return item.quantity_locked if options[:locked]

  item.quantity_available
end