Class: CabooseStore::ShippingCalculator

Inherits:
Object
  • Object
show all
Defined in:
app/models/caboose_store/shipping_calculator.rb

Class Method Summary collapse

Class Method Details

.rate(order) ⇒ Object



74
75
76
77
78
# File 'app/models/caboose_store/shipping_calculator.rb', line 74

def self.rate(order)
  return nil if !order.shipping_method_code
  self.rates(order).each { |rate| return rate if rate[:service_code] == order.shipping_method_code }
  return nil
end

.rates(order) ⇒ Object



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_store/shipping_calculator.rb', line 6

def self.rates(order)
  return [] if CabooseStore::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 => CabooseStore::shipping[:origin][:country],
    :state   => CabooseStore::shipping[:origin][:state],
    :city    => CabooseStore::shipping[:origin][:city],
    :zip     => CabooseStore::shipping[:origin][:zip]
  )
  
  destination = Location.new(
    :country     => CabooseStore::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            => CabooseStore::shipping[:ups][:key],
    :login          => CabooseStore::shipping[:ups][:username],
    :password       => CabooseStore::shipping[:ups][:password],
    :origin_account => CabooseStore::shipping[:ups][:origin_account]
  )
  
  ups_response = ups.find_rates(origin, destination, packages)
  
  rates = ups_response.rates.collect do |rate|
    next if CabooseStore::allowed_shipping_method_codes && !CabooseStore::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