Class: Refinery::Orders::Order

Inherits:
Core::BaseModel
  • Object
show all
Includes:
AASM
Defined in:
app/models/refinery/orders/order.rb

Overview

#########################################################################

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.any_digidownloads?(user_id) ⇒ Boolean



Returns:

  • (Boolean)


291
292
293
# File 'app/models/refinery/orders/order.rb', line 291

def self.any_digidownloads?( user_id )
  where( :has_digidownloads => true, :order_customer_id => user_id ).count > 0
end

.checkout!(cart, user) ⇒ Object




192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'app/models/refinery/orders/order.rb', line 192

def self.checkout!( cart, user )
  order = Order.new
  order.user = user

    # convert cart.items to order.line_items
  cart.items.each do | item |
    order.line_items.build(
        :product    => item.product,
        :quantity   => item.quantity,
        :unit_price => item.price
    )
  end  # do

  order.save!

  return order
end

Instance Method Details

#any_digidownloads?Boolean


Returns:

  • (Boolean)


285
286
287
# File 'app/models/refinery/orders/order.rb', line 285

def any_digidownloads?()
  LineItem.has_digidownloads?( self.id )
end

#display_dateObject




298
299
300
# File 'app/models/refinery/orders/order.rb', line 298

def display_date
   ( self.shipped_on || self.cc_purchased_on || self.updated_at ).to_date
end

#get_billship_addresses(customer) ⇒ Object




217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'app/models/refinery/orders/order.rb', line 217

def get_billship_addresses( customer )

  bill_address  = self.billing_address  || 
                  customer.billing_address || 
                  ::Refinery::Addresses::Address.new( :is_billing => true  )

    # ship needs to be populated from bill if no ship already present
  ship_address  = self.shipping_address || 
                  customer.shipping_address || 
                  ::Refinery::Addresses::Address.new( 
                    :first_name  =>     bill_address.first_name ,  
                    :last_name   =>     bill_address.last_name  ,  
                    :phone       =>     bill_address.phone      ,  
                    :email       =>     bill_address.email      ,  
                    :address1    =>     bill_address.address1   ,  
                    :address2    =>     bill_address.address2   ,  
                    :city        =>     bill_address.city       ,  
                    :state       =>     bill_address.state      ,  
                    :zip         =>     bill_address.zip        ,  
                    :country     =>     bill_address.country    ,  
                    :is_billing  =>     false
                  ) 

  return bill_address, ship_address
end

#get_render_templateObject




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

def get_render_template
  action = {
    :checkout_started            => :edit,
    :edit_stage_completed        => :confirmation
  }[ self.order_status.to_sym ]

end

#handle_update(params) ⇒ Object




171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/models/refinery/orders/order.rb', line 171

def handle_update( params )
  if checkout_started?
    bill_address, ship_address = 
      ::Refinery::Addresses::Address.update_addresses( self, params )
    next_in_process!  if  bill_address.errors.empty? && ship_address.errors.empty?
  end

  self.cc_token = params[:stripeToken]
  self.cc_last4 = params[:last4]
  self.cc_card_type = params[:card_type]
  self.cc_expiry_month = params[:expiry_month]
  self.cc_expiry_year = params[:expiry_year]
  self.save!


  return bill_address, ship_address
end

#process_purchase(store_gateway) ⇒ Object




261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'app/models/refinery/orders/order.rb', line 261

def process_purchase( store_gateway )
  raise OrderProcessingException unless self.purchase_confirmed?

  result = store_gateway.charge_purchase( 
      self.cc_token, 
      self.product_total + self.shipping_charges + self.tax_charges, 
      self, 
      self.billing_address.email
  )

  unless result.nil?  # unless there were errors ...
    self.cc_purchased_on = Time.now         # date of purchase
    self.cc_confirmation_id = result.id     # confirmation code
      # next step saves above changes IFF paid is true
    self.payment_verified! if result.paid   # state becomes paid & final
  end   # unless there were errors

  return result

end

#total_itemsObject





247
248
249
# File 'app/models/refinery/orders/order.rb', line 247

def total_items
  line_items.sum { |item| item.quantity }
end

#total_priceObject



252
253
254
# File 'app/models/refinery/orders/order.rb', line 252

def total_price
  line_items.sum { |item| item.unit_price * item.quantity }
end