Class: Caboose::OrderPackage

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/caboose/order_package.rb

Constant Summary collapse

STATUS_PENDING =
'Pending'
STATUS_SHIPPED =
'Shipped'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_for_order(order) ⇒ Object

Calculates the shipping packages required for all the items in the order



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
92
93
94
95
96
97
98
# File 'app/models/caboose/order_package.rb', line 25

def self.create_for_order(order)
  
  store_config = order.site.store_config            
  if !store_config.calculate_packages                        
    self.custom_order_packages(store_config, order)
    return
  end
              
  # Make sure all the line items in the order have a quantity of 1
  extra_line_items = []
  order.line_items.each do |li|        
    if li.quantity > 1          
      (1..li.quantity).each{ |i|            
        extra_line_items << li.copy 
      }
      li.quantity = 1
      li.save
    end        
  end
  extra_line_items.each do |li|         
    li.quantity = 1                        
    li.save 
  end 
  
  # Make sure all the items in the order have attributes set
  order.line_items.each do |li|              
    v = li.variant
    Caboose.log("Error: variant #{v.id} has a zero weight") and return false if v.weight.nil? || v.weight == 0
    next if v.volume && v.volume > 0
    Caboose.log("Error: variant #{v.id} has a zero length") and return false if v.length.nil? || v.length == 0
    Caboose.log("Error: variant #{v.id} has a zero width" ) and return false if v.width.nil?  || v.width  == 0
    Caboose.log("Error: variant #{v.id} has a zero height") and return false if v.height.nil? || v.height == 0        
    v.volume = v.length * v.width * v.height
    v.save
  end
        
  # Reorder the items in the order by volume            
  h = {}
  order.line_items.each do |li|
    (1..li.quantity).each do |i|        
      v = li.variant          
      h[v.volume] = li          
    end
  end      
  line_items = h.sort_by{ |k,v| k }.collect{ |x| x[1] }      
  all_packages = ShippingPackage.reorder(:price).all      
  
  # Now go through each variant and fit it in a new or existing package
  line_items.each do |li|
    
    # See if the item will fit in any of the existing packages
    it_fits = false
    order.packages.each do |op|
      it_fits = op.fits(li)
      if it_fits
        li.order_package_id = op.id
        li.save            
        break
      end
    end        
    next if it_fits
    
    # Otherwise find the cheapest package the item will fit into
    all_packages.each do |sp|          
      if sp.fits(li.variant)
        op = OrderPackage.create(:order_id => order.id, :shipping_package_id => sp.id)
        li.order_package_id = op.id
        li.save                          
        break
      end
    end
           
  end            
end

.custom_order_packages(store_config, order) ⇒ Object



20
21
22
# File 'app/models/caboose/order_package.rb', line 20

def self.custom_order_packages(store_config, order)          
  eval(store_config.order_packages_function)    
end

Instance Method Details

#activemerchant_packageObject

Gets the activemerchant package based on the shipping package



107
108
109
110
111
112
113
# File 'app/models/caboose/order_package.rb', line 107

def activemerchant_package      
  weight = 0.0
  self.line_items.each{ |li| weight = weight + li.variant.weight }
  weight = weight * 0.035274 # Convert from grams to ounces
  sp = self.shipping_package
  return Package.new(weight, [sp.outside_length, sp.outside_width, sp.outside_height], :units => :imperial)
end

#fits(line_item = nil) ⇒ Object



100
101
102
103
104
# File 'app/models/caboose/order_package.rb', line 100

def fits(line_item = nil)
  variants = self.line_items.collect{ |li| li.variant }
  variants << line_item.variant if line_item
  return self.shipping_package.fits(variants)
end