Example #1
0
 public function getType($user_id)
 {
     $type_minus_id = null;
     if ($this->category) {
         switch ($this->category) {
             case "exist":
                 $type_minus_id = $this->category_exist;
                 break;
             case "new":
                 if (!$this->category_new) {
                     $this->addError('category_new', "Нет категории");
                     return null;
                 } else {
                     $exist_category = TypeIncome::find()->where(['like', 'name', $this->category_new])->one();
                     if ($exist_category) {
                         $type_minus_id = $exist_category->id;
                     } else {
                         $category = new TypeIncome();
                         $category->name = $this->category_new;
                         $category->user_id = $user_id;
                         $category->save();
                         $type_minus_id = $category->id;
                     }
                     break;
                 }
         }
     } else {
         if (!$this->category_new) {
             $this->addError("Нет категории");
         }
         $category = new TypeIncome();
         $category->name = $this->category_new;
         $category->user_id = $user_id;
         $category->save();
         $type_minus_id = $category->id;
     }
     return $type_minus_id;
 }
Example #2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getTypeIncome()
 {
     return $this->hasOne(TypeIncome::className(), ['id' => 'type_income_id']);
 }
Example #3
0
 /**
  * добавление категории  доходов
  * @return string
  */
 public function actionTypeAdd()
 {
     $message = null;
     $errors = null;
     $model = new TypeIncomeForm();
     $model->setAttributes(Yii::$app->request->post());
     if (Yii::$app->request->isPost) {
         if ($model->validate()) {
             $user_id = Yii::$app->user->getId();
             $type = new TypeIncome();
             $type->setAttributes($model->getAttributes());
             $type->user_id = $user_id;
             $result = $type->save();
             if ($result) {
                 //                    $message = '<div class="alert alert-success" role="alert">Тип добавлен</div>';
                 return $this->redirect('/income/types/', 302);
             } else {
                 $errors = $type->getErrors();
             }
         } else {
             $errors = $model->getErrors();
         }
     }
     return $this->render('type/form', ['type' => $model, 'errors' => $errors, 'message' => $message]);
 }