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.

Add method to calculate total item quantity in shipment (#12944)

Dimitrios Panagkasidis2684ef56a6aa
diff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rb
index a0166e91be..ebccc9a1c6 100644
--- a/core/app/models/spree/shipment.rb
+++ b/core/app/models/spree/shipment.rb
@@ -221,6 +221,11 @@ module Spree
inventory_units.where(line_item_id: line_item.id, variant_id: line_item.variant_id || variant.id)
end
+ # Returns the total quantity of all line items in the shipment
+ def item_quantity
+ manifest.sum(&:quantity)
+ end
+
# Returns the cost of the shipment
#
# @return [BigDecimal]
diff --git a/core/spec/models/spree/shipment_spec.rb b/core/spec/models/spree/shipment_spec.rb
index bb6b9780cb..9c1b040399 100644
--- a/core/spec/models/spree/shipment_spec.rb
+++ b/core/spec/models/spree/shipment_spec.rb
@@ -203,6 +203,54 @@ describe Spree::Shipment, type: :model do
end
end
+ describe '#item_quantity' do
+ it 'returns the sum of all manifest quantities with multiple quantities per line_item' do
+ order = create(:order)
+ variant1 = create(:variant)
+ variant2 = create(:variant)
+ create(:line_item, order: order, variant: variant1, quantity: 3)
+ create(:line_item, order: order, variant: variant2, quantity: 2)
+ shipment = create(:shipment, order: order)
+ expect(shipment.item_quantity).to eq(5)
+ end
+
+ it 'returns the sum of all manifest quantities with single quantity per line_item' do
+ order = create(:order)
+ variant1 = create(:variant)
+ variant2 = create(:variant)
+ create(:line_item, order: order, variant: variant1, quantity: 1)
+ create(:line_item, order: order, variant: variant2, quantity: 1)
+ shipment = create(:shipment, order: order)
+ expect(shipment.item_quantity).to eq(2)
+ end
+
+ it 'returns only the sum of items in the specific shipment, not in other shipments' do
+ order = create(:order)
+ variant1 = create(:variant)
+ variant2 = create(:variant)
+ line_item1 = create(:line_item, order: order, variant: variant1, quantity: 2)
+ line_item2 = create(:line_item, order: order, variant: variant2, quantity: 4)
+
+ # First shipment for line_item1
+ shipment1 = create(:shipment, order: order)
+ shipment1.inventory_units.destroy_all
+ shipment1.set_up_inventory('on_hand', variant1, order, line_item1, 2)
+
+ # Second shipment for line_item2
+ shipment2 = create(:shipment, order: order)
+ shipment2.inventory_units.destroy_all
+ shipment2.set_up_inventory('on_hand', variant2, order, line_item2, 4)
+
+ expect(shipment1.item_quantity).to eq(2)
+ expect(shipment2.item_quantity).to eq(4)
+ end
+
+ it 'returns 0 if there are no items in the shipment' do
+ shipment = create(:shipment)
+ expect(shipment.item_quantity).to eq(0)
+ end
+ end
+
describe '#item_weight' do
it 'equals line items weight' do
order = create(:order)