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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Add more validations to `Spree::StockTransfer` (#12211)
Damian Kaczmarczyk3c6db72342ed
diff --git a/core/app/models/spree/stock_transfer.rb b/core/app/models/spree/stock_transfer.rbindex 240c44c923..34cfce2266 100644--- a/core/app/models/spree/stock_transfer.rb+++ b/core/app/models/spree/stock_transfer.rb@@ -15,6 +15,9 @@ module Spreeself.whitelisted_ransackable_attributes = %w[reference source_location_id destination_location_id number]+ validate :source_location_is_not_destination_location+ validates :destination_location, presence: true+def source_movementsfind_stock_location_with_location_id(source_location_id)end@@ -24,6 +27,16 @@ module Spreeenddef transfer(source_location, destination_location, variants)+ if variants.nil? || variants.empty?+ errors.add(:base, Spree.t('stock_transfer.errors.must_have_variant'))+ return false+ end++ unless variants_available_in_source_location?(source_location, variants)+ errors.add(:base, Spree.t('stock_transfer.errors.variants_unavailable'))+ return false+ end+transaction dovariants.each_pair do |variant, quantity|source_location&.unstock(variant, quantity, self)@@ -34,6 +47,8 @@ module Spreesave!endend++ trueend# receive inventory from external vendor@@ -46,5 +61,19 @@ module Spreedef find_stock_location_with_location_id(location_id)stock_movements.joins(:stock_item).where('spree_stock_items.stock_location_id' => location_id)end++ def source_location_is_not_destination_location+ return unless source_location_id.present?+ return unless destination_location_id.present?+ return if source_location_id != destination_location_id++ errors.add(:source_location, Spree.t('stock_transfer.errors.same_location'))+ end++ def variants_available_in_source_location?(source_location, variants)+ return true if source_location.nil?++ source_location.stock_items.where(variant: variants.keys).where(Spree::StockItem.arel_table[:count_on_hand].gt(0)).size == variants.keys.size+ endendenddiff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex 6f04623c31..f1afd5a400 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -1605,6 +1605,7 @@ en:errors:must_have_variant: You must add at least one variant.variants_unavailable: "Some variants are not available on %{stock}"+ same_location: "cannot be the same as the destination location"stock_transfers: Stock Transfersstop: Stopstore: Storediff --git a/core/lib/spree/testing_support/factories/stock_transfer_factory.rb b/core/lib/spree/testing_support/factories/stock_transfer_factory.rbindex 68c85e3772..b1f6483b64 100644--- a/core/lib/spree/testing_support/factories/stock_transfer_factory.rb+++ b/core/lib/spree/testing_support/factories/stock_transfer_factory.rb@@ -1,9 +1,9 @@FactoryBot.define dofactory :stock_transfer, class: Spree::StockTransfer do- destination_location {}+ association :source_location, factory: :stock_location+ association :destination_location, factory: :stock_locationnumber {}reference {}- source_location {}type {}endenddiff --git a/core/spec/models/spree/stock_transfer_spec.rb b/core/spec/models/spree/stock_transfer_spec.rbindex 5cad2108fb..406bac8453 100644--- a/core/spec/models/spree/stock_transfer_spec.rb+++ b/core/spec/models/spree/stock_transfer_spec.rb@@ -2,7 +2,7 @@ require 'spec_helper'module Spreedescribe StockTransfer, type: :model do- subject { StockTransfer.create(reference: 'PO123') }+ subject { described_class.create(reference: 'PO123', source_location: source_location, destination_location: destination_location) }let(:destination_location) { create(:stock_location_with_items) }let(:source_location) { create(:stock_location_with_items) }@@ -23,21 +23,44 @@ module Spreeit { is_expected.to match(/T\d+/) }end- it 'transfers variants between 2 locations' do- variants = { variant => 5 }+ describe '#transfer' do+ it 'transfers variants between 2 locations' do+ variants = { variant => 5 }- subject.transfer(source_location,- destination_location,- variants)+ subject.transfer(source_location, destination_location, variants)- expect(source_location.count_on_hand(variant)).to eq 5- expect(destination_location.count_on_hand(variant)).to eq 5+ expect(source_location.count_on_hand(variant)).to eq 5+ expect(destination_location.count_on_hand(variant)).to eq 5- expect(subject.source_location).to eq source_location- expect(subject.destination_location).to eq destination_location+ expect(subject.source_location).to eq source_location+ expect(subject.destination_location).to eq destination_location++ expect(subject.source_movements.first.quantity).to eq(-5)+ expect(subject.destination_movements.first.quantity).to eq 5+ end++ context 'when variants are not available in the source location' do+ let(:other_variant) { create(:variant) }- expect(subject.source_movements.first.quantity).to eq(-5)- expect(subject.destination_movements.first.quantity).to eq 5+ it 'does not transfer the variants' do+ expect(subject.transfer(source_location, destination_location, { variant => 5, other_variant => 5 })).to be false+ expect(subject.errors[:base]).to include(Spree.t('stock_transfer.errors.variants_unavailable'))+ end+ end++ context 'when variants are empty' do+ it 'does not transfer the variants' do+ expect(subject.transfer(source_location, destination_location, {})).to be false+ expect(subject.errors[:base]).to include(Spree.t('stock_transfer.errors.must_have_variant'))+ end+ end++ context 'when variants are nil' do+ it 'does not transfer the variants' do+ expect(subject.transfer(source_location, destination_location, nil)).to be false+ expect(subject.errors[:base]).to include(Spree.t('stock_transfer.errors.must_have_variant'))+ end+ endendit 'receive new inventory (from a vendor)' do@@ -50,5 +73,13 @@ module Spreeexpect(subject.source_location).to be_nilexpect(subject.destination_location).to eq destination_locationend++ describe '#validations' do+ it 'checks if source location and destination location are the same' do+ expect(described_class.new(source_location: source_location, destination_location: source_location)).to be_invalid+ expect(described_class.new(source_location: source_location, destination_location: destination_location)).to be_valid+ expect(described_class.new(destination_location: destination_location)).to be_valid+ end+ endendend