From: fomichev Date: Thu, 14 Nov 2024 10:51:53 +0000 (+0300) Subject: Создание миграций моделей контроллера и index view X-Git-Tag: 1.7~226^2~11 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=db0295c0dc11d0702146a75a3d957424acce85be;p=erp24_rep%2Fyii-erp24%2F.git Создание миграций моделей контроллера и index view --- diff --git a/erp24/controllers/crud/Product1cReplacementController.php b/erp24/controllers/crud/Product1cReplacementController.php new file mode 100644 index 00000000..d9bf05e3 --- /dev/null +++ b/erp24/controllers/crud/Product1cReplacementController.php @@ -0,0 +1,149 @@ + [ + '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.'); + } +} diff --git a/erp24/migrations/m241114_085742_create_product_1c_replacement_table.php b/erp24/migrations/m241114_085742_create_product_1c_replacement_table.php new file mode 100644 index 00000000..8c2a324b --- /dev/null +++ b/erp24/migrations/m241114_085742_create_product_1c_replacement_table.php @@ -0,0 +1,33 @@ +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); + } +} diff --git a/erp24/migrations/m241114_085841_create_product_1c_replacement_log_table.php b/erp24/migrations/m241114_085841_create_product_1c_replacement_log_table.php new file mode 100644 index 00000000..4739d5dc --- /dev/null +++ b/erp24/migrations/m241114_085841_create_product_1c_replacement_log_table.php @@ -0,0 +1,44 @@ +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); + } +} diff --git a/erp24/records/Product1cReplacement.php b/erp24/records/Product1cReplacement.php new file mode 100644 index 00000000..57e5889d --- /dev/null +++ b/erp24/records/Product1cReplacement.php @@ -0,0 +1,78 @@ + 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(); + } + } +} diff --git a/erp24/records/Product1cReplacementLog.php b/erp24/records/Product1cReplacementLog.php new file mode 100644 index 00000000..4e242642 --- /dev/null +++ b/erp24/records/Product1cReplacementLog.php @@ -0,0 +1,68 @@ + 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']); + } +} diff --git a/erp24/records/Product1cReplacementSearch.php b/erp24/records/Product1cReplacementSearch.php new file mode 100644 index 00000000..abdcd51a --- /dev/null +++ b/erp24/records/Product1cReplacementSearch.php @@ -0,0 +1,71 @@ + $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; + } +} diff --git a/erp24/views/crud/product1c-replacement/_form.php b/erp24/views/crud/product1c-replacement/_form.php new file mode 100644 index 00000000..35fd0f1b --- /dev/null +++ b/erp24/views/crud/product1c-replacement/_form.php @@ -0,0 +1,29 @@ + + +
+ + + + field($model, 'guid')->textInput(['maxlength' => true]) ?> + + field($model, 'guid_replacement')->textInput(['maxlength' => true]) ?> + + field($model, 'created_at')->textInput() ?> + + field($model, 'updated_at')->textInput() ?> + +
+ 'btn btn-success']) ?> +
+ + + +
diff --git a/erp24/views/crud/product1c-replacement/_search.php b/erp24/views/crud/product1c-replacement/_search.php new file mode 100644 index 00000000..963c228c --- /dev/null +++ b/erp24/views/crud/product1c-replacement/_search.php @@ -0,0 +1,38 @@ + + + diff --git a/erp24/views/crud/product1c-replacement/create.php b/erp24/views/crud/product1c-replacement/create.php new file mode 100644 index 00000000..6511ef70 --- /dev/null +++ b/erp24/views/crud/product1c-replacement/create.php @@ -0,0 +1,20 @@ +title = 'Create Product1c Replacement'; +$this->params['breadcrumbs'][] = ['label' => 'Product1c Replacements', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/erp24/views/crud/product1c-replacement/index.php b/erp24/views/crud/product1c-replacement/index.php new file mode 100644 index 00000000..b04c6216 --- /dev/null +++ b/erp24/views/crud/product1c-replacement/index.php @@ -0,0 +1,59 @@ +title = 'Возможные замены номенклатуры'; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ +

+ 'btn btn-success']) ?> +

+ + + + $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 = ''; + return $listItems; + } + return null; + }, + 'format' => 'raw', + ], + ], + ]); ?> + + + +
\ No newline at end of file diff --git a/erp24/views/crud/product1c-replacement/update.php b/erp24/views/crud/product1c-replacement/update.php new file mode 100644 index 00000000..c73b731d --- /dev/null +++ b/erp24/views/crud/product1c-replacement/update.php @@ -0,0 +1,21 @@ +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'; +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/erp24/views/crud/product1c-replacement/view.php b/erp24/views/crud/product1c-replacement/view.php new file mode 100644 index 00000000..45331245 --- /dev/null +++ b/erp24/views/crud/product1c-replacement/view.php @@ -0,0 +1,40 @@ +title = $model->id; +$this->params['breadcrumbs'][] = ['label' => 'Product1c Replacements', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +\yii\web\YiiAsset::register($this); +?> +
+ +

title) ?>

+ +

+ $model->id], ['class' => 'btn btn-primary']) ?> + $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +

+ + $model, + 'attributes' => [ + 'id', + 'guid', + 'guid_replacement', + 'created_at', + 'updated_at', + ], + ]) ?> + +