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.

Phase 3: write a tax row for every treatment, zero ones included

Łukasz Adamczyk6404e38e25de
diff --git a/spree/core/app/models/spree/tax_provider/internal.rb b/spree/core/app/models/spree/tax_provider/internal.rb
index 43baa35eed..827c0c1c2f 100644
--- a/spree/core/app/models/spree/tax_provider/internal.rb
+++ b/spree/core/app/models/spree/tax_provider/internal.rb
@@ -22,11 +22,13 @@ module Spree
relevant_rates = rates.select { |rate| rate.tax_category_id == tax_category_id_for(item) }
store_pre_tax_amount(item, relevant_rates)
+ # A matched rate always produces a row, zero-amount ones included:
+ # a zero rate is a treatment ("this is zero-rated"), which reporting
+ # and e-invoicing both need to see. No matched rate means no opinion,
+ # and writes nothing.
relevant_rates.each do |rate|
amount = compute_tax(rate, item, relevant_rates)
- next if amount.zero?
-
- write_tax_line(owner, item, rate, amount)
+ write_tax_line(owner, item, rate, amount, reason_for(rate))
end
end
end
@@ -84,17 +86,37 @@ module Spree
item.update_column(:pre_tax_amount, pre_tax_amount)
end
- def write_tax_line(owner, item, rate, amount)
+ # Finer distinctions — a reduced rate, an exempt supply — are a matter of
+ # rate configuration rather than something to infer from the number.
+ def reason_for(rate)
+ rate.amount.to_d.zero? ? 'zero_rated' : 'standard_rated'
+ end
+
+ # The jurisdiction that taxed the line. Read from the owner's tax address
+ # rather than its tax zone, because a Zone can span several countries and
+ # so has no single country to report. Rates carry their own country from
+ # the tax-side Zone decoupling onwards.
+ def jurisdiction_for(owner)
+ address = owner.tax_address
+ return {} if address.nil?
+
+ { country_iso: address.country&.iso, state_code: address.state&.abbr }
+ end
+
+ def write_tax_line(owner, item, rate, amount, reason)
owner_key = owner.is_a?(Spree::Order) ? :order : :cart
Spree::TaxLine.create!(
- tax_line_foreign_key(item.class) => item.id,
- owner_key => owner,
- tax_rate: rate,
- amount: amount,
- rate: rate.amount,
- label: rate.adjustment_label,
- included: rate.included_in_price,
- provider_id: 'internal'
+ {
+ tax_line_foreign_key(item.class) => item.id,
+ owner_key => owner,
+ tax_rate: rate,
+ amount: amount,
+ rate: rate.amount,
+ label: rate.adjustment_label,
+ included: rate.included_in_price,
+ provider_id: 'internal',
+ taxability_reason: reason
+ }.merge(jurisdiction_for(owner))
)
end
end
diff --git a/spree/core/lib/spree/testing_support/factories/tax_line_factory.rb b/spree/core/lib/spree/testing_support/factories/tax_line_factory.rb
index b8ec34f8be..f6666b0a71 100644
--- a/spree/core/lib/spree/testing_support/factories/tax_line_factory.rb
+++ b/spree/core/lib/spree/testing_support/factories/tax_line_factory.rb
@@ -7,5 +7,13 @@ FactoryBot.define do
label { 'Tax 5%' }
included { false }
provider_id { 'internal' }
+
+ # Tax written during checkout, before the cart becomes an order. The owner
+ # is exactly one of cart/order, so the order FK has to be cleared.
+ trait :on_cart do
+ line_item { create(:cart_with_line_items, line_items_count: 1).line_items.first }
+ order { nil }
+ cart { line_item.cart }
+ end
end
end
diff --git a/spree/core/spec/models/spree/tax_line_spec.rb b/spree/core/spec/models/spree/tax_line_spec.rb
index 9f0061bb52..9a087721e8 100644
--- a/spree/core/spec/models/spree/tax_line_spec.rb
+++ b/spree/core/spec/models/spree/tax_line_spec.rb
@@ -25,6 +25,14 @@ describe Spree::TaxLine, type: :model do
expect(tax_line).not_to be_valid
end
+ it 'is owned by a cart during checkout' do
+ tax_line = create(:tax_line, :on_cart)
+
+ expect(tax_line.owner).to eq(tax_line.cart)
+ expect(tax_line.order).to be_nil
+ expect(tax_line.cart.tax_lines.reload).to include(tax_line)
+ end
+
it 'stays meaningful without a tax rate' do
tax_line = create(:tax_line, order: order, line_item: line_item, tax_rate: nil, provider_id: 'avalara', rate: 0.0825)
expect(tax_line.reload.rate).to eq(0.0825)
diff --git a/spree/core/spec/models/spree/tax_provider/internal_spec.rb b/spree/core/spec/models/spree/tax_provider/internal_spec.rb
index 939dc5de72..3132959700 100644
--- a/spree/core/spec/models/spree/tax_provider/internal_spec.rb
+++ b/spree/core/spec/models/spree/tax_provider/internal_spec.rb
@@ -27,6 +27,16 @@ describe Spree::TaxProvider::Internal, type: :model do
expect(tax_line.line_item).to eq(line_item)
end
+ it 'stamps the treatment and the taxing jurisdiction' do
+ provider.estimate(order)
+
+ tax_line = order.tax_lines.reload.sole
+ expect(tax_line.taxability_reason).to eq('standard_rated')
+ expect(tax_line.category_code).to eq('S')
+ expect(tax_line.country_iso).to eq(order.tax_address.country.iso)
+ expect(tax_line.state_code).to eq(order.tax_address.state&.abbr)
+ end
+
it 'stores the pre-tax amount' do
provider.estimate(order)
expect(line_item.reload.pre_tax_amount).to eq(10)
@@ -73,6 +83,52 @@ describe Spree::TaxProvider::Internal, type: :model do
end
end
+ context 'with a matched zero rate' do
+ let!(:rate) { create(:tax_rate, zone: zone, amount: 0, tax_category: line_item.tax_category, included_in_price: false) }
+
+ it 'still writes a row, marked zero-rated' do
+ provider.estimate(order)
+
+ tax_line = order.tax_lines.reload.sole
+ expect(tax_line.amount).to eq(0)
+ expect(tax_line.taxability_reason).to eq('zero_rated')
+ expect(tax_line.category_code).to eq('Z')
+ expect(tax_line.tax_rate).to eq(rate)
+ end
+ end
+
+ context 'with no matching rate' do
+ let!(:rate) { create(:tax_rate, zone: zone, amount: 0.1, tax_category: create(:tax_category), included_in_price: false) }
+
+ it 'writes nothing, having formed no opinion' do
+ provider.estimate(order)
+
+ expect(order.tax_lines.reload).to be_empty
+ end
+ end
+
+ context 'when the owner is a cart' do
+ let(:cart) { create(:cart_with_line_items, line_items_count: 1, ship_address: create(:address)) }
+ let(:cart_line_item) { cart.line_items.first }
+ let(:cart_zone) do
+ create(:zone, kind: 'country', default_tax: true).tap do |zone|
+ zone.members.create!(zoneable: cart.tax_address.country)
+ end
+ end
+ let!(:rate) do
+ create(:tax_rate, zone: cart_zone, amount: 0.1, tax_category: cart_line_item.tax_category, included_in_price: false)
+ end
+
+ it 'owns the row through the cart FK' do
+ provider.estimate(cart)
+
+ tax_line = cart.tax_lines.reload.sole
+ expect(tax_line.cart).to eq(cart)
+ expect(tax_line.order).to be_nil
+ expect(tax_line.taxability_reason).to eq('standard_rated')
+ end
+ end
+
context 'with a taxable fee' do
let!(:rate) do
create(:tax_rate, zone: zone, amount: 0.1, tax_category: create(:tax_category, is_default: true), included_in_price: false)