/**
  * This is the default 'index' action that is invOked
  * when an action is not explicitly requested by users.
  */
 public function actionIndex()
 {
     $this->redirect(array('/sipAccount/index'));
     $transactionLogMdl = new TransactionLog();
     if (isset($_POST['TransactionLog'])) {
         $transactionLogMdl->attributes = $_POST['TransactionLog'];
         if ($transactionLogMdl->save()) {
             /*send to remote*/
             $rmt = new RemoteTransferFund();
             $remoteResult = $rmt->commitTransaction($transactionLogMdl->freevoipAccount->username, $transactionLogMdl->freevoipAccount->password, $transactionLogMdl->to_username, $transactionLogMdl->amount, $transactionLogMdl->pincode);
             $newTransactionlink = CHtml::link('Transaction Log', array('transactionLog/view', 'id' => $transactionLogMdl->id));
             $transactionLogMdl->result_string = $remoteResult->resultstring;
             $transactionLogMdl->result_description = $remoteResult->description;
             $transactionLogMdl->save();
             if ($remoteResult->resultstring == 'success') {
                 Yii::app()->user->setFlash('success', '<strong>Success!</strong> Credit transfered . ' . $newTransactionlink);
             } else {
                 if ($remoteResult->resultstring == 'failure') {
                     $reasonOfFailure = "unknown";
                     if (isset($remoteResult->description)) {
                         $reasonOfFailure = $remoteResult->description;
                     }
                     Yii::app()->user->setFlash('error', '<strong>Transaction Failed!</strong> We met some error while transferring the amount . <br>But here is your transaction log ' . $newTransactionlink . ' , you can resend it later. <br>Reason of failure : ' . $reasonOfFailure);
                 }
             }
             $this->redirect("/site/index");
         }
     }
     $voipAccountsCount = FreeVoipAccounts::model()->count();
     $transactionCount = TransactionLog::model()->count();
     $sipAccountCount = SipAccount::model()->count();
     $this->render('dashboard', compact('voipAccountsCount', 'transactionCount', 'transactionLogMdl', 'sipAccountCount'));
 }
 /**
  * @param $freeVoipAccountUsername
  * @param $topupValue
  * @param RemoteDataCache $subAccount
  * @throws CHttpException
  */
 public function topUp($freeVoipAccountUsername, $topupValue, RemoteDataCache $subAccount)
 {
     $criteria = new CDbCriteria();
     $criteria->compare("username", $freeVoipAccountUsername);
     $freeVoipAccount = FreeVoipAccounts::model()->find($criteria);
     if ($freeVoipAccount) {
         //transfer fund from FreeVoipAccount to SipAccount
         /**
          * @TODO - transfer this to a component
          */
         $rmt = new RemoteTransferFund();
         $remoteResult = $rmt->commitTransaction($freeVoipAccount->username, $freeVoipAccount->password, $subAccount->main_user, doubleval($topupValue), $freeVoipAccount->pincode);
         ViciActionLogger::logAction("MAIN_TOPUP", "Credit increased account {$subAccount->main_user}", $topupValue, uniqid(), time());
     } else {
         throw new CHttpException(404, "{$freeVoipAccountUsername} doesnt exists");
     }
 }
 /**
  * Top-ups main SIP account using freevoip username
  * @return void [type] [description]
  */
 public function actionMainSip()
 {
     header("Content-Type: application/json");
     $jsonMessage = array("success" => false, "message" => "incomplete data/parameter");
     $postedData = json_decode(file_get_contents("php://input"), true);
     if (isset($postedData['freeVoipUsername']) && isset($postedData['mainUsername']) && isset($postedData['mainPassword']) && isset($postedData['credits'])) {
         /**
          * @var SubSipAccount @model
          */
         $criteria = new CDbCriteria();
         $criteria->compare("username", $postedData['freeVoipUsername']);
         $freeVoipAccount = FreeVoipAccounts::model()->find($criteria);
         // Yii::log($freeVoipAccount->username. ' '. $freeVoipAccount->password, CLogger::LEVEL_INFO,'info');
         if (is_null($freeVoipAccount)) {
             $jsonMessage = array("success" => false, "message" => "Cant find FreeVOIP Account");
         } else {
             $rmt = new RemoteTransferFund();
             $remoteResult = $rmt->commitTransaction($freeVoipAccount->username, $freeVoipAccount->password, $postedData['mainUsername'], doubleval($postedData['credits']), $freeVoipAccount->pincode);
             $jsonMessage = array("success" => true, "message" => "Credits transfered");
         }
     }
     echo json_encode($jsonMessage);
 }
 public function actionResend($transactionId)
 {
     $model = $this->loadModel($transactionId);
     $rmt = new RemoteTransferFund();
     $remoteResult = $rmt->commitTransaction($model->freevoipAccount->username, $model->freevoipAccount->password, $model->to_username, $model->amount, $model->pincode);
     $newTransactionlink = CHtml::link('Transaction Log', array('transactionLog/view', 'id' => $model->id));
     if ($remoteResult->resultstring == 'success') {
         Yii::app()->user->setFlash('success', '<strong>Success!</strong> Transaction resent successfully . Heres you log  : ' . $newTransactionlink);
     } else {
         if ($remoteResult->resultstring == 'failure') {
             $reasonOfFailure = "unknown";
             if (isset($remoteResult->description)) {
                 $reasonOfFailure = $remoteResult->description;
             }
             Yii::app()->user->setFlash('error', '<strong>Transaction Failed!</strong> We met some error while transferring the amount . <br>But here is your transaction log ' . $newTransactionlink . ' , you can resend it later. <br>Reason of failure : ' . $reasonOfFailure);
         }
     }
     $this->redirect("/");
 }