Class: Order

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

Defined Under Namespace

Modules: Totaling

Instance Method Summary collapse

Instance Method Details

#add_variant(variant, quantity = 1) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/order.rb', line 115

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)
    current_item.variant = variant
    current_item.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)


101
102
103
# File 'app/models/order.rb', line 101

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

#allow_pay?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'app/models/order.rb', line 111

def allow_pay?
  checkout_complete
end

#allow_resume?Boolean

Returns:

  • (Boolean)


105
106
107
108
109
# File 'app/models/order.rb', line 105

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

#charge_totalObject



197
198
199
# File 'app/models/order.rb', line 197

def charge_total
  charges.reload.total
end

#checkout_completeObject



64
65
66
# File 'app/models/order.rb', line 64

def checkout_complete
  checkout.completed_at
end

#contains?(variant) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#create_tax_chargeObject



201
202
203
204
205
206
207
208
209
# File 'app/models/order.rb', line 201

def create_tax_charge
  if tax_charges.empty?
    tax_charges.create({
        :order => self,
        :description => I18n.t(:tax),
        :adjustment_source => self,
      })
  end
end

#credit_totalObject



193
194
195
# File 'app/models/order.rb', line 193

def credit_total
  credits.reload.total.abs
end

#generate_order_numberObject



142
143
144
145
146
147
148
149
# File 'app/models/order.rb', line 142

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)


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

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

#mark_shippedObject



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

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

#payment_totalObject



181
182
183
# File 'app/models/order.rb', line 181

def payment_total
  payments.reload.total
end

#restore_stateObject



95
96
97
98
99
# File 'app/models/order.rb', line 95

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

#ship_addressObject



38
# File 'app/models/order.rb', line 38

def ship_address; shipment.address; end

#ship_totalObject



185
186
187
# File 'app/models/order.rb', line 185

def ship_total
  shipping_charges.reload.total
end

#shipmentObject

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



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

def shipment
  shipments.last
end

#shipping_countriesObject

collection of available shipping countries



172
173
174
# File 'app/models/order.rb', line 172

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

#shipping_methodsObject



176
177
178
179
# File 'app/models/order.rb', line 176

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

#tax_totalObject



189
190
191
# File 'app/models/order.rb', line 189

def tax_total
  tax_charges.reload.total
end

#to_paramObject



58
59
60
61
62
# File 'app/models/order.rb', line 58

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

#update_totalsObject



211
212
213
214
215
216
217
218
219
220
221
222
# File 'app/models/order.rb', line 211

def update_totals
  self.item_total       = self.line_items.total

  # save the items which might be changed by an order update, so that
  # charges can be recalculated accurately.
  self.line_items.map(&:save)

  adjustments.reload.each(&:update_amount)
  self.adjustment_total = self.charge_total - self.credit_total

  self.total            = self.item_total   + self.adjustment_total
end

#update_totals!Object



224
225
226
227
# File 'app/models/order.rb', line 224

def update_totals!
  update_totals
  save!
end