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
Resolve a buyer's company only within the sale's own store
Łukasz Adamczyk48a0a7f1e909
diff --git a/spree/core/app/models/concerns/spree/purchase/company.rb b/spree/core/app/models/concerns/spree/purchase/company.rbindex 6fc864d8ad..2d368fce4e 100644--- a/spree/core/app/models/concerns/spree/purchase/company.rb+++ b/spree/core/app/models/concerns/spree/purchase/company.rb@@ -73,9 +73,18 @@ module Spree# safely needs the reset-on-reload machinery; the repeated query is the# cheaper problem.def sole_customer_location- return nil if customer.nil?+ return nil if customer.nil? || store_id.nil?++ # Scoped to this sale's store. Customers are global, so without this a+ # buyer who is a contact at a company in another store would resolve to+ # that business here — and its registration and exemption certificates+ # would then decide this sale's tax. The explicit-assignment path is+ # guarded by the validation above; this is the implicit one.+ locations = customer.company_locations.+ joins(:company).+ merge(Spree::Company.where(store_id: store_id)).+ to_a.uniq- locations = customer.company_locations.to_a.uniqlocations.one? ? locations.first : nilendenddiff --git a/spree/core/spec/models/concerns/spree/purchase/company_spec.rb b/spree/core/spec/models/concerns/spree/purchase/company_spec.rbindex b3115df079..2d17105386 100644--- a/spree/core/spec/models/concerns/spree/purchase/company_spec.rb+++ b/spree/core/spec/models/concerns/spree/purchase/company_spec.rb@@ -33,6 +33,28 @@ RSpec.shared_examples 'a company host' doexpect(record.resolved_company_location).to be_nilend++ # Customers are global across stores, so a buyer can be a contact at a+ # business this store does not trade as. Resolving to it would hand this+ # sale that company's registration and exemption certificates.+ it 'ignores a branch belonging to another store' do+ elsewhere = create(:company_location, company: create(:company, store: create(:store)))+ create(:company_contact, company_location: elsewhere, customer: record.customer)++ expect(record.reload.resolved_company_location).to be_nil+ expect(record.company).to be_nil+ end++ # The foreign branch must not count towards "acts for several" either —+ # otherwise it would suppress a resolution that is genuinely unambiguous+ # within this store.+ it 'still resolves the one branch in this store when another store has one too' do+ create(:company_contact, company_location: location, customer: record.customer)+ elsewhere = create(:company_location, company: create(:company, store: create(:store)))+ create(:company_contact, company_location: elsewhere, customer: record.customer)++ expect(record.reload.resolved_company_location).to eq(location)+ endendend