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
Credit tax only on returned items that arrived
Łukasz Adamczyk963a41b2f9e1
diff --git a/spree/core/app/models/spree/market.rb b/spree/core/app/models/spree/market.rbindex bcb89f211b..3c32298fbe 100644--- a/spree/core/app/models/spree/market.rb+++ b/spree/core/app/models/spree/market.rb@@ -108,7 +108,7 @@ module Spree## @return [Spree::TaxProvider::Base]def tax_provider_instance- (tax_provider.presence || Rails.application.config.spree.tax_provider.to_s).constantize.new+ (tax_provider.presence || Spree.default_tax_provider_class.to_s).constantize.newend# Returns supported locales as an array, always including default_localediff --git a/spree/core/app/models/spree/tax_provider/base.rb b/spree/core/app/models/spree/tax_provider/base.rbindex 2c7cbc6fb6..05d7dab948 100644--- a/spree/core/app/models/spree/tax_provider/base.rb+++ b/spree/core/app/models/spree/tax_provider/base.rb@@ -65,7 +65,7 @@ module Spreename: display_name,available: available_for_store?(store),unsupported_capabilities: unsupported_capability_details,- default: name == Rails.application.config.spree.tax_provider.to_s+ default: name == Spree.default_tax_provider_class.to_s}enddiff --git a/spree/core/app/workflows/spree/orders/cancel.rb b/spree/core/app/workflows/spree/orders/cancel.rbindex 29da6fb622..761d666d1c 100644--- a/spree/core/app/workflows/spree/orders/cancel.rb+++ b/spree/core/app/workflows/spree/orders/cancel.rb@@ -95,12 +95,6 @@ module Spree# Gateway I/O. Payments fully covered by a gift card are only voided,# never refunded; everything else cancels captured payments (void or# refund at the gateway's discretion) and voids what never completed.- # Reverses the filed tax document. The canceled sale must stop appearing- # in the merchant's tax liability.- def void_tax- order.tax_provider.void(order)- end-def settle_paymentsif order.gift_card.present? && order.covered_by_store_credit?order.payments.completed.store_credits.each(&:void!)@@ -110,6 +104,12 @@ module Spreeorder.payments.store_credits.pending.each(&:void!)endend++ # Reverses the filed tax document. The canceled sale must stop appearing+ # in the merchant's tax liability.+ def void_tax+ order.tax_provider.void(order)+ endendendenddiff --git a/spree/core/app/workflows/spree/returns/refund.rb b/spree/core/app/workflows/spree/returns/refund.rbindex 452c6b8988..f8043e50eb 100644--- a/spree/core/app/workflows/spree/returns/refund.rb+++ b/spree/core/app/workflows/spree/returns/refund.rb@@ -49,9 +49,16 @@ module Spree# Credits the returned items against the filed tax document, keyed to the# original supply date rather than today's — the rate that applied then is# the rate to credit back.+ #+ # Only lines that actually arrived are credited, matching what+ # +received_total+ refunds: a customer who announced three items and sent+ # two must not have tax credited on the third.def refund_tax+ received = return_record.return_line_items.select { |line| line.received_quantity.to_i.positive? }+ return if received.empty?+order = return_record.order- order.tax_provider.refund(order, return_record.return_line_items.to_a, tax_date: order.completed_at)+ order.tax_provider.refund(order, received, tax_date: order.completed_at)enddef internal_refund?diff --git a/spree/core/lib/spree/core.rb b/spree/core/lib/spree/core.rbindex 21a43f0bdd..002fd20711 100644--- a/spree/core/lib/spree/core.rb+++ b/spree/core/lib/spree/core.rb@@ -300,6 +300,16 @@ module SpreeRails.application.config.spree.tax_provider.newend+ # The store-wide default engine as a class, for callers that need to name it+ # rather than use it — market selection constantizing its own choice, and the+ # admin marking which entry is the default. Instantiating just to read a class+ # name is what this avoids.+ #+ # @return [Class]+ def self.default_tax_provider_class+ Rails.application.config.spree.tax_provider+ end+def self.tax_provider=(value)Rails.application.config.spree.tax_provider = valueenddiff --git a/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb b/spree/core/spec/workflows/spree/returns/lifecycle_spec.rbindex 029fe7ef51..5caebd64a9 100644--- a/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb+++ b/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb@@ -171,6 +171,33 @@ RSpec.describe 'Spree::Returns workflows' doexpect(credit.amount).to eq(return_record.refund_total)end+ it 'credits only the lines that actually arrived' do+ # Two of three announced units came back, so the third must not be+ # credited — the same rule resolve_amount applies to the money.+ line = return_record.return_line_items.first+ line.update!(quantity: 3, received_quantity: 2)+ # A second announced line the warehouse never received.+ unreceived = return_record.return_line_items.create!(+ fulfillment_item: line.fulfillment_item,+ line_item: line.line_item,+ variant: line.variant,+ quantity: 1,+ received_quantity: 0+ )++ order = return_record.order+ provider = instance_double(Spree::TaxProvider::Internal, refund: nil, estimate: nil)+ allow(order).to receive(:tax_provider).and_return(provider)+ allow_any_instance_of(Spree::Return).to receive(:order).and_return(order)++ Spree::Returns::Refund.call(return_record: return_record, refund_method: 'store_credit')++ expect(provider).to have_received(:refund) do |_order, lines, **|+ expect(lines).to include(line)+ expect(lines).not_to include(unreceived)+ end+ end+it 'credits the returned items against the filed tax document' doorder = return_record.orderprovider = instance_double(Spree::TaxProvider::Internal, refund: nil, estimate: nil)