diff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rb index 434a1d12f24..853accf1a2a 100644 --- a/core/app/models/spree/shipment.rb +++ b/core/app/models/spree/shipment.rb @@ -184,7 +184,7 @@ def inventory_units_for_item(line_item, variant = nil) end def item_cost - line_items.map(&:total).sum + manifest.sum(&:item_cost) end def ready_or_pending? diff --git a/core/app/models/spree/shipping_manifest.rb b/core/app/models/spree/shipping_manifest.rb index 0d87fcbb4b0..0e25c10cd5e 100644 --- a/core/app/models/spree/shipping_manifest.rb +++ b/core/app/models/spree/shipping_manifest.rb @@ -1,7 +1,11 @@ # frozen_string_literal: true class Spree::ShippingManifest - ManifestItem = Struct.new(:line_item, :variant, :quantity, :states) + ManifestItem = Struct.new(:line_item, :variant, :quantity, :states) do + def item_cost + (line_item.price + (line_item.adjustment_total / line_item.quantity)) * quantity + end + end def initialize(inventory_units:) @inventory_units = inventory_units.to_a diff --git a/core/spec/models/spree/shipment_spec.rb b/core/spec/models/spree/shipment_spec.rb index 214e0f1c580..bed4dbfd4bd 100644 --- a/core/spec/models/spree/shipment_spec.rb +++ b/core/spec/models/spree/shipment_spec.rb @@ -141,6 +141,26 @@ it 'should equal line items final amount with tax' do expect(shipment.item_cost).to eql(11.0) end + + context 'with more shipments for the same line_item' do + let(:order) do + create( + :order_with_line_items, + line_items_attributes: [{ price: 10, variant: variant, quantity: 2 }], + ship_address: ship_address, + ) + end + let(:other_shipment) { order.shipments.create(stock_location_id: shipment.stock_location) } + + it 'returns shipment line items amount with tax' do + expect(order.shipments.first.item_cost).to eql(22.0) + expect { + order.inventory_units.last.update(shipment_id: other_shipment.id) + }.to change { order.shipments.reload.first.item_cost }.from(22.0).to(11.0) + + expect(order.shipments.second.item_cost).to eql(11.0) + end + end end it "#discounted_cost" do