Class: Caboose::InvoicePackage

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

Constant Summary collapse

STATUS_PENDING =
'Pending'
STATUS_SHIPPED =
'Shipped'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.custom_invoice_packages(store_config, invoice) ⇒ Object



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

def self.custom_invoice_packages(store_config, invoice)          
  eval(store_config.custom_packages_function)    
end

Instance Method Details

#activemerchant_packageObject

Gets the activemerchant package based on the shipping package



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/caboose/invoice_package.rb', line 57

def activemerchant_package
  sc = self.invoice.site.store_config
  
  weight = 0.0
  self.line_items.each{ |li| weight = weight + (li.variant.weight * li.quantity) }      
  weight = weight * 0.035274 if sc.weight_unit == StoreConfig::WEIGHT_UNIT_METRIC # grams to ounces
              
  sp = self.shipping_package
  dimensions = [sp.outside_length, sp.outside_width, sp.outside_height]
  if sc.length_unit == StoreConfig::LENGTH_UNIT_METRIC # cm to inches
    dimensions[0] = dimensions[0] / 2.54
    dimensions[1] = dimensions[0] / 2.54
    dimensions[2] = dimensions[0] / 2.54
  end
  
  return ActiveShipping::Package.new(weight, dimensions, :units => :imperial, :container => :variable)
end

#calculate_shipping_packageObject

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/caboose/invoice_package.rb', line 31

def calculate_shipping_package
  
  store_config = invoice.site.store_config            
  if !store_config.auto_calculate_packages                        
    return self.custom_shipping_package(store_config, self)        
  end
  
  variants = self.line_items.sort_by{ |li| li.quantity * (li.variant.volume ? li.variant.volume : 0.00) * -1 }.collect{ |li| li.variant.downloadable ? nil : li.variant }.reject{ |v| v.nil? }      
  ShippingPackage.where(:site_id => self.invoice.site_id).reorder(:flat_rate_price).all.each do |sp|
    if sp.fits(variants)
      self.shipping_package_id = sp.id
      self.save
      return true          
    end 
  end                    
  Caboose.log("Error: line item #{li.id} (#{li.variant.product.title}) does not fit into any package.")
  return false
end

#check_nil_fieldsObject



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

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

#fits(line_item = nil) ⇒ Object



50
51
52
53
54
# File 'app/models/caboose/invoice_package.rb', line 50

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