Class: Caboose::InvoicePackage

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

Constant Summary collapse

STATUS_PENDING =
'Pending'
STATUS_READY_TO_SHIP =
'Ready to Ship'
STATUS_SHIPPED =
'Shipped'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.custom_invoice_packages(store_config, invoice) ⇒ Object


28
29
30
# File 'app/models/caboose/invoice_package.rb', line 28

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


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

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


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

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


24
25
26
# File 'app/models/caboose/invoice_package.rb', line 24

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

#fits(line_item = nil) ⇒ Object


52
53
54
55
56
# File 'app/models/caboose/invoice_package.rb', line 52

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