public function actionRegister() { $errors = null; $model = new UserForm(); if (Yii::$app->request->isPost) { $model->setAttributes(Yii::$app->request->post()); if ($model->validate()) { $user = new User(); $user->setAttributes($model->getAttributes()); $user->setPassword($model->password); $user->generateAuthKey(); $save = $user->save(); if ($save) { $purse = new Purse(); $purse->user_id = $user->id; $purse->active = 1; $purse->balance = 0; $purse->name = "Основной"; $purse->save(); $login = Yii::$app->user->login($user, 3600 * 24 * 14); if ($login) { return $this->goHome(); } } } else { $errors = $model->getErrors(); } } return $this->renderPartial('register', ['errors' => $errors, 'model' => $model]); }
/** * Страница редактирования кошелька * @param $id * @return string */ public function actionEdit($id) { $purse = Purse::find()->where(['id' => $id])->one(); if ($purse->load(Yii::$app->request->post()) && $purse->save()) { Yii::$app->getSession()->setFlash('notification', 'Данные кошелька "' . $purse->name . '" успешно изменены'); return $this->redirect(Url::toRoute(['/purse/index'])); } $this->getView()->params['title'] = 'Редактирование кошелька "' . $purse->name . '"'; return $this->render('edit', ['purse' => $purse]); }
/** * удаляем ajax затрату * @return array */ public function actionDelete() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $id = Yii::$app->request->post('id'); if ($id) { $expense = Expense::find()->where(['id' => $id])->one(); $purse = Purse::find()->where(['id' => $expense->purse_id])->one(); $purse->balance += $expense->money; $purse->save(); $result = $expense->delete(); } return ['success' => $result]; }
/** * @return \yii\db\ActiveQuery */ public function getPurse() { return $this->hasOne(Purse::className(), ['id' => 'purse_id']); }
/** * создаем расход по плану * @return array */ public function actionToExpense() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $id = Yii::$app->request->post('id'); $planValue = Yii::$app->request->post('planValue'); $return = []; if ($id && $planValue) { $plan = Plan::find()->where(['id' => $id])->one(); $purse = Purse::find()->where(['id' => $plan->purse_id])->one(); if ($purse->balance >= $planValue) { $expense = new Expense(); $expense->setAttributes($plan->getAttributes()); $expense->money = $planValue; $save = $expense->save(); if ($save) { $purse->balance -= $planValue; $purse->save(); $plan->delete(); } $return = ['success' => 1, 'message' => '']; } else { $return = ['success' => 0, 'message' => 'Недостаточно средств на кошельке']; } } return ['success' => $return]; }
public function getPurses() { return $this->hasMany(Purse::className(), ['user_id' => 'id']); }
/** * добавление нового дохода * @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]); }