--- /dev/null
+<?php
+
+namespace app\controllers;
+
+use Yii;
+use yii_app\records\BonusLevels;
+use yii\data\ActiveDataProvider;
+use yii\web\Controller;
+use yii\web\NotFoundHttpException;
+use yii\filters\VerbFilter;
+
+/**
+ * BonusLevelsController implements the CRUD actions for BonusLevels model.
+ */
+class BonusLevelsController extends Controller
+{
+ /**
+ * @inheritDoc
+ */
+ public function behaviors()
+ {
+ return array_merge(
+ parent::behaviors(),
+ [
+ 'verbs' => [
+ '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.');
+ }
+}
--- /dev/null
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Handles the creation of table `{{%bonus_levels}}`.
+ */
+class m250203_113221_create_bonus_levels_table extends Migration
+{
+ const TABLE_NAME = 'erp24.bonus_levels';
+ /**
+ * {@inheritdoc}
+ */
+ public function safeUp()
+ {
+ $tableSchema = $this->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);
+ }
+ }
+}
--- /dev/null
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Handles the creation of table `{{%users_bonus_levels}}`.
+ */
+class m250203_113244_create_users_bonus_levels_table extends Migration
+{
+ const TABLE_NAME = 'erp24.users_bonus_levels';
+ /**
+ * {@inheritdoc}
+ */
+ public function safeUp()
+ {
+ $tableSchema = $this->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);
+ }
+ }
+}
--- /dev/null
+<?php
+
+namespace yii_app\records;
+
+use Yii;
+
+/**
+ * This is the model class for table "bonus_levels".
+ *
+ * @property int $id ID
+ * @property string $name Наименование уровня
+ * @property string $alias Алиас уровня
+ * @property int $threshold Пороговое значение суммы покупок
+ * @property int $cashback_rate Процент начисления кешбека
+ * @property int $referal_rate Процент начисления рефералу
+ * @property int $bonus_rate Процент списания бонусов
+ * @property int $active Активность записи
+ * @property string $date_start Дата создания записи
+ * @property string|null $date_end Дата закрытия записи
+ * @property int $created_by ID создавшего запись
+ * @property int|null $updated_by ID закрывшего запись
+ */
+class BonusLevels extends \yii\db\ActiveRecord
+{
+ /**
+ * {@inheritdoc}
+ */
+ public static function tableName()
+ {
+ return 'bonus_levels';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function rules()
+ {
+ return [
+ [['name', 'alias', 'date_start', 'created_by'], 'required'],
+ [['threshold', 'cashback_rate', 'referal_rate', 'bonus_rate', 'active', 'created_by', 'updated_by'], 'default', 'value' => 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']);
+ }
+}
--- /dev/null
+<?php
+
+namespace yii_app\records;
+
+use Yii;
+
+/**
+ * This is the model class for table "users_bonus_levels".
+ *
+ * @property int $id ID
+ * @property string $phone Телефон клиента
+ * @property int $user_id ID клиента
+ * @property string $bonus_level Уровань клиента в БС
+ * @property string $created_at Дата создания
+ * @property int $active Активность записи
+ * @property string|null $updated_at Дата изменения
+ */
+class UsersBonusLevels extends \yii\db\ActiveRecord
+{
+ /**
+ * {@inheritdoc}
+ */
+ public static function tableName()
+ {
+ return 'users_bonus_levels';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function rules()
+ {
+ return [
+ [['phone', 'user_id', 'bonus_level'], 'required'],
+ [['user_id', 'active'], 'default', 'value' => 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' => 'Дата изменения',
+ ];
+ }
+}
--- /dev/null
+<?php
+
+use yii\helpers\Html;
+use yii\widgets\ActiveForm;
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\BonusLevels $model */
+/** @var yii\widgets\ActiveForm $form */
+?>
+
+<div class="bonus-levels-form col-4">
+
+ <?php $form = ActiveForm::begin(); ?>
+
+ <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
+
+ <?= $form->field($model, 'alias')->textInput(['maxlength' => true]) ?>
+
+ <?= $form->field($model, 'threshold')->textInput() ?>
+
+ <?= $form->field($model, 'cashback_rate')->textInput() ?>
+
+ <?= $form->field($model, 'referal_rate')->textInput() ?>
+
+ <?= $form->field($model, 'bonus_rate')->textInput() ?>
+
+ <?= $form->field($model, 'created_by')->hiddenInput(['value' => Yii::$app->user->id])->label(false) ?>
+ <?= $form->field($model, 'date_start')->hiddenInput(['value' => date('Y-m-d')])->label(false) ?>
+ <?= $form->field($model, 'active')->hiddenInput(['value' => 1])->label(false) ?>
+ <?= $form->field($model, 'date_end')->hiddenInput()->label(false) ?>
+
+
+ <div class="form-group">
+ <?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
+ </div>
+
+ <?php ActiveForm::end(); ?>
+
+</div>
--- /dev/null
+<?php
+
+use yii\helpers\Html;
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\BonusLevels $model */
+
+$this->title = 'Создание бонусного уровня';
+$this->params['breadcrumbs'][] = ['label' => 'Bonus Levels', 'url' => ['index']];
+$this->params['breadcrumbs'][] = $this->title;
+?>
+<div class="bonus-levels-create p-4">
+ <?= Html::a('Назад', ['index'], ['class' => 'btn btn-primary mb-3']) ?>
+ <h1><?= Html::encode($this->title) ?></h1>
+
+ <?= $this->render('_form', [
+ 'model' => $model,
+ ]) ?>
+
+</div>
--- /dev/null
+<?php
+use yii\grid\GridView;
+use yii\helpers\Html;
+/** @var yii\web\View $this */
+/** @var array $groupedData */
+?>
+
+<div class="bonus-levels-history p-4">
+ <?= Html::a('Назад', ['index'], ['class' => 'btn btn-primary mb-3']) ?>
+ <h1>История изменений уровней</h1>
+
+ <?php $colors = ['#f9f9f9', '#e6f7ff', '#fffbe6', '#e6ffe6', '#ffe6f7'];
+ $colorIndex = 0;
+ ?>
+
+ <?php foreach ($groupedData as $name => $levels): ?>
+ <?php $backgroundColor = $colors[$colorIndex % count($colors)]; ?>
+ <h2 style="background-color: <?= $backgroundColor ?>; padding: 10px;">Уровень: <?= Html::encode($name) ?></h2>
+ <?= GridView::widget([
+ 'dataProvider' => 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;'
+ ]);
+ },
+ ],
+ ],
+ ]); ?>
+ <?php $colorIndex++; ?>
+ <?php endforeach; ?>
+</div>
+
--- /dev/null
+<?php
+
+use yii_app\records\BonusLevels;
+use yii\helpers\Html;
+use yii\helpers\Url;
+use yii\grid\ActionColumn;
+use yii\grid\GridView;
+
+/** @var yii\web\View $this */
+/** @var yii\data\ActiveDataProvider $dataProvider */
+
+$this->title = 'Уровни бонусной системы';
+$this->params['breadcrumbs'][] = $this->title;
+?>
+<div class="bonus-levels-index p-4">
+
+ <h1><?= Html::encode($this->title) ?></h1>
+
+ <p>
+ <?= Html::a('Создать бонусный уровень', ['create'], ['class' => 'btn btn-success']) ?>
+ <?= Html::a('Посмотреть историю', ['history'], ['class' => 'btn btn-success']) ?>
+ </p>
+
+
+ <?= GridView::widget([
+ 'dataProvider' => $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]);
+ }
+ ],
+ ],
+ ]); ?>
+
+
+</div>
--- /dev/null
+<?php
+
+use yii\helpers\Html;
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\BonusLevels $model */
+
+$this->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';
+?>
+<div class="bonus-levels-update p-4">
+ <?= Html::a('Назад', ['index'], ['class' => 'btn btn-primary mb-3']) ?>
+ <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\BonusLevels $model */
+
+$this->title = $model->name;
+$this->params['breadcrumbs'][] = ['label' => 'Bonus Levels', 'url' => ['index']];
+$this->params['breadcrumbs'][] = $this->title;
+\yii\web\YiiAsset::register($this);
+?>
+<div class="bonus-levels-view p-4">
+ <?= Html::a('Назад', ['index'], ['class' => 'btn btn-primary mb-3']) ?>
+ <h1><?= Html::encode($this->title) ?></h1>
+
+ <p>
+ <?= Html::a('Изменить', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
+
+ </p>
+
+ <?= DetailView::widget([
+ 'model' => $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 ?? 'Неизвестно';
+ },
+ ],
+ ],
+ ]) ?>
+
+</div>