/**
  * function to send out sms
  * @param string_type $to : is mobile number where message needs to be send
  * @param string_type $message :it is message content
  * @param string_type $dlr_url: it is used for delivering report to client
  * @param string_type $type: type in which report is delivered
  * @return output		$this->api=$apiurl;
  */
 function process_sms($to, $message, $dlr_url = "", $type = "xml", $time = '', $unicode = '')
 {
     $message = urlencode($message);
     $this->to = $to;
     $to = substr($to, -10);
     $arrayto = array("9", "8", "7");
     $to_check = substr($to, 0, 1);
     if (in_array($to_check, $arrayto)) {
         $this->to = $to;
     } else {
         //echo "invalid number";
         Yii::ankFileSave("invalid number=>" . $to);
     }
     if ($time == 'null') {
         $time = '';
     } else {
         $time = "&time={$time}";
     }
     if ($unicode == 'null') {
         $unicode = '';
     } else {
         $unicode = "&unicode={$unicode}";
     }
     $url = "{$this->api_url}/web2sms.php?workingkey={$this->apikey}&sender={$this->senderid}&to={$to}&message={$message}&type={$type}&dlr_url={$dlr_url}{$time}{$unicode}";
     return $this->execute($url);
 }
 /**
  * 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;
 }
    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate($id)
    {
        $model = $this->loadModel($id, 'is_deleted = "no" and 
		tiffin2user_details = ' . AppCommon::getUserDetailsId());
        //loading record only if owner of record id opening it
        //quantity_available = initial_quantity maintains that order has not been made on this record
        //as ordered record must not be changed( except quantity change in limits) or deleted
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        if (isset($_POST['ATiffin'])) {
            $model->attributes = $_POST['ATiffin'];
            if (isset($_POST['APriceTime'])) {
                foreach ($_POST['APriceTime'] as $value) {
                    //Yii::ankFileSave( $key );
                    Yii::ankFileSave($value["time"]);
                    Yii::ankFileSave($value["price"]);
                    Yii::ankFileSave($value["discount"]);
                }
            }
            //if($model->save())
            $this->redirect(array('view', 'id' => $model->id));
        }
        $this->render('update', array('model' => $model));
    }
 private function saveAllRecords($loginRecord, $detailsRecord, $emailRecord, $timeRecord)
 {
     $transaction = Yii::app()->db->beginTransaction();
     try {
         if ($timeRecord->save()) {
             $detailsRecord->user_details2time_details = $timeRecord->id;
             if ($detailsRecord->save()) {
                 $detailsRecord->unique_name = $detailsRecord->first_name . "-" . $detailsRecord->id;
                 if (!$detailsRecord->save()) {
                     throw new Exception('error');
                 }
                 //add in wallet 50 rupees for new registration
                 AppCommonWallet::createWalletRecord('credit', 50, 50, 'registration_bonus', null, $detailsRecord->id, date('Y-m-d H:i:s'), '2016-01-01 00:00:00', null, $detailsRecord->id, 'wallet');
                 //***
                 $loginRecord->user_login2user_details = $detailsRecord->id;
                 if ($loginRecord->save()) {
                     $emailRecord->link = $detailsRecord->id;
                     $emailRecord->link_table = "user_details";
                     if ($emailRecord->save()) {
                         $transaction->commit();
                         return true;
                     } else {
                         throw new Exception('error');
                     }
                 } else {
                     throw new Exception('error');
                 }
             } else {
                 throw new Exception('error');
             }
         } else {
             throw new Exception('error');
         }
     } catch (Exception $e) {
         Yii::ankFileSave($e->getMessage());
         Yii::app()->user->setFlash('registration', UserModule::t("Some problem happened. Kindly try again."));
         $transaction->rollback();
         $this->refresh();
     }
 }
 /**
  * aanan fanan me likha buy :P
  */
 public function actionBuy($id)
 {
     return;
     $currDateTime = new DateTime();
     $currDateTime = $currDateTime->format('Y-m-d H:i:s');
     $selectedTiffin = ATiffin::model()->findAll(array('select' => 't.id, t.name, t.contents', 'condition' => 't.verified_by != "not verified" AND t.is_deleted = "no" AND t.id = ' . $id, 'order' => 't.id ASC, aPriceTimes.id ASC', 'with' => array('aPriceTimes' => array('select' => 'aPriceTimes.price_after_discount, aPriceTimes.order_end_time, aPriceTimes.discount, aPriceTimes.id, 
 	    					aPriceTimes.order_delivery_time, aPriceTimes.quantity_currently_available, 
 	    					aPriceTimes.orderType', 'on' => 'aPriceTimes.order_start_time <= ' . '"' . $currDateTime . '"' . ' AND aPriceTimes.order_end_time >= ' . '"' . $currDateTime . '"', 'condition' => 'aPriceTimes.verified_by != "not verified" AND aPriceTimes.is_deleted = "no"'))));
     if (isset($selectedTiffin[0]) and isset($selectedTiffin[0]->aPriceTimes[0])) {
         $model = new BuyForm();
         if (isset($_POST['BuyForm'])) {
             $model->attributes = $_POST['BuyForm'];
             $model->name = Yii::app()->user->name;
             $model->email = AppCommon::getEmail();
             if ($model->validate()) {
                 $temp_date = DateTime::createFromFormat('Y-m-d H:i:s', $selectedTiffin[0]->aPriceTimes[0]->order_delivery_time);
                 $temp_date_sms = $temp_date;
                 if ($temp_date != FALSE) {
                     $temp_date_sms = $temp_date->format('d M Y h:i a');
                     //01 Apr 2015 01:22 am
                     $temp_date = $temp_date->format('Y-M-d l, h:i:s a');
                 }
                 Yii::ankFileSave("\n");
                 Yii::ankFileSave("order-start");
                 Yii::ankFileSave($model->name);
                 Yii::ankFileSave($model->quantity);
                 Yii::ankFileSave($model->email);
                 Yii::ankFileSave($model->phone);
                 Yii::ankFileSave($model->quantity);
                 Yii::ankFileSave($model->techpark);
                 Yii::ankFileSave($model->address);
                 Yii::ankFileSave($model->paymentMode);
                 Yii::ankFileSave($selectedTiffin[0]->name);
                 Yii::ankFileSave($selectedTiffin[0]->id);
                 Yii::ankFileSave($selectedTiffin[0]->aPriceTimes[0]->id);
                 Yii::ankFileSave($temp_date);
                 Yii::ankFileSave("order-stop");
                 Yii::ankFileSave("\n");
                 $subject_email = "Tiffin order confirmation " . $temp_date;
                 $content_user = "******" . $model->name . ",\n" . "Your order comprising " . $model->quantity . " tiffin(s) of " . $selectedTiffin[0]->name . " has been accepted." . " We will deliver the order by " . $temp_date . " ( +/- 15 min. ) to " . $model->address . ", " . $model->techpark . ".\n The order is " . $model->paymentMode . " and order's cost is " . $model->quantity . " x " . $selectedTiffin[0]->aPriceTimes[0]->price_after_discount . " = Rs. " . $model->quantity * $selectedTiffin[0]->aPriceTimes[0]->price_after_discount . ". We may call on " . $model->phone . " for asking directions, if needed." . " Kindly receive the calls to make us deliver quickly. \n Thanks and regards, \n tw.in team ";
                 //notify customer
                 AppCommon::sendEmail($model->email, $model->name, $subject_email, $content_user, array("order_notification_customer"));
                 //notify tw team
                 AppCommon::sendEmail(Yii::app()->params['adminEmail'], $model->name, $subject_email, $content_user, array("order_notification_tiffinwale.in_team"));
                 $content_sms = "Dear Customer, we have received your order and it will be delivered" . " on " . $temp_date_sms . " ( +/- 15 min. ). Do check your email for complete details." . " Thanks! tw.in";
                 /*$content_sms = "Dear Customer, we have received your order and it will be delivered".
                 		" on 01 Apr 2015 12:00 am. Do check your email for complete details.".
                 		" Thanks! tw.in";
                 		$content_sms = "Dear Customer, we have received your order from".
                 		" and it will be delivered between 12:30 PM to 2:00 PM. Thank You! tw.in";*/
                 //sms customer
                 AppCommon::sendSmsOnTime($model->phone, $content_sms, "null", true);
                 Yii::app()->user->setFlash('buy', 'Thank you for ordering. Kindly check sms on your phone, we will deliver the meal at your address by ' . $temp_date . '.');
                 $this->refresh();
             }
         }
         $this->render('buy', array('model' => $model, 'data' => $selectedTiffin[0]));
     } else {
         Yii::app()->user->setFlash('buy', 'Currently this meal is unavailable. Kindly do visit us again tomorrow, during lunch hours.');
         //$this->refresh();
         $this->render('buy');
     }
 }
 public static function sendSmsOnTime($toPhone, $message, $time = "null", $isSecure = true)
 {
     try {
         $url = null;
         if ($isSecure) {
             $url = Yii::app()->params['springedgeSecuredUrl'];
         } else {
             $url = Yii::app()->params['springedgeUrl'];
         }
         $sendsms = new sendsms($url, Yii::app()->params['springedgeApiKey'], Yii::app()->params['springedgeSenderId']);
         $xmlResponse = $sendsms->schedule_sms($toPhone, $message, "", "json", $time);
         //json argument
         // is changed to xml downstream
         Yii::ankFileSave('A springedge sms response=>' . $toPhone . '=>' . $message);
         Yii::ankFileSave($xmlResponse);
     } catch (Exception $e) {
         Yii::ankFileSave('A springedge sms error occurred: ' . ' - ' . $e->getMessage());
     }
 }