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.

[SD-885] Adds check and flash message when trying to create stock transfer with a variant not available on hand

Robert Nowakowski08166cccb3ad
diff --git a/backend/app/controllers/spree/admin/stock_transfers_controller.rb b/backend/app/controllers/spree/admin/stock_transfers_controller.rb
index bb5181e793..489a1a9f1f 100644
--- a/backend/app/controllers/spree/admin/stock_transfers_controller.rb
+++ b/backend/app/controllers/spree/admin/stock_transfers_controller.rb
@@ -22,6 +22,9 @@ module Spree
if params[:variant].nil?
flash[:error] = Spree.t('stock_transfer.errors.must_have_variant')
render :new
+ elsif any_missing_variants?(params[:variant])
+ flash.now[:error] = Spree.t('stock_transfer.errors.variants_unavailable', stock: source_location.name)
+ render :new
else
variants = Hash.new(0)
params[:variant].each_with_index do |variant_id, i|
@@ -51,6 +54,10 @@ module Spree
def destination_location
@destination_location ||= StockLocation.find(params[:transfer_destination_location_id])
end
+
+ def any_missing_variants?(variant_ids)
+ source_location&.stock_items&.where(variant_id: variant_ids, count_on_hand: 0)&.any?
+ end
end
end
end
diff --git a/backend/spec/features/admin/stock_transfer_spec.rb b/backend/spec/features/admin/stock_transfer_spec.rb
index 9842820956..686e862034 100644
--- a/backend/spec/features/admin/stock_transfer_spec.rb
+++ b/backend/spec/features/admin/stock_transfer_spec.rb
@@ -43,6 +43,25 @@ describe 'Stock Transfers', type: :feature, js: true do
expect(transfer.stock_movements.size).to eq 2
end
+ it 'does not transfer if variant is not available on hand' do
+ create(:stock_location_with_items, name: 'NY') # source_location
+ create(:stock_location, name: 'SF') # destination_location
+
+ product = create(:product)
+ Spree::StockLocation.first.stock_items.where(variant_id: product.master.id).first.adjust_count_on_hand(0)
+
+ visit spree.admin_stock_transfers_path
+ click_on 'New Stock Transfer'
+ fill_in 'reference', with: 'PO 666'
+
+ select2 product.master.name, from: 'Variant', search: true
+
+ click_button 'Add'
+ click_button 'Transfer Stock'
+
+ expect(page).to have_content('Some variants are not available')
+ end
+
describe 'received stock transfer' do
def it_is_received_stock_transfer(page)
expect(page).to have_content('Reference PO 666')
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index e1a40d3a4a..5b6cf04e2f 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -1626,6 +1626,7 @@ en:
stock_transfer:
errors:
must_have_variant: You must add atleast one variant.
+ variants_unavailable: "Some variants are not available on %{stock}"
stock_transfers: Stock Transfers
stop: Stop
store: Store