From 7a03f57da0fed481070ceb5db8896a89e589048a Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Wed, 1 Jul 2020 17:48:08 +0100 Subject: [PATCH] Merge shipment decorator with class brought from spree_core --- app/controllers/spree/orders_controller.rb | 2 +- app/models/spree/shipment.rb | 25 ++++++++++++- app/models/spree/shipment_decorator.rb | 41 ---------------------- 3 files changed, 25 insertions(+), 43 deletions(-) delete mode 100644 app/models/spree/shipment_decorator.rb diff --git a/app/controllers/spree/orders_controller.rb b/app/controllers/spree/orders_controller.rb index 85939fe1ad..545ae1ce60 100644 --- a/app/controllers/spree/orders_controller.rb +++ b/app/controllers/spree/orders_controller.rb @@ -166,7 +166,7 @@ module Spree # recalculates the shipment taxes def update_totals_and_taxes @order.updater.update_totals - @order.shipment&.ensure_correct_adjustment_with_included_tax + @order.shipment&.ensure_correct_adjustment end # Sets the adjustments to open to perform the block's action and restores diff --git a/app/models/spree/shipment.rb b/app/models/spree/shipment.rb index f3b0b03f0c..c5fea38d2c 100644 --- a/app/models/spree/shipment.rb +++ b/app/models/spree/shipment.rb @@ -159,13 +159,18 @@ module Spree end def manifest - inventory_units.joins(:variant).includes(:variant).group_by(&:variant).map do |variant, units| + inventory_units.group_by(&:variant).map do |variant, units| states = {} units.group_by(&:state).each { |state, iu| states[state] = iu.count } + scoper.scope(variant) OpenStruct.new(variant: variant, quantity: units.length, states: states) end end + def scoper + @scoper ||= OpenFoodNetwork::ScopeVariantToHub.new(order.distributor) + end + def line_items if order.complete? && Spree::Config[:track_inventory_levels] order.line_items.select { |li| inventory_units.pluck(:variant_id).include?(li.variant_id) } @@ -295,10 +300,28 @@ module Spree "open") reload # ensure adjustment is present on later saves end + + update_adjustment_included_tax if adjustment + end + + def update_adjustment_included_tax + if Config.shipment_inc_vat && (order.distributor.nil? || order.distributor.charges_sales_tax) + adjustment.set_included_tax! Config.shipping_tax_rate + else + adjustment.set_included_tax! 0 + end end def update_order order.update! end + + # NOTE: This is an override of spree's method, needed to allow orders + # without line items (ie. user invoices) to not have inventory units + def require_inventory + return false unless line_items.count > 0 # This line altered + + order.completed? && !order.canceled? + end end end diff --git a/app/models/spree/shipment_decorator.rb b/app/models/spree/shipment_decorator.rb deleted file mode 100644 index b23bd7266f..0000000000 --- a/app/models/spree/shipment_decorator.rb +++ /dev/null @@ -1,41 +0,0 @@ -module Spree - Shipment.class_eval do - def ensure_correct_adjustment_with_included_tax - ensure_correct_adjustment_without_included_tax - - update_adjustment_included_tax if adjustment - end - alias_method_chain :ensure_correct_adjustment, :included_tax - - def update_adjustment_included_tax - if Config.shipment_inc_vat && (order.distributor.nil? || order.distributor.charges_sales_tax) - adjustment.set_included_tax! Config.shipping_tax_rate - else - adjustment.set_included_tax! 0 - end - end - - def manifest - inventory_units.group_by(&:variant).map do |variant, units| - states = {} - units.group_by(&:state).each { |state, iu| states[state] = iu.count } - scoper.scope(variant) - OpenStruct.new(variant: variant, quantity: units.length, states: states) - end - end - - def scoper - @scoper ||= OpenFoodNetwork::ScopeVariantToHub.new(order.distributor) - end - - private - - # NOTE: This is an override of spree's method, needed to allow orders - # without line items (ie. user invoices) to not have inventory units - def require_inventory - return false unless line_items.count > 0 # This line altered - - order.completed? && !order.canceled? - end - end -end