Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Disable propagating all variants for new stock locations by default (#11586)

Damian Legawiecc1aebea02b2b
diff --git a/core/app/models/spree/inventory_unit.rb b/core/app/models/spree/inventory_unit.rb
index 689b4fb3f5..f9c6a406b6 100644
--- a/core/app/models/spree/inventory_unit.rb
+++ b/core/app/models/spree/inventory_unit.rb
@@ -63,8 +63,7 @@ module Spree
end
def find_stock_item
- Spree::StockItem.where(stock_location_id: shipment.stock_location_id,
- variant_id: variant_id).first
+ shipment.stock_location.stock_item_or_create(variant)
end
def self.split(original_inventory_unit, extract_quantity)
diff --git a/core/app/models/spree/stock_location.rb b/core/app/models/spree/stock_location.rb
index 4113c7b885..59fb11ea17 100644
--- a/core/app/models/spree/stock_location.rb
+++ b/core/app/models/spree/stock_location.rb
@@ -60,9 +60,15 @@ module Spree
# @param variant Variant instance or Variant ID
#
# @return [StockItem] Corresponding StockItem for the StockLocation's variant.
- def stock_item_or_create(variant)
- variant_id = variant.is_a?(Spree::Variant) ? variant.id : variant
- stock_item(variant_id) || stock_items.create(variant_id: variant_id)
+ def stock_item_or_create(variant_or_variant_id)
+ if variant_or_variant_id.is_a?(Spree::Variant)
+ variant_id = variant_or_variant_id.id
+ variant = variant_or_variant_id
+ else
+ variant_id = variant_or_variant_id
+ variant = Spree::Variant.find(variant_or_variant_id)
+ end
+ stock_item(variant_id) || propagate_variant(variant)
end
def count_on_hand(variant)
@@ -95,8 +101,7 @@ module Spree
end
def fill_status(variant, quantity)
- if item = stock_item(variant)
-
+ if item = stock_item_or_create(variant)
if item.count_on_hand >= quantity
on_hand = quantity
backordered = 0
diff --git a/core/db/migrate/20211229162122_disable_propagate_all_variants_by_default.rb b/core/db/migrate/20211229162122_disable_propagate_all_variants_by_default.rb
new file mode 100644
index 0000000000..c3fd8c938c
--- /dev/null
+++ b/core/db/migrate/20211229162122_disable_propagate_all_variants_by_default.rb
@@ -0,0 +1,5 @@
+class DisablePropagateAllVariantsByDefault < ActiveRecord::Migration[5.2]
+ def change
+ change_column_default :spree_stock_locations, :propagate_all_variants, false
+ end
+end
diff --git a/core/lib/spree/testing_support/factories/order_factory.rb b/core/lib/spree/testing_support/factories/order_factory.rb
index bd74868524..da159b8604 100644
--- a/core/lib/spree/testing_support/factories/order_factory.rb
+++ b/core/lib/spree/testing_support/factories/order_factory.rb
@@ -55,7 +55,8 @@ FactoryBot.define do
order.line_items.reload
end
- create(:shipment, order: order, cost: evaluator.shipment_cost)
+ stock_location = order.line_items&.first&.variant&.stock_items&.first&.stock_location || create(:stock_location)
+ create(:shipment, order: order, cost: evaluator.shipment_cost, stock_location: stock_location)
order.shipments.reload
order.update_with_updater!
diff --git a/core/lib/spree/testing_support/factories/product_factory.rb b/core/lib/spree/testing_support/factories/product_factory.rb
index 189f17632d..2eea8ad1a2 100644
--- a/core/lib/spree/testing_support/factories/product_factory.rb
+++ b/core/lib/spree/testing_support/factories/product_factory.rb
@@ -21,6 +21,9 @@ FactoryBot.define do
product.stores << [store]
end
end
+ after(:create) do |product|
+ Spree::StockLocation.all.each { |stock_location| stock_location.propagate_variant(product.master) unless stock_location.stock_items.exists?(variant: product.master) }
+ end
factory :custom_product do
name { 'Custom Product' }
diff --git a/core/lib/spree/testing_support/factories/stock_location_factory.rb b/core/lib/spree/testing_support/factories/stock_location_factory.rb
index 9767851921..6a92a8c992 100644
--- a/core/lib/spree/testing_support/factories/stock_location_factory.rb
+++ b/core/lib/spree/testing_support/factories/stock_location_factory.rb
@@ -7,6 +7,7 @@ FactoryBot.define do
phone { '(202) 456-1111' }
active { true }
backorderable_default { true }
+ propagate_all_variants { false }
country { |stock_location| Spree::Country.first || stock_location.association(:country) }
state do |stock_location|
@@ -21,8 +22,8 @@ FactoryBot.define do
product_1 = create(:product, stores: [store])
product_2 = create(:product, stores: [store])
- stock_location.stock_items.where(variant_id: product_1.master.id).first.adjust_count_on_hand(10)
- stock_location.stock_items.where(variant_id: product_2.master.id).first.adjust_count_on_hand(20)
+ stock_location.stock_item_or_create(product_1.master).adjust_count_on_hand(10)
+ stock_location.stock_item_or_create(product_2.master).adjust_count_on_hand(20)
end
end
end
diff --git a/core/lib/spree/testing_support/factories/variant_factory.rb b/core/lib/spree/testing_support/factories/variant_factory.rb
index ab1b4debc4..3e8d11d63f 100644
--- a/core/lib/spree/testing_support/factories/variant_factory.rb
+++ b/core/lib/spree/testing_support/factories/variant_factory.rb
@@ -18,6 +18,10 @@ FactoryBot.define do
# ensure stock item will be created for this variant
before(:create) { create(:stock_location) unless Spree::StockLocation.any? }
+ after(:create) do |variant|
+ Spree::StockLocation.all.each { |stock_location| stock_location.propagate_variant(variant) }
+ end
+
factory :variant do
# on_hand 5
product { |p| p.association(:product, stores: [create(:store)]) }
diff --git a/core/spec/models/spree/customer_return_spec.rb b/core/spec/models/spree/customer_return_spec.rb
index a5d786a10a..1221a71b62 100644
--- a/core/spec/models/spree/customer_return_spec.rb
+++ b/core/spec/models/spree/customer_return_spec.rb
@@ -199,7 +199,7 @@ describe Spree::CustomerReturn, type: :model do
end
context 'to a different stock location' do
- let(:new_stock_location) { create(:stock_location, name: 'other') }
+ let(:new_stock_location) { create(:stock_location, name: 'other', propagate_all_variants: true) }
it 'updates the stock item counts in new stock location' do
expect do
diff --git a/core/spec/models/spree/fulfilment_changer_spec.rb b/core/spec/models/spree/fulfilment_changer_spec.rb
index 15466313a3..e56a7c6437 100644
--- a/core/spec/models/spree/fulfilment_changer_spec.rb
+++ b/core/spec/models/spree/fulfilment_changer_spec.rb
@@ -62,12 +62,7 @@ describe Spree::FulfilmentChanger do
context 'when transferring to another stock location' do
let(:desired_stock_location) { create(:stock_location) }
- let!(:stock_item) do
- variant.stock_items.find_or_create_by!(
- stock_location: desired_stock_location,
- variant: variant
- )
- end
+ let!(:stock_item) { desired_stock_location.stock_item_or_create(variant) }
before do
stock_item.set_count_on_hand(desired_count_on_hand)
@@ -196,7 +191,7 @@ describe Spree::FulfilmentChanger do
end
context 'when the original shipment had some backordered units' do
- let(:current_stock_item) { current_shipment.stock_location.stock_items.find_by(variant: variant) }
+ let(:current_stock_item) { current_shipment.stock_location.stock_item_or_create(variant) }
let(:desired_stock_item) { desired_shipment.stock_location.stock_items.find_by(variant: variant) }
let(:backordered_units) { 6 }
diff --git a/core/spec/models/spree/order_inventory_spec.rb b/core/spec/models/spree/order_inventory_spec.rb
index 2ae14107dd..edac2956f1 100644
--- a/core/spec/models/spree/order_inventory_spec.rb
+++ b/core/spec/models/spree/order_inventory_spec.rb
@@ -61,7 +61,7 @@ describe Spree::OrderInventory, type: :model do
context 'variant doesnt track inventory' do
let(:variant) { create(:variant) }
- before { variant.track_inventory = false }
+ before { variant.update(track_inventory: false) }
it 'creates only on hand inventory units' do
variant.stock_items.destroy_all
diff --git a/core/spec/models/spree/reimbursement_spec.rb b/core/spec/models/spree/reimbursement_spec.rb
index 896342afaa..f58301e70d 100644
--- a/core/spec/models/spree/reimbursement_spec.rb
+++ b/core/spec/models/spree/reimbursement_spec.rb
@@ -119,7 +119,9 @@ describe Spree::Reimbursement, type: :model do
end
context 'when exchange is required' do
- let(:exchange_variant) { build(:variant) }
+ # we need to have a variant that will pass eligibility checks
+ # so it needs to be a variant of the same product
+ let!(:exchange_variant) { create(:variant, is_master: true, product: inventory_unit.variant.product) }
before { return_item.exchange_variant = exchange_variant }
diff --git a/core/spec/models/spree/shipment_spec.rb b/core/spec/models/spree/shipment_spec.rb
index 9e2f21a2ff..9ca81d2093 100644
--- a/core/spec/models/spree/shipment_spec.rb
+++ b/core/spec/models/spree/shipment_spec.rb
@@ -743,7 +743,7 @@ describe Spree::Shipment, type: :model do
context '#transfer_to_location' do
# Order with 2 line items in order to be able to split one shipment into 2
let(:order) { create(:completed_order_with_totals, line_items_count: 2) }
- let(:stock_location) { create(:stock_location) }
+ let(:stock_location) { create(:stock_location, propagate_all_variants: true, backorderable_default: true) }
let(:variant) { order.line_items.first.variant }
before do
diff --git a/core/spec/models/spree/stock/packer_spec.rb b/core/spec/models/spree/stock/packer_spec.rb
index da21a15ef1..9620b44110 100644
--- a/core/spec/models/spree/stock/packer_spec.rb
+++ b/core/spec/models/spree/stock/packer_spec.rb
@@ -26,11 +26,17 @@ module Spree
let!(:inventory_units) { Array.new(2) { InventoryUnit.new variant: create(:variant) } }
it 'contains all the items' do
+ inventory_units.each do |inventory_unit|
+ stock_location.propagate_variant(inventory_unit.variant)
+ end
package = subject.default_package
expect(package.contents.size).to eq 2
end
it 'variants are added as backordered without enough on_hand' do
+ inventory_units.each do |inventory_unit|
+ stock_location.propagate_variant(inventory_unit.variant)
+ end
expect(stock_location).to receive(:fill_status).twice.and_return(
*(Array.new(1, [1, 0]) + Array.new(1, [0, 1]))
)
diff --git a/core/spec/models/spree/stock/quantifier_spec.rb b/core/spec/models/spree/stock/quantifier_spec.rb
index fefc82631f..ca28da6f1e 100644
--- a/core/spec/models/spree/stock/quantifier_spec.rb
+++ b/core/spec/models/spree/stock/quantifier_spec.rb
@@ -76,8 +76,8 @@ module Spree
end
context 'with multiple stock locations/items' do
- let!(:stock_location_2) { create :stock_location }
- let!(:stock_location_3) { create :stock_location, active: false }
+ let!(:stock_location_2) { create :stock_location, propagate_all_variants: true }
+ let!(:stock_location_3) { create :stock_location, active: false, propagate_all_variants: true }
before do
stock_location_2.stock_items.where(variant_id: stock_item.variant).update_all(count_on_hand: 5, backorderable: false)
diff --git a/core/spec/models/spree/stock_item_spec.rb b/core/spec/models/spree/stock_item_spec.rb
index 38721f1aaa..75930afd29 100644
--- a/core/spec/models/spree/stock_item_spec.rb
+++ b/core/spec/models/spree/stock_item_spec.rb
@@ -250,7 +250,7 @@ describe Spree::StockItem, type: :model do
context 'when a new stock location is added' do
it 'touches its variant' do
expect do
- create(:stock_location)
+ create(:stock_location, propagate_all_variants: true)
end.to change { subject.variant.reload.updated_at }
end
end
diff --git a/core/spec/models/spree/stock_location_spec.rb b/core/spec/models/spree/stock_location_spec.rb
index 543b7dd483..ee5b1ba2c7 100644
--- a/core/spec/models/spree/stock_location_spec.rb
+++ b/core/spec/models/spree/stock_location_spec.rb
@@ -10,14 +10,14 @@ module Spree
context 'handling the stock items creation after create' do
let!(:variant) { create(:variant) }
- before { StockLocation.destroy_all }
+ before { described_class.destroy_all }
it 'creates stock_items for all variants' do
expect do
- create(:stock_location)
+ create(:stock_location, propagate_all_variants: true)
end.to(
change { Variant.count }.by(0).and(
- change { StockLocation.count }.from(0).to(1).and(
+ change { described_class.count }.from(0).to(1).and(
change { StockItem.count }.from(0).to(2)
)
)
@@ -26,15 +26,15 @@ module Spree
end
it 'validates uniqueness' do
- StockLocation.create(name: 'Test')
- expect(StockLocation.new(name: 'Test')).not_to be_valid
+ described_class.create(name: 'Test')
+ expect(described_class.new(name: 'Test')).not_to be_valid
end
context 'handling stock items' do
let!(:variant) { create(:variant) }
context 'given a variant' do
- subject { StockLocation.create(name: 'testing', propagate_all_variants: false) }
+ subject { described_class.create(name: 'testing', propagate_all_variants: false) }
context 'set up' do
it 'creates stock item' do
@@ -76,7 +76,7 @@ module Spree
end
context 'propagate all variants' do
- subject { StockLocation.new(name: 'testing') }
+ subject { described_class.new(name: 'testing') }
context 'true' do
before { subject.propagate_all_variants = true }
@@ -188,7 +188,7 @@ module Spree
it 'can be deactivated' do
create(:stock_location, active: true)
create(:stock_location, active: false)
- expect(Spree::StockLocation.active.count).to eq 1
+ expect(described_class.active.count).to eq 1
end
it 'ensures only one stock location is default at a time' do
@@ -223,7 +223,7 @@ module Spree
end
it 'zero on_hand with all backordered' do
- expect(subject).to receive(:stock_item).with(variant).and_return(zero_stock_item)
+ expect(subject).to receive(:stock_item_or_create).with(variant).and_return(zero_stock_item)
on_hand, backordered = subject.fill_status(variant, 20)
expect(on_hand).to eq 0
@@ -233,7 +233,7 @@ module Spree
context 'when backordering is not allowed' do
before do
allow(stock_item).to receive_messages backorderable?: false
- expect(subject).to receive(:stock_item).with(variant).and_return(stock_item)
+ expect(subject).to receive(:stock_item_or_create).with(variant).and_return(stock_item)
end
it 'all on_hand' do
@@ -266,25 +266,25 @@ module Spree
let(:variant) { create(:base_variant) }
- it 'zero on_hand and backordered' do
+ it 'zero on_hand and one backordered' do
subject
variant.stock_items.destroy_all
on_hand, backordered = subject.fill_status(variant, 1)
expect(on_hand).to eq 0
- expect(backordered).to eq 0
+ expect(backordered).to eq 1
end
end
end
context '#state_text' do
context 'state is blank' do
- subject { StockLocation.create(name: 'testing', state: nil, state_name: 'virginia') }
+ subject { described_class.create(name: 'testing', state: nil, state_name: 'virginia') }
specify { expect(subject.state_text).to eq('virginia') }
end
context 'both name and abbr is present' do
- subject { StockLocation.create(name: 'testing', state: state, state_name: nil) }
+ subject { described_class.create(name: 'testing', state: state, state_name: nil) }
let(:state) { create(:state, name: 'virginia', abbr: 'va') }
@@ -292,7 +292,7 @@ module Spree
end
context 'only name is present' do
- subject { StockLocation.create(name: 'testing', state: state, state_name: nil) }
+ subject { described_class.create(name: 'testing', state: state, state_name: nil) }
let(:state) { create(:state, name: 'virginia', abbr: nil) }
diff --git a/sample/db/samples/stock.rb b/sample/db/samples/stock.rb
index 3fc0a39ec7..4e8c765644 100644
--- a/sample/db/samples/stock.rb
+++ b/sample/db/samples/stock.rb
@@ -16,8 +16,8 @@ Spree::StockLocations::StockItems::Create.call(stock_location: location)
product_1 = Spree::Product.find_by!(name: 'Denim Shirt')
product_2 = Spree::Product.find_by!(name: 'Checked Shirt')
-product_1.master.stock_items.find_by!(stock_location: location).update!(count_on_hand: 1)
-product_2.master.stock_items.find_by!(stock_location: location).update!(count_on_hand: 1)
+location.stock_item_or_create(product_1.master).update(count_on_hand: 1)
+location.stock_item_or_create(product_2.master).update(count_on_hand: 1)
Spree::Variant.all.each do |variant|
next if variant.is_master? && variant.product.has_variants?