From: fomichev Date: Mon, 3 Feb 2025 14:02:16 +0000 (+0300) Subject: Таблица уровней X-Git-Tag: 1.7~16^2~9 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=06a0b3f3280ea0c303fc3fecf32f1ffdf52f6882;p=erp24_rep%2Fyii-erp24%2F.git Таблица уровней --- diff --git a/erp24/controllers/BonusLevelsController.php b/erp24/controllers/BonusLevelsController.php new file mode 100644 index 00000000..f609073d --- /dev/null +++ b/erp24/controllers/BonusLevelsController.php @@ -0,0 +1,202 @@ + [ + 'class' => VerbFilter::class, + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ] + ); + } + + /** + * Lists all BonusLevels models. + * + * @return string + */ + public function actionIndex() + { + $dataProvider = new ActiveDataProvider([ + 'query' => BonusLevels::find()->where(['active' => 1]), + /* + 'pagination' => [ + 'pageSize' => 50 + ], + 'sort' => [ + 'defaultOrder' => [ + 'id' => SORT_DESC, + ] + ], + */ + ]); + + return $this->render('index', [ + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single BonusLevels 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 BonusLevels model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return string|\yii\web\Response + */ + public function actionCreate() + { + $model = new BonusLevels(); + + if ($this->request->isPost) { + $model->load($this->request->post()); + $model->created_by = Yii::$app->user->id; + $model->date_start = date('Y-m-d'); + + if (BonusLevels::find()->where(['name' => $model->name])->exists()) { + Yii::$app->session->setFlash('error', 'Запись с таким именем уже существует.'); + } else if ($model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } + } else { + $model->loadDefaultValues(); + } + + return $this->render('create', [ + 'model' => $model, + ]); + } + + public function actionHistory() + { + $bonusLevels = BonusLevels::find() + ->orderBy(['name' => SORT_ASC, 'date_start' => SORT_ASC, 'id' => SORT_ASC]) + ->all(); + + $groupedData = []; + foreach ($bonusLevels as $level) { + $groupedData[$level->name][] = $level; + } + + return $this->render('history', [ + 'groupedData' => $groupedData, + ]); + } + + + /** + * Updates an existing BonusLevels 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); + $oldAttributes = $model->getOldAttributes(); + $userId = Yii::$app->user->id; + + if ($this->request->isPost && $model->load($this->request->post())) { + $changedFields = ['cashback_rate', 'referal_rate', 'bonus_rate']; + $isChanged = false; + + foreach ($changedFields as $field) { + if ($oldAttributes[$field] != $model->$field) { + $isChanged = true; + break; + } + } + + if ($isChanged) { + // Деактивация старой записи + $model->active = 0; + $model->date_end = date('Y-m-d'); + $model->updated_by = $userId; + $model->save(false); + + // Создание новой записи + $newModel = new BonusLevels(); + $newModel->attributes = $oldAttributes; + $newModel->cashback_rate = $model->cashback_rate; + $newModel->referal_rate = $model->referal_rate; + $newModel->bonus_rate = $model->bonus_rate; + $newModel->active = 1; + $newModel->created_by = $userId; + $newModel->date_start = date('Y-m-d'); + $newModel->date_end = null; + + if ($newModel->save()) { + return $this->redirect(['view', 'id' => $newModel->id]); + } + } + } + + return $this->render('update', [ + 'model' => $model, + ]); + } + + + /** + * Deletes an existing BonusLevels 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 BonusLevels 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 BonusLevels the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = BonusLevels::findOne(['id' => $id])) !== null) { + return $model; + } + + throw new NotFoundHttpException('The requested page does not exist.'); + } +} diff --git a/erp24/migrations/m250203_113221_create_bonus_levels_table.php b/erp24/migrations/m250203_113221_create_bonus_levels_table.php new file mode 100644 index 00000000..ba4bcc3a --- /dev/null +++ b/erp24/migrations/m250203_113221_create_bonus_levels_table.php @@ -0,0 +1,52 @@ +db->getTableSchema(self::TABLE_NAME); + + if (!isset($tableSchema)) { + $this->createTable(self::TABLE_NAME, [ + 'id' => $this->primaryKey()->comment('ID'), + 'name' => $this->string()->notNull()->comment('Наименование уровня'), + 'alias' => $this->string()->notNull()->comment('Алиас уровня'), + 'threshold' => $this->integer()->notNull()->defaultValue(0) + ->comment('Пороговое значение суммы покупок'), + 'cashback_rate' => $this->integer()->notNull()->defaultValue(0) + ->comment('Процент начисления кешбека'), + 'referal_rate' => $this->integer()->notNull()->defaultValue(0) + ->comment('Процент начисления рефералу'), + 'bonus_rate' => $this->integer()->notNull()->defaultValue(0) + ->comment('Процент списания бонусов'), + 'active' => $this->tinyInteger(1)->notNull()->defaultValue(1) + ->comment('Активность записи'), + 'date_start' => $this->date()->notNull()->comment('Дата создания записи'), + 'date_end' => $this->date()->null()->comment('Дата закрытия записи'), + 'created_by' => $this->integer()->notNull()->comment('ID создавшего запись'), + 'updated_by' => $this->integer()->null()->comment('ID закрывшего запись'), + ]); + } + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $tableSchema = $this->db->getTableSchema(self::TABLE_NAME); + + if (isset($tableSchema)) { + $this->dropTable(self::TABLE_NAME); + } + } +} diff --git a/erp24/migrations/m250203_113244_create_users_bonus_levels_table.php b/erp24/migrations/m250203_113244_create_users_bonus_levels_table.php new file mode 100644 index 00000000..532846ab --- /dev/null +++ b/erp24/migrations/m250203_113244_create_users_bonus_levels_table.php @@ -0,0 +1,45 @@ +db->getTableSchema(self::TABLE_NAME); + + if (!isset($tableSchema)) { + $this->createTable(self::TABLE_NAME, [ + 'id' => $this->primaryKey()->comment('ID'), + 'phone' => $this->string()->notNull()->comment('Телефон клиента'), + 'user_id' => $this->integer()->notNull()->comment('ID клиента'), + 'bonus_level' => $this->string()->notNull()->comment('Уровань клиента в БС'), + 'created_at' => $this->timestamp() + ->defaultExpression('CURRENT_TIMESTAMP') + ->notNull()->comment('Дата создания'), + 'active' => $this->tinyInteger(1) + ->notNull()->defaultValue(1)->comment('Активность записи'), + 'updated_at' => $this->timestamp()->null()->comment('Дата изменения'), + ]); + } + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $tableSchema = $this->db->getTableSchema(self::TABLE_NAME); + + if (isset($tableSchema)) { + $this->dropTable(self::TABLE_NAME); + } + } +} diff --git a/erp24/records/BonusLevels.php b/erp24/records/BonusLevels.php new file mode 100644 index 00000000..b65e0683 --- /dev/null +++ b/erp24/records/BonusLevels.php @@ -0,0 +1,78 @@ + null], + [['threshold', 'cashback_rate', 'referal_rate', 'bonus_rate', 'active', 'created_by', 'updated_by'], 'integer'], + [['date_start', 'date_end'], 'safe'], + [['name', 'alias'], 'string', 'max' => 255], + + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'name' => 'Наименование уровня', + 'alias' => 'Алиас уровня', + 'threshold' => 'Пороговое значение суммы покупок', + 'cashback_rate' => 'Процент начисления кешбека', + 'referal_rate' => 'Процент начисления рефералу', + 'bonus_rate' => 'Процент списания бонусов', + 'active' => 'Активность записи', + 'date_start' => 'Дата создания записи', + 'date_end' => 'Дата закрытия записи', + 'created_by' => 'ID создавшего запись', + 'updated_by' => 'ID закрывшего запись', + ]; + } + + public function getCreatedBy() + { + return $this->hasOne(Admin::class, ['id' => 'created_by']); + } + + public function getUpdatedBy() + { + return $this->hasOne(Admin::class, ['id' => 'updated_by']); + } +} diff --git a/erp24/records/UsersBonusLevels.php b/erp24/records/UsersBonusLevels.php new file mode 100644 index 00000000..00dad293 --- /dev/null +++ b/erp24/records/UsersBonusLevels.php @@ -0,0 +1,57 @@ + null], + [['user_id', 'active'], 'integer'], + [['created_at', 'updated_at'], 'safe'], + [['phone', 'bonus_level'], 'string', 'max' => 255], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'phone' => 'Телефон клиента', + 'user_id' => 'ID клиента', + 'bonus_level' => 'Уровань клиента в БС', + 'created_at' => 'Дата создания', + 'active' => 'Активность записи', + 'updated_at' => 'Дата изменения', + ]; + } +} diff --git a/erp24/views/bonus-levels/_form.php b/erp24/views/bonus-levels/_form.php new file mode 100644 index 00000000..fa2db670 --- /dev/null +++ b/erp24/views/bonus-levels/_form.php @@ -0,0 +1,39 @@ + + +
+ + + + field($model, 'name')->textInput(['maxlength' => true]) ?> + + field($model, 'alias')->textInput(['maxlength' => true]) ?> + + field($model, 'threshold')->textInput() ?> + + field($model, 'cashback_rate')->textInput() ?> + + field($model, 'referal_rate')->textInput() ?> + + field($model, 'bonus_rate')->textInput() ?> + + field($model, 'created_by')->hiddenInput(['value' => Yii::$app->user->id])->label(false) ?> + field($model, 'date_start')->hiddenInput(['value' => date('Y-m-d')])->label(false) ?> + field($model, 'active')->hiddenInput(['value' => 1])->label(false) ?> + field($model, 'date_end')->hiddenInput()->label(false) ?> + + +
+ 'btn btn-success']) ?> +
+ + + +
diff --git a/erp24/views/bonus-levels/create.php b/erp24/views/bonus-levels/create.php new file mode 100644 index 00000000..d4d67b31 --- /dev/null +++ b/erp24/views/bonus-levels/create.php @@ -0,0 +1,20 @@ +title = 'Создание бонусного уровня'; +$this->params['breadcrumbs'][] = ['label' => 'Bonus Levels', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ 'btn btn-primary mb-3']) ?> +

title) ?>

+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/erp24/views/bonus-levels/history.php b/erp24/views/bonus-levels/history.php new file mode 100644 index 00000000..1f57ee3a --- /dev/null +++ b/erp24/views/bonus-levels/history.php @@ -0,0 +1,48 @@ + + +
+ 'btn btn-primary mb-3']) ?> +

История изменений уровней

+ + + + $levels): ?> + +

Уровень:

+ new yii\data\ArrayDataProvider([ + 'allModels' => $levels, + 'pagination' => false, + ]), + 'rowOptions' => function ($model) use ($backgroundColor) { + return ['style' => 'background-color: ' . $backgroundColor]; + }, + 'columns' => [ + 'id', + 'cashback_rate', + 'referal_rate', + 'bonus_rate', + 'date_start', + 'date_end', + [ + 'attribute' => 'active', + 'format' => 'raw', + 'value' => function ($model) { + return Html::tag('span', $model->active ? 'Активен' : 'Не активен', [ + 'style' => 'color: ' . ($model->active ? 'green' : 'red') . '; font-weight: bold;' + ]); + }, + ], + ], + ]); ?> + + +
+ diff --git a/erp24/views/bonus-levels/index.php b/erp24/views/bonus-levels/index.php new file mode 100644 index 00000000..80ca6507 --- /dev/null +++ b/erp24/views/bonus-levels/index.php @@ -0,0 +1,58 @@ +title = 'Уровни бонусной системы'; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ +

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

+ + + $dataProvider, + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], + + 'id', + 'name', + 'alias', + 'threshold', + 'cashback_rate', + 'referal_rate', + 'bonus_rate', + //'active', + //'date_start', + //'date_end', + [ + 'attribute' => 'created_by', + 'value' => function ($model) { + return $model->createdBy->name ?? 'Неизвестно'; + }, + ], + //'updated_by', + [ + 'class' => ActionColumn::class, + 'template' => '{view} {update}', + 'urlCreator' => function ($action, BonusLevels $model, $key, $index, $column) { + return Url::toRoute([$action, 'id' => $model->id]); + } + ], + ], + ]); ?> + + +
diff --git a/erp24/views/bonus-levels/update.php b/erp24/views/bonus-levels/update.php new file mode 100644 index 00000000..9fe7e344 --- /dev/null +++ b/erp24/views/bonus-levels/update.php @@ -0,0 +1,21 @@ +title = 'Update Bonus Levels: ' . $model->name; +$this->params['breadcrumbs'][] = ['label' => 'Bonus Levels', 'url' => ['index']]; +$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]]; +$this->params['breadcrumbs'][] = 'Update'; +?> +
+ 'btn btn-primary mb-3']) ?> +

title) ?>

+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/erp24/views/bonus-levels/view.php b/erp24/views/bonus-levels/view.php new file mode 100644 index 00000000..94fcae4d --- /dev/null +++ b/erp24/views/bonus-levels/view.php @@ -0,0 +1,59 @@ +title = $model->name; +$this->params['breadcrumbs'][] = ['label' => 'Bonus Levels', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +\yii\web\YiiAsset::register($this); +?> +
+ 'btn btn-primary mb-3']) ?> +

title) ?>

+ +

+ $model->id], ['class' => 'btn btn-primary']) ?> + +

+ + $model, + 'attributes' => [ + 'id', + 'name', + 'alias', + 'threshold', + 'cashback_rate', + 'referal_rate', + 'bonus_rate', + [ + 'attribute' => 'active', + 'format' => 'raw', + 'value' => function ($model) { + return Html::tag('span', $model->active ? 'Активен' : 'Не активен', [ + 'style' => 'color: ' . ($model->active ? 'green' : 'red') . '; font-weight: bold;', + ]); + }, + ], + 'date_start', + 'date_end', + [ + 'attribute' => 'created_by', + 'value' => function ($model) { + return $model->createdBy->name ?? 'Неизвестно'; + }, + ], + [ + 'attribute' => 'updated_by', + 'value' => function ($model) { + return $model->updatedBy->name ?? 'Неизвестно'; + }, + ], + ], + ]) ?> + +