Class: Order

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

Instance Method Summary collapse

Instance Method Details

#add_variant(variant, quantity = 1) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/order.rb', line 90

def add_variant(variant, quantity=1)
  current_item = contains?(variant)
  if current_item
    current_item.increment_quantity unless quantity > 1
    current_item.quantity = (current_item.quantity + quantity) if quantity > 1
    current_item.save
  else
    current_item = LineItem.new(:quantity => quantity, :variant => variant, :price => variant.price)
    self.line_items << current_item
  end
  
  # populate line_items attributes for additional_fields entries
  # that have populate => [:line_item]
  Variant.additional_fields.select{|f| !f[:populate].nil? && f[:populate].include?(:line_item) }.each do |field| 
    value = ""
    
    if field[:only].nil? || field[:only].include?(:variant)
      value = variant.send(field[:name].gsub(" ", "_").downcase)
    elsif field[:only].include?(:product)
      value = variant.product.send(field[:name].gsub(" ", "_").downcase)
    end
    current_item.update_attribute(field[:name].gsub(" ", "_").downcase, value)
  end
end

#allow_cancel?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'app/models/order.rb', line 76

def allow_cancel?
  self.state != 'canceled'
end

#allow_pay?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'app/models/order.rb', line 86

def allow_pay?
  checkout_complete
end

#allow_resume?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
# File 'app/models/order.rb', line 80

def allow_resume?
  # we shouldn't allow resume for legacy orders b/c we lack the information necessary to restore to a previous state
  return false if state_events.empty? || state_events.last.previous_state.nil?
  true
end

#contains?(variant) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
# File 'app/models/order.rb', line 146

def contains?(variant)
  line_items.select { |line_item| line_item.variant == variant }.first
end

#generate_order_numberObject



115
116
117
118
119
120
121
122
# File 'app/models/order.rb', line 115

def generate_order_number                
  record = true
  while record
    random = "R#{Array.new(9){rand(9)}.join}"                                        
    record = Order.find(:first, :conditions => ["number = ?", random])
  end          
  self.number = random
end

#grant_access?(token = nil) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
153
154
# File 'app/models/order.rb', line 150

def grant_access?(token=nil)
  return true if token && token == self.token
  return false unless current_user_session = UserSession.find   
  return current_user_session.user == self.user
end

#item_totalObject

total of line items (no tax or shipping inc.)



129
130
131
132
133
134
135
# File 'app/models/order.rb', line 129

def item_total
  tot = 0
  self.line_items.each do |li|
    tot += li.total
  end
  self.item_total = tot
end

#mark_shippedObject



155
156
157
158
159
# File 'app/models/order.rb', line 155

def mark_shipped
  inventory_units.each do |inventory_unit|
    inventory_unit.ship!
  end
end

#payment_totalObject



124
125
126
# File 'app/models/order.rb', line 124

def payment_total
  payments.inject(0) {|sum, payment| sum + payment.amount}
end

#restore_stateObject



70
71
72
73
74
# File 'app/models/order.rb', line 70

def restore_state
  # pop the resume event so we can see what the event before that was
  state_events.pop if state_events.last.name == "resume"
  update_attribute("state", state_events.last.previous_state)
end

#shipmentObject

convenience method since many stores will not allow user to create multiple shipments



142
143
144
# File 'app/models/order.rb', line 142

def shipment
  shipments.last
end

#shipping_countriesObject

collection of available shipping countries



162
163
164
# File 'app/models/order.rb', line 162

def shipping_countries
  ShippingMethod.all.collect { |method| method.zone.country_list }.flatten.uniq.sort_by {|item| item.send 'name'}
end

#shipping_methodsObject



166
167
168
169
# File 'app/models/order.rb', line 166

def shipping_methods
  return [] unless ship_address
  ShippingMethod.all.select { |method| method.zone.include?(ship_address) && method.available?(self) }
end

#to_paramObject



35
36
37
38
39
# File 'app/models/order.rb', line 35

def to_param  
  self.number if self.number
  generate_order_number unless self.number
  self.number.parameterize.to_s.upcase
end

#totalObject



137
138
139
# File 'app/models/order.rb', line 137

def total
  self.total = self.item_total + self.ship_amount + self.tax_amount
end

#update_totalsObject



171
172
173
174
175
176
177
178
179
180
# File 'app/models/order.rb', line 171

def update_totals
  # finalize order totals 
  unless shipment.nil?
    calculator = shipment.shipping_method.shipping_calculator.constantize.new
    self.ship_amount = calculator.calculate_shipping(shipment) 
  else
    self.ship_amount = 0
  end
  self.tax_amount = calculate_tax
end