Skip to content

Commit

Permalink
Merge pull request #4336 from tvdeyen/fix-determine-shipment-state-v3.0
Browse files Browse the repository at this point in the history
[v3.0] Update in-memory shipments of order in order_shipping
  • Loading branch information
waiting-for-dev authored Apr 26, 2022
2 parents 8608a5c + 2ae2fd2 commit f37cca8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
15 changes: 6 additions & 9 deletions core/app/models/spree/order_shipping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,15 @@ def ship(inventory_units:, stock_location:, address:, shipping_method:,
end

inventory_units.map(&:shipment).uniq.each do |shipment|
# Temporarily propagate the tracking number to the shipment as well
# TODO: Remove tracking numbers from shipments.
shipment.update!(tracking: tracking_number)

next unless shipment.inventory_units.reload.all? { |iu| iu.shipped? || iu.canceled? }
# TODO: make OrderShipping#ship_shipment call Shipment#ship! rather than
# having Shipment#ship! call OrderShipping#ship_shipment. We only really
# need this `update_columns` for the specs, until we make that change.
shipment.update_columns(state: 'shipped', shipped_at: Time.current)
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

send_shipment_emails(carton) if stock_location.fulfillable? && !suppress_mailer # e.g. digital gift cards that aren't actually shipped
@order.shipments.reload
@order.recalculate

carton
Expand Down
15 changes: 15 additions & 0 deletions core/spec/models/spree/order_shipping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ def emails
end
end

context "when a second shipment is shipped" do
let(:order) { create(:order_ready_to_ship) }

it "sets the order to shipped state" do
order.shipping.ship_shipment(order.shipments.first)

unshipped_shipment = create(:shipment, order: order, state: "ready")

order.reload
order.recalculate

expect { order.shipping.ship_shipment(unshipped_shipment) }.to(change { order.shipment_state }.from("partial").to("shipped"))
end
end

context "when told to suppress the mailer" do
subject do
order.shipping.ship_shipment(
Expand Down

0 comments on commit f37cca8

Please sign in to comment.