6
7
8
9
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
|
# File 'app/models/caboose/shipping_calculator.rb', line 6
def self.rates(order)
return [] if Caboose::shipping.nil?
total = 0.0
weight = 0.0
order.line_items.each do |li|
total = total + (li.variant.shipping_unit_value.nil? ? 0 : li.variant.shipping_unit_value)
weight = weight + (li.variant.weight || 0)
end
length = 0
width = 0
height = 0
if total <= 5
length = 15.5
width = 9.5
height = 6
elsif total <= 10
length = 12
width = 8
height = 5
else
length = 20
width = 16
height = 14
end
origin = Location.new(
:country => Caboose::shipping[:origin][:country],
:state => Caboose::shipping[:origin][:state],
:city => Caboose::shipping[:origin][:city],
:zip => Caboose::shipping[:origin][:zip]
)
destination = Location.new(
:country => Caboose::shipping[:origin][:country],
:state => order.shipping_address.state,
:city => order.shipping_address.city,
:postal_code => order.shipping_address.zip
)
packages = [Package.new(weight, [length, width, height], :units => :imperial)]
ups = UPS.new(
:key => Caboose::shipping[:ups][:key],
:login => Caboose::shipping[:ups][:username],
:password => Caboose::shipping[:ups][:password],
:origin_account => Caboose::shipping[:ups][:origin_account]
)
ups_response = ups.find_rates(origin, destination, packages)
rates = ups_response.rates.collect do |rate|
next if Caboose::allowed_shipping_method_codes && !Caboose::allowed_shipping_method_codes.index(rate.service_code)
{
:service_code => rate.service_code,
:service_name => rate.service_name,
:total_price => rate.total_price.to_d / 100,
:negotiated_rate => rate.negotiated_rate
}
end
return rates.compact
end
|