106 lines
3.7 KiB
Ruby
106 lines
3.7 KiB
Ruby
class Cart < ActiveRecord::Base
|
|
has_many :item_in_carts, -> { order "created_at" }
|
|
belongs_to :delivery_destination
|
|
|
|
def self.get_current(anonymous_id, user_id)
|
|
safe_user_id = (user_id > 0) ? user_id : nil
|
|
|
|
if user_id > 0
|
|
uCart = Cart.where(user_id: user_id).where(ordered: false).first
|
|
|
|
uCart.delivery_destination ||= DeliveryDestination.find_or_create(anonymous_id, user_id);
|
|
return uCart
|
|
else
|
|
|
|
anonymousCart = Cart.where(anonymous_id_string: anonymous_id).where(ordered: false).first
|
|
|
|
anonymousCart.delivery_destination ||= DeliveryDestination.find_or_create(anonymous_id, user_id);
|
|
return anonymousCart
|
|
end
|
|
end
|
|
|
|
def self.find_or_create(anonymous_id, user_id)
|
|
|
|
safe_user_id = (user_id > 0) ? user_id : nil
|
|
|
|
if user_id > 0
|
|
#we're logged in
|
|
# users anonymous stuff
|
|
anonymousCart = Cart.where(anonymous_id_string: anonymous_id).where(ordered: false).first
|
|
|
|
# users stuff in the db, create if needed
|
|
uCart = Cart.where(user_id: user_id).where(ordered: false).first
|
|
uCart ||= Cart.create!(user_id: safe_user_id, ordered: false )
|
|
|
|
# now we have two carts for this logged in user, since he is logged in,
|
|
# we'll merge everything
|
|
|
|
if anonymousCart
|
|
# the user has an anonymous cart we need to merge it into ucart and delete it afterwards
|
|
anonymousCart.item_in_carts.each do |item|
|
|
uCart.item_in_carts << item
|
|
end
|
|
|
|
uCart.save
|
|
anonymousCart.delete
|
|
end
|
|
|
|
uCart.delivery_destination ||= DeliveryDestination.find_or_create(anonymous_id, user_id);
|
|
return uCart
|
|
else
|
|
|
|
# we're not logged in
|
|
# get the anonymous cart, create if needed
|
|
anonymousCart = Cart.where(anonymous_id_string: anonymous_id).where(ordered: false).first
|
|
anonymousCart ||= Cart.create!(anonymous_id_string: anonymous_id, user_id: safe_user_id, ordered: false )
|
|
|
|
anonymousCart.delivery_destination ||= DeliveryDestination.find_or_create(anonymous_id, user_id);
|
|
return anonymousCart
|
|
end
|
|
# logger.debug "Cart.find_or_create"
|
|
|
|
#cart = Cart.where(anonymous_id_string: anonymous_id).where(ordered: false).first
|
|
#safe_user_id = (user_id > 0) ? user_id : nil
|
|
|
|
#if cart != nil && user_id > 0
|
|
## we have anonymous cart but also a user id
|
|
## we need to merge carts
|
|
|
|
#uCart = Cart.where(user_id: user_id).where(ordered: false).first
|
|
#uCart ||= Cart.create!(user_id: safe_user_id, ordered: false )
|
|
|
|
#uCart.delivery_destination ||= DeliveryDestination.find_or_create(anonymous_id, user_id);
|
|
#logger.debug "Cart also found with aid but now has valid user id"
|
|
|
|
#cart.item_in_carts.each do |item|
|
|
#ucart.item_in_carts << item
|
|
#end
|
|
##cart.user_id = safe_user_id
|
|
##cart.anonymous_id_string = nil
|
|
##cart.save
|
|
#uCart.save
|
|
#cart.delete
|
|
##cart.delivery_destination ||= DeliveryDestination.find_or_create(anonymous_id, user_id);
|
|
#return uCart
|
|
#end
|
|
|
|
#if cart != nil
|
|
#return cart
|
|
#end
|
|
|
|
#cart = Cart.where(user_id: user_id).where(ordered: false).first
|
|
|
|
#if cart != nil
|
|
#logger.debug "Cart found by user_id"
|
|
#cart.delivery_destination ||= DeliveryDestination.find_or_create(anonymous_id, user_id);
|
|
#return cart
|
|
#end
|
|
|
|
#logger.debug "Cart with aid will be used!"
|
|
#cart ||= Cart.create!(anonymous_id_string: anonymous_id, user_id: safe_user_id, ordered: false )
|
|
|
|
#cart.delivery_destination ||= DeliveryDestination.find_or_create(anonymous_id, user_id);
|
|
#return cart
|
|
end
|
|
end
|