diff --git a/core/app/models/spree/fulfilment_changer.rb b/core/app/models/spree/fulfilment_changer.rb index b30e4b4abf2..1178c08f8e6 100644 --- a/core/app/models/spree/fulfilment_changer.rb +++ b/core/app/models/spree/fulfilment_changer.rb @@ -144,15 +144,10 @@ def handle_stock_counts? end def get_available_quantity - positive_stock = ->(shipment) do - on_hand = shipment.stock_location.count_on_hand(variant) - on_hand.positive? ? on_hand : 0 - end - if current_stock_location != desired_stock_location - positive_stock.call(desired_shipment) + desired_location_quantifier.positive_stock else - sl_availability = positive_stock.call(current_shipment) + sl_availability = current_location_quantifier.positive_stock shipment_availability = current_shipment.inventory_units.where(variant: variant).on_hand.count sl_availability + shipment_availability end @@ -174,11 +169,19 @@ def current_shipment_not_already_shipped end def enough_stock_at_desired_location - unless Spree::Stock::Quantifier.new(variant, desired_stock_location).can_supply?(quantity) + unless desired_location_quantifier.can_supply?(quantity) errors.add(:desired_shipment, :not_enough_stock_at_desired_location) end end + def desired_location_quantifier + @desired_location_quantifier ||= Spree::Stock::Quantifier.new(variant, desired_stock_location) + end + + def current_location_quantifier + @current_location_quantifier ||= Spree::Stock::Quantifier.new(variant, current_stock_location) + end + def desired_shipment_different_from_current if desired_shipment.id == current_shipment.id errors.add(:desired_shipment, :can_not_transfer_within_same_shipment)