From 63241b8b329cef2ddfe91e9f782b2a2cb36f1de1 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Wed, 13 Apr 2022 11:59:43 +0200 Subject: [PATCH] Update in-memory shipments of order in order_shipping Since we update the shipments state in the database, but we read the shipsments from memory in the OrderUpdater#determine_shipment_state we need to set the new values here on the object as well. Co-authored-by: darkswoop --- core/app/models/spree/order_shipping.rb | 15 ++++++--------- core/spec/models/spree/order_shipping_spec.rb | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/core/app/models/spree/order_shipping.rb b/core/app/models/spree/order_shipping.rb index 5aa84375a97..15b23cda18a 100644 --- a/core/app/models/spree/order_shipping.rb +++ b/core/app/models/spree/order_shipping.rb @@ -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 diff --git a/core/spec/models/spree/order_shipping_spec.rb b/core/spec/models/spree/order_shipping_spec.rb index 9f481215a53..ec60f5ae615 100644 --- a/core/spec/models/spree/order_shipping_spec.rb +++ b/core/spec/models/spree/order_shipping_spec.rb @@ -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(