/**
  * @param $id
  * @param null $type
  */
 public function actionBilling($id, $type = null)
 {
     $user = UserModel::model()->findByPk($id);
     $billing = new TransModel();
     if ($type == 'import') {
         if (isset($_POST['payment']) && !empty($_POST['payment'])) {
             $billing->user_id = $id;
             $billing->service_id = $_POST['other_payment_type'];
             $billing->summ = $_POST['payment'];
             $billing->status = TransModel::STATUS_PAYMENT;
             $billing->remark = 'Пополнение счета';
             $billing->date_add = date('Y-m-d H:i:s');
             $billing->currency_id = 1;
             $user->balance = $user->balance + $_POST['payment'];
             $user->save(false);
             $billing->save(false);
             Yii::app()->user->setFlash('success', 'Баланс пополнен');
         }
     }
     if ($type == 'export') {
         if (isset($_POST['payment']) && !empty($_POST['payment'])) {
             $balance = $user->balance - $_POST['payment'];
             if ($balance < 0) {
                 Yii::app()->user->setFlash('error', 'Недостаточно денег на счету');
             } else {
                 $billing = new TransModel();
                 $billing->user_id = $id;
                 $billing->service_id = $_POST['other_payment_type'];
                 $billing->summ = $_POST['payment'];
                 $billing->status = TransModel::STATUS_PAYMENT;
                 $billing->remark = 'Вывод средств';
                 $billing->date_add = date('Y-m-d H:i:s');
                 $billing->currency_id = 1;
                 $user->balance = $balance;
                 $user->save(false);
                 $billing->save(false);
                 Yii::app()->user->setFlash('success', 'Успешно!');
             }
         }
     }
     if ($user) {
         $this->render('billing', ['billing' => $billing, 'user' => $user]);
     } else {
         $this->redirect('/');
     }
 }
 public function actionWithdrawals()
 {
     $msg = '';
     if (Yii::app()->request->isPostRequest) {
         //	    if( empty($_POST['summ']) || empty($_POST['service']) || intval($_POST['summ']) > 0){
         //		$msg = 'Ошибка входных данных';
         //	    }else{
         $trans = new TransModel();
         $trans->setAttributes(['user_id' => Yii::app()->user->getId(), 'service_id' => new CDbExpression('NULL'), 'summ' => -$_POST['summ'], 'status' => TransModel::STATUS_PAYMENT, 'remark' => $_POST['comments'], 'currency_id' => 1]);
         if ($trans->validate()) {
             $trans->save(FALSE);
             Yii::app()->user->setFlash('notify', 'Ваш запрос отправлен');
             $this->redirect(['billing/']);
         } else {
             $msg = current(current($trans->getErrors()));
         }
         //	    }
     }
     $this->render('withdrawals', ['msg' => $msg]);
 }
 public function actionAdd()
 {
     /**
      * @var CWebApplication $app
      * @var Document $costDocument
      */
     if (Yii::app()->user->isGuest) {
         Yii::app()->user->setFlash('error', 'Заказать документ может только зарегестрированный пользователь');
         $this->redirect($this->createUrl('site/index'));
     }
     $app = Yii::app();
     $view = 'document';
     $id = $app->user->getState('document_id');
     $model = null;
     $user = UserModel::model()->findByPk($app->user->id);
     if (!$id) {
         $document = new Document();
         if (!empty($_POST['Document'])) {
             $document->attributes = $_POST['Document'];
             $image = CUploadedFile::getInstance($document, 'file');
             $document->user_id = Yii::app()->user->id;
             $document->status = 0;
             if ($document->save()) {
                 if ($image) {
                     if (!is_dir('uploads/document/images')) {
                         mkdir('uploads/document/images', 0777, true);
                     }
                     $ext = explode('.', $image->name);
                     $document->file = Yii::app()->user->id . '_' . md5(time()) . '.' . $ext[1];
                     $image->saveAs('uploads/document/images/' . $document->file);
                     $document->save(false);
                 }
                 $app->user->setState('document_id', $document->id);
                 $view = 'document_user_info';
                 $document = new DocumentUser();
             }
         }
         $model = $document;
     } elseif (null !== ($document = Document::model()->findByPk($id)) && !$document->document_user) {
         $document_user = new DocumentUser();
         $view = 'document_user_info';
         if (!empty($_POST['DocumentUser'])) {
             $document_user->attributes = $_POST['DocumentUser'];
             $document_user->document_id = $document->id;
             if ($document_user->save()) {
                 $view = 'document_pay';
                 $document_user = $user;
             }
         }
         $model = $document_user;
     } else {
         $model = $user;
         if (isset($_POST['payment']) && !empty($_POST['payment'])) {
             $balance = $model->balance - $_POST['payment'];
             if ($balance < 0) {
                 Yii::app()->user->setFlash('error', 'Недостаточно денег на счету');
             } else {
                 $billing = new TransModel();
                 $billing->user_id = $id;
                 $billing->service_id = $_POST['other_payment_type'];
                 $billing->summ = $_POST['payment'];
                 $billing->status = TransModel::STATUS_PAYMENT;
                 $billing->remark = 'Вывод средств';
                 $billing->date_add = date('Y-m-d H:i:s');
                 $billing->currency_id = 1;
                 $model->balance = $balance;
                 $model->save(false);
                 $billing->save(false);
                 $documentId = $app->user->getState('document_id');
                 $costDocument = Document::model()->findByPk($documentId);
                 $costDocument->cost = $_POST['payment'];
                 $costDocument->save(false);
                 Yii::app()->user->setFlash('success', 'Успешно! Ожидайте юрист с вами свяжется!');
                 $app->user->setState('document_id', null);
                 $this->refresh();
             }
         }
         $view = 'document_pay';
     }
     $this->render($view, ['model' => $model]);
 }