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
Phase 1: close the gaps the review found in cross-border pricing
Łukasz Adamczykc17db2df1acd
diff --git a/spree/core/app/models/concerns/spree/vat_price_calculation.rb b/spree/core/app/models/concerns/spree/vat_price_calculation.rbindex 589b286d9d..c72947f39d 100644--- a/spree/core/app/models/concerns/spree/vat_price_calculation.rb+++ b/spree/core/app/models/concerns/spree/vat_price_calculation.rb@@ -2,9 +2,14 @@ module Spree# Restates a VAT-inclusive price for a customer in another country: the# home-country VAT comes out, the destination's goes on.module VatPriceCalculation+ # Cheapest test first: a domestic sale needs no restatement whatever the+ # provider is, so the provider lookup (a constantize and an instantiation) is+ # skipped for every home-country price — and a market pointing at a provider+ # class that is no longer loadable keeps failing only where tax is computed,+ # not on every price read.def gross_amount(amount, price_options)- return amount if amount.nil? || !restatement_available?(price_options)- return amount unless outside_default_vat_zone?(price_options)+ return amount if amount.nil? || !outside_default_vat_zone?(price_options)+ return amount unless restatement_available?(price_options)round_to_two_places(add_foreign_vat_for(amount, price_options))enddiff --git a/spree/core/app/models/spree/line_item.rb b/spree/core/app/models/spree/line_item.rbindex 5f16752aaf..7dad1e5c10 100644--- a/spree/core/app/models/spree/line_item.rb+++ b/spree/core/app/models/spree/line_item.rb@@ -134,7 +134,15 @@ module Spreecontext = Spree::Pricing::Context.from_order(variant, owner, quantity: quantity)currency_price = variant.price_for(context)- self.price = currency_price.price_including_vat_for(address: owner&.tax_address, market: owner&.market) if currency_price.present?+ # country:, not just address: — an address-less cart still has a taxing+ # country (the market's own), and the provider already estimates against it.+ # Without it a cross-border cart is taxed for the destination at a price+ # quoted for home, and the merchant silently absorbs the rate difference.+ if currency_price.present?+ self.price = currency_price.price_including_vat_for(+ address: owner&.tax_address, country: owner&.tax_country, market: owner&.market+ )+ endself.price_list_id = currency_price.price_list_id if currency_price.present?end@@ -320,7 +328,9 @@ module Spreereturn unless currency_price.present?- new_price = currency_price.price_including_vat_for(address: owner&.tax_address, market: owner&.market)+ new_price = currency_price.price_including_vat_for(+ address: owner&.tax_address, country: owner&.tax_country, market: owner&.market+ )return unless new_price.present?diff --git a/spree/core/app/models/spree/price.rb b/spree/core/app/models/spree/price.rbindex a926ea2379..f888517dc1 100644--- a/spree/core/app/models/spree/price.rb+++ b/spree/core/app/models/spree/price.rb@@ -128,7 +128,13 @@ module Spree## @return [Boolean]def final_for_destination?- price_list&.price_rules&.any?(&:geographic?) || false+ rules = price_list&.price_rules.to_a+ return false unless rules.any?(&:geographic?)++ # Under 'any' the list can win on a non-geographic rule alone, so a+ # geographic rule being present does not mean this buyer's geography is why+ # the price applied. Under 'all' every rule matched, so it does.+ price_list.match_policy == 'all' || rules.all?(&:geographic?)enddef price_including_vat_for(price_options)diff --git a/spree/core/app/models/spree/price_rules/market_rule.rb b/spree/core/app/models/spree/price_rules/market_rule.rbindex 792accbbc6..1655245aaf 100644--- a/spree/core/app/models/spree/price_rules/market_rule.rb+++ b/spree/core/app/models/spree/price_rules/market_rule.rb@@ -26,8 +26,10 @@ module Spreepreferred_market_ids.map(&:to_s).include?(context.market.id.to_s)end+ # An empty market list matches every buyer (see #applicable?), so it names+ # no geography — and that is the state a freshly created rule row is in.def geographic?- true+ preferred_market_ids.present?enddef self.descriptiondiff --git a/spree/core/app/models/spree/price_rules/zone_rule.rb b/spree/core/app/models/spree/price_rules/zone_rule.rbindex f3a8b59b97..0e393ee6b2 100644--- a/spree/core/app/models/spree/price_rules/zone_rule.rb+++ b/spree/core/app/models/spree/price_rules/zone_rule.rb@@ -26,8 +26,10 @@ module Spreepreferred_country_ids.map(&:to_s).include?(context.country.id.to_s)end+ # An empty country list matches every buyer (see #applicable?), so it names+ # no geography.def geographic?- true+ preferred_country_ids.present?enddef self.descriptiondiff --git a/spree/core/spec/models/concerns/spree/vat_price_calculation_spec.rb b/spree/core/spec/models/concerns/spree/vat_price_calculation_spec.rbindex b824307d27..af39349de2 100644--- a/spree/core/spec/models/concerns/spree/vat_price_calculation_spec.rb+++ b/spree/core/spec/models/concerns/spree/vat_price_calculation_spec.rb@@ -72,6 +72,31 @@ describe Spree::VatPriceCalculation, type: :model doendend+ # The provider estimates against tax_country, which falls back to the market's+ # own country, so a cart with no address is still taxed for its destination. The+ # price has to follow, or the merchant absorbs the rate difference silently.+ describe 'a cart with no shipping address yet' do+ let(:french_market) do+ create(:market, store: store, name: "France #{Time.current.to_f}", currency: 'USD',+ default_locale: 'en', countries: [france])+ end++ before do+ create(:tax_rate, name: 'FR 20% incl', amount: 0.20, tax_category: category,+ country: france, included_in_price: true, store: store)+ end++ it 'restates from the market country, keeping the net intact' do+ cart = create(:cart, store: store, market: french_market, currency: 'USD')+ Spree::Carts::AddItem.call(cart: cart, variant: variant, quantity: 1)++ line_item = cart.line_items.reload.first+ expect(cart.tax_address).to be_nil+ expect(line_item.price).to eq(100.84)+ expect(line_item.pre_tax_amount).to be_within(0.01).of(84.03)+ end+ end+describe 'a price the merchant set for the destination' dolet(:french_market) docreate(:market, store: store, name: "France #{Time.current.to_f}", currency: 'USD',@@ -109,4 +134,60 @@ describe Spree::VatPriceCalculation, type: :model doexpect(volume_price.price_including_vat_for(country: france, market: french_market)).to eq(99.83)endend+ describe 'geographic rules that do not actually narrow' do+ let(:french_market) do+ create(:market, store: store, name: "France #{Time.current.to_f}", currency: 'USD',+ default_locale: 'en', countries: [france])+ end++ before do+ create(:tax_rate, name: 'FR 20% incl', amount: 0.20, tax_category: category,+ country: france, included_in_price: true, store: store)+ end++ # A market rule with nothing selected matches everyone, which is the state the+ # admin creates it in — it must not switch restatement off for the whole list.+ it 'still restates when the market rule names no markets' do+ list = create(:price_list, store: store)+ list.price_rules.create!(type: 'Spree::PriceRules::MarketRule', preferences: { market_ids: [] })+ price = create(:price, variant: variant, currency: 'USD', amount: 95, price_list: list)++ expect(price.final_for_destination?).to be(false)+ expect(price.price_including_vat_for(country: france, market: french_market)).to eq(95.80)+ end++ # Under 'any' the list can win on the volume rule alone, so the buyer's+ # geography is not necessarily why this price applied.+ it "still restates a mixed list under the 'any' match policy" do+ list = create(:price_list, store: store, match_policy: 'any')+ list.price_rules.create!(type: 'Spree::PriceRules::MarketRule',+ preferences: { market_ids: [french_market.id] })+ list.price_rules.create!(type: 'Spree::PriceRules::VolumeRule', preferences: { min_quantity: 1 })+ price = create(:price, variant: variant, currency: 'USD', amount: 90, price_list: list)++ expect(price.final_for_destination?).to be(false)+ expect(price.price_including_vat_for(country: france, market: french_market)).to eq(90.76)+ end++ it "treats a mixed list under 'all' as final, since every rule matched" do+ list = create(:price_list, store: store, match_policy: 'all')+ list.price_rules.create!(type: 'Spree::PriceRules::MarketRule',+ preferences: { market_ids: [french_market.id] })+ list.price_rules.create!(type: 'Spree::PriceRules::VolumeRule', preferences: { min_quantity: 1 })+ price = create(:price, variant: variant, currency: 'USD', amount: 90, price_list: list)++ expect(price.final_for_destination?).to be(true)+ expect(price.price_including_vat_for(country: france, market: french_market)).to eq(90.00)+ end++ it 'applies the same rule to the compare-at price' do+ list = create(:price_list, store: store)+ list.price_rules.create!(type: 'Spree::PriceRules::MarketRule',+ preferences: { market_ids: [french_market.id] })+ price = create(:price, variant: variant, currency: 'USD', amount: 90,+ compare_at_amount: 120, price_list: list)++ expect(price.compare_at_price_including_vat_for(country: france, market: french_market)).to eq(120.00)+ end+ endenddiff --git a/spree/core/spec/models/spree/price_rules/market_rule_spec.rb b/spree/core/spec/models/spree/price_rules/market_rule_spec.rbindex 78154c0086..a01017586a 100644--- a/spree/core/spec/models/spree/price_rules/market_rule_spec.rb+++ b/spree/core/spec/models/spree/price_rules/market_rule_spec.rb@@ -103,10 +103,18 @@ describe Spree::PriceRules::MarketRule, type: :model doendenddescribe '#geographic?' do- it 'is true' do- # A list narrowed by geography states its prices for that geography, so- # they are charged as entered rather than restated for the buyer's VAT.- expect(described_class.new.geographic?).to be(true)+ # A list narrowed by geography states its prices for that geography, so they+ # are charged as entered rather than restated for the buyer's VAT.+ it 'is true once markets are named' do+ rule = described_class.new(price_list: price_list)+ rule.preferred_market_ids = [market.id]++ expect(rule.geographic?).to be(true)+ end++ # An empty list matches every buyer, so it narrows nothing.+ it 'is false while no market is named' do+ expect(described_class.new(price_list: price_list).geographic?).to be(false)endendenddiff --git a/spree/core/spec/models/spree/price_rules/zone_rule_spec.rb b/spree/core/spec/models/spree/price_rules/zone_rule_spec.rbindex b316f7c386..1a96e95c62 100644--- a/spree/core/spec/models/spree/price_rules/zone_rule_spec.rb+++ b/spree/core/spec/models/spree/price_rules/zone_rule_spec.rb@@ -56,10 +56,15 @@ describe Spree::PriceRules::ZoneRule, type: :model doendenddescribe '#geographic?' do- it 'is true' do- # A list narrowed by geography states its prices for that geography, so- # they are charged as entered rather than restated for the buyer's VAT.- expect(described_class.new.geographic?).to be(true)+ it 'is true once countries are named' do+ rule = described_class.new(price_list: price_list)+ rule.preferred_country_ids = [country.id]++ expect(rule.geographic?).to be(true)+ end++ it 'is false while no country is named' do+ expect(described_class.new(price_list: price_list).geographic?).to be(false)endendend