Class: Spree::Calculator::ExactorTaxCalculator

Inherits:
Spree::Calculator
  • Object
show all
Defined in:
app/models/spree/calculator/exactor_tax_calculator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.descriptionObject



6
7
8
# File 'app/models/spree/calculator/exactor_tax_calculator.rb', line 6

def self.description
  "Exactor sales tax compliance provider"
end

Instance Method Details

#compute(computable = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/spree/calculator/exactor_tax_calculator.rb', line 46

def compute(computable=nil)
  if computable
    settings = Spree::ExactorSetting.get_settings
    if settings and (Date.current < settings.effective_date)
      return 0
    end
    if (!computable.line_items.empty? and !computable.shipments.empty?) or computable.completed?
      exactor = SpreeExactorConnector::SpreeExactorConnector.new
      if is_needed_to_calculate_tax?(computable, exactor)
        response = exactor.calculate_tax_for_order(computable)
        if response.is_valid
          computable.update_attribute_without_callbacks(:exactor_invoice_transaction, response.invoice_id)
          update_line_items_with_tax_info(computable, response)
          return response.total_tax
        else
          computable.update_column(:exactor_order_hash_sum, nil)
          computable.errors.add(:base, "Unable to calculate tax: #{response.error_code}:#{response.error_description}")
          return 0
        end
      end
    end
    return computable.tax_total
  end
end

#is_full?(order) ⇒ Boolean

Whether this order is fully formed and ready for tax calculation.

Parameters:

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/spree/calculator/exactor_tax_calculator.rb', line 74

def is_full?(order)

  variant2quantity = Hash.new(0)

  order.shipments.each do |shipment|
    shipment.manifest.each do |item|
      variant2quantity[item.variant.id] += item.quantity
    end
  end

  return false unless variant2quantity.size == order.line_items.size

  return order.line_items.find_all { |li| variant2quantity[li.variant.id] != li.quantity }.empty?
end

#is_needed_to_calculate_tax?(computable, exactor) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/spree/calculator/exactor_tax_calculator.rb', line 11

def is_needed_to_calculate_tax?(computable, exactor)

  # don't try to calculate hash for partially formed order
  return false unless is_full?(computable)

  order_hash = exactor.calculate_hash(computable)
  current_order_hash = Spree::Order.find(computable.id).exactor_order_hash_sum
  result = order_hash!=current_order_hash
  if result
    computable.update_column(:exactor_order_hash_sum, order_hash)
  end
  return result
end

#update_line_items_with_tax_info(computable, response) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/spree/calculator/exactor_tax_calculator.rb', line 25

def update_line_items_with_tax_info(computable, response)
  variant2line = Hash.new
  computable.line_items.each { |line_item| variant2line[line_item.variant_id] = line_item }

  line2tax = Hash.new(0)

  computable.shipments.each do |shipment|
    shipment.manifest.each do |item|
      line = variant2line[item.variant.id]
      # line item collection is fresher then manifest collection. User can remove line item but it will remain in the manifest collection!
      unless line.nil?
        line2tax[line] += response.get_line_item_tax("_#{shipment.stock_location_id}_#{line.id}")
      end
    end
  end

  line2tax.each do |line_item, tax|
    line_item.update_attribute_without_callbacks(:exactor_inline_tax, tax)
  end
end