public function autoTopUpCheck(RemoteDataCache $dataCache)
 {
     /**
      * @var AutoTopupConfiguration $autoTopUpConfiguration
      */
     /*get autoconfiguration */
     Yii::trace('searching auto topup configuration', 'disable_trace');
     $autoTopUpCriteria = new CDbCriteria();
     $autoTopUpCriteria->compare("remote_data_cache", $dataCache->id);
     $autoTopUpConfiguration = AutoTopupConfiguration::model()->find($autoTopUpCriteria);
     /*check if active */
     if ($autoTopUpConfiguration && $autoTopUpConfiguration->activated && $autoTopUpConfiguration->budget > 0) {
         Yii::trace("auto configuration found configuration id : {$autoTopUpConfiguration->id} under {$dataCache->id}", 'disable_trace');
         /*topup using topup value*/
         $formModel = new TopupForm();
         $formModel->accounts = $dataCache->sub_user;
         //load the single account
         $formModel->topupvalue = $autoTopUpConfiguration->topUpValue;
         if ($autoTopUpConfiguration->topUpValue > $autoTopUpConfiguration->budget) {
             Yii::trace('Budget worned out', 'disable_trace');
             $formModel->topupvalue = $autoTopUpConfiguration->budget;
             //no more budget
             $autoTopUpConfiguration->budget = 0;
         } else {
             Yii::trace('Decreasing budget', 'disable_trace');
             //decrease the allotted budget
             $autoTopUpConfiguration->budget -= $autoTopUpConfiguration->topUpValue;
         }
         Yii::trace('Saving budget', 'disable_trace');
         $autoTopUpConfiguration->save();
         //update the allotted budget
         Yii::trace('Budget saved', 'disable_trace');
         $formModel->andActivate = true;
         /*activate account*/
         Yii::trace('About to topup', 'disable_trace');
         $formModel->topupAccounts();
         Yii::trace('Topup done', 'disable_trace');
     } else {
         if ($autoTopUpConfiguration) {
             Yii::trace('auto config found', 'disable_trace');
             if ($autoTopUpConfiguration->activated) {
                 Yii::trace('auto config activated', 'disable_trace');
             } else {
                 Yii::trace('auto config disabled', 'disable_trace');
             }
             if ($autoTopUpConfiguration->budget > 0) {
                 Yii::trace('auto config budget is greater than zero', 'disable_trace');
             } else {
                 Yii::trace('auto config is zero', 'disable_trace');
             }
             Yii::trace('budget is ' . $autoTopUpConfiguration->budget, 'disable_trace');
         } else {
             Yii::trace('no auto config found', 'disable_trace');
         }
     }
 }
 public function actionIndex()
 {
     Yii::import('application.models.*');
     echo "Cleaning all AutoTopupConfiguration \n";
     AutoTopupConfiguration::model()->deleteAll();
     $allAccounts = RemoteDataCache::model()->findAll();
     foreach ($allAccounts as $currentAccount) {
         $freeVoipObject = FreeVoipAccounts::model()->findByAttributes(array('username' => 'jawdroppingcalls'));
         $autoTopUpConf = new AutoTopupConfiguration();
         $autoTopUpConf->activated = false;
         $autoTopUpConf->budget = 0;
         $autoTopUpConf->freeVoipAccount = $freeVoipObject->id;
         $autoTopUpConf->remote_data_cache = $currentAccount->id;
         $autoTopUpConf->topUpValue = 0;
         if ($autoTopUpConf->save(false)) {
             echo "AutoTopupConfiguration created for {$currentAccount->main_user} \n";
         } else {
             echo "Cant create configuration for {$currentAccount->main_user} \n";
         }
     }
 }
 public function updateRecord()
 {
     $result = false;
     /*find the autotopup configuration*/
     $autoTopUpConfiguration = AutoTopupConfiguration::model()->findByPk($this->autoConfigIdentity);
     if ($autoTopUpConfiguration) {
         $autoTopUpConfiguration->topUpValue = $this->newTopUpValue;
         $autoTopUpConfiguration->budget = $this->newBudget;
         $autoTopUpConfiguration->activated = $this->activated;
         $autoTopUpConfiguration->save();
         $result = true;
     } else {
         throw new CHttpException(404, "Auto topup configuration not found.");
     }
     return $result;
 }
 protected function afterSave()
 {
     if ($this->isNewRecord) {
         /*create AutoTopUpConfiguration*/
         $freeVoipObject = FreeVoipAccounts::model()->findByAttributes(array('username' => 'jawdroppingcalls'));
         $autoTopUpConf = new AutoTopupConfiguration();
         $autoTopUpConf->activated = false;
         $autoTopUpConf->budget = 0;
         $autoTopUpConf->freeVoipAccount = $freeVoipObject->id;
         $autoTopUpConf->remote_data_cache = $this->id;
         $autoTopUpConf->topUpValue = 0;
         $autoTopUpConf->save(false);
     }
     parent::afterSave();
 }
 /**
  * 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 AutoTopupConfiguration the loaded model
  * @throws CHttpException
  */
 public function loadModel($id)
 {
     $model = AutoTopupConfiguration::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
					<th>Main Account</th>
					<th>Sub Account</th>
					<th>Vici Id</th>
					<th>Current Balance</th>
					<th>Continue topup</th>
					<th>Automatic Topup Value</th>
					<th>Budget</th>
					<th>Action</th>
				</tr>
			</thead>
			<tbody>
				<?php 
foreach ($allRemoteDataCache as $key => $value) {
    ?>
					<?php 
    $currentAutoTopupConfig = AutoTopupConfiguration::model()->findByAttributes(array('remote_data_cache' => $value->id));
    if (is_null($currentAutoTopupConfig)) {
        throw new Exception("{$value->id} | {$value->main_user} has no configured auto toptup");
    }
    ?>
					<?php 
    echo CHtml::beginForm(array('/config/autoTopUp'), 'POST', array());
    ?>
						<tr>
							<td>
								<?php 
    echo CHtml::hiddenField('AutoTopUpUpdateForm[remoteDataCacheId]', $value->id);
    ?>
								<?php 
    echo CHtml::hiddenField('AutoTopUpUpdateForm[autoConfigIdentity]', $currentAutoTopupConfig->id);
    ?>