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
Drop an exemption that would exempt more than it claims
Łukasz Adamczykb5adee1b9a9a
diff --git a/spree/core/app/models/concerns/spree/purchase/taxation.rb b/spree/core/app/models/concerns/spree/purchase/taxation.rbindex e656e2151f..cb7919ff0e 100644--- a/spree/core/app/models/concerns/spree/purchase/taxation.rb+++ b/spree/core/app/models/concerns/spree/purchase/taxation.rb@@ -39,7 +39,7 @@ module Spree{tax_date: Time.current,tax_identifier: resolved_tax_identifier,- exemptions: Spree.tax_resolve_exemptions_service.new.call(order: self).value+ exemptions: usable_exemptions}end@@ -81,6 +81,31 @@ module Spreeprivate+ # The resolver is swappable, so what it returns is host code. An entry+ # whose per-line overrides cannot name their line is not a narrower claim+ # but a wider one — the provider would read it as the whole order being+ # exempt — so it is dropped rather than trusted. Charging tax is the safe+ # direction: under-exempting surfaces as a customer query, over-exempting+ # is tax the merchant owes and never collected.+ def usable_exemptions+ resolved = Array(Spree.tax_resolve_exemptions_service.new.call(order: self).value)++ resolved.select do |exemption|+ next true unless exemption.respond_to?(:valid?)+ next true if exemption.valid?++ Rails.error.report(+ Spree::Tax::UnusableExemptionError.new(+ "Discarded an unusable tax exemption: #{exemption.errors.full_messages.to_sentence}"+ ),+ handled: true,+ context: { order_id: id, reason_code: exemption.try(:reason_code) },+ source: 'spree.core'+ )+ false+ end+ end+def company_tax_identifierbest_of(company&.tax_identifiers)enddiff --git a/spree/core/app/models/spree/tax_exemption.rb b/spree/core/app/models/spree/tax_exemption.rbindex 543f456fa1..4f3666862e 100644--- a/spree/core/app/models/spree/tax_exemption.rb+++ b/spree/core/app/models/spree/tax_exemption.rb@@ -25,6 +25,7 @@ module Spreeattribute :item_overrides, default: -> { [] }validates :reason_code, presence: true+ validate :item_overrides_can_name_their_item# Whether this entry claims exemption for the given item.#@@ -63,6 +64,16 @@ module Spreeprivate+ # An override that cannot say which line it applies to does not narrow this+ # entry — it widens it. #override_for never matches it, so #covers_item?+ # falls through to true for every line and a per-line carve-out becomes an+ # order-wide exemption.+ def item_overrides_can_name_their_item+ return if Array(item_overrides).all? { |override| override.try(:item_id).present? }++ errors.add(:item_overrides, :invalid)+ end+def override_for(item)item_overrides.to_a.find { |override| override.item_id.to_s == item.prefixed_id.to_s }enddiff --git a/spree/core/app/services/spree/tax/resolve_exemptions.rb b/spree/core/app/services/spree/tax/resolve_exemptions.rbindex 391bf94cc2..6f0b2f89f5 100644--- a/spree/core/app/services/spree/tax/resolve_exemptions.rb+++ b/spree/core/app/services/spree/tax/resolve_exemptions.rb@@ -1,5 +1,10 @@module Spreemodule Tax+ # Raised for reporting when an exemption reaches core in a state that would+ # widen rather than narrow a claim. Reported, never raised at the customer:+ # the sale continues with tax charged.+ class UnusableExemptionError < StandardError; end+# Assembles the exemption evidence for a sale, handed to the tax provider# as +estimate+'s +exemptions:+ input.#diff --git a/spree/core/spec/models/concerns/spree/purchase/taxation_spec.rb b/spree/core/spec/models/concerns/spree/purchase/taxation_spec.rbindex a21172c9c3..2d21c4760a 100644--- a/spree/core/spec/models/concerns/spree/purchase/taxation_spec.rb+++ b/spree/core/spec/models/concerns/spree/purchase/taxation_spec.rb@@ -103,6 +103,38 @@ RSpec.shared_examples 'a taxation host' doexpect(inputs[:exemptions]).to eq([exemption])end+ # The resolver is swappable, so a host's handler can hand core an entry the+ # provider would read as "the whole order is exempt". Charging tax is the+ # safe direction, so it is dropped and reported rather than passed on.+ it 'drops an exemption whose overrides cannot name their line' do+ unusable = Spree::TaxExemption.new(+ reason_code: 'resale',+ item_overrides: [Spree::TaxExemption::ItemOverride.new(exempt: false)]+ )+ allow(Spree.tax_resolve_exemptions_service).to receive(:new).+ and_return(instance_double(Spree::Tax::ResolveExemptions,+ call: Spree::ServiceModule::Result.new(true, [unusable], nil)))+ allow(Rails.error).to receive(:report)++ expect(record.tax_estimate_inputs[:exemptions]).to be_empty+ expect(Rails.error).to have_received(:report).+ with(instance_of(Spree::Tax::UnusableExemptionError), hash_including(handled: true))+ end++ it 'keeps a usable one alongside it' do+ usable = Spree::TaxExemption.new(reason_code: 'resale')+ unusable = Spree::TaxExemption.new(+ reason_code: 'government',+ item_overrides: [Spree::TaxExemption::ItemOverride.new(exempt: false)]+ )+ allow(Spree.tax_resolve_exemptions_service).to receive(:new).+ and_return(instance_double(Spree::Tax::ResolveExemptions,+ call: Spree::ServiceModule::Result.new(true, [usable, unusable], nil)))+ allow(Rails.error).to receive(:report)++ expect(record.tax_estimate_inputs[:exemptions]).to eq([usable])+ end+it 'is accepted whole by the provider contract' doexpect { Spree::TaxProvider::Internal.new.estimate(record, [], **record.tax_estimate_inputs) }.not_to raise_errordiff --git a/spree/core/spec/models/spree/tax_exemption_spec.rb b/spree/core/spec/models/spree/tax_exemption_spec.rbindex 2710fe2e30..b5fef1757e 100644--- a/spree/core/spec/models/spree/tax_exemption_spec.rb+++ b/spree/core/spec/models/spree/tax_exemption_spec.rb@@ -67,6 +67,36 @@ describe Spree::TaxExemption, type: :model doendend+ # The failure this guards is silent and in the merchant's disfavour: an+ # override that cannot name its line is skipped by #covers_item?, which then+ # reports every line exempt — so a carve-out becomes a whole-order exemption.+ describe 'an override that cannot name its line' do+ it 'makes the entry invalid' do+ exemption = described_class.new(+ reason_code: 'resale',+ item_overrides: [Spree::TaxExemption::ItemOverride.new(exempt: false)]+ )++ expect(exemption).not_to be_valid+ expect(exemption.errors[:item_overrides]).to be_present+ end++ it 'leaves an entry with no overrides at all valid — that claims the order' do+ expect(described_class.new(reason_code: 'resale')).to be_valid+ end++ # Stated so the widening is visible: this is what the validation prevents+ # from ever reaching a provider.+ it 'would otherwise report every line as covered' do+ exemption = described_class.new(+ reason_code: 'resale',+ item_overrides: [Spree::TaxExemption::ItemOverride.new(exempt: false)]+ )++ expect(exemption.covers_item?(line_item)).to be(true)+ end+ end+describe Spree::TaxExemption::ItemOverride doit 'requires the item it applies to' doexpect(described_class.new(item_id: 'li_abc')).to be_valid