From 54a4277069f27b4379f2c6107bc3bf7933893330 Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Thu, 12 Nov 2020 14:59:14 +0100
Subject: [PATCH] FIX #14949 TIME 0:40 fix reject visa preprocess no valid user

---
 .../PreProcessActionController.php            | 23 ++++++++++++++-----
 src/lang/lang-en.json                         |  5 ++--
 src/lang/lang-fr.json                         |  3 ++-
 3 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/src/app/action/controllers/PreProcessActionController.php b/src/app/action/controllers/PreProcessActionController.php
index 2a272d9b899..21d849bba81 100755
--- a/src/app/action/controllers/PreProcessActionController.php
+++ b/src/app/action/controllers/PreProcessActionController.php
@@ -1121,16 +1121,27 @@ class PreProcessActionController
                 }
             } else {
                 $hasPrevious = ListInstanceModel::get([
-                    'select'  => [1],
+                    'select'  => ['listinstance_id', 'item_id'],
                     'where'   => ['res_id = ?', 'difflist_type = ?', 'process_date is not null'],
                     'data'    => [$resId, 'VISA_CIRCUIT'],
-                    'orderBy' => ['listinstance_id'],
-                    'limit'   => 1
+                    'orderBy' => ['listinstance_id desc'],
                 ]);
-                if (!empty($hasPrevious)) {
-                    $resourcesInformation['success'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId];
-                } else {
+                if (empty($hasPrevious)) {
                     $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'circuitNotStarted'];
+                } else {
+                    $validFound = false;
+                    foreach ($hasPrevious as $previous) {
+                        $user = UserModel::getById(['id' => $previous['item_id'], 'select' => ['status']]);
+                        if (!empty($user) && !in_array($user['status'], ['SPD', 'DEL'])) {
+                            $validFound = true;
+                            break;
+                        }
+                    }
+                    if (!$validFound) {
+                        $resourcesInformation['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'noPreviousValid'];
+                    } else {
+                        $resourcesInformation['success'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId];
+                    }
                 }
             }
         }
diff --git a/src/lang/lang-en.json b/src/lang/lang-en.json
index 25d24a220c7..640b8aca71d 100644
--- a/src/lang/lang-en.json
+++ b/src/lang/lang-en.json
@@ -2166,5 +2166,6 @@
     "registeredMailMsgActivate": "Activating this range will close the current range used for that type of recommended.",
     "redirectUserVisaWorkflow": "Change of approver / signatory for mails in the process of visa / signature",
     "chooseNewVisaUser": "is in <b>visa / signature</b> of some mails, please choose a <b>user replacement</b>",
-    "hideModal": "Hide this modal"
-}
\ No newline at end of file
+    "hideModal": "Hide this modal",
+    "noPreviousValid": "No valid previous user"
+}
diff --git a/src/lang/lang-fr.json b/src/lang/lang-fr.json
index ddb2e3ca93a..79f96b1150d 100644
--- a/src/lang/lang-fr.json
+++ b/src/lang/lang-fr.json
@@ -2158,5 +2158,6 @@
     "almostThere": "Vous y êtes presque !",
     "redirectUserVisaWorkflow": "Changement de viseur / signataire pour les courriers en cours de visa / signature",
     "chooseNewVisaUser": "est en <b>visa / signature</b> de certains courriers, veuillez choisir un utilisateur de <b>remplacement</b>",
-    "hideModal": "Ne plus afficher cette fenêtre"
+    "hideModal": "Ne plus afficher cette fenêtre",
+    "noPreviousValid": "Aucun utilisateur précédent valide"
 }
-- 
GitLab