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]); }
/** * добавление нового расхода * @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]); }
/** * добавление нового плана расхода * @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]); }