Sha256: f8c526a42f7716a391bf36ae366feb2f4b700893424dfabc6da337bc74516d02

Contents?: true

Size: 1.25 KB

Versions: 11

Compression:

Stored size: 1.25 KB

Contents

module Spree #:nodoc:
  class SalesTaxCalculator

    def self.calculate_tax(order, rates) 
      return 0 if rates.empty?
      # note: there is a bug with associations in rails 2.1 model caching so we're using this hack
      # (see http://rails.lighthouseapp.com/projects/8994/tickets/785-caching-models-fails-in-development)
      cache_hack = rates.first.respond_to?(:tax_category_id)
            
      taxable_totals = {}
      order.line_items.each do |line_item|
        next unless tax_category = line_item.variant.product.tax_category
        next unless rate = rates.find { | sales_rate | sales_rate.tax_category_id == tax_category.id } if cache_hack        
        next unless rate = rates.find { | sales_rate | sales_rate.tax_category == tax_category } unless cache_hack        

        taxable_totals[tax_category] ||= 0        
        taxable_totals[tax_category] += line_item.total
      end

      return 0 if taxable_totals.empty?
      tax = 0
      rates.each do |rate|
        tax_category = rate.tax_category unless cache_hack
        tax_category = TaxCategory.find(rate.tax_category_id) if cache_hack
        next unless taxable_total = taxable_totals[tax_category]   
        tax += taxable_total * rate.amount
      end
      tax
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
spree-0.8.4 vendor/extensions/tax_calculator/lib/spree/sales_tax_calculator.rb
spree-0.8.5 vendor/extensions/tax_calculator/lib/spree/sales_tax_calculator.rb
spree-0.5.0 vendor/extensions/tax_calculator/lib/spree/sales_tax_calculator.rb
spree-0.5.1 vendor/extensions/tax_calculator/lib/spree/sales_tax_calculator.rb
spree-0.6.0 vendor/extensions/tax_calculator/lib/spree/sales_tax_calculator.rb
spree-0.7.0 vendor/extensions/tax_calculator/lib/spree/sales_tax_calculator.rb
spree-0.7.1 vendor/extensions/tax_calculator/lib/spree/sales_tax_calculator.rb
spree-0.8.0 vendor/extensions/tax_calculator/lib/spree/sales_tax_calculator.rb
spree-0.8.1 vendor/extensions/tax_calculator/lib/spree/sales_tax_calculator.rb
spree-0.8.2 vendor/extensions/tax_calculator/lib/spree/sales_tax_calculator.rb
spree-0.8.3 vendor/extensions/tax_calculator/lib/spree/sales_tax_calculator.rb