From 6f4cd135209ae938cb48a9c6aaddd904ffff3e99 Mon Sep 17 00:00:00 2001 From: "Ben A. Morgan" Date: Tue, 4 Jul 2023 21:32:11 -0600 Subject: [PATCH 1/2] mark a shipment as canceled if all the inventory units have been canceled --- core/app/models/spree/order_cancellations.rb | 4 +++- core/app/models/spree/shipment.rb | 21 ++++++++++++------- .../models/spree/order_cancellations_spec.rb | 6 +++--- core/spec/models/spree/shipment_spec.rb | 5 +++++ 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/core/app/models/spree/order_cancellations.rb b/core/app/models/spree/order_cancellations.rb index d708dbd4a87..0b4c65c9558 100644 --- a/core/app/models/spree/order_cancellations.rb +++ b/core/app/models/spree/order_cancellations.rb @@ -118,7 +118,9 @@ def update_shipped_shipments(inventory_units) to_a shipments.each do |shipment| - if shipment.inventory_units.all? { |iu| iu.shipped? || iu.canceled? } + if shipment.inventory_units_canceled? + shipment.update!(state: 'canceled', shipped_at: nil) + elsif shipment.inventory_units_shipped_or_canceled? shipment.update!(state: 'shipped', shipped_at: Time.current) end end diff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rb index 336d76693d1..023bbab72e0 100644 --- a/core/app/models/spree/shipment.rb +++ b/core/app/models/spree/shipment.rb @@ -200,19 +200,24 @@ def selected_shipping_rate_id=(id) # Determines the appropriate +state+ according to the following logic: # - # canceled if order is canceled + # canceled if all of the inventory units are canceled + # shipped if all of the inventory units are either shipped or canceled # pending unless order is complete and +order.payment_state+ is +paid+ - # shipped if already shipped (ie. does not change the state) # ready all other cases def determine_state(order) - return 'canceled' if order.canceled? + return 'canceled' if inventory_units_canceled? || order.canceled? return 'shipped' if shipped? return 'pending' unless order.can_ship? - if can_transition_from_pending_to_ready? - 'ready' - else - 'pending' - end + + can_transition_from_pending_to_ready? ? 'ready' : 'pending' + end + + def inventory_units_canceled? + inventory_units.all?(&:canceled?) + end + + def inventory_units_shipped_or_canceled? + inventory_units.all? { |iu| iu.shipped? || iu.canceled? } end def set_up_inventory(state, variant, _order, line_item) diff --git a/core/spec/models/spree/order_cancellations_spec.rb b/core/spec/models/spree/order_cancellations_spec.rb index 20554304889..9917ca50abb 100644 --- a/core/spec/models/spree/order_cancellations_spec.rb +++ b/core/spec/models/spree/order_cancellations_spec.rb @@ -83,15 +83,15 @@ end it "cancels the inventory unit" do - expect { subject }.to change { inventory_unit.state }.to "canceled" + expect { subject }.to change { inventory_unit.state }.to 'canceled' end it "updates the shipment.state" do - expect { subject }.to change { shipment.reload.state }.from('ready').to('shipped') + expect { subject }.to change { shipment.reload.state }.from('ready').to('canceled') end it "updates the order.shipment_state" do - expect { subject }.to change { order.shipment_state }.from('ready').to('shipped') + expect { subject }.to change { order.shipment_state }.from('ready').to('canceled') end it "adjusts the order" do diff --git a/core/spec/models/spree/shipment_spec.rb b/core/spec/models/spree/shipment_spec.rb index dd09e487bc6..3f5b0d5f5d4 100644 --- a/core/spec/models/spree/shipment_spec.rb +++ b/core/spec/models/spree/shipment_spec.rb @@ -50,6 +50,11 @@ expect(shipment.determine_state(order)).to eq 'canceled' end + it 'returns canceled if all of the inventory units are canceled' do + shipment.inventory_units.each(&:cancel!) + expect(shipment.determine_state(order)).to eql 'canceled' + end + it 'returns pending unless order.can_ship?' do allow(order).to receive_messages can_ship?: false expect(shipment.determine_state(order)).to eq 'pending' From 3bde83e2e0e3fc3c51847aea6fc2067cffe6ed21 Mon Sep 17 00:00:00 2001 From: "Ben A. Morgan" Date: Tue, 4 Jul 2023 21:45:49 -0600 Subject: [PATCH 2/2] backport logic from order cancelation to shipment determine_state for shipped --- core/app/models/spree/shipment.rb | 7 ++++--- core/spec/models/spree/shipment_spec.rb | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rb index 023bbab72e0..2d1626a2697 100644 --- a/core/app/models/spree/shipment.rb +++ b/core/app/models/spree/shipment.rb @@ -201,12 +201,13 @@ def selected_shipping_rate_id=(id) # Determines the appropriate +state+ according to the following logic: # # canceled if all of the inventory units are canceled - # shipped if all of the inventory units are either shipped or canceled + # shipped if already shipped (ie. does not change the state) or all of + # the inventory units are either shipped or canceled # pending unless order is complete and +order.payment_state+ is +paid+ # ready all other cases def determine_state(order) - return 'canceled' if inventory_units_canceled? || order.canceled? - return 'shipped' if shipped? + return 'canceled' if order.canceled? || inventory_units_canceled? + return 'shipped' if shipped? || inventory_units_shipped_or_canceled? return 'pending' unless order.can_ship? can_transition_from_pending_to_ready? ? 'ready' : 'pending' diff --git a/core/spec/models/spree/shipment_spec.rb b/core/spec/models/spree/shipment_spec.rb index 3f5b0d5f5d4..521d224c077 100644 --- a/core/spec/models/spree/shipment_spec.rb +++ b/core/spec/models/spree/shipment_spec.rb @@ -55,6 +55,12 @@ expect(shipment.determine_state(order)).to eql 'canceled' end + it 'returns shipped if some of the inventory units are canceled and some shipped' do + shipment.inventory_units.each(&:cancel!) + create :inventory_unit, shipment: shipment, state: 'shipped' + expect(shipment.determine_state(order)).to eql 'shipped' + end + it 'returns pending unless order.can_ship?' do allow(order).to receive_messages can_ship?: false expect(shipment.determine_state(order)).to eq 'pending'