Beispiel #1
0
 public function actionIndex()
 {
     $user_id = Yii::$app->user->getId();
     $user = User::findIdentity($user_id);
     $active_purse = Purse::find()->where(['user_id' => $user_id, 'active' => 1])->one();
     $purses = Purse::find()->where(['user_id' => $user_id])->all();
     if ($user->payday) {
         // если есть дата зп
         $datePayDay = DateComponent::getDateFilterWithPayday();
     } else {
         $datePayDay = [date('Y'), date("m"), 1];
         // иначе ставим 1 число текущего месяца
     }
     $operations = Expense::find()->orderBy('datetime desc')->limit(6)->all();
     $types = TypeExpense::find()->where(['user_id' => $user_id])->select(['type_expense.*', TypeExpense::SELECT_SUM])->innerJoinWith(['expenses' => function ($query) use($datePayDay) {
         $query->where('date(datetime)>=:date_before', [':date_before' => implode('-', $datePayDay)]);
     }])->where(['purse_id' => $active_purse->id])->groupBy('type_expense.id')->all();
     $arrTypes = [];
     $sum = 0;
     foreach ($types as $type) {
         $sum += $type->sum;
         array_push($arrTypes, ['value' => $type->sum, 'color' => ColorComponent::random_color(), 'label' => $type->name]);
     }
     //        var_dump($types->prepare(Yii::$app->db->queryBuilder)->createCommand()->rawSql);die;
     return $this->render('index', ['arrTypes' => $arrTypes, 'date' => $datePayDay[2] . "." . $datePayDay[1] . "." . $datePayDay[0], 'active_purse' => $active_purse, 'purses' => $purses, 'sum' => $sum, 'operations' => $operations]);
 }
Beispiel #2
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 = TypeExpense::find()->where(['like', 'name', $this->category_new])->one();
                     if ($exist_category) {
                         $type_minus_id = $exist_category->id;
                     } else {
                         $category = new TypeExpense();
                         $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 TypeExpense();
         $category->name = $this->category_new;
         $category->user_id = $user_id;
         $category->save();
         $type_minus_id = $category->id;
     }
     return $type_minus_id;
 }
Beispiel #3
0
 /**
  * добавление категории  расходов
  * @return string
  */
 public function actionTypeAdd()
 {
     $message = null;
     $errors = null;
     $model = new TypeExpenseForm();
     $model->setAttributes(Yii::$app->request->post());
     if (Yii::$app->request->isPost) {
         if ($model->validate()) {
             $user_id = Yii::$app->user->getId();
             $type = new TypeExpense();
             $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('/expense/types/', 302);
             } else {
                 $errors = $type->getErrors();
             }
         } else {
             $errors = $model->getErrors();
         }
     }
     return $this->render('type/form', ['type' => $model, 'errors' => $errors, 'message' => $message]);
 }
Beispiel #4
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getTypeExpense()
 {
     return $this->hasOne(TypeExpense::className(), ['id' => 'type_expense_id']);
 }
Beispiel #5
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]);
 }
Beispiel #6
0
 public function getTypeExpenses()
 {
     return $this->hasMany(TypeExpense::className(), ['user_id' => 'id']);
 }