From ed711821a548c8931d0bbca290fd7b762dc723e6 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Tue, 14 Jan 2020 10:44:58 +0100
Subject: [PATCH] FEAT #10633 TIME 0:15 Action visa

---
 .../controllers/ActionMethodController.php    | 31 +++++++++++++++++--
 .../PreProcessActionController.php            |  3 +-
 2 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/src/app/action/controllers/ActionMethodController.php b/src/app/action/controllers/ActionMethodController.php
index 45e513384db..2eec67ad3ef 100644
--- a/src/app/action/controllers/ActionMethodController.php
+++ b/src/app/action/controllers/ActionMethodController.php
@@ -16,6 +16,7 @@ use AcknowledgementReceipt\models\AcknowledgementReceiptModel;
 use Action\models\ActionModel;
 use Action\models\BasketPersistenceModel;
 use Action\models\ResMarkAsReadModel;
+use Attachment\models\AttachmentModel;
 use Entity\controllers\ListInstanceController;
 use Entity\models\ListInstanceModel;
 use ExternalSignatoryBook\controllers\MaarchParapheurController;
@@ -48,6 +49,7 @@ class ActionMethodController
         'createAcknowledgementReceiptsAction'   => 'createAcknowledgementReceipts',
         'updateAcknowledgementSendDateAction'   => 'updateAcknowledgementSendDateAction',
         'sendShippingAction'                    => 'createMailevaShippings',
+        'sendSignatureBookAction'               => 'sendSignatureBook',
         'noConfirmAction'                       => null
     ];
 
@@ -253,8 +255,6 @@ class ActionMethodController
         ValidatorModel::intVal($args, ['resId']);
         ValidatorModel::arrayType($args, ['data']);
 
-        $currentUser = UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id']]);
-
         $listInstances = [];
         if (!empty($args['data']['onlyRedirectDest'])) {
             if (count($args['data']['listInstances']) == 1) {
@@ -263,7 +263,7 @@ class ActionMethodController
         }
 
         $listInstances = array_merge($listInstances, $args['data']['listInstances']);
-        $controller = ListInstanceController::updateListInstance(['data' => [['resId' => $args['resId'], 'listInstances' => $listInstances]], 'userId' => $currentUser['id']]);
+        $controller = ListInstanceController::updateListInstance(['data' => [['resId' => $args['resId'], 'listInstances' => $listInstances]], 'userId' => $GLOBALS['id']]);
         if (!empty($controller['errors'])) {
             return ['errors' => [$controller['errors']]];
         }
@@ -271,6 +271,31 @@ class ActionMethodController
         return true;
     }
 
+    public function sendSignatureBook(array $args)
+    {
+        ValidatorModel::notEmpty($args, ['resId']);
+        ValidatorModel::intVal($args, ['resId']);
+
+        $signableAttachmentsTypes = [];
+        $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
+        foreach ($attachmentsTypes as $key => $type) {
+            if ($type['sign']) {
+                $signableAttachmentsTypes[] = $key;
+            }
+        }
+
+        $attachments = AttachmentModel::get([
+            'select'    => [1],
+            'where'     => ['res_id_master = ?', 'attachment_type in (?)', 'in_signature_book = ?', 'status not in (?)'],
+            'data'      => [$args['resId'], $signableAttachmentsTypes, true, ['OBS', 'DEL', 'FRZ']]
+        ]);
+        if (empty($attachments)) {
+            return ['errors' => ['No available attachments']];
+        }
+
+        return true;
+    }
+
     public static function sendExternalNoteBookAction(array $args)
     {
         ValidatorModel::notEmpty($args, ['resId']);
diff --git a/src/app/action/controllers/PreProcessActionController.php b/src/app/action/controllers/PreProcessActionController.php
index ad1f880e552..2c9e7828190 100755
--- a/src/app/action/controllers/PreProcessActionController.php
+++ b/src/app/action/controllers/PreProcessActionController.php
@@ -834,8 +834,7 @@ class PreProcessActionController
             $attachments = AttachmentModel::get([
                 'select'    => [1],
                 'where'     => ['res_id_master = ?', 'attachment_type in (?)', 'in_signature_book = ?', 'status not in (?)'],
-                'data'      => [$resId, $signableAttachmentsTypes, true, ['OBS', 'DEL', 'FRZ']],
-                'groupBy'   => ['res_id_master']
+                'data'      => [$resId, $signableAttachmentsTypes, true, ['OBS', 'DEL', 'FRZ']]
             ]);
 
             if (empty($attachments)) {
-- 
GitLab