From 1cba9692af10859bcbba722809b374cb2dbd488c Mon Sep 17 00:00:00 2001 From: iv Date: Tue, 25 Mar 2025 23:24:16 +0300 Subject: [PATCH] feat: add team controller --- controllers/TeamController.php | 159 +++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 controllers/TeamController.php diff --git a/controllers/TeamController.php b/controllers/TeamController.php new file mode 100644 index 0000000..5040bac --- /dev/null +++ b/controllers/TeamController.php @@ -0,0 +1,159 @@ +search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + public function actionCreate() + { + $model = new Team(); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + Yii::$app->session->setFlash('success', 'Команда успешно создана'); + return $this->redirect(['index']); + } + + return $this->render('create', [ + 'model' => $model, + ]); + } + + public function actionStore() + { + return $this->actionCreate(); + } + + public function actionEdit($id) + { + $model = $this->findModel($id); + + return $this->render('edit', [ + 'model' => $model, + ]); + } + + public function actionUpdate($id) + { + $model = $this->findModel($id); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + Yii::$app->session->setFlash('success', 'Команда успешно обновлена'); + return $this->redirect(['show', 'id' => $model->id]); + } + + return $this->render('edit', [ + 'model' => $model, + ]); + } + + public function actionDestroy($id) + { + $this->findModel($id)->delete(); + Yii::$app->session->setFlash('success', 'Команда успешно удалена'); + return $this->redirect(['index']); + } + + public function actionShow($id) + { + $model = $this->findModel($id); + + $balanceMoloco = $model->getBalanceByType('moloco'); + $balanceIronSource = $model->getBalanceByType('ironsource'); + + $startDate = date('Y-m-01'); + $endDate = date('Y-m-t'); + + $monthlySpendMoloco = Transaction::find() + ->where(['account_type' => 'moloco']) + ->andWhere(['>', 'amount', 0]) + ->andFilterWhere(['between', 'spend_date', $startDate, $endDate]) + ->sum('amount'); + + $monthlySpendIronSource = Transaction::find() + ->where(['account_type' => 'ironsource']) + ->andWhere(['>', 'amount', 0]) + ->andFilterWhere(['between', 'spend_date', $startDate, $endDate]) + ->sum('amount'); + + $positiveTransactions = Transaction::find() + ->where(['team_id' => $id]) + ->andWhere(['>', 'amount', 0]) + ->orderBy(['created_at' => SORT_DESC]) + ->all(); + + $latestDates = [ + 'moloco' => Transaction::find() + ->select(['spend_date']) + ->where(['account_type' => 'moloco']) + ->max('spend_date'), + 'ironsource' => Transaction::find() + ->select(['spend_date']) + ->where(['account_type' => 'ironsource']) + ->max('spend_date'), + ]; + + return $this->render('show', [ + 'model' => $model, + 'balanceMoloco' => $balanceMoloco, + 'balanceIronSource' => $balanceIronSource, + 'positiveTransactions' => $positiveTransactions, + 'latestDates' => $latestDates, + 'monthlySpendMoloco' => $monthlySpendMoloco, + 'monthlySpendIronSource' => $monthlySpendIronSource, + ]); + } + + public function actionEditMinBalance($id) + { + $model = $this->findModel($id); + + return $this->render('edit-min-balance', [ + 'model' => $model, + ]); + } + + public function actionUpdateMinBalance($id) + { + $model = $this->findModel($id); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + Yii::$app->session->setFlash('success', 'Минимальный баланс успешно обновлен'); + return $this->redirect(['show', 'id' => $model->id]); + } + + return $this->render('edit-min-balance', [ + 'model' => $model, + ]); + } + + protected function findModel($id) + { + if (($model = Team::findOne($id)) !== null) { + return $model; + } + + throw new NotFoundHttpException('Команда не найдена'); + } +} \ No newline at end of file