mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-05 22:26:07 +00:00
Checking for sufficient stock takes variant overrides into account
This commit is contained in:
@@ -125,6 +125,18 @@ Spree::LineItem.class_eval do
|
||||
Spree::InventoryUnit.decrease(order, variant, quantity)
|
||||
end
|
||||
|
||||
# MONKEYPATCH of Spree method
|
||||
# Enables scoping of variant to hub/shop, so we check stock against relevant overrides if they exist
|
||||
def sufficient_stock?
|
||||
scoper.scope(variant) # This line added
|
||||
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
|
||||
|
||||
private
|
||||
|
||||
def scoper
|
||||
|
||||
@@ -140,6 +140,37 @@ module Spree
|
||||
end
|
||||
end
|
||||
|
||||
describe "determining if sufficient stock is present" do
|
||||
let!(:v) { create(:variant, on_demand: false, on_hand: 10) }
|
||||
let!(:li) { create(:line_item, variant: v, quantity: 5, max_quantity: 5) }
|
||||
let!(:hub) { create(:distributor_enterprise) }
|
||||
|
||||
before do
|
||||
Spree::Config.set allow_backorders: false
|
||||
li.order.update_attributes(distributor_id: hub.id)
|
||||
end
|
||||
|
||||
context "when no variant override is in place" do
|
||||
it "uses stock level on the variant" do
|
||||
expect(li.sufficient_stock?).to be_true
|
||||
v.update_attributes(on_hand: 4)
|
||||
expect(li.sufficient_stock?).to be_false
|
||||
end
|
||||
end
|
||||
|
||||
context "when a variant override is in place" do
|
||||
let!(:vo) { create(:variant_override, hub: hub, variant: v, count_on_hand: 5) }
|
||||
|
||||
it "uses stock level on the override" do
|
||||
expect(li.sufficient_stock?).to be_true
|
||||
v.update_attributes(on_hand: 4)
|
||||
expect(li.sufficient_stock?).to be_true
|
||||
vo.update_attributes(count_on_hand: 4)
|
||||
expect(li.sufficient_stock?).to be_false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "calculating price with adjustments" do
|
||||
it "does not return fractional cents" do
|
||||
li = LineItem.new
|
||||
|
||||
Reference in New Issue
Block a user