Class: Caboose::ShippingCalculator
- Inherits:
-
Object
- Object
- Caboose::ShippingCalculator
- Defined in:
- app/models/caboose/shipping_calculator.rb
Class Method Summary collapse
- .calculate_rate(op) ⇒ Object
- .custom_rates(store_config, order) ⇒ Object
- .order_package_rates(op) ⇒ Object
- .rate(order) ⇒ Object
-
.rates(order) ⇒ Object
def self.rates(order) return [] if order.site.nil? || order.site.store_config.nil? sc = order.site.store_config if !sc.auto_calculate_shipping rates = self.custom_rates(sc, order) return rates end.
Class Method Details
.calculate_rate(op) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'app/models/caboose/shipping_calculator.rb', line 113 def self.calculate_rate(op) order = op.order sc = order.site.store_config sa = order.shipping_address origin = ActiveShipping::Location.new(:country => sc.origin_country, :state => sc.origin_state, :city => sc.origin_city, :zip => sc.origin_zip) destination = ActiveShipping::Location.new(:country => sc.origin_country, :state => sa.state, :city => sa.city, :postal_code => sa.zip) carrier = case op.shipping_method.carrier when 'UPS' then ActiveShipping::UPS.new( :login => sc.ups_username, :password => sc.ups_password, :key => sc.ups_key, :origin_account => sc.ups_origin_account) when 'USPS' then ActiveShipping::USPS.new( :login => sc.usps_username) when 'FedEx' then ActiveShipping::FedEx.new(:login => sc.fedex_username, :password => sc.fedex_password, :key => sc.fedex_key, :account => sc.fedex_account) end #Caboose.log(op.shipping_method.inspect) sm = op.shipping_method package = op.activemerchant_package resp = carrier.find_rates(origin, destination, package) resp.rates.sort_by(&:price).each do |rate| #Caboose.log("rate.service_code = #{rate.service_code}, rate.service_name = #{rate.service_name}") sm2 = ShippingMethod.where( :carrier => sm.carrier, :service_code => rate.service_code, :service_name => rate.service_name).first sm2 = ShippingMethod.create(:carrier => sm.carrier, :service_code => rate.service_code, :service_name => rate.service_name) if sm2.nil? if sm2.id == sm.id price = rate.total_price price = rate.package_rates[0].rate if price.nil? && rate.package_rates && rate.package_rates.count > 0 return price.to_d/100 end end return nil end |
.custom_rates(store_config, order) ⇒ Object
7 8 9 |
# File 'app/models/caboose/shipping_calculator.rb', line 7 def self.custom_rates(store_config, order) return eval(store_config.custom_shipping_function) end |
.order_package_rates(op) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'app/models/caboose/shipping_calculator.rb', line 79 def self.order_package_rates(op) order = op.order sc = order.site.store_config sa = order.shipping_address origin = ActiveShipping::Location.new(:country => sc.origin_country, :state => sc.origin_state, :city => sc.origin_city, :zip => sc.origin_zip) destination = ActiveShipping::Location.new(:country => sc.origin_country, :state => sa.state, :city => sa.city, :postal_code => sa.zip) carriers = {} carriers['UPS'] = ActiveShipping::UPS.new( :login => sc.ups_username, :password => sc.ups_password, :key => sc.ups_key, :origin_account => sc.ups_origin_account) if sc.ups_username && sc.ups_username.strip.length > 0 carriers['USPS'] = ActiveShipping::USPS.new( :login => sc.usps_username) if sc.usps_username && sc.usps_username.strip.length > 0 carriers['FedEx'] = ActiveShipping::FedEx.new(:login => sc.fedex_username, :password => sc.fedex_password, :key => sc.fedex_key, :account => sc.fedex_account) if sc.fedex_username && sc.fedex_username.strip.length > 0 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, :service_name => rate.service_name).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) price = rate.total_price price = rate.package_rates[0].rate if price.nil? && rate.package_rates && rate.package_rates.count > 0 rates << { :shipping_method => sm, :total_price => (price.to_d/100) } end end end if rates.count == 0 Caboose.log("Error: no shipping rates found for order package #{op.id}.") end return rates end |
.rate(order) ⇒ Object
146 147 148 149 150 |
# File 'app/models/caboose/shipping_calculator.rb', line 146 def self.rate(order) return nil if !order.shipping_service_code self.rates(order).each { |rate| return rate if rate[:service_code] == order.shipping_service_code } return nil end |
.rates(order) ⇒ Object
def self.rates(order)
return [] if order.site.nil? || order.site.store_config.nil?
sc = order.site.store_config
if !sc.auto_calculate_shipping
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 && sc.ups_username.strip.length > 0
carriers['USPS'] = USPS.new( :login => sc.usps_username) if sc.usps_username && 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 && sc.fedex_username.strip.length > 0
all_rates = []
order.order_packages.all.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, :service_name => rate.service_name).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)
price = rate.total_price
price = rate.package_rates[0].rate if price.nil? && rate.package_rates && rate.package_rates.count > 0
rates << { :shipping_method => sm, :total_price => (price.to_d/100) }
end
end
end
if rates.count == 0
Caboose.log("Error: no shipping rates found for order package #{op.id}.")
end
all_rates << { :order_package => op, :rates => rates }
end
return all_rates
end
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/models/caboose/shipping_calculator.rb', line 63 def self.rates(order) return [] if order.site.nil? || order.site.store_config.nil? sc = order.site.store_config if !sc.auto_calculate_shipping rates = self.custom_rates(sc, order) return rates end all_rates = order.order_packages.all.collect{ |op| { :order_package => op, :rates => self.order_package_rates(op) }} return all_rates end |