/**
  *
  * @throws Exception
  * @return integer Returns number of updated accounts
  */
 public function topupAccounts()
 {
     $accountsAffectedInt = 0;
     $accountsArr = explode(",", $this->accounts);
     $groupId = uniqid();
     foreach ($accountsArr as $key => $currentAccountName) {
         /**
          * @var $model RemoteDataCache
          */
         $model = RemoteDataCache::model()->findByAttributes(array('sub_user' => $currentAccountName));
         if ($model) {
             //topup the main account
             /**
              * @var $topUpMainAccount TopUpMainAccount
              */
             $topUpMainAccount = Yii::app()->topUpMainAccount;
             $topUpMainAccount->topUp($this->freeVoipAccountUsername, $this->topupvalue, $model);
             //$this->topUpMainAccount($model); // topup the main account  , old implementation
             //TOPUP the sub accounts
             /**
              * @TODO - use Yii component , put this to yii component
              */
             $remoteAcctUpdated = new ApiRemoteUpdateBalance($model->sub_pass, $model->sub_user, $model->main_pass, $model->main_user, $this->topupvalue);
             /**
              * Logger
              */
             ViciActionLogger::logAction("SUB_ACCOUNT_TOPUP", "Top upping {$model->sub_user} with {$this->topupvalue}", $this->topupvalue, $groupId, time());
             /**
              * @TODO - use Yii component
              */
             if ($remoteAcctUpdated->update()) {
                 if ($this->andActivate) {
                     /**
                      * @TODO - use Yii component
                      */
                     $activator = new ActivationFormModel();
                     $activator->activateAccount($model);
                 }
             }
             /**
              * Force the RemoteDataCache instance to update its current campaign
              */
             $campaignForcer = Yii::app()->campaignForcer;
             $campaignForcer->update($this->forceAgent, $model->sub_user);
             /**
              * Get the latest data from remote api
              */
             $lastBalance = $model->balance;
             $voipInfoRetriever = new BestVOIPInformationRetriever();
             $remoteVoipResult = $voipInfoRetriever->getInfo($model->main_user, $model->main_pass, $model->sub_user, $model->sub_pass);
             /**
              * Checking
              */
             if ($model->last_balance_since_topup !== 0 && !is_null($model->last_balance_since_topup)) {
                 /**
                  * Create a charge log for certain remote data cache
                  */
                 $newChargeLog = new AccountChargeLog();
                 $newChargeLog->account_id = $model->id;
                 $newChargeLog->charge = doubleval($model->last_balance_since_topup) - doubleval($model->exact_balance);
                 if (!$newChargeLog->save()) {
                     throw new Exception(CHtml::errorSummary($newChargeLog));
                 }
             }
             $model->balance = doubleval($remoteVoipResult->getBalance());
             $model->exact_balance = doubleval($remoteVoipResult->getSpecificBalance());
             $model->last_balance = $lastBalance;
             $model->last_balance_since_topup = $remoteVoipResult->getSpecificBalance();
             $model->save();
             /**
              * Update the counter
              */
             $accountsAffectedInt++;
         }
     }
     return $accountsAffectedInt;
 }
 public function actionSipData()
 {
     if (!isset(Yii::app()->request->urlReferrer)) {
         throw new CHttpException(500, "Invalid request");
     } else {
         header("Content-Type: application/json");
         $criteria = new CDbCriteria();
         // $criteria->order = "is_active ASC  , balance DESC";
         $criteria->order = "vici_user ASC";
         $allremoteData = RemoteDataCache::model()->findAll($criteria);
         $updatedData = array();
         /*format some data*/
         foreach ($allremoteData as $curObj) {
             /**
              * @var $curObj ORemoteDataCache
              */
             $curObj->date_updated = $this->simpleAgoHelper(strtotime($curObj->date_updated));
             $criteria = new CDbCriteria();
             $criteria->compare("account_id", $curObj->id);
             $criteria->order = "date_created DESC";
             $last_credit_update = AccountChargeLog::model()->find($criteria);
             //get just one from the bottom , the latest
             $curObj->last_credit_update = '';
             //
             if ($last_credit_update) {
                 $curObj->last_credit_update = date("F j, Y, g:i a", strtotime($last_credit_update->date_created));
             }
             $updatedData[] = CMap::mergeArray($curObj->attributes, ["last_credit_update" => $curObj->last_credit_update]);
         }
         echo CJSON::encode($updatedData);
         Yii::app()->end();
     }
 }
 /**
  * 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 AccountChargeLog the loaded model
  * @throws CHttpException
  */
 public function loadModel($id)
 {
     $model = AccountChargeLog::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }