From 12b66d82ab75805474c069e08934cf01430193e5 Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Mon, 8 Mar 2021 11:25:52 -0800 Subject: [PATCH 1/3] verify line item when placing sub order This removes the inventory unit from the shipment manifest, so that the item no longer appears on the order view in the admin view. --- app/jobs/subscription_placement_job.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/jobs/subscription_placement_job.rb b/app/jobs/subscription_placement_job.rb index ee4369c1d9..432ddc021a 100644 --- a/app/jobs/subscription_placement_job.rb +++ b/app/jobs/subscription_placement_job.rb @@ -62,6 +62,7 @@ class SubscriptionPlacementJob < ActiveJob::Base unavailable_stock_lines_for(order).each do |line_item| changes[line_item.id] = changes[line_item.id] || line_item.quantity line_item.update(quantity: 0) + Spree::OrderInventory.new(order).verify(line_item) end changes end From 70b7143e7b7dfba31b6106ecfcaca911470195aa Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Wed, 17 Mar 2021 11:33:27 -0700 Subject: [PATCH 2/3] reload line items and recalculate fees after removing line item --- app/jobs/subscription_placement_job.rb | 7 ++++++- app/models/spree/order_inventory.rb | 3 +-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/jobs/subscription_placement_job.rb b/app/jobs/subscription_placement_job.rb index 432ddc021a..4cd02392a4 100644 --- a/app/jobs/subscription_placement_job.rb +++ b/app/jobs/subscription_placement_job.rb @@ -62,7 +62,12 @@ class SubscriptionPlacementJob < ActiveJob::Base unavailable_stock_lines_for(order).each do |line_item| changes[line_item.id] = changes[line_item.id] || line_item.quantity line_item.update(quantity: 0) - Spree::OrderInventory.new(order).verify(line_item) + + Spree::OrderInventory.new(order).verify(line_item, order.shipment) + end + if changes.present? + order.line_items.reload + order.update_order_fees! end changes end diff --git a/app/models/spree/order_inventory.rb b/app/models/spree/order_inventory.rb index 8416bb4755..44ff226609 100644 --- a/app/models/spree/order_inventory.rb +++ b/app/models/spree/order_inventory.rb @@ -98,8 +98,7 @@ module Spree inventory_unit.destroy removed_quantity += 1 end - - shipment.destroy if shipment.inventory_units.count == 0 + shipment.destroy if shipment.inventory_units.reload.count == 0 # removing this from shipment, and adding to stock_location if order.completed? From 577401fca030af161ad449d5ee292265cadba1f2 Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Thu, 18 Mar 2021 09:18:46 -0700 Subject: [PATCH 3/3] add regression spec --- spec/jobs/subscription_placement_job_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/jobs/subscription_placement_job_spec.rb b/spec/jobs/subscription_placement_job_spec.rb index 32a621c436..96e79012a7 100644 --- a/spec/jobs/subscription_placement_job_spec.rb +++ b/spec/jobs/subscription_placement_job_spec.rb @@ -115,6 +115,18 @@ describe SubscriptionPlacementJob do expect(changes[line_item2.id]).to be 3 expect(changes[line_item3.id]).to be 3 end + + context "and the order has been placed" do + before do + allow(order).to receive(:ensure_available_shipping_rates) { true } + allow(order).to receive(:process_each_payment) { true } + job.send(:place_order, order.reload) + end + + it "removes the unavailable items from the shipment" do + expect(order.shipment.manifest.size).to eq 1 + end + end end end end