--- /dev/null
+<?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
*/
class m240605_072755_create_table_promocode extends Migration
{
- const TABLE_NAME = 'promocode';
+ const TABLE_NAME = '{{%erp24.promocode}}';
/**
* {@inheritdoc}
*/
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']);
+ }
+}
--- /dev/null
+<?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
--- /dev/null
+<?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>