]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
add promocode view/editing
authorAlexander Smirnov <fredeom@mail.ru>
Wed, 5 Jun 2024 13:06:10 +0000 (16:06 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Wed, 5 Jun 2024 13:06:10 +0000 (16:06 +0300)
erp24/controllers/PromocodeController.php [new file with mode: 0644]
erp24/migrations/m240605_072755_create_table_promocode.php
erp24/records/Promocode.php
erp24/views/promocode/edit.php [new file with mode: 0644]
erp24/views/promocode/index.php [new file with mode: 0644]

diff --git a/erp24/controllers/PromocodeController.php b/erp24/controllers/PromocodeController.php
new file mode 100644 (file)
index 0000000..be7cd3e
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+
+namespace app\controllers;
+
+use Yii;
+use yii\data\ActiveDataProvider;
+use yii\web\Controller;
+use yii_app\records\Promocode;
+
+class PromocodeController extends Controller
+{
+    public function actionIndex() {
+        $dataProvider = new ActiveDataProvider(['query' => Promocode::find()->orderBy(['created_at' => SORT_DESC])]);
+        return $this->render('index', compact('dataProvider'));
+    }
+
+    public function actionEdit($id = 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);
+        }
+        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
+            if (!$model->created_by) {
+                $model->created_by = Yii::$app->user->id;
+                $model->created_at = date("Y-m-d H:i:s");
+            } else {
+                $model->updated_by = Yii::$app->user->id;
+                $model->updated_at = date("Y-m-d H:i:s");
+            }
+            if ($model->validate()) {
+                $model->save();
+                return $this->redirect('/promocode/index');
+            } else {
+                var_dump($model->getErrors());
+            }
+        }
+        return $this->render('edit', compact('model'));
+    }
+}
\ No newline at end of file
index 6c7329ea69eb8ec23306db36e003a2a2c70a0456..7864024c3fc2763253d081e135da8191729cdb0a 100755 (executable)
@@ -7,7 +7,7 @@ use yii\db\Migration;
  */
 class m240605_072755_create_table_promocode extends Migration
 {
-    const TABLE_NAME = 'promocode';
+    const TABLE_NAME = '{{%erp24.promocode}}';
     /**
      * {@inheritdoc}
      */
index 048a899a843e6449ce3e3b841bad5459fcf2b5b7..04b76cfc73e6d97352321d9a5740cffd4db07e72 100644 (file)
@@ -23,14 +23,35 @@ class Promocode extends \yii\db\ActiveRecord
     public static function tableName() { return 'promocode'; }
 
     public function rules() {
-        return [];
+        return [
+            [['code', 'bonus', 'active', 'date_start', 'date_end', 'created_by', 'created_at'], 'required'],
+            [['code'], 'string', 'max' => 13],
+            [['bonus', 'active', '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'],
+        ];
     }
 
     public function attributeLabels() {
         return [
             'id' => 'ID',
+            'code' => 'Code',
+            'bonus' => 'Bonus',
+            'active' => 'Active',
+            'date_start' => 'Date Start',
+            'date_end' => 'Date End',
+            'created_by' => 'Created By',
+            'updated_by' => 'Updated By',
+            'created_at' => 'Created At',
+            'updated_at' => 'Updated At',
         ];
     }
 
+    public function getCreatedBy() {
+        return $this->hasOne(Admin::class, ['id' => 'created_by']);
+    }
 
-}
\ No newline at end of file
+    public function getUpdatedBy() {
+        return $this->hasOne(Admin::class, ['id' => 'updated_by']);
+    }
+}
diff --git a/erp24/views/promocode/edit.php b/erp24/views/promocode/edit.php
new file mode 100644 (file)
index 0000000..4347d26
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+
+use dosamigos\datetimepicker\DateTimePicker;
+
+use \yii\helpers\Html;
+use \yii\widgets\ActiveForm;
+use \yii_app\records\Promocode;
+use \yii_app\helpers\PrintBlockHelper;
+
+/** @var $model Promocode */
+
+?>
+
+
+<div class="promocodeEdit m-5">
+    <h3><?= $model->id ? 'Редактирование' : 'Создание' ?> <?= Html::a('Назад', '/promocode/index', ['class' => 'btn btn-secondary btn-sm']) ?> </h3>
+
+    <?php $form = ActiveForm::begin() ?>
+
+    <?php if ($model->id): ?>
+        <?= $form->field($model, 'id')->hiddenInput()->label(false) ?>
+    <?php endif; ?>
+
+    <?php PrintBlockHelper::printBlock('Промокод', $form->field($model, 'code')->textInput()->label(false)) ?>
+
+    <?php PrintBlockHelper::printBlock('Бонус', $form->field($model, 'bonus')->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, [
+        'language' => 'ru',
+        'template' => '{input}',
+        'clientOptions' => [
+            'autoclose' => true,
+            'format' => 'Y-m-d H:i:s',
+            'todayBtn' => true
+        ],
+    ])->label(false)) ?>
+
+    <?php PrintBlockHelper::printBlock('Дата окончания действия промокода', $form->field($model, 'date_end')->widget(DateTimePicker::class, [
+        'language' => 'ru',
+        'template' => '{input}',
+        'clientOptions' => [
+            'autoclose' => true,
+            'format' => 'Y-m-d H:i:s',
+            'todayBtn' => true
+        ],
+    ])->label(false)) ?>
+
+    <div class="form-group">
+        <?= Html::submitButton('Сохранить', ['class' => 'btn btn-success btn-lg'])?>
+    </div>
+
+    <?php ActiveForm::end() ?>
+
+</div>
\ No newline at end of file
diff --git a/erp24/views/promocode/index.php b/erp24/views/promocode/index.php
new file mode 100644 (file)
index 0000000..6547fb7
--- /dev/null
@@ -0,0 +1,74 @@
+<?php
+
+use yii\data\ActiveDataProvider;
+use yii\grid\GridView;
+use yii\helpers\Html;
+
+/** @var $dataProvider ActiveDataProvider */
+
+?>
+
+<div class="promocodeIndex m-5">
+
+    <h1>Промокоды</h1>
+
+    <?= Html::a('Создать новый промокод', ['edit'], ['class' => 'btn btn-success']) ?>
+
+    <?= 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->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)) : '';
+                }
+            ],
+        ],
+    ]); ?>
+
+</div>