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
Reduce tax rate matching to one primitive
Łukasz Adamczyk72cebb1903bb
diff --git a/spree/core/app/models/spree/tax_rate.rb b/spree/core/app/models/spree/tax_rate.rbindex 9b39a1dda1..9791504e0a 100644--- a/spree/core/app/models/spree/tax_rate.rb+++ b/spree/core/app/models/spree/tax_rate.rb@@ -36,18 +36,21 @@ module Spreebefore_validation :resolve_state_codevalidate :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.+ # The primitive: rates that reach a jurisdiction, which is a country and+ # optionally one of its states. Rates naming neither are included, since a+ # rate with no country taxes everywhere. Takes the pair rather than an+ # address because tax applies before a customer has given one — the+ # provider falls back to the market's country (see+ # Spree::Purchase::Taxation#tax_country).scope :for_jurisdiction, lambda { |country, state = nil|where(country_id: [country&.id, nil].uniq).where(state_id: [state&.id, nil].uniq)}+ # The same question asked with an address in hand.+ scope :for_address, ->(address) { for_jurisdiction(address&.country, address&.state) }+ # Country-wide: unlike for_jurisdiction(country, nil) this leaves the state+ # unconstrained, so it sums a country's state-level rates too. Used where the+ # state is irrelevant — backing VAT out of a gross price.+ scope :for_country, ->(country) { where(country_id: [country&.id, nil].uniq) }scope :for_tax_category,->(category) { where(tax_category_id: category.try(:id)) }scope :included_in_price, -> { where(included_in_price: true) }@@ -79,16 +82,6 @@ module Spree@state_code_input = valueend- # 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+diff --git a/spree/core/spec/models/spree/tax_rate_spec.rb b/spree/core/spec/models/spree/tax_rate_spec.rbindex 42f9d77110..cf0c612a8d 100644--- a/spree/core/spec/models/spree/tax_rate_spec.rb+++ b/spree/core/spec/models/spree/tax_rate_spec.rb@@ -1,35 +1,47 @@require 'spec_helper'describe Spree::TaxRate, type: :model do- describe '.match' do+ describe 'jurisdiction matching' dolet(: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 'matches nothing without an address' do- expect(described_class.match(nil)).to be_empty+ it 'falls back to rates that tax everywhere when the jurisdiction is unknown' do+ worldwide = create(:tax_rate, :worldwide, tax_category: tax_category)+ create(:tax_rate, country: germany, tax_category: tax_category)++ # A rate naming no country taxes everywhere, so it still applies when we+ # cannot say where the sale is — a country-specific one does not.+ expect(described_class.for_jurisdiction(nil)).to eq([worldwide])+ end++ it 'reads a jurisdiction off an address, or takes the pair directly' do+ rate = create(:tax_rate, country: germany, tax_category: tax_category)++ expect(described_class.for_address(german_address)).to eq([rate])+ expect(described_class.for_jurisdiction(germany)).to eq([rate])endit 'matches a rate for the address country' dorate = create(:tax_rate, country: germany, tax_category: tax_category)create(:tax_rate, country: france, tax_category: tax_category)- expect(described_class.match(german_address)).to eq([rate])+ expect(described_class.for_address(german_address)).to eq([rate])endit 'matches every rate configured for that country' dostandard = 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))- expect(described_class.match(german_address)).to match_array([standard, reduced])+ expect(described_class.for_address(german_address)).to match_array([standard, reduced])endit 'matches a rate that taxes every country' doworldwide = create(:tax_rate, :worldwide, tax_category: tax_category)- expect(described_class.match(german_address)).to eq([worldwide])+ expect(described_class.for_address(german_address)).to eq([worldwide])endcontext 'with state-level rates' do@@ -39,20 +51,20 @@ describe Spree::TaxRate, type: :model docountry_rate = create(:tax_rate, country: germany, tax_category: tax_category)state_rate = create(:tax_rate, country: germany, state: berlin, tax_category: create(:tax_category))- expect(described_class.match(berlin_address)).to match_array([country_rate, state_rate])+ expect(described_class.for_address(berlin_address)).to match_array([country_rate, state_rate])endit 'does not match another state rate' dohamburg = create(:state, country: germany, abbr: 'HH', name: 'Hamburg')create(:tax_rate, country: germany, state: hamburg, tax_category: tax_category)- expect(described_class.match(berlin_address)).to be_empty+ expect(described_class.for_address(berlin_address)).to be_emptyendit 'does not match a state rate for an address with no state' docreate(:tax_rate, country: germany, state: berlin, tax_category: tax_category)- expect(described_class.match(german_address)).to be_empty+ expect(described_class.for_address(german_address)).to be_emptyendend