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.

Companies and tax exemptions, Phase 3: prefer the business's tax registration over the buyer's

Łukasz Adamczykd40b7fd53185
diff --git a/spree/core/app/models/concerns/spree/purchase/taxation.rb b/spree/core/app/models/concerns/spree/purchase/taxation.rb
index dd1afcfc78..e656e2151f 100644
--- a/spree/core/app/models/concerns/spree/purchase/taxation.rb
+++ b/spree/core/app/models/concerns/spree/purchase/taxation.rb
@@ -52,15 +52,17 @@ module Spree
# again, so its tax can still be explained after the customer edits or
# withdraws the registration.
#
- # Among a customer's registrations a verified one wins, then the most
- # recent — a plain fallback chain, which is why this is an overridable
- # method rather than a Dependencies seam.
+ # A business customer's own registration outranks the buyer's, because the
+ # invoice is addressed to the entity rather than the person who placed the
+ # order. Among a customer's registrations a verified one wins, then the
+ # most recent — a plain fallback chain, which is why this is an
+ # overridable method rather than a Dependencies seam.
#
# @return [Spree::TaxIdentifier, nil]
def resolved_tax_identifier
return tax_identifier if is_a?(Spree::Order) && completed?
- tax_identifier || customer_tax_identifier
+ tax_identifier || company_tax_identifier || customer_tax_identifier
end
def tax_total
@@ -79,8 +81,16 @@ module Spree
private
+ def company_tax_identifier
+ best_of(company&.tax_identifiers)
+ end
+
def customer_tax_identifier
- identifiers = customer&.tax_identifiers.to_a
+ best_of(customer&.tax_identifiers)
+ end
+
+ def best_of(identifiers)
+ identifiers = identifiers.to_a
identifiers.find(&:verified?) || identifiers.max_by(&:created_at)
end
end
diff --git a/spree/core/app/models/spree/company.rb b/spree/core/app/models/spree/company.rb
index 809bc1b3b2..de63b8f754 100644
--- a/spree/core/app/models/spree/company.rb
+++ b/spree/core/app/models/spree/company.rb
@@ -24,6 +24,8 @@ module Spree
has_many :company_locations, class_name: 'Spree::CompanyLocation', dependent: :destroy,
inverse_of: :company
has_many :company_contacts, through: :company_locations
+ has_many :tax_identifiers, class_name: 'Spree::TaxIdentifier', dependent: :destroy,
+ inverse_of: :company
validates :name, presence: true
# Paired with the partial unique index, per the convention — without it a
diff --git a/spree/core/app/models/spree/tax_identifier.rb b/spree/core/app/models/spree/tax_identifier.rb
index a312b5d3fc..bd4b61b129 100644
--- a/spree/core/app/models/spree/tax_identifier.rb
+++ b/spree/core/app/models/spree/tax_identifier.rb
@@ -40,6 +40,7 @@ module Spree
# Owner — exactly one
belongs_to :customer, class_name: Spree.customer_class.to_s, optional: true, inverse_of: :tax_identifiers
+ belongs_to :company, class_name: 'Spree::Company', optional: true, inverse_of: :tax_identifiers
belongs_to :cart, class_name: 'Spree::Cart', optional: true, inverse_of: :tax_identifier
belongs_to :order, class_name: 'Spree::Order', optional: true, inverse_of: :tax_identifier
@@ -51,6 +52,7 @@ module Spree
# makes resolution deterministic. Cart and order rows are singular by their
# own associations.
validates :kind, uniqueness: { scope: :customer_id }, if: -> { customer_id.present? }
+ validates :kind, uniqueness: { scope: :company_id }, if: -> { company_id.present? }
# Data hygiene, not a format claim — no tax regime issues numbers this long.
validates :value, length: { maximum: 64 }
validates :validation_status, inclusion: { in: VALIDATION_STATUSES }, allow_nil: true
@@ -67,9 +69,9 @@ module Spree
persisted? && order_id.present?
end
- # @return [Spree::Customer, Spree::Cart, Spree::Order, nil]
+ # @return [Spree::Customer, Spree::Company, Spree::Cart, Spree::Order, nil]
def owner
- customer || cart || order
+ customer || company || cart || order
end
def verified?
@@ -128,7 +130,7 @@ module Spree
end
def exactly_one_owner
- return if [customer, cart, order].compact.one?
+ return if [customer, company, cart, order].compact.one?
errors.add(:base, Spree.t('errors.messages.exactly_one_tax_identifier_owner'))
end
diff --git a/spree/core/app/workflows/spree/carts/complete.rb b/spree/core/app/workflows/spree/carts/complete.rb
index f9082f23cd..c13722faee 100644
--- a/spree/core/app/workflows/spree/carts/complete.rb
+++ b/spree/core/app/workflows/spree/carts/complete.rb
@@ -222,10 +222,18 @@ module Spree
resolved = cart.resolved_tax_identifier
return if resolved.nil?
- attributes = resolved.attributes.except('id', 'customer_id', 'cart_id', 'created_at', 'updated_at')
- order.create_tax_identifier!(
- attributes.merge('source' => resolved.cart_id.present? ? 'override' : 'customer')
- )
+ attributes = resolved.attributes.except('id', 'customer_id', 'company_id', 'cart_id',
+ 'created_at', 'updated_at')
+ order.create_tax_identifier!(attributes.merge('source' => source_of(resolved)))
+ end
+
+ # Which link of the chain produced the snapshot, so a placed order's tax
+ # treatment names its own reason.
+ def source_of(resolved)
+ return 'override' if resolved.cart_id.present?
+ return 'company' if resolved.company_id.present?
+
+ 'customer'
end
def copy_line_items!(cart, order)
diff --git a/spree/core/db/migrate/20260807000002_add_company_to_spree_tax_identifiers.rb b/spree/core/db/migrate/20260807000002_add_company_to_spree_tax_identifiers.rb
new file mode 100644
index 0000000000..5fdbdfb1b7
--- /dev/null
+++ b/spree/core/db/migrate/20260807000002_add_company_to_spree_tax_identifiers.rb
@@ -0,0 +1,9 @@
+class AddCompanyToSpreeTaxIdentifiers < ActiveRecord::Migration[8.1]
+ def change
+ # A fourth owner, still exactly one per row: the registration of the entity
+ # the invoice is addressed to, which outranks the buyer's own.
+ add_reference :spree_tax_identifiers, :company, null: true
+
+ add_index :spree_tax_identifiers, [:company_id, :kind], unique: true
+ end
+end
diff --git a/spree/core/spec/models/concerns/spree/purchase/taxation_spec.rb b/spree/core/spec/models/concerns/spree/purchase/taxation_spec.rb
index 75f13d5daa..a21172c9c3 100644
--- a/spree/core/spec/models/concerns/spree/purchase/taxation_spec.rb
+++ b/spree/core/spec/models/concerns/spree/purchase/taxation_spec.rb
@@ -46,6 +46,35 @@ RSpec.shared_examples 'a taxation host' do
expect(record.resolved_tax_identifier).to eq(verified)
end
+ context 'when the sale is for a business' do
+ let(:company) { create(:company, store: store) }
+ let(:location) { create(:company_location, company: company) }
+
+ before { record.update!(company_location: location) }
+
+ # The invoice is addressed to the entity, so its registration wins.
+ it 'prefers the company registration over the buyer own' do
+ create(:tax_identifier, customer: customer, kind: 'eu_vat', value: 'DE111111111')
+ company_identifier = create(:tax_identifier, customer: nil, company: company,
+ kind: 'eu_vat', value: 'DE222222222')
+
+ expect(record.resolved_tax_identifier).to eq(company_identifier)
+ end
+
+ it 'still falls back to the buyer when the company has none' do
+ identifier = create(:tax_identifier, customer: customer)
+
+ expect(record.resolved_tax_identifier).to eq(identifier)
+ end
+
+ it 'prefers a verified company registration' do
+ verified = create(:tax_identifier, :verified, customer: nil, company: company, kind: 'eu_vat')
+ create(:tax_identifier, customer: nil, company: company, kind: 'gb_vat')
+
+ expect(record.resolved_tax_identifier).to eq(verified)
+ end
+ end
+
it 'prefers the record own override over the customer registration' do
create(:tax_identifier, customer: customer)
owner_key = record.is_a?(Spree::Cart) ? :cart : :order
diff --git a/spree/core/spec/models/spree/tax_identifier_spec.rb b/spree/core/spec/models/spree/tax_identifier_spec.rb
index 21f7d5bbf6..b9383faffd 100644
--- a/spree/core/spec/models/spree/tax_identifier_spec.rb
+++ b/spree/core/spec/models/spree/tax_identifier_spec.rb
@@ -143,4 +143,30 @@ describe Spree::TaxIdentifier, type: :model do
Spree.tax_identifier_validators.delete('eu_vat')
end
end
+
+ describe 'a company owner' do
+ let(:company) { create(:company, store: @default_store) }
+
+ it 'is a valid sole owner' do
+ expect(build(:tax_identifier, customer: nil, company: company)).to be_valid
+ end
+
+ it 'is rejected alongside another owner' do
+ identifier = build(:tax_identifier, company: company, customer: create(:customer))
+
+ expect(identifier).not_to be_valid
+ end
+
+ it 'holds one registration per kind' do
+ create(:tax_identifier, customer: nil, company: company, kind: 'eu_vat')
+
+ expect(build(:tax_identifier, customer: nil, company: company, kind: 'eu_vat')).not_to be_valid
+ end
+
+ it 'reports the company as its owner' do
+ identifier = create(:tax_identifier, customer: nil, company: company)
+
+ expect(identifier.owner).to eq(company)
+ end
+ end
end
diff --git a/spree/core/spec/workflows/spree/carts/complete_spec.rb b/spree/core/spec/workflows/spree/carts/complete_spec.rb
index 17e52f050a..1fc81a5497 100644
--- a/spree/core/spec/workflows/spree/carts/complete_spec.rb
+++ b/spree/core/spec/workflows/spree/carts/complete_spec.rb
@@ -154,6 +154,21 @@ module Spree
expect(order.tax_identifier.source).to eq('override')
end
+ it 'stamps a company registration as such' do
+ company = create(:company, store: store)
+ location = create(:company_location, company: company)
+ ready_cart.update!(company_location: location)
+ create(:tax_identifier, customer: nil, company: company, kind: 'eu_vat', value: 'DE777777777')
+
+ order = described_class.call(cart: ready_cart).value
+
+ expect(order.tax_identifier.value).to eq('DE777777777')
+ expect(order.tax_identifier.source).to eq('company')
+ # The copy must not carry the company along, or the order row would
+ # have two owners.
+ expect(order.tax_identifier.company_id).to be_nil
+ end
+
it 'copies nothing for a consumer sale' do
order = described_class.call(cart: ready_cart).value