]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
add single use promocodes to ui
authorAlexander Smirnov <fredeom@mail.ru>
Mon, 10 Jun 2024 07:34:19 +0000 (10:34 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Mon, 10 Jun 2024 07:34:19 +0000 (10:34 +0300)
erp24/controllers/PromocodeController.php
erp24/migrations/m240605_072755_create_table_promocode.php
erp24/records/Promocode.php
erp24/services/PromocodeService.php [new file with mode: 0644]
erp24/views/promocode/edit.php
erp24/views/promocode/index.php

index be7cd3ef3024a7600d51daf71ba446ec9134b9c4..026883084ee13aafa52343042c643f2a0fea6d0c 100644 (file)
@@ -6,22 +6,26 @@ use Yii;
 use yii\data\ActiveDataProvider;
 use yii\web\Controller;
 use yii_app\records\Promocode;
+use yii_app\services\PromocodeService;
 
 class PromocodeController extends Controller
 {
     public function actionIndex() {
-        $dataProvider = new ActiveDataProvider(['query' => Promocode::find()->orderBy(['created_at' => SORT_DESC])]);
+        $dataProvider = new ActiveDataProvider(['query' => Promocode::find()->where(['parent_id' => null])->orderBy(['created_at' => SORT_DESC])]);
         return $this->render('index', compact('dataProvider'));
     }
 
     public function actionEdit($id = null) {
+        $dataProvider = null;
         if (!$id) {
             $model = new Promocode;
             $model->date_start = date("Y-m-d 00:00:00");
             $model->date_end = date("Y-m-d 00:00:00", strtotime("+366 day", time()));
         } else {
             $model = Promocode::findOne($id);
+            $dataProvider = new ActiveDataProvider(['query' => Promocode::find()->where(['base' => Promocode::BASE_SINGLE_USE, 'parent_id' => $id])]);
         }
+        /** @var $model Promocode */
         if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
             if (!$model->created_by) {
                 $model->created_by = Yii::$app->user->id;
@@ -31,12 +35,18 @@ class PromocodeController extends Controller
                 $model->updated_at = date("Y-m-d H:i:s");
             }
             if ($model->validate()) {
+                $model->code = mb_strtoupper($model->code, 'UTF-8');
                 $model->save();
+                if (!empty($model->generatePromocodeCount) && $model->active == Promocode::ACTIVE_ON) {
+                    PromocodeService::generateSingleUsePromocodes($model);
+                    $model->base = Promocode::BASE_BASE;
+                    $model->save();
+                }
                 return $this->redirect('/promocode/index');
             } else {
                 var_dump($model->getErrors());
             }
         }
-        return $this->render('edit', compact('model'));
+        return $this->render('edit', compact('model', 'dataProvider'));
     }
 }
\ No newline at end of file
index 7864024c3fc2763253d081e135da8191729cdb0a..fe58bfd600a1d7b226c74ca52582a00db9695a0f 100755 (executable)
@@ -17,7 +17,10 @@ class m240605_072755_create_table_promocode extends Migration
             'id' => $this->primaryKey(),
             'code' => $this->string(13)->notNull()->comment("Промокод"),
             'bonus' => $this->integer()->notNull()->comment('Количество бонусов получаемых по промокоду'),
+            'duration' => $this->integer()->notNull()->comment('Продолжительность действия бонуса'),
             'active' => $this->tinyInteger()->notNull()->defaultValue(0)->comment('0 - не активный, 1 - активный'),
+            'base' => $this->tinyInteger()->notNull()->defaultValue(0)->comment('0 - многоразовый промокод, 1 - база для одноразовых, 2 - одноразовый промокод'),
+            'parent_id' => $this->integer()->null()->comment('ID промокода базы'),
             'date_start' => $this->timestamp()->notNull()->comment('Дата начала действия промокода'),
             'date_end'   => $this->timestamp()->notNull()->comment('Дата окончания действия промокода'),
             'created_by' => $this->integer()->notNull()->comment('ID создателя записи'),
index 04b76cfc73e6d97352321d9a5740cffd4db07e72..c4ae00fa216ff3cefcc331a333124444baa1d7d7 100644 (file)
@@ -10,7 +10,10 @@ use Yii;
  * @property int $id
  * @property string $code Промокод
  * @property int $bonus Количество бонусов получаемых по промокоду
+ * @property int $duration Продолжительность действия бонуса
  * @property int $active 0 - не активный, 1 - активный
+ * @property int $base 0 - многоразовый промокод, 1 - база для одноразовых, 2 - одноразовый промокод
+ * @property int|null $parent_id ID промокода базы
  * @property string $date_start Дата начала действия промокода
  * @property string $date_end Дата окончания действия промокода
  * @property int $created_by ID создателя записи
@@ -20,15 +23,23 @@ use Yii;
  */
 class Promocode extends \yii\db\ActiveRecord
 {
+    const ACTIVE_ON = 1;
+    const ACTIVE_OFF = 0;
+    const BASE_SHARED = 0;
+    const BASE_BASE = 1;
+    const BASE_SINGLE_USE = 2;
+
+    public $generatePromocodeCount;
+
     public static function tableName() { return 'promocode'; }
 
     public function rules() {
         return [
-            [['code', 'bonus', 'active', 'date_start', 'date_end', 'created_by', 'created_at'], 'required'],
+            [['code', 'bonus', 'duration', 'active', 'date_start', 'date_end', 'created_by', 'created_at'], 'required'],
             [['code'], 'string', 'max' => 13],
-            [['bonus', 'active', 'created_by', 'updated_by'], 'integer'],
+            [['bonus', 'duration', 'active', 'base', 'parent_id', 'created_by', 'updated_by'], 'integer'],
             [['date_start', 'date_end', 'created_at', 'updated_at'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
-            [['updated_by', 'updated_at'], 'safe'],
+            [['updated_by', 'updated_at', 'generatePromocodeCount'], 'safe'],
         ];
     }
 
@@ -37,7 +48,10 @@ class Promocode extends \yii\db\ActiveRecord
             'id' => 'ID',
             'code' => 'Code',
             'bonus' => 'Bonus',
+            'duration' => 'Duration',
             'active' => 'Active',
+            'base' => 'Base',
+            'parent_id' => 'Parent ID',
             'date_start' => 'Date Start',
             'date_end' => 'Date End',
             'created_by' => 'Created By',
@@ -54,4 +68,8 @@ class Promocode extends \yii\db\ActiveRecord
     public function getUpdatedBy() {
         return $this->hasOne(Admin::class, ['id' => 'updated_by']);
     }
+
+    public function getParent() {
+        return $this->hasOne(Promocode::class, ['id' => 'parent_id']);
+    }
 }
diff --git a/erp24/services/PromocodeService.php b/erp24/services/PromocodeService.php
new file mode 100644 (file)
index 0000000..55ec619
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+namespace yii_app\services;
+
+use Yii;
+use yii_app\records\Promocode;
+
+class PromocodeService
+{
+    private static function generateTwoChars() {
+        $chars = "ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ";
+        return mb_substr($chars, rand() % mb_strlen($chars), 1) .
+               mb_substr($chars, rand() % mb_strlen($chars), 1);
+    }
+
+    public static function generateSingleUsePromocodes(Promocode $basePromocode) {
+        foreach (range(1, $basePromocode->generatePromocodeCount) as $num) {
+            $word = self::generateTwoChars();
+            while (Promocode::find()->where(['code' => $basePromocode->code . $word])->one()) {
+                $word = self::generateTwoChars();
+            }
+            $singleUsePromocode = new Promocode;
+            $singleUsePromocode->code = mb_strtoupper($basePromocode->code . $word, 'UTF-8');
+            $singleUsePromocode->bonus = $basePromocode->bonus;
+            $singleUsePromocode->duration = $basePromocode->duration;
+            $singleUsePromocode->active = $basePromocode->active;
+            $singleUsePromocode->base = Promocode::BASE_SINGLE_USE;
+            $singleUsePromocode->parent_id = $basePromocode->id;
+            $singleUsePromocode->date_start = $basePromocode->date_start;
+            $singleUsePromocode->date_end = $basePromocode->date_end;
+            $singleUsePromocode->date_start = $basePromocode->date_start;
+            $singleUsePromocode->created_by = Yii::$app->user->id;
+            $singleUsePromocode->created_at = date("Y-m-d H:i:s");
+            $singleUsePromocode->save();
+            if ($singleUsePromocode->getErrors()) {
+                var_dump($singleUsePromocode->getErrors());
+            }
+        }
+    }
+}
\ No newline at end of file
index 4347d2698acffe5b6c3e3b16965cdce9d8d34fea..090394ee798b3708307d73bc620c4c1d36d35cac 100644 (file)
@@ -2,12 +2,15 @@
 
 use dosamigos\datetimepicker\DateTimePicker;
 
+use yii\data\ActiveDataProvider;
+use yii\grid\GridView;
 use \yii\helpers\Html;
 use \yii\widgets\ActiveForm;
 use \yii_app\records\Promocode;
 use \yii_app\helpers\PrintBlockHelper;
 
 /** @var $model Promocode */
+/** @var $dataProvider ActiveDataProvider */
 
 ?>
 
@@ -25,6 +28,8 @@ use \yii_app\helpers\PrintBlockHelper;
 
     <?php PrintBlockHelper::printBlock('Бонус', $form->field($model, 'bonus')->textInput(['type' => 'number'])->label(false)) ?>
 
+    <?php PrintBlockHelper::printBlock('Продолжительность действия бонуса в днях', $form->field($model, 'duration')->textInput(['type' => 'number'])->label(false)) ?>
+
     <?php PrintBlockHelper::printBlock('Активность', $form->field($model, 'active')->dropDownList(['Не активен', 'Активен'])->label(false)) ?>
 
     <?php PrintBlockHelper::printBlock('Дата начала действия промокода', $form->field($model, 'date_start')->widget(DateTimePicker::class, [
@@ -47,10 +52,87 @@ use \yii_app\helpers\PrintBlockHelper;
         ],
     ])->label(false)) ?>
 
+    <?php if ($model->base < 2): ?>
+        <?php PrintBlockHelper::printBlock('Количество дополнительных одноразовых промокодов', $form->field($model, 'generatePromocodeCount')->textInput(['type' => 'number'])->label(false)) ?>
+    <?php endif; ?>
+
     <div class="form-group">
         <?= Html::submitButton('Сохранить', ['class' => 'btn btn-success btn-lg'])?>
     </div>
 
     <?php ActiveForm::end() ?>
 
+    <?php if (isset($dataProvider) && $model->base == 1): ?>
+
+        <?= GridView::widget([
+            'dataProvider' => $dataProvider,
+            'columns' => [
+                ['class' => 'yii\grid\SerialColumn'],
+                [
+                    'label' => 'Промокод',
+                    'format' => 'raw',
+                    'value' => function ($model) {
+                        return Html::a($model->code, ['edit', 'id' => $model->id], ['class' => 'btn btn-link']);
+                    }
+                ],
+                'bonus',
+                [
+                    'label' => 'Время действия бонуса в днях',
+                    'value' => function ($model) {
+                        return $model->duration;
+                    }
+                ],
+                [
+                    'label' => 'Тип промокода',
+                    'value' => function ($model) {
+                        return "Одноразовый";
+                    }
+                ],
+                [
+                    'label' => 'Активен ли промокод',
+                    'value' => function ($model) {
+                        return $model->active ? 'Да' : 'Нет';
+                    }
+                ],
+                [
+                    'label' => 'Дата начала действия промокода',
+                    'value' => function ($model) {
+                        return date("Y-m-d", strtotime($model->date_start));
+                    }
+                ],
+                [
+                    'label' => 'Дата окончания действия промокода',
+                    'value' => function ($model) {
+                        return date("Y-m-d", strtotime($model->date_end));
+                    }
+                ],
+                [
+                    'label' => 'Создатель',
+                    'value' => function ($model) {
+                        return $model->createdBy->name ?? '';
+                    }
+                ],
+                [
+                    'label' => 'Обновил',
+                    'value' => function ($model) {
+                        return $model->updatedBy->name ?? '';
+                    }
+                ],
+                [
+                    'label' => 'Дата создания промокода',
+                    'value' => function ($model) {
+                        return date("Y-m-d H:i:s", strtotime($model->created_at));
+                    }
+                ],
+                [
+                    'label' => 'Дата обновления промокода',
+                    'value' => function ($model) {
+                        return $model->updated_at ? date("Y-m-d H:i:s", strtotime($model->updated_at)) : '';
+                    }
+                ],
+            ],
+        ]); ?>
+
+    <?php endif; ?>
+
 </div>
\ No newline at end of file
index 6547fb767f57c925d19218d8fdda6c79fa9bcabb..4fd9c4f2a2016768e1b797af05f5c515da55b9d3 100644 (file)
@@ -26,6 +26,18 @@ use yii\helpers\Html;
                 }
             ],
             'bonus',
+            [
+                'label' => 'Время действия бонуса в днях',
+                'value' => function ($model) {
+                    return $model->duration;
+                }
+            ],
+            [
+                'label' => 'Тип промокода',
+                'value' => function ($model) {
+                    return $model->base == 0 ? "Множественный" : "База для одноразовых";
+                }
+            ],
             [
                 'label' => 'Активен ли промокод',
                 'value' => function ($model) {