Exemple #1
0
 /**
  * Lists all models.
  */
 public function actionIndex()
 {
     $this->layout = '//layouts/column1';
     $id = Yii::app()->user->id;
     //echo Credits::getBalance(1);
     // confirm email
     //Credits::deduct(1, 500, 6);
     $model = $this->loadModel($id);
     $history_model = new CreditsHistory('search');
     $history_model->unsetAttributes();
     // clear any default values
     if (isset($_GET['CreditsHistory'])) {
         $history_model->attributes = $_GET['CreditsHistory'];
     }
     $this->render('index', array('model' => $model, 'history_model' => $history_model));
 }
 /**
  * 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 the ID of the model to be loaded
  */
 public function loadModel($id)
 {
     $model = CreditsHistory::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
Exemple #3
0
 public static function deduct($user_id, $amount, $item_id)
 {
     $user_id = (int) $user_id;
     if (self::checkBalance($user_id, $item_id)) {
         $transaction = Yii::app()->db->beginTransaction();
         $history = new CreditsHistory();
         $credit = Credits::model()->find('user_id = :user_id', array(':user_id' => $user_id));
         try {
             $history_data = array('user_id' => $user_id, 'credit_item_id' => $item_id, 'type' => 'deduction', 'value' => $amount);
             $history->attributes = $history_data;
             $history->save();
             $new_balance = $credit->balance - $amount;
             $credit_data = array('balance' => $new_balance);
             $credit->attributes = $credit_data;
             $credit->save();
             $transaction->commit();
         } catch (Exception $e) {
             $transaction->rollback();
         }
         return true;
     }
     return false;
 }