Module: Auth::Concerns::Shopping::DiscountConcern

Extended by:
ActiveSupport::Concern
Includes:
ChiefModelConcern, EsConcern, OwnerConcern
Included in:
Shopping::Discount
Defined in:
app/models/auth/concerns/shopping/discount_concern.rb

Overview

### sample discount object:

id : whatever product_ids : [] count : 123 needs_verification : true/false discount_percentage : discount_amount : origin_cart_id : whatever pending_verification : [cart_id => date_time] used_by : [cart_id => date/time]

other_fields will be added from owner_concern.

*if both percentage and amount are present, then amount will be used.

*when a payment is made => check if cart fully paid => show option to create coupons,enter discount amount/percentage,enter needs verification or not => if yes => create a discount_object with those details

*now when someone finds this discount object => give link to create a cart directly with it,(there should be an action on cart_item called bulk_create => there will first create the cart items => then redirect_to create_cart => with those cart_items, and also the discount code)

*now in prepare_cart => if dicount_code is provided:

  1. check that the code id exsits

  2. check how many have been utilized

  3. if verification is necessary then send a message to the verifier, or in case the cart is being proxied, then the verification is bypassed => when he updates the discount object as verified(for that user id)

  4. if verification is not necessary , add the user to the discount object -> as those who used this.

*now if no verification is needed or after verification incorporate the discount_amount in the calculation of the cart_price.

*show the modified cart price.

*let him make a payment, he can make with amount as 0, with cash. gateway payment is disabled if pending balance is 0.

*the payment is instantly accepted if pending_balance is zero.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#admin_and_cart_id_absentObject

returns true if:

  1. the user is an admin

  2. there is no cart id.



507
508
509
510
511
512
513
514
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 507

def admin_and_cart_id_absent
	#puts "signed in resource is:"
	#puts signed_in_resource.to_s
	#puts "is it an admin?"
	#puts signed_in_resource.is_admin?
	#puts "cart id is nil: #{cart_id.nil?}"
	signed_in_resource.is_admin? && cart_id.nil?
end

#cart_can_create_discount_couponsObject

this should only be if a cart id is provided.



476
477
478
479
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 476

def cart_can_create_discount_coupons
	set_cart unless self.cart
	self.errors.add(:cart, "you cannot create discount coupons on this cart") unless cart.can_create_discount_coupons? 
end

#cart_existsObject



471
472
473
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 471

def cart_exists
	self.errors.add(:cart,"the cart does not exist") unless self.cart
end

#cart_has_multiples_of_all_itemsObject



487
488
489
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 487

def cart_has_multiples_of_all_items
	self.errors.add(:cart,"the cart must have equal numbers of all items") if self.cart.cart_items.select{|c| c.quantity != self.count}.size > 0
end

#discount_percentage_permittedObject



492
493
494
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 492

def discount_percentage_permitted
	self.errors.add(:discount_percentage,"you cannot set a discount percentage") if self.discount_percentage > 0
end

#maximum_discount_amountObject



497
498
499
500
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 497

def maximum_discount_amount
	## cannot exceed the sum of all cart items prices, we mean one multiple of the cart item prices.
	self.errors.add(:discount_amount,"the discount amount cannot exceed:") if self.discount_amount > (self.cart.cart_items.map{|c| c = c.price}.inject(:+))
end

#one_discount_object_per_cartObject

VALIDATION METHODS.



460
461
462
463
464
465
466
467
468
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 460

def one_discount_object_per_cart
	discount_coupons_with_cart = 
	Auth.configuration.discount_class.constantize.where(:cart_id => self.cart_id.to_s)
	
	count = self.new_record? ? 0 : 1

	self.errors.add(:cart,"you can only create one discount coupon per cart") if discount_coupons_with_cart.size > count
	
end

#set_autocomplete_descriptionObject



418
419
420
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 418

def set_autocomplete_description

end

#set_autocomplete_tagsObject



411
412
413
414
415
416
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 411

def set_autocomplete_tags
	self.tags = []
	self.tags << "Discount"
	self.tags << self.discount_amount.to_s if self.discount_amount
	self.tags << self.discount_percentage.to_s if self.discount_percentage
end

#set_cartObject

CALLBACK METHODS.



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 435

def set_cart
	begin

		if self.cart_id

			self.cart = Auth.configuration.cart_class.constantize.find(self.cart_id)
			
			
			self.cart.prepare_cart

		end

	rescue => e
		puts e.to_s
	end
end

AUTOCOMPLETE METHODS.



405
406
407
408
409
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 405

def set_primary_link
	unless self.primary_link
		self.primary_link = Rails.application.routes.url_helpers.send(Auth::OmniAuth::Path.show_or_update_or_delete_path(Auth.configuration.discount_class),self.id.to_s)
	end
end


422
423
424
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 422

def set_secondary_links
	
end

#user_can_create_discount_couponsObject



482
483
484
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 482

def user_can_create_discount_coupons
	self.errors.add(:cart,"you cannot create discount coupons") unless get_resource.can_create_discount_coupons?
end