diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index 550b94138d..de9dffbcc4 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -47,6 +47,7 @@ Spree::LineItem.class_eval do def cap_quantity_at_stock! + scoper.scope(variant) update_attributes!(quantity: variant.on_hand) if quantity > variant.on_hand end @@ -129,7 +130,7 @@ Spree::LineItem.class_eval do def scoper return @scoper unless @scoper.nil? @scoper = OpenFoodNetwork::ScopeVariantToHub.new(order.distributor) - end + end def calculate_final_weight_volume if final_weight_volume.present? && quantity_was > 0 diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index d5ab83b59c..00e2077dc9 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -75,6 +75,19 @@ module Spree li.quantity.should == 10 li.max_quantity.should == 10 end + + context "when a variant override is in place" do + let!(:hub) { create(:distributor_enterprise) } + + before { li.order.update_attributes(distributor_id: hub.id) } + + let!(:vo) { create(:variant_override, hub: hub, variant: v, count_on_hand: 2) } + + it "caps quantity to override stock level" do + li.cap_quantity_at_stock! + li.quantity.should == 2 + end + end end describe "tracking stock when quantity is changed" do