diff --git a/app/models/stock/package.rb b/app/models/stock/package.rb deleted file mode 100644 index eada3741c1..0000000000 --- a/app/models/stock/package.rb +++ /dev/null @@ -1,45 +0,0 @@ -# Extends Spree's Package implementation to skip shipping methods that are not -# valid for OFN. -# -# It requires the following configuration in config/initializers/spree.rb: -# -# Spree.config do |config| -# ... -# config.package_factory = Stock::Package -# end -# -module Stock - class Package < Spree::Stock::Package - # Returns all existing shipping categories. - # It does not filter by the shipping categories of the products in the order. - # It allows checkout of products with categories that are not the shipping methods categories - # It disables the matching of product shipping category with shipping method's category - # - # @return [Array] - def shipping_categories - Spree::ShippingCategory.all - end - - # Skips the methods that are not used by the order's distributor - # - # @return [Array] - def shipping_methods - available_shipping_methods = super.to_a - - available_shipping_methods.keep_if do |shipping_method| - ships_with?(order.distributor.shipping_methods.to_a, shipping_method) - end - end - - private - - # Checks whether the given distributor provides the specified shipping method - # - # @param shipping_methods [Array] - # @param shipping_method [Spree::ShippingMethod] - # @return [Boolean] - def ships_with?(shipping_methods, shipping_method) - shipping_methods.include?(shipping_method) - end - end -end diff --git a/config/initializers/spree.rb b/config/initializers/spree.rb index 5aca0161f8..d331a0567f 100644 --- a/config/initializers/spree.rb +++ b/config/initializers/spree.rb @@ -30,7 +30,7 @@ Spree.config do |config| config.auto_capture = true #config.override_actionmailer_config = false - config.package_factory = Stock::Package + config.package_factory = OrderManagement::Stock::Package config.order_updater_decorator = OrderUpdater # S3 settings diff --git a/engines/order_management/app/services/order_management/stock/package.rb b/engines/order_management/app/services/order_management/stock/package.rb new file mode 100644 index 0000000000..6397c6d90e --- /dev/null +++ b/engines/order_management/app/services/order_management/stock/package.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +# Extends Spree's Package implementation to skip shipping methods that are not +# valid for OFN. +# +# It requires the following configuration in config/initializers/spree.rb: +# +# Spree.config do |config| +# ... +# config.package_factory = OrderManagement::Stock::Package +# end +# +module OrderManagement + module Stock + class Package < Spree::Stock::Package + # Returns all existing shipping categories. + # It does not filter by the shipping categories of the products in the order: it allows + # checkout of products with categories that are not the shipping method's categories + # It disables the matching of product shipping category with shipping method's category + # + # @return [Array] + def shipping_categories + Spree::ShippingCategory.all + end + + # Skips the methods that are not used by the order's distributor + # + # @return [Array] + def shipping_methods + available_shipping_methods = super.to_a + + available_shipping_methods.keep_if do |shipping_method| + ships_with?(order.distributor.shipping_methods.to_a, shipping_method) + end + end + + private + + # Checks whether the given distributor provides the specified shipping method + # + # @param shipping_methods [Array] + # @param shipping_method [Spree::ShippingMethod] + # @return [Boolean] + def ships_with?(shipping_methods, shipping_method) + shipping_methods.include?(shipping_method) + end + end + end +end diff --git a/engines/order_management/spec/services/order_management/stock/package_spec.rb b/engines/order_management/spec/services/order_management/stock/package_spec.rb new file mode 100644 index 0000000000..dd06edb775 --- /dev/null +++ b/engines/order_management/spec/services/order_management/stock/package_spec.rb @@ -0,0 +1,60 @@ +require 'spec_helper' + +module OrderManagement + module Stock + describe Package do + let(:stock_location) { double(:stock_location) } + + subject(:package) { Package.new(stock_location, order, contents) } + + let(:enterprise) { create(:enterprise) } + let(:other_enterprise) { create(:enterprise) } + + let(:order) { build(:order, distributor: enterprise) } + + let(:variant1) do + instance_double( + Spree::Variant, + shipping_category: shipping_method1.shipping_categories.first + ) + end + let(:variant2) do + instance_double( + Spree::Variant, + shipping_category: shipping_method2.shipping_categories.first + ) + end + let(:variant3) do + instance_double(Spree::Variant, shipping_category: nil) + end + + let(:contents) do + [ + Package::ContentItem.new(variant1, 1), + Package::ContentItem.new(variant1, 1), + Package::ContentItem.new(variant2, 1), + Package::ContentItem.new(variant3, 1) + ] + end + + let(:shipping_method1) { create(:shipping_method, distributors: [enterprise]) } + let(:shipping_method2) { create(:shipping_method, distributors: [other_enterprise]) } + + describe '#shipping_methods' do + it 'does not return shipping methods not used by the package\'s order distributor' do + expect(package.shipping_methods).to eq [shipping_method1] + end + end + + describe '#shipping_categories' do + it "returns shipping categories that are not shipping categories of the order's products" do + package + other_shipping_category = Spree::ShippingCategory.create(name: "Custom") + + expect(package.shipping_categories).to eq [shipping_method1.shipping_categories.first, + other_shipping_category] + end + end + end + end +end diff --git a/spec/models/spree/calculator/flat_percent_item_total_spec.rb b/spec/models/spree/calculator/flat_percent_item_total_spec.rb index 529c2b11d9..ec4b54fc70 100644 --- a/spec/models/spree/calculator/flat_percent_item_total_spec.rb +++ b/spec/models/spree/calculator/flat_percent_item_total_spec.rb @@ -14,7 +14,7 @@ describe Spree::Calculator::FlatPercentItemTotal do it_behaves_like "a model using the LocalizedNumber module", [:preferred_flat_percent] end - it "computes amount correctly for a given Stock::Package" do + it "computes amount correctly for a given OrderManagement::Stock::Package" do order = double(:order, line_items: [line_item] ) package = double(:package, order: order) diff --git a/spec/models/stock/package_spec.rb b/spec/models/stock/package_spec.rb deleted file mode 100644 index 6a3ac355cc..0000000000 --- a/spec/models/stock/package_spec.rb +++ /dev/null @@ -1,58 +0,0 @@ -require 'spec_helper' - -module Stock - describe Package do - let(:stock_location) { double(:stock_location) } - - subject(:package) { Package.new(stock_location, order, contents) } - - let(:enterprise) { create(:enterprise) } - let(:other_enterprise) { create(:enterprise) } - - let(:order) { build(:order, distributor: enterprise) } - - let(:variant1) do - instance_double( - Spree::Variant, - shipping_category: shipping_method1.shipping_categories.first - ) - end - let(:variant2) do - instance_double( - Spree::Variant, - shipping_category: shipping_method2.shipping_categories.first - ) - end - let(:variant3) do - instance_double(Spree::Variant, shipping_category: nil) - end - - let(:contents) do - [ - Package::ContentItem.new(variant1, 1), - Package::ContentItem.new(variant1, 1), - Package::ContentItem.new(variant2, 1), - Package::ContentItem.new(variant3, 1) - ] - end - - let(:shipping_method1) { create(:shipping_method, distributors: [enterprise]) } - let(:shipping_method2) { create(:shipping_method, distributors: [other_enterprise]) } - - describe '#shipping_methods' do - it 'does not return shipping methods not used by the package\'s order distributor' do - expect(package.shipping_methods).to eq [shipping_method1] - end - end - - describe '#shipping_categories' do - it "returns shipping categories that are not shipping categories of the order's products" do - package - other_shipping_category = Spree::ShippingCategory.create(name: "Custom") - - expect(package.shipping_categories).to eq [shipping_method1.shipping_categories.first, - other_shipping_category] - end - end - end -end