Skip to content

Commit

Permalink
Merge pull request #8572 from VzqzAc/splitted-shipment-no-taxes
Browse files Browse the repository at this point in the history
Splitted shipment has no taxes nor cost
  • Loading branch information
bbonislawski committed Feb 14, 2018
2 parents d0292e1 + 4ca9f3e commit 879bb7c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
8 changes: 7 additions & 1 deletion core/app/models/spree/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,15 @@ def refresh_rates(shipping_method_filter = ShippingMethod::DISPLAY_ON_FRONT_END)

if shipping_method
selected_rate = shipping_rates.detect do |rate|
rate.shipping_method_id == original_shipping_method_id
if original_shipping_method_id
rate.shipping_method_id == original_shipping_method_id
else
rate.selected
end
end
save!
self.selected_shipping_rate_id = selected_rate.id if selected_rate
reload
end

shipping_rates
Expand Down Expand Up @@ -340,6 +345,7 @@ def transfer_to_location(variant, quantity, stock_location)

order.contents.remove(variant, quantity, shipment: self)
order.contents.add(variant, quantity, shipment: new_shipment)
order.create_tax_charge!
order.update_with_updater!

refresh_rates
Expand Down
5 changes: 5 additions & 0 deletions core/spec/models/spree/order/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ module Spree

it 'incorporates refunds' do
order = create(:completed_order_with_totals)
calculator = order.shipments.first.shipping_method.calculator

calculator.set_preference(:amount, order.shipments.first.cost)
calculator.save!

order.payments << create(:payment, state: :completed, order: order, amount: order.total)

create(:refund, amount: 10, payment: order.payments.first)
Expand Down
54 changes: 53 additions & 1 deletion core/spec/models/spree/shipment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,12 @@ def perform
payment
end

before do
calculator = @shipment.shipping_method.calculator
calculator.set_preference(:amount, @shipment.cost)
calculator.save!
end

it 'can fully capture an authorized payment' do
payment.update_attribute(:amount, @order.total)

Expand All @@ -590,7 +596,7 @@ def perform
expect(payment.amount).to eq payment.uncaptured_amount
@shipment.ship!
expect(payment.captured_amount).to eq @order.total
expect(payment.captured_amount).to eq payment.amount
expect(payment.captured_amount).to eq payment.amount - 50
expect(payment.order.payments.pending.first.amount).to eq 50
end
end
Expand Down Expand Up @@ -706,6 +712,52 @@ def perform
end
end

context '#transfer_to_location' do
# Order with 2 line items in order to be able to split one shipment into 2
let(:order) { create(:completed_order_with_totals, line_items_count: 2) }
let(:stock_location) { create(:stock_location) }
let(:variant) { order.line_items.first.variant }

before do
shipping_method = order.shipments.first.shipping_method
shipping_method.calculator.preferences[:amount] = order.shipments.first.cost
shipping_method.calculator.save!
end

it 'creates new shipment for same order' do
shipment = order.shipments.first

expect { shipment.transfer_to_location(variant, 1, stock_location) }.
to change { order.reload.shipments.size }.from(1).to(2)
end

it 'sets the given stock location for new shipment' do
shipment = order.shipments.first
shipment.transfer_to_location(variant, 1, stock_location)

new_shipment = order.reload.shipments.last

expect(new_shipment.stock_location).to_not eq(shipment.stock_location)
end

it 'sets proper costs for new shipment' do
shipment = order.shipments.first
shipment.transfer_to_location(variant, 1, shipment.stock_location)

new_shipment = order.reload.shipments.last
# Cost must be the same since both come from the same stock location
expect(new_shipment.cost).to eq(shipment.cost)
end

it 'updates `order.shipment_total` to the sum of shipments cost' do
shipment = order.shipments.first
shipment.transfer_to_location(variant, 1, shipment.stock_location)

order.reload
expect(order.shipment_total).to eq(order.shipments.sum(&:cost))
end
end

context 'set up new inventory units' do
# let(:line_item) { double(
let(:variant) { double('Variant', id: 9) }
Expand Down

0 comments on commit 879bb7c

Please sign in to comment.