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 2: carry the tax treatment on TaxLine, not just the amount
Łukasz Adamczyk7f4b02c0dc4a
diff --git a/spree/core/app/models/spree/tax_line.rb b/spree/core/app/models/spree/tax_line.rbindex b3a13d3079..d869fc4cbc 100644--- a/spree/core/app/models/spree/tax_line.rb+++ b/spree/core/app/models/spree/tax_line.rb@@ -9,6 +9,46 @@ module Spreehas_prefix_id :tl+ # Why this line was taxed the way it was. Configuration, not a frozen+ # constant — a provider modelling a treatment core doesn't know about+ # registers its reason here, together with an INVOICE_CATEGORY_CODES entry.+ # The bar for a new value: it maps to a distinct invoice category code, or+ # to a distinct box on a tax return.+ class_attribute :taxability_reasons,+ default: %w[standard_rated reduced_rated zero_rated reverse_charge+ intra_community_supply export customer_exempt product_exempt+ not_collecting not_subject_to_tax]++ # Reason -> EN 16931 invoice category code (BT-151), from the UNTDID 5305+ # list. Shipped as code so a revision rides a release rather than a data+ # migration. Not exhaustive of the code list: `not_collecting` has no+ # meaningful code, since there is no EU VAT invoice to produce.+ INVOICE_CATEGORY_CODES = {+ 'standard_rated' => 'S',+ 'reduced_rated' => 'S',+ 'zero_rated' => 'Z',+ 'reverse_charge' => 'AE',+ 'intra_community_supply' => 'K',+ 'export' => 'G',+ 'customer_exempt' => 'E',+ 'product_exempt' => 'E',+ 'not_subject_to_tax' => 'O'+ }.freeze++ # Reason -> EN 16931 exemption reason code (BT-121) for the cross-border+ # cases. Genuinely exempt supplies (healthcare, education, financial+ # services) are absent by design: their code depends on which exemption+ # applied, which lives with the exemption input and the tax category.+ EXEMPTION_REASON_CODES = {+ 'reverse_charge' => 'VATEX-EU-AE',+ 'intra_community_supply' => 'VATEX-EU-IC',+ 'export' => 'VATEX-EU-G',+ 'not_subject_to_tax' => 'VATEX-EU-O'+ }.freeze++ # Provider payloads (jurisdiction breakdowns, external ids) read nil-safe.+ attribute :data, default: -> { {} }+# Source — nil for externally-computed taxbelongs_to :tax_rate, class_name: 'Spree::TaxRate', optional: true@@ -21,6 +61,11 @@ module Spreeattribute :included, :boolean, default: falsevalidates :rate, numericality: { greater_than_or_equal_to: 0 }+ # The lambda re-reads the list at validation time, so a reason an extension+ # registers after boot is accepted.+ validates :taxability_reason,+ inclusion: { in: ->(tax_line) { tax_line.class.taxability_reasons } },+ allow_nil: truevalidate :exactly_one_adjustablescope :included_in_price, -> { where(included: true) }@@ -34,6 +79,22 @@ module Spreeline_item || fulfillment || feeend+ # The EN 16931 invoice category code (BT-151) an e-invoice line carries.+ #+ # @return [String, nil] nil when the reason is unset or maps to no code+ def category_code+ INVOICE_CATEGORY_CODES[taxability_reason]+ end++ # The EN 16931 exemption reason code (BT-121). Nil for a genuinely exempt+ # supply, whose code depends on which exemption applied rather than on the+ # reason alone.+ #+ # @return [String, nil]+ def exemption_reason_code+ EXEMPTION_REASON_CODES[taxability_reason]+ end+def included?includedenddiff --git a/spree/core/db/migrate/20260806000002_add_treatment_columns_to_spree_tax_lines.rb b/spree/core/db/migrate/20260806000002_add_treatment_columns_to_spree_tax_lines.rbnew file mode 100644index 0000000000..40b0b3b46b--- /dev/null+++ b/spree/core/db/migrate/20260806000002_add_treatment_columns_to_spree_tax_lines.rb@@ -0,0 +1,19 @@+class AddTreatmentColumnsToSpreeTaxLines < ActiveRecord::Migration[8.1]+ def change+ # The cause of the treatment, not just its amount: zero-tax lines land in+ # different boxes of a tax return depending on why they are zero, and+ # e-invoicing integrations derive the invoice category and exemption codes+ # European mandates require from this reason plus the order's own facts.+ add_column :spree_tax_lines, :taxability_reason, :string++ # The taxing jurisdiction of the line. Not derivable from the shipping+ # address: origin-taxed sales (below-threshold digital supplies, US states+ # that source to the seller) tax somewhere else entirely, and the EU+ # one-stop-shop return is filed per destination country.+ add_column :spree_tax_lines, :country_iso, :string+ add_column :spree_tax_lines, :state_code, :string++ add_index :spree_tax_lines, :taxability_reason+ add_index :spree_tax_lines, [:country_iso, :state_code]+ end+enddiff --git a/spree/core/spec/models/spree/tax_line_spec.rb b/spree/core/spec/models/spree/tax_line_spec.rbindex 1996924aec..9f0061bb52 100644--- a/spree/core/spec/models/spree/tax_line_spec.rb+++ b/spree/core/spec/models/spree/tax_line_spec.rb@@ -31,6 +31,78 @@ describe Spree::TaxLine, type: :model doexpect(tax_line.tax_rate).to be_nilend+ describe 'taxability reason' do+ subject(:tax_line) { described_class.new(order: order, line_item: line_item, amount: 0, rate: 0, label: 'VAT') }++ it 'accepts a core reason and nil' do+ tax_line.taxability_reason = 'reverse_charge'+ expect(tax_line).to be_valid++ tax_line.taxability_reason = nil+ expect(tax_line).to be_valid+ end++ it 'rejects a reason outside the vocabulary' do+ tax_line.taxability_reason = 'made_up'+ expect(tax_line).not_to be_valid+ expect(tax_line.errors[:taxability_reason]).to be_present+ end++ it 'accepts a reason registered after boot' do+ original = described_class.taxability_reasons+ described_class.taxability_reasons = original + ['margin_scheme']++ tax_line.taxability_reason = 'margin_scheme'+ expect(tax_line).to be_valid+ ensure+ described_class.taxability_reasons = original+ end+ end++ describe 'invoice codes' do+ subject(:tax_line) { described_class.new }++ it 'maps rated reasons to a category code without an exemption reason' do+ tax_line.taxability_reason = 'reduced_rated'+ expect(tax_line.category_code).to eq('S')+ expect(tax_line.exemption_reason_code).to be_nil+ end++ it 'maps cross-border reasons to both codes' do+ tax_line.taxability_reason = 'intra_community_supply'+ expect(tax_line.category_code).to eq('K')+ expect(tax_line.exemption_reason_code).to eq('VATEX-EU-IC')+ end++ it 'leaves an exempt supply without an exemption reason code' do+ tax_line.taxability_reason = 'product_exempt'+ expect(tax_line.category_code).to eq('E')+ expect(tax_line.exemption_reason_code).to be_nil+ end++ it 'has no code for a jurisdiction the merchant does not collect in' do+ tax_line.taxability_reason = 'not_collecting'+ expect(tax_line.category_code).to be_nil+ end++ it 'has no code without a reason' do+ expect(tax_line.category_code).to be_nil+ end+ end++ describe 'provider data' do+ it 'defaults to an empty hash so payloads read nil-safe' do+ expect(described_class.new.data).to eq({})+ expect(described_class.new.data['jurisdictions']).to be_nil+ end++ it 'round-trips a provider payload' do+ tax_line = create(:tax_line, order: order, line_item: line_item,+ data: { 'jurisdictions' => [{ 'name' => 'WA', 'amount' => '1.2' }] })+ expect(tax_line.reload.data['jurisdictions'].first['name']).to eq('WA')+ end+ end+describe 'included vs additional' dolet!(:included_line) { create(:tax_line, order: order, line_item: line_item, included: true) }let!(:additional_line) { create(:tax_line, order: order, line_item: line_item, included: false) }