From: Alexander Smirnov Date: Wed, 5 Jun 2024 13:06:10 +0000 (+0300) Subject: add promocode view/editing X-Git-Tag: 1.2^2~2^2~9 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=351b08f1f4b99156196c727aab69f358108e40ea;p=erp24_rep%2Fyii-erp24%2F.git add promocode view/editing --- diff --git a/erp24/controllers/PromocodeController.php b/erp24/controllers/PromocodeController.php new file mode 100644 index 00000000..be7cd3ef --- /dev/null +++ b/erp24/controllers/PromocodeController.php @@ -0,0 +1,42 @@ + 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 diff --git a/erp24/migrations/m240605_072755_create_table_promocode.php b/erp24/migrations/m240605_072755_create_table_promocode.php index 6c7329ea..7864024c 100755 --- a/erp24/migrations/m240605_072755_create_table_promocode.php +++ b/erp24/migrations/m240605_072755_create_table_promocode.php @@ -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} */ diff --git a/erp24/records/Promocode.php b/erp24/records/Promocode.php index 048a899a..04b76cfc 100644 --- a/erp24/records/Promocode.php +++ b/erp24/records/Promocode.php @@ -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 index 00000000..4347d269 --- /dev/null +++ b/erp24/views/promocode/edit.php @@ -0,0 +1,56 @@ + + + +
+

id ? 'Редактирование' : 'Создание' ?> 'btn btn-secondary btn-sm']) ?>

+ + + + id): ?> + field($model, 'id')->hiddenInput()->label(false) ?> + + + field($model, 'code')->textInput()->label(false)) ?> + + field($model, 'bonus')->textInput(['type' => 'number'])->label(false)) ?> + + field($model, 'active')->dropDownList(['Не активен', 'Активен'])->label(false)) ?> + + 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)) ?> + + 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)) ?> + +
+ 'btn btn-success btn-lg'])?> +
+ + + +
\ No newline at end of file diff --git a/erp24/views/promocode/index.php b/erp24/views/promocode/index.php new file mode 100644 index 00000000..6547fb76 --- /dev/null +++ b/erp24/views/promocode/index.php @@ -0,0 +1,74 @@ + + +
+ +

Промокоды

+ + 'btn btn-success']) ?> + + $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)) : ''; + } + ], + ], + ]); ?> + +