]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Корректировки по таблице
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Tue, 4 Feb 2025 06:36:15 +0000 (09:36 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Tue, 4 Feb 2025 06:36:15 +0000 (09:36 +0300)
erp24/controllers/BonusLevelsController.php
erp24/migrations/m250203_113244_create_users_bonus_levels_table.php
erp24/views/bonus-levels/_form.php

index f609073df392a3720618111921c636fb63f6f923..6be6584d9664f8e09dbbb6b8bdb2f1c37679432d 100644 (file)
@@ -125,51 +125,50 @@ class BonusLevelsController extends Controller
      */
     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;
-                }
-            }
+        $oldModel = $this->findModel($id);
+        $newModel = new BonusLevels();
+
+        if ($newModel->load(Yii::$app->request->post())) {
+            // Проверяем изменения
+            $isChanged = ($oldModel->cashback_rate != $newModel->cashback_rate) ||
+            ($oldModel->referal_rate != $newModel->referal_rate) ||
+            ($oldModel->bonus_rate != $newModel->bonus_rate) || ($oldModel->threshold != $newModel->threshold);
 
             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()) {
+                $transaction = Yii::$app->db->beginTransaction();
+                try {
+                    // Деактивируем старую запись
+                    $oldModel->active = 0;
+                    $oldModel->date_end = date('Y-m-d H:i:s');
+                    $oldModel->updated_by = Yii::$app->user->id;
+                    $oldModel->save();
+
+                    // Создаём новую запись с данными из формы
+                    $newModel->active = 1;
+                    $newModel->date_start = date('Y-m-d H:i:s');
+                    $newModel->date_end = null;
+                    $newModel->created_by = Yii::$app->user->id;
+                    $newModel->save();
+
+                    $transaction->commit();
                     return $this->redirect(['view', 'id' => $newModel->id]);
+                } catch (\Exception $e) {
+                    $transaction->rollBack();
+                    throw $e;
                 }
+            } else {
+                $oldModel->load(Yii::$app->request->post());
+                $oldModel->save();
             }
         }
 
         return $this->render('update', [
-            'model' => $model,
+            'model' => $oldModel,
         ]);
     }
 
 
+
     /**
      * Deletes an existing BonusLevels model.
      * If deletion is successful, the browser will be redirected to the 'index' page.
index 532846ab9d30ba14b966eb8bdafd9f58522024ff..c971437a6b5399a42e39d7c01b96f506a7ce0bf4 100644 (file)
@@ -21,6 +21,8 @@ class m250203_113244_create_users_bonus_levels_table extends Migration
                 'phone' => $this->string()->notNull()->comment('Телефон клиента'),
                 'user_id' => $this->integer()->notNull()->comment('ID клиента'),
                 'bonus_level' => $this->string()->notNull()->comment('Уровань клиента в БС'),
+                'check_id' => $this->string()->null()->comment('Основание для повышения уровня - GUID'),
+                'check_name' => $this->string()->null()->comment('Основание для повышения уровня - номер чека'),
                 'created_at' => $this->timestamp()
                     ->defaultExpression('CURRENT_TIMESTAMP')
                     ->notNull()->comment('Дата создания'),
index fa2db670227ba49ceecd95619466178e8b0b4f22..128453b6faea65df89acc302c71f3ceaa6d3df14 100644 (file)
@@ -12,9 +12,13 @@ use yii\widgets\ActiveForm;
 
     <?php $form = ActiveForm::begin(); ?>
 
-    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
-
-    <?= $form->field($model, 'alias')->textInput(['maxlength' => true]) ?>
+    <?php if ($model->isNewRecord) : ?>
+        <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
+        <?= $form->field($model, 'alias')->textInput(['maxlength' => true]) ?>
+    <?php else : ?>
+        <?= $form->field($model, 'name')->hiddenInput()->label(false) ?>
+        <?= $form->field($model, 'alias')->hiddenInput()->label(false) ?>
+    <?php endif; ?>
 
     <?= $form->field($model, 'threshold')->textInput() ?>