Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix incorrect inventory unit merge

Nathan Flood9069b099f666
diff --git a/core/app/models/spree/fulfilment_changer.rb b/core/app/models/spree/fulfilment_changer.rb
index 8cfc768194..46471043e7 100644
--- a/core/app/models/spree/fulfilment_changer.rb
+++ b/core/app/models/spree/fulfilment_changer.rb
@@ -92,7 +92,7 @@ module Spree
end
def get_desired_shipment_inventory_unit(state)
- desired_shipment.inventory_units.find_or_create_by(state: state) do |unit|
+ desired_shipment.inventory_units.find_or_create_by(state: state, variant: variant) do |unit|
current_shipment_unit = current_shipment_units.first
unit.variant_id = current_shipment_unit.variant_id
unit.order_id = current_shipment_unit.order_id
diff --git a/core/spec/models/spree/fulfilment_changer_spec.rb b/core/spec/models/spree/fulfilment_changer_spec.rb
index e56a7c6437..c5723a3956 100644
--- a/core/spec/models/spree/fulfilment_changer_spec.rb
+++ b/core/spec/models/spree/fulfilment_changer_spec.rb
@@ -38,6 +38,57 @@ describe Spree::FulfilmentChanger do
variant.stock_items.first.update_column(:count_on_hand, 100)
end
+ context 'when the order has multiple line items' do
+ let(:current_shipment_inventory_unit_count) { 3 }
+ let(:second_variant) { create(:variant)}
+ let(:third_variant) { create(:variant)}
+ let(:quantity) { 3 }
+
+
+ let(:order) do
+ create(
+ :completed_order_with_totals,
+ without_line_items: true,
+ line_items_attributes: [
+ {
+ quantity: current_shipment_inventory_unit_count,
+ variant: variant
+ },
+ {
+ quantity: current_shipment_inventory_unit_count,
+ variant: second_variant
+ },
+ {
+ quantity: current_shipment_inventory_unit_count,
+ variant: third_variant
+ }
+ ]
+ )
+ end
+
+ it 'can move more than one variant to the desired shipment' do
+ described_class.new(
+ current_stock_location: current_shipment.stock_location,
+ desired_stock_location: desired_shipment.stock_location,
+ current_shipment: current_shipment,
+ desired_shipment: desired_shipment,
+ variant: variant,
+ quantity: quantity
+ ).run!
+ expect(desired_shipment.inventory_units.count).to eq 1
+ described_class.new(
+ current_stock_location: current_shipment.stock_location,
+ desired_stock_location: desired_shipment.stock_location,
+ current_shipment: current_shipment,
+ desired_shipment: desired_shipment,
+ variant: second_variant,
+ quantity: quantity
+ ).run!
+ expect(current_shipment.inventory_units.count).to eq 1
+ expect(desired_shipment.inventory_units.count).to eq 2
+ end
+ end
+
context 'when the current shipment has enough inventory units' do
let(:current_shipment_inventory_unit_count) { 3 }
let(:quantity) { 1 }