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 1: bind tax rates and tax categories to a store

Łukasz Adamczykcfbca31291d4
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rb
index 2d9ab8943d..1ea4263a27 100644
--- a/spree/core/app/models/spree/product.rb
+++ b/spree/core/app/models/spree/product.rb
@@ -496,7 +496,7 @@ module Spree
# Returns tax category for Product
# @return [Spree::TaxCategory, nil]
def tax_category
- @tax_category ||= super || TaxCategory.default
+ @tax_category ||= super || TaxCategory.default(assignable_store)
end
def first_or_default_variant(currency)
@@ -900,7 +900,7 @@ module Spree
end
def assign_default_tax_category
- self.tax_category = Spree::TaxCategory.default if new_record? && self[:tax_category_id].blank?
+ self.tax_category = Spree::TaxCategory.default(assignable_store) if new_record? && self[:tax_category_id].blank?
end
def run_touch_callbacks
diff --git a/spree/core/app/models/spree/store.rb b/spree/core/app/models/spree/store.rb
index 75da4af8f7..6be7d11946 100644
--- a/spree/core/app/models/spree/store.rb
+++ b/spree/core/app/models/spree/store.rb
@@ -127,6 +127,9 @@ module Spree
has_many :stock_locations, class_name: 'Spree::StockLocation', dependent: :nullify
has_many :promotions, class_name: 'Spree::Promotion', dependent: :nullify
+ has_many :tax_categories, class_name: 'Spree::TaxCategory', dependent: :destroy, inverse_of: :store
+ has_many :tax_rates, class_name: 'Spree::TaxRate', dependent: :destroy, inverse_of: :store
+
has_many :wishlists, class_name: 'Spree::Wishlist'
has_many :data_feeds, class_name: 'Spree::DataFeed'
diff --git a/spree/core/app/models/spree/tax_category.rb b/spree/core/app/models/spree/tax_category.rb
index 2c9a3f02e2..bf93fd126d 100644
--- a/spree/core/app/models/spree/tax_category.rb
+++ b/spree/core/app/models/spree/tax_category.rb
@@ -3,7 +3,11 @@ module Spree
has_prefix_id :taxcat
acts_as_paranoid
- validates :name, presence: true, uniqueness: { case_sensitive: false, scope: spree_base_uniqueness_scope + [:deleted_at] }
+
+ include Spree::SingleStoreResource
+
+ validates :name, presence: true,
+ uniqueness: { case_sensitive: false, scope: [:store_id], conditions: -> { where(deleted_at: nil) } }
has_many :tax_rates, dependent: :destroy, inverse_of: :tax_category
has_many :products, dependent: :nullify
@@ -13,14 +17,20 @@ module Spree
self.whitelisted_ransackable_attributes = %w[name is_default tax_code]
- def self.default
- find_by(is_default: true)
+ # The category applied to taxable records that carry none of their own.
+ #
+ # @param store [Spree::Store, nil] store to look within, the current store by default
+ # @return [Spree::TaxCategory, nil]
+ def self.default(store = Spree::Current.store)
+ scope = store ? for_store(store) : all
+ scope.find_by(is_default: true)
end
def set_default_category
- # set existing default tax category to false if this one has been marked as default
+ # only one category per store is the default, so marking this one demotes
+ # the store's previous holder
- if is_default && tax_category = self.class.where(is_default: true).where.not(id: id).first
+ if is_default && tax_category = self.class.where(store_id: store_id, is_default: true).where.not(id: id).first
tax_category.update_columns(is_default: false, updated_at: Time.current)
end
end
diff --git a/spree/core/app/models/spree/tax_rate.rb b/spree/core/app/models/spree/tax_rate.rb
index 3a8cb95400..cef05ef3f7 100644
--- a/spree/core/app/models/spree/tax_rate.rb
+++ b/spree/core/app/models/spree/tax_rate.rb
@@ -9,6 +9,7 @@ module Spree
include Spree::Metafields
include Spree::Metadata
+ include Spree::SingleStoreResource
with_options inverse_of: :tax_rates do
belongs_to :zone, class_name: 'Spree::Zone', optional: true
diff --git a/spree/core/app/services/spree/seeds/all.rb b/spree/core/app/services/spree/seeds/all.rb
index edc5c020d8..94636ad65c 100644
--- a/spree/core/app/services/spree/seeds/all.rb
+++ b/spree/core/app/services/spree/seeds/all.rb
@@ -17,7 +17,6 @@ module Spree
# additional data
ReturnsEnvironment.call
StoreCreditCategories.call
- TaxCategories.call
DigitalDelivery.call
# store & stock location
@@ -28,6 +27,7 @@ module Spree
# add store resources
PaymentMethods.call
+ TaxCategories.call
ProductTypes.call
CustomerGroups.call
ApiKeys.call
diff --git a/spree/core/app/services/spree/seeds/tax_categories.rb b/spree/core/app/services/spree/seeds/tax_categories.rb
index ac5ee719eb..fe45a9277a 100644
--- a/spree/core/app/services/spree/seeds/tax_categories.rb
+++ b/spree/core/app/services/spree/seeds/tax_categories.rb
@@ -4,8 +4,12 @@ module Spree
prepend Spree::ServiceModule::Base
def call
- Spree::TaxCategory.find_or_create_by(name: 'Default', is_default: true)
- Spree::TaxCategory.find_or_create_by(name: 'Non-taxable')
+ Spree::Store.find_each do |store|
+ store.tax_categories.find_or_create_by!(name: 'Default') do |tax_category|
+ tax_category.is_default = true
+ end
+ store.tax_categories.find_or_create_by!(name: 'Non-taxable')
+ end
end
end
end
diff --git a/spree/core/db/migrate/20260806000001_add_store_id_to_spree_tax_rates_and_categories.rb b/spree/core/db/migrate/20260806000001_add_store_id_to_spree_tax_rates_and_categories.rb
new file mode 100644
index 0000000000..b1e8d552fc
--- /dev/null
+++ b/spree/core/db/migrate/20260806000001_add_store_id_to_spree_tax_rates_and_categories.rb
@@ -0,0 +1,16 @@
+class AddStoreIdToSpreeTaxRatesAndCategories < ActiveRecord::Migration[8.1]
+ def change
+ # Tax rates and tax categories are the internal tax provider's entire
+ # configuration, so they belong to a store like every other commerce
+ # setting. Kept null: true here; existing rows are backfilled by
+ # `spree:backfill_tax_store_ids` and presence is enforced on the model.
+ # The NOT NULL constraints land in 6.1.
+ add_reference :spree_tax_rates, :store, null: true
+ add_reference :spree_tax_categories, :store, null: true
+
+ # Category names are unique per store. Legacy rows carry a NULL store_id
+ # until the backfill runs, and NULLs never collide in a unique index, so
+ # this constraint starts enforcing only once rows are bound to a store.
+ add_index :spree_tax_categories, [:store_id, :name], unique: true, where: 'deleted_at IS NULL'
+ 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 7d0c5c9592..0dd7a162de 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,19 @@ steps:
their own records afterwards. The NOT NULL constraints land in 6.1.
Idempotent.
+ - id: backfill_tax_store_ids
+ name: "Bind tax rates and tax categories to the default store"
+ task: "spree:backfill_tax_store_ids"
+ notes: |
+ Tax rates and tax categories carry a store_id in Spree 6 — they are the
+ internal tax provider's configuration and belong to a store like every
+ other commerce setting. Unassigned rows, soft-deleted ones included, go
+ to the default store; additional stores manage their own records
+ afterwards. Category names become unique per store rather than globally,
+ so installs that relied on global names may need to reconcile duplicates
+ once several stores hold tax configuration. The NOT NULL constraints land
+ in 6.1. Idempotent.
+
- id: backfill_order_markets
name: "Backfill order markets"
task: "spree:backfill_order_markets"
diff --git a/spree/core/lib/tasks/store_binding_migration.rake b/spree/core/lib/tasks/store_binding_migration.rake
index df26ad7359..7b1fe7a030 100644
--- a/spree/core/lib/tasks/store_binding_migration.rake
+++ b/spree/core/lib/tasks/store_binding_migration.rake
@@ -14,4 +14,20 @@ namespace :spree do
puts " Assigned #{methods} delivery methods and #{locations} stock locations to store #{store.name} (#{store.id})."
end
+
+ desc <<~DESC
+ Assigns the default store to tax rates and tax categories that predate
+ their store binding (Spree 6.0). Idempotent — rows that already carry a
+ store_id are skipped. Soft-deleted rows are bound too, so restoring one
+ later still yields a valid record.
+ DESC
+ task backfill_tax_store_ids: :environment do
+ store = Spree::Store.default
+ abort 'No default store found — create a store first.' if store.nil?
+
+ categories = Spree::TaxCategory.with_deleted.where(store_id: nil).update_all(store_id: store.id)
+ rates = Spree::TaxRate.with_deleted.where(store_id: nil).update_all(store_id: store.id)
+
+ puts " Assigned #{categories} tax categories and #{rates} tax rates to store #{store.name} (#{store.id})."
+ 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 745f71d914..e960cb036c 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
@@ -107,6 +107,34 @@ describe '6.0 data migration tasks' do
end
end
+ describe 'spree:backfill_tax_store_ids' do
+ it 'assigns the default store to unbound rows and skips bound ones' do
+ other_store = create(:store)
+ bound_category = create(:tax_category)
+ bound_category.update_columns(store_id: other_store.id)
+ legacy_category = create(:tax_category)
+ legacy_category.update_columns(store_id: nil)
+ legacy_rate = create(:tax_rate)
+ legacy_rate.update_columns(store_id: nil)
+
+ run_task('spree:backfill_tax_store_ids')
+
+ expect(legacy_category.reload.store_id).to eq(@default_store.id)
+ expect(legacy_rate.reload.store_id).to eq(@default_store.id)
+ expect(bound_category.reload.store_id).to eq(other_store.id)
+ end
+
+ it 'binds soft-deleted rows too' do
+ deleted_rate = create(:tax_rate)
+ deleted_rate.destroy
+ deleted_rate.update_columns(store_id: nil)
+
+ run_task('spree:backfill_tax_store_ids')
+
+ expect(Spree::TaxRate.with_deleted.find(deleted_rate.id).store_id).to eq(@default_store.id)
+ 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/tax_category_spec.rb b/spree/core/spec/models/spree/tax_category_spec.rb
index 0bd726abd5..bdcc07734c 100644
--- a/spree/core/spec/models/spree/tax_category_spec.rb
+++ b/spree/core/spec/models/spree/tax_category_spec.rb
@@ -32,6 +32,31 @@ describe Spree::TaxCategory, type: :model do
end
end
+ describe 'store binding' do
+ it 'falls back to the current store' do
+ expect(create(:tax_category).store).to eq(@default_store)
+ end
+
+ it 'allows the same name in another store' do
+ other_store = create(:store)
+ create(:tax_category, name: 'Standard')
+
+ expect(build(:tax_category, name: 'Standard', store: other_store)).to be_valid
+ end
+
+ it 'rejects a duplicate name within the same store, ignoring case' do
+ create(:tax_category, name: 'Standard')
+
+ expect(build(:tax_category, name: 'standard')).not_to be_valid
+ end
+
+ it 'ignores soft-deleted rows when checking the name' do
+ create(:tax_category, name: 'Standard').destroy
+
+ expect(build(:tax_category, name: 'Standard')).to be_valid
+ end
+ end
+
describe '#destroy' do
let!(:tax_category) { create(:tax_category) }
let!(:tax_rate) { create(:tax_rate, tax_category: tax_category) }
diff --git a/spree/core/spec/models/spree/tax_rate_spec.rb b/spree/core/spec/models/spree/tax_rate_spec.rb
index 3379ddee95..8407baeeac 100644
--- a/spree/core/spec/models/spree/tax_rate_spec.rb
+++ b/spree/core/spec/models/spree/tax_rate_spec.rb
@@ -418,4 +418,23 @@ describe Spree::TaxRate, type: :model do
end
end
end
+
+ describe 'store binding' do
+ it 'falls back to the current store' do
+ expect(create(:tax_rate).store).to eq(@default_store)
+ end
+
+ it 'keeps an explicitly assigned store' do
+ other_store = create(:store)
+ expect(create(:tax_rate, store: other_store).store).to eq(other_store)
+ end
+
+ it 'only returns rates of the given store' do
+ other_store = create(:store)
+ own_rate = create(:tax_rate)
+ create(:tax_rate, store: other_store)
+
+ expect(Spree::TaxRate.for_store(@default_store)).to eq([own_rate])
+ end
+ end
end
diff --git a/spree/core/spec/services/spree/seeds/tax_categories_spec.rb b/spree/core/spec/services/spree/seeds/tax_categories_spec.rb
index e0b9ce787a..2901b0314a 100644
--- a/spree/core/spec/services/spree/seeds/tax_categories_spec.rb
+++ b/spree/core/spec/services/spree/seeds/tax_categories_spec.rb
@@ -11,20 +11,30 @@ RSpec.describe Spree::Seeds::TaxCategories do
]
end
- it 'creates all TaxCategories' do
+ it 'creates all TaxCategories for the store' do
expect { subject }.to change(Spree::TaxCategory, :count).by(expected_categories.count)
expected_categories.each do |category_attrs|
- tax_category = Spree::TaxCategory.find_by(name: category_attrs[:name])
+ tax_category = @default_store.tax_categories.find_by(name: category_attrs[:name])
expect(tax_category).to be_present
expect(tax_category.is_default).to eq(category_attrs[:is_default])
end
end
+ it 'gives every store its own categories' do
+ other_store = create(:store)
+
+ subject
+
+ expect(other_store.tax_categories.pluck(:name)).to match_array(expected_categories.pluck(:name))
+ expect(other_store.tax_categories.find_by(is_default: true).name).to eq('Default')
+ expect(@default_store.tax_categories.find_by(is_default: true).name).to eq('Default')
+ end
+
context 'when TaxCategories already exist' do
before do
expected_categories.each do |category_attrs|
- Spree::TaxCategory.create!(name: category_attrs[:name], is_default: category_attrs[:is_default])
+ @default_store.tax_categories.create!(name: category_attrs[:name], is_default: category_attrs[:is_default])
end
end