Sha256: dc02b32546693ccdf685e24922e73627d0cbe326a059ee5b1de9b4f7743557a3

Contents?: true

Size: 1.6 KB

Versions: 21

Compression:

Stored size: 1.6 KB

Contents

class Calculator::SalesTax < Calculator

  def self.description
    I18n.t("sales_tax")
  end

  def self.register
    super
    TaxRate.register_calculator(self)
  end

  def self.calculate_tax(order, rates)
    ActiveSupport::Deprecation.warn("please use Calculator::SalesTax#compute instead", caller)

    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

  def compute(order)
    rate = self.calculable
    line_items = order.line_items.select { |i| i.product.tax_category == rate.tax_category }
    line_items.inject(0) {|sum, line_item|
      sum += line_item.total * rate.amount
    }
  end
end

Version data entries

21 entries across 21 versions & 6 rubygems

Version Path
apispree_core-0.0.0 app/models/calculator/sales_tax.rb
My-Commerce_core-1.1.0 app/models/calculator/sales_tax.rb
My-Commerce_core-1.0.0 app/models/calculator/sales_tax.rb
MyCommerceapi-1.0.0 core/app/models/calculator/sales_tax.rb
MyCommerce-0.0.3 core/app/models/calculator/sales_tax.rb
rfcommerce_core-0.0.3 app/models/calculator/sales_tax.rb
spree_core-0.60.6 app/models/calculator/sales_tax.rb
spree_core-0.60.5 app/models/calculator/sales_tax.rb
spree_core-0.50.4 app/models/calculator/sales_tax.rb
spree_core-0.60.4 app/models/calculator/sales_tax.rb
spree_core-0.50.3 app/models/calculator/sales_tax.rb
spree_core-0.60.3 app/models/calculator/sales_tax.rb
spree_core-0.60.2 app/models/calculator/sales_tax.rb
spree_core-0.70.0.rc2 app/models/calculator/sales_tax.rb
spree_core-0.70.RC1 app/models/calculator/sales_tax.rb
spree_core-0.60.1 app/models/calculator/sales_tax.rb
spree_core-0.60.0 app/models/calculator/sales_tax.rb
spree_core-0.60.0.RC1 app/models/calculator/sales_tax.rb
spree_core-0.50.2 app/models/calculator/sales_tax.rb
spree_core-0.50.1 app/models/calculator/sales_tax.rb