From a7f05abbaee8097a39df078fabcaf386bc4ef431 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Tue, 11 Feb 2020 18:21:23 +0100
Subject: [PATCH] FEAT #12091 TIME 1:10 Send signed response when needed

---
 .../controllers/ActionMethodController.php    |  1 -
 src/app/action/controllers/ShippingTrait.php  | 26 ++++++++++++++-----
 .../controllers/MaarchParapheurController.php | 23 ++++++++++++++--
 3 files changed, 40 insertions(+), 10 deletions(-)

diff --git a/src/app/action/controllers/ActionMethodController.php b/src/app/action/controllers/ActionMethodController.php
index fc046a5f6e6..48bd35f7dbe 100644
--- a/src/app/action/controllers/ActionMethodController.php
+++ b/src/app/action/controllers/ActionMethodController.php
@@ -30,7 +30,6 @@ use Resource\controllers\ResController;
 use Resource\models\ResModel;
 use Respect\Validation\Validator;
 use SrcCore\models\CoreConfigModel;
-use SrcCore\models\CurlModel;
 use SrcCore\models\DatabaseModel;
 use SrcCore\models\ValidatorModel;
 use User\models\UserModel;
diff --git a/src/app/action/controllers/ShippingTrait.php b/src/app/action/controllers/ShippingTrait.php
index 5b4c53cf706..04ffef37dba 100644
--- a/src/app/action/controllers/ShippingTrait.php
+++ b/src/app/action/controllers/ShippingTrait.php
@@ -59,9 +59,9 @@ trait ShippingTrait
         $shippingTemplate['fee'] = json_decode($shippingTemplate['fee'], true);
 
         $attachments = AttachmentModel::get([
-            'select'    => ['res_id', 'title', 'recipient_id', 'recipient_type', 'external_id'],
+            'select'    => ['res_id', 'title', 'recipient_id', 'recipient_type', 'external_id', 'status'],
             'where'     => ['res_id_master = ?', 'in_send_attach = ?', 'status not in (?)', 'attachment_type not in (?)'],
-            'data'      => [$args['resId'], true, ['OBS', 'DEL', 'TMP', 'FRZ'], ['print_folder']]
+            'data'      => [$args['resId'], true, ['OBS', 'DEL', 'TMP', 'FRZ'], ['print_folder', 'signed_response']]
         ]);
 
         if (empty($attachments) && empty($integrations['inShipping'])) {
@@ -73,6 +73,16 @@ trait ShippingTrait
         $contacts = [];
         foreach ($attachments as $attachment) {
             $attachmentId = $attachment['res_id'];
+            if ($attachment['status'] == 'SIGN') {
+                $signedAttachment = AttachmentModel::get([
+                    'select'    => ['res_id'],
+                    'where'     => ['origin = ?', 'status not in (?)', 'attachment_type = ?'],
+                    'data'      => ["{$args['resId']},res_attachments", ['OBS', 'DEL', 'TMP', 'FRZ'], 'signed_response']
+                ]);
+                if (!empty($signedAttachment[0])) {
+                    $attachmentId = $signedAttachment[0]['res_id'];
+                }
+            }
 
             $convertedDocument = AdrModel::getConvertedDocumentById([
                 'select'    => ['docserver_id','path', 'filename', 'fingerprint'],
@@ -105,12 +115,14 @@ trait ShippingTrait
 
         $contactsResource = [];
         if (!empty($integrations['inShipping'])) {
-            $convertedDocument = AdrModel::getConvertedDocumentById([
-                'select'    => ['docserver_id','path', 'filename', 'fingerprint'],
-                'resId'     => $args['resId'],
-                'collId'    => 'letterbox_coll',
-                'type'      => 'PDF'
+            $convertedDocument = AdrModel::getDocuments([
+                'select'    => ['docserver_id', 'path', 'filename', 'fingerprint'],
+                'where'     => ['res_id = ?', 'type in (?)', 'version = ?'],
+                'data'      => [$args['resId'], ['PDF', 'SIGN'], $resource['version']],
+                'orderBy'   => ['version', "type='SIGN' DESC"],
+                'limit'     => 1
             ]);
+            $convertedDocument = $convertedDocument[0] ?? null;
             if (empty($convertedDocument)) {
                 return ['errors' => ['No conversion for resource']];
             }
diff --git a/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php b/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php
index 9f985e22125..59423022d7e 100755
--- a/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php
+++ b/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php
@@ -17,6 +17,7 @@ namespace ExternalSignatoryBook\controllers;
 use Attachment\models\AttachmentModel;
 use Contact\controllers\ContactController;
 use Convert\controllers\ConvertPdfController;
+use Convert\models\AdrModel;
 use Docserver\models\DocserverModel;
 use Entity\models\EntityModel;
 use Entity\models\ListInstanceModel;
@@ -246,8 +247,26 @@ class MaarchParapheurController
                 foreach ($attachments as $value) {
                     $resId  = $value['res_id'];
                     $collId = 'attachments_coll';
-                    
-                    $adrInfo = ConvertPdfController::getConvertedPdfById(['resId' => $resId, 'collId' => $collId]);
+
+                    if ($value['status'] == 'SIGN') {
+                        $signedAttachment = AttachmentModel::get([
+                            'select'    => ['res_id'],
+                            'where'     => ['origin = ?', 'status not in (?)', 'attachment_type = ?'],
+                            'data'      => ["{$resId},res_attachments", ['OBS', 'DEL', 'TMP', 'FRZ'], 'signed_response']
+                        ]);
+                        if (!empty($signedAttachment[0])) {
+                            $adrInfo = AdrModel::getConvertedDocumentById([
+                                'select'    => ['docserver_id','path', 'filename', 'fingerprint'],
+                                'resId'     => $signedAttachment[0]['res_id'],
+                                'collId'    => 'attachments_coll',
+                                'type'      => 'PDF'
+                            ]);
+                        }
+                    }
+
+                    if (empty($adrInfo)) {
+                        $adrInfo = ConvertPdfController::getConvertedPdfById(['resId' => $resId, 'collId' => $collId]);
+                    }
                     if (empty($adrInfo['docserver_id']) || strtolower(pathinfo($adrInfo['filename'], PATHINFO_EXTENSION)) != 'pdf') {
                         return ['error' => 'Attachment ' . $resId . ' is not converted in pdf'];
                     }
-- 
GitLab