Sha256: df7f8c04535374602ac1cdf3eec30057a809e91d3e3cb07293dd55d89259d103

Contents?: true

Size: 1.38 KB

Versions: 9

Compression:

Stored size: 1.38 KB

Contents

require_dependency 'spree/calculator'

module Spree
  class Calculator::DefaultTax < Calculator
    def self.description
      Spree.t(:default_tax)
    end

    def compute(computable)
      case computable
        when Spree::Order
          compute_order(computable)
        when Spree::LineItem
          compute_line_item(computable)
      end
    end


    private

      def rate
        self.calculable
      end

      def compute_order(order)
        matched_line_items = order.line_items.select do |line_item|
          line_item.tax_category == rate.tax_category
        end

        line_items_total = matched_line_items.sum(&:total)
        if rate.included_in_price
          deduced_total_by_rate(line_items_total, rate)
        else
          round_to_two_places(line_items_total * rate.amount)
        end
      end

      def compute_line_item(line_item)
        if line_item.tax_category == rate.tax_category
          if rate.included_in_price
            deduced_total_by_rate(line_item.total, rate)
          else
            round_to_two_places(line_item.total * rate.amount)
          end
        else
          0
        end
      end

      def round_to_two_places(amount)
        BigDecimal.new(amount.to_s).round(2, BigDecimal::ROUND_HALF_UP)
      end

      def deduced_total_by_rate(total, rate)
        round_to_two_places(total - ( total / (1 + rate.amount) ) )
      end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
spree_core-2.1.12 app/models/spree/calculator/default_tax.rb
spree_core-2.1.11 app/models/spree/calculator/default_tax.rb
spree_core-2.1.10 app/models/spree/calculator/default_tax.rb
spree_core-2.1.9 app/models/spree/calculator/default_tax.rb
spree_core-2.1.8 app/models/spree/calculator/default_tax.rb
spree_core-2.1.7 app/models/spree/calculator/default_tax.rb
spree_core-2.1.6 app/models/spree/calculator/default_tax.rb
spree_core-2.1.5 app/models/spree/calculator/default_tax.rb
spree_core-2.1.4 app/models/spree/calculator/default_tax.rb