From ac95e0bf224a96dab1a400b699e2d8b95745c043 Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Fri, 31 Jan 2020 11:07:40 +0100
Subject: [PATCH] FEAT #12346 TIME 3:00 send resource to maileva preprocess

---
 .../PreProcessActionController.php            | 56 ++++++++++++++++++-
 .../ShippingTemplateController.php            |  9 +--
 2 files changed, 60 insertions(+), 5 deletions(-)

diff --git a/src/app/action/controllers/PreProcessActionController.php b/src/app/action/controllers/PreProcessActionController.php
index eeaf8569482..72720c74e72 100755
--- a/src/app/action/controllers/PreProcessActionController.php
+++ b/src/app/action/controllers/PreProcessActionController.php
@@ -791,13 +791,67 @@ class PreProcessActionController
                             break;
                         }
 
+                        $attachment['collId'] = 'attachments_coll';
                         $resources[] = $attachment;
                         unset($aAttachments[$key]);
                     }
                 }
 
+                $resInfo = ResModel::getById(['select' => ['alt_identifier', 'integrations', 'res_id', 'subject as title'], 'resId' => $valueResId]);
+                $integrations = json_decode($resInfo['integrations'], true);
+                if (!empty($integrations['inShipping'])) {
+                    $resIdFound = true;
+
+                    $convertedDocument = ConvertPdfController::getConvertedPdfById([
+                        'select' => ['docserver_id', 'path', 'filename', 'fingerprint'],
+                        'resId'  => $valueResId,
+                        'collId' => 'letterbox_coll',
+                        'type'   => 'PDF'
+                    ]);
+                    if (empty($convertedDocument['docserver_id'])) {
+                        $canNotSend[] = [
+                            'resId'  => $valueResId, 'chrono' => $resInfo['alt_identifier'],
+                            'reason' => 'noAttachmentConversion', 'attachmentIdentifier' => $resInfo['identifier']
+                        ];
+                    } else {
+                        $resourceContacts = ResourceContactModel::get([
+                            'where' => ['res_id = ?', 'mode = ?', 'type = ?'],
+                            'data'  => [$valueResId, 'recipient', 'contact']
+                        ]);
+                        if (empty($resourceContacts)) {
+                            $canNotSend[] = [
+                                'resId'  => $valueResId, 'chrono' => $resInfo['alt_identifier'],
+                                'reason' => 'noAttachmentContact', 'attachmentIdentifier' => $resInfo['identifier']
+                            ];
+                        } else {
+                            foreach ($resourceContacts as $resourceContact) {
+                                $contact = ContactModel::getById(['select' => ['*'], 'id' => $resourceContact['item_id']]);
+                                if (empty($contact)) {
+                                    $canNotSend[] = [
+                                        'resId'  => $valueResId, 'chrono' => $resInfo['alt_identifier'],
+                                        'reason' => 'noAttachmentContact', 'attachmentIdentifier' => $resInfo['identifier']
+                                    ];
+                                } else if (!empty($contact['address_country']) && strtoupper(trim($contact['address_country'])) != 'FRANCE') {
+                                    $canNotSend[] = [
+                                        'resId'  => $valueResId, 'chrono' => $resInfo['alt_identifier'],
+                                        'reason' => 'noFranceContact', 'attachmentIdentifier' => $resInfo['identifier']
+                                    ];
+                                } else {
+                                    $afnorAddress = ContactController::getContactAfnor($contact);
+                                    if ((empty($afnorAddress[1]) && empty($afnorAddress[2])) || empty($afnorAddress[6]) || !preg_match("/^\d{5}\s/", $afnorAddress[6])) {
+                                        $canNotSend[] = [
+                                            'resId'  => $valueResId, 'chrono' => $resInfo['alt_identifier'],
+                                            'reason' => 'incompleteAddressForPostal', 'attachmentIdentifier' => $resInfo['identifier']
+                                        ];
+                                    }
+                                }
+                            }
+                        }
+                        $resInfo['collId'] = 'letterbox_coll';
+                        $resources[] = $resInfo;
+                    }
+                }
                 if (!$resIdFound) {
-                    $resInfo = ResModel::getById(['select' => ['alt_identifier'], 'resId' => $valueResId]);
                     $canNotSend[] = ['resId' => $valueResId, 'chrono' => $resInfo['alt_identifier'], 'reason' => 'noAttachmentToSend'];
                 }
             }
diff --git a/src/app/shipping/controllers/ShippingTemplateController.php b/src/app/shipping/controllers/ShippingTemplateController.php
index f1d497e748e..d3b0964d790 100755
--- a/src/app/shipping/controllers/ShippingTemplateController.php
+++ b/src/app/shipping/controllers/ShippingTemplateController.php
@@ -289,10 +289,11 @@ class ShippingTemplateController
     {
         $fee = 0;
         foreach ($aArgs['resources'] as $value) {
-            $attachmentId = $value['res_id'];
-            $convertedAttachment = ConvertPdfController::getConvertedPdfById(['select' => ['docserver_id', 'path', 'filename'], 'resId' => $attachmentId, 'collId' => 'attachments_coll']);
-            $docserver           = DocserverModel::getByDocserverId(['docserverId' => $convertedAttachment['docserver_id'], 'select' => ['path_template']]);
-            $pathToDocument      = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $convertedAttachment['path']) . $convertedAttachment['filename'];
+            $resourceId = $value['res_id'];
+
+            $convertedResource = ConvertPdfController::getConvertedPdfById(['select' => ['docserver_id', 'path', 'filename'], 'resId' => $resourceId, 'collId' => $value['collId']]);
+            $docserver         = DocserverModel::getByDocserverId(['docserverId' => $convertedResource['docserver_id'], 'select' => ['path_template']]);
+            $pathToDocument    = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $convertedResource['path']) . $convertedResource['filename'];
 
             $img = new \Imagick();
             $img->pingImage($pathToDocument);
-- 
GitLab