From 77f64ef361ef9bc3922ffd64d921e056bd46ea2a Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Wed, 9 Sep 2020 10:13:34 +0200
Subject: [PATCH] FIX #14657 TIME 1:00 fix signatory not saved in attachment

---
 .../controllers/AttachmentController.php      |  8 +++--
 .../resource/controllers/StoreController.php  | 33 ++++++++++---------
 .../controllers/SignatureBookController.php   | 23 ++++++-------
 3 files changed, 35 insertions(+), 29 deletions(-)

diff --git a/src/app/attachment/controllers/AttachmentController.php b/src/app/attachment/controllers/AttachmentController.php
index a5f9b4e751b..6c440d0c759 100755
--- a/src/app/attachment/controllers/AttachmentController.php
+++ b/src/app/attachment/controllers/AttachmentController.php
@@ -308,12 +308,16 @@ class AttachmentController
 
             if ($attachment['status'] == 'SIGN') {
                 $signedResponse = AttachmentModel::get([
-                    'select'    => ['creation_date', 'typist'],
+                    'select'    => ['creation_date', 'typist', 'signatory_user_serial_id'],
                     'where'     => ['origin = ?', 'status not in (?)'],
                     'data'      => ["{$attachment['resId']},res_attachments", ['DEL']]
                 ]);
                 if (!empty($signedResponse[0])) {
-                    $attachments[$key]['signatory'] = UserModel::getLabelledUserById(['id' => $signedResponse[0]['typist']]);
+                    if (!empty($signedResponse[0]['signatory_user_serial_id'])) {
+                        $attachments[$key]['signatory'] = UserModel::getLabelledUserById(['id' => $signedResponse[0]['signatory_user_serial_id']]);
+                    } else {
+                        $attachments[$key]['signatory'] = UserModel::getLabelledUserById(['id' => $signedResponse[0]['typist']]);
+                    }
                     $attachments[$key]['signDate'] = $signedResponse[0]['creation_date'];
                 }
             }
diff --git a/src/app/resource/controllers/StoreController.php b/src/app/resource/controllers/StoreController.php
index 65fc05998b2..11430655def 100755
--- a/src/app/resource/controllers/StoreController.php
+++ b/src/app/resource/controllers/StoreController.php
@@ -385,22 +385,23 @@ class StoreController
 
         $inSignatureBook = isset($args['inSignatureBook']) ? $args['inSignatureBook'] : $shouldBeInSignatureBook;
         $preparedData = [
-            'title'                 => $args['title'] ?? null,
-            'identifier'            => $args['chrono'] ?? null,
-            'typist'                => $typist,
-            'status'                => $args['status'] ?? 'A_TRA',
-            'relation'              => $relation,
-            'origin_id'             => $args['originId'] ?? null,
-            'origin'                => $linkSign ?? null,
-            'res_id_master'         => $args['resIdMaster'],
-            'attachment_type'       => $args['type'],
-            'recipient_id'          => $args['recipientId'] ?? null,
-            'recipient_type'        => !empty($args['recipientId']) ? $args['recipientType'] : null,
-            'validation_date'       => $args['validationDate'] ?? null,
-            'effective_date'        => $args['effectiveDate'] ?? null,
-            'in_signature_book'     => $inSignatureBook ? 'true' : 'false',
-            'external_id'           => $externalId,
-            'creation_date'         => 'CURRENT_TIMESTAMP'
+            'title'                    => $args['title'] ?? null,
+            'identifier'               => $args['chrono'] ?? null,
+            'typist'                   => $typist,
+            'status'                   => $args['status'] ?? 'A_TRA',
+            'relation'                 => $relation,
+            'origin_id'                => $args['originId'] ?? null,
+            'origin'                   => $linkSign ?? null,
+            'res_id_master'            => $args['resIdMaster'],
+            'attachment_type'          => $args['type'],
+            'recipient_id'             => $args['recipientId'] ?? null,
+            'recipient_type'           => !empty($args['recipientId']) ? $args['recipientType'] : null,
+            'validation_date'          => $args['validationDate'] ?? null,
+            'effective_date'           => $args['effectiveDate'] ?? null,
+            'in_signature_book'        => $inSignatureBook ? 'true' : 'false',
+            'external_id'              => $externalId,
+            'creation_date'            => 'CURRENT_TIMESTAMP',
+            'signatory_user_serial_id' => !empty($args['signatory_user_serial_id']) ? $args['signatory_user_serial_id'] : null
         ];
 
         return $preparedData;
diff --git a/src/app/signatureBook/controllers/SignatureBookController.php b/src/app/signatureBook/controllers/SignatureBookController.php
index 20011a0d03e..183664b111f 100755
--- a/src/app/signatureBook/controllers/SignatureBookController.php
+++ b/src/app/signatureBook/controllers/SignatureBookController.php
@@ -595,17 +595,18 @@ class SignatureBookController
         unlink($tmpPath.$convertedDocument['filename']);
 
         $data = [
-            'title'             => $attachment['title'],
-            'encodedFile'       => base64_encode($signedDocument),
-            'format'            => 'pdf',
-            'typist'            => $attachment['typist'],
-            'resIdMaster'       => $attachment['res_id_master'],
-            'chrono'            => $attachment['identifier'],
-            'type'              => 'signed_response',
-            'originId'          => $args['id'],
-            'recipientId'       => $attachment['recipient_id'],
-            'recipientType'     => $attachment['recipient_type'],
-            'inSignatureBook'   => true
+            'title'                    => $attachment['title'],
+            'encodedFile'              => base64_encode($signedDocument),
+            'format'                   => 'pdf',
+            'typist'                   => $attachment['typist'],
+            'resIdMaster'              => $attachment['res_id_master'],
+            'chrono'                   => $attachment['identifier'],
+            'type'                     => 'signed_response',
+            'originId'                 => $args['id'],
+            'recipientId'              => $attachment['recipient_id'],
+            'recipientType'            => $attachment['recipient_type'],
+            'inSignatureBook'          => true,
+            'signatory_user_serial_id' => $GLOBALS['id']
         ];
         $id = StoreController::storeAttachment($data);
         if (!empty($id['errors'])) {
-- 
GitLab