Sha256: 455479d6d3f957470d7446d0234b4ed852fbbec49febda062be6300ba7df551e

Contents?: true

Size: 1.73 KB

Versions: 5

Compression:

Stored size: 1.73 KB

Contents

module Spree
  module Tax
    module TaxHelpers
      private

      # Imagine with me this scenario:
      # You are living in Spain and you have a store which ships to France.
      # Spain is therefore your default tax rate.
      # When you ship to Spain, you want the Spanish rate to apply.
      # When you ship to France, you want the French rate to apply.
      #
      # Normally, Spree would notice that you have two potentially applicable
      # tax rates for one particular item.
      # When you ship to Spain, only the Spanish one will apply.
      # When you ship to France, you'll see a Spanish refund AND a French tax.
      # This little bit of code at the end stops the Spanish refund from appearing.
      #
      # For further discussion, see https://github.com/spree/spree/issues/4397 and https://github.com/spree/spree/issues/4327.
      def applicable_rates(order)
        order_zone_tax_category_ids = rates_for_order(order).map(&:tax_category_id)
        default_rates_with_unmatched_tax_category = rates_for_default_zone.to_a.delete_if do |default_rate|
          order_zone_tax_category_ids.include?(default_rate.tax_category_id)
        end

        (rates_for_order(order) + default_rates_with_unmatched_tax_category).uniq
      end

      def rates_for_order(order)
        @rates_for_order ||= Spree::TaxRate.for_address(order.tax_address)
      end

      def rates_for_default_zone
        @rates_for_default_zone ||= Spree::TaxRate.for_zone(Spree::Zone.default_tax)
      end

      def sum_of_included_tax_rates(item)
        rates_for_item(item).map(&:amount).sum
      end

      def rates_for_item(item)
        applicable_rates(item.order).select { |rate| rate.tax_category_id == item.tax_category_id }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
solidus_core-2.2.2 app/models/spree/tax/tax_helpers.rb
solidus_core-2.2.1 app/models/spree/tax/tax_helpers.rb
solidus_core-2.2.0 app/models/spree/tax/tax_helpers.rb
solidus_core-2.2.0.rc1 app/models/spree/tax/tax_helpers.rb
solidus_core-2.2.0.beta1 app/models/spree/tax/tax_helpers.rb