*/
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.
'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('Дата создания'),
<?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() ?>