]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Изменение логики работы задачи
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 2 Jul 2025 09:23:59 +0000 (12:23 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 2 Jul 2025 09:23:59 +0000 (12:23 +0300)
erp24/commands/CronController.php
erp24/controllers/CategoryPlanController.php
erp24/views/category-plan/index.php
erp24/web/js/category-plan/index.js

index 866807531c51942318ba404c9446ac57e4248bdf..665b5bb1b0d9a5cfd4d5e9db4fb2fc71fd3b7142 100644 (file)
@@ -1754,6 +1754,7 @@ class CronController extends Controller
                 ->one();
             if ($scriptLauncherLog && $scriptLauncherLog['active'] == 1 && $scriptLauncherLog['status'] == 1) {
                 //$task = json_decode($scriptLauncherLog['context'], true );
+               // $cache->delete($cacheKey);
                 $this->stdout("Задача уже запущена {$task['startTime']}\n");
                 return ExitCode::OK;
             }
@@ -1792,6 +1793,7 @@ class CronController extends Controller
             $log->year = (int)$task['year'];
             $log->month = (int)$task['month'];
             $log->active = 1;
+            $log->progress = 0;
             $log->status = 1;
             $log->date_start = date('Y-m-d H:i:s');
             if (!$log->save()) {
@@ -1913,21 +1915,22 @@ class CronController extends Controller
 
                 $this->stdout("Сохранена автопланограмма для магазина {$storeId}\n", BaseConsole::FG_GREEN);
 
-
                 $task['status'] = 'done';
                 $task['progress'] = 100;
                 $cache->set($cacheKey, $task);
 
+                $log->context = json_encode($task, JSON_UNESCAPED_UNICODE);
                 $log->message = 'Finished successfully';
                 $log->progress = 100;
                 $log->active = 0;
+                $log->progress = 100;
                 $log->date_finish = date('Y-m-d H:i:s');
                 $log->status = 2;
                 if (!$log->save()) {
                     Yii::error(json_encode($log->getErrors(), JSON_UNESCAPED_UNICODE));
                     LogService::apiErrorLog(json_encode(["error_id" => 8, "error" => $log->getErrors()], JSON_UNESCAPED_UNICODE));
                 }
-
+                $cache->delete($cacheKey);
                 $this->stdout("Расчет для магазина {$storeId} закончен\n");
                 return ExitCode::OK;
 
@@ -1947,7 +1950,7 @@ class CronController extends Controller
                     Yii::error(json_encode($log->getErrors(), JSON_UNESCAPED_UNICODE));
                     LogService::apiErrorLog(json_encode(["error_id" => 8, "error" => $log->getErrors()], JSON_UNESCAPED_UNICODE));
                 }
-                Yii::$app->cache->delete("apRecalculateTask");
+                $cache->delete($cacheKey);
                 $this->stderr("Ошибка в ходе расчета: {$e->getMessage()}\n");
                 return ExitCode::UNSPECIFIED_ERROR;
             }
index bc9dc24cc3f1c781e979e7d1e01fa05eaa48209a..fef2742585dc76f26560d37ed21c31387c934824 100644 (file)
@@ -596,7 +596,7 @@ class CategoryPlanController extends Controller {
             ->asArray()
             ->limit(1)
             ->one();
-        if ($scriptLauncherLog && $scriptLauncherLog['active'] == 1) {
+        if ($scriptLauncherLog && $scriptLauncherLog['active'] == 1 && $scriptLauncherLog['status'] == 1) {
             return $this->asJson(['status' => 'running' ]);
         } else {
             $taskName = "apRecalculateTask";
@@ -621,12 +621,29 @@ class CategoryPlanController extends Controller {
     public function actionCheckTask()
     {
         $task = Yii::$app->cache->get('apRecalculateTask');
-        if ($task && $task['status'] === 'done') {
-            Yii::$app->cache->delete("apRecalculateTask");
-        }
         if (!$task) {
+            $scriptLauncherLog = ScriptLauncherLog::find()
+                ->andWhere(['name' => "taskApRecalculate"])
+                ->orderBy(['created_at' => SORT_DESC])
+                ->asArray()
+                ->limit(1)
+                ->one();
+
+            if (!$scriptLauncherLog) {
+                return $this->asJson(['status' => 'not_found']);
+            }
 
-            return $this->asJson(['status' => 'not_found']);
+            $dateFinish = $scriptLauncherLog['date_finish'];
+            $ageSeconds = $dateFinish
+                ? time() - strtotime($dateFinish)
+                : null;
+
+
+            if ($dateFinish === null || $ageSeconds <= 5) {
+                $task = json_decode($scriptLauncherLog['context'], true);
+            } else {
+                return $this->asJson(['status' => 'not_found']);
+            }
         }
 
         return $this->asJson([
index f0c91af8edd745e11d79c8463260c5615a2ea195..80396cb335c088c1e18789a914c61fd4d44bc1d8 100644 (file)
@@ -172,6 +172,8 @@ input[readonly] {
                 <h1><?= $stores[$model->store_id]?></h1>
                 <div id="changes-count" class="fs-6"></div>
                 <div id="changes" style="display:none;"></div>
+                <div id="changes-hint" style="display:none;" class="fs-6 mt-3 text-danger" >После внесения всех изменений нажмите на кнопку
+                    <br>"Пересчитать автопланограмму"</div>
                 <?= Html::button('Пересчитать автопланограмму', [
                     'class' => 'btn btn-success m-2',
                     'disabled' => true,
index e552ad6a79f10c326ec3dd9e98c3bafbf5939a84..ce77ad492c2e0fdad44c942aa1d1dc14de623c0b 100644 (file)
@@ -136,9 +136,9 @@ function updateChangesLog(store_id) {
         }
 
     });
-    $('#changes-hint').remove();
-    $('#changes').after('<div id="changes-hint" class="fs-6 mt-3 text-danger" >После внесения всех изменений нажмите на кнопку\n' +
-        '<br>"Пересчитать автопланограмму"</div>');
+    $('#changes-hint').show();
+    $('#changes').show();
+    $('#changes-count').show();
     changesBox.html(`<ul>${listItems}</ul>`).hide();
     changesCount.html(`Были внесены изменения (число изменений) - ${count} <button id="show-changes" class="btn btn-link">Подробнее</button>`);
 
@@ -157,13 +157,20 @@ $(document).ready(() => {
         language: data_table_language
     });
     const store_id = $('#selected-store').val();
-    const changes = JSON.parse(localStorage.getItem('planChanges') || '{}');
+    const changesRaw = localStorage.getItem('planChanges');
+    const changes = changesRaw ? JSON.parse(changesRaw) : {};
 
-    if (store_id && changes[store_id]) {
-        updateChangesLog(store_id);
-        $('#rebuild').prop('disabled', false);
-        $('#delete').prop('disabled', false);
 
+    const hasChanges = changes[store_id]
+        && Object.keys(changes[store_id]).length > 0;
+
+    if (hasChanges) {
+        updateChangesLog(store_id);
+        $('#rebuild, #delete, #filter-btn').prop('disabled', false);
+        $('#changes-hint,#changes,#changes-count').show();
+    } else {
+        $('#rebuild, #delete').prop('disabled', true);
+        $('#changes-hint,#changes,#changes-count').hide();
     }
 
     $('#delete').on('click', function (event) {
@@ -262,6 +269,8 @@ function startTaskPolling() {
                     $('#delete').prop('disabled', true);
                     $('#filter-btn').prop('disabled', true);
                     $('#changes-hint').hide();
+                    $('#changes').hide();
+                    $('#changes-count').hide();
                     $('#categoryPlan').addClass('block_table');
                 }
                 if (data.status === 'done') {
@@ -269,9 +278,10 @@ function startTaskPolling() {
                     taskPollInterval = null;
                     localStorage.removeItem('planChanges');
                     $('#categoryPlan').removeClass('block_table');
-                    $('#rebuild').prop('disabled', false).text('Пересчитать автопланограмму');
-                    $('#delete').prop('disabled', false);
-                    $('#changes-hint').remove();
+                    $('#rebuild').prop('disabled', true).text('Пересчитать автопланограмму');
+                    $('#delete').prop('disabled', true);
+                    $('#changes-hint').hide();
+                    $('#changes-count').hide();
                     $('#changes').hide();
                     $('#filter-btn').prop('disabled', false);
                     alert('Персчет автопланнограммы успешно завершен');