Skip to content

Commit

Permalink
Sum Spree::ShippingManifest::ManifestItem.item_cost
Browse files Browse the repository at this point in the history
This commit is going to add `item_cost` calculation, all these item costs will be summed.
  • Loading branch information
DanielePalombo committed Feb 1, 2019
1 parent aaa2411 commit 8f7e971
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/app/models/spree/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
6 changes: 5 additions & 1 deletion core/app/models/spree/shipping_manifest.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 20 additions & 0 deletions core/spec/models/spree/shipment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8f7e971

Please sign in to comment.