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
Read a fulfillment's tax classification from its delivery method
Łukasz Adamczyk709f5845cba2
diff --git a/spree/core/app/models/spree/fulfillment.rb b/spree/core/app/models/spree/fulfillment.rbindex 0ab1c94c8d..9b51152600 100644--- a/spree/core/app/models/spree/fulfillment.rb+++ b/spree/core/app/models/spree/fulfillment.rb@@ -520,18 +520,22 @@ module Spreecost + discounts.sum(:amount)end- # Returns the tax category of the selected shipping rate+ # How the delivery charge is classified for tax, as the merchant set it on+ # the delivery method. Read from the method rather than through the selected+ # rate's tax_rate: that hop answers out of the internal provider's rate+ # table, so an external provider asking for the freight classification got+ # nothing, and a classification with no matching rate configured reported+ # none at all — leaving delivery to be taxed under the store's default+ # category instead.#- # @return [Spree::TaxCategory]+ # @return [Spree::TaxCategory, nil]def tax_category- selected_delivery_rate.try(:tax_rate).try(:tax_category)+ delivery_method&.tax_categoryend- # Returns the tax category ID of the selected shipping rate- #- # @return [Integer]+ # @return [String, nil]def tax_category_id- selected_delivery_rate.try(:tax_rate).try(:tax_category_id)+ delivery_method&.tax_category_idend# Only one of either included_tax_total or additional_tax_total is setdiff --git a/spree/core/spec/models/spree/shipment_spec.rb b/spree/core/spec/models/spree/shipment_spec.rbindex 44bd91c6d6..92596cf376 100644--- a/spree/core/spec/models/spree/shipment_spec.rb+++ b/spree/core/spec/models/spree/shipment_spec.rb@@ -958,6 +958,44 @@ describe Spree::Shipment, type: :model doendend+ describe '#tax_category' do+ let(:order) { create(:order_with_line_items, line_items_count: 1) }+ let(:fulfillment) { order.fulfillments.first }+ let(:reduced_rate_category) { create(:tax_category, name: "Reduced #{Time.current.to_f}") }+ let(:delivery_method) { create(:shipping_method, tax_category: reduced_rate_category) }++ def select_rate(method)+ fulfillment.delivery_rates.destroy_all+ create(:delivery_rate, fulfillment: fulfillment, delivery_method: method, selected: true)+ fulfillment.reload+ end++ it "is the classification the merchant set on the delivery method" do+ select_rate(delivery_method)++ expect(fulfillment.tax_category).to eq(reduced_rate_category)+ expect(fulfillment.tax_category_id).to eq(reduced_rate_category.id)+ end++ # The classification used to be read through the selected rate's tax_rate,+ # so a method classified for a rate the merchant never configured in the+ # destination reported no category — and delivery was then taxed under the+ # store's default one.+ it 'answers even when no tax rate covers that category' do+ select_rate(delivery_method)++ expect(Spree::TaxRate.for_tax_category(reduced_rate_category)).to be_empty+ expect(fulfillment.tax_category).to eq(reduced_rate_category)+ end++ it 'is nil when the delivery method carries no classification' do+ select_rate(create(:shipping_method, tax_category: nil))++ expect(fulfillment.tax_category).to be_nil+ expect(fulfillment.tax_category_id).to be_nil+ end+ end+describe '#selected_delivery_rate_id / #selected_delivery_rate_id=' dolet(:order) { create(:order_with_line_items, line_items_count: 1) }let(:shipment) { order.fulfillments.first }diff --git a/spree/core/spec/models/spree/tax_provider/internal_spec.rb b/spree/core/spec/models/spree/tax_provider/internal_spec.rbindex 6f738bd945..6639bdbeaf 100644--- a/spree/core/spec/models/spree/tax_provider/internal_spec.rb+++ b/spree/core/spec/models/spree/tax_provider/internal_spec.rb@@ -217,6 +217,45 @@ describe Spree::TaxProvider::Internal, type: :model doendend+ context 'with a classified delivery charge' do+ let(:fulfillment) { order.fulfillments.first }+ let(:reduced_rate_category) { create(:tax_category, name: "Reduced #{Time.current.to_f}") }+ let(:delivery_method) { create(:shipping_method, tax_category: reduced_rate_category) }++ before do+ fulfillment.delivery_rates.destroy_all+ create(:delivery_rate, fulfillment: fulfillment, delivery_method: delivery_method, selected: true)+ # update_columns: the cost is the tax basis here, not an occasion to+ # recalculate the order and estimate tax a second time.+ fulfillment.update_columns(cost: 10)+ # A store default exists, so falling back to it is a visible wrong answer.+ create(:tax_rate, country: country, amount: 0.2, included_in_price: false,+ tax_category: create(:tax_category, is_default: true))+ fulfillment.reload+ end++ it "taxes delivery at its delivery method's category" do+ reduced_rate = create(:tax_rate, country: country, amount: 0.05, included_in_price: false,+ tax_category: reduced_rate_category)++ provider.estimate(order, [fulfillment])++ tax_line = order.tax_lines.reload.sole+ expect(tax_line.fulfillment).to eq(fulfillment)+ expect(tax_line.tax_rate).to eq(reduced_rate)+ expect(tax_line.amount).to eq(0.5)+ end++ # The classification is the merchant's answer even where they configured no+ # rate for it. Taxing delivery at the default category's standard rate+ # instead would over-collect with nothing to show it happened.+ it 'writes no delivery tax when no rate covers that classification' do+ provider.estimate(order, [fulfillment])++ expect(order.tax_lines.reload).to be_empty+ end+ end+context 'with a taxable fee' dolet!(:rate) docreate(:tax_rate, country: country, amount: 0.1, tax_category: create(:tax_category, is_default: true), included_in_price: false)