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

Extended by:
ActiveSupport::Concern
Includes:
ChiefModelConcern, 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.



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

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.



442
443
444
445
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 442

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



437
438
439
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 437

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

#cart_has_multiples_of_all_itemsObject



453
454
455
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 453

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



458
459
460
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 458

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

#maximum_discount_amountObject



463
464
465
466
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 463

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.



426
427
428
429
430
431
432
433
434
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 426

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_cartObject

CALLBACK METHODS.



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 401

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

#user_can_create_discount_couponsObject



448
449
450
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 448

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