Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Shipment#update_attributes_and_order - run full order updater #1514

Merged
merged 4 commits into from
Nov 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 2 additions & 18 deletions core/app/models/spree/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,25 +296,9 @@ def update_attributes_and_order(params = {})
if update_attributes params
if params.key? :selected_shipping_rate_id
# Changing the selected Shipping Rate won't update the cost (for now)
# so we persist the Shipment#cost before calculating order shipment
# total and updating payment state (given a change in shipment cost
# might change the Order#payment_state)
# so we persist the Shipment#cost before running `order.update!`
update_amounts

order.updater.update_shipment_total
order.updater.update_payment_state

# Update shipment state only after order total is updated because it
# (via Order#paid?) affects the shipment state (YAY)
update_columns(
state: determine_state(order),
updated_at: Time.current
)

# And then it's time to update shipment states and finally persist
# order changes
order.updater.update_shipment_state
order.updater.persist_totals
order.update!
end

true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@
)
end
end

context 'when shipments should be taxed' do
let!(:ship_address) { create(:address) }
let!(:tax_zone) { create(:global_zone) } # will include the above address
let!(:tax_rate) { create(:tax_rate, amount: 0.10, zone: tax_zone, tax_category: tax_category) }

let(:tax_category) { create(:tax_category) }
let(:shipping_method) { create(:shipping_method, tax_category: tax_category, zones: [tax_zone]) }

it 'shipments get a tax adjustment' do
order = create(factory, ship_address: ship_address, shipping_method: shipping_method)
shipment = order.shipments[0]

expect(shipment.additional_tax_total).to be > 0
end
end
end

describe 'order ready to complete' do
Expand Down
49 changes: 33 additions & 16 deletions core/spec/models/spree/shipment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -546,32 +546,49 @@
end

context "changes shipping rate via general update" do
let(:store) { create :store }
let!(:ship_address) { create(:address) }
let!(:tax_zone) { create(:global_zone) } # will include the above address
let!(:tax_rate) { create(:tax_rate, amount: 0.10, zone: tax_zone, tax_category: tax_category) }
let(:tax_category) { create(:tax_category) }

let(:order) do
Spree::Order.create(
payment_total: 100,
payment_state: 'paid',
total: 100,
item_total: 100,
store: store
create(
:order_ready_to_ship,
ship_address: ship_address,
shipment_cost: 10,
shipping_method: ten_dollar_shipping_method,
line_items_count: 1,
line_items_price: 100,
)
end

let(:shipment) { Spree::Shipment.create order_id: order.id }
let(:ten_dollar_shipping_method) { create(:shipping_method, tax_category: tax_category, zones: [tax_zone], cost: 10) }
let(:twenty_dollar_shipping_method) { create(:shipping_method, tax_category: tax_category, zones: [tax_zone], cost: 20) }

let(:shipping_rate) do
Spree::ShippingRate.create shipment_id: shipment.id, cost: 10
end
let(:shipment) { order.shipments[0] }

before do
shipment.update_attributes_and_order selected_shipping_rate_id: shipping_rate.id
let(:twenty_dollar_shipping_rate) do
create(:shipping_rate, cost: 20, shipment: shipment, shipping_method: twenty_dollar_shipping_method)
end

it "updates everything around order shipment total and state" do
expect(shipment.cost.to_f).to eq 10
expect(shipment.state).to eq 'ready'
expect(shipment.cost).to eq 10
expect(shipment.additional_tax_total).to eq 1

expect(order.shipment_total).to eq 10
expect(order.total).to eq 121 # shipment: 10 + 1 (tax) + line item: 100 + 10 (tax)
expect(order.payment_state).to eq 'paid'

shipment.update_attributes_and_order selected_shipping_rate_id: twenty_dollar_shipping_rate.id

expect(shipment.state).to eq 'pending'
expect(shipment.order.total.to_f).to eq 110
expect(shipment.order.payment_state).to eq 'balance_due'
expect(shipment.cost).to eq 20
expect(shipment.additional_tax_total).to eq 2

expect(order.shipment_total).to eq 20
expect(order.total).to eq 132 # shipment: 20 + 2 (tax) + line item: 100 + 10 (tax)
expect(order.payment_state).to eq 'balance_due'
end
end

Expand Down