public function sync(RemoteDataCache $model)
 {
     if (!$model) {
         throw new Exception("RemoteDataCache cannot be null");
     }
     /* get information from remote api server */
     $voipInfoRetriever = new BestVOIPInformationRetriever();
     $last_balance = $model->balance;
     $remoteVoipResult = $voipInfoRetriever->getInfo($model->main_user, $model->main_pass, $model->sub_user, $model->sub_pass);
     $model->balance = doubleval($remoteVoipResult->getBalance());
     $model->exact_balance = doubleval($remoteVoipResult->getSpecificBalance());
     $model->last_balance = $last_balance;
     $model->save();
     /* check notification */
     // $this->checkNotification($model);
     /* check status */
     // $this->checkStatus($model);
 }
 /**
  * @param RemoteDataCache $remoteDataCacheMdl
  * @return bool
  */
 public function activateAccount(RemoteDataCache $remoteDataCacheMdl)
 {
     $sipAccount = new SipAccount();
     $sipAccount->vicidial_identification = $remoteDataCacheMdl->vici_user;
     /**
      * @var VicidialDbDeactivator $deactivator
      */
     $activator = Yii::app()->vicidialActivator;
     $activator->setVicidialUser($sipAccount->vicidial_identification);
     if ($activator->run()) {
         $groupId = uniqid();
         ViciActionLogger::logAction(ViciLogAction::VICILOG_ACTION_SUBSIP_ACTIVIVATE, "{$remoteDataCacheMdl->sub_user} under {$remoteDataCacheMdl->main_user} is now activated", 0, $groupId, time());
         Yii::log(json_encode("{$remoteDataCacheMdl->sub_user} under {$remoteDataCacheMdl->main_user} is now activated"), CLogger::LEVEL_INFO, "activation");
     } else {
         Yii::log(json_encode("deactivation failed {$sipAccount->vicidial_identification}"), CLogger::LEVEL_INFO, "activation");
     }
     /* find the RemoteDataCache and update it too */
     $remoteDataCacheMdl->is_active = "ACTIVE";
     return $remoteDataCacheMdl->save();
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new RemoteDataCache();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['RemoteDataCache'])) {
         $model->attributes = $_POST['RemoteDataCache'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     } else {
         $model->last_balance = 0;
         $model->balance = 0;
         $model->exact_balance = 0;
         $model->last_balance_since_topup = 0;
         $model->last_credit_update = 0;
         $model->ip_address = '1.2.3.4';
     }
     $this->render('create', array('model' => $model));
 }
 public function synData()
 {
     /**
      * @var RemoteDataCache $foundModel
      */
     $fetchData = new ChartInfoDataArr();
     Yii::log("Fetching data from remote source.", CLogger::LEVEL_INFO, 'sync_log');
     $fetchedData = $fetchData->getData();
     Yii::log("Data Fetched from remote source", CLogger::LEVEL_INFO, 'sync_log');
     foreach ($fetchedData as $currentFetchedData) {
         Yii::log(sprintf("Processing data - %s - %s - %s - %s", $currentFetchedData['main_user'], $currentFetchedData['main_pass'], $currentFetchedData['sub_user'], $currentFetchedData['sub_pass']), CLogger::LEVEL_INFO, 'sync_log');
         $criteria = new CDbCriteria();
         $criteria->compare("main_user", $currentFetchedData['main_user']);
         $criteria->compare("main_pass", $currentFetchedData['main_pass']);
         $criteria->compare("sub_user", $currentFetchedData['sub_user']);
         $criteria->compare("sub_pass", $currentFetchedData['sub_pass']);
         $foundModel = RemoteDataCache::model()->find($criteria);
         if ($foundModel) {
             $last_balance = $foundModel->balance;
             Yii::log("Current Data . " . json_encode($currentFetchedData), CLogger::LEVEL_INFO, 'sync_log');
             Yii::log("Model found . ", CLogger::LEVEL_INFO, 'sync_log');
             /* check current data before saving */
             /* notification check */
             Yii::log("Checking data for notification . ", CLogger::LEVEL_INFO, 'sync_log');
             $this->checkNotification($foundModel);
             /* deactivation check */
             Yii::log("Checking status code . ", CLogger::LEVEL_INFO, 'sync_log');
             //
             //$this->checkStatus($foundModel);
             //
             /* proceed with update */
             Yii::log("Updating balance . ", CLogger::LEVEL_INFO, 'sync_log');
             $foundModel->balance = doubleval($currentFetchedData['balance']);
             Yii::log("Balance updated . ", CLogger::LEVEL_INFO, 'sync_log');
             $foundModel->exact_balance = doubleval($currentFetchedData['exact_balance']);
             $foundModel->last_balance = $last_balance;
             $foundModel->vici_user = doubleval($currentFetchedData['vici_user']);
             $foundModel->num_lines = doubleval(@$currentFetchedData['number_of_lines']);
             $foundModel->ip_address = $currentFetchedData['server_ip'];
             $foundModel->campaign = $currentFetchedData["campaign"];
             //Removing this for now to prevent fresh data from updating deactivated record
             // $foundModel->is_active = $currentFetchedData["status"];
             if ($foundModel->save()) {
                 Yii::log("Found Model Updated . ", CLogger::LEVEL_INFO, 'sync_log');
             } else {
                 Yii::log("Cant update model because :  " . CHtml::errorSummary($foundModel), CLogger::LEVEL_INFO, 'sync_log');
             }
         } else {
             Yii::log(sprintf("Model not found  , saving as new model instead - %s - %s - %s - %s", $currentFetchedData['main_user'], $currentFetchedData['main_pass'], $currentFetchedData['sub_user'], $currentFetchedData['sub_pass']), CLogger::LEVEL_INFO, 'sync_log');
             $newModel = new RemoteDataCache();
             $newModel->main_user = $currentFetchedData['main_user'];
             $newModel->main_pass = $currentFetchedData['main_pass'];
             $newModel->sub_user = $currentFetchedData['sub_user'];
             $newModel->sub_pass = $currentFetchedData['sub_pass'];
             $newModel->balance = doubleval($currentFetchedData['balance']);
             $newModel->exact_balance = doubleval($currentFetchedData['exact_balance']);
             $newModel->vici_user = doubleval($currentFetchedData['vici_user']);
             $newModel->num_lines = doubleval(@$currentFetchedData['number_of_lines']);
             $newModel->ip_address = $currentFetchedData['server_ip'];
             $newModel->campaign = $currentFetchedData["campaign"];
             $newModel->is_active = $currentFetchedData["status"];
             if ($newModel->save()) {
                 Yii::log("New Model Saved . ", CLogger::LEVEL_INFO, 'sync_log');
             } else {
                 Yii::log("Cant save new model because :  " . CHtml::errorSummary($newModel), CLogger::LEVEL_INFO, 'sync_log');
             }
         }
     }
 }