]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Задвоение удалений и при удалении последней записи переход на индекс
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 25 Nov 2024 14:46:07 +0000 (17:46 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 25 Nov 2024 14:46:07 +0000 (17:46 +0300)
erp24/controllers/crud/Product1cReplacementController.php
erp24/web/js/crud/product1cReplacement/multy-form.js

index 727a00be9d92dfe315fd9db4328c52842144ad72..204851df79ce765ed3bcd8837d2e23a69dafd338 100644 (file)
@@ -356,20 +356,36 @@ class Product1cReplacementController extends Controller
         Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
 
         $model = $this->findModel($id);
+
         if ($model) {
+
+            $guid = $model->guid;
+
+
+            $remainingRecords = Product1cReplacement::find()
+                ->where(['guid' => $guid])
+                ->andWhere(['deleted_at' => null])
+                ->count();
+
             $stateBefore = $model->guid_replacement;
             $this->logReplacementAction(
                 $model->id,
                 $stateBefore,
                 'Запись удалена'
             );
-            if ($model->softDelete()) {
 
-                return ['success' => true];
+            if ($model->softDelete()) {
+                return [
+                    'success' => true,
+                    'message' => 'Запись успешно удалена',
+                    'redirect' => $remainingRecords <= 1,
+                ];
             }
         }
         return ['success' => false, 'message' => 'Ошибка при удалении записи.'];
     }
+
+
     /**
      * Finds the Product1CReplacement model based on its primary key value.
      * If the model is not found, a 404 HTTP exception will be thrown.
index 37a2849fd2169429a7b81b441dea868c4331119c..ac52caa5b7b492367d122ad1a8f82e5fd4fe360d 100644 (file)
@@ -1,7 +1,7 @@
 function initReplacementHandlers() {
     let counter = $('.guid-replacement-select').length;
 
-    $('#add-guid-replacement').on('click', function () {
+    $('#add-guid-replacement').off('click').on('click', function () {
         counter++;
         let newField = `
             <div class="guid-replacement-group my-4" id="guid-replacement-group-${counter}" style="position: relative;">
@@ -46,12 +46,12 @@ function initReplacementHandlers() {
         });
     });
 
-    $(document).on('click', '.remove-guid-replacement', function () {
+    $(document).off('click', '.remove-guid-replacement').on('click', '.remove-guid-replacement', function () {
         let target = $(this).data('target');
         $(target).remove();
     });
 
-    $(document).on('click', '.delete-guid-replacement', function (e) {
+    $(document).off('click', '.delete-guid-replacement').on('click', '.delete-guid-replacement', function (e) {
         e.preventDefault();
         if (!confirm('Вы уверены, что хотите удалить эту запись?')) {
             return;
@@ -64,8 +64,12 @@ function initReplacementHandlers() {
             success: function(data) {
                 console.log(data);
                 if (data.success) {
-
-                    $.pjax.reload({container: '#pjax-container', async: false});
+                    alert(data.message);
+                    if (data.redirect) {
+                        window.location.href = '/crud/product1c-replacement/index';
+                    } else {
+                        $.pjax.reload({container: '#pjax-container', async: false});
+                    }
                 } else {
                     alert(data.message || 'Ошибка при удалении записи.');
                 }