Module: Auth::Concerns::Shopping::CartConcern

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

Overview

NEED A SEPERATE MODEL THAT IMPLEMENTS IT

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_cart(cart_id, resource) ⇒ Object

used in initialize vars in cart_controlller_concern. override this method to allow an admin to also interact with all the actions of the controller.



380
381
382
383
384
385
386
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 380

def self.find_cart(cart_id,resource)
	if cart_collection = self.where(:id => cart_id, :resource_id => resource_id)
		cart_collection.size > 0 ? cart_collection.first : nil
	else
		nil
	end
end

Instance Method Details

#add_or_remove(item_ids, add_or_remove) ⇒ Object

adds a validation error if the cart items could not be successfully added or removed. called from the controller. TODO: you should change this to be called before_validation instead, so that all code remains in the model.



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 336

def add_or_remove(item_ids,add_or_remove)
    item_ids.map {|id|

      begin
      	  #puts "the signed in resource is:"
      	  #puts self.signed_in_resource
      	  
	      cart_item = Auth.configuration.cart_item_class.constantize.find_self(id,self.signed_in_resource)
	      
	      #puts "found cart item: #{cart_item.id.to_s}"

	      cart_item.signed_in_resource = self.signed_in_resource
	      	
	      #puts "Add or remove is: #{add_or_remove}"

	      ## and personality also has to be set here.
	      resp = (add_or_remove == 1) ? cart_item.set_cart_and_resource(self) : cart_item.unset_cart
	      
	      ## add these to the autocomplete tags
	      if resp == true
	      	#puts "the response of adding was true."
	      	if cart_item.parent_id
	      		self.tags << cart_item.name unless self.tags.include? cart_item.name
	      	else
	      		self.tags.delete(cart_item.name)
	      	end
	      else
	      	#puts "Response of adding was false."
	      end
	      
	      resp
  	  rescue Mongoid::Errors::DocumentNotFound => error
  	  	#puts "--------------------------------------------DIDNT FIND THE CART ITEM"
  	  	#puts error.to_s
  	  	#puts "--------------------------------------------"
  	  	true
  	  end
    }
    #self.errors.add(:cart_items,"some cart items could not be added or removed successfully") if ((add_remove_results.size > 1) || (add_remove_results[0] == false))  
   
end

#add_or_remove_validationObject



328
329
330
331
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 328

def add_or_remove_validation
	add_or_remove(add_cart_item_ids,1) if add_cart_item_ids
	add_or_remove(remove_cart_item_ids,-1) if remove_cart_item_ids
end

#as_json(options = {}) ⇒ Object



324
325
326
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 324

def as_json(options={})
	super.merge({:cart_items => self.cart_items, :cart_payments => self.cart_payments, :cart_price => self.cart_price, :cart_paid_amount => self.cart_paid_amount, :cart_pending_balance => self.cart_pending_balance, :cart_credit => self.cart_credit, :cart_minimum_payable_amount => self.cart_minimum_payable_amount})
end

#can_accept_item?(cart_item) ⇒ Boolean

STATE MACHINE BEGINS.

@param cart_item : the cart_item that needs to be added to this cart @return true/false : depending on whether

Returns:

  • (Boolean)


456
457
458
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 456

def can_accept_item?(cart_item)
	return true
end

#can_create_discount_coupons?Boolean

@return true/false : override to decide how the cart decides if it can create discount coupons for its contents or not. the current implementation returns true if all items have been fully paid for.

Returns:

  • (Boolean)


390
391
392
393
394
395
396
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 390

def can_create_discount_coupons?
	#return (self.cart_items.select{|c| c.accepted.nil? || c.accepted == false}.size == 0)
	#puts "CAME TO CAN CREATE DISCOUNT COUPONS."
	prepare_cart
	self.cart_pending_balance == 0		

end

#can_process_item?(cart_item) ⇒ Boolean

@param cart_item : each cart item calls this method when : a) in cart_controller, after_create, b) in cart_controller after_update here it will check if the payment_stage has been satisfied or not, and then will decide to process the item further.

Returns:

  • (Boolean)


462
463
464
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 462

def can_process_item?(cart_item)
	return true
end

#debit(amount) ⇒ Object

debits the @amount from the cart credit. returns the current credit.



268
269
270
271
272
273
274
275
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 268

def debit(amount)
	#puts "want to debit: #{amount}"
	#puts "current credit is: #{self.cart_credit}"
	self.cart_credit-=amount
	
	#puts "after debiting: #{self.cart_credit}"
	self.cart_credit
end

#find_cart_itemsObject

set the cart items, [Array] of cart items.



144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 144

def find_cart_items
	#puts "find cart items."
	#puts "get resource is: #{get_resource.id.to_s}"
	#Auth.configuration.cart_item_class.constantize.all.each do |citem|
	#	puts "this is the cart item."
	#	puts citem.parent_id.to_s
	#end
	conditions = {:resource_id => get_resource.id.to_s, :parent_id => self.id.to_s}
	self.cart_items = Auth.configuration.cart_item_class.constantize.where(conditions).order(:created_at => 'asc')
	
	self.cart_items
end

#fully_paidObject

fully paid.



285
286
287
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 285

def fully_paid
	get_cart_pending_balance == 0
end

#get_cart_creditObject

initially is the same as cart_paid_amount, by calling debit, we can debit from credit, the costs of various items.



262
263
264
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 262

def get_cart_credit
	self.cart_credit 
end

#get_cart_itemsObject



157
158
159
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 157

def get_cart_items
	self.cart_items 
end

#get_cart_paid_amountObject

sum total of all payments made to the cart.



208
209
210
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 208

def get_cart_paid_amount
	self.cart_paid_amount 
end

#get_cart_paymentsObject



188
189
190
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 188

def get_cart_payments
	self.cart_payments 
end

#get_cart_pending_balanceObject



217
218
219
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 217

def get_cart_pending_balance
	self.cart_pending_balance 
end

#get_cart_priceObject



170
171
172
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 170

def get_cart_price
	self.cart_price 
end

#has_items?Boolean

checks if there are any items in the cart. expected that prepare_cart has been called in advance. return true if there is one or more items in the cart @used_in: payment_concern validation cart_not_empty

Returns:

  • (Boolean)


319
320
321
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 319

def has_items?
	get_cart_items.size > 0
end

#has_pendingObject

not fully paid, there is some amount to be taken from the customer yet.



280
281
282
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 280

def has_pending
	get_cart_pending_balance > 0
end

#not_paid_at_allObject

not paid a penny.



290
291
292
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 290

def not_paid_at_all
	get_cart_pending_balance == get_cart_price
end

#prepare_cartObject

sets all the attribute accessors of the cart. @param : a payment object can be passed in. this is used in case there is a new payment which is calling prepare_cart. in that case the new payment has to be also added to the cart_payments. this is alwasy the case when a new payment is made with a status directly set as accepted, i.e for eg a cashier makes a payment on behalf of the customer.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 119

def prepare_cart
		
	#puts "--------------- CALLING PREPARE CART ----------------"

	find_cart_items
	
	set_cart_price
	
	set_cart_payments
	
	set_cart_paid_amount
	
	set_cart_pending_balance

	set_cart_minimum_payable_amount

	set_discount

	set_personality

end

#prepare_receiptObject

just replaces the cart_items with an array of cart_item_ids replaces the cart_payments with an array of cart_payment_ids then returns a hash with these two instead of the original values.



307
308
309
310
311
312
313
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 307

def prepare_receipt
	#puts "PREPARE RECEIPT-----------------------------------"
	cart_item_ids = cart_items.map{|c| c = c.id.to_s}
	cart_payment_ids = cart_payments.map{|c| c = c.id.to_s}
	receipt = self.attributes.merge({:cart_items => cart_item_ids, :cart_payments => cart_payment_ids})
	receipt
end

#process_itemsObject



466
467
468
469
470
471
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 466

def process_items
	self.prepare_cart unless self.cart_items
	self.cart_items.each do |item|
		item.process if can_process_item?(item)
	end
end

#refund_amountObject

returns the amount that needs to be refunded to the customer from this cart.

Returns:

  • : 0 if the pending_balance is greater than 0,otherwise returns the pending balance.



298
299
300
301
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 298

def refund_amount
	return 0 unless get_cart_pending_balance < 0
	return get_cart_pending_balance
end

#set_autocomplete_descriptionObject



438
439
440
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 438

def set_autocomplete_description

end

#set_autocomplete_tagsObject

so as long as before save is called, this problem is sorted out. let me check that first.



429
430
431
432
433
434
435
436
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 429

def set_autocomplete_tags
	self.tags = [] 
	self.tags << "Cart"
	if self.personality_id
		self.personality = Auth.configuration.personality_class.constantize.find(self.personality_id) unless self.personality
		self.personality.add_info(self.tags)
	end
end

#set_cart_minimum_payable_amountObject

takes all the cart items that are not accepted, and sums their “minimum_price_required_to_accept_cart_item” methods.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 223

def set_cart_minimum_payable_amount

	pending_or_unaccepted_cart_items = self.cart_items.select{|c| c.accepted.nil? || c.accepted == false}

	accepted_cart_items = self.cart_items.select{|c| c.accepted == true}

	if pending_or_unaccepted_cart_items.size > 0

		self.cart_minimum_payable_amount = pending_or_unaccepted_cart_items.map{|c| c = c.minimum_price_required_to_accept_cart_item}.inject(:+)

		## it can happen that he has payed somewhere halfway between the required amount and the full price of the cart -> see the tests.
		self.cart_minimum_payable_amount = self.cart_pending_balance if (self.cart_pending_balance < self.cart_minimum_payable_amount)
	
	elsif accepted_cart_items.size > 0

		self.cart_minimum_payable_amount = 0
	
	else

	end


	## we can add more stuff here like the credit score of the user.
	## so that is a function of the cart_owner.
	## so everytime a cart is fully paid, it can add to the credit score of the user, and that can further titrate this..
	## so we have discount and all this.
end

#set_cart_paid_amountObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 192

def set_cart_paid_amount
	#puts "set cart paid amount"
	total_paid = 0
	payments = get_cart_payments
	price = get_cart_price
	payments.each do |payment|

		total_paid += payment.amount if (payment.payment_success)
	end
	self.cart_paid_amount = total_paid
	
	self.cart_credit = self.cart_paid_amount
	self.cart_paid_amount
end

#set_cart_paymentsObject



175
176
177
178
179
180
181
182
183
184
185
186
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 175

def set_cart_payments
	#puts "set cart payments."
	self.cart_payments = []
	payments_made_to_this_cart = Auth.configuration.payment_class.constantize.find_payments(get_resource,self)
	
	payments_made_to_this_cart.each do |payment|
		
		self.cart_payments << payment
	end
	
	self.cart_payments	 
end

#set_cart_pending_balanceObject



213
214
215
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 213

def set_cart_pending_balance
	self.cart_pending_balance = get_cart_price - get_cart_paid_amount
end

#set_cart_priceObject

>



162
163
164
165
166
167
168
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 162

def set_cart_price
	## so basically somewhere a cart_item is being directly created
	## without using the controller
	## that's the issue.
	self.cart_price = total_value_of_all_items_in_cart = get_cart_items.map{|c| c = c.price*c.quantity}.sum
	self.cart_price
end

#set_discountObject

will do first check if the discount id exists, and then if it requires



252
253
254
255
256
257
258
259
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 252

def set_discount
	begin
		self.discount = Auth.configuration.discount_class.constantize.find(discount_id)
		
	rescue => e
		
	end
end

#set_personalityObject



398
399
400
401
402
403
404
405
406
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 398

def set_personality
	#puts "personality id is : #{self.personality_id}"
	return if self.personality_id.blank?
	#puts "self personality id is: #{self.personality_id}"
	self.personality = Auth.configuration.personality_class.constantize.find(self.personality_id)
	#puts "self personality is: "
	#puts self.personality.to_s
	
end

#set_placeObject



408
409
410
411
412
# File 'app/models/auth/concerns/shopping/cart_concern.rb', line 408

def set_place
	return if self.place_id.blank?
	#puts "Self place id is: #{self.place_id}"
	self.place = Auth.configuration.place_class.constantize.find(self.place_id)
end

METHODS FOR AUTOCOMPLETE CONCERNS.



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

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.cart_class),self.id.to_s)
	end
end


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

def set_secondary_links

end