Class: Caboose::TaxCalculator

Inherits:
Object
  • Object
show all
Defined in:
app/models/caboose/tax_calculator.rb

Class Method Summary collapse

Class Method Details

.authorized(invoice) ⇒ Object


40
41
42
43
44
45
# File 'app/models/caboose/tax_calculator.rb', line 40

def self.authorized(invoice)
  t = self.transaction(invoice)
  return if t.nil? || t.cart_items.nil? || t.cart_items.count == 0        
  t.invoice_id = invoice.id
  t.authorized            
end

.captured(invoice) ⇒ Object


47
48
49
50
51
52
# File 'app/models/caboose/tax_calculator.rb', line 47

def self.captured(invoice)
  t = self.transaction(invoice)      
  return if t.nil? || t.cart_items.nil? || t.cart_items.count == 0
  t.invoice_id = invoice.id
  t.captured            
end

.custom_tax(store_config, invoice) ⇒ Object


6
7
8
# File 'app/models/caboose/tax_calculator.rb', line 6

def self.custom_tax(store_config, invoice)          
  return store_config.custom_tax_function && store_config.custom_tax_function.strip.length > 0 ? eval(store_config.custom_tax_function) : 0.00    
end

.tax(invoice) ⇒ Object


10
11
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
# File 'app/models/caboose/tax_calculator.rb', line 10

def self.tax(invoice)
  sc = invoice.site.store_config                                                      

  if invoice.instore_pickup
    if sc.instore_tax_rate.nil?
      Caboose.log("Error: Null in-store tax rate")
      return 0.00
    end         
    return invoice.subtotal * sc.instore_tax_rate
  end

  return self.custom_tax(sc, invoice) if !sc.auto_calculate_tax
  return 0.00 if !invoice.shipping_address 
  return 0.00 if !invoice.has_taxable_items?
  return 0.00 if !invoice.has_shippable_items?

  return invoice.subtotal * invoice.tax_rate if invoice.tax_rate # See if the tax rate has already been calculated

  t = self.transaction(invoice)      
  lookup = t.lookup
  tax = lookup.tax_amount
  
  # Save the tax rate      
  invoice.tax_rate = tax/invoice.subtotal
  invoice.save
  
  # Return the tax amount
  return tax                                             
end

.transaction(invoice) ⇒ Object


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/caboose/tax_calculator.rb', line 54

def self.transaction(invoice)            
  sc = invoice.site.store_config 
  return nil if sc.nil? || !sc.auto_calculate_tax
  
  sa = invoice.shipping_address
  if sa.nil? || sa.address1.nil? || sa.city.nil? || sa.state.nil? || sa.zip.nil?        
    sa = invoice.billing_address
  end
  return nil if sa.nil? || sa.address1.nil? || sa.city.nil? || sa.state.nil? || sa.zip.nil?
  
  TaxCloud.configure do |config|
    config.  = sc.taxcloud_api_id
    config.api_key       = sc.taxcloud_api_key
    config.usps_username = sc.usps_username
  end
  
  origin      = TaxCloud::Address.new(:address1 => sc.origin_address1 , :address2 => sc.origin_address2 , :city => sc.origin_city , :state => sc.origin_state , :zip5 => sc.origin_zip )      
  destination = TaxCloud::Address.new(:address1 => sa.address1        , :address2 => sa.address2        , :city => sa.city        , :state => sa.state        , :zip5 => sa.zip        )      
  transaction = TaxCloud::Transaction.new(:customer_id => invoice.customer_id, :cart_id => invoice.id, :origin => origin, :destination => destination)
  invoice.line_items.each_with_index do |li, i|
    next if !li.variant.taxable # Skip any non-taxable items        
    transaction.cart_items << TaxCloud::CartItem.new(
      :index    => i,
      :item_id  => li.variant.id,
      :tic      => TaxCloud::TaxCodes::GENERAL,
      :price    => li.unit_price,
      :quantity => li.quantity
    )
  end
  return transaction
end