From aa643fcd00c85ff1d9aad37319c06742a532556a Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Fri, 19 Oct 2018 16:10:12 +0200
Subject: [PATCH] FEAT #8657 Chrono

---
 apps/maarch_entreprise/js/indexing.js         |  1 +
 core/class/ExportControler.php                | 11 ++++
 .../resource/controllers/StoreController.php  | 57 ++++++++++---------
 3 files changed, 41 insertions(+), 28 deletions(-)

diff --git a/apps/maarch_entreprise/js/indexing.js b/apps/maarch_entreprise/js/indexing.js
index 816987297e1..5cd7682dfd9 100755
--- a/apps/maarch_entreprise/js/indexing.js
+++ b/apps/maarch_entreprise/js/indexing.js
@@ -1949,6 +1949,7 @@ function initSenderRecipientAutocomplete() {
         templateValue: "{{otherInfo}}",
         minLength: 3,
         dynamic: true,
+        filter: false,
         source: {
             ajax: function (query) {
                 return {
diff --git a/core/class/ExportControler.php b/core/class/ExportControler.php
index 0f5de287ccf..c63d481c4f3 100755
--- a/core/class/ExportControler.php
+++ b/core/class/ExportControler.php
@@ -371,6 +371,17 @@ class ExportFunctions
         $this->object_export->{$this->pos}->get_tags = implode(' ## ', $arr_tags);
     }
 
+    public function get_contact_type($libelle, $res_id)
+    {
+        $db = new Database();
+
+        $query = 'SELECT ct.label from contacts_v2 cont LEFT JOIN mlb_coll_ext mlb ON (mlb.exp_contact_id = cont.contact_id OR mlb.dest_contact_id = cont.contact_id) LEFT JOIN contact_types ct ON ct.id = cont.contact_type WHERE mlb.res_id = ?';
+        $stmt = $db->query($query, array($res_id));
+        $result = $stmt->fetchObject();
+
+        $this->object_export->{$this->pos}->get_contact_type = $result->label;
+    }
+
     public function get_contact_civility($libelle, $res_id)
     {
         $db = new Database();
diff --git a/src/app/resource/controllers/StoreController.php b/src/app/resource/controllers/StoreController.php
index c16cd8937e9..92c20492285 100644
--- a/src/app/resource/controllers/StoreController.php
+++ b/src/app/resource/controllers/StoreController.php
@@ -183,40 +183,41 @@ class StoreController
         ValidatorModel::stringType($aArgs, ['docserver_id', 'filename', 'format', 'path', 'fingerprint', 'status']);
         ValidatorModel::intVal($aArgs, ['filesize']);
 
-        $typistFound        = false;
-        $processLimitDateFound  = false;
-
-        foreach ($aArgs as $key => $value) {
-            if ($key == 'typist') {
-                $typistFound = true;
-            } elseif ($key == 'process_limit_date') {
-                $processLimitDateFound = true;
-            } elseif ($key == 'exp_contact_id' && !is_numeric($value)) {
-                $mail = explode('<', str_replace('>', '', $value));
-                $contact = ContactModel::getByEmail(['email' => $mail[count($mail) - 1], 'select' => ['contacts_v2.contact_id']]);
-                if (!empty($contact['contact_id'])) {
-                    $aArgs['exp_contact_id'] = $contact['contact_id'];
-                } else {
-                    $aArgs['exp_contact_id'] = 0;
-                }
-            } elseif ($key == 'address_id' && !is_numeric($value)) {
-                $mail = explode('<', str_replace('>', '', $value));
-                $contact = ContactModel::getByEmail(['email' => $mail[count($mail) - 1], 'select' => ['contact_addresses.id']]);
-                if (!empty($contact['id'])) {
-                    $aArgs['address_id'] = $contact['id'];
-                } else {
-                    $aArgs['address_id'] = 0;
-                }
-            }
+        if (empty($aArgs['typist'])) {
+            $aArgs['typist'] = 'auto';
         }
 
-        if (!$typistFound) {
-            $aArgs['typist'] = 'auto';
+        unset($aArgs['alt_identifier']);
+        if (!empty($aArgs['chrono'])) {
+            $aArgs['alt_identifier'] = ChronoModel::getChrono(['id' => $aArgs['category_id'], 'entityId' => $aArgs['destination'], 'typeId' => $aArgs['type_id']]);
         }
-        if (!$processLimitDateFound) {
+        unset($aArgs['chrono']);
+
+        if (empty($aArgs['process_limit_date'])) {
             $processLimitDate = ResModel::getStoredProcessLimitDate(['typeId' => $aArgs['type_id'], 'admissionDate' => $aArgs['admission_date']]);
             $aArgs['process_limit_date'] = $processLimitDate;
         }
+
+        if (!empty($aArgs['exp_contact_id']) && !is_numeric($aArgs['exp_contact_id'])) {
+            $mail = explode('<', str_replace('>', '', $aArgs['exp_contact_id']));
+            $contact = ContactModel::getByEmail(['email' => $mail[count($mail) - 1], 'select' => ['contacts_v2.contact_id']]);
+            if (!empty($contact['contact_id'])) {
+                $aArgs['exp_contact_id'] = $contact['contact_id'];
+            } else {
+                $aArgs['exp_contact_id'] = 0;
+            }
+        }
+
+        if (!empty($aArgs['address_id']) && !is_numeric($aArgs['address_id'])) {
+            $mail = explode('<', str_replace('>', '', $aArgs['address_id']));
+            $contact = ContactModel::getByEmail(['email' => $mail[count($mail) - 1], 'select' => ['contact_addresses.id']]);
+            if (!empty($contact['id'])) {
+                $aArgs['address_id'] = $contact['id'];
+            } else {
+                $aArgs['address_id'] = 0;
+            }
+        }
+
         $aArgs['creation_date'] = 'CURRENT_TIMESTAMP';
 
         return $aArgs;
-- 
GitLab