Class: Spree::Stock::Quantifier

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/stock/quantifier.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variant, stock_location = nil, excluded_order: nil, stock_levels: nil) ⇒ Quantifier

Returns a new instance of Quantifier.

Parameters:

  • excluded_order (Spree::Cart, Spree::Order, nil) (defaults to: nil) —

    when given, reservations belonging to this cart/order are not counted against availability. Used when checking a cart's own line items so the customer's own checkout hold doesn't make their item look out of stock.

  • stock_levels (Enumerable<Spree::StockLevel>, nil) (defaults to: nil) —

    rows to count instead of the variant's own. Passed by the inventory provider when stock lives in an external system; the rows may be unsaved.



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

def initialize(variant, stock_location = nil, excluded_order: nil, stock_levels: nil)
  @variant         = variant
  @stock_location  = stock_location
  @excluded_order  = excluded_order
  @provided_stock_levels = stock_levels
end

Instance Attribute Details

#excluded_order ⇒ Object (readonly)

Returns the value of attribute excluded_order.



4
5
6
# File 'app/models/spree/stock/quantifier.rb', line 4

def excluded_order
  @excluded_order
end

#provided_stock_levels ⇒ Object (readonly)

Returns the value of attribute provided_stock_levels.



4
5
6
# File 'app/models/spree/stock/quantifier.rb', line 4

def provided_stock_levels
  @provided_stock_levels
end

#stock_location ⇒ Object (readonly)

Returns the value of attribute stock_location.



4
5
6
# File 'app/models/spree/stock/quantifier.rb', line 4

def stock_location
  @stock_location
end

#variant ⇒ Object (readonly)

Returns the value of attribute variant.



4
5
6
# File 'app/models/spree/stock/quantifier.rb', line 4

def variant
  @variant
end

Class Method Details

.preload(variants) ⇒ void

This method returns an undefined value.

What a quantifier reads, preloaded for many variants at once, so quantifying each of them reads memory instead of querying per variant. Variants whose levels are loaded already are left as they are.

Parameters:



12
13
14
15
16
17
18
19
20
# File 'app/models/spree/stock/quantifier.rb', line 12

def self.preload(variants)
  unloaded = variants.compact.reject { |variant| variant.association(:stock_levels).loaded? }
  return if unloaded.empty?

  ActiveRecord::Associations::Preloader.new(
    records: unloaded,
    associations: { stock_levels: [:stock_location, :active_stock_reservations] }
  ).call
end

Instance Method Details

#available_stock ⇒ Integer

Physical pool minus already-allocated units, summed across the variant's active stock levels.

Returns:

  • (Integer) —

    units available before checkout reservations



57
58
59
60
61
62
63
# File 'app/models/spree/stock/quantifier.rb', line 57

def available_stock
  if association_loaded?
    stock_levels.sum(&:available_count)
  else
    stock_levels.sum('count_on_hand - allocated_count')
  end
end

#backorderable? ⇒ Boolean

Check if any of variant stock items is backorderable

Returns:

  • (Boolean)


102
103
104
# File 'app/models/spree/stock/quantifier.rb', line 102

def backorderable?
  @backorderable ||= stock_levels.any?(&:backorderable)
end

#can_supply?(required = 1) ⇒ Boolean

Whether the requested quantity can be supplied. Beyond on-hand stock a variant may oversell two ways: backorderable stock, or a pre-order (which also lifts the publish gate for a scheduled launch). Either way the variant's backorder_limit caps how far below zero it may go; a nil (empty) limit means unlimited.

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/spree/stock/quantifier.rb', line 111

def can_supply?(required = 1)
  return false unless variant.available? || variant.preorder?
  return true unless variant.should_track_inventory?

  oversellable = backorderable? || variant.preorder?
  limit = variant.backorder_limit
  return true if oversellable && limit.nil?

  # On-hand stock, plus — for a capped oversell — the room to sell below
  # zero down to +-backorder_limit+ (a single clamp of signed on-hand plus
  # the limit).
  supplyable = if oversellable
                 [available_stock - reserved_quantity + limit, 0].max
               else
                 total_on_hand
               end
  supplyable >= required
end

#persisted_stock_level_ids ⇒ Array<Integer>

Reservations hang off Spree's own rows, so an external provider's unsaved rows carry no id to match on. Resolve them by (variant, location) instead: a checkout hold is Spree's to honour whoever owns the stock figure, and matching on a nil id would silently subtract nothing and oversell the hold.

Returns:

  • (Array<Integer>)


141
142
143
144
145
146
147
148
149
# File 'app/models/spree/stock/quantifier.rb', line 141

def persisted_stock_level_ids
  ids = stock_levels.map(&:id).compact
  return ids if ids.length == stock_levels.size

  locations = stock_levels.map(&:stock_location_id).compact
  return ids if locations.empty?

  ids | Spree::StockLevel.where(variant_id: variant.id, stock_location_id: locations).ids
end

#reserved_quantity ⇒ Integer

Units currently held by active checkout reservations on the location-filtered stock items. Returns 0 when stock reservations are globally disabled.

Reads through the same #stock_levels collection as #available_stock so a per-location query (filtered by stock_location) only counts reservations that belong to those same stock items — otherwise a multi-location variant would subtract reservations from other warehouses.

When excluded_order is set, that order's own reservations are left out of the count so an order's own checkout hold doesn't count against the availability of its own line items.

Returns:

  • (Integer)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/spree/stock/quantifier.rb', line 80

def reserved_quantity
  return @reserved_quantity if defined?(@reserved_quantity)
  return @reserved_quantity = 0 unless reservations_enabled?
  return @reserved_quantity = 0 if stock_levels.blank?

  excluded_owner_key = excluded_order.is_a?(Spree::Cart) ? :cart_id : :order_id
  excluded_owner_id = excluded_order&.id

  @reserved_quantity = if reservations_preloaded?
                         stock_levels.sum do |si|
                           reservations = si.active_stock_reservations
                           reservations = reservations.reject { |r| r.public_send(excluded_owner_key) == excluded_owner_id } if excluded_owner_id
                           reservations.sum(&:quantity)
                         end
                       else
                         reservations = Spree::StockReservation.active.where(stock_level_id: persisted_stock_level_ids)
                         reservations = reservations.where.not(excluded_owner_key => excluded_owner_id) if excluded_owner_id
                         reservations.sum(:quantity)
                       end
end

#stock_levels ⇒ Object



130
131
132
# File 'app/models/spree/stock/quantifier.rb', line 130

def stock_levels
  @stock_levels ||= scope_to_location(provided_stock_levels || variant.stock_levels)
end

#total_on_hand ⇒ Integer, BigDecimal

Units a customer can purchase right now: physical pool minus already-allocated units minus active checkout reservations. Clamped at zero so callers never see a negative count.

Returns BigDecimal::INFINITY when the variant does not track inventory (effectively unlimited supply).

Returns:

  • (Integer, BigDecimal) —

    purchasable quantity, or INFINITY



45
46
47
48
49
50
51
# File 'app/models/spree/stock/quantifier.rb', line 45

def total_on_hand
  @total_on_hand ||= if variant.should_track_inventory?
                       [available_stock - reserved_quantity, 0].max
                     else
                       BigDecimal::INFINITY
                     end
end