Skip to content
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
2 changes: 1 addition & 1 deletion core/app/models/spree/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Shipment < Spree::Base
belongs_to :order, class_name: 'Spree::Order', touch: true, inverse_of: :shipments, optional: true
belongs_to :stock_location, class_name: 'Spree::StockLocation', optional: true

has_many :adjustments, as: :adjustable, inverse_of: :adjustable, dependent: :delete_all
has_many :adjustments, as: :adjustable, inverse_of: :adjustable, dependent: :delete_all, autosave: true
has_many :inventory_units, dependent: :destroy, inverse_of: :shipment
has_many :shipping_rates, -> { order(:cost) }, dependent: :destroy, inverse_of: :shipment
has_many :shipping_methods, through: :shipping_rates
Expand Down
71 changes: 58 additions & 13 deletions core/spec/models/spree/order_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ module Spree
let(:tax_category) { create(:tax_category) }
let(:ship_address) { create(:address, state: new_york) }
let(:new_york) { create(:state, state_code: "NY") }
let(:tax_zone) { create(:zone, states: [new_york]) }
let(:new_york_tax_zone) { create(:zone, states: [new_york]) }

let!(:tax_rate) do
let!(:new_york_tax_rate) do
create(
:tax_rate,
name: "New York Sales Tax",
tax_categories: [tax_category],
zone: tax_zone,
zone: new_york_tax_zone,
included_in_price: false,
amount: 0.1
)
Expand Down Expand Up @@ -111,21 +111,66 @@ module Spree
end

context 'when the address has changed to a different state' do
let(:new_shipping_address) { create(:address) }
let(:oregon) { create(:state, state_code: "OR") }
let(:oregon_tax_zone) { create(:zone, states: [oregon]) }
let!(:oregon_tax_rate) do
create(
:tax_rate,
name: "Oregon Sales Tax",
tax_categories: [tax_category],
zone: oregon_tax_zone,
included_in_price: false,
amount: 0.2
)
end
let(:new_address) { create(:address, state: oregon) }
let(:shipping_method) { create(:shipping_method, tax_category:, zones: [oregon_tax_zone, new_york_tax_zone], cost: 10) }
let(:shipping_rate) do
create(:shipping_rate, cost: 10, shipping_method: shipping_method)
end
let(:shipment) { order.shipments[0] }

subject do
order.ship_address = new_address
order.bill_address = new_address

order.recalculate
end

before do
order.ship_address = new_shipping_address
shipment.shipping_rates = [shipping_rate]
shipment.selected_shipping_rate_id = shipping_rate.id
order.recalculate
end

it 'removes the old taxes' do
it 'updates the taxes to reflect the new state' do
expect {
order.recalculate
subject
}.to change {
order.all_adjustments.tax.count
}.from(1).to(0)
order.additional_tax_total
}.from(2).to(4)
end

expect(order.additional_tax_total).to eq 0
expect(order.adjustment_total).to eq 0
it 'updates the shipment taxes to reflect the new state' do
expect {
subject
}.to change {
order.shipments.first.additional_tax_total
}.from(1).to(2)
.and change {
order.shipments.first.adjustments.first.amount
}.from(1).to(2)
end

it 'updates the line item taxes to reflect the new state' do
expect {
subject
}.to change {
order.line_items.first.additional_tax_total
}.from(1).to(2)
.and change {
order.line_items.first.adjustments.first.amount
}.from(1).to(2)
end
end

Expand Down Expand Up @@ -179,7 +224,7 @@ module Spree
order_taxes: [
Spree::Tax::ItemTax.new(
label: "Delivery Fee",
tax_rate:,
tax_rate: new_york_tax_rate,
amount: 2.60,
included_in_price: false
)
Expand All @@ -188,7 +233,7 @@ module Spree
Spree::Tax::ItemTax.new(
item_id: line_item.id,
label: "Item Tax",
tax_rate:,
tax_rate: new_york_tax_rate,
amount: 1.40,
included_in_price: false
)
Expand Down