From aad2e3072f975ae2b7702c46ab84deaa19e4a337 Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Wed, 12 Feb 2020 23:45:39 +0100
Subject: [PATCH] FEAT #12345 TIME 0:30 mailing before send to external
 signatory book + fix count attachment

---
 .../ExternalSignatoryBookTrait.php            | 39 +++++++++++--------
 .../PreProcessActionController.php            |  4 +-
 .../controllers/AttachmentController.php      |  2 -
 .../controllers/ResourceListController.php    |  3 +-
 4 files changed, 25 insertions(+), 23 deletions(-)

diff --git a/src/app/action/controllers/ExternalSignatoryBookTrait.php b/src/app/action/controllers/ExternalSignatoryBookTrait.php
index 4027cec4532..b51716950d4 100644
--- a/src/app/action/controllers/ExternalSignatoryBookTrait.php
+++ b/src/app/action/controllers/ExternalSignatoryBookTrait.php
@@ -12,6 +12,7 @@
 
 namespace Action\controllers;
 
+use Attachment\controllers\AttachmentController;
 use Attachment\models\AttachmentModel;
 use ExternalSignatoryBook\controllers\MaarchParapheurController;
 use ExternalSignatoryBook\controllers\XParaphController;
@@ -38,6 +39,25 @@ trait ExternalSignatoryBookTrait
                 }
             }
 
+            if (!empty($config['id'])) {
+                $attachments = AttachmentModel::get([
+                    'select'    => [
+                        'res_id', 'status'
+                    ],
+                    'where'     => ["res_id_master = ?", "attachment_type not in (?)", "status not in ('DEL', 'OBS', 'FRZ', 'TMP')", "in_signature_book = 'true'"],
+                    'data'      => [$args['resId'], ['converted_pdf', 'print_folder', 'signed_response']]
+                ]);
+
+                foreach ($attachments as $attachment) {
+                    if ($attachment['status'] == 'SEND_MASS') {
+                        $generated = AttachmentController::generateMailing(['id' => $attachment['res_id'], 'userId' => $GLOBALS['id']]);
+                        if (!empty($generated['errors'])) {
+                            return ['errors' => $generated['errors']];
+                        }
+                    }
+                }
+            }
+
             if ($config['id'] == 'ixbus') {
                 // TODO
             } elseif ($config['id'] == 'iParapheur') {
@@ -45,21 +65,13 @@ trait ExternalSignatoryBookTrait
             } elseif ($config['id'] == 'fastParapheur') {
                 // TODO
             } elseif ($config['id'] == 'maarchParapheur') {
-                $attachments = AttachmentModel::get([
-                    'select'    => [
-                        'count(1) as nb'
-                    ],
-                    'where'     => ["res_id_master = ?", "attachment_type not in (?)", "status not in ('DEL', 'OBS', 'FRZ', 'TMP', 'SEND_MASS')", "in_signature_book = 'true'"],
-                    'data'      => [$args['resId'], ['converted_pdf', 'print_folder', 'signed_response']]
-                ]);
-
                 $integratedResource = ResModel::get([
                     'select' => [1],
                     'where'  => ['integrations->>\'inSignatureBook\' = \'true\'', 'external_id->>\'signatureBookId\' is null', 'res_id = ?'],
                     'data'   => [$args['resId']]
                 ]);
 
-                if ($attachments[0]['nb'] == 0 && empty($integratedResource) && $args['data']['objectSent'] == 'attachment') {
+                if (empty($attachments) && empty($integratedResource) && $args['data']['objectSent'] == 'attachment') {
                     $noAttachmentsResource = ResModel::getById(['resId' => $args['resId'], 'select' => ['alt_identifier']]);
                     return ['errors' => ['No attachment for this mail : ' . $noAttachmentsResource['alt_identifier']]];
                 }
@@ -80,14 +92,7 @@ trait ExternalSignatoryBookTrait
 
                 $historyInfo = $sendedInfo['historyInfos'];
             } elseif ($config['id'] == 'xParaph') {
-                $attachments = AttachmentModel::get([
-                    'select'    => [
-                        'count(1) as nb'
-                    ],
-                    'where'     => ["res_id_master = ?", "attachment_type not in (?)", "status not in ('DEL', 'OBS', 'FRZ', 'TMP', 'SEND_MASS')", "in_signature_book = 'true'"],
-                    'data'      => [$args['resId'], ['converted_pdf', 'print_folder', 'signed_response']]
-                ]);
-                if ($attachments[0]['nb'] == 0) {
+                if (empty($attachments)) {
                     $noAttachmentsResource = ResModel::getById(['resId' => $args['resId'], 'select' => ['alt_identifier']]);
                     return ['errors' => ['No attachment for this mail : ' . $noAttachmentsResource['alt_identifier']]];
                 }
diff --git a/src/app/action/controllers/PreProcessActionController.php b/src/app/action/controllers/PreProcessActionController.php
index 74112da2193..cc749c10e6a 100755
--- a/src/app/action/controllers/PreProcessActionController.php
+++ b/src/app/action/controllers/PreProcessActionController.php
@@ -423,7 +423,7 @@ class PreProcessActionController
                             'status', 'typist', 'docserver_id', 'path', 'filename', 'creation_date',
                             'validation_date', 'relation', 'origin_id'
                         ],
-                        'where'     => ["res_id_master = ?", "attachment_type not in (?)", "status not in ('DEL', 'OBS', 'FRZ', 'TMP', 'SEND_MASS')", "in_signature_book = 'true'"],
+                        'where'     => ["res_id_master = ?", "attachment_type not in (?)", "status not in ('DEL', 'OBS', 'FRZ', 'TMP')", "in_signature_book = 'true'"],
                         'data'      => [$resId, ['converted_pdf', 'print_folder', 'signed_response']]
                     ]);
 
@@ -507,7 +507,7 @@ class PreProcessActionController
                             'status', 'typist', 'docserver_id', 'path', 'filename', 'creation_date',
                             'validation_date', 'relation', 'origin_id'
                         ],
-                        'where'     => ["res_id_master = ?", "attachment_type not in (?)", "status not in ('DEL', 'OBS', 'FRZ', 'TMP', 'SEND_MASS')", "in_signature_book = 'true'"],
+                        'where'     => ["res_id_master = ?", "attachment_type not in (?)", "status not in ('DEL', 'OBS', 'FRZ', 'TMP')", "in_signature_book = 'true'"],
                         'data'      => [$resId, ['converted_pdf', 'print_folder', 'signed_response']]
                     ]);
                     
diff --git a/src/app/attachment/controllers/AttachmentController.php b/src/app/attachment/controllers/AttachmentController.php
index 09e744e61aa..e30e2a7a65c 100755
--- a/src/app/attachment/controllers/AttachmentController.php
+++ b/src/app/attachment/controllers/AttachmentController.php
@@ -33,9 +33,7 @@ use Respect\Validation\Validator;
 use Slim\Http\Request;
 use Slim\Http\Response;
 use SrcCore\models\CoreConfigModel;
-use SrcCore\models\DatabaseModel;
 use SrcCore\models\ValidatorModel;
-use Template\controllers\TemplateController;
 use User\models\UserModel;
 
 class AttachmentController
diff --git a/src/app/resource/controllers/ResourceListController.php b/src/app/resource/controllers/ResourceListController.php
index e847c5cd042..445214718ef 100644
--- a/src/app/resource/controllers/ResourceListController.php
+++ b/src/app/resource/controllers/ResourceListController.php
@@ -25,7 +25,6 @@ use Contact\controllers\ContactController;
 use Entity\models\EntityModel;
 use Entity\models\ListInstanceModel;
 use Folder\models\FolderModel;
-use Group\controllers\PrivilegeController;
 use Group\models\GroupModel;
 use Note\models\NoteModel;
 use Priority\models\PriorityModel;
@@ -88,7 +87,7 @@ class ResourceListController
         $defaultAction = [];
         $displayFolderTags = false;
         if (!empty($resIds)) {
-            $excludeAttachmentTypes = ['converted_pdf', 'print_folder'];
+            $excludeAttachmentTypes = ['converted_pdf', 'print_folder', 'signed_response'];
             $attachments = AttachmentModel::get([
                 'select'    => ['COUNT(res_id)', 'res_id_master'],
                 'where'     => ['res_id_master in (?)', 'status not in (?)', 'attachment_type not in (?)', '((status = ? AND typist = ?) OR status != ?)'],
-- 
GitLab