Class: Calculator::Vat

Inherits:
Calculator
  • Object
show all
Defined in:
app/models/calculator/vat.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Calculator

#available?, calculators, #description, register, #to_s

Class Method Details

.calculate_tax_on(taxable) ⇒ Object



7
8
9
10
11
# File 'app/models/calculator/vat.rb', line 7

def self.calculate_tax_on(taxable)
  # taxable may be product or variant
  taxable = taxable.product if taxable.respond_to?(:product)
  (taxable.price * taxable.effective_tax_rate).round(2, BigDecimal::ROUND_HALF_UP)
end

.descriptionObject



3
4
5
# File 'app/models/calculator/vat.rb', line 3

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

Instance Method Details

#compute(order) ⇒ Object

computes vat for line_items associated with order, and tax rate and now coupon discounts are taken into account in tax calcs and tax is added to shipment if :shipment_inc_vat is set coupons and shipment are applied if this object is the rate for the default category

(also items with no category get this rate applied)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/calculator/vat.rb', line 18

def compute(order)
  rate = self.calculable
  tax = 0

  if rate.tax_category.is_default
    order.adjustments.each do | adjust |
      next if adjust.originator_type == "TaxRate"
      next if adjust.originator_type == "ShippingMethod" and not Spree::Config[:shipment_inc_vat]

      tax += (adjust.amount * rate.amount).round(2, BigDecimal::ROUND_HALF_UP)
    end
  end

  order.line_items.each do  | line_item|
    if line_item.product.tax_category  #only apply this calculator to products assigned this rates category
      next unless line_item.product.tax_category == rate.tax_category
    else
      next unless rate.tax_category.is_default # and apply to products with no category, if this is the default rate
    end
    tax += (line_item.price * rate.amount).round(2, BigDecimal::ROUND_HALF_UP) * line_item.quantity
  end

  tax
end