Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Phase 10: tax rates name their jurisdiction instead of a zone

Łukasz Adamczyk60f295245878
diff --git a/packages/admin-sdk/src/types/generated/TaxRate.ts b/packages/admin-sdk/src/types/generated/TaxRate.ts
index 2501da2e94..1807655113 100644
--- a/packages/admin-sdk/src/types/generated/TaxRate.ts
+++ b/packages/admin-sdk/src/types/generated/TaxRate.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 6b0a46ca795afe728d7f71806a5c05c0
+// Typelizer digest 207cd4838d5488ee10ad59a154e0e6e7
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
@@ -15,7 +15,10 @@ interface TaxRate {
amount_percentage: number | null;
tax_category_id: string | null;
store_id: string | null;
- zone_id: string | null;
+ country_id: number | null;
+ state_id: number | null;
+ country_iso: string | null;
+ state_code: string | null;
}
export default TaxRate;
diff --git a/spree/api/app/controllers/spree/api/v3/admin/tax_rates_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/tax_rates_controller.rb
index 9afac76889..272b8c0289 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/tax_rates_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/tax_rates_controller.rb
@@ -18,12 +18,13 @@ module Spree
end
def collection_includes
- [:tax_category, :zone]
+ [:tax_category, :country, :state]
end
def permitted_params
params.permit(:name, :amount, :amount_percentage, :included_in_price,
- :show_rate_in_label, :tax_category_id, :zone_id, metadata: {})
+ :show_rate_in_label, :tax_category_id,
+ :country_id, :state_id, :country_iso, :state_code, metadata: {})
end
end
end
diff --git a/spree/api/app/serializers/spree/api/v3/admin/tax_rate_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/tax_rate_serializer.rb
index a7f60978c8..58ca41fb4c 100644
--- a/spree/api/app/serializers/spree/api/v3/admin/tax_rate_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/admin/tax_rate_serializer.rb
@@ -12,7 +12,10 @@ module Spree
show_rate_in_label: :boolean,
tax_category_id: [:string, nullable: true],
store_id: [:string, nullable: true],
- zone_id: [:string, nullable: true],
+ country_id: [:number, nullable: true],
+ country_iso: [:string, nullable: true],
+ state_id: [:number, nullable: true],
+ state_code: [:string, nullable: true],
metadata: ['Record<string, unknown>', nullable: true],
deleted_at: [:string, nullable: true]
@@ -37,8 +40,18 @@ module Spree
tax_rate.store&.prefixed_id
end
- attribute :zone_id do |tax_rate|
- tax_rate.zone&.prefixed_id
+ # The jurisdiction the rate taxes. A null country means every country,
+ # and a country with no state means the whole country. Countries and
+ # states are the two models without prefixed ids, so these are raw —
+ # write with `country_iso`/`state_code` instead.
+ attributes :country_id, :state_id
+
+ attribute :country_iso do |tax_rate|
+ tax_rate.country&.iso
+ end
+
+ attribute :state_code do |tax_rate|
+ tax_rate.state&.abbr
end
end
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/tax_rates_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/tax_rates_controller_spec.rb
index 66850fb04a..031516a4c7 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/tax_rates_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/tax_rates_controller_spec.rb
@@ -6,8 +6,9 @@ RSpec.describe Spree::Api::V3::Admin::TaxRatesController, type: :controller do
include_context 'API v3 Admin authenticated'
let!(:tax_category) { create(:tax_category) }
- let!(:zone) { create(:zone, kind: 'country') }
- let!(:tax_rate) { create(:tax_rate, tax_category: tax_category, zone: zone, amount: 0.19, included_in_price: true) }
+ let!(:germany) { Spree::Country.find_by(iso: 'DE') || create(:country, iso: 'DE', name: 'Germany') }
+ let!(:berlin) { create(:state, country: germany, abbr: 'BE', name: 'Berlin') }
+ let!(:tax_rate) { create(:tax_rate, tax_category: tax_category, country: germany, amount: 0.19, included_in_price: true) }
before { request.headers.merge!(headers) }
@@ -34,7 +35,10 @@ RSpec.describe Spree::Api::V3::Admin::TaxRatesController, type: :controller do
expect(json_response['amount_percentage']).to eq(19.0)
expect(json_response['included_in_price']).to be(true)
expect(json_response['tax_category_id']).to eq(tax_category.prefixed_id)
- expect(json_response['zone_id']).to eq(zone.prefixed_id)
+ expect(json_response['country_id']).to eq(germany.id)
+ expect(json_response['country_iso']).to eq('DE')
+ expect(json_response['state_id']).to be_nil
+ expect(json_response['state_code']).to be_nil
expect(json_response['store_id']).to eq(@default_store.prefixed_id)
end
end
@@ -42,7 +46,7 @@ RSpec.describe Spree::Api::V3::Admin::TaxRatesController, type: :controller do
describe 'POST #create' do
let(:create_params) do
{ name: 'German VAT', amount: 0.19, included_in_price: true,
- tax_category_id: tax_category.prefixed_id, zone_id: zone.prefixed_id }
+ tax_category_id: tax_category.prefixed_id, country_iso: 'DE' }
end
it 'creates a rate bound to the current store' do
@@ -60,6 +64,23 @@ RSpec.describe Spree::Api::V3::Admin::TaxRatesController, type: :controller do
expect(Spree::TaxRate.last.amount).to eq(0.07)
end
+ it 'accepts a state-level rate by its code' do
+ post :create, params: create_params.merge(state_code: 'BE'), as: :json
+
+ expect(response).to have_http_status(:created)
+ expect(json_response['state_code']).to eq('BE')
+ expect(Spree::TaxRate.last.state).to eq(berlin)
+ end
+
+ it 'rejects a state that is not in the named country' do
+ france = create(:country, iso: 'FR', name: 'France')
+
+ post :create, params: create_params.merge(country_iso: 'FR', state_id: berlin.id), as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(france.reload).to be_present
+ end
+
it 'rejects a rate with no category' do
post :create, params: create_params.except(:tax_category_id), as: :json
diff --git a/spree/api/spec/integration/spree/api/v3/admin/tax_rates_spec.rb b/spree/api/spec/integration/spree/api/v3/admin/tax_rates_spec.rb
index 52142ba519..678c86e130 100644
--- a/spree/api/spec/integration/spree/api/v3/admin/tax_rates_spec.rb
+++ b/spree/api/spec/integration/spree/api/v3/admin/tax_rates_spec.rb
@@ -6,8 +6,8 @@ RSpec.describe 'Admin Tax Rates API', type: :request, swagger_doc: 'api-referenc
include_context 'API v3 Admin'
let!(:tax_category) { create(:tax_category) }
- let!(:zone) { create(:zone, kind: 'country') }
- let!(:tax_rate) { create(:tax_rate, tax_category: tax_category, zone: zone, amount: 0.19, included_in_price: true) }
+ let!(:germany) { Spree::Country.find_by(iso: 'DE') || create(:country, iso: 'DE', name: 'Germany') }
+ let!(:tax_rate) { create(:tax_rate, tax_category: tax_category, country: germany, amount: 0.19, included_in_price: true) }
let(:Authorization) { "Bearer #{admin_jwt_token}" }
path '/api/v3/admin/tax_rates' do
@@ -17,9 +17,10 @@ RSpec.describe 'Admin Tax Rates API', type: :request, swagger_doc: 'api-referenc
security [api_key: [], bearer_auth: []]
description <<~DESC
Returns the store's tax rates. Rates are the entire configuration of
- the built-in tax provider: each pairs a tax category with a zone and a
- percentage, and decides whether the tax is already included in the
- product price (VAT-style) or added on top (US-style).
+ the built-in tax provider: each pairs a tax category with a jurisdiction
+ (a country, optionally narrowed to one state) and a percentage, and
+ decides whether the tax is already included in the product price
+ (VAT-style) or added on top (US-style).
Markets using an external tax provider ignore these rows — that
provider carries its own jurisdiction data.
@@ -68,6 +69,9 @@ RSpec.describe 'Admin Tax Rates API', type: :request, swagger_doc: 'api-referenc
Creates a tax rate for the current store. Give either `amount` as a
decimal (`0.19`) or `amount_percentage` as a percentage (`19`).
+ Name the jurisdiction with `country_iso` (and `state_code` for a
+ state-level rate). Omitting the country makes the rate apply everywhere.
+
Set `included_in_price` for VAT-style pricing, where the rate is backed
out of the displayed price rather than added to it.
DESC
@@ -85,7 +89,8 @@ RSpec.describe 'Admin Tax Rates API', type: :request, swagger_doc: 'api-referenc
included_in_price: { type: :boolean },
show_rate_in_label: { type: :boolean },
tax_category_id: { type: :string },
- zone_id: { type: :string }
+ country_iso: { type: :string, description: 'ISO code, e.g. "DE". Omit for every country.' },
+ state_code: { type: :string, description: 'State abbreviation within that country, e.g. "CA".' }
},
required: %w[name tax_category_id]
}
@@ -94,7 +99,7 @@ RSpec.describe 'Admin Tax Rates API', type: :request, swagger_doc: 'api-referenc
let(:'x-spree-api-key') { secret_api_key.plaintext_token }
let(:body) do
{ name: 'VAT', amount_percentage: 19, included_in_price: true,
- tax_category_id: tax_category.prefixed_id, zone_id: zone.prefixed_id }
+ tax_category_id: tax_category.prefixed_id, country_iso: 'DE' }
end
schema '$ref' => '#/components/schemas/TaxRate'
@@ -160,7 +165,8 @@ RSpec.describe 'Admin Tax Rates API', type: :request, swagger_doc: 'api-referenc
included_in_price: { type: :boolean },
show_rate_in_label: { type: :boolean },
tax_category_id: { type: :string },
- zone_id: { type: :string }
+ country_iso: { type: :string },
+ state_code: { type: :string }
}
}
diff --git a/spree/api/spec/requests/spree/api/v3/admin/tax_configuration_spec.rb b/spree/api/spec/requests/spree/api/v3/admin/tax_configuration_spec.rb
index edfab96c38..fdea26687a 100644
--- a/spree/api/spec/requests/spree/api/v3/admin/tax_configuration_spec.rb
+++ b/spree/api/spec/requests/spree/api/v3/admin/tax_configuration_spec.rb
@@ -8,11 +8,6 @@ RSpec.describe 'Admin tax configuration', type: :request do
let(:country) { Spree::Country.find_by(iso: 'DE') || create(:country, iso: 'DE', name: 'Germany') }
let(:address) { create(:address, country: country, state: nil, state_name: 'Berlin') }
- let(:zone) do
- create(:zone, kind: 'country', default_tax: true).tap do |zone|
- zone.members.create!(zoneable: country)
- end
- end
let(:tax_category) { create(:tax_category) }
let(:cart) do
@@ -22,13 +17,16 @@ RSpec.describe 'Admin tax configuration', type: :request do
end
end
+ # Prices are quoted including German VAT, so the market is Germany.
+ before { store.default_market.update!(countries: [country]) }
+
# The publishable key the storefront reads with.
let(:store_headers) { { 'x-spree-api-key' => create(:api_key, :publishable, store: store).token } }
it 'configures a rate and produces the tax it describes' do
post '/api/v3/admin/tax_rates',
params: { name: 'German VAT', amount_percentage: 19, included_in_price: true,
- tax_category_id: tax_category.prefixed_id, zone_id: zone.prefixed_id },
+ tax_category_id: tax_category.prefixed_id, country_iso: 'DE' },
headers: headers, as: :json
expect(response).to have_http_status(:created)
@@ -45,7 +43,7 @@ RSpec.describe 'Admin tax configuration', type: :request do
it 'still records a treatment when the rate is zero' do
post '/api/v3/admin/tax_rates',
params: { name: 'German VAT', amount_percentage: 19, included_in_price: true,
- tax_category_id: tax_category.prefixed_id, zone_id: zone.prefixed_id },
+ tax_category_id: tax_category.prefixed_id, country_iso: 'DE' },
headers: headers, as: :json
rate_id = JSON.parse(response.body)['id']
@@ -64,7 +62,7 @@ RSpec.describe 'Admin tax configuration', type: :request do
it 'shows the storefront the tax on a line, without the admin treatment fields' do
post '/api/v3/admin/tax_rates',
params: { name: 'German VAT', amount_percentage: 19, included_in_price: true,
- tax_category_id: tax_category.prefixed_id, zone_id: zone.prefixed_id },
+ tax_category_id: tax_category.prefixed_id, country_iso: 'DE' },
headers: headers, as: :json
Spree::Carts::RecalculateTotals.call(cart: cart)
diff --git a/spree/core/app/models/concerns/spree/purchase/taxation.rb b/spree/core/app/models/concerns/spree/purchase/taxation.rb
index 35111d28e3..eb0d729f38 100644
--- a/spree/core/app/models/concerns/spree/purchase/taxation.rb
+++ b/spree/core/app/models/concerns/spree/purchase/taxation.rb
@@ -15,6 +15,15 @@ module Spree
@tax_zone ||= Spree::Zone.match(tax_address) || Spree::Zone.default_tax
end
+ # The country whose tax applies. Before the customer enters an address
+ # that is the market's own country, which is what lets a storefront show
+ # tax-inclusive prices from the first page view.
+ #
+ # @return [Spree::Country, nil]
+ def tax_country
+ tax_address&.country || market&.default_country || store&.default_country
+ end
+
# The tax engine for this sale, chosen by its market. Falls back to the
# store-wide default when the market names none, or when there is no
# market yet (an empty cart before currency resolution).
diff --git a/spree/core/app/models/concerns/spree/vat_price_calculation.rb b/spree/core/app/models/concerns/spree/vat_price_calculation.rb
index 6ab1348a6a..15268d18d9 100644
--- a/spree/core/app/models/concerns/spree/vat_price_calculation.rb
+++ b/spree/core/app/models/concerns/spree/vat_price_calculation.rb
@@ -1,4 +1,6 @@
module Spree
+ # Restates a VAT-inclusive price for a customer in another country: the
+ # home-country VAT comes out, the destination's goes on.
module VatPriceCalculation
def gross_amount(amount, price_options)
return amount if amount.nil? || !outside_default_vat_zone?(price_options)
@@ -18,7 +20,7 @@ module Spree
end
def default_vat(tax_category)
- included_tax_amount(tax_zone: default_zone, tax_category: tax_category)
+ included_tax_amount(country: default_tax_country, tax_category: tax_category)
end
def foreign_vat(price_options)
@@ -29,16 +31,38 @@ module Spree
amount * (1 + foreign_vat(price_options))
end
+ # Whether the price has to be restated at all. The zone in price_options is
+ # the browsing context's; what matters for tax is the country behind it.
def outside_default_vat_zone?(price_options)
- price_options[:tax_zone] && default_zone && price_options[:tax_zone] != default_zone
+ country = country_from(price_options)
+
+ country.present? && default_tax_country.present? && country != default_tax_country
end
def included_tax_amount(price_options)
- Spree::TaxRate.included_tax_amount_for(price_options).to_f
+ country = price_options[:country] || country_from(price_options)
+
+ Spree::TaxRate.included_tax_amount_for(
+ country: country, tax_category: price_options[:tax_category]
+ ).to_f
+ end
+
+ def country_from(price_options)
+ return price_options[:country] if price_options[:country]
+ return price_options[:address].country if price_options[:address]
+
+ # A zone can span several countries; its own country list is the only
+ # thing to read, and a single-country zone is the case that matters here.
+ zone = price_options[:tax_zone]
+ zone&.countries&.first
end
- def default_zone
- @default_zone ||= Spree::Zone.default_tax
+ # The country whose VAT the catalogue prices already include — the market
+ # being browsed, or the store's own country when there is no market.
+ def default_tax_country
+ return @default_tax_country if defined?(@default_tax_country)
+
+ @default_tax_country = Spree::Current.market&.default_country || Spree::Current.store&.default_country
end
def round_to_two_places(amount)
diff --git a/spree/core/app/models/spree/line_item.rb b/spree/core/app/models/spree/line_item.rb
index 97f1c8b8ef..728742096c 100644
--- a/spree/core/app/models/spree/line_item.rb
+++ b/spree/core/app/models/spree/line_item.rb
@@ -130,7 +130,7 @@ module Spree
context = Spree::Pricing::Context.from_order(variant, owner, quantity: quantity)
currency_price = variant.price_for(context)
- self.price = currency_price.price_including_vat_for(tax_zone: tax_zone) if currency_price.present?
+ self.price = currency_price.price_including_vat_for(address: owner&.tax_address) if currency_price.present?
self.price_list_id = currency_price.price_list_id if currency_price.present?
end
@@ -316,7 +316,7 @@ module Spree
return unless currency_price.present?
- new_price = currency_price.price_including_vat_for(tax_zone: tax_zone)
+ new_price = currency_price.price_including_vat_for(address: owner&.tax_address)
return unless new_price.present?
diff --git a/spree/core/app/models/spree/stock/estimator.rb b/spree/core/app/models/spree/stock/estimator.rb
index 07dce36a62..ab2c13ff90 100644
--- a/spree/core/app/models/spree/stock/estimator.rb
+++ b/spree/core/app/models/spree/stock/estimator.rb
@@ -57,15 +57,15 @@ module Spree
def taxation_options_for(delivery_method)
{
tax_category: delivery_method.tax_category,
+ address: @order.tax_address,
tax_zone: @order.tax_zone
}
end
def first_tax_rate_for(tax_category)
- return unless @order.tax_zone && tax_category
+ return unless @order.tax_address && tax_category
- Spree::TaxRate.for_tax_category(tax_category).
- potential_rates_for_zone(@order.tax_zone).first
+ Spree::TaxRate.for_tax_category(tax_category).for_address(@order.tax_address).first
end
# The eligibility seam: every rate consumer (checkout, routing, cart
diff --git a/spree/core/app/models/spree/tax_provider/internal.rb b/spree/core/app/models/spree/tax_provider/internal.rb
index d52f684ca1..5cda3a7d1f 100644
--- a/spree/core/app/models/spree/tax_provider/internal.rb
+++ b/spree/core/app/models/spree/tax_provider/internal.rb
@@ -2,11 +2,11 @@ module Spree
module TaxProvider
# Computes tax directly from TaxRate configuration: rate × discounted
# base, with VAT included-in-price math backed out of the gross basis.
- # Zone matching is unchanged from 5.x (TaxRate.match over the owner's tax
- # zone). Fees without a tax category are taxed with the default tax
- # category's rates.
+ # Rates are matched on the owner's tax address, each naming its own country
+ # and optionally a state. Fees without a tax category are taxed with the
+ # default tax category's rates.
class Internal < Base
- # Rates are configured per zone with no local-tax breakdown, no buyer
+ # Rates are configured per country or state with no local-tax breakdown, no buyer
# registration handling and no distance-selling thresholds. Declared so a
# merchant pairing this with a market is told, rather than discovering it
# in a return.
@@ -22,8 +22,7 @@ module Spree
items ||= owner.line_items.to_a + owner.fulfillments.to_a + owner.fees.to_a
return if items.empty?
- rates = Spree::TaxRate.match(owner.tax_zone)
- jurisdiction = jurisdiction_for(owner)
+ rates = Spree::TaxRate.for_store(owner.store).for_jurisdiction(owner.tax_country, owner.tax_address&.state).to_a
items.group_by(&:class).each do |klass, group|
# Replace-all set semantics per estimate: stale lines die with the
@@ -33,16 +32,22 @@ module Spree
items.each do |item|
relevant_rates = rates.select { |rate| rate.tax_category_id == tax_category_id_for(item) }
- exemption = exemption_for(item, exemptions, jurisdiction)
- # An exempt item has no tax to back out of its price, so the whole
- # basis is pre-tax.
- store_pre_tax_amount(item, exemption ? [] : relevant_rates)
# A matched rate always produces a row, zero-amount ones included:
# a zero rate is a treatment ("this is zero-rated"), which reporting
# and e-invoicing both need to see. No matched rate means no opinion,
# and writes nothing.
- relevant_rates.each do |rate|
+ taxed = relevant_rates.map do |rate|
+ jurisdiction = jurisdiction_for(rate, owner)
+ [rate, jurisdiction, exemption_for(item, exemptions, jurisdiction)]
+ end
+
+ # An exempt item has no tax to back out of its price, so its whole
+ # basis is pre-tax.
+ exempt_rates = taxed.select { |_rate, _jurisdiction, exemption| exemption }.map(&:first)
+ store_pre_tax_amount(item, relevant_rates - exempt_rates)
+
+ taxed.each do |rate, jurisdiction, exemption|
if exemption
write_tax_line(owner, item, rate, 0, 'customer_exempt', jurisdiction,
data: exemption_data(exemption, item))
@@ -113,15 +118,14 @@ module Spree
rate.amount.to_d.zero? ? 'zero_rated' : 'standard_rated'
end
- # The jurisdiction that taxed the line. Read from the owner's tax address
- # rather than its tax zone, because a Zone can span several countries and
- # so has no single country to report. Rates carry their own country from
- # the tax-side Zone decoupling onwards.
- def jurisdiction_for(owner)
- address = owner.tax_address
- return {} if address.nil?
-
- { country_iso: address.country&.iso, state_code: address.state&.abbr }
+ # The jurisdiction that taxed the line, read off the rate that matched.
+ # A rate covering every country falls back to the address, so the row can
+ # still say where the sale was taxed.
+ def jurisdiction_for(rate, owner)
+ {
+ country_iso: (rate.country || owner.tax_country)&.iso,
+ state_code: (rate.state || owner.tax_address&.state)&.abbr
+ }
end
# The first exemption entry that claims both this item and the
diff --git a/spree/core/app/models/spree/tax_rate.rb b/spree/core/app/models/spree/tax_rate.rb
index cef05ef3f7..4de2bffa0f 100644
--- a/spree/core/app/models/spree/tax_rate.rb
+++ b/spree/core/app/models/spree/tax_rate.rb
@@ -5,6 +5,11 @@ module Spree
class TaxRate < Spree.base_class
has_prefix_id :tax
+ # Rates name their country and state directly since 6.0. No code reads
+ # zone_id any more, but the column stays in the database through 6.0: it is
+ # what `spree:migrate_tax_zones` converts from, and that task runs after
+ # db:migrate. The physical drop lands in 6.1.
+
acts_as_paranoid
include Spree::Metafields
@@ -12,11 +17,15 @@ module Spree
include Spree::SingleStoreResource
with_options inverse_of: :tax_rates do
- belongs_to :zone, class_name: 'Spree::Zone', optional: true
belongs_to :tax_category,
class_name: 'Spree::TaxCategory'
end
+ # The jurisdiction this rate applies in. Nil country means everywhere, and a
+ # country with no state means the whole country.
+ belongs_to :country, class_name: 'Spree::Country', optional: true
+ belongs_to :state, class_name: 'Spree::State', optional: true
+
has_many :tax_lines, class_name: 'Spree::TaxLine', dependent: :nullify
with_options presence: true do
@@ -24,18 +33,25 @@ module Spree
validates :tax_category, :name
end
- scope :by_zone, ->(zone) { where(zone_id: zone.id) }
- scope :potential_rates_for_zone,
- ->(zone) do
- where(zone_id: Spree::Zone.potential_matching_zones(zone).pluck(:id))
- end
- scope :for_default_zone,
- -> { potential_rates_for_zone(Spree::Zone.default_tax) }
+ validate :state_belongs_to_country
+
+ # Rates that reach the given address: its own country and state, plus the
+ # broader rates that cover everywhere.
+ scope :for_address, lambda { |address|
+ where(country_id: [address&.country_id, nil].uniq).
+ where(state_id: [address&.state_id, nil].uniq)
+ }
+ scope :for_country, ->(country) { where(country_id: [country&.id, nil].uniq) }
+ # Country and state named directly — the shape the provider matches on,
+ # since tax can apply before a customer has given an address at all.
+ scope :for_jurisdiction, lambda { |country, state = nil|
+ where(country_id: [country&.id, nil].uniq).where(state_id: [state&.id, nil].uniq)
+ }
scope :for_tax_category,
->(category) { where(tax_category_id: category.try(:id)) }
scope :included_in_price, -> { where(included_in_price: true) }
- self.whitelisted_ransackable_attributes = %w[amount zone_id tax_category_id included_in_price name]
+ self.whitelisted_ransackable_attributes = %w[amount country_id state_id tax_category_id included_in_price name]
# Virtual attribute for percentage display in admin forms
def amount_percentage
@@ -48,20 +64,39 @@ module Spree
self.amount = value.present? ? (value.to_f / 100) : nil
end
- # Gets the array of TaxRates appropriate for the specified tax zone
- def self.match(order_tax_zone)
- return [] unless order_tax_zone
+ # Countries and states are addressed by their codes across the v3 API —
+ # neither model carries a prefixed id, and the codes are what a merchant
+ # recognises. The numeric FKs stay writable for internal callers.
+ def country_iso=(value)
+ self.country = value.present? ? Spree::Country.by_iso(value) : nil
+ end
- potential_rates_for_zone(order_tax_zone)
+ # Looked up within the rate's own country, so assign the country first —
+ # state abbreviations repeat across countries.
+ def state_code=(value)
+ self.state = value.blank? ? nil : Spree::State.where(country_id: country_id).find_by(abbr: value)
end
+ # The rates that tax a sale delivered to this address.
+ #
+ # @param address [Spree::Address, nil]
+ # @return [ActiveRecord::Relation<Spree::TaxRate>]
+ def self.match(address)
+ return none if address.nil?
+
+ for_address(address)
+ end
+
+ # The included-in-price rate to back out of a gross price.
+ #
+ # @param options [Hash] +:country+ (or +:address+) and +:tax_category+
+ # @return [BigDecimal] 0 when the jurisdiction or category is unknown
def self.included_tax_amount_for(options)
- return 0 unless options[:tax_zone] && options[:tax_category]
+ country = options[:country] || options[:address]&.country
+ return 0 unless country && options[:tax_category]
- potential_rates_for_zone(options[:tax_zone]).
- included_in_price.
- for_tax_category(options[:tax_category]).
- sum(:amount)
+ scope = options[:address] ? for_address(options[:address]) : for_country(country)
+ scope.included_in_price.for_tax_category(options[:tax_category]).sum(:amount)
end
def included?
@@ -84,6 +119,14 @@ module Spree
private
+ # A state-level rate whose state sits in another country would never match
+ # any address — the two columns have to agree.
+ def state_belongs_to_country
+ return if state.nil? || country.nil? || state.country_id == country_id
+
+ errors.add(:state, :invalid)
+ end
+
def amount_for_label
return '' unless show_rate_in_label?
return '' if amount.zero?
diff --git a/spree/core/db/migrate/20260806000005_add_country_and_state_to_spree_tax_rates.rb b/spree/core/db/migrate/20260806000005_add_country_and_state_to_spree_tax_rates.rb
new file mode 100644
index 0000000000..43d188e116
--- /dev/null
+++ b/spree/core/db/migrate/20260806000005_add_country_and_state_to_spree_tax_rates.rb
@@ -0,0 +1,14 @@
+class AddCountryAndStateToSpreeTaxRates < ActiveRecord::Migration[8.1]
+ def change
+ # Merchants think "Germany charges 19% VAT", not in zones. A rate now names
+ # its jurisdiction directly: nil country means everywhere, and a country
+ # with no state means the whole country.
+ #
+ # Zones were also the reason a tax line could not say which country taxed
+ # it — one zone can span several. Existing rows are converted by
+ # `spree:migrate_tax_zones`, which splits a multi-country zone into one rate
+ # per country, before `zone_id` comes off.
+ add_reference :spree_tax_rates, :country, null: true
+ add_reference :spree_tax_rates, :state, null: true
+ end
+end
diff --git a/spree/core/lib/spree/testing_support/factories/tax_rate_factory.rb b/spree/core/lib/spree/testing_support/factories/tax_rate_factory.rb
index e97238ff10..9cc03415a9 100644
--- a/spree/core/lib/spree/testing_support/factories/tax_rate_factory.rb
+++ b/spree/core/lib/spree/testing_support/factories/tax_rate_factory.rb
@@ -1,8 +1,13 @@
FactoryBot.define do
factory :tax_rate, class: Spree::TaxRate do
name { "TaxRate - #{rand(999_999)}" }
- zone
+ country { Spree::Country.find_by(iso: 'US') || association(:country) }
tax_category
amount { 0.1 }
+
+ # Taxes every country — what a single-market store configures.
+ trait :worldwide do
+ country { nil }
+ end
end
end
diff --git a/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml b/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml
index 0dd7a162de..a4d1a93470 100644
--- a/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml
+++ b/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml
@@ -184,6 +184,22 @@ steps:
their own records afterwards. The NOT NULL constraints land in 6.1.
Idempotent.
+ - id: migrate_tax_zones
+ name: "Convert tax zones to country and state references"
+ task: "spree:migrate_tax_zones"
+ notes: |
+ Tax rates name their jurisdiction directly in Spree 6 — merchants think
+ "Germany charges 19% VAT", and a tax line has to be able to report the
+ country that taxed it, which a zone spanning several countries cannot
+ say. Copies each rate's zone countries and states onto the rate, splitting
+ a multi-country zone into one rate per country. Zones with no members stay
+ as every-country rates.
+
+ Run immediately after db:migrate, BEFORE the migration that drops
+ spree_tax_rates.zone_id — that column is the only record of what a rate
+ was configured against. Spree::Zone itself is not dropped; shipping and
+ the browsing context still use it. Idempotent.
+
- id: backfill_tax_store_ids
name: "Bind tax rates and tax categories to the default store"
task: "spree:backfill_tax_store_ids"
diff --git a/spree/core/lib/tasks/tax_zones_migration.rake b/spree/core/lib/tasks/tax_zones_migration.rake
new file mode 100644
index 0000000000..9b20f45f85
--- /dev/null
+++ b/spree/core/lib/tasks/tax_zones_migration.rake
@@ -0,0 +1,59 @@
+namespace :spree do
+ desc <<~DESC
+ Converts the zones tax rates were configured against into direct
+ country/state references (Spree 6.0). A zone spanning several countries
+ becomes one rate per country, since a rate can now name only one
+ jurisdiction — which is what lets a tax line report the country that taxed
+ it. Idempotent: rates that already carry a country or state are skipped, and
+ so are rates whose zone had no members, which stay as "everywhere" rates.
+ DESC
+ task migrate_tax_zones: :environment do
+ # One entry per taxing jurisdiction in the zone. State members carry their
+ # own country, so a mixed zone still yields complete pairs.
+ jurisdictions_for = lambda do |zone|
+ zone.zone_members.filter_map do |member|
+ case member.zoneable_type
+ when 'Spree::Country'
+ { country_id: member.zoneable_id, state_id: nil }
+ when 'Spree::State'
+ state = Spree::State.find_by(id: member.zoneable_id)
+ { country_id: state.country_id, state_id: state.id } if state
+ end
+ end.uniq
+ end
+
+ converted = 0
+ duplicated = 0
+ skipped = 0
+
+ Spree::TaxRate.with_deleted.
+ where(country_id: nil, state_id: nil).where.not(zone_id: nil).find_each do |rate|
+ zone = Spree::Zone.find_by(id: rate.zone_id)
+ jurisdictions = zone ? jurisdictions_for.call(zone) : []
+
+ if jurisdictions.empty?
+ skipped += 1
+ next
+ end
+
+ first, *rest = jurisdictions
+ rate.update_columns(country_id: first[:country_id], state_id: first[:state_id], updated_at: Time.current)
+ converted += 1
+
+ rest.each do |jurisdiction|
+ Spree::TaxRate.insert(
+ rate.attributes.slice(*Spree::TaxRate.column_names).except('id', 'created_at', 'updated_at').merge(
+ 'country_id' => jurisdiction[:country_id],
+ 'state_id' => jurisdiction[:state_id],
+ 'created_at' => Time.current,
+ 'updated_at' => Time.current
+ )
+ )
+ duplicated += 1
+ end
+ end
+
+ puts " Converted #{converted} tax rates, split #{duplicated} extra rate(s) off multi-jurisdiction zones, " \
+ "skipped #{skipped}."
+ end
+end
diff --git a/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rb b/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rb
index e960cb036c..833f9221c0 100644
--- a/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rb
+++ b/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rb
@@ -10,6 +10,7 @@ describe '6.0 data migration tasks' do
load Spree::Core::Engine.root.join('lib', 'tasks', 'products.rake')
load Spree::Core::Engine.root.join('lib', 'tasks', 'order_market_backfill.rake')
load Spree::Core::Engine.root.join('lib', 'tasks', 'store_binding_migration.rake')
+ load Spree::Core::Engine.root.join('lib', 'tasks', 'tax_zones_migration.rake')
end
let(:store) { @default_store }
@@ -135,6 +136,67 @@ describe '6.0 data migration tasks' do
end
end
+ describe 'spree:migrate_tax_zones' do
+ let(:germany) { Spree::Country.find_by(iso: 'DE') || create(:country, iso: 'DE', name: 'Germany') }
+ let(:france) { Spree::Country.find_by(iso: 'FR') || create(:country, iso: 'FR', name: 'France') }
+
+ def zone_with(*zoneables)
+ zone = Spree::Zone.create!(name: "Tax Zone #{Time.current.to_f}#{rand(1000)}", kind: 'country')
+ zoneables.each { |zoneable| zone.zone_members.create!(zoneable: zoneable) }
+ zone
+ end
+
+ def unconverted_rate(zone, **attributes)
+ create(:tax_rate, **attributes).tap do |rate|
+ rate.update_columns(country_id: nil, state_id: nil, zone_id: zone.id)
+ end
+ end
+
+ it 'copies a single-country zone onto the rate' do
+ rate = unconverted_rate(zone_with(germany))
+
+ run_task('spree:migrate_tax_zones')
+
+ expect(rate.reload.country).to eq(germany)
+ expect(rate.state).to be_nil
+ end
+
+ it 'splits a multi-country zone into one rate per country' do
+ rate = unconverted_rate(zone_with(germany, france), amount: 0.19)
+
+ expect { run_task('spree:migrate_tax_zones') }.to change(Spree::TaxRate, :count).by(1)
+
+ countries = Spree::TaxRate.where(name: rate.name).map(&:country)
+ expect(countries).to contain_exactly(germany, france)
+ expect(Spree::TaxRate.where(name: rate.name).map(&:amount).uniq).to eq([0.19])
+ end
+
+ it 'keeps a state member as a country and state pair' do
+ state = create(:state, country: germany, abbr: 'BE', name: 'Berlin')
+ rate = unconverted_rate(zone_with(state))
+
+ run_task('spree:migrate_tax_zones')
+
+ expect(rate.reload.country).to eq(germany)
+ expect(rate.state).to eq(state)
+ end
+
+ it 'leaves a memberless zone as an every-country rate' do
+ rate = unconverted_rate(zone_with)
+
+ run_task('spree:migrate_tax_zones')
+
+ expect(rate.reload.country).to be_nil
+ end
+
+ it 'is idempotent' do
+ unconverted_rate(zone_with(germany, france))
+ run_task('spree:migrate_tax_zones')
+
+ expect { run_task('spree:migrate_tax_zones') }.not_to change(Spree::TaxRate, :count)
+ end
+ end
+
describe 'spree:migrate_zones_to_delivery_zones' do
let!(:country) { Spree::Country.find_by(iso: 'US') || create(:country_us) }
let!(:zone) do
diff --git a/spree/core/spec/models/spree/concerns/vat_price_calculation_spec.rb b/spree/core/spec/models/spree/concerns/vat_price_calculation_spec.rb
index 5ae32b91f8..9804ae4681 100644
--- a/spree/core/spec/models/spree/concerns/vat_price_calculation_spec.rb
+++ b/spree/core/spec/models/spree/concerns/vat_price_calculation_spec.rb
@@ -14,54 +14,76 @@ module Spree
describe '#gross_amount' do
subject(:gross_amount) { test_class.new.gross_amount(amount, price_options) }
- let(:zone) { Zone.new }
- let(:tax_category) { TaxCategory.new }
- let(:price_options) do
- {
- tax_zone: zone,
- tax_category: tax_category
- }
- end
+ # The catalogue is priced including this country's VAT.
+ let(:home_country) { create(:country, iso: 'DE', name: 'Germany') }
+ let(:destination) { create(:country, iso: 'FR', name: 'France') }
+ let(:tax_category) { create(:tax_category) }
+ let(:price_options) { { country: destination, tax_category: tax_category } }
let(:amount) { 100 }
- context 'with no default zone set' do
- it 'does not call TaxRate.included_tax_amount_for' do
+ context 'with no home country resolvable' do
+ it 'leaves the price alone' do
+ # Records first: a tax category needs a store, and this stubs it away.
+ tax_category
+ allow(Spree::Current).to receive(:market).and_return(nil)
+ allow(Spree::Current).to receive(:store).and_return(nil)
+
expect(TaxRate).not_to receive(:included_tax_amount_for)
- gross_amount
+ expect(gross_amount).to eq(100)
end
end
- context 'with no zone given' do
- let(:zone) { nil }
+ context 'with a home country' do
+ before { @default_store.default_market.update!(countries: [home_country]) }
- it 'does not call TaxRate.included_tax_amount_for' do
- expect(TaxRate).not_to receive(:included_tax_amount_for)
- gross_amount
+ context 'and no destination given' do
+ let(:price_options) { { tax_category: tax_category } }
+
+ it 'leaves the price alone' do
+ expect(TaxRate).not_to receive(:included_tax_amount_for)
+ expect(gross_amount).to eq(100)
+ end
end
- end
- context 'with a default zone set' do
- let(:default_zone) { Spree::Zone.new }
+ context 'and the destination is the home country' do
+ let(:destination) { home_country }
- before do
- allow(Spree::Zone).to receive(:default_tax).and_return(default_zone)
+ it 'leaves the price alone — the VAT in it is already the right one' do
+ expect(TaxRate).not_to receive(:included_tax_amount_for)
+ expect(gross_amount).to eq(100)
+ end
end
- context 'and zone equal to the default zone' do
- let(:zone) { default_zone }
+ context 'and the destination is elsewhere' do
+ it 'takes the home VAT out and puts the destination VAT on' do
+ create(:tax_rate, country: home_country, tax_category: tax_category, amount: 0.19,
+ included_in_price: true)
+ create(:tax_rate, country: destination, tax_category: tax_category, amount: 0.25,
+ included_in_price: true)
- it "does not call 'TaxRate.included_tax_amount_for'" do
- expect(TaxRate).not_to receive(:included_tax_amount_for)
+ # 100 gross at 19% is 84.03 net, which is 105.04 gross at 25%.
+ expect(gross_amount).to eq(105.04)
+ end
+
+ it 'reads both rates from the tax configuration' do
+ expect(TaxRate).to receive(:included_tax_amount_for).twice.and_return(0)
gross_amount
end
end
- context 'and zone not equal to default zone' do
- let(:zone) { Spree::Zone.new }
+ context 'and the destination came in as a zone' do
+ let(:price_options) { { tax_zone: zone, tax_category: tax_category } }
+ let(:zone) do
+ create(:zone, kind: 'country').tap { |zone| zone.zone_members.create!(zoneable: destination) }
+ end
- it 'calls TaxRate.included_tax_amount_for two times' do
- expect(TaxRate).to receive(:included_tax_amount_for).twice
- gross_amount
+ it 'still restates the price, reading the country off the zone' do
+ create(:tax_rate, country: home_country, tax_category: tax_category, amount: 0.19,
+ included_in_price: true)
+ create(:tax_rate, country: destination, tax_category: tax_category, amount: 0.25,
+ included_in_price: true)
+
+ expect(gross_amount).to eq(105.04)
end
end
end
diff --git a/spree/core/spec/models/spree/delivery_rate_spec.rb b/spree/core/spec/models/spree/delivery_rate_spec.rb
index 8e5df140ea..910c8c3732 100644
--- a/spree/core/spec/models/spree/delivery_rate_spec.rb
+++ b/spree/core/spec/models/spree/delivery_rate_spec.rb
@@ -11,16 +11,15 @@ describe Spree::DeliveryRate, type: :model do
context '#display_price' do
context 'when tax included in price' do
- let!(:default_zone) { create(:zone, default_tax: true) }
let(:default_tax_rate) do
create :tax_rate,
name: 'VAT',
amount: 0.1,
included_in_price: true,
- zone: default_zone
+ country: @default_country
end
- context 'when the tax rate is from the default zone' do
+ context 'when the rate taxes the home country' do
before { shipping_rate.tax_rate = default_tax_rate }
it 'shows correct tax amount' do
@@ -39,20 +38,18 @@ describe Spree::DeliveryRate, type: :model do
end
end
- context 'when the tax rate is from another zone' do
- let!(:non_default_zone) { create(:zone, default_tax: false) }
-
+ context 'when the rate taxes another country' do
let(:non_default_tax_rate) do
create :tax_rate,
name: 'VAT',
amount: 0.2,
included_in_price: true,
- zone: non_default_zone
+ country: create(:country, iso: 'FR', name: 'France')
end
before { shipping_rate.tax_rate = non_default_tax_rate }
- it "deducts the other zone's VAT from the calculated shipping rate" do
+ it "deducts the other country's VAT from the calculated shipping rate" do
expect(shipping_rate.display_price.to_s).
to eq("$10.00 (incl. $1.67 #{non_default_tax_rate.name})")
end
diff --git a/spree/core/spec/models/spree/line_item_spec.rb b/spree/core/spec/models/spree/line_item_spec.rb
index 3d5c320e3f..f274a671a6 100644
--- a/spree/core/spec/models/spree/line_item_spec.rb
+++ b/spree/core/spec/models/spree/line_item_spec.rb
@@ -263,19 +263,11 @@ describe Spree::LineItem, type: :model do
let(:variant) { create(:variant) }
before do
- create(:tax_rate, zone: tax_rate_zone, tax_category: variant.tax_category)
+ create(:tax_rate, country: rate_country, tax_category: variant.tax_category)
end
- context 'when order has a tax zone' do
- let(:tax_rate_zone) do
- create(:zone, default_tax: true, kind: 'country').tap do |zone|
- zone.zone_members.create!(zoneable: order.tax_address.country)
- end
- end
-
- before do
- expect(order.tax_zone).to be_present
- end
+ context 'when a rate covers the order destination' do
+ let(:rate_country) { order.tax_address.country }
it 'creates a tax adjustment' do
Spree::Orders::AddItem.call(order: order, variant: variant)
@@ -284,15 +276,8 @@ describe Spree::LineItem, type: :model do
end
end
- context 'when order does not have a tax zone' do
- let(:tax_rate_zone) { create(:zone, kind: 'country') }
-
- before do
- order.bill_address = nil
- order.ship_address = nil
- order.save
- expect(order.reload.tax_zone).to be_nil
- end
+ context 'when the only rate belongs to another country' do
+ let(:rate_country) { create(:country, iso: 'FR', name: 'France') }
it 'does not create a tax adjustment' do
Spree::Orders::AddItem.call(order: order, variant: variant)
diff --git a/spree/core/spec/models/spree/order/adjustments_spec.rb b/spree/core/spec/models/spree/order/adjustments_spec.rb
index a04ac3974d..02d77bcfbd 100644
--- a/spree/core/spec/models/spree/order/adjustments_spec.rb
+++ b/spree/core/spec/models/spree/order/adjustments_spec.rb
@@ -30,8 +30,6 @@ describe Spree::Order do
let!(:line_item) { create(:line_item) }
let!(:country) { create(:country) }
- let!(:zone) { create(:zone) }
- let!(:zone_member) { create(:zone_member, zone: zone, zoneable: country) }
let(:address) { create(:address, country: country) }
let!(:tax_category) { create(:tax_category) }
@@ -40,7 +38,7 @@ describe Spree::Order do
amount: 0.2,
included_in_price: true,
tax_category: tax_category,
- zone: zone
+ country: country
)
}
let!(:shipping_method) do
@@ -63,12 +61,15 @@ describe Spree::Order do
order.create_shipment_tax_charge!
end
- it 'removes the fulfillment tax lines' do
+ it 'charges no fulfillment tax' do
order.coupon_code = free_shipping_promotion.code
Spree::PromotionHandler::Coupon.new(order).apply
order.apply_free_shipping_promotions
- expect(order.tax_lines.for_fulfillments).to be_blank
+ # Free shipping leaves nothing to tax. The row stays, at zero: since 6.0 a
+ # matched rate always records its treatment, so reporting can tell a
+ # zero-tax delivery from one nobody assessed.
+ expect(order.tax_lines.for_fulfillments.sum(:amount)).to eq(0)
end
end
end
diff --git a/spree/core/spec/models/spree/order_updater_spec.rb b/spree/core/spec/models/spree/order_updater_spec.rb
index 58a584c1b6..bca6346b44 100644
--- a/spree/core/spec/models/spree/order_updater_spec.rb
+++ b/spree/core/spec/models/spree/order_updater_spec.rb
@@ -54,8 +54,9 @@ module Spree
end
it 'update order adjustments' do
- # Tax is provider-written; stub it out so the synthetic tax line survives.
- allow(Spree).to receive(:tax_provider).and_return(instance_double(Spree::TaxProvider::Internal, estimate: nil))
+ # Tax is provider-written, and the provider comes from the order's own
+ # market since 6.0; stub it so the synthetic tax line survives.
+ allow(order).to receive(:tax_provider).and_return(instance_double(Spree::TaxProvider::Internal, estimate: nil))
line_item = order.line_items.first
create(:discount, order: order, line_item: line_item, amount: -2.50, kind: 'manual')
diff --git a/spree/core/spec/models/spree/price_spec.rb b/spree/core/spec/models/spree/price_spec.rb
index 309384a699..b4cac99e27 100644
--- a/spree/core/spec/models/spree/price_spec.rb
+++ b/spree/core/spec/models/spree/price_spec.rb
@@ -200,24 +200,23 @@ describe Spree::Price, type: :model do
end
end
- describe '#price_including_vat_for(zone)' do
+ describe '#price_including_vat_for' do
subject(:price_with_vat) { price.price_including_vat_for(price_options) }
let(:variant) { create(:variant) }
- let(:default_zone) { Spree::Zone.new }
- let(:zone) { Spree::Zone.new }
+ let(:home_country) { Spree::Country.new(iso: 'DE') }
+ let(:destination) { Spree::Country.new(iso: 'FR') }
let(:amount) { 10 }
let(:tax_category) { Spree::TaxCategory.new }
let(:price) { build :price, variant: variant, amount: amount }
- let(:price_options) { { tax_zone: zone } }
+ let(:price_options) { { country: destination } }
- context 'when called with a non-default zone' do
+ context 'when the destination is another country' do
before do
allow(variant).to receive(:tax_category).and_return(tax_category)
- expect(price).to receive(:default_zone).at_least(:once).and_return(default_zone)
- allow(price).to receive(:apply_foreign_vat?).and_return(true)
- allow(price).to receive(:included_tax_amount).with({ tax_zone: default_zone, tax_category: tax_category }).and_return(0.19)
- allow(price).to receive(:included_tax_amount).with({ tax_zone: zone, tax_category: tax_category }).and_return(0.25)
+ expect(price).to receive(:default_tax_country).at_least(:once).and_return(home_country)
+ allow(price).to receive(:included_tax_amount).with({ country: home_country, tax_category: tax_category }).and_return(0.19)
+ allow(price).to receive(:included_tax_amount).with({ country: destination, tax_category: tax_category }).and_return(0.25)
end
it 'returns the correct price including another VAT to two digits' do
@@ -225,10 +224,10 @@ describe Spree::Price, type: :model do
end
end
- context 'when called from the default zone' do
+ context 'when the destination is the home country' do
before do
allow(variant).to receive(:tax_category).and_return(tax_category)
- expect(price).to receive(:default_zone).at_least(:once).and_return(zone)
+ expect(price).to receive(:default_tax_country).at_least(:once).and_return(destination)
end
it 'returns the correct price' do
@@ -237,38 +236,37 @@ describe Spree::Price, type: :model do
end
end
- context 'when no default zone is set' do
+ context 'when no home country is set' do
before do
allow(variant).to receive(:tax_category).and_return(tax_category)
- expect(price).to receive(:default_zone).at_least(:once).and_return(nil)
+ expect(price).to receive(:default_tax_country).at_least(:once).and_return(nil)
end
it 'returns the correct price' do
expect(price).to receive(:price).and_call_original
- expect(price.price_including_vat_for(tax_zone: zone)).to eq(10.00)
+ expect(price.price_including_vat_for(country: destination)).to eq(10.00)
end
end
end
- describe '#compare_at_price_including_vat_for(zone)' do
+ describe '#compare_at_price_including_vat_for' do
subject(:compare_at_price_with_vat) { price.compare_at_price_including_vat_for(price_options) }
let(:variant) { create(:variant) }
- let(:default_zone) { Spree::Zone.new }
- let(:zone) { Spree::Zone.new }
+ let(:home_country) { Spree::Country.new(iso: 'DE') }
+ let(:destination) { Spree::Country.new(iso: 'FR') }
let(:amount) { 10 }
let(:compare_at_amount) { 100 }
let(:tax_category) { Spree::TaxCategory.new }
let(:price) { build :price, variant: variant, amount: amount, compare_at_amount: compare_at_amount }
- let(:price_options) { { tax_zone: zone } }
+ let(:price_options) { { country: destination } }
- context 'when called with a non-default zone' do
+ context 'when the destination is another country' do
before do
allow(variant).to receive(:tax_category).and_return(tax_category)
- expect(price).to receive(:default_zone).at_least(:once).and_return(default_zone)
- allow(price).to receive(:apply_foreign_vat?).and_return(true)
- allow(price).to receive(:included_tax_amount).with({ tax_zone: default_zone, tax_category: tax_category }).and_return(0.19)
- allow(price).to receive(:included_tax_amount).with({ tax_zone: zone, tax_category: tax_category }).and_return(0.25)
+ expect(price).to receive(:default_tax_country).at_least(:once).and_return(home_country)
+ allow(price).to receive(:included_tax_amount).with({ country: home_country, tax_category: tax_category }).and_return(0.19)
+ allow(price).to receive(:included_tax_amount).with({ country: destination, tax_category: tax_category }).and_return(0.25)
end
it 'returns the correct price including another VAT to two digits' do
@@ -276,10 +274,10 @@ describe Spree::Price, type: :model do
end
end
- context 'when called from the default zone' do
+ context 'when the destination is the home country' do
before do
allow(variant).to receive(:tax_category).and_return(tax_category)
- expect(price).to receive(:default_zone).at_least(:once).and_return(zone)
+ expect(price).to receive(:default_tax_country).at_least(:once).and_return(destination)
end
it 'returns the correct price' do
@@ -288,15 +286,15 @@ describe Spree::Price, type: :model do
end
end
- context 'when no default zone is set' do
+ context 'when no home country is set' do
before do
allow(variant).to receive(:tax_category).and_return(tax_category)
- expect(price).to receive(:default_zone).at_least(:once).and_return(nil)
+ expect(price).to receive(:default_tax_country).at_least(:once).and_return(nil)
end
it 'returns the correct price' do
expect(price).to receive(:compare_at_price).and_call_original
- expect(price.compare_at_price_including_vat_for(tax_zone: zone)).to eq(100.00)
+ expect(price.compare_at_price_including_vat_for(country: destination)).to eq(100.00)
end
end
end
diff --git a/spree/core/spec/models/spree/promotion_handler/coupon_spec.rb b/spree/core/spec/models/spree/promotion_handler/coupon_spec.rb
index b4ea79a65c..6e22cff145 100644
--- a/spree/core/spec/models/spree/promotion_handler/coupon_spec.rb
+++ b/spree/core/spec/models/spree/promotion_handler/coupon_spec.rb
@@ -224,9 +224,8 @@ module Spree
context 'for an order with taxable line items' do
let!(:order) { create(:order, line_items_price: 0.0, store: store) }
- let!(:zone) { create(:zone_with_country, default_tax: true) }
let!(:tax_category) { create(:tax_category, name: 'Taxable Foo') }
- let!(:rate) { create(:tax_rate, amount: 0.10, tax_category: tax_category, zone: zone) }
+ let!(:rate) { create(:tax_rate, amount: 0.10, tax_category: tax_category, country: @default_country) }
context 'and the product price is less than promo discount' do
diff --git a/spree/core/spec/models/spree/promotion_tax_recalculation_spec.rb b/spree/core/spec/models/spree/promotion_tax_recalculation_spec.rb
index 6e1174c2d1..b4c18d146a 100644
--- a/spree/core/spec/models/spree/promotion_tax_recalculation_spec.rb
+++ b/spree/core/spec/models/spree/promotion_tax_recalculation_spec.rb
@@ -10,11 +10,6 @@ describe 'Promotion discounts and the taxable basis', type: :model do
let(:store) { @default_store }
let(:tax_category) { create(:tax_category) }
- let!(:zone) do
- create(:zone, name: 'Default tax zone', kind: 'country', default_tax: true).tap do |zone|
- zone.zone_members.create!(zoneable: @default_country)
- end
- end
let(:product) { create(:product, price: item_price, tax_category: tax_category, store: store) }
let(:order) { create(:order, store: store, currency: 'USD') }
@@ -74,7 +69,7 @@ describe 'Promotion discounts and the taxable basis', type: :model do
context 'with additional (US-style) tax of 10%' do
let(:item_price) { 250 }
let!(:tax_rate) do
- create(:tax_rate, zone: zone, tax_category: tax_category, amount: 0.10, included_in_price: false)
+ create(:tax_rate, country: @default_country, tax_category: tax_category, amount: 0.10, included_in_price: false)
end
before { build_cart! }
@@ -162,7 +157,7 @@ describe 'Promotion discounts and the taxable basis', type: :model do
context 'with included (VAT-style) tax of 20%' do
let(:item_price) { 350 }
let!(:tax_rate) do
- create(:tax_rate, zone: zone, tax_category: tax_category, amount: 0.20, included_in_price: true)
+ create(:tax_rate, country: @default_country, tax_category: tax_category, amount: 0.20, included_in_price: true)
end
before { build_cart! }
diff --git a/spree/core/spec/models/spree/stock/estimator_spec.rb b/spree/core/spec/models/spree/stock/estimator_spec.rb
index 1e1f321709..0652a3f2ce 100644
--- a/spree/core/spec/models/spree/stock/estimator_spec.rb
+++ b/spree/core/spec/models/spree/stock/estimator_spec.rb
@@ -163,7 +163,7 @@ module Spree
zone.zone_members.create!(zoneable: ship_address.country)
end
end
- let!(:tax_rate) { create(:tax_rate, zone: default_tax_zone) }
+ let!(:tax_rate) { create(:tax_rate, country: order.tax_address.country) }
before do
Spree::ShippingMethod.all.each do |sm|
@@ -183,52 +183,61 @@ module Spree
let(:tax_category) { create :tax_category }
let!(:shipping_method) { create(:shipping_method, tax_category: tax_category) }
- let(:default_zone) { create(:zone_with_country, default_tax: true) }
+ # Shipping is priced including the home country's VAT.
+ let(:home_country) { order.tax_address.country }
let!(:default_vat) do
create :tax_rate,
included_in_price: true,
- zone: default_zone,
+ country: home_country,
amount: 0.2,
tax_category: shipping_method.tax_category
end
- context 'when the order does not have a tax zone' do
- before { allow(order).to receive(:tax_zone).and_return nil }
+ before { @default_store.default_market.update!(countries: [home_country]) }
+
+ context 'when the order has no address to tax against' do
+ before { allow(order).to receive(:tax_address).and_return nil }
it_behaves_like 'shipping rate matches'
end
- context "when the order's tax zone is the default zone" do
- before { allow(order).to receive(:tax_zone).and_return(default_zone) }
-
+ context 'when the order ships within the home country' do
it_behaves_like 'shipping rate matches'
end
- context "when the order's tax zone is a non-VAT zone" do
- let!(:zone_without_vat) { create(:zone_with_country) }
+ context 'when the order ships to a country with no VAT' do
+ let(:country_without_vat) { create(:country, iso: 'IN', name: 'India') }
- before { allow(order).to receive(:tax_zone).and_return(zone_without_vat) }
+ before do
+ allow(order).to receive(:tax_address).
+ and_return(create(:address, country: country_without_vat, state: nil,
+ state_name: 'Delhi', zipcode: '110001'))
+ end
- it 'deducts the default VAT from the cost' do
+ it 'deducts the home VAT from the cost' do
delivery_rates = subject.delivery_rates(package)
# deduct default vat: 4.00 / 1.2 = 3.33 (rounded)
expect(delivery_rates.first.cost).to eq(3.33)
end
end
- context "when the order's tax zone is a zone with VAT outside the default zone" do
- let(:other_vat_zone) { create(:zone_with_country) }
+ context 'when the order ships to another country that charges VAT' do
+ let(:other_country) { create(:country, iso: 'FR', name: 'France') }
let!(:other_vat) do
create :tax_rate,
included_in_price: true,
- zone: other_vat_zone,
+ country: other_country,
amount: 0.3,
tax_category: shipping_method.tax_category
end
- before { allow(order).to receive(:tax_zone).and_return(other_vat_zone) }
+ before do
+ allow(order).to receive(:tax_address).
+ and_return(create(:address, country: other_country, state: nil,
+ state_name: 'Paris', zipcode: '75001'))
+ end
- it 'deducts the default vat and applies the foreign vat to calculate the price' do
+ it 'deducts the home vat and applies the foreign vat to calculate the price' do
delivery_rates = subject.delivery_rates(package)
#
# deduct default vat: 4.00 / 1.2 = 3.33 (rounded)
diff --git a/spree/core/spec/models/spree/tax_provider/internal_spec.rb b/spree/core/spec/models/spree/tax_provider/internal_spec.rb
index d02da044d8..7f28d46a34 100644
--- a/spree/core/spec/models/spree/tax_provider/internal_spec.rb
+++ b/spree/core/spec/models/spree/tax_provider/internal_spec.rb
@@ -5,15 +5,12 @@ describe Spree::TaxProvider::Internal, type: :model do
let(:order) { create(:order_with_line_items, line_items_count: 1) }
let(:line_item) { order.line_items.first }
- let(:zone) do
- create(:zone, kind: 'country', default_tax: true).tap do |zone|
- zone.members.create!(zoneable: order.tax_address.country)
- end
- end
+ # Rates name their country directly since 6.0 — the order's own tax address.
+ let(:country) { order.tax_address.country }
describe '#estimate' do
context 'with an additional rate' do
- let!(:rate) { create(:tax_rate, zone: zone, amount: 0.1, tax_category: line_item.tax_category, included_in_price: false) }
+ let!(:rate) { create(:tax_rate, country: country, amount: 0.1, tax_category: line_item.tax_category, included_in_price: false) }
it 'writes a tax line with snapshots' do
provider.estimate(order)
@@ -51,12 +48,11 @@ describe Spree::TaxProvider::Internal, type: :model do
expect(order.tax_lines.count).to eq(1)
end
- it 'removes lines when the zone stops matching' do
+ it 'removes lines when the rate stops covering the destination' do
provider.estimate(order)
expect(order.tax_lines.reload.count).to eq(1)
- zone.members.delete_all
- allow(order).to receive(:tax_zone).and_return(nil)
+ rate.update!(country: create(:country, iso: 'JP', name: 'Japan'))
provider.estimate(order)
expect(order.tax_lines.reload).to be_empty
end
@@ -71,7 +67,7 @@ describe Spree::TaxProvider::Internal, type: :model do
end
context 'with an included (VAT) rate' do
- let!(:rate) { create(:tax_rate, zone: zone, amount: 0.2, tax_category: line_item.tax_category, included_in_price: true) }
+ let!(:rate) { create(:tax_rate, country: country, amount: 0.2, tax_category: line_item.tax_category, included_in_price: true) }
it 'backs the tax out of the gross basis' do
provider.estimate(order)
@@ -84,7 +80,7 @@ describe Spree::TaxProvider::Internal, type: :model do
end
context 'with a matched zero rate' do
- let!(:rate) { create(:tax_rate, zone: zone, amount: 0, tax_category: line_item.tax_category, included_in_price: false) }
+ let!(:rate) { create(:tax_rate, country: country, amount: 0, tax_category: line_item.tax_category, included_in_price: false) }
it 'still writes a row, marked zero-rated' do
provider.estimate(order)
@@ -98,7 +94,7 @@ describe Spree::TaxProvider::Internal, type: :model do
end
context 'with no matching rate' do
- let!(:rate) { create(:tax_rate, zone: zone, amount: 0.1, tax_category: create(:tax_category), included_in_price: false) }
+ let!(:rate) { create(:tax_rate, country: country, amount: 0.1, tax_category: create(:tax_category), included_in_price: false) }
it 'writes nothing, having formed no opinion' do
provider.estimate(order)
@@ -110,13 +106,9 @@ describe Spree::TaxProvider::Internal, type: :model do
context 'when the owner is a cart' do
let(:cart) { create(:cart_with_line_items, line_items_count: 1, ship_address: create(:address)) }
let(:cart_line_item) { cart.line_items.first }
- let(:cart_zone) do
- create(:zone, kind: 'country', default_tax: true).tap do |zone|
- zone.members.create!(zoneable: cart.tax_address.country)
- end
- end
+ let(:cart_country) { cart.tax_address.country }
let!(:rate) do
- create(:tax_rate, zone: cart_zone, amount: 0.1, tax_category: cart_line_item.tax_category, included_in_price: false)
+ create(:tax_rate, country: cart_country, amount: 0.1, tax_category: cart_line_item.tax_category, included_in_price: false)
end
it 'owns the row through the cart FK' do
@@ -130,7 +122,7 @@ describe Spree::TaxProvider::Internal, type: :model do
end
context 'with an exemption covering the sale' do
- let!(:rate) { create(:tax_rate, zone: zone, amount: 0.1, tax_category: line_item.tax_category, included_in_price: false) }
+ let!(:rate) { create(:tax_rate, country: country, amount: 0.1, tax_category: line_item.tax_category, included_in_price: false) }
let(:exemption) { Spree::TaxExemption.new(reason_code: 'resale', certificate_number: 'CERT-1') }
it 'writes a zero row recording the claim' do
@@ -164,7 +156,7 @@ describe Spree::TaxProvider::Internal, type: :model do
end
context 'with an exemption against an included (VAT) rate' do
- let!(:rate) { create(:tax_rate, zone: zone, amount: 0.2, tax_category: line_item.tax_category, included_in_price: true) }
+ let!(:rate) { create(:tax_rate, country: country, amount: 0.2, tax_category: line_item.tax_category, included_in_price: true) }
it 'leaves the whole basis pre-tax, having no tax to back out' do
provider.estimate(order, exemptions: [Spree::TaxExemption.new(reason_code: 'government')])
@@ -176,7 +168,7 @@ describe Spree::TaxProvider::Internal, type: :model do
context 'with a taxable fee' do
let!(:rate) do
- create(:tax_rate, zone: zone, amount: 0.1, tax_category: create(:tax_category, is_default: true), included_in_price: false)
+ create(:tax_rate, country: country, amount: 0.1, tax_category: create(:tax_category, is_default: true), included_in_price: false)
end
let!(:fee) { create(:fee, order: order, amount: 5, kind: 'surcharge', label: 'Handling') }
diff --git a/spree/core/spec/models/spree/tax_rate_spec.rb b/spree/core/spec/models/spree/tax_rate_spec.rb
index 8407baeeac..42f9d77110 100644
--- a/spree/core/spec/models/spree/tax_rate_spec.rb
+++ b/spree/core/spec/models/spree/tax_rate_spec.rb
@@ -1,197 +1,119 @@
require 'spec_helper'
describe Spree::TaxRate, type: :model do
- context 'match' do
- let(:order) { create(:order) }
- let(:country) { create(:country) }
+ describe '.match' do
+ let(:germany) { create(:country, iso: 'DE', name: 'Germany') }
+ let(:berlin) { create(:state, country: germany, abbr: 'BE', name: 'Berlin') }
+ let(:france) { create(:country, iso: 'FR', name: 'France') }
let(:tax_category) { create(:tax_category) }
+ let(:german_address) { create(:address, country: germany, state: nil, state_name: 'Berlin') }
- it 'returns an empty array when tax_zone is nil' do
- allow(order).to receive_messages tax_zone: nil
- expect(Spree::TaxRate.match(order.tax_zone)).to eq([])
+ it 'matches nothing without an address' do
+ expect(described_class.match(nil)).to be_empty
end
- context 'when no rate zones match the tax zone' do
- before do
- Spree::TaxRate.create(name: 'Tax Rate #1', amount: 1, zone: create(:zone))
- end
+ it 'matches a rate for the address country' do
+ rate = create(:tax_rate, country: germany, tax_category: tax_category)
+ create(:tax_rate, country: france, tax_category: tax_category)
- context 'when there is no default tax zone' do
- before do
- @zone = create(:zone, name: 'Country Zone', kind: 'country', default_tax: false, zone_members: [])
- @zone.zone_members.create(zoneable: country)
- end
+ expect(described_class.match(german_address)).to eq([rate])
+ end
- it 'returns an empty array' do
- allow(order).to receive_messages tax_zone: @zone
- expect(Spree::TaxRate.match(order.tax_zone)).to eq([])
- end
+ it 'matches every rate configured for that country' do
+ standard = create(:tax_rate, country: germany, amount: 0.19, tax_category: tax_category)
+ reduced = create(:tax_rate, country: germany, amount: 0.07, tax_category: create(:tax_category))
- it 'returns the rate that matches the rate zone' do
- rate = Spree::TaxRate.create(
- name: 'Tax Rate #1',
- amount: 1,
- zone: @zone,
- tax_category: tax_category
- )
+ expect(described_class.match(german_address)).to match_array([standard, reduced])
+ end
- allow(order).to receive_messages tax_zone: @zone
- expect(Spree::TaxRate.match(order.tax_zone)).to eq([rate])
- end
+ it 'matches a rate that taxes every country' do
+ worldwide = create(:tax_rate, :worldwide, tax_category: tax_category)
- it 'returns all rates that match the rate zone' do
- rate1 = Spree::TaxRate.create(
- name: 'Tax Rate #1',
- amount: 1,
- zone: @zone,
- tax_category: tax_category
- )
-
- rate2 = Spree::TaxRate.create(
- name: 'Tax Rate #2',
- amount: 2,
- zone: @zone,
- tax_category: tax_category
- )
-
- allow(order).to receive_messages tax_zone: @zone
- expect(Spree::TaxRate.match(order.tax_zone)).to match_array([rate1, rate2])
- end
+ expect(described_class.match(german_address)).to eq([worldwide])
+ end
- context 'when the tax_zone is contained within a rate zone' do
- before do
- sub_zone = create(:zone, name: 'State Zone', kind: 'state', zone_members: [])
- sub_zone.zone_members.create(zoneable: create(:state, country: country))
- allow(order).to receive_messages tax_zone: sub_zone
- @rate = Spree::TaxRate.create(
- name: 'Tax Rate #1',
- amount: 1,
- zone: @zone,
- tax_category: tax_category
- )
- end
-
- it 'returns the rate zone' do
- expect(Spree::TaxRate.match(order.tax_zone)).to eq([@rate])
- end
- end
- end
+ context 'with state-level rates' do
+ let(:berlin_address) { create(:address, country: germany, state: berlin) }
- context 'when there is a default tax zone' do
- subject { Spree::TaxRate.match(order.tax_zone) }
+ it 'matches the state rate alongside the country rate' do
+ country_rate = create(:tax_rate, country: germany, tax_category: tax_category)
+ state_rate = create(:tax_rate, country: germany, state: berlin, tax_category: create(:tax_category))
- before do
- @zone = create(:zone, name: 'Country Zone', kind: 'country', default_tax: true, zone_members: [])
- @zone.zone_members.create(zoneable: country)
- end
+ expect(described_class.match(berlin_address)).to match_array([country_rate, state_rate])
+ end
- let(:included_in_price) { false }
- let!(:rate) do
- Spree::TaxRate.create(name: 'Tax Rate #1',
- amount: 1,
- zone: @zone,
- tax_category: tax_category,
- included_in_price: included_in_price)
- end
+ it 'does not match another state rate' do
+ hamburg = create(:state, country: germany, abbr: 'HH', name: 'Hamburg')
+ create(:tax_rate, country: germany, state: hamburg, tax_category: tax_category)
- context 'when the order has the same tax zone' do
- before do
- allow(order).to receive_messages tax_zone: @zone
- end
+ expect(described_class.match(berlin_address)).to be_empty
+ end
- context 'when the tax is not a VAT' do
- it { is_expected.to eq([rate]) }
- end
+ it 'does not match a state rate for an address with no state' do
+ create(:tax_rate, country: germany, state: berlin, tax_category: tax_category)
- context 'when the tax is a VAT' do
- let(:included_in_price) { true }
+ expect(described_class.match(german_address)).to be_empty
+ end
+ end
- it { is_expected.to eq([rate]) }
- end
- end
+ it 'rejects a state that is not in the rate country' do
+ rate = build(:tax_rate, country: france, state: berlin)
- context 'when the order has a different tax zone' do
- before do
- allow(order).to receive_messages tax_zone: build(:zone, kind: 'country', name: 'Other Zone')
- end
-
- context 'when the tax is a VAT' do
- let(:included_in_price) { true }
-
- # The rate should NOT match in this instance because:
- # The order has a different tax zone, and the price is
- # henceforth a net price and will not change.
- it 'return no tax rate' do
- expect(subject).to be_empty
- end
- end
-
- context 'when the tax is not VAT' do
- it 'returns no tax rate' do
- expect(subject).to be_empty
- end
- end
- end
- end
+ expect(rate).not_to be_valid
+ expect(rate.errors[:state]).to be_present
end
end
describe 'estimating through the tax provider' do
+ # The EU one-stop-shop case: a catalogue priced gross in the store's own
+ # country, restated for a customer taxed somewhere else.
context 'for MOSS taxation in Europe' do
- let(:germany) { create :country, name: 'Germany' }
- let(:india) { create :country, name: 'India' }
- let(:france) { create :country, name: 'France' }
- let(:france_zone) { create :zone_with_country, kind: 'country', name: 'France Zone' }
- let(:germany_zone) { create :zone_with_country, kind: 'country', name: 'Germany Zone', default_tax: true }
- let(:india_zone) { create :zone_with_country, kind: 'country', name: 'India' }
- let(:moss_category) { Spree::TaxCategory.create(name: 'Digital Goods') }
- let(:normal_category) { Spree::TaxCategory.create(name: 'Analogue Goods') }
- let(:eu_zone) { create(:zone, name: 'EU') }
+ let(:germany) { create(:country, iso: 'DE', name: 'Germany') }
+ let(:france) { create(:country, iso: 'FR', name: 'France') }
+ let(:india) { create(:country, iso: 'IN', name: 'India') }
+
+ let(:moss_category) { create(:tax_category, name: 'Digital Goods') }
+ let(:normal_category) { create(:tax_category, name: 'Analogue Goods') }
let!(:german_vat) do
- Spree::TaxRate.create(
- name: 'German VAT',
- amount: 0.19,
- tax_category: moss_category,
- zone: germany_zone,
- included_in_price: true
- )
+ create(:tax_rate, name: 'German VAT', amount: 0.19, tax_category: moss_category,
+ country: germany, included_in_price: true)
end
let!(:french_vat) do
- Spree::TaxRate.create(
- name: 'French VAT',
- amount: 0.25,
- tax_category: moss_category,
- zone: france_zone,
- included_in_price: true
- )
+ create(:tax_rate, name: 'French VAT', amount: 0.25, tax_category: moss_category,
+ country: france, included_in_price: true)
end
- let!(:eu_vat) do
- Spree::TaxRate.create(
- name: 'EU_VAT',
- amount: 0.19,
- tax_category: normal_category,
- zone: eu_zone,
- included_in_price: true
- )
+ # Physical goods are taxed where they ship from, so both EU countries
+ # carry the same rate — one row each, a zone spanning both being gone.
+ let!(:german_goods_vat) do
+ create(:tax_rate, name: 'EU VAT (DE)', amount: 0.19, tax_category: normal_category,
+ country: germany, included_in_price: true)
+ end
+ let!(:french_goods_vat) do
+ create(:tax_rate, name: 'EU VAT (FR)', amount: 0.19, tax_category: normal_category,
+ country: france, included_in_price: true)
end
let(:download) { create(:product, tax_category: moss_category, price: 100) }
let(:tshirt) { create(:product, tax_category: normal_category, price: 100) }
- let(:order) { Spree::Order.create }
-
- before do
- germany_zone.zone_members.create(zoneable: germany)
- france_zone.zone_members.create(zoneable: france)
- india_zone.zone_members.create(zoneable: india)
- eu_zone.zone_members.create(zoneable: germany)
- eu_zone.zone_members.create(zoneable: france)
+ let(:order) { create(:order, ship_address: nil, bill_address: nil) }
+
+ # Prices are quoted including German VAT — the market being browsed is
+ # Germany, which is where the store's own country comes from.
+ before { @default_store.default_market.update!(countries: [germany]) }
+
+ ZIPCODES = { 'DE' => '10115', 'FR' => '75001', 'IN' => '110001' }.freeze
+
+ def ship_to(country)
+ order.update!(
+ ship_address: create(:address, country: country, state: nil, state_name: 'Somewhere',
+ zipcode: ZIPCODES.fetch(country.iso))
+ )
+ order.update_line_item_prices!
end
context 'a download' do
- before do
- Spree::Orders::AddItem.call(order: order, variant: download.default_variant)
- end
+ before { Spree::Orders::AddItem.call(order: order, variant: download.default_variant) }
it 'without an address costs 100 euros including tax' do
order.recalculate_totals!
@@ -200,15 +122,14 @@ describe Spree::TaxRate, type: :model do
end
it 'to germany costs 100 euros including tax' do
- allow(order).to receive(:tax_zone).and_return(germany_zone)
+ ship_to(germany)
order.recalculate_totals!
expect(order.display_total).to eq(Spree::Money.new(100))
expect(order.included_tax_total).to eq(15.97)
end
it 'to france costs more including tax' do
- allow(order).to receive(:tax_zone).and_return(france_zone)
- order.update_line_item_prices!
+ ship_to(france)
order.recalculate_totals!
expect(order.display_total).to eq(Spree::Money.new(105.04))
expect(order.included_tax_total).to eq(21.01)
@@ -216,8 +137,7 @@ describe Spree::TaxRate, type: :model do
end
it 'to somewhere else costs the net amount' do
- allow(order).to receive(:tax_zone).and_return(india_zone)
- order.update_line_item_prices!
+ ship_to(india)
order.recalculate_totals!
expect(order.included_tax_total).to eq(0)
expect(order.display_total).to eq(Spree::Money.new(84.03))
@@ -225,18 +145,17 @@ describe Spree::TaxRate, type: :model do
end
context 'a t-shirt' do
+ before { Spree::Orders::AddItem.call(order: order, variant: tshirt.default_variant) }
+
it 'to germany costs 100 euros including tax' do
- allow(order).to receive(:tax_zone).and_return(germany_zone)
- Spree::Orders::AddItem.call(order: order, variant: tshirt.default_variant)
+ ship_to(germany)
order.recalculate_totals!
expect(order.display_total).to eq(Spree::Money.new(100))
expect(order.included_tax_total).to eq(15.97)
end
it 'to france costs 100 euros including tax' do
- allow(order).to receive(:tax_zone).and_return(france_zone)
- Spree::Orders::AddItem.call(order: order, variant: tshirt.default_variant)
- order.update_line_item_prices!
+ ship_to(france)
order.recalculate_totals!
expect(order.display_total).to eq(Spree::Money.new(100.00))
expect(order.included_tax_total).to eq(15.97)
@@ -244,9 +163,7 @@ describe Spree::TaxRate, type: :model do
end
it 'to somewhere else costs the net amount' do
- allow(order).to receive(:tax_zone).and_return(india_zone)
- Spree::Orders::AddItem.call(order: order, variant: tshirt.default_variant)
- order.update_line_item_prices!
+ ship_to(india)
order.recalculate_totals!
expect(order.included_tax_total).to eq(0)
expect(order.display_total).to eq(Spree::Money.new(84.03))
@@ -255,23 +172,16 @@ describe Spree::TaxRate, type: :model do
end
describe 'tax line lifecycle around rate deletion' do
- let!(:order) { create(:order) }
- let(:zone) do
- create(:zone, name: 'Country Zone', kind: 'country', default_tax: true, zone_members: []).tap do |zone|
- zone.zone_members.create(zoneable: create(:country))
- end
- end
- let(:category) { Spree::TaxCategory.create(name: 'Taxable Foo') }
+ let(:country) { Spree::Country.find_by(iso: 'US') || create(:country) }
+ let!(:order) { create(:order, ship_address: create(:address, country: country)) }
+ let(:category) { create(:tax_category, name: 'Taxable Foo') }
let!(:rate) do
- Spree::TaxRate.create(name: 'Tax Rate #1', amount: 0.1, tax_category: category, zone: zone)
+ create(:tax_rate, name: 'Tax Rate #1', amount: 0.1, tax_category: category, country: country)
end
let(:taxable) { create(:product, tax_category: category) }
let!(:line_item) { Spree::Orders::AddItem.call(order: order, variant: taxable.default_variant).value }
- before do
- allow_any_instance_of(Spree::Order).to receive(:tax_zone).and_return(zone)
- Spree.tax_provider.estimate(order)
- end
+ before { order.tax_provider.estimate(order) }
it 'keeps the tax line with snapshots when the rate is deleted' do
expect(order.tax_lines.reload.count).to eq(1)
@@ -284,7 +194,7 @@ describe Spree::TaxRate, type: :model do
it 'drops the line on the next estimate after the rate is gone' do
rate.destroy!
- Spree.tax_provider.estimate(order)
+ order.tax_provider.estimate(order)
expect(order.tax_lines.reload).to be_empty
end
end
@@ -294,53 +204,44 @@ describe Spree::TaxRate, type: :model do
subject(:included_tax_amount) { Spree::TaxRate.included_tax_amount_for(price_options) }
let!(:order) { create :order_with_line_items }
- let!(:default_tax_zone) do
- create(:zone, default_tax: true, kind: 'country').tap do |zone|
- zone.zone_members.create!(zoneable: order.tax_address.country)
- end
- end
+ let(:line_item) { order.line_items.first }
+ let(:country) { order.tax_address.country }
+
let!(:included_tax_rate) do
- create :tax_rate,
- included_in_price: true,
- tax_category: order.line_items.first.tax_category,
- zone: order.tax_zone,
- amount: 0.4
+ create(:tax_rate, included_in_price: true, tax_category: line_item.tax_category,
+ country: country, amount: 0.4)
end
-
let!(:other_included_tax_rate) do
- create :tax_rate,
- included_in_price: true,
- tax_category: order.line_items.first.tax_category,
- zone: order.tax_zone,
- amount: 0.05
+ create(:tax_rate, included_in_price: true, tax_category: line_item.tax_category,
+ country: country, amount: 0.05)
end
-
+ # Additional tax is added to the price rather than sitting inside it.
let!(:additional_tax_rate) do
- create :tax_rate,
- included_in_price: false,
- tax_category: order.line_items.first.tax_category,
- zone: order.tax_zone,
- amount: 0.2
+ create(:tax_rate, included_in_price: false, tax_category: line_item.tax_category,
+ country: country, amount: 0.2)
end
-
let!(:included_tax_rate_from_somewhere_else) do
- create :tax_rate,
- included_in_price: true,
- tax_category: order.line_items.first.tax_category,
- zone: create(:zone_with_country),
- amount: 0.1
+ create(:tax_rate, included_in_price: true, tax_category: line_item.tax_category,
+ country: create(:country, iso: 'JP', name: 'Japan'), amount: 0.1)
end
- let(:price_options) do
- {
- tax_zone: order.tax_zone,
- tax_category: line_item.tax_category
- }
+
+ let(:price_options) { { country: country, tax_category: line_item.tax_category } }
+
+ it 'sums only the included rates of that jurisdiction' do
+ expect(included_tax_amount).to eq(0.45)
end
- let(:line_item) { order.line_items.first }
+ it 'accepts an address instead of a country' do
+ expect(described_class.included_tax_amount_for(address: order.tax_address,
+ tax_category: line_item.tax_category)).to eq(0.45)
+ end
+
+ it 'is zero without a jurisdiction' do
+ expect(described_class.included_tax_amount_for(tax_category: line_item.tax_category)).to eq(0)
+ end
- it 'will only get me tax amounts from tax_rates that match' do
- expect(subject).to eq(included_tax_rate.amount + other_included_tax_rate.amount)
+ it 'is zero without a tax category' do
+ expect(described_class.included_tax_amount_for(country: country)).to eq(0)
end
end
diff --git a/spree/core/spec/workflows/spree/carts/add_item_spec.rb b/spree/core/spec/workflows/spree/carts/add_item_spec.rb
index 6f0c0271b8..d1d4d10d47 100644
--- a/spree/core/spec/workflows/spree/carts/add_item_spec.rb
+++ b/spree/core/spec/workflows/spree/carts/add_item_spec.rb
@@ -89,7 +89,7 @@ module Spree
amount: 0.25,
included_in_price: true,
tax_category: category,
- zone: create(:zone_with_country, default_tax: true)
+ country: @default_country
)
end
let(:variant) { create(:variant, price: 1000) }