Skip to content

Commit c416047

Browse files
Fix Spree::Shipment#item_cost for split shipments
This commit is going to fix the bug reported here solidusio#2919. Now when shipment is split, it creates two shipment but associated line_item has quantity 2 which is resulting in incorrect item_cost because it is taken from line_item not from shipment. Shipment manifest know the right quantity for every shipment, use it to calculate the item_cost as sum of `(single_line item + weighted adjustment per line item) * quantity`
1 parent 622aa25 commit c416047

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

core/app/models/spree/shipment.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def inventory_units_for_item(line_item, variant = nil)
186186
end
187187

188188
def item_cost
189-
line_items.map(&:total).sum
189+
manifest.map { |m| (m.line_item.price + (m.line_item.adjustment_total / m.line_item.quantity)) * m.quantity }.sum
190190
end
191191

192192
def ready_or_pending?

core/spec/models/spree/shipment_spec.rb

+20
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,26 @@
141141
it 'should equal line items final amount with tax' do
142142
expect(shipment.item_cost).to eql(11.0)
143143
end
144+
145+
context 'whit more shipments for the same line_item' do
146+
let(:order) do
147+
create(
148+
:order_with_line_items,
149+
line_items_attributes: [{ price: 10, variant: variant, quantity: 2 }],
150+
ship_address: ship_address,
151+
)
152+
end
153+
let(:other_shipment) { order.shipments.create(stock_location_id: shipment.stock_location) }
154+
155+
it 'should equal shipment line items amount with tax' do
156+
expect(order.shipments.first.item_cost).to eql(22.0)
157+
expect {
158+
order.inventory_units.last.update(shipment_id: other_shipment.id)
159+
}.to change { order.shipments.reload.first.item_cost }.from(22.0).to(11.0)
160+
161+
expect(order.shipments.second.item_cost).to eql(11.0)
162+
end
163+
end
144164
end
145165

146166
it "#discounted_cost" do

0 commit comments

Comments
 (0)