From c9bebabb16c51f27ac6c4de681e4bdc4b34a9671 Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Fri, 24 May 2019 16:06:17 +0100
Subject: [PATCH] FEAT #10704 TIME 1 retrieve doc with workflow + note

---
 .../controllers/MaarchParapheurController.php | 53 +++++++++++++------
 1 file changed, 36 insertions(+), 17 deletions(-)

diff --git a/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php b/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php
index 5725b95b9b4..f1f7e38468e 100755
--- a/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php
+++ b/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php
@@ -356,26 +356,27 @@ class MaarchParapheurController
 
     public static function retrieveSignedMails(array $aArgs)
     {
-        $validated = $aArgs['config']['data']['externalValidated'];
-        $refused   = $aArgs['config']['data']['externalRefused'];
-
         foreach (['noVersion', 'isVersion', 'resLetterbox'] as $version) {
             foreach ($aArgs['idsToRetrieve'][$version] as $resId => $value) {
-                $documentStatus = MaarchParapheurController::getDocumentStatus(['config' => $aArgs['config'], 'documentId' => $value->external_id]);
+                $documentWorkflow = MaarchParapheurController::getDocumentWorkflow(['config' => $aArgs['config'], 'documentId' => $value->external_id]);
+                $state = MaarchParapheurController::getState(['workflow' => $documentWorkflow]);
                 
-                if (in_array($documentStatus['reference'], [$validated, $refused])) {
-                    $signedDocument = MaarchParapheurController::getProcessedDocument(['config' => $aArgs['config'], 'documentId' => $value->external_id]);
-                    $aArgs['idsToRetrieve'][$version][$resId]->format = 'pdf'; // format du fichier récupéré
-                    $aArgs['idsToRetrieve'][$version][$resId]->encodedFile = $signedDocument;
-                    if ($documentStatus['reference'] == $validated && $documentStatus['mode'] == 'SIGN') {
+                if (in_array($state['status'], ['validated', 'refused'])) {
+                    $signedDocument = MaarchParapheurController::getDocument(['config' => $aArgs['config'], 'documentId' => $value->external_id]);
+                    $aArgs['idsToRetrieve'][$version][$resId]->format = 'pdf';
+                    $aArgs['idsToRetrieve'][$version][$resId]->encodedFile = $signedDocument['encodedDocument'];
+                    if ($state['status'] == 'validated' && in_array($state['mode'], ['sign', 'visa'])) {
                         $aArgs['idsToRetrieve'][$version][$resId]->status = 'validated';
-                    } elseif ($documentStatus['reference'] == $refused && $documentStatus['mode'] == 'SIGN') {
+                    } elseif ($state['status'] == 'refused' && in_array($state['mode'], ['sign', 'visa'])) {
                         $aArgs['idsToRetrieve'][$version][$resId]->status = 'refused';
-                    } elseif ($documentStatus['reference'] == $validated && $documentStatus['mode'] == 'NOTE') {
+                    } elseif ($state['status'] == 'validated' && $state['mode'] == 'note') {
                         $aArgs['idsToRetrieve'][$version][$resId]->status = 'validatedNote';
-                    } elseif ($documentStatus['reference'] == $refused && $documentStatus['mode'] == 'NOTE') {
+                    } elseif ($state['status'] == 'refused' && $state['mode'] == 'note') {
                         $aArgs['idsToRetrieve'][$version][$resId]->status = 'refusedNote';
                     }
+                    if (!empty($state['note'])) {
+                        $aArgs['idsToRetrieve'][$version][$resId]->noteContent = $state['note'];
+                    }
                 } else {
                     unset($aArgs['idsToRetrieve'][$version][$resId]);
                 }
@@ -386,22 +387,22 @@ class MaarchParapheurController
         return $aArgs['idsToRetrieve'];
     }
 
-    public static function getDocumentStatus(array $aArgs)
+    public static function getDocumentWorkflow(array $aArgs)
     {
         $response = CurlModel::exec([
-            'url'      => rtrim($aArgs['config']['data']['url'], '/') . '/rest/documents/'.$aArgs['documentId'].'/status',
+            'url'      => rtrim($aArgs['config']['data']['url'], '/') . '/rest/documents/'.$aArgs['documentId'].'/workflow',
             'user'     => $aArgs['config']['data']['userId'],
             'password' => $aArgs['config']['data']['password'],
             'method'   => 'GET'
         ]);
 
-        return $response['status'];
+        return $response['workflow'];
     }
 
-    public static function getProcessedDocument(array $aArgs)
+    public static function getDocument(array $aArgs)
     {
         $response = CurlModel::exec([
-            'url'      => rtrim($aArgs['config']['data']['url'], '/') . '/rest/documents/'.$aArgs['documentId'].'/processedDocument',
+            'url'      => rtrim($aArgs['config']['data']['url'], '/') . '/rest/documents/'.$aArgs['documentId'],
             'user'     => $aArgs['config']['data']['userId'],
             'password' => $aArgs['config']['data']['password'],
             'method'   => 'GET'
@@ -409,4 +410,22 @@ class MaarchParapheurController
 
         return $response['encodedDocument'];
     }
+
+    public static function getState($aArgs)
+    {
+        $step['status'] == 'validated';
+        foreach ($aArgs['workflow'] as $step) {
+            if ($step['status'] == 'REF') {
+                $state['status'] = 'refused';
+                $state['note']   = $step['note'];
+                break;
+            } elseif (empty($step['status'])) {
+                $state['status'] = 'inProgress';
+                break;
+            }
+        }
+
+        $state['mode'] = $step['mode'];
+        return $state;
+    }
 }
-- 
GitLab