Skip to content

Commit 2e17aeb

Browse files
committed
Add Quantifier#positive_stock method
When the quantifier is initialized with a stock location or its ID, the new method returns the positive amount of stock on hand or zero, if the stock for the variant is negative. Otherwise, it returns nil.
1 parent 0524dcd commit 2e17aeb

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

core/app/models/spree/stock/quantifier.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,25 @@ def can_supply?(required)
4343
total_on_hand >= required || backorderable?
4444
end
4545

46+
def positive_stock
47+
return unless stock_location
48+
49+
on_hand = stock_location.count_on_hand(variant)
50+
on_hand.positive? ? on_hand : 0
51+
end
52+
4653
private
4754

4855
attr_reader :variant, :stock_location_or_id
4956

57+
def stock_location
58+
@stock_location ||= if stock_location_or_id.is_a?(Spree::StockLocation)
59+
stock_location_or_id
60+
else
61+
Spree::StockLocation.find_by(id: stock_location_or_id)
62+
end
63+
end
64+
5065
def variant_stock_items
5166
variant.stock_items.select do |stock_item|
5267
if stock_location_or_id

core/spec/models/spree/stock/quantifier_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ module Stock
1212
expect(subject.can_supply?(100_001)).to eq true
1313
end
1414
end
15+
16+
shared_examples_for "returns the positive stock on hand" do
17+
context "when the stock location has no stock for the variant" do
18+
it { is_expected.to be_zero }
19+
end
20+
21+
context "when the stock location has negative stock for the variant" do
22+
before { stock_item.set_count_on_hand(-1) }
23+
24+
it { is_expected.to be_zero }
25+
end
26+
27+
context "when the stock location has positive stock for the variant" do
28+
before { stock_item.set_count_on_hand(10) }
29+
30+
it { is_expected.to eq(10) }
31+
end
32+
end
33+
1534
let(:target_stock_location) { nil }
1635
let!(:stock_location) { create :stock_location_with_items }
1736
let!(:stock_item) { stock_location.stock_items.order(:id).first }
@@ -107,6 +126,35 @@ module Stock
107126
expect(subject.can_supply?(6)).to eq false
108127
end
109128
end
129+
130+
describe "#positive_stock" do
131+
let(:variant) { create(:variant) }
132+
let(:stock_location) { create(:stock_location) }
133+
let(:stock_item) { stock_location.set_up_stock_item(variant) }
134+
let(:instance) { described_class.new(variant, stock_location_or_id) }
135+
136+
subject { instance.positive_stock }
137+
138+
context "when stock location is not present" do
139+
let(:stock_location_or_id) { nil }
140+
141+
it { is_expected.to be_nil }
142+
end
143+
144+
context "when stock_location_id is present" do
145+
context "when stock_location_id is a stock location" do
146+
let(:stock_location_or_id) { stock_location }
147+
148+
it_behaves_like "returns the positive stock on hand"
149+
end
150+
151+
context "when stock_location_id is a stock location id" do
152+
let(:stock_location_or_id) { stock_location.id }
153+
154+
it_behaves_like "returns the positive stock on hand"
155+
end
156+
end
157+
end
110158
end
111159
end
112160
end

0 commit comments

Comments
 (0)