Module: SpreeCmCommissioner::Stock::AvailabilityValidatorDecorator

Defined in:
app/models/spree_cm_commissioner/stock/availability_validator_decorator.rb

Instance Method Summary collapse

Instance Method Details

#validate(line_item) ⇒ Object

override Complexity cops: core's own validate already trips these under similarly strict config.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/spree_cm_commissioner/stock/availability_validator_decorator.rb', line 6

def validate(line_item) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  unit_count = line_item.inventory_units.reject(&:pending?).sum(&:quantity)
  return if unit_count >= line_item.quantity

  quantity = line_item.quantity - unit_count
  return if quantity.zero?

  # Ticket-transfer orders move existing inventory between guests rather than
  # purchasing new stock, so they must not be gated by the live stock check — and
  # skipping it here means we never even build a checker for them.
  return if line_item.order&.ticket_transfer?

  checker = SpreeCmCommissioner::Stock::LineItemAvailabilityChecker.new(line_item).checker
  return if checker.can_supply?(quantity)

  if line_item.variant.available?
    if line_item.locked_stock?
      # own allotment exhausted — ordinary sold out, never "reserved"
      add_sold_out_error(line_item)
    else
      # public line: own pool already failed — check both pools together
      if checker.can_supply?(quantity, include_locked: true) # rubocop:disable Style/IfInsideElse -- elsif would collapse the locked/public split
        add_reserved_error(line_item)
      else
        add_sold_out_error(line_item)
      end
    end
  else
    add_inactive_product_error(line_item)
  end
end