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



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
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/caboose/order_package.rb', line 31

def self.create_for_order(order)
  
  store_config = order.site.store_config            
  if !store_config.auto_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.where(:site_id => order.site_id).reorder(:flat_rate_price).all      
  
  # Now go through each variant and fit it in a new or existing package
  line_items.each do |li|
    next if li.variant.downloadable
    
    # See if the item will fit in any of the existing packages
    it_fits = false
    order.order_packages.all.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
    it_fits = false
    all_packages.each do |sp|
      it_fits = sp.fits(li.variant)          
      if it_fits            
        op = OrderPackage.create(:order_id => order.id, :shipping_package_id => sp.id)
        li.order_package_id = op.id
        li.save                          
        break
      end
    end
    next if it_fits
    
    Caboose.log("Error: line item #{li.id} (#{li.variant.product.title}) does not fit into any package.")               
  end      
end

.custom_order_packages(store_config, order) ⇒ Object



26
27
28
# File 'app/models/caboose/order_package.rb', line 26

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

Instance Method Details

#activemerchant_packageObject

Gets the activemerchant package based on the shipping package



118
119
120
121
122
123
124
# File 'app/models/caboose/order_package.rb', line 118

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

#check_nil_fieldsObject



22
23
24
# File 'app/models/caboose/order_package.rb', line 22

def check_nil_fields      
  self.total = 0.00 if self.total.nil?
end

#fits(line_item = nil) ⇒ Object



111
112
113
114
115
# File 'app/models/caboose/order_package.rb', line 111

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