From 563066df88ef9fce7cd9872521ddd9be939d7ae1 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Tue, 14 Jan 2020 18:01:30 +0100
Subject: [PATCH] FEAT #10633 TIME 1:00 Continue circuit action

---
 core/xml/actions_pages.xml                    |  2 +-
 migration/19.12/1912.sql                      |  1 +
 rest/index.php                                |  1 +
 .../controllers/ActionMethodController.php    | 40 +++++++++++++---
 .../PreProcessActionController.php            | 47 +++++++++++++++++++
 5 files changed, 84 insertions(+), 7 deletions(-)

diff --git a/core/xml/actions_pages.xml b/core/xml/actions_pages.xml
index e789f4a0844..276973015fe 100755
--- a/core/xml/actions_pages.xml
+++ b/core/xml/actions_pages.xml
@@ -255,7 +255,7 @@ An action page is described in a ACTIONPAGE tag :
         <LABEL>_PROCEED_WORKFLOW</LABEL>
         <NAME>visa_workflow</NAME>
         <DESC>_PROCEED_WORKFLOW_DESC</DESC>
-        <component>v1Action</component>
+        <component>continueCircuitAction</component>
         <ORIGIN>module</ORIGIN>
         <MODULE>visa</MODULE>
         <FLAG_CREATE>false</FLAG_CREATE>
diff --git a/migration/19.12/1912.sql b/migration/19.12/1912.sql
index ef2e646f49f..7f81a689590 100644
--- a/migration/19.12/1912.sql
+++ b/migration/19.12/1912.sql
@@ -91,6 +91,7 @@ UPDATE actions SET component = 'rejectVisaBackToPreviousAction' WHERE action_pag
 UPDATE actions SET component = 'rejectVisaBackToRedactorAction' WHERE action_page = 'rejection_visa_redactor';
 UPDATE actions SET component = 'interruptVisaAction' WHERE action_page = 'interrupt_visa';
 UPDATE actions SET component = 'sendSignatureBookAction' WHERE action_page = 'send_to_visa';
+UPDATE actions SET component = 'continueCircuitAction' WHERE action_page = 'visa_workflow';
 
 
 /* FOLDERS */
diff --git a/rest/index.php b/rest/index.php
index ab666c492d3..153200ca0fc 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -381,6 +381,7 @@ $app->post('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/ch
 $app->get('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/actions/{actionId}/getRedirect', \Action\controllers\PreProcessActionController::class . ':getRedirectInformations');
 $app->post('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/actions/{actionId}/checkShippings', \Action\controllers\PreProcessActionController::class . ':checkShippings');
 $app->post('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/actions/{actionId}/checkSignatureBook', \Action\controllers\PreProcessActionController::class . ':checkSignatureBook');
+$app->post('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/actions/{actionId}/checkContinueCircuit', \Action\controllers\PreProcessActionController::class . ':checkContinueCircuit');
 
 //Search
 $app->get('/search', \Search\controllers\SearchController::class . ':get');
diff --git a/src/app/action/controllers/ActionMethodController.php b/src/app/action/controllers/ActionMethodController.php
index 657e4b39c6e..5c2b164a28d 100644
--- a/src/app/action/controllers/ActionMethodController.php
+++ b/src/app/action/controllers/ActionMethodController.php
@@ -52,6 +52,7 @@ class ActionMethodController
         'updateAcknowledgementSendDateAction'   => 'updateAcknowledgementSendDateAction',
         'sendShippingAction'                    => 'createMailevaShippings',
         'sendSignatureBookAction'               => 'sendSignatureBook',
+        'continueCircuitAction'                 => 'continueCircuit',
         'rejectVisaBackToPrevious'              => 'rejectVisaBackToPrevious',
         'redirectInitiatorEntityAction'         => 'redirectInitiatorEntityAction',
         'rejectVisaBackToPreviousAction'        => 'rejectVisaBackToPrevious',
@@ -351,6 +352,34 @@ class ActionMethodController
         return true;
     }
 
+    public function continueCircuit(array $args)
+    {
+        ValidatorModel::notEmpty($args, ['resId']);
+        ValidatorModel::intVal($args, ['resId']);
+
+        $listInstance = ListInstanceModel::get([
+            'select'    => ['listinstance_id'],
+            'where'     => ['res_id = ?', 'difflist_type = ?', 'process_date is null'],
+            'data'      => [$args['resId'], 'VISA_CIRCUIT'],
+            'orderBy'   => ['listinstance_id'],
+            'limit'     => 1
+        ]);
+
+        if (empty($listInstance[0])) {
+            return ['errors' => ['No available circuit']];
+        }
+
+        ListInstanceModel::update([
+            'set'   => [
+                'process_date' => 'CURRENT_TIMESTAMP'
+            ],
+            'where' => ['listinstance_id = ?'],
+            'data'  => [$listInstance[0]['listinstance_id']]
+        ]);
+
+        return true;
+    }
+
     public static function sendExternalNoteBookAction(array $args)
     {
         ValidatorModel::notEmpty($args, ['resId']);
@@ -403,11 +432,11 @@ class ActionMethodController
         ValidatorModel::intVal($args, ['resId']);
 
         $listInstances = ListInstanceModel::get([
-            'select'   => ['listinstance_id'],
-            'where'    => ['res_id = ?', 'difflist_type = ?', 'process_date is not null'],
-            'data'     => [$args['resId'], 'VISA_CIRCUIT'],
-            'orderBy' => ['listinstance_id desc'],
-            'limit'    => 1
+            'select'    => ['listinstance_id'],
+            'where'     => ['res_id = ?', 'difflist_type = ?', 'process_date is not null'],
+            'data'      => [$args['resId'], 'VISA_CIRCUIT'],
+            'orderBy'   => ['listinstance_id desc'],
+            'limit'     => 1
         ]);
 
         if (empty($listInstances)) {
@@ -466,7 +495,6 @@ class ActionMethodController
             ]);
         }
 
-
         ListInstanceModel::update([
             'set'   => [
                 'process_date' => 'CURRENT_TIMESTAMP',
diff --git a/src/app/action/controllers/PreProcessActionController.php b/src/app/action/controllers/PreProcessActionController.php
index da5a75827cf..3afe3f0be7e 100755
--- a/src/app/action/controllers/PreProcessActionController.php
+++ b/src/app/action/controllers/PreProcessActionController.php
@@ -22,6 +22,7 @@ use Convert\controllers\ConvertPdfController;
 use Docserver\models\DocserverModel;
 use Doctype\models\DoctypeModel;
 use Entity\models\EntityModel;
+use Entity\models\ListInstanceModel;
 use ExternalSignatoryBook\controllers\MaarchParapheurController;
 use Group\models\GroupModel;
 use Parameter\models\ParameterModel;
@@ -884,6 +885,52 @@ class PreProcessActionController
         return $response->withJson(['resourcesInformations' => $resourcesInformations]);
     }
 
+    public function checkContinueCircuit(Request $request, Response $response, array $args)
+    {
+        $body = $request->getParsedBody();
+
+        if (!Validator::arrayType()->notEmpty()->validate($body['resources'])) {
+            return $response->withStatus(400)->withJson(['errors' => 'Body resources is empty or not an array']);
+        }
+
+        $errors = ResourceListController::listControl(['groupId' => $args['groupId'], 'userId' => $args['userId'], 'basketId' => $args['basketId'], 'currentUserId' => $GLOBALS['id']]);
+        if (!empty($errors['errors'])) {
+            return $response->withStatus($errors['code'])->withJson(['errors' => $errors['errors']]);
+        }
+
+        $body['resources'] = array_slice($body['resources'], 0, 500);
+        if (!ResController::hasRightByResId(['resId' => $body['resources'], 'userId' => $GLOBALS['id']])) {
+            return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
+        }
+
+        $signableAttachmentsTypes = [];
+        $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
+        foreach ($attachmentsTypes as $key => $type) {
+            if ($type['sign']) {
+                $signableAttachmentsTypes[] = $key;
+            }
+        }
+
+        $resourcesInformations = [];
+        foreach ($body['resources'] as $resId) {
+            $resource = ResModel::getById(['resId' => $resId, 'select' => ['alt_identifier']]);
+            if (empty($resource['alt_identifier'])) {
+                $resource['alt_identifier'] = _UNDEFINED;
+            }
+
+            $isSignatory = ListInstanceModel::get(['select' => ['signatory', 'requested_signature'], 'where' => ['res_id = ?', 'difflist_type = ?', 'process_date is null'], 'data' => [$resId, 'VISA_CIRCUIT'], 'orderBy' => ['listinstance_id'], 'limit' => 1]);
+            if (empty($isSignatory[0])) {
+                $resourcesInformations['error'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'noCircuitAvailable'];
+            } elseif ($isSignatory[0]['requested_signature'] && !$isSignatory[0]['signatory']) {
+                $resourcesInformations['warning'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'userHasntSigned'];
+            } else {
+                $resourcesInformations['success'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId];
+            }
+        }
+
+        return $response->withJson(['resourcesInformations' => $resourcesInformations]);
+    }
+
     public function isDestinationChanging(Request $request, Response $response, array $args)
     {
         if (!ResController::hasRightByResId(['resId' => [$args['resId']], 'userId' => $GLOBALS['id']])) {
-- 
GitLab