/**
  * it does not adds to cart rather it sets the value in cart after making quantity value zero. 
  * 
  * returns number of items to increased or decreased in cart and success or failure
  * cart cookie data structure
  * array(
  * "tiffin_id_1"=>array( quantity, added date),
  * "tiffin_id_2"=>array( quantity, added date),
  * 		)
  */
 public function actionAddToCart($id, $quantity, $doecho = 1)
 {
     $response = array();
     $response["message"] = "not ok";
     $response["change"] = 0;
     if ((Yii::app()->getRequest()->getIsAjaxRequest() and $_GET['ajax'] == 1) || $doecho == 0 and $quantity > 0) {
         $currDateTime = new DateTime();
         $currDateTime = $currDateTime->format('Y-m-d H:i:s');
         if (Yii::app()->user->isGuest) {
             $response["message"] = "ok";
             $allCookies = Yii::app()->request->getCookies();
             $cartCookiesArr = array();
             if (isset($allCookies[Yii::app()->params['cartCookieName']])) {
                 $cartCookiesArr = CJSON::decode($allCookies[Yii::app()->params['cartCookieName']]->value, true);
             }
             //check if tiffin with id exists
             if (isset($cartCookiesArr[$id])) {
                 $response["change"] = $quantity - $cartCookiesArr[$id][0];
                 $cartCookiesArr[$id][0] = $quantity;
             } else {
                 $cartCookiesArr[$id][] = $quantity;
                 $cartCookiesArr[$id][] = $currDateTime;
                 $response["change"] = $quantity;
             }
             //update the cookie
             $allCookies[Yii::app()->params['cartCookieName']] = new CHttpCookie(Yii::app()->params['cartCookieName'], CJSON::encode($cartCookiesArr), array('expire' => time() + 60 * 60 * 24 * 100, 'httpOnly' => true));
         } else {
             $response["message"] = "ok";
             $alreadyPresentRecord = ACart::model()->findAll(array('condition' => 'is_deleted = "no" AND cart2tiffin = ' . $id . ' AND cart2user = ' . AppCommon::getUserDetailsId()));
             if (isset($alreadyPresentRecord[0])) {
                 $response["change"] = $quantity - $alreadyPresentRecord[0]->quantity;
                 $alreadyPresentRecord[0]->quantity = $quantity;
                 $alreadyPresentRecord[0]->added_time = $currDateTime;
                 $alreadyPresentRecord[0]->save();
             } else {
                 try {
                     $model = new ACart();
                     $model->cart2tiffin = $id;
                     $model->cart2user = AppCommon::getUserDetailsId();
                     $model->quantity = $quantity;
                     $model->added_time = $currDateTime;
                     $model->save();
                     $response["change"] = $quantity;
                 } catch (Exception $e) {
                     $ignored = true;
                     //added to ignore exception
                     $response["message"] = "not ok";
                     $response["change"] = 0;
                 }
             }
         }
     }
     $response["total_items"] = AppCommon::cartItemCount();
     if ($doecho == 1) {
         echo CJSON::encode($response);
         return;
     } else {
         return CJSON::encode($response);
     }
 }
 /**
  * merges cookies and db cart if logged in.
  */
 public static function mergeCookieAndDbCart()
 {
     $allCookies = Yii::app()->request->getCookies();
     if (!Yii::app()->user->isGuest) {
         if (isset($allCookies[Yii::app()->params['cartCookieName']])) {
             $cartCookiesArr = array();
             $cartCookiesArr = CJSON::decode($allCookies[Yii::app()->params['cartCookieName']]->value, true);
             $alreadyPresentRecord = ACart::model()->findAll(array('condition' => 'is_deleted = "no" AND cart2user = '******'cartCookieName']]);
         }
     }
 }