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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
End a tax identifier's verdict when its number changes
Łukasz Adamczyk5dcc67b65736
diff --git a/docs/plans/6.0-tax-provider.md b/docs/plans/6.0-tax-provider.mdindex a82452a9ce..8eb1a85fca 100644--- a/docs/plans/6.0-tax-provider.md+++ b/docs/plans/6.0-tax-provider.md@@ -567,7 +567,9 @@ scope :for_address, ->(address) {State-level rates override country-level (more specific wins). **Tax stops using Zone** — a data migration converts Zone records used by TaxRate into direct country/state FKs, and no code reads `zone_id` on TaxRate any more. The request-context readers went too (`Spree::Current.zone`/`default_tax_zone`, `Market#tax_zone`, `Purchase::Taxation#tax_zone`), replaced by `Spree::Current.tax_country`; `spree_tax_rates.zone_id` stays as the conversion source until Phase 9. Dropping the `Spree::Zone` model itself belongs to `6.0-delivery-zones.md`, which owns the full consumer inventory and does it inside 6.0.-**`spree_tax_rates.zone_id` is dropped in 6.1, not 6.0.** The column is what `spree:migrate_tax_zones` converts *from*, and the manifest runs after `db:migrate` — so a migration dropping it in the same release would destroy the conversion's own input, and leave no rollback source if a merchant needs to re-run it. 6.0 therefore severs every reader (the `belongs_to :zone`, the zone scopes, and `Zone#has_many :tax_rates`, whose `dependent: :destroy` would otherwise delete live rates when a shipping zone is destroyed) and leaves the column in place, unread. This matches how `spree_adjustments` is handled by `migrate_adjustments_to_typed_rows`. **Phase 9** drops the column alongside the other converted-away schema.+**`spree_tax_rates.zone_id` is dropped in 6.1, not 6.0.** The column is what `spree:migrate_tax_zones` converts *from*, and the manifest runs after `db:migrate` — so a migration dropping it in the same release would destroy the conversion's own input, and leave no rollback source if a merchant needs to re-run it.++**The gap this leaves, and why it stays a gap.** Between `db:migrate` and the conversion task, an unconverted rate has no `country_iso` — and a blank country means *everywhere*, so a rate configured for one country matches every destination and over-collects until the task runs. The fix is not to move the conversion into the migration: data transformations belong in rake tasks (see `CLAUDE.md`), the manifest deliberately runs after `db:migrate`, and `migrate_adjustments_to_typed_rows` and the delivery migration already carry the same window. Two things keep it small — `bin/rake spree:upgrade` runs the manifest immediately after migrating, and the task is idempotent, so it can be re-run without thought. **An upgrade that serves traffic between the two steps should expect over-collection in that window**; failing the other way (treating unconverted rates as taxing nothing) trades a customer-facing error for an under-collection the merchant is liable for, which is the worse of the two. 6.0 therefore severs every reader (the `belongs_to :zone`, the zone scopes, and `Zone#has_many :tax_rates`, whose `dependent: :destroy` would otherwise delete live rates when a shipping zone is destroyed) and leaves the column in place, unread. This matches how `spree_adjustments` is handled by `migrate_adjustments_to_typed_rows`. **Phase 9** drops the column alongside the other converted-away schema.## Design Detailsdiff --git a/spree/core/app/models/spree/tax_identifier.rb b/spree/core/app/models/spree/tax_identifier.rbindex f8c7dc5de2..636aa6e09e 100644--- a/spree/core/app/models/spree/tax_identifier.rb+++ b/spree/core/app/models/spree/tax_identifier.rb@@ -51,9 +51,10 @@ module Spree# produces it itself.normalizes :value, with: ->(value) { value&.to_s&.gsub(/\s+/, '')&.upcase }- # The verdict resets as part of the write, not afterwards: the response to- # the request that changed the number has to say "we're checking this"- # rather than report a stale verdict or a blank one.+ # A verdict belongs to the number it answered about, so changing the number+ # ends it. Reset as part of the write, not afterwards: the response to the+ # request that made the change has to report the new state, not a stale+ # verdict or a blank one.before_save :reset_validation_verdict, if: :number_changing?after_commit :publish_number_changed, on: %i[create update]@@ -113,12 +114,22 @@ module Spreeenddef number_changing?- validatable? && (will_save_change_to_value? || will_save_change_to_kind?)+ order_id.nil? && (will_save_change_to_value? || will_save_change_to_kind?)end+ # `pending` when something here can answer for the new number, and nothing at+ # all when nothing can — a kind with no validator gets no promise, and the old+ # answer cannot stay: Purchase::Taxation#best_of prefers a verified row, so a+ # stale `verified` would be actively chosen to decide tax.+ #+ # A new row has no earlier verdict to invalidate, so a number imported with one+ # already recorded keeps it.def reset_validation_verdict- self.validation_status = 'pending'+ return if new_record? && !validatable?++ self.validation_status = validatable? ? 'pending' : nilself.validated_at = nil+ self.validation_evidence = nil unless validatable?end# Announces the fact; Spree::TaxIdentifierValidationSubscriber decides whatdiff --git a/spree/core/spec/models/spree/tax_identifier_spec.rb b/spree/core/spec/models/spree/tax_identifier_spec.rbindex 3cf8921f27..1844a00c82 100644--- a/spree/core/spec/models/spree/tax_identifier_spec.rb+++ b/spree/core/spec/models/spree/tax_identifier_spec.rb@@ -124,6 +124,20 @@ describe Spree::TaxIdentifier, type: :model doexpect(identifier.validated_at).to be_nilend+ # Purchase::Taxation#best_of prefers a verified row, so a verdict left behind+ # on a kind nothing can check would be actively chosen to decide tax.+ it 'clears the verdict when the kind becomes one nothing can check' do+ identifier = create(:tax_identifier, customer: customer, kind: 'eu_vat')+ identifier.update_columns(validation_status: 'verified', validated_at: Time.current)++ identifier.update!(kind: 'au_abn', value: '51824753556')++ expect(identifier).not_to be_validatable+ expect(identifier.validation_status).to be_nil+ expect(identifier.validated_at).to be_nil+ expect(identifier).not_to be_verified+ end+it 'clears an earlier verdict when the number changes' doidentifier = create(:tax_identifier, customer: customer, kind: 'eu_vat')identifier.update_columns(validation_status: 'verified', validated_at: Time.current)