--- /dev/null
+<?php
+
+namespace yii_app\controllers\crud;
+
+use yii\behaviors\TimestampBehavior;
+use yii_app\records\Product1cReplacement;
+use yii_app\records\Product1cReplacementSearch;
+use yii\web\Controller;
+use yii\web\NotFoundHttpException;
+use yii\filters\VerbFilter;
+
+/**
+ * Product1CReplacementController implements the CRUD actions for Product1CReplacement model.
+ */
+class Product1cReplacementController extends Controller
+{
+ /**
+ * @inheritDoc
+ */
+ public function behaviors()
+ {
+ return array_merge(
+ parent::behaviors(),
+ [
+ 'verbs' => [
+ 'class' => VerbFilter::class,
+ 'actions' => [
+ 'delete' => ['POST'],
+ ],
+ ],
+ [
+ 'class' => TimestampBehavior::class,
+ 'createdAtAttribute' => 'created_at',
+ 'updatedAtAttribute' => 'updated_at',
+ 'value' => new \yii\db\Expression('NOW()'),
+ ],
+ ]
+ );
+ }
+
+ /**
+ * Lists all Product1CReplacement models.
+ *
+ * @return string
+ */
+ public function actionIndex()
+ {
+ $searchModel = new Product1cReplacementSearch();
+ $dataProvider = $searchModel->search($this->request->queryParams);
+
+ // Группируем данные по guid
+ $models = $dataProvider->query->all();
+ $groupedReplacements = [];
+ foreach ($models as $model) {
+ $groupedReplacements[$model->guid][] = $model->guid_replacement;
+ }
+
+ return $this->render('index', [
+ 'searchModel' => $searchModel,
+ 'dataProvider' => $dataProvider,
+ 'groupedReplacements' => $groupedReplacements,
+ ]);
+ }
+
+ /**
+ * Displays a single Product1CReplacement 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 Product1CReplacement model.
+ * If creation is successful, the browser will be redirected to the 'view' page.
+ * @return string|\yii\web\Response
+ */
+ public function actionCreate()
+ {
+ $model = new Product1cReplacement();
+
+ if ($this->request->isPost) {
+ if ($model->load($this->request->post()) && $model->save()) {
+ return $this->redirect(['view', 'id' => $model->id]);
+ }
+ } else {
+ $model->loadDefaultValues();
+ }
+
+ return $this->render('create', [
+ 'model' => $model,
+ ]);
+ }
+
+ /**
+ * Updates an existing Product1CReplacement 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 Product1CReplacement 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 Product1CReplacement 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 Product1cReplacement the loaded model
+ * @throws NotFoundHttpException if the model cannot be found
+ */
+ protected function findModel($id)
+ {
+ if (($model = Product1cReplacement::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 `{{%product1c-replacement}}`.
+ */
+class m241114_085742_create_product_1c_replacement_table extends Migration
+{
+
+ const TABLE_NAME = 'erp24.product_1c_replacement';
+ /**
+ * {@inheritdoc}
+ */
+ public function safeUp()
+ {
+ $this->createTable(self::TABLE_NAME, [
+ 'id' => $this->primaryKey(),
+ 'guid' => $this->string()->notNull(),
+ 'guid_replacement' => $this->string()->notNull(),
+ 'created_at' => $this->timestamp()->defaultExpression('CURRENT_TIMESTAMP'),
+ 'updated_at' => $this->timestamp()->defaultValue(null),
+ ]);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function safeDown()
+ {
+ $this->dropTable(self::TABLE_NAME);
+ }
+}
--- /dev/null
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Handles the creation of table `{{%product1c_replacement_log}}`.
+ */
+class m241114_085841_create_product_1c_replacement_log_table extends Migration
+{
+
+ const TABLE_NAME = 'erp24.product_1c_replacement_log';
+ /**
+ * {@inheritdoc}
+ */
+ public function safeUp()
+ {
+ $this->createTable(self::TABLE_NAME, [
+ 'id' => $this->primaryKey(),
+ 'replacement_id' => $this->integer()->notNull(),
+ 'state_before' => $this->string()->notNull(),
+ 'state_after' => $this->string()->notNull(),
+ 'date' => $this->timestamp()->defaultExpression('CURRENT_TIMESTAMP'),
+ 'admin_id' => $this->integer()->notNull(),
+ ]);
+
+ $this->addForeignKey(
+ 'fk-product_1c_replacement_log-replacement_id',
+ self::TABLE_NAME,
+ 'replacement_id',
+ '{{%erp24.product_1c_replacement}}',
+ 'id',
+ 'CASCADE'
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function safeDown()
+ {
+ $this->dropForeignKey('fk-product_1c_replacement_log-replacement_id', self::TABLE_NAME);
+ $this->dropTable(self::TABLE_NAME);
+ }
+}
--- /dev/null
+<?php
+
+namespace yii_app\records;
+
+use Yii;
+
+/**
+ * This is the model class for table "product_1c_replacement".
+ *
+ * @property int $id
+ * @property string $guid
+ * @property string $guid_replacement
+ * @property string|null $created_at
+ * @property string|null $updated_at
+ *
+ * @property Product1cReplacementLog[] $product1cReplacementLogs
+ */
+class Product1cReplacement extends \yii\db\ActiveRecord
+{
+ /**
+ * {@inheritdoc}
+ */
+ public static function tableName()
+ {
+ return 'product_1c_replacement';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function rules()
+ {
+ return [
+ [['guid', 'guid_replacement'], 'required'],
+ [['created_at', 'updated_at'], 'safe'],
+ [['guid', 'guid_replacement'], 'string', 'max' => 255],
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function attributeLabels()
+ {
+ return [
+ 'id' => 'ID',
+ 'guid' => 'Guid',
+ 'guid_replacement' => 'Guid Replacement',
+ 'created_at' => 'Created At',
+ 'updated_at' => 'Updated At',
+ ];
+ }
+
+ /**
+ * Gets query for [[Product1cReplacementLogs]].
+ *
+ * @return \yii\db\ActiveQuery
+ */
+ public function getProduct1CReplacementLogs()
+ {
+ return $this->hasMany(Product1cReplacementLog::class, ['replacement_id' => 'id']);
+ }
+
+ public function afterSave($insert, $changedAttributes)
+ {
+ parent::afterSave($insert, $changedAttributes);
+
+ if (!$insert) {
+
+ $log = new Product1cReplacementLog();
+ $log->replacement_id = $this->id;
+ $log->state_before = $changedAttributes['guid_replacement'] ?? null;
+ $log->state_after = $this->guid_replacement;
+ $log->admin_id = Yii::$app->user->id;
+ $log->save();
+ }
+ }
+}
--- /dev/null
+<?php
+
+namespace yii_app\records;
+
+use Yii;
+
+/**
+ * This is the model class for table "product_1c_replacement_log".
+ *
+ * @property int $id
+ * @property int $replacement_id
+ * @property string $state_before
+ * @property string $state_after
+ * @property string|null $date
+ * @property int $admin_id
+ *
+ * @property Product1cReplacementLog $replacement
+ */
+class Product1cReplacementLog extends \yii\db\ActiveRecord
+{
+ /**
+ * {@inheritdoc}
+ */
+ public static function tableName()
+ {
+ return 'product_1c_replacement_log';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function rules()
+ {
+ return [
+ [['replacement_id', 'state_before', 'state_after', 'admin_id'], 'required'],
+ [['replacement_id', 'admin_id'], 'default', 'value' => null],
+ [['replacement_id', 'admin_id'], 'integer'],
+ [['date'], 'safe'],
+ [['state_before', 'state_after'], 'string', 'max' => 255],
+ [['replacement_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product1cReplacement::class, 'targetAttribute' => ['replacement_id' => 'id']],
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function attributeLabels()
+ {
+ return [
+ 'id' => 'ID',
+ 'replacement_id' => 'Replacement ID',
+ 'state_before' => 'State Before',
+ 'state_after' => 'State After',
+ 'date' => 'Date',
+ 'admin_id' => 'Admin ID',
+ ];
+ }
+
+ /**
+ * Gets query for [[Replacement]].
+ *
+ * @return \yii\db\ActiveQuery
+ */
+ public function getReplacement()
+ {
+ return $this->hasOne(Product1cReplacement::class, ['id' => 'replacement_id']);
+ }
+}
--- /dev/null
+<?php
+
+namespace yii_app\records;
+
+use yii\base\Model;
+use yii\data\ActiveDataProvider;
+use yii_app\records\Product1cReplacement;
+
+/**
+ * Product1CReplacementSearch represents the model behind the search form of `yii_app\records\Product1CReplacement`.
+ */
+class Product1cReplacementSearch extends Product1cReplacement
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function rules()
+ {
+ return [
+ [['id'], 'integer'],
+ [['guid', 'guid_replacement', 'created_at', 'updated_at'], '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 = Product1cReplacement::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,
+ 'created_at' => $this->created_at,
+ 'updated_at' => $this->updated_at,
+ ]);
+
+ $query->andFilterWhere(['ilike', 'guid', $this->guid])
+ ->andFilterWhere(['ilike', 'guid_replacement', $this->guid_replacement]);
+
+ return $dataProvider;
+ }
+}
--- /dev/null
+<?php
+
+use yii\helpers\Html;
+use yii\widgets\ActiveForm;
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\Product1cReplacement $model */
+/** @var yii\widgets\ActiveForm $form */
+?>
+
+<div class="product1-creplacement-form">
+
+ <?php $form = ActiveForm::begin(); ?>
+
+ <?= $form->field($model, 'guid')->textInput(['maxlength' => true]) ?>
+
+ <?= $form->field($model, 'guid_replacement')->textInput(['maxlength' => true]) ?>
+
+ <?= $form->field($model, 'created_at')->textInput() ?>
+
+ <?= $form->field($model, 'updated_at')->textInput() ?>
+
+ <div class="form-group">
+ <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
+ </div>
+
+ <?php ActiveForm::end(); ?>
+
+</div>
--- /dev/null
+<?php
+
+use yii\helpers\Html;
+use yii\widgets\ActiveForm;
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\Product1cReplacementSearch $model */
+/** @var yii\widgets\ActiveForm $form */
+?>
+
+<div class="product1-creplacement-search">
+
+ <?php $form = ActiveForm::begin([
+ 'action' => ['index'],
+ 'method' => 'get',
+ 'options' => [
+ 'data-pjax' => 1
+ ],
+ ]); ?>
+
+ <?= $form->field($model, 'id') ?>
+
+ <?= $form->field($model, 'guid') ?>
+
+ <?= $form->field($model, 'guid_replacement') ?>
+
+ <?= $form->field($model, 'created_at') ?>
+
+ <?= $form->field($model, 'updated_at') ?>
+
+ <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\Product1cReplacement $model */
+
+$this->title = 'Create Product1c Replacement';
+$this->params['breadcrumbs'][] = ['label' => 'Product1c Replacements', 'url' => ['index']];
+$this->params['breadcrumbs'][] = $this->title;
+?>
+<div class="product1-creplacement-create">
+
+ <h1><?= Html::encode($this->title) ?></h1>
+
+ <?= $this->render('_form', [
+ 'model' => $model,
+ ]) ?>
+
+</div>
--- /dev/null
+<?php
+
+use yii\helpers\Html;
+use yii\grid\GridView;
+use yii\widgets\Pjax;
+
+/* @var $this yii\web\View */
+/* @var $searchModel app\models\Product1CReplacementSearch */
+/* @var $dataProvider yii\data\ActiveDataProvider */
+/* @var $groupedReplacements array */
+
+$this->title = 'Возможные замены номенклатуры';
+$this->params['breadcrumbs'][] = $this->title;
+?>
+<div class="product1c-replacement-index p-4">
+
+ <h1><?= Html::encode($this->title) ?></h1>
+
+ <p>
+ <?= Html::a('Создать замену', ['create'], ['class' => 'btn btn-success']) ?>
+ </p>
+
+ <?php Pjax::begin(); ?>
+
+ <?= GridView::widget([
+ 'dataProvider' => $dataProvider,
+ 'filterModel' => $searchModel,
+ 'columns' => [
+ [
+ 'attribute' => 'guid',
+ 'label' => 'GUID, который заменяют',
+ 'value' => function ($model) {
+ return Html::encode($model->guid);
+ },
+ 'format' => 'raw',
+ ],
+ [
+ 'label' => 'GUIDы замены',
+ 'value' => function ($model) use ($groupedReplacements) {
+
+ $guidReplacements = $groupedReplacements[$model->guid] ?? [];
+ if (!empty($guidReplacements)) {
+ $listItems = '<ul>';
+ foreach ($guidReplacements as $replacementGuid) {
+ $listItems .= '<li>' . Html::encode($replacementGuid) . '</li>';
+ }
+ $listItems .= '</ul>';
+ return $listItems;
+ }
+ return null;
+ },
+ 'format' => 'raw',
+ ],
+ ],
+ ]); ?>
+
+ <?php Pjax::end(); ?>
+
+</div>
\ No newline at end of file
--- /dev/null
+<?php
+
+use yii\helpers\Html;
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\Product1cReplacement $model */
+
+$this->title = 'Update Product1c Replacement: ' . $model->id;
+$this->params['breadcrumbs'][] = ['label' => 'Product1c Replacements', 'url' => ['index']];
+$this->params['breadcrumbs'][] = ['label' => $model->id, 'url' => ['view', 'id' => $model->id]];
+$this->params['breadcrumbs'][] = 'Update';
+?>
+<div class="product1-creplacement-update">
+
+ <h1><?= Html::encode($this->title) ?></h1>
+
+ <?= $this->render('_form', [
+ 'model' => $model,
+ ]) ?>
+
+</div>
--- /dev/null
+<?php
+
+use yii\helpers\Html;
+use yii\widgets\DetailView;
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\Product1cReplacement $model */
+
+$this->title = $model->id;
+$this->params['breadcrumbs'][] = ['label' => 'Product1c Replacements', 'url' => ['index']];
+$this->params['breadcrumbs'][] = $this->title;
+\yii\web\YiiAsset::register($this);
+?>
+<div class="product1-creplacement-view">
+
+ <h1><?= Html::encode($this->title) ?></h1>
+
+ <p>
+ <?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
+ <?= Html::a('Delete', ['delete', 'id' => $model->id], [
+ 'class' => 'btn btn-danger',
+ 'data' => [
+ 'confirm' => 'Are you sure you want to delete this item?',
+ 'method' => 'post',
+ ],
+ ]) ?>
+ </p>
+
+ <?= DetailView::widget([
+ 'model' => $model,
+ 'attributes' => [
+ 'id',
+ 'guid',
+ 'guid_replacement',
+ 'created_at',
+ 'updated_at',
+ ],
+ ]) ?>
+
+</div>