Ejemplo n.º 1
0
 /**
  * Creates a new Income model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Income();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Ejemplo n.º 2
0
 /**
  * добавление нового дохода
  * @return string
  */
 public function actionAdd()
 {
     $errors = [];
     $message = null;
     $user_id = Yii::$app->user->getId();
     $model = new IncomeForm();
     if (Yii::$app->request->isPost) {
         $model->setAttributes(Yii::$app->request->post("IncomeForm"));
         if ($model->datetime) {
             $date = DateComponent::PeopleDateToDate($model->datetime);
             $model->datetime = $date;
         }
         if (!$model->datetime || date("Y-m-d") >= $model->datetime) {
             $purse = Purse::find()->where(['id' => $model->purse_id])->one();
             if ($purse) {
                 if ($model->validate()) {
                     $purse_minus = new Income();
                     $purse_minus->setAttributes($model->getAttributes());
                     $type = $model->getType($user_id);
                     if ($type) {
                         $purse_minus->type_income_id = $type;
                         $save = $purse_minus->save();
                         if ($save) {
                             $purse->balance += $model->money;
                             $purse->save();
                             $message = '<div class="alert alert-success" role="alert">Новый доход добавлен</div>';
                             $model = new IncomeForm();
                         }
                     } else {
                         $errors = $model->getErrors();
                     }
                 } else {
                     $errors = $model->getErrors();
                 }
             }
         } else {
             $message = '<div class="alert alert-danger" role="alert">Дата не должна быть позже сегодняшней</div>';
         }
     }
     $purses = Purse::find()->where(['user_id' => $user_id])->orderBy('active DESC')->all();
     $categories = TypeIncome::find()->where(['user_id' => $user_id])->orderBy('id desc')->all();
     return $this->render('add', ['purses' => $purses, 'categories' => $categories, 'message' => $message, 'model' => $model, 'errors' => $errors]);
 }