Class: Gemgento::Quote

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

Overview

Author:

  • Gemgento LLC

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#destroy_after_rollbackObject

Returns the value of attribute destroy_after_rollback.



21
22
23
# File 'app/models/gemgento/quote.rb', line 21

def destroy_after_rollback
  @destroy_after_rollback
end

#order_increment_idObject

Returns the value of attribute order_increment_id.



21
22
23
# File 'app/models/gemgento/quote.rb', line 21

def order_increment_id
  @order_increment_id
end

#push_addressesObject

Returns the value of attribute push_addresses.



21
22
23
# File 'app/models/gemgento/quote.rb', line 21

def push_addresses
  @push_addresses
end

#push_customerObject

Returns the value of attribute push_customer.



21
22
23
# File 'app/models/gemgento/quote.rb', line 21

def push_customer
  @push_customer
end

#push_payment_methodObject

Returns the value of attribute push_payment_method.



21
22
23
# File 'app/models/gemgento/quote.rb', line 21

def push_payment_method
  @push_payment_method
end

#push_shipping_methodObject

Returns the value of attribute push_shipping_method.



21
22
23
# File 'app/models/gemgento/quote.rb', line 21

def push_shipping_method
  @push_shipping_method
end

#same_as_billingObject

Returns the value of attribute same_as_billing.



21
22
23
# File 'app/models/gemgento/quote.rb', line 21

def same_as_billing
  @same_as_billing
end

#same_as_shippingObject

Returns the value of attribute same_as_shipping.



21
22
23
# File 'app/models/gemgento/quote.rb', line 21

def same_as_shipping
  @same_as_shipping
end

#subscribeObject

Returns the value of attribute subscribe.



21
22
23
# File 'app/models/gemgento/quote.rb', line 21

def subscribe
  @subscribe
end

Class Method Details

.current(store, quote_id = nil, user = nil) ⇒ Gemgento:Quote

Get the current quote given a quote_id, Store, and User.

Parameters:

Returns:

  • (Gemgento:Quote)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/gemgento/quote.rb', line 51

def self.current(store, quote_id = nil, user = nil)

  if !quote_id.blank? && user.nil? # quote_id but no current_user
    quote = Quote.where('created_at >= ?', Date.today - 30.days).
        find_by(id: quote_id, store: store, converted_at: nil)
    quote = Quote.new(store: store) if quote.nil?

  elsif !quote_id.blank? && !user.nil? # quote_id and current_user
    quote = Quote.where('created_at >= ?', Date.today - 30.days).
        find_by(id: quote_id, store: store, converted_at: nil)

    if quote.nil? || (!quote.user.nil? && quote.user != user) # when quote does not belong to user
      quote = Quote.where('created_at >= ?', Date.today - 30.days).
          find_by(id: quote_id, store: store, user: user, converted_at: nil)
      quote = Quote.new(store: store) if quote.nil?
    end

  elsif quote_id.blank? && !user.nil? # no quote id and a current_user
    quote = Quote.where('created_at >= ?', Date.today - 30.days).
        where(store: store, user: user, converted_at: nil).
        order(updated_at: :desc).first_or_initialize
    quote.reset unless quote.magento_id.nil?

  else
    quote = Quote.new(store: store)
  end

  return quote
end

Instance Method Details

#after_convert_failObject



342
343
344
# File 'app/models/gemgento/quote.rb', line 342

def after_convert_fail

end

#after_convert_successObject



338
339
340
# File 'app/models/gemgento/quote.rb', line 338

def after_convert_success
  push_gift_message_comment unless self.gift_message.blank?
end

#apply_coupon(code) ⇒ Boolean

Apply a coupon code to the quote.

Parameters:

  • code (String)

    coupon code

Returns:

  • (Boolean)


147
148
149
150
151
152
153
154
155
156
157
158
# File 'app/models/gemgento/quote.rb', line 147

def apply_coupon(code)
  response = API::SOAP::Checkout::Coupon.add(self, code)

  if response.success?
    self.coupon_codes << code unless self.coupon_codes.include? code
    save
    return true
  else
    handle_magento_response(response)
    return false
  end
end

#apply_gift_card(code) ⇒ Boolean

Apply a gift card to quote.

Parameters:

  • code (String)

    gift card code

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/models/gemgento/quote.rb', line 113

def apply_gift_card(code)
  response = API::SOAP::GiftCard.quote_add(self.magento_id, code, self.store.magento_id)

  if response.success?
    self.gift_card_codes << code unless self.gift_card_codes.include? code
    save
    return true
  else
    handle_magento_response(response)
    return false
  end
end

#as_json(options = nil) ⇒ Object



346
347
348
349
350
351
352
353
354
355
# File 'app/models/gemgento/quote.rb', line 346

def as_json(options = nil)
  result = super
  result['user'] = self.user
  result['line_items'] = self.line_items
  result['shipping_address'] = self.shipping_address
  result['billing_address'] = self.billing_address
  result['payment'] = self.payment
  result['totals'] = self.totals
  return result
end

#before_convertObject



334
335
336
# File 'app/models/gemgento/quote.rb', line 334

def before_convert

end

#convert(remote_ip = nil) ⇒ Boolean

Convert a Quote to an Order.

Returns:

  • (Boolean)


286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'app/models/gemgento/quote.rb', line 286

def convert(remote_ip = nil)
  return false unless items_in_stock?

  before_convert
  response = API::SOAP::Checkout::Cart.order(self, self.payment, remote_ip)

  if response.success?
    self.order_increment_id = response.body[:result]
    self.mark_converted!
    return true

  else
    handle_magento_response(response)
    after_convert_fail
    return false
  end

end

#equalize_qty_ordered(line_item) ⇒ Object



403
404
405
406
407
408
409
410
411
412
413
# File 'app/models/gemgento/quote.rb', line 403

def equalize_qty_ordered(line_item)
  inventory = line_item.product.inventories.find_by(store: self.store)

  if inventory.quantity > 0
    line_item.update(qty_ordered: inventory.quantity)
    errors.add(:base, "available quantity of #{line_item.product.attribute_value('name', self.store)} has been reduced to reflect inventory updates")
  else
    line_item.destroy
    errors.add(:base, "#{line_item.product.attribute_value('name', self.store)} is no longer available and has been removed from your cart")
  end
end

#get_shipping_amount(selected_method, shipping_methods = nil) ⇒ BigDecimal

Get the shipping method price.

Parameters:

  • selected_method (String)
  • shipping_methods (Array(Hash), nil) (defaults to: nil)

Returns:

  • (BigDecimal)


229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'app/models/gemgento/quote.rb', line 229

def get_shipping_amount(selected_method, shipping_methods = nil)
  shipping_methods = self.shipping_methods if shipping_methods.blank?

  shipping_methods.each do |shipping_method|
    if shipping_method[:code] == selected_method
      return shipping_method[:price].to_d
    end
  end

  shipping_methods.uniq! { |sm| sm[:code] }

  return 0.0
end

#get_totalsArray(Hash)?

Fetch quote totals from Magento.

Returns:

  • (Array(Hash), nil)


91
92
93
94
95
96
97
98
99
100
# File 'app/models/gemgento/quote.rb', line 91

def get_totals
  response = API::SOAP::Checkout::Cart.totals(self)

  if response.success?
    return response.body[:result][:item]
  else
    handle_magento_response(response)
    return nil
  end
end

#item_quantityBigDecimal

Total quantity of line items.

Returns:

  • (BigDecimal)


371
372
373
# File 'app/models/gemgento/quote.rb', line 371

def item_quantity
  line_items.sum(:qty_ordered).to_d
end

#items_in_stock?Boolean

Returns:

  • (Boolean)


397
398
399
400
401
# File 'app/models/gemgento/quote.rb', line 397

def items_in_stock?
  self.line_items.each do |line_item|
    self.equalize_qty_ordered(line_item) unless line_item.product.in_stock?(line_item.qty_ordered, self.store)
  end
end

#mark_converted!Void

Mark a quote as converted. Ensures the Order data is fetched from Magento and calls the after_convert_success method.

Returns:

  • (Void)


309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'app/models/gemgento/quote.rb', line 309

def mark_converted!
  begin
    Gemgento::Magento::OrderAdapter.find(self.order_increment_id).import

    # attempt to rescue once due to race condition of Magento pushing at the same time
    # after one retry, just move past, the quote has been converted and
    # we can't throw an error at this point in time
  rescue Exception
    if (retries ||= 0) <= 1
      retries += 1
      retry
    end
  end

  self.converted_at = Time.now
  self.save
  self.reload

  if self.user && Config[:extensions]['authorize-net-cim-payment-module']
    Gemgento::API::SOAP::Authnetcim::Payment.fetch(self.user)
  end

  after_convert_success
end

#payment_methodsArray(Hash)?

Get payment methods from Magento.

Returns:

  • (Array(Hash), nil)


260
261
262
263
264
265
266
267
268
269
270
# File 'app/models/gemgento/quote.rb', line 260

def payment_methods
  response = API::SOAP::Checkout::Payment.list(self)
  response.body[:result]

  if response.success?
    return response.body[:result]
  else
    handle_magento_response(response)
    return nil
  end
end

#push_gift_message_commentVoid

Push the gift_message to associated Magento Order as a comment.

Returns:

  • (Void)


392
393
394
395
# File 'app/models/gemgento/quote.rb', line 392

def push_gift_message_comment
  API::SOAP::Sales::Order.add_comment(self.order.increment_id, self.order.status, "Gemgento Gift Message: #{self.gift_message}")
  order.update(gift_message: self.gift_message)
end

#remove_couponsBoolean

Remove a coupon code from the quote.

Returns:

  • (Boolean)


163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/models/gemgento/quote.rb', line 163

def remove_coupons
  response = API::SOAP::Checkout::Coupon.remove(self)

  if response.success?
    self.coupon_codes = []
    self.save
    return true
  else
    handle_magento_response(response)
    return false
  end
end

#remove_gift_card(code) ⇒ Boolean

Remove a gift card from quote.

Parameters:

  • code (String)

    gift card code

Returns:

  • (Boolean)

    true if the gift card was removed, otherwise an error message.



130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/models/gemgento/quote.rb', line 130

def remove_gift_card(code)
  response = API::SOAP::GiftCard.quote_remove(self.magento_id, code, self.store.magento_id)

  if response.success?
    self.gift_card_codes.delete code
    save
    return true
  else
    handle_magento_response(response)
    return false
  end
end

#resetVoid

Reset quote data to before checkout began.

Returns:

  • (Void)


378
379
380
381
382
383
384
385
386
387
# File 'app/models/gemgento/quote.rb', line 378

def reset
  self.user_id = nil
  self.customer_email = nil
  self.billing_address.destroy unless self.billing_address.nil?
  self.shipping_address.destroy unless self.shipping_address.nil?
  self.shipping_method = nil
  self.shipping_amount = nil
  self.payment.destroy unless self.payment.nil?
  self.save
end

#reset_totalsHash

Recalculate quote totals.

Returns:

  • (Hash)


105
106
107
# File 'app/models/gemgento/quote.rb', line 105

def reset_totals
  @totals = set_totals
end

#set_magento_addressesBoolean

Push Quote shipping and billing addresses to Magento.

Returns:

  • (Boolean)


193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'app/models/gemgento/quote.rb', line 193

def set_magento_addresses
  # re-set magento customer if guest so that customer name can be pulled from addresses.
  return false if self.customer_is_guest && !self.set_magento_customer

  response = API::SOAP::Checkout::Customer.address(self)

  if response.success?
    return true
  else
    handle_magento_response(response)
    return false
  end
end

#set_magento_customerBoolean

Set quote customer in Magento.

Returns:

  • (Boolean)


179
180
181
182
183
184
185
186
187
188
# File 'app/models/gemgento/quote.rb', line 179

def set_magento_customer
  response = API::SOAP::Checkout::Customer.set(self)

  if response.success?
    return true
  else
    handle_magento_response(response)
    return false
  end
end

#set_magento_payment_methodObject



272
273
274
275
276
277
278
279
280
281
# File 'app/models/gemgento/quote.rb', line 272

def set_magento_payment_method
  response = API::SOAP::Checkout::Payment.method(self, self.payment)

  if response.success?
    return true
  else
    handle_magento_response(response)
    return false
  end
end

#set_magento_shipping_methodBoolean

Set Quote shipping method in Magento.

Returns:

  • (Boolean)


246
247
248
249
250
251
252
253
254
255
# File 'app/models/gemgento/quote.rb', line 246

def set_magento_shipping_method
  response = API::SOAP::Checkout::Shipping.method(self, self.shipping_method)

  if response.success?
    return true
  else
    handle_magento_response(response)
    return false
  end
end

#shipping_method_required?Boolean

Determine if a shipping method is required for the Quote.

Returns:

  • (Boolean)


418
419
420
421
422
423
424
# File 'app/models/gemgento/quote.rb', line 418

def shipping_method_required?
  self.line_items.collect do |li|
    return true if li.product.magento_type != 'giftvoucher'
  end

  return false
end

#shipping_methodsArray(Hash)

Fetch available shipping methods from Magento.

Returns:

  • (Array(Hash))


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

def shipping_methods
  response =  API::SOAP::Checkout::Shipping.list(self)

  if response.success?
    return [] if response.body[:result][:item].nil?
    response.body[:result][:item] = [response.body[:result][:item]] unless response.body[:result][:item].is_a? Array

    return response.body[:result][:item]
  else
    handle_magento_response(response)
    return []
  end
end

#subtotalBigDecimal

Calculate the subtotal of the Quote.

Returns:

  • (BigDecimal)


360
361
362
363
364
365
366
# File 'app/models/gemgento/quote.rb', line 360

def subtotal
  if self.line_items.any?
    return self.line_items.map{ |li| li.price * li.qty_ordered.to_d }.inject(&:+)
  else
    return 0
  end
end

#totalsHash

Quote totals.

Returns:

  • (Hash)


84
85
86
# File 'app/models/gemgento/quote.rb', line 84

def totals
  @totals ||= set_totals
end