Class: Gemgento::Quote
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Gemgento::Quote
- Defined in:
- app/models/gemgento/quote.rb
Overview
Instance Attribute Summary collapse
-
#destroy_after_rollback ⇒ Object
Returns the value of attribute destroy_after_rollback.
-
#order_increment_id ⇒ Object
Returns the value of attribute order_increment_id.
-
#push_addresses ⇒ Object
Returns the value of attribute push_addresses.
-
#push_customer ⇒ Object
Returns the value of attribute push_customer.
-
#push_payment_method ⇒ Object
Returns the value of attribute push_payment_method.
-
#push_shipping_method ⇒ Object
Returns the value of attribute push_shipping_method.
-
#same_as_billing ⇒ Object
Returns the value of attribute same_as_billing.
-
#same_as_shipping ⇒ Object
Returns the value of attribute same_as_shipping.
-
#subscribe ⇒ Object
Returns the value of attribute subscribe.
Class Method Summary collapse
-
.current(store, quote_id = nil, user = nil) ⇒ Gemgento:Quote
Get the current quote given a quote_id, Store, and User.
Instance Method Summary collapse
- #after_convert_fail ⇒ Object
- #after_convert_success ⇒ Object
-
#apply_coupon(code) ⇒ Boolean
Apply a coupon code to the quote.
-
#apply_gift_card(code) ⇒ Boolean
Apply a gift card to quote.
- #as_json(options = nil) ⇒ Object
- #before_convert ⇒ Object
-
#convert(remote_ip = nil) ⇒ Boolean
Convert a Quote to an Order.
- #equalize_qty_ordered(line_item) ⇒ Object
-
#get_shipping_amount(selected_method, shipping_methods = nil) ⇒ BigDecimal
Get the shipping method price.
-
#get_totals ⇒ Array(Hash)?
Fetch quote totals from Magento.
-
#item_quantity ⇒ BigDecimal
Total quantity of line items.
- #items_in_stock? ⇒ Boolean
-
#mark_converted! ⇒ Void
Mark a quote as converted.
-
#payment_methods ⇒ Array(Hash)?
Get payment methods from Magento.
-
#push_gift_message_comment ⇒ Void
Push the gift_message to associated Magento Order as a comment.
-
#remove_coupons ⇒ Boolean
Remove a coupon code from the quote.
-
#remove_gift_card(code) ⇒ Boolean
Remove a gift card from quote.
-
#reset ⇒ Void
Reset quote data to before checkout began.
-
#reset_totals ⇒ Hash
Recalculate quote totals.
-
#set_magento_addresses ⇒ Boolean
Push Quote shipping and billing addresses to Magento.
-
#set_magento_customer ⇒ Boolean
Set quote customer in Magento.
- #set_magento_payment_method ⇒ Object
-
#set_magento_shipping_method ⇒ Boolean
Set Quote shipping method in Magento.
-
#shipping_method_required? ⇒ Boolean
Determine if a shipping method is required for the Quote.
-
#shipping_methods ⇒ Array(Hash)
Fetch available shipping methods from Magento.
-
#subtotal ⇒ BigDecimal
Calculate the subtotal of the Quote.
-
#totals ⇒ Hash
Quote totals.
Instance Attribute Details
#destroy_after_rollback ⇒ Object
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_id ⇒ Object
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_addresses ⇒ Object
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_customer ⇒ Object
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_method ⇒ Object
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_method ⇒ Object
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_billing ⇒ Object
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_shipping ⇒ Object
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 |
#subscribe ⇒ Object
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.
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_fail ⇒ Object
342 343 344 |
# File 'app/models/gemgento/quote.rb', line 342 def after_convert_fail end |
#after_convert_success ⇒ Object
338 339 340 |
# File 'app/models/gemgento/quote.rb', line 338 def after_convert_success unless self..blank? end |
#apply_coupon(code) ⇒ Boolean
Apply a coupon code to the quote.
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.
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( = 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_convert ⇒ Object
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.
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.
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_totals ⇒ Array(Hash)?
Fetch quote totals from Magento.
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_quantity ⇒ BigDecimal
Total quantity of line items.
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
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.
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_methods ⇒ Array(Hash)?
Get payment methods from Magento.
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_comment ⇒ Void
Push the gift_message to associated Magento Order as a comment.
392 393 394 395 |
# File 'app/models/gemgento/quote.rb', line 392 def API::SOAP::Sales::Order.add_comment(self.order.increment_id, self.order.status, "Gemgento Gift Message: #{self.}") order.update(gift_message: self.) end |
#remove_coupons ⇒ Boolean
Remove a coupon code from the quote.
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.
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 |
#reset ⇒ Void
Reset quote data to before checkout began.
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_totals ⇒ Hash
Recalculate quote totals.
105 106 107 |
# File 'app/models/gemgento/quote.rb', line 105 def reset_totals @totals = set_totals end |
#set_magento_addresses ⇒ Boolean
Push Quote shipping and billing addresses to Magento.
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_customer ⇒ Boolean
Set quote customer in Magento.
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_method ⇒ Object
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_method ⇒ Boolean
Set Quote shipping method in Magento.
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.
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_methods ⇒ Array(Hash)
Fetch available shipping methods from Magento.
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 |
#subtotal ⇒ BigDecimal
Calculate the subtotal of the Quote.
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 |
#totals ⇒ Hash
Quote totals.
84 85 86 |
# File 'app/models/gemgento/quote.rb', line 84 def totals @totals ||= set_totals end |