Class: Spree::OrderShipping
- Inherits:
-
Object
- Object
- Spree::OrderShipping
- Defined in:
- app/models/spree/order_shipping.rb
Overview
A service layer that handles generating Carton objects when inventory units are actually shipped. It also takes care of things like updating order and shipment states and delivering shipment emails as needed.
Instance Method Summary collapse
-
#create_proposed_shipments ⇒ Array<Spree::Shipment>
A method that destroys the current shipments on an order, creates new shipments for the line items, and assigns them to the order.
-
#initialize(order) ⇒ OrderShipping
constructor
A new instance of OrderShipping.
-
#ship(inventory_units:, stock_location:, address:, shipping_method:, shipped_at: Time.current, external_number: nil, tracking_number: nil, suppress_mailer: false) ⇒ Object
Generate a carton from the supplied inventory units and marks those units as shipped.
-
#ship_shipment(shipment, external_number: nil, tracking_number: nil, suppress_mailer: false) ⇒ Object
A shortcut method that ships all inventory units in a shipment in a single carton.
Constructor Details
#initialize(order) ⇒ OrderShipping
Returns a new instance of OrderShipping.
7 8 9 |
# File 'app/models/spree/order_shipping.rb', line 7 def initialize(order) @order = order end |
Instance Method Details
#create_proposed_shipments ⇒ Array<Spree::Shipment>
A method that destroys the current shipments on an order, creates new shipments for the line items, and assigns them to the order.
17 18 19 20 21 22 23 24 25 26 |
# File 'app/models/spree/order_shipping.rb', line 17 def create_proposed_shipments if @order.completed? raise Spree::Order::CannotRebuildShipments.new(I18n.t("spree.cannot_rebuild_shipments_order_completed")) elsif @order.shipments.any? { |shipment| !shipment.pending? } raise Spree::Order::CannotRebuildShipments.new(I18n.t("spree.cannot_rebuild_shipments_shipments_not_pending")) else @order.shipments.destroy_all @order.shipments.push(*Spree::Config.stock.coordinator_class.new(@order).shipments) end end |
#ship(inventory_units:, stock_location:, address:, shipping_method:, shipped_at: Time.current, external_number: nil, tracking_number: nil, suppress_mailer: false) ⇒ Object
Generate a carton from the supplied inventory units and marks those units as shipped. Also sends shipment emails if appropriate and updates shipment_states for associated orders.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/models/spree/order_shipping.rb', line 62 def ship(inventory_units:, stock_location:, address:, shipping_method:, shipped_at: Time.current, external_number: nil, tracking_number: nil, suppress_mailer: false) carton = nil Spree::InventoryUnit.transaction do inventory_units.each(&:ship!) carton = Spree::Carton.create!( stock_location:, address:, shipping_method:, inventory_units:, shipped_at:, external_number:, suppress_email: suppress_mailer, tracking: tracking_number ) end inventory_units.map(&:shipment).uniq.each do |shipment| if shipment.inventory_units.reload.all? { |iu| iu.shipped? || iu.canceled? } shipment.update!(state: "shipped", shipped_at: Time.current, tracking: tracking_number) else shipment.update!(tracking: tracking_number) end end @order.shipments.reload @order.recalculate Spree::Bus.publish(:carton_shipped, carton:) carton end |
#ship_shipment(shipment, external_number: nil, tracking_number: nil, suppress_mailer: false) ⇒ Object
A shortcut method that ships all inventory units in a shipment in a single carton. See also #ship.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/models/spree/order_shipping.rb', line 35 def ship_shipment(shipment, external_number: nil, tracking_number: nil, suppress_mailer: false) ship( inventory_units: shipment.inventory_units.shippable, stock_location: shipment.stock_location, address: shipment.order.ship_address, shipping_method: shipment.shipping_method, shipped_at: Time.current, external_number:, # TODO: Remove the `|| shipment.tracking` once Shipment#ship! is called by # OrderShipping#ship rather than vice versa tracking_number: tracking_number || shipment.tracking, suppress_mailer: ) end |