Skip to content

Commit

Permalink
Add Quantifier#positive_stock method
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
spaghetticode committed Mar 7, 2024
1 parent 5a21d93 commit d0a348a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
13 changes: 13 additions & 0 deletions core/app/models/spree/stock/quantifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,23 @@ def can_supply?(required)
total_on_hand >= required || backorderable?
end

def positive_stock
return unless stock_location

on_hand = stock_location.count_on_hand(variant)
on_hand.positive? ? on_hand : 0
end

private

attr_reader :variant, :stock_location_or_id

def stock_location
@stock_location ||= stock_location_or_id.is_a?(Spree::StockLocation) ?
stock_location_or_id :
Spree::StockLocation.find_by(id: stock_location_or_id)
end

def get_stock_items
variant.stock_items.select do |stock_item|
if stock_location_or_id
Expand Down
48 changes: 48 additions & 0 deletions core/spec/models/spree/stock/quantifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ module Stock
expect(subject.can_supply?(100_001)).to eq true
end
end

shared_examples_for "returns the positive stock on hand" do
context "when the stock location has no stock for the variant" do
it { is_expected.to be_zero }
end

context "when the stock location has negative stock for the variant" do
before { stock_item.set_count_on_hand(-1) }

it { is_expected.to be_zero }
end

context "when the stock location has positive stock for the variant" do
before { stock_item.set_count_on_hand(10) }

it { is_expected.to eq(10) }
end
end

let(:target_stock_location) { nil }
let!(:stock_location) { create :stock_location_with_items }
let!(:stock_item) { stock_location.stock_items.order(:id).first }
Expand Down Expand Up @@ -107,6 +126,35 @@ module Stock
expect(subject.can_supply?(6)).to eq false
end
end

describe "#positive_stock" do
let(:variant) { create(:variant) }
let(:stock_location) { create(:stock_location) }
let(:stock_item) { stock_location.set_up_stock_item(variant) }
let(:instance) { described_class.new(variant, stock_location_or_id) }

subject { instance.positive_stock }

context "when stock location is not present" do
let(:stock_location_or_id) { nil }

it { is_expected.to be_nil }
end

context "when stock_location_id is present" do
context "when stock_location_id is a stock location" do
let(:stock_location_or_id) { stock_location }

it_behaves_like "returns the positive stock on hand"
end

context "when stock_location_id is a stock location id" do
let(:stock_location_or_id) { stock_location.id }

it_behaves_like "returns the positive stock on hand"
end
end
end
end
end
end

0 comments on commit d0a348a

Please sign in to comment.