feat: add team controller

This commit is contained in:
iv 2025-03-25 23:24:16 +03:00
commit 1cba9692af

View File

@ -0,0 +1,159 @@
<?php
namespace app\controllers;
use Yii;
use app\models\Team;
use app\models\Transaction;
use app\models\Account;
use yii\web\Controller;
use yii\web\Response;
use yii\web\NotFoundHttpException;
use yii\widgets\ActiveForm;
use yii\data\ActiveDataProvider;
use yii\helpers\Url;
class TeamController extends Controller
{
public function actionIndex()
{
$searchModel = new Team();
$dataProvider = $searchModel->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('Команда не найдена');
}
}