diff --git a/core/app/models/spree/shipping_rate_tax.rb b/core/app/models/spree/shipping_rate_tax.rb index d581b5565db..289f9e39ba7 100644 --- a/core/app/models/spree/shipping_rate_tax.rb +++ b/core/app/models/spree/shipping_rate_tax.rb @@ -5,7 +5,7 @@ module Spree # @attr [Spree::ShippingRate] shipping_rate The shipping rate to be taxed # @attr [Spree::TaxRate] tax_rate The tax rate used to calculate the tax amount # @since 1.3.0 - # @see Spree::Tax::ShippingRateTaxer + # @see Spree::Stock::Estimator class ShippingRateTax < Spree::Base belongs_to :shipping_rate, class_name: "Spree::ShippingRate", optional: true belongs_to :tax_rate, class_name: "Spree::TaxRate", optional: true diff --git a/core/app/models/spree/tax/shipping_rate_taxer.rb b/core/app/models/spree/tax/shipping_rate_taxer.rb deleted file mode 100644 index a43a406309c..00000000000 --- a/core/app/models/spree/tax/shipping_rate_taxer.rb +++ /dev/null @@ -1,24 +0,0 @@ -# frozen_string_literal: true - -module Spree - module Tax - # Used to build shipping rate taxes - class ShippingRateTaxer - # Build shipping rate taxes for a shipping rate - # Modifies the passed-in shipping rate with associated shipping rate taxes. - # @param [Spree::ShippingRate] shipping_rate The shipping rate to add taxes to. - # This parameter will be modified. - # @return [Spree::ShippingRate] The shipping rate with associated tax objects - def tax(shipping_rate) - taxes = Spree::Config.shipping_rate_tax_calculator_class.new(shipping_rate.order).calculate(shipping_rate) - taxes.each do |tax| - shipping_rate.taxes.build( - amount: tax.amount, - tax_rate: tax.tax_rate - ) - end - shipping_rate - end - end - end -end diff --git a/core/app/models/spree/tax_calculator/shipping_rate.rb b/core/app/models/spree/tax_calculator/shipping_rate.rb index 12c354e2292..c7bea0d8449 100644 --- a/core/app/models/spree/tax_calculator/shipping_rate.rb +++ b/core/app/models/spree/tax_calculator/shipping_rate.rb @@ -9,7 +9,7 @@ module TaxCalculator # looking to provide their own calculator should adhere to the API of this # class. # - # @see Spree::Tax::ShippingRateTaxer + # @see Spree::Stock::Estimator class ShippingRate include Spree::Tax::TaxHelpers diff --git a/core/spec/models/spree/shipping_rate_spec.rb b/core/spec/models/spree/shipping_rate_spec.rb index 2041fb07a57..4d7bb217ce8 100644 --- a/core/spec/models/spree/shipping_rate_spec.rb +++ b/core/spec/models/spree/shipping_rate_spec.rb @@ -38,7 +38,10 @@ let(:order_address) { address } before do - Spree::Tax::ShippingRateTaxer.new.tax(shipping_rate) + shipping_rate.taxes.build( + amount: 0.91, + tax_rate: tax_rate, + ) end it "shows correct tax amount" do @@ -68,7 +71,10 @@ let(:order_address) { address } before do - Spree::Tax::ShippingRateTaxer.new.tax(shipping_rate) + shipping_rate.taxes.build( + amount: 1.0, + tax_rate: tax_rate, + ) end it "shows correct tax amount" do @@ -107,7 +113,15 @@ let(:order_address) { address } before do - Spree::Tax::ShippingRateTaxer.new.tax(shipping_rate) + shipping_rate.taxes.build( + amount: 1.0, + tax_rate: tax_rate, + ) + + shipping_rate.taxes.build( + amount: 0.5, + tax_rate: other_tax_rate, + ) end it "shows correct tax amount" do diff --git a/core/spec/models/spree/tax/shipping_rate_taxer_spec.rb b/core/spec/models/spree/tax/shipping_rate_taxer_spec.rb deleted file mode 100644 index d5ab7f4dc72..00000000000 --- a/core/spec/models/spree/tax/shipping_rate_taxer_spec.rb +++ /dev/null @@ -1,59 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe Spree::Tax::ShippingRateTaxer do - let(:shipping_rate) { build_stubbed(:shipping_rate) } - - subject(:taxer) { described_class.new(shipping_rate) } - - describe '#tax' do - subject(:taxer) { described_class.new.tax(shipping_rate) } - - context 'with no matching tax rates' do - it 'returns the object' do - expect(subject).to eq(shipping_rate) - expect(subject.taxes).to eq([]) - end - end - - context 'with tax rates' do - let(:ship_address) { create :address } - let(:tax_category) { create :tax_category } - let(:order) { create :order, ship_address: ship_address } - let(:shipment) { create :shipment, order: order } - let!(:shipping_method) { create :shipping_method, tax_category: tax_category, zones: [zone] } - let(:zone) { create :zone, countries: [ship_address.country] } - let!(:tax_rate_one) { create :tax_rate, tax_categories: [tax_category], zone: zone, amount: 0.1 } - let!(:tax_rate_two) do - create( - :tax_rate, - tax_categories: [create(:tax_category), tax_category], - zone: zone, - amount: 0.2 - ) - end - let!(:non_applicable_rate) { create :tax_rate, zone: zone } - let(:shipping_rate) { create :shipping_rate, cost: 10, shipping_method: shipping_method } - - it 'builds a shipping rate tax for every matching tax rate' do - expect(subject.taxes.length).to eq(2) - expect(subject.taxes.map(&:tax_rate)).to include(tax_rate_one) - expect(subject.taxes.map(&:tax_rate)).to include(tax_rate_two) - # This rate has a different tax category. - expect(subject.taxes.map(&:tax_rate)).not_to include(non_applicable_rate) - end - - it 'will produce a shipping rate that, when saved, also saves the taxes' do - expect { subject.save }.to change(Spree::ShippingRateTax, :count).by(2) - end - - it 'will produce a shipping rate with correct taxes' do - tax_one = subject.taxes.detect { |tax| tax.tax_rate == tax_rate_one } - tax_two = subject.taxes.detect { |tax| tax.tax_rate == tax_rate_two } - expect(tax_one.amount).to eq(shipping_rate.cost * tax_rate_one.amount) - expect(tax_two.amount).to eq(shipping_rate.cost * tax_rate_two.amount) - end - end - end -end