Permalink
Browse files

Account for purchased units in stock validation

Merges #1525
  • Loading branch information...
cyu authored and radar committed May 9, 2012
1 parent 73680a5 commit 7c4cd77a31afa97369fbf7044102f49a925fa4f1
Showing with 31 additions and 1 deletion.
  1. +6 −1 core/app/models/spree/line_item.rb
  2. +25 −0 core/spec/models/line_item_spec.rb
@@ -45,7 +45,12 @@ def adjust_quantity
end
def sufficient_stock?
Spree::Config[:allow_backorders] ? true : (variant.on_hand >= quantity)
return true if Spree::Config[:allow_backorders]
if new_record? || !order.completed?
variant.on_hand >= quantity
else
variant.on_hand >= (quantity - self.changed_attributes['quantity'].to_i)
end
end
def insufficient_stock?
@@ -141,6 +141,31 @@
line_item.insufficient_stock?.should be_false
line_item.sufficient_stock?.should be_true
end
context 'when line item has been saved' do
before { line_item.stub(:new_record? => false) }
it 'should report sufficient stock when reducing purchased quantity' do
line_item.stub(:changed_attributes => {'quantity' => 6}, :quantity => 5)
line_item.stub_chain :variant, :on_hand => 0
line_item.insufficient_stock?.should be_false
line_item.sufficient_stock?.should be_true
end
it 'should report sufficient stock when increasing purchased quantity and variant has enough on_hand' do
line_item.stub(:changed_attributes => {'quantity' => 5}, :quantity => 6)
line_item.stub_chain :variant, :on_hand => 1
line_item.insufficient_stock?.should be_false
line_item.sufficient_stock?.should be_true
end
it 'should report insufficient stock when increasing purchased quantity and new units is more than variant on_hand' do
line_item.stub(:changed_attributes => {'quantity' => 5}, :quantity => 7)
line_item.stub_chain :variant, :on_hand => 1
line_item.insufficient_stock?.should be_true
line_item.sufficient_stock?.should be_false
end
end
end
context 'when backordering is enabled' do

0 comments on commit 7c4cd77

Please sign in to comment.