Example #1
0
 /**
  * добавление нового расхода
  * @return string
  */
 public function actionAdd()
 {
     $errors = [];
     $message = null;
     $user_id = Yii::$app->user->getId();
     $model = new ExpenseForm();
     if (Yii::$app->request->isPost) {
         $model->setAttributes(Yii::$app->request->post("ExpenseForm"));
         if ($model->datetime) {
             $date = DateComponent::PeopleDateToDate($model->datetime);
             $model->datetime = $date;
         }
         $purse = Purse::find()->where(['id' => $model->purse_id])->one();
         if ($purse) {
             if (!$model->datetime || date("Y-m-d") >= $model->datetime) {
                 if ($purse->balance < $model->money) {
                     $message = '<div class="alert alert-danger" role="alert">На балансе этого кошелька денег меньше чем требуется для расхода</div>';
                 } else {
                     if ($model->validate()) {
                         $purse_minus = new Expense();
                         $purse_minus->setAttributes($model->getAttributes());
                         $type = $model->getType($user_id);
                         if ($type) {
                             $purse_minus->type_expense_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 ExpenseForm();
                             }
                         } 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 = TypeExpense::find()->where(['user_id' => $user_id])->orderBy('id desc')->all();
     return $this->render('add', ['purses' => $purses, 'categories' => $categories, 'model' => $model, 'message' => $message, 'errors' => $errors]);
 }
Example #2
0
 /**
  * добавление нового плана расхода
  * @return string
  */
 public function actionAdd()
 {
     $errors = [];
     $message = null;
     $user_id = Yii::$app->user->getId();
     $model = new ExpenseForm();
     if (Yii::$app->request->isPost) {
         $model->setAttributes(Yii::$app->request->post("PlanForm"));
         $purse = Purse::find()->where(['id' => $model->purse_id])->one();
         if ($purse) {
             if ($model->validate()) {
                 if ($model->datetime) {
                     $date = DateComponent::PeopleDateToDate($model->datetime);
                     $model->datetime = $date;
                 }
                 if (!$model->datetime || $model->datetime && date("Y-m-d") <= $model->datetime) {
                     $plan = new Plan();
                     $plan->setAttributes($model->getAttributes());
                     $type = $model->getType($user_id);
                     if ($type) {
                         $plan->type_expense_id = $type;
                         $save = $plan->save();
                         if ($save) {
                             $message = '<div class="alert alert-success" role="alert">Новый план добавлен</div>';
                             $model = new ExpenseForm(['scenario' => 'plan']);
                         }
                     } else {
                         $errors = $model->getErrors();
                     }
                 } else {
                     $message = '<div class="alert alert-danger" role="alert">Дата не должна быть раньше сегодняшней</div>';
                 }
             } else {
                 $errors = $model->getErrors();
             }
         }
     }
     $purses = Purse::find()->where(['user_id' => $user_id])->orderBy('active DESC')->all();
     $categories = TypeExpense::find()->where(['user_id' => $user_id])->orderBy('id desc')->all();
     return $this->render('add', ['purses' => $purses, 'categories' => $categories, 'model' => $model, 'message' => $message, 'errors' => $errors]);
 }