Class: Spree::Stock::PosAvailabilityValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/models/spree/stock/pos_availability_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate(line_item) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'app/models/spree/stock/pos_availability_validator.rb', line 4

def validate(line_item)
  variant = line_item.variant
  stock_location = line_item.order.pos_shipment.try(:stock_location)
  quantity_required = line_item.quantity - line_item.quantity_was.to_i
  if !stock_location || !stock_location.active? || !stock_location.store?
    line_item.errors[:stock_location] << 'No Active Store Associated'
  elsif !stock_location.can_supply?(quantity_required, variant)
    line_item.errors[:quantity] << 'Adding More Than Available'
  end
end