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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
85
86
87
88
89
90
91
|
# File 'app/models/caboose/tax_calculator.rb', line 10
def self.tax(order)
return 0.00 if !order.shipping_address
sc = order.site.store_config
if !sc.auto_calculate_tax
tax = self.custom_tax(sc, order)
return tax
end
if order.tax_rate
return order.subtotal * order.tax_rate
end
sa = order.shipping_address
TaxCloud.configure do |config|
config.api_login_id = 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 => order.customer_id,
:cart_id => order.id,
:origin => origin,
:destination => destination
)
order.line_items.each_with_index do |li, i|
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
lookup = transaction.lookup
tax = lookup.tax_amount
order.tax_rate = tax/order.subtotal
order.save
return tax
end
|