Class: Calculator::SalesTax
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Calculator
#available?, calculators, #description, #to_s
Class Method Details
.calculate_tax(order, rates) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'app/models/calculator/sales_tax.rb', line 12
def self.calculate_tax(order, rates)
ActiveSupport::Deprecation.warn("please use Calculator::SalesTax#compute instead", caller)
return 0 if rates.empty?
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
|
.description ⇒ Object
3
4
5
|
# File 'app/models/calculator/sales_tax.rb', line 3
def self.description
I18n.t("sales_tax")
end
|
7
8
9
10
|
# File 'app/models/calculator/sales_tax.rb', line 7
def self.register
super
TaxRate.register_calculator(self)
end
|
Instance Method Details
#compute(order) ⇒ Object
41
42
43
44
45
46
47
|
# File 'app/models/calculator/sales_tax.rb', line 41
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
|