diff --git a/app/controllers/spree/admin/orders/customer_details_controller.rb b/app/controllers/spree/admin/orders/customer_details_controller.rb index 8ad0c7ea2860..6d697958d23e 100644 --- a/app/controllers/spree/admin/orders/customer_details_controller.rb +++ b/app/controllers/spree/admin/orders/customer_details_controller.rb @@ -25,7 +25,9 @@ def update OrderWorkflow.new(@order).complete - @order.shipments.map(&:refresh_rates) + @order.shipments.map { + |shipment| shipment.refresh_rates(checkout: false, apply_tags: false) + } flash[:success] = Spree.t('customer_details_updated') redirect_to admin_order_customer_path(@order) else diff --git a/app/controllers/spree/admin/orders_controller.rb b/app/controllers/spree/admin/orders_controller.rb index 0e3b28d630f0..4f343ace5831 100644 --- a/app/controllers/spree/admin/orders_controller.rb +++ b/app/controllers/spree/admin/orders_controller.rb @@ -33,7 +33,9 @@ def new end def edit - @order.shipments.map(&:refresh_rates) + @order.shipments.map { + |shipment| shipment.refresh_rates(checkout: false, apply_tags: false) + } OrderWorkflow.new(@order).complete diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index bf125da21d5b..5497cfd8bc16 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -97,7 +97,9 @@ def create_proposed_shipments adjustments.shipping.delete_all shipments.destroy_all - packages = OrderManagement::Stock::Coordinator.new(self).packages + packages = OrderManagement::Stock::Coordinator.new(self).packages( + checkout: true, apply_tags: true + ) packages.each do |package| shipments << package.to_shipment end diff --git a/app/models/spree/shipment.rb b/app/models/spree/shipment.rb index 58ab022a05d1..17a3803b73b8 100644 --- a/app/models/spree/shipment.rb +++ b/app/models/spree/shipment.rb @@ -103,13 +103,13 @@ def selected_shipping_rate_id=(id) save! end - def refresh_rates(apply_tags = true) + def refresh_rates(checkout: true, apply_tags: true) return shipping_rates if shipped? # The call to Stock::Estimator below will replace the current shipping_method original_shipping_method_id = shipping_method.try(:id) - self.shipping_rates = - OrderManagement::Stock::Estimator.new(order).shipping_rates(to_package, true, apply_tags) + self.shipping_rates = OrderManagement::Stock::Estimator.new(order).shipping_rates( + package: to_package, checkout: checkout, apply_tags: apply_tags) keep_original_shipping_method_selection(original_shipping_method_id) diff --git a/app/services/distributor_shipping_methods.rb b/app/services/distributor_shipping_methods.rb index cb589ba97e7b..492fa40ea0ea 100644 --- a/app/services/distributor_shipping_methods.rb +++ b/app/services/distributor_shipping_methods.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class DistributorShippingMethods - def self.shipping_methods(distributor:, checkout: false, apply_tags: true, customer: nil) + def self.shipping_methods(distributor:, customer: nil, checkout: false, apply_tags: true) return [] if distributor.blank? shipping_methods = distributor.shipping_methods diff --git a/engines/order_management/app/services/order_management/stock/coordinator.rb b/engines/order_management/app/services/order_management/stock/coordinator.rb index 64bbbc8cbc90..e64c77bdb5fd 100644 --- a/engines/order_management/app/services/order_management/stock/coordinator.rb +++ b/engines/order_management/app/services/order_management/stock/coordinator.rb @@ -9,10 +9,10 @@ def initialize(order) @order = order end - def packages + def packages(checkout: true, apply_tags: true) packages = build_packages packages = prioritize_packages(packages) - estimate_packages(packages) + estimate_packages(packages: packages, checkout: checkout, apply_tags: apply_tags) end # Build package with default stock location @@ -32,10 +32,12 @@ def prioritize_packages(packages) prioritizer.prioritized_packages end - def estimate_packages(packages) + def estimate_packages(packages:, checkout: true, apply_tags: true) estimator = OrderManagement::Stock::Estimator.new(order) packages.each do |package| - package.shipping_rates = estimator.shipping_rates(package) + package.shipping_rates = estimator.shipping_rates( + package: package, checkout: checkout, apply_tags: apply_tags + ) end packages end diff --git a/engines/order_management/app/services/order_management/stock/estimator.rb b/engines/order_management/app/services/order_management/stock/estimator.rb index 03a1fb469720..b342ed700275 100644 --- a/engines/order_management/app/services/order_management/stock/estimator.rb +++ b/engines/order_management/app/services/order_management/stock/estimator.rb @@ -10,9 +10,10 @@ def initialize(order) @currency = order.currency end - def shipping_rates(package, frontend_only = true, apply_tags = true) + def shipping_rates(package:, checkout: true, apply_tags: true) shipping_rates = [] - shipping_methods = shipping_methods(package, apply_tags) + shipping_methods = + shipping_methods(package: package, checkout: checkout, apply_tags: apply_tags) return [] unless shipping_methods shipping_methods.each do |shipping_method| @@ -23,7 +24,7 @@ def shipping_rates(package, frontend_only = true, apply_tags = true) shipping_rates.sort_by! { |r| r.cost || 0 } unless shipping_rates.empty? - if frontend_only + if checkout shipping_rates.each do |rate| if rate.shipping_method.frontend? rate.selected = true @@ -40,12 +41,8 @@ def shipping_rates(package, frontend_only = true, apply_tags = true) private - def shipping_methods(package, apply_tags) - shipping_methods = if apply_tags - package.filtered_shipping_methods - else - package.shipping_methods - end + def shipping_methods(package:, checkout: true, apply_tags: true) + shipping_methods = package.shipping_methods(checkout: checkout, apply_tags: apply_tags) shipping_methods.delete_if { |ship_method| !ship_method.calculator.available?(package) } shipping_methods.delete_if { |ship_method| !ship_method.include?(order.ship_address) } shipping_methods.delete_if { |ship_method| diff --git a/engines/order_management/app/services/order_management/stock/package.rb b/engines/order_management/app/services/order_management/stock/package.rb index fb6e25bc28a1..f1dfc37193b7 100644 --- a/engines/order_management/app/services/order_management/stock/package.rb +++ b/engines/order_management/app/services/order_management/stock/package.rb @@ -79,21 +79,12 @@ def currency # TODO calculate from first variant? end - def shipping_methods + def shipping_methods(checkout: true, apply_tags: true) DistributorShippingMethods.shipping_methods( distributor: order.distributor, - checkout: false, + checkout: checkout, customer: order.customer, - apply_tags: false - ) - end - - def filtered_shipping_methods - DistributorShippingMethods.shipping_methods( - distributor: order.distributor, - checkout: false, - customer: order.customer, - apply_tags: true + apply_tags: apply_tags ) end