/**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer $id the ID of the model to be loaded
  * @return ACart the loaded model
  * @throws CHttpException
  */
 public function loadModel($id)
 {
     $model = ACart::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
 /**
  * gets cart values
  * $location to select tiffins at a particular location
  * $doeditcart edit cart if needed.
  * return
  */
 public static function getCart($location = null, $doeditcart = 1)
 {
     /*Yii::ankFileSave( var_export( AppCommon::modifyInputTiffinsQuantity( 
     		array(
     		1=>2,
     		9=>1,
     		10=>20,
     		11=>0,
     		16=>1,
     		)
     		, "Embassy Tech Village" ) ,true));
     		echo "wahh wahh";
     		return;*/
     $cartData = null;
     if (Yii::app()->user->isGuest) {
         $cartCookiesArr = array();
         $allCookies = Yii::app()->request->getCookies();
         if (isset($allCookies[Yii::app()->params['cartCookieName']])) {
             $cartCookiesArr = CJSON::decode($allCookies[Yii::app()->params['cartCookieName']]->value, true);
         }
         $cartArr = array();
         if (isset($cartCookiesArr)) {
             foreach ($cartCookiesArr as $key => $value) {
                 $cartArr[$key] = $value[0];
                 //array(tiffinid=>quantity,...)
             }
         }
         $cartData = AppCommon::modifyInputTiffinsQuantity($cartArr, $location);
         if ($cartData["has_changed"]) {
             if (isset($cartCookiesArr)) {
                 foreach ($cartCookiesArr as $key1 => $value1) {
                     if (isset($cartData[$key1]) and $cartData[$key1]["has_changed"]) {
                         if ($cartData[$key1]["error_msg"] == AppCommon::$cartErrorMsgTiffinNotExist) {
                             unset($cartCookiesArr[$key1]);
                         } else {
                             if ($cartData[$key1]["error_msg"] == AppCommon::$cartErrorMsgTiffinNotAvailable and $doeditcart == 1) {
                                 unset($cartCookiesArr[$key1]);
                             } else {
                                 if ($cartData[$key1]["error_msg"] == AppCommon::$cartErrorMsgTiffinQuanCappedToLimits and $doeditcart == 1) {
                                     $cartCookiesArr[$key1][0] = $cartData[$key1]["quantity"];
                                 }
                             }
                         }
                     }
                 }
             }
             //update the cookie
             //TODO:: check CJSON::encode( $cartCookiesArr ) is not null
             $allCookies[Yii::app()->params['cartCookieName']] = new CHttpCookie(Yii::app()->params['cartCookieName'], CJSON::encode($cartCookiesArr), array('expire' => time() + 60 * 60 * 24 * 100, 'httpOnly' => true));
         }
     } else {
         $alreadyPresentRecord = ACart::model()->findAll(array('condition' => 'is_deleted = "no" AND cart2user = ' . AppCommon::getUserDetailsId()));
         $cartArr = array();
         foreach ($alreadyPresentRecord as $rec) {
             $cartArr[$rec->cart2tiffin] = $rec->quantity;
             //array(tiffinid=>quantity,...)
         }
         $cartData = AppCommon::modifyInputTiffinsQuantity($cartArr, $location);
         try {
             if ($cartData["has_changed"]) {
                 foreach ($alreadyPresentRecord as $rec1) {
                     if (isset($cartData[$rec1->cart2tiffin]) and $cartData[$rec1->cart2tiffin]["has_changed"]) {
                         if ($cartData[$rec1->cart2tiffin]["error_msg"] == AppCommon::$cartErrorMsgTiffinNotExist) {
                             $rec1->is_deleted = AppCommon::getUserDetailsId();
                             $rec1->save();
                             //update the record
                         } else {
                             if ($cartData[$rec1->cart2tiffin]["error_msg"] == AppCommon::$cartErrorMsgTiffinNotAvailable and $doeditcart == 1) {
                                 $rec1->is_deleted = AppCommon::getUserDetailsId();
                                 $rec1->save();
                                 //update the record
                             } else {
                                 if ($cartData[$rec1->cart2tiffin]["error_msg"] == AppCommon::$cartErrorMsgTiffinQuanCappedToLimits and $doeditcart == 1) {
                                     $rec1->quantity = $cartData[$rec1->cart2tiffin]["quantity"];
                                     $rec1->save();
                                     //update the record
                                 }
                             }
                         }
                     }
                 }
             }
         } catch (Exception $e) {
             Yii::ankFileSave(" get cart exception while saving cart in db");
             //added to ignore exception
         }
     }
     $cartData["total_items"] = AppCommon::cartItemCount();
     return $cartData;
 }
 /**
  * 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']]);
         }
     }
 }