/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Donation();
     if (isset($_GET['user'])) {
         $model->user_id = $_GET['user'];
     }
     if (isset($_GET['visit'])) {
         $model->visit_id = $_GET['visit'];
         $model->solicitor_id = Visits::model()->findByPk($model->visit_id)->solicitor_id;
     }
     $users_lists = BaseModel::getAll('Users');
     $users = array();
     foreach ($users_lists as $user) {
         $users[$user->id] = $user->first_name . ' ' . $user->last_name . '(' . $user->username . ')';
     }
     // $users = CHtml::listData(BaseModel::getAll('Users'),'id','username');
     $lists = BaseModel::getAll('Solicitor');
     $solicitors = array();
     foreach ($lists as $list) {
         $solicitors[$list->id] = $list->first_name . ' ' . $list->last_name . '(' . $list->solicitor_code . ')';
     }
     $visits = CHtml::listData(BaseModel::getAll('Visits'), 'id', 'visit_code');
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Donation'])) {
         $model->attributes = $_POST['Donation'];
         $model->reference_number = getToken(8);
         $model->validate();
         if (!empty($model->user_id)) {
             $user_id = $model->user_id;
             $user_balance = Users::model()->getUserBalance($user_id);
             $user_model = Users::model()->findByPk($user_id);
             $credit_limits = $user_model->credit_limits;
             $donation_amt = $model->amount;
             if ($user_balance >= 0) {
                 if ($user_balance < $donation_amt) {
                     $user_allowable_amount = $user_balance + $credit_limits;
                     if ($donation_amt > $user_allowable_amount) {
                         $model->addError('amount', "Sorry. The user has not the sufficient balance.");
                     }
                 }
             } else {
                 if ($user_balance < 0) {
                     $user_allowable_amount = $credit_limits - abs($user_balance);
                     if ($donation_amt > $user_allowable_amount) {
                         $model->addError('amount', "Sorry. The user has not the sufficient balance.");
                     }
                 }
             }
         }
         if (empty($model->errors)) {
             if ($model->save()) {
                 $trans = new UserTrans();
                 $trans->tran_type = 'DONATION';
                 $trans->user_id = $model->user_id;
                 $trans->debit = $model->amount;
                 $trans->donation_id = $model->id;
                 $trans->save();
                 $this->redirect(array('view', 'id' => $model->id));
             }
         }
     }
     $this->render('create', array('model' => $model, 'users' => $users, 'solicitors' => $solicitors, 'visits' => $visits));
 }