Module: Auth::Concerns::Shopping::CartItemConcern

Extended by:
ActiveSupport::Concern
Includes:
EsConcern, OwnerConcern, ProductConcern
Included in:
Shopping::CartItem
Defined in:
app/models/auth/concerns/shopping/cart_item_concern.rb

Overview

NEED A SEPERATE MODEL THAT IMPLEMENTS IT

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#as_indexed_json(options = {}) ⇒ Object



437
438
439
440
441
442
443
444
# File 'app/models/auth/concerns/shopping/cart_item_concern.rb', line 437

def as_indexed_json(options={})
 {
  name: name,
    price: price,
    resource_id: resource_id,
    public: public
 }
end

#assign_product_attributesObject

before creating the document assigns attributes defined in the def #product_attributes_to_assign, to the cart item.



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'app/models/auth/concerns/shopping/cart_item_concern.rb', line 409

def assign_product_attributes
  begin
    if self.product_id
      if product = Auth.configuration.product_class.constantize.find(product_id)
        product_attributes_to_assign.each do |attr|
          ## only if the present attribute is nil, then we assign it from the product.
          if self.respond_to? attr.to_sym
             if self.send("#{attr}").nil?
               self.send("#{attr}=",product.send("#{attr}"))
             end
          end
        end
      end
    end
  rescue

  end
end

#cart_has_sufficient_credit_for_item?(cart) ⇒ Boolean

debits an amount from the cart equal to (item_price*accept_order_at_percentage_of_price) the #debit function returns the current cart credit. return true or false depending on whether , after debiting there is any credit left in the cart or not.

Returns:

  • (Boolean)


315
316
317
318
319
320
# File 'app/models/auth/concerns/shopping/cart_item_concern.rb', line 315

def cart_has_sufficient_credit_for_item?(cart)
  puts "cart credit is: #{cart.cart_credit}"
  cart_has_credit = cart.debit((self.accept_order_at_percentage_of_price*self.price)) >= 0
  puts "cart has credit is: #{cart_has_credit.to_s}"
  cart_has_credit
end

#minimum_price_required_to_accept_cart_itemObject

this is got by multiplying the price of the cart item by the minimum_acceptable at field.



433
434
435
# File 'app/models/auth/concerns/shopping/cart_item_concern.rb', line 433

def minimum_price_required_to_accept_cart_item
  price*accept_order_at_percentage_of_price
end

#product_attributes_to_assignObject



428
429
430
# File 'app/models/auth/concerns/shopping/cart_item_concern.rb', line 428

def product_attributes_to_assign
  ["name","price","bunch"]
end

#product_id_exists?Boolean

Returns:

  • (Boolean)


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

def product_id_exists?
  begin
    Auth.configuration.product_class.constantize.find(self.product_id)
  rescue
    self.errors.add(:product_id,"this product id does not exist")
  end
end

#refresh_acceptedObject

this is an internal method, cannot be set by admin or anyone, it is done after validation, since it is not necessary for someone to be admin, even the user can call refresh on the record to get the new state of the acceptence. just checks if the accepted by payment id exists, and if yes, then doesnt do anything, otherwise will update the cart item status as false.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'app/models/auth/concerns/shopping/cart_item_concern.rb', line 198

def refresh_accepted
  
  if self.accepted_by_payment_id

    begin
      payment = Auth.configuration.payment_class.constantize.find(self.accepted_by_payment_id)
      ## check if this payment status is approved or not.
      ## if the payment status is approved, then dont do anything to the cart item.(we don't retro check payment to cart item.)
      ## if the payment status is not approved, then make the cart item accepted as false.
      if (payment.payment_status.nil? || payment.payment_status == 0)
        self.accepted = false
      end
    rescue Mongoid::Errors::DocumentNotFound
      
      self.accepted = false
    end
  end

  ## if it doesnt have a cart, then it cannot be accepted.
  if self.parent_id.nil?
    self.accepted = false 
    self.accepted_by_payment_id = nil
  end


  ## we should ideally do this in the payment.
  ## so that it can actually do what it usually does.
  ## we can set say refresh_payment.
  ## but it may not pay for everything.
  ## but that is the only way through.
  ## so if the payment is accepted then the cart_items_accepted will not be triggered.
  ## but if we update the first payment, then we can do it.
  ## basically take the last payment and update it, force calling the set_cart_items_accepted
  ## and suppose they are not successfully accepted, then what about validation errors ?
  ## so that will have to be skipped in that case.
end

#set_accepted(payment, override) ⇒ Object

called from payment#update_cart_items_accepted sets accepted to true or false depending on whether the cart has enough credit for the item. does not SAVE.



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'app/models/auth/concerns/shopping/cart_item_concern.rb', line 238

def set_accepted(payment,override)
  
  #puts "the existing self accepted is:"
  #puts self.accepted.to_s

  if cart_has_sufficient_credit_for_item?(payment.cart) 
    
    ## is it already accepted?
    if self.accepted
    
      return true
    else
    
      self.accepted = true
    end
  else 
    self.accepted = false
  end
  
  

  
  
  ## so that it doesnt do refresh_cart_item
  self.skip_callbacks = {:before_validation => true}
  
  ## this will first call validate 
  self.validate

  ## if the accepted is being set as false, then it should be set like that, where it was true?
  ## 
  self.accepted_by_payment_id = self.accepted ? payment.id.to_s : nil

  
  ## if it hasnt changed, dont query and update anything.
  
  return true unless self.accepted_changed?
  
  if self.errors.full_messages.empty?
    
    doc_after_update = 

    Auth.configuration.cart_item_class.constantize.
    where({
      "$and" => [
        "accepted" => {
          "$nin" => [self.accepted]
        },
        "_id" => BSON::ObjectId(self.id.to_s)
      ]
    }).
    find_one_and_update(
      {
        "$set" => {
          "accepted" => self.accepted,
          "accepted_by_payment_id" => self.accepted_by_payment_id
        }
      },
      {
        :return_document => :after
      }
    )

    puts "the doc after update is:"
    puts doc_after_update.attributes.to_s

    return false unless doc_after_update
    return false if doc_after_update.accepted != self.accepted
    return true
  else
    return false
  end
end

#set_cart_and_resource(cart) ⇒ Object

assigns a cart and resource, resource_id to the cart_item.



351
352
353
354
355
356
357
358
# File 'app/models/auth/concerns/shopping/cart_item_concern.rb', line 351

def set_cart_and_resource(cart)
  return true if self.parent_id
  return false if (owner_matches(cart) == false)
  self.parent_id = cart.id.to_s
  self.accepted = nil
  self.accepted_by_payment_id = nil
  self.save
end

#unset_cartObject

unsets the cart item , if it has not been accepted upto now. assume that a payment was made, this cart item was updated wth its id as success, but some others were not, so the payment was not saved. but this item has got accepted as true. so whether or not the payment that was made exists, we dont allow the cart to be unset. however in case the signed_in_resource is an admin -> it is allowed to unset the cart, whether the item is already accepted or not. @return : result of saving the cart item.



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'app/models/auth/concerns/shopping/cart_item_concern.rb', line 330

def unset_cart
  if self.signed_in_resource.is_admin?
    self.parent_id = nil
    self.accepted = nil
    self.accepted_by_payment_id = nil
  else
    if (self.accepted.nil? || self.accepted == false)
      self.parent_id = nil 
      self.accepted = nil
      self.accepted_by_payment_id = nil
    end
  end
  rs = self.save
  puts self.errors.full_messages
  rs
end

#user_cannot_change_anything_if_payment_acceptedObject

as long as it is not the accepted_by_payment id that has gone from nil to something, if anything else in the cart_item has changed, and the user is not an admin, and there is an accepted_by_payment id, then the error will be triggered. these conditions are applicable to the gateway payment, or any other payment also. and this has happened because the same item was pushed back in.



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'app/models/auth/concerns/shopping/cart_item_concern.rb', line 369

def user_cannot_change_anything_if_payment_accepted
  if !self.signed_in_resource.is_admin?

    ## THE USER CAN : UPDATE A PAYMENT TO MAKE THIS CART ITEM ACCEPTED / REJECTED.
    ## if the payment status is changing, let it be, because this is an internal updated.

    return if self.new_record?

    puts "accepted changed? #{self.accepted_changed?.to_s}"

    return if self.accepted_changed? 

    ## THE USER CAN REMOVE THE CART ITEM FROM A CART, AS LONG AS IT HAS NOT BEEN ACCEPTED.
    ## if the item is being removed from the cart, while it has not yet been accepted.
    return if self.parent_id_changed? && self.parent_id.nil? && (self.accepted.nil? || self.accepted == false)


    ## THE USER CAN CHANGE ANY OTHER STUFF ALSO AS LONG AS THE ACCEPTED IS NIL OR FALSE
    return if (self.accepted.nil? || self.accepted == false)


    ## THE LAST SITUATION IS WHEN THE ITEM WAS ACCEPTED, AND NOW WE ARE UPDATING IT AS FALSE, AND ALSO THE PAYMENT ID AS NIL, THIS IS ALLOWED, BECAUSE IT IS WHAT HAPPENS IF THE ACCEPTING PAYMENT IS NIL OR ITS STATUS IS NOT APPROVED.



    ## otherwise, the updated_by_payment_id, changes only when we are doing a gateway payment or any payment actually.
    self.errors.add(:quantity,"you cannot change this item since payment has already been made")
    # if self.accepted_by_payment_id && self.changed? && !self.new_record? && !(self.accepted_by_payment_id_changed? && self.accepted_by_payment_id_was.nil?)
  end
end