Class: Spree::FulfilmentChanger
- Inherits:
-
Object
- Object
- Spree::FulfilmentChanger
- Includes:
- ActiveModel::Validations
- Defined in:
- app/models/spree/fulfilment_changer.rb
Overview
Service class to change fulfilment of inventory units of a particular variant to another shipment. The other shipment would typically have a different shipping method, stock location or delivery date, such that we actually change the planned fulfilment for the items in question.
Can be used to merge shipments by moving all items to another shipment, because this class will delete any empty original shipment.
Instance Attribute Summary collapse
-
#current_shipment ⇒ Spree::Shipment
The shipment we transfer units from.
-
#current_stock_location ⇒ Spree::StockLocation
The stock location of the current shipment.
-
#desired_shipment ⇒ Spree::Shipment
The shipment we want to move units onto.
-
#desired_stock_location ⇒ Spree::StockLocation
The stock location of the desired shipment.
-
#quantity ⇒ Integer
How many units we want to move.
-
#track_inventory ⇒ Object
readonly
Returns the value of attribute track_inventory.
-
#variant ⇒ Spree::Variant
We only move units that represent this variant.
Instance Method Summary collapse
-
#initialize(current_shipment:, desired_shipment:, variant:, quantity:, track_inventory:) ⇒ FulfilmentChanger
constructor
A new instance of FulfilmentChanger.
-
#run! ⇒ true, false
Performs the change of fulfilment.
Constructor Details
#initialize(current_shipment:, desired_shipment:, variant:, quantity:, track_inventory:) ⇒ FulfilmentChanger
Returns a new instance of FulfilmentChanger.
25 26 27 28 29 30 31 32 33 |
# File 'app/models/spree/fulfilment_changer.rb', line 25 def initialize(current_shipment:, desired_shipment:, variant:, quantity:, track_inventory:) @current_shipment = current_shipment @desired_shipment = desired_shipment @current_stock_location = current_shipment.stock_location @desired_stock_location = desired_shipment.stock_location @variant = variant @quantity = quantity @track_inventory = track_inventory end |
Instance Attribute Details
#current_shipment ⇒ Spree::Shipment
The shipment we transfer units from
19 20 21 |
# File 'app/models/spree/fulfilment_changer.rb', line 19 def current_shipment @current_shipment end |
#current_stock_location ⇒ Spree::StockLocation
The stock location of the current shipment
19 20 21 |
# File 'app/models/spree/fulfilment_changer.rb', line 19 def current_stock_location @current_stock_location end |
#desired_shipment ⇒ Spree::Shipment
The shipment we want to move units onto
19 20 21 |
# File 'app/models/spree/fulfilment_changer.rb', line 19 def desired_shipment @desired_shipment end |
#desired_stock_location ⇒ Spree::StockLocation
The stock location of the desired shipment
19 20 21 |
# File 'app/models/spree/fulfilment_changer.rb', line 19 def desired_stock_location @desired_stock_location end |
#quantity ⇒ Integer
How many units we want to move
19 20 21 |
# File 'app/models/spree/fulfilment_changer.rb', line 19 def quantity @quantity end |
#track_inventory ⇒ Object (readonly)
Returns the value of attribute track_inventory.
23 24 25 |
# File 'app/models/spree/fulfilment_changer.rb', line 23 def track_inventory @track_inventory end |
#variant ⇒ Spree::Variant
We only move units that represent this variant
19 20 21 |
# File 'app/models/spree/fulfilment_changer.rb', line 19 def variant @variant end |
Instance Method Details
#run! ⇒ true, false
Performs the change of fulfilment
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/models/spree/fulfilment_changer.rb', line 43 def run! # Validations here are intended to catch all necessary prerequisites. # We return early so all checks have happened already. return false if invalid? desired_shipment.save! if desired_shipment.new_record? if track_inventory run_tracking_inventory else run_without_tracking_inventory end # We modified the inventory units at the database level for speed reasons. # The downside of that is that we need to reload the associations. current_shipment.inventory_units.reload desired_shipment.inventory_units.reload # If the current shipment now has no inventory units left, we won't need it any longer. if current_shipment.inventory_units.length.zero? current_shipment.destroy! else # The current shipment has changed, so we need to make sure that shipping rates # have the correct amount. current_shipment.refresh_rates end # The desired shipment has also change, so we need to make sure shipping rates # are up-to-date, too. desired_shipment.refresh_rates # In order to reflect the changes in the order totals desired_shipment.order.reload desired_shipment.order.recalculate true end |