From 41b16fe7a1678c2c2072af3aeaf98b8434be8487 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Fri, 27 Dec 2019 15:57:58 +0100
Subject: [PATCH] FEAT #11991 TIME 0:45 Send note to MP

---
 .../controllers/ActionMethodController.php    | 11 ++--
 .../ExternalSignatoryBookTrait.php            |  1 +
 .../PreProcessActionController.php            |  6 +-
 .../controllers/AttachmentController.php      |  4 +-
 .../controllers/MaarchParapheurController.php | 61 ++++++++++++-------
 .../maarch-paraph/maarch-paraph.component.ts  |  2 +-
 6 files changed, 52 insertions(+), 33 deletions(-)

diff --git a/src/app/action/controllers/ActionMethodController.php b/src/app/action/controllers/ActionMethodController.php
index cde80cd3ba4..45e513384db 100644
--- a/src/app/action/controllers/ActionMethodController.php
+++ b/src/app/action/controllers/ActionMethodController.php
@@ -290,11 +290,12 @@ class ActionMethodController
 
             $processingUserInfo = MaarchParapheurController::getUserById(['config' => $config, 'id' => $args['data']['processingUser']]);
             $sendedInfo = MaarchParapheurController::sendDatas([
-                'config'           => $config,
-                'resIdMaster'      => $args['resId'],
-                'processingUser'   => $args['data']['processingUser'],
-                'objectSent'       => 'mail',
-                'userId'           => $GLOBALS['userId']
+                'config'            => $config,
+                'resIdMaster'       => $args['resId'],
+                'processingUser'    => $args['data']['processingUser'],
+                'objectSent'        => 'mail',
+                'userId'            => $GLOBALS['userId'],
+                'note'              => $args['note'] ?? null
             ]);
             if (!empty($sendedInfo['error'])) {
                 return ['errors' => [$sendedInfo['error']]];
diff --git a/src/app/action/controllers/ExternalSignatoryBookTrait.php b/src/app/action/controllers/ExternalSignatoryBookTrait.php
index 2406adc0bcb..2ee4146b0f4 100644
--- a/src/app/action/controllers/ExternalSignatoryBookTrait.php
+++ b/src/app/action/controllers/ExternalSignatoryBookTrait.php
@@ -63,6 +63,7 @@ trait ExternalSignatoryBookTrait
                     'objectSent'  => 'attachment',
                     'userId'      => $GLOBALS['userId'],
                     'steps'       => $args['data']['steps'],
+                    'note'        => $args['note'] ?? null
                 ]);
                 if (!empty($sendedInfo['error'])) {
                     return ['errors' => [$sendedInfo['error']]];
diff --git a/src/app/action/controllers/PreProcessActionController.php b/src/app/action/controllers/PreProcessActionController.php
index 1ebe09dad54..9307722510b 100755
--- a/src/app/action/controllers/PreProcessActionController.php
+++ b/src/app/action/controllers/PreProcessActionController.php
@@ -419,9 +419,11 @@ class PreProcessActionController
                         'select'   => ['destination'],
                         'resId'    => $data['resources'][0]
                     ]);
-                    $destination = EntityModel::getByEntityId(['entityId' => $resDestination['destination'], 'select' => ['id']]);
+                    if (!empty($resDestination['destination'])) {
+                        $destination = EntityModel::getByEntityId(['entityId' => $resDestination['destination'], 'select' => ['id']]);
+                        $additionalsInfos['destinationId'] = $destination['id'];
+                    }
 
-                    $additionalsInfos['destinationId'] = $destination['id'];
                 } else {
                     $additionalsInfos['destinationId'] = '';
                 }
diff --git a/src/app/attachment/controllers/AttachmentController.php b/src/app/attachment/controllers/AttachmentController.php
index adeba2945c9..5c0b5716257 100755
--- a/src/app/attachment/controllers/AttachmentController.php
+++ b/src/app/attachment/controllers/AttachmentController.php
@@ -882,9 +882,9 @@ class AttachmentController
                 return ['errors' => 'Body recipientType is empty or not in [user, contact]'];
             }
             if ($body['recipientType'] == 'user') {
-                $recipient = UserModel::getById(['id' => $body['destUserId'], 'select' => [1], 'noDeleted' => true]);
+                $recipient = UserModel::getById(['id' => $body['recipientId'], 'select' => [1], 'noDeleted' => true]);
             } elseif ($body['recipientType'] == 'contact') {
-                $recipient = ContactModel::getById(['id' => $body['contactId'], 'select' => [1]]);
+                $recipient = ContactModel::getById(['id' => $body['recipientId'], 'select' => [1]]);
             }
             if (empty($recipient)) {
                 return ['errors' => 'Body recipientId does not exist'];
diff --git a/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php b/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php
index 0348537cbd8..7ac85f6b201 100755
--- a/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php
+++ b/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php
@@ -67,26 +67,27 @@ class MaarchParapheurController
     {
         $attachmentToFreeze = [];
 
-        $adrMainInfo = ConvertPdfController::getConvertedPdfById(['resId' => $aArgs['resIdMaster'], 'collId' => 'letterbox_coll']);
-        if (empty($adrMainInfo['docserver_id'])) {
-            return ['error' => 'Document ' . $aArgs['resIdMaster'] . ' is not converted in pdf'];
-        }
-        $docserverMainInfo = DocserverModel::getByDocserverId(['docserverId' => $adrMainInfo['docserver_id']]);
-        if (empty($docserverMainInfo['path_template'])) {
-            return ['error' => 'Docserver does not exist ' . $adrMainInfo['docserver_id']];
-        }
-        $arrivedMailMainfilePath = $docserverMainInfo['path_template'] . str_replace('#', '/', $adrMainInfo['path']) . $adrMainInfo['filename'];
-
         $mainResource = ResModel::getOnView([
-            'select' => ['process_limit_date', 'status', 'category_id', 'alt_identifier', 'subject', 'priority', 'res_id', 'admission_date', 'creation_date', 'doc_date', 'initiator', 'typist', 'type_label', 'destination'],
+            'select' => ['process_limit_date', 'status', 'category_id', 'alt_identifier', 'subject', 'priority', 'res_id', 'admission_date', 'creation_date', 'doc_date', 'initiator', 'typist', 'type_label', 'destination', 'filename'],
             'where'  => ['res_id = ?'],
             'data'   => [$aArgs['resIdMaster']]
         ]);
-        $recipients = ContactController::getFormattedContacts(['resId' => $mainResource[0]['res_id'], 'mode' => 'recipient']);
-
         if (empty($mainResource)) {
             return ['error' => 'Mail does not exist'];
         }
+        if (!empty($mainResource[0]['filename'])) {
+            $adrMainInfo = ConvertPdfController::getConvertedPdfById(['resId' => $aArgs['resIdMaster'], 'collId' => 'letterbox_coll']);
+            if (empty($adrMainInfo['docserver_id'])) {
+                return ['error' => 'Document ' . $aArgs['resIdMaster'] . ' is not converted in pdf'];
+            }
+            $docserverMainInfo = DocserverModel::getByDocserverId(['docserverId' => $adrMainInfo['docserver_id']]);
+            if (empty($docserverMainInfo['path_template'])) {
+                return ['error' => 'Docserver does not exist ' . $adrMainInfo['docserver_id']];
+            }
+            $arrivedMailMainfilePath = $docserverMainInfo['path_template'] . str_replace('#', '/', $adrMainInfo['path']) . $adrMainInfo['filename'];
+        }
+        $recipients = ContactController::getFormattedContacts(['resId' => $mainResource[0]['res_id'], 'mode' => 'recipient']);
+
 
         $units = [];
         $units[] = ['unit' => 'primaryInformations'];
@@ -149,7 +150,11 @@ class MaarchParapheurController
         $concatPdf->setPrintHeader(false);
 
         if ($aArgs['objectSent'] == 'mail') {
-            foreach ([$filename, $arrivedMailMainfilePath] as $file) {
+            $filesToConcat = [$filename];
+            if (!empty($arrivedMailMainfilePath)) {
+                $filesToConcat[] = $arrivedMailMainfilePath;
+            }
+            foreach ($filesToConcat as $file) {
                 $pageCount = $concatPdf->setSourceFile($file);
                 for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
                     $pageId = $concatPdf->ImportPage($pageNo);
@@ -251,17 +256,22 @@ class MaarchParapheurController
                         }
                         $metadata[_RECIPIENTS] = $contact;
                     }
-        
+
                     $bodyData = [
-                        'title'           => $value['title'],
-                        'reference'       => $value['identifier'],
-                        'encodedDocument' => $encodedZipDocument,
-                        'sender'          => trim($sender['firstname'] . ' ' .$sender['lastname']),
-                        'deadline'        => $processLimitDate,
-                        'attachments'     => $attachmentsData,
-                        'workflow'        => $workflow,
-                        'metadata'        => $metadata
+                        'title'             => $value['title'],
+                        'reference'         => $value['identifier'],
+                        'encodedDocument'   => $encodedZipDocument,
+                        'sender'            => trim($sender['firstname'] . ' ' .$sender['lastname']),
+                        'deadline'          => $processLimitDate,
+                        'attachments'       => $attachmentsData,
+                        'workflow'          => $workflow,
+                        'metadata'          => $metadata
                     ];
+                    if (!empty($aArgs['note'])) {
+                        $noteCreationDate = new \DateTime();
+                        $noteCreationDate = $noteCreationDate->format('Y-m-d');
+                        $bodyData['notes'] = ['creator' => trim($sender['firstname'] . ' ' .$sender['lastname']), 'creationDate' => $noteCreationDate, 'value' => $aArgs['note']];
+                    }
         
                     $response = CurlModel::exec([
                         'url'      => rtrim($aArgs['config']['data']['url'], '/') . '/rest/documents',
@@ -301,6 +311,11 @@ class MaarchParapheurController
                 'workflow'         => $workflow,
                 'metadata'         => $metadata
             ];
+            if (!empty($aArgs['note'])) {
+                $noteCreationDate = new \DateTime();
+                $noteCreationDate = $noteCreationDate->format('Y-m-d');
+                $bodyData['notes'] = ['creator' => trim($sender['firstname'] . ' ' .$sender['lastname']), 'creationDate' => $noteCreationDate, 'value' => $aArgs['note']];
+            }
 
             $response = CurlModel::exec([
                 'url'      => rtrim($aArgs['config']['data']['url'], '/') . '/rest/documents',
diff --git a/src/frontend/app/actions/send-external-signatory-book-action/maarch-paraph/maarch-paraph.component.ts b/src/frontend/app/actions/send-external-signatory-book-action/maarch-paraph/maarch-paraph.component.ts
index 98a073e9613..7e2a2f7aad0 100644
--- a/src/frontend/app/actions/send-external-signatory-book-action/maarch-paraph/maarch-paraph.component.ts
+++ b/src/frontend/app/actions/send-external-signatory-book-action/maarch-paraph/maarch-paraph.component.ts
@@ -33,7 +33,7 @@ export class MaarchParaphComponent implements OnInit {
     constructor(public http: HttpClient, private notify: NotificationService) { }
 
     ngOnInit(): void {
-        if (this.additionalsInfos.destinationId !== '') {
+        if (typeof this.additionalsInfos.destinationId !== "undefined" && this.additionalsInfos.destinationId !== '') {
             setTimeout(() => {
                 this.appVisaWorkflow.loadListModel(this.additionalsInfos.destinationId);  
             }, 0);
-- 
GitLab