Skip to content

Commit 1d321f2

Browse files
Sum Spree::ShippingManifest::ManifestItem.item_cost
This commit is going to add `item_cost` calculation, all these item costs will be summed.
1 parent aaa2411 commit 1d321f2

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

core/app/models/spree/shipment.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def inventory_units_for_item(line_item, variant = nil)
184184
end
185185

186186
def item_cost
187-
line_items.map(&:total).sum
187+
manifest.sum(&:item_cost)
188188
end
189189

190190
def ready_or_pending?

core/app/models/spree/shipping_manifest.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# frozen_string_literal: true
22

33
class Spree::ShippingManifest
4-
ManifestItem = Struct.new(:line_item, :variant, :quantity, :states)
4+
ManifestItem = Struct.new(:line_item, :variant, :quantity, :states) do
5+
def item_cost
6+
(line_item.price + (line_item.adjustment_total / line_item.quantity)) * quantity
7+
end
8+
end
59

610
def initialize(inventory_units:)
711
@inventory_units = inventory_units.to_a

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 'with 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 'returns 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)