Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Variant to check stock by stock_location #3884

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions core/app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,22 @@ def in_stock?
end

# @param quantity [Fixnum] how many are desired
# @param stock_location [Spree::StockLocation] Optionally restrict stock
# quantity check to a specific stock location. If unspecified it will
# check inventory in all available StockLocations.
# @return [Boolean] true if the desired quantity can be supplied
def can_supply?(quantity = 1)
Spree::Stock::Quantifier.new(self).can_supply?(quantity)
def can_supply?(quantity = 1, stock_location = nil)
Spree::Stock::Quantifier.new(self, stock_location).can_supply?(quantity)
end

# Fetches the on-hand quantity of the variant.
#
# @param stock_location [Spree::StockLocation] Optionally restrict stock
# quantity check to a specific stock location. If unspecified it will
# check inventory in all available StockLocations.
# @return [Fixnum] the number currently on-hand
def total_on_hand
Spree::Stock::Quantifier.new(self).total_on_hand
def total_on_hand(stock_location = nil)
Spree::Stock::Quantifier.new(self, stock_location).total_on_hand
end

# Shortcut method to determine if inventory tracking is enabled for this
Expand Down
32 changes: 32 additions & 0 deletions core/spec/models/spree/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,22 @@
expect(quantifier).to receive(:can_supply?).with(10)
variant.can_supply?(10)
end

context "with a stock_location specified" do
subject { variant.can_supply?(10, stock_location) }

let(:quantifier) { instance_double(Spree::Stock::Quantifier) }
let(:stock_location) { build_stubbed(:stock_location) }

it "initializes the quantifier with the stock location" do
expect(Spree::Stock::Quantifier).
to receive(:new).
with(variant, stock_location).
and_return(quantifier)
allow(quantifier).to receive(:can_supply?).with(10)
subject
end
end
end

context 'when stock_items are backorderable' do
Expand Down Expand Up @@ -633,6 +649,22 @@
variant = build(:variant)
expect(variant.total_on_hand).to eq(Spree::Stock::Quantifier.new(variant).total_on_hand)
end

context "with a stock_location specified" do
subject { variant.total_on_hand(stock_location) }

let(:quantifier) { instance_double(Spree::Stock::Quantifier) }
let(:stock_location) { build_stubbed(:stock_location) }

it "initializes the quantifier with the stock location" do
expect(Spree::Stock::Quantifier).
to receive(:new).
with(variant, stock_location).
and_return(quantifier)
allow(quantifier).to receive(:total_on_hand)
subject
end
end
end

describe '#tax_category' do
Expand Down