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
|
# File 'app/models/caboose/shipping_calculator.rb', line 11
def self.rates(order)
return [] if Caboose::store_shipping.nil?
sc = order.site.store_config
if sc.shipping_rates_function
rates = self.custom_rates(sc, order)
return rates
end
origin = Location.new(
:country => sc.origin_country,
:state => sc.origin_state,
:city => sc.origin_city,
:zip => sc.origin_zip
)
destination = Location.new(
:country => sc.origin_country,
:state => order.shipping_address.state,
:city => order.shipping_address.city,
:postal_code => order.shipping_address.zip
)
carriers = {}
carriers['UPS'] = UPS.new( :login => sc.ups_username, :password => sc.ups_password, :key => sc.ups_key, :origin_account => sc.ups_origin_account) if sc.ups_username.strip.length > 0
carriers['USPS'] = USPS.new( :login => sc.usps_username) if sc.usps_username.strip.length > 0
carriers['FedEx'] = FedEx.new(:login => sc.fedex_username, :password => sc.fedex_password, :key => sc.fedex_key, :account => sc.fedex_account) if sc.fedex_username.strip.length > 0
all_rates = []
order.packages.each do |op|
sp = op.shipping_package
package = op.activemerchant_package
rates = []
carriers.each do |name, carrier|
if sp.uses_carrier(name)
resp = carrier.find_rates(origin, destination, package)
resp.rates.sort_by(&:price).each do |rate|
sm = ShippingMethod.where(:carrier => name, :service_code => rate.service_code).first
sm = ShippingMethod.create(:carrier => name, :service_code => rate.service_code, :service_name => rate.service_name) if sm.nil?
next if !sp.uses_shipping_method(sm)
rates << { :shipping_method => sm, :price => rate.total_price.to_d / 100 }
end
end
end
all_rates << { :order_package => op, :rates => rates }
end
return all_rates
end
|