--- /dev/null
+<?php
+
+namespace yii_app\controllers\crud;
+
+use Yii;
+use yii\helpers\ArrayHelper;
+use yii_app\records\Admin;
+use yii_app\records\Cluster;
+use yii_app\records\ClusterAdmin;
+use yii_app\records\ClusterAdminSearch;
+use yii\web\Controller;
+use yii\web\NotFoundHttpException;
+use yii\filters\VerbFilter;
+
+/**
+ * ClusterAdminController implements the CRUD actions for ClusterAdmin model.
+ */
+class ClusterAdminController extends Controller
+{
+ /**
+ * @inheritDoc
+ */
+ public function behaviors()
+ {
+ return array_merge(
+ parent::behaviors(),
+ [
+ 'verbs' => [
+ 'class' => VerbFilter::className(),
+ 'actions' => [
+ 'delete' => ['POST'],
+ ],
+ ],
+ ]
+ );
+ }
+
+ /**
+ * Lists all ClusterAdmin models.
+ *
+ * @return string
+ */
+ public function actionIndex()
+ {
+ $searchModel = new ClusterAdminSearch();
+ $dataProvider = $searchModel->search($this->request->queryParams);
+
+ return $this->render('index', [
+ 'searchModel' => $searchModel,
+ 'dataProvider' => $dataProvider,
+ ]);
+ }
+
+ /**
+ * Displays a single ClusterAdmin model.
+ * @param int $id ID записи
+ * @return string
+ * @throws NotFoundHttpException if the model cannot be found
+ */
+ public function actionView($id)
+ {
+ return $this->render('view', [
+ 'model' => $this->findModel($id),
+ ]);
+ }
+
+ /**
+ * Creates a new ClusterAdmin model.
+ * If creation is successful, the browser will be redirected to the 'view' page.
+ * @return string|\yii\web\Response
+ */
+ public function actionCreate()
+ {
+ $model = new ClusterAdmin();
+
+ if ($this->request->isPost && $model->load($this->request->post())) {
+ // Проверка на существование записи с такими же cluster_id и admin_id и date_end > текущей даты
+ $existingRecord = ClusterAdmin::find()
+ ->where(['cluster_id' => $model->cluster_id, 'admin_id' => $model->admin_id])
+ ->andWhere(['>', 'date_end', date('Y-m-d')])
+ ->one();
+
+ if ($existingRecord) {
+ Yii::$app->session->setFlash('error', 'Запись с таким кустом и кустовым уже существует и ещё актуальна.');
+ } else {
+ // Проверка на пересечение дат
+ $overlappingRecord = ClusterAdmin::find()
+ ->where(['cluster_id' => $model->cluster_id, 'admin_id' => $model->admin_id])
+ ->andWhere(['<=', 'date_start', $model->date_start])
+ ->andWhere(['>=', 'date_end', $model->date_start])
+ ->one();
+
+ if ($overlappingRecord) {
+ Yii::$app->session->setFlash('error', 'Дата начала пересекается с существующей записью.');
+ } else {
+ if ($model->save()) {
+ return $this->redirect(['view', 'id' => $model->id]);
+ }
+ }
+ }
+ }
+ // Получение данных для выпадающих списков
+ $clusters = ArrayHelper::map(Cluster::find()->all(), 'id', 'name');
+ $admins = ArrayHelper::map(Admin::find()
+ ->select(['id', 'name'])
+ ->where(['NOT IN', 'group_id', [-1, 1000]]) // Исключаем сотрудников с group_id = -1 и group_id = 1000
+ ->all(), 'id', 'name');
+
+ return $this->render('create', [
+ 'model' => $model,
+ 'clusters' => $clusters,
+ 'admins' => $admins,
+ ]);
+ }
+
+ /**
+ * Updates an existing ClusterAdmin model.
+ * If update is successful, the browser will be redirected to the 'view' page.
+ * @param int $id ID записи
+ * @return string|\yii\web\Response
+ * @throws NotFoundHttpException if the model cannot be found
+ */
+ public function actionUpdate($id)
+ {
+ $model = $this->findModel($id);
+
+ if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
+ return $this->redirect(['view', 'id' => $model->id]);
+ }
+
+ return $this->render('update', [
+ 'model' => $model,
+ ]);
+ }
+
+ /**
+ * Deletes an existing ClusterAdmin model.
+ * If deletion is successful, the browser will be redirected to the 'index' page.
+ * @param int $id ID записи
+ * @return \yii\web\Response
+ * @throws NotFoundHttpException if the model cannot be found
+ */
+ public function actionDelete($id)
+ {
+ $this->findModel($id)->delete();
+
+ return $this->redirect(['index']);
+ }
+
+ /**
+ * Finds the ClusterAdmin model based on its primary key value.
+ * If the model is not found, a 404 HTTP exception will be thrown.
+ * @param int $id ID записи
+ * @return ClusterAdmin the loaded model
+ * @throws NotFoundHttpException if the model cannot be found
+ */
+ protected function findModel($id)
+ {
+ if (($model = ClusterAdmin::findOne(['id' => $id])) !== null) {
+ return $model;
+ }
+
+ throw new NotFoundHttpException('The requested page does not exist.');
+ }
+}
--- /dev/null
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Handles the creation of table `{{%cluster_admin}}`.
+ */
+class m241015_125203_create_cluster_admin_table extends Migration
+{
+ const TABLE_NAME = 'erp24.cluster_admin';
+ /**
+ * {@inheritdoc}
+ */
+ public function safeUp()
+ {
+ $this->createTable(self::TABLE_NAME, [
+ 'id' => $this->primaryKey(),
+ 'cluster_id' => $this->integer()->notNull()->comment('ID куста'),
+ 'admin_id' => $this->integer()->notNull()->comment('ID пользователя'),
+ 'date_start' => $this->date()->notNull()->comment('Дата начала привязки'),
+ 'date_end' => $this->date()->defaultValue('2100-01-01')->notNull()->comment('Дата окончания привязки'),
+ ]);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function safeDown()
+ {
+ $this->dropTable(self::TABLE_NAME);
+ }
+}
--- /dev/null
+<?php
+
+namespace yii_app\records;
+
+use Yii;
+
+/**
+ * This is the model class for table "cluster_admin".
+ *
+ * @property int $id
+ * @property int $cluster_id ID куста
+ * @property int $admin_id ID пользователя
+ * @property string $date_start Дата начала привязки
+ * @property string $date_end Дата окончания привязки
+ */
+class ClusterAdmin extends \yii\db\ActiveRecord
+{
+ /**
+ * {@inheritdoc}
+ */
+ public static function tableName()
+ {
+ return 'cluster_admin';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function rules()
+ {
+ return [
+ [['cluster_id', 'admin_id', 'date_start'], 'required'],
+ [['cluster_id', 'admin_id'], 'integer'],
+ [['date_start', 'date_end'], 'date', 'format' => 'php:Y-m-d'],
+ ['date_end', 'compare', 'compareAttribute' => 'date_start',
+ 'operator' => '>=', 'type' => 'date',
+ 'message' => 'Дата окончания должна быть больше или равна дате начала'],
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function attributeLabels()
+ {
+ return [
+ 'id' => 'ID записи',
+ 'cluster_id' => 'Куст',
+ 'admin_id' => 'Кустовой',
+ 'date_start' => 'Начало привязки',
+ 'date_end' => 'Окончание привязки',
+ ];
+ }
+
+ public function getCluster()
+ {
+ return $this->hasOne(Cluster::class, ['id' => 'cluster_id']);
+ }
+
+ public function getAdmin()
+ {
+ return $this->hasOne(Admin::class, ['id' => 'admin_id']);
+ }
+
+}
--- /dev/null
+<?php
+
+namespace yii_app\records;
+
+use yii\base\Model;
+use yii\data\ActiveDataProvider;
+use yii_app\records\ClusterAdmin;
+
+/**
+ * ClusterAdminSearch represents the model behind the search form of `yii_app\records\ClusterAdmin`.
+ */
+class ClusterAdminSearch extends ClusterAdmin
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function rules()
+ {
+ return [
+ [['id', 'cluster_id', 'admin_id'], 'integer'],
+ [['date_start', 'date_end'], 'safe'],
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function scenarios()
+ {
+ // bypass scenarios() implementation in the parent class
+ return Model::scenarios();
+ }
+
+ /**
+ * Creates data provider instance with search query applied
+ *
+ * @param array $params
+ *
+ * @return ActiveDataProvider
+ */
+ public function search($params)
+ {
+ $query = ClusterAdmin::find();
+
+ // add conditions that should always apply here
+
+ $dataProvider = new ActiveDataProvider([
+ 'query' => $query,
+ ]);
+
+ $this->load($params);
+
+ if (!$this->validate()) {
+ // uncomment the following line if you do not want to return any records when validation fails
+ // $query->where('0=1');
+ return $dataProvider;
+ }
+
+ // grid filtering conditions
+ $query->andFilterWhere([
+ 'id' => $this->id,
+ 'cluster_id' => $this->cluster_id,
+ 'admin_id' => $this->admin_id,
+ 'date_start' => $this->date_start,
+ 'date_end' => $this->date_end,
+ ]);
+
+ return $dataProvider;
+ }
+}
--- /dev/null
+<?php
+
+use kartik\select2\Select2;
+use yii\helpers\Html;
+use yii\widgets\ActiveForm;
+use yii_app\records\Admin;
+use yii_app\records\Cluster;
+
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\ClusterAdmin $model */
+/** @var yii\widgets\ActiveForm $form */
+/** @var array $clusters */
+/** @var array $admins */
+
+
+?>
+
+<div class="cluster-admin-form">
+
+
+ <?php if(Yii::$app->session->hasFlash('error')) { ?>
+ <div class="alert alert-danger" role="alert">
+ <?=Yii::$app->session->getFlash('error')?>
+ </div>
+ <?php } ?>
+<div class="col-6">
+ <?php $form = ActiveForm::begin(); ?>
+
+
+
+ <?= $form->field($model, 'cluster_id')->widget(Select2::class, [
+ 'data' => $clusters,
+ 'options' => ['placeholder' => 'Выберите куст...'],
+ 'pluginOptions' => [
+ 'allowClear' => true
+ ],
+ ]); ?>
+
+ <?= $form->field($model, 'admin_id')->widget(Select2::class, [
+ 'data' => $admins,
+ 'options' => ['placeholder' => 'Выберите кустового...'],
+ 'pluginOptions' => [
+ 'allowClear' => true
+ ],
+ ]); ?>
+
+ <?= $form->field($model, 'date_start')->input('date', [
+ 'value' => date('Y-m-d'),
+ ]) ?>
+
+ <?= $form->field($model, 'date_end')->input('date') ?>
+
+ <div class="form-group">
+ <?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
+ </div>
+
+ <?php ActiveForm::end(); ?>
+</div>
+</div>
--- /dev/null
+<?php
+
+use yii\helpers\Html;
+use yii\widgets\ActiveForm;
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\ClusterAdminSearch $model */
+/** @var yii\widgets\ActiveForm $form */
+?>
+
+<div class="cluster-admin-search">
+
+ <?php $form = ActiveForm::begin([
+ 'action' => ['index'],
+ 'method' => 'get',
+ ]); ?>
+
+ <?= $form->field($model, 'id') ?>
+
+ <?= $form->field($model, 'cluster_id') ?>
+
+ <?= $form->field($model, 'admin_id') ?>
+
+ <?= $form->field($model, 'date_start') ?>
+
+ <?= $form->field($model, 'date_end') ?>
+
+ <div class="form-group">
+ <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
+ <?= Html::resetButton('Reset', ['class' => 'btn btn-outline-secondary']) ?>
+ </div>
+
+ <?php ActiveForm::end(); ?>
+
+</div>
--- /dev/null
+<?php
+
+use yii\helpers\Html;
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\ClusterAdmin $model */
+/** @var array $clusters */
+/** @var array $admins */
+
+$this->title = 'Создание привязки Куста к Кустовому';
+$this->params['breadcrumbs'][] = ['label' => 'Cluster Admins', 'url' => ['index']];
+$this->params['breadcrumbs'][] = $this->title;
+?>
+<div class="cluster-admin-create p-4">
+
+ <h1><?= Html::encode($this->title) ?></h1>
+
+ <?= $this->render('_form', [
+ 'model' => $model,
+ 'clusters' => $clusters,
+ 'admins' => $admins,
+ ]) ?>
+
+</div>
--- /dev/null
+<?php
+
+use yii_app\records\ClusterAdmin;
+use yii\helpers\Html;
+use yii\helpers\Url;
+use yii\grid\ActionColumn;
+use yii\grid\GridView;
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\ClusterAdminSearch $searchModel */
+/** @var yii\data\ActiveDataProvider $dataProvider */
+
+$this->title = 'Привязка Куст-Кустовой';
+$this->params['breadcrumbs'][] = $this->title;
+?>
+<div class="cluster-admin-index p-4">
+
+ <h1><?= Html::encode($this->title) ?></h1>
+
+ <p>
+ <?= Html::a('Создать новую привязку', ['create'], ['class' => 'btn btn-success']) ?>
+ </p>
+
+ <?php // echo $this->render('_search', ['model' => $searchModel]); ?>
+
+ <?= GridView::widget([
+ 'dataProvider' => $dataProvider,
+ 'filterModel' => $searchModel,
+ 'columns' => [
+ ['class' => 'yii\grid\SerialColumn'],
+
+ 'id',
+ [
+ 'attribute' => 'cluster_id',
+ 'value' => function ($model) {
+ return $model->cluster ? $model->cluster->name . ' (' . $model->cluster_id . ')' : null;
+ },
+ ],
+ [
+ 'attribute' => 'admin_id',
+ 'value' => function ($model) {
+ return $model->admin ? $model->admin->name . ' (' . $model->admin_id . ')' : null;
+ },
+ ],
+ 'date_start',
+ 'date_end',
+ [
+ 'class' => ActionColumn::class,
+ 'urlCreator' => function ($action, ClusterAdmin $model, $key, $index, $column) {
+ return Url::toRoute([$action, 'id' => $model->id]);
+ }
+ ],
+ ],
+ ]); ?>
+
+
+</div>
--- /dev/null
+<?php
+
+use yii\helpers\Html;
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\ClusterAdmin $model */
+/** @var array $clusters */
+/** @var array $admins */
+
+$this->title = 'Редактирование привязки куста к кустовому: ' . $model->id;
+$this->params['breadcrumbs'][] = ['label' => 'Cluster Admins', 'url' => ['index']];
+$this->params['breadcrumbs'][] = ['label' => $model->id, 'url' => ['view', 'id' => $model->id]];
+$this->params['breadcrumbs'][] = 'Update';
+?>
+<div class="cluster-admin-update p-4">
+
+ <h1><?= Html::encode($this->title) ?></h1>
+
+ <?= $this->render('_form', [
+ 'model' => $model,
+ 'clusters' => $clusters,
+ 'admins' => $admins,
+ ]) ?>
+
+</div>
--- /dev/null
+<?php
+
+use yii\helpers\Html;
+use yii\widgets\DetailView;
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\ClusterAdmin $model */
+
+$this->title = "Привязка кустового к кусту №" . $model->id . " от " . $model->date_start;
+$this->params['breadcrumbs'][] = ['label' => 'Cluster Admins', 'url' => ['index']];
+$this->params['breadcrumbs'][] = $this->title;
+\yii\web\YiiAsset::register($this);
+?>
+<div class="cluster-admin-view p-4">
+
+ <h1><?= Html::encode($this->title) ?></h1>
+
+ <p>
+ <?= Html::a('Изменить', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
+ <?= Html::a('Удалить', ['delete', 'id' => $model->id], [
+ 'class' => 'btn btn-danger',
+ 'data' => [
+ 'confirm' => 'Вы уверены что хотите удалить запись?',
+ 'method' => 'post',
+ ],
+ ]) ?>
+ </p>
+
+ <?= DetailView::widget([
+ 'model' => $model,
+ 'attributes' => [
+ 'id',
+ [
+ 'attribute' => 'cluster_id',
+ 'value' => function ($model) {
+ return $model->cluster ? $model->cluster->name . ' (' . $model->cluster_id . ')' : null;
+ },
+ ],
+ [
+ 'attribute' => 'admin_id',
+ 'value' => function ($model) {
+ return $model->admin ? $model->admin->name . ' (' . $model->admin_id . ')' : null;
+ },
+ ],
+ 'date_start',
+ 'date_end',
+ ],
+ ]) ?>
+
+</div>