From 8826b9f5c50f151e037fb429f61d89f0e0cacf4c Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Tue, 5 Nov 2019 15:01:08 +0100
Subject: [PATCH] FEAT #11691 TIME 0:45 refactoring attachments

---
 .../actions/sendToExternalSignatureBook.php   | 20 ++-----
 migration/19.12/1912-postScript.sql           |  2 +
 migration/19.12/1912.sql                      | 25 ---------
 modules/attachments/attachments_content.php   |  2 +-
 .../class/attachments_controler_Abstract.php  |  1 -
 modules/attachments/del_attachment.php        | 48 +++++++----------
 modules/attachments/fill_input.php            |  4 +-
 .../SendMessageExchangeController.php         |  2 +-
 modules/visa/batch/batch_tools.php            |  4 +-
 .../batch/process_mailsFromSignatoryBook.php  |  4 +-
 .../visa/class/FastParapheurController.php    |  4 +-
 modules/visa/class/IParapheurController.php   |  4 +-
 modules/visa/class/IxbusController.php        |  5 +-
 modules/visa/generatePdf.php                  |  2 -
 .../ExternalSignatoryBookTrait.php            |  4 +-
 .../PreProcessActionController.php            | 52 ++++++-------------
 src/app/action/controllers/ShippingTrait.php  | 14 ++---
 .../controllers/AttachmentController.php      | 42 +++++++--------
 .../controllers/ConvertPdfController.php      |  7 +--
 .../controllers/MaarchParapheurController.php |  5 +-
 .../controllers/XParaphController.php         |  3 +-
 .../folder/controllers/FolderController.php   |  2 +-
 .../resource/controllers/ExportController.php |  2 +-
 .../resource/controllers/ResController.php    | 43 ++++++---------
 .../controllers/ResourceListController.php    |  2 +-
 .../ShippingTemplateController.php            | 10 +---
 .../controllers/SignatureBookController.php   | 45 ++++++----------
 27 files changed, 123 insertions(+), 235 deletions(-)

diff --git a/apps/maarch_entreprise/actions/sendToExternalSignatureBook.php b/apps/maarch_entreprise/actions/sendToExternalSignatureBook.php
index bf13abedaf5..84cc291baad 100755
--- a/apps/maarch_entreprise/actions/sendToExternalSignatureBook.php
+++ b/apps/maarch_entreprise/actions/sendToExternalSignatureBook.php
@@ -162,28 +162,18 @@ function check_form($form_id, $values)
                 return false;
             } else {
                 foreach ($aResources as $resId) {
-                    $attachments = \Attachment\models\AttachmentModel::getOnView([
+                    $attachments = \Attachment\models\AttachmentModel::get([
                         'select'    => [
-                            'res_id', 'res_id_version', 'title', 'identifier', 'attachment_type',
+                            'res_id', 'title', 'identifier', 'attachment_type',
                             'status', 'typist', 'docserver_id', 'path', 'filename', 'creation_date',
-                            'validation_date', 'relation', 'attachment_id_master'
+                            'validation_date', 'relation'
                         ],
                         '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']]
                     ]);
 
                     foreach ($attachments as $value) {
-                        if (!empty($value['res_id'])) {
-                            $resIdAttachment  = $value['res_id'];
-                            $collId = 'attachments_coll';
-                            $is_version = false;
-                        } else {
-                            $resIdAttachment  = $value['res_id_version'];
-                            $collId = 'attachments_version_coll';
-                            $is_version = true;
-                        }
-                    
-                        $adrInfo       = \Convert\controllers\ConvertPdfController::getConvertedPdfById(['resId' => $resIdAttachment, 'collId' => $collId, 'isVersion' => $is_version]);
+                        $adrInfo       = \Convert\controllers\ConvertPdfController::getConvertedPdfById(['resId' => $value['res_id'], 'collId' => 'attachments_coll']);
                         $docserverInfo = \Docserver\models\DocserverModel::getByDocserverId(['docserverId' => $adrInfo['docserver_id']]);
                         $filePath      = $docserverInfo['path_template'] . str_replace('#', '/', $adrInfo['path']) . $adrInfo['filename'];
                         if (!is_file($filePath)) {
@@ -337,7 +327,7 @@ function hasAttachmentError()
 
     $noAttachment = [];
     foreach ($resIds as $resId) {
-        $attachments = \Attachment\models\AttachmentModel::getOnView([
+        $attachments = \Attachment\models\AttachmentModel::get([
             'select'    => [
                 'count(1) as nb'
             ],
diff --git a/migration/19.12/1912-postScript.sql b/migration/19.12/1912-postScript.sql
index ee5fd95ae35..ce9a92689f1 100644
--- a/migration/19.12/1912-postScript.sql
+++ b/migration/19.12/1912-postScript.sql
@@ -22,6 +22,8 @@ DROP TABLE IF EXISTS groupbasket_status;
 DROP TABLE IF EXISTS indexingmodels;
 
 DROP TABLE IF EXISTS mlb_coll_ext;
+DROP TABLE IF EXISTS res_version_attachments;
+DROP TABLE IF EXISTS adr_attachments_version;
 
 ALTER TABLE priorities DROP COLUMN IF EXISTS default_priority;
 
diff --git a/migration/19.12/1912.sql b/migration/19.12/1912.sql
index 20b5be4d954..582070d88a6 100644
--- a/migration/19.12/1912.sql
+++ b/migration/19.12/1912.sql
@@ -27,8 +27,6 @@ UPDATE res_letterbox SET fulltext_result = 'SUCCESS' WHERE fulltext_result = '1'
 UPDATE res_letterbox SET fulltext_result = 'ERROR' WHERE fulltext_result = '-1' OR fulltext_result = '-2';
 UPDATE res_attachments SET fulltext_result = 'SUCCESS' WHERE fulltext_result = '1' OR fulltext_result = '2';
 UPDATE res_attachments SET fulltext_result = 'ERROR' WHERE fulltext_result = '-1' OR fulltext_result = '-2';
-UPDATE res_version_attachments SET fulltext_result = 'SUCCESS' WHERE fulltext_result = '1' OR fulltext_result = '2';
-UPDATE res_version_attachments SET fulltext_result = 'ERROR' WHERE fulltext_result = '-1' OR fulltext_result = '-2';
 
 
 /* GROUPS INDEXING */
@@ -434,22 +432,16 @@ DO $$ BEGIN
   END IF;
 END$$;
 ALTER TABLE res_letterbox DROP COLUMN IF EXISTS converter_result;
-ALTER TABLE res_version_attachments DROP COLUMN IF EXISTS converter_result;
 ALTER TABLE res_letterbox DROP COLUMN IF EXISTS convert_result;
 ALTER TABLE res_attachments DROP COLUMN IF EXISTS convert_result;
-ALTER TABLE res_version_attachments DROP COLUMN IF EXISTS convert_result;
 ALTER TABLE res_letterbox DROP COLUMN IF EXISTS convert_attempts;
 ALTER TABLE res_attachments DROP COLUMN IF EXISTS convert_attempts;
-ALTER TABLE res_version_attachments DROP COLUMN IF EXISTS convert_attempts;
 ALTER TABLE res_letterbox DROP COLUMN IF EXISTS fulltext_attempts;
 ALTER TABLE res_attachments DROP COLUMN IF EXISTS fulltext_attempts;
-ALTER TABLE res_version_attachments DROP COLUMN IF EXISTS fulltext_attempts;
 ALTER TABLE res_letterbox DROP COLUMN IF EXISTS tnl_attempts;
 ALTER TABLE res_attachments DROP COLUMN IF EXISTS tnl_attempts;
-ALTER TABLE res_version_attachments DROP COLUMN IF EXISTS tnl_attempts;
 ALTER TABLE res_letterbox DROP COLUMN IF EXISTS tnl_result;
 ALTER TABLE res_attachments DROP COLUMN IF EXISTS tnl_result;
-ALTER TABLE res_version_attachments DROP COLUMN IF EXISTS tnl_result;
 ALTER TABLE usergroups DROP COLUMN IF EXISTS enabled;
 ALTER TABLE actions DROP COLUMN IF EXISTS enabled;
 ALTER TABLE actions DROP COLUMN IF EXISTS origin;
@@ -457,7 +449,6 @@ ALTER TABLE actions DROP COLUMN IF EXISTS create_id;
 ALTER TABLE actions DROP COLUMN IF EXISTS category_id;
 DROP VIEW IF EXISTS fp_view_fileplan;
 ALTER TABLE res_attachments DROP COLUMN IF EXISTS folders_system_id;
-ALTER TABLE res_version_attachments DROP COLUMN IF EXISTS folders_system_id;
 DROP TABLE IF EXISTS foldertypes;
 DROP TABLE IF EXISTS foldertypes_doctypes;
 DROP TABLE IF EXISTS foldertypes_doctypes_level1;
@@ -491,22 +482,6 @@ DO $$ BEGIN
   END IF;
 END$$;
 
-
-/* RE CREATE VIEWS */
-CREATE VIEW res_view_attachments AS
-  SELECT '0' as res_id, res_id as res_id_version, title, subject, description, type_id, format, typist,
-  creation_date, fulltext_result, author, identifier, source, relation, doc_date, docserver_id, path,
-  filename, offset_doc, fingerprint, filesize, status, destination, validation_date, effective_date, origin, priority, initiator, dest_user, external_id,
-  coll_id, dest_contact_id, dest_address_id, updated_by, is_multicontacts, is_multi_docservers, res_id_master, attachment_type, attachment_id_master, in_signature_book, in_send_attach, signatory_user_serial_id
-  FROM res_version_attachments
-  UNION ALL
-  SELECT res_id, '0' as res_id_version, title, subject, description, type_id, format, typist,
-  creation_date, fulltext_result, author, identifier, source, relation, doc_date, docserver_id, path,
-  filename, offset_doc, fingerprint, filesize, status, destination, validation_date, effective_date, origin, priority, initiator, dest_user, external_id,
-  coll_id, dest_contact_id, dest_address_id, updated_by, is_multicontacts, is_multi_docservers, res_id_master, attachment_type, '0', in_signature_book, in_send_attach, signatory_user_serial_id
-  FROM res_attachments;
-
-
 /* DATA */
 TRUNCATE TABLE custom_fields;
 INSERT INTO custom_fields (id, label, type, values) VALUES (1, 'Nature', 'select', '["Courrier simple", "Courriel", "Courrier suivi", "Courrier avec AR", "Autre"]');
diff --git a/modules/attachments/attachments_content.php b/modules/attachments/attachments_content.php
index e53e069f08b..9230e021a1f 100755
--- a/modules/attachments/attachments_content.php
+++ b/modules/attachments/attachments_content.php
@@ -465,7 +465,7 @@ if (isset($_POST['add']) && $_POST['add']) {
                                                     }
 
                                                     if (!empty($select)) {
-                                                        $document = \Attachment\models\AttachmentModel::getOnView(['select' => $select, 'where' => ['res_id = ?'], 'data' => [$id]]);
+                                                        $document = \Attachment\models\AttachmentModel::get(['select' => $select, 'where' => ['res_id = ?'], 'data' => [$id]]);
                                                     }
                                                     if ($object['name'] == 'citoyen') {
                                                         $contact = \Contact\models\ContactModel::getOnView(['select' => ['external_id', 'ca_id'], 'where' => ['ca_id = ?'], 'data' => [$resource[0]['address_id']]]);
diff --git a/modules/attachments/class/attachments_controler_Abstract.php b/modules/attachments/class/attachments_controler_Abstract.php
index 8e79aecc7cc..e36e7af8973 100755
--- a/modules/attachments/class/attachments_controler_Abstract.php
+++ b/modules/attachments/class/attachments_controler_Abstract.php
@@ -210,7 +210,6 @@ abstract class attachments_controler_Abstract
             $infos['res_id_master'] = $line->res_id_master;
             $infos['identifier'] = $line->identifier;
             
-            $infos['is_version'] =  false;
             if (empty($infos['target_table_origin'])) {
                 $infos['target_table_origin'] = 'res_attachments';
             }
diff --git a/modules/attachments/del_attachment.php b/modules/attachments/del_attachment.php
index 0fba67cab63..5e15ec28efa 100755
--- a/modules/attachments/del_attachment.php
+++ b/modules/attachments/del_attachment.php
@@ -43,14 +43,7 @@ if ($pdf_id != 0) {
     $stmt = $db->query('UPDATE '.RES_ATTACHMENTS_TABLE." SET status = 'DEL' WHERE res_id = ?", array($pdf_id));
 }
 
-// IS VERSION ATTACHMENT ?
-if ($info_doc['is_version'] == true && $info_doc['status'] != 'TMP') {
-    //DEL PREVIOUS ATTACHMENT VERSION
-    $stmt = $db->query('SELECT attachment_id_master FROM res_attachments WHERE res_id = ?', array($_REQUEST['id']));
-    $res = $stmt->fetchObject();
-    $stmt = $db->query("UPDATE res_attachments SET status = 'DEL' WHERE attachment_id_master = ?", array($res->attachment_id_master));
-    $stmt = $db->query("UPDATE res_attachments SET status = 'DEL' WHERE res_id = ?", array($res->attachment_id_master));
-}
+$stmt = $db->query("UPDATE res_attachments SET status = 'DEL' WHERE origin_id = ? or res_id = ?", array($res->origin_id, $res->origin_id));
 
 //LOG DELETE ACTION IN HISTORY
 if ($_SESSION['history']['attachdel'] == 'true') {
@@ -97,27 +90,26 @@ if (empty($_REQUEST['rest'])) {
         $query .= " and attachment_type NOT IN ('converted_pdf','print_folder')";
     }
     $stmt = $db->query($query, array($info_doc['res_id_master']));
-    $new_nb_attach = $stmt->total;
-    ?>
-    <script type="text/javascript">
-        var eleframe1 =  parent.document.getElementsByName('list_attach');
-        if(eleframe1[0] === undefined){
-            eleframe1 =  parent.document.getElementsByName('uniqueDetailsIframe');
-        }
-        var nb_attach = '<?php functions::xecho($new_nb_attach); ?>';
-        <?php if (isset($_REQUEST['fromDetail']) && $_REQUEST['fromDetail'] == 'attachments') {
+    $new_nb_attach = $stmt->total; ?>
+<script type="text/javascript">
+    var eleframe1 = parent.document.getElementsByName('list_attach');
+    if (eleframe1[0] === undefined) {
+        eleframe1 = parent.document.getElementsByName('uniqueDetailsIframe');
+    }
+    var nb_attach = '<?php functions::xecho($new_nb_attach); ?>';
+    <?php if (isset($_REQUEST['fromDetail']) && $_REQUEST['fromDetail'] == 'attachments') {
         ?>
-            eleframe1[0].src = "<?php echo $_SESSION['config']['businessappurl'].'index.php?display=true&module=attachments&page=frame_list_attachments&load&attach_type_exclude=response_project,signed_response,outgoing_mail_signed,converted_pdf,outgoing_mail,print_folder,aihp&fromDetail=attachments'; ?>";
-        <?php
+    eleframe1[0].src = "<?php echo $_SESSION['config']['businessappurl'].'index.php?display=true&module=attachments&page=frame_list_attachments&load&attach_type_exclude=response_project,signed_response,outgoing_mail_signed,converted_pdf,outgoing_mail,print_folder,aihp&fromDetail=attachments'; ?>";
+    <?php
     } elseif (isset($_REQUEST['fromDetail']) && $_REQUEST['fromDetail'] == 'response') {
-            ?>
-            eleframe1[0].src = "<?php echo $_SESSION['config']['businessappurl'].'index.php?display=true&module=attachments&page=frame_list_attachments&load&attach_type=response_project,outgoing_mail_signed,signed_response,outgoing_mail,aihp&fromDetail=response'; ?>";
-        <?php
-        } else {
-            ?>
-            parent.document.getElementById('list_attach').src = "<?php echo $_SESSION['config']['businessappurl'].'index.php?display=true&module=attachments&template_selected=documents_list_attachments_simple&page=frame_list_attachments&load&attach_type_exclude=converted_pdf,print_folder'; ?>";
-        <?php
-        } ?>
-    </script>
+        ?>
+    eleframe1[0].src = "<?php echo $_SESSION['config']['businessappurl'].'index.php?display=true&module=attachments&page=frame_list_attachments&load&attach_type=response_project,outgoing_mail_signed,signed_response,outgoing_mail,aihp&fromDetail=response'; ?>";
+    <?php
+    } else {
+        ?>
+    parent.document.getElementById('list_attach').src = "<?php echo $_SESSION['config']['businessappurl'].'index.php?display=true&module=attachments&template_selected=documents_list_attachments_simple&page=frame_list_attachments&load&attach_type_exclude=converted_pdf,print_folder'; ?>";
+    <?php
+    } ?>
+</script>
 <?php
 }
diff --git a/modules/attachments/fill_input.php b/modules/attachments/fill_input.php
index 1c531507bd2..a6aaf855733 100755
--- a/modules/attachments/fill_input.php
+++ b/modules/attachments/fill_input.php
@@ -53,7 +53,7 @@ $defaultInfos = \Resource\models\ResModel::getById(['resId' => $_SESSION['doc_id
 if(count($_GET['field']) == 1){
 
     // Check if there is a response project and retrieve the infos about it
-    $queryProjectResponse = \Attachment\models\AttachmentModel::getOnView([
+    $queryProjectResponse = \Attachment\models\AttachmentModel::get([
         'select'    =>  ['identifier, title, dest_contact_id, dest_address_id'],
         'where'     =>  ["res_id_master = (?) AND attachment_type = 'response_project' AND status <> 'DEL'"],
         'data'      =>  [$_GET['field']]
@@ -134,7 +134,7 @@ if(count($_GET['field']) == 1){
 	$str = '<select id="listProjectResponse" name="chrono_number_list" onchange="fillHiddenInput(this.options[this.selectedIndex].value)">';
 	$str .= "<option value=''>" . _CHOOSE_CHRONO_NUMBER . "</option>";
     for($i = 0; $i< count($_GET['field']); $i++){
-        $queryAllProjectReponse = \Attachment\models\AttachmentModel::getOnView([
+        $queryAllProjectReponse = \Attachment\models\AttachmentModel::get([
             'select'    =>  ['title,identifier, dest_contact_id, dest_address_id'],
             'where'     =>  ["res_id_master = (?) AND attachment_type = 'response_project'"],
             'data'      =>  [$_GET['field'][$i]]
diff --git a/modules/sendmail/Controllers/SendMessageExchangeController.php b/modules/sendmail/Controllers/SendMessageExchangeController.php
index 8fa71d9d4f0..9d34815ffb3 100755
--- a/modules/sendmail/Controllers/SendMessageExchangeController.php
+++ b/modules/sendmail/Controllers/SendMessageExchangeController.php
@@ -69,7 +69,7 @@ class SendMessageExchangeController
         /**************** GET ATTACHMENTS INFOS ***************/
         $AttachmentsInfo = [];
         if (!empty($aArgs['join_attachment'])) {
-            $AttachmentsInfo = \Attachment\models\AttachmentModel::getOnView(['select' => ['*'], 'where' => ['res_id in (?)'], 'data' => [$aArgs['join_attachment']]]);
+            $AttachmentsInfo = \Attachment\models\AttachmentModel::get(['select' => ['*'], 'where' => ['res_id in (?)'], 'data' => [$aArgs['join_attachment']]]);
             foreach ($AttachmentsInfo as $key => $value) {
                 $AttachmentsInfo[$key]['Title']                                  = $value['title'];
                 $AttachmentsInfo[$key]['OriginatingAgencyArchiveUnitIdentifier'] = $value['identifier'];
diff --git a/modules/visa/batch/batch_tools.php b/modules/visa/batch/batch_tools.php
index 73f02031822..d3ec70618ae 100755
--- a/modules/visa/batch/batch_tools.php
+++ b/modules/visa/batch/batch_tools.php
@@ -214,8 +214,8 @@ function Bt_createAttachment($aArgs = [])
     array_push($dataValue, ['column' => 'relation',         'value' => $relation,                 'type' => 'integer']);
     array_push($dataValue, ['column' => 'in_signature_book','value' => $inSignatureBook,          'type' => 'bool']);
 
-    if (!empty($aArgs['attachment_id_master'])) {
-        array_push($dataValue, ['column' => 'attachment_id_master','value' => $aArgs['attachment_id_master'], 'type' => 'integer']);
+    if (!empty($aArgs['origin_id'])) {
+        array_push($dataValue, ['column' => 'origin_id','value' => $aArgs['origin_id'], 'type' => 'integer']);
     }
 
     $allDatas = [
diff --git a/modules/visa/batch/process_mailsFromSignatoryBook.php b/modules/visa/batch/process_mailsFromSignatoryBook.php
index 0271a463676..4aa1410247b 100755
--- a/modules/visa/batch/process_mailsFromSignatoryBook.php
+++ b/modules/visa/batch/process_mailsFromSignatoryBook.php
@@ -234,7 +234,7 @@ if (file_exists($GLOBALS['errorLckFile'])) {
 Bt_getWorkBatch();
 
 $GLOBALS['logger']->write('Retrieve attachments sent to remote signatory book', 'INFO');
-$query = "SELECT res_id, external_id->>'signatureBookId' as external_id, external_id->>'xparaphDepot' as xparaphdepot, format, res_id_master, title, identifier, type_id, attachment_type, dest_contact_id, dest_address_id, dest_user, typist, attachment_id_master, relation 
+$query = "SELECT res_id, external_id->>'signatureBookId' as external_id, external_id->>'xparaphDepot' as xparaphdepot, format, res_id_master, title, identifier, type_id, attachment_type, dest_contact_id, dest_address_id, dest_user, typist, origin_id, relation 
         FROM res_attachments WHERE status = 'FRZ' AND external_id->>'signatureBookId' IS NOT NULL AND external_id->>'signatureBookId' <> ''";
 $stmt = $GLOBALS['db']->query($query, []);
     
@@ -320,7 +320,7 @@ foreach ($retrievedMails['noVersion'] as $resId => $value) {
                 'format'          => $value->format,
                 'attachment_type' => $value->attachment_type,
                 'relation'        => $value->relation + 1,
-                'attachment_id_master' => $resId,
+                'origin_id'       => $resId,
                 'status'          => 'TRA',
                 'encodedFile'     => $value->encodedFile,
                 'table'           => 'res_attachments',
diff --git a/modules/visa/class/FastParapheurController.php b/modules/visa/class/FastParapheurController.php
index e337c2f4554..781ba8f02a4 100755
--- a/modules/visa/class/FastParapheurController.php
+++ b/modules/visa/class/FastParapheurController.php
@@ -92,7 +92,7 @@ class FastParapheurController
             $annexes['letterbox'][0]['filePath'] = $letterboxPath['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $annexes['letterbox'][0]['path']) . $annexes['letterbox'][0]['filename'];
         }
 
-        $annexes['attachments']     = \Attachment\models\AttachmentModel::getOnView([
+        $annexes['attachments']     = \Attachment\models\AttachmentModel::get([
             'select'                => ['res_id', 'path', 'filename', 'format'],
             'where'                 => ['res_id_master = ?', 'attachment_type not in (?)', "status NOT IN ('DEL','OBS')", 'in_signature_book = FALSE'],
             'data'                  => [$aArgs['resIdMaster'], 'print_folder']
@@ -107,7 +107,7 @@ class FastParapheurController
         }
         // END annexes
 
-        $attachments         = \Attachment\models\AttachmentModel::getOnView([
+        $attachments         = \Attachment\models\AttachmentModel::get([
             'select'         => ['res_id', 'title', 'attachment_type','path', 'res_id_master', 'format'],
             'where'          => ['res_id_master = ?', 'attachment_type not in (?)', "status not in ('DEL', 'OBS')", 'in_signature_book = TRUE'],
             'data'           => [$aArgs['resIdMaster'], ['converted_pdf', 'incoming_mail_attachment', 'print_folder', 'signed_response']]
diff --git a/modules/visa/class/IParapheurController.php b/modules/visa/class/IParapheurController.php
index 660b1a38be7..3b338fa1284 100755
--- a/modules/visa/class/IParapheurController.php
+++ b/modules/visa/class/IParapheurController.php
@@ -72,7 +72,7 @@ class IParapheurController
         $letterboxPath                      = \Docserver\models\DocserverModel::getByDocserverId(['docserverId' => $annexes['letterbox']['docserver_id'], 'select' => ['path_template']]);
         $annexes['letterbox']['filePath']   = $letterboxPath['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $annexes['letterbox']['path']) . $annexes['letterbox']['filename'];
 
-        $annexes['attachments']     = \Attachment\models\AttachmentModel::getOnView([
+        $annexes['attachments']     = \Attachment\models\AttachmentModel::get([
             'select'        => ['res_id', 'path', 'filename' ],
             'where'         => ['res_id_master = ?', 'attachment_type not in (?)', "status NOT IN ('DEL','OBS')", 'in_signature_book = FALSE', "format = 'pdf'"],
             'data'          => [$aArgs['resIdMaster'], 'print_folder']
@@ -87,7 +87,7 @@ class IParapheurController
         }
         // END annexes
 
-        $attachments         = \Attachment\models\AttachmentModel::getOnView([
+        $attachments         = \Attachment\models\AttachmentModel::get([
             'select'         => ['res_id', 'title', 'attachment_type','path'],
             'where'          => ['res_id_master = ?', 'attachment_type not in (?)', "status not in ('DEL', 'OBS', 'FRZ', 'TMP')", 'in_signature_book = TRUE'],
             'data'           => [$aArgs['resIdMaster'], ['converted_pdf', 'incoming_mail_attachment', 'print_folder', 'signed_response']]
diff --git a/modules/visa/class/IxbusController.php b/modules/visa/class/IxbusController.php
index 8a0979b931d..9062cbe948f 100755
--- a/modules/visa/class/IxbusController.php
+++ b/modules/visa/class/IxbusController.php
@@ -272,11 +272,11 @@ class IxbusController
         }
         $userInfo  = IxbusController::getInfoUtilisateur(['config' => $aArgs['config'], 'login' => $aArgs['loginIxbus'], 'password' => $aArgs['passwordIxbus']]);
 
-        $attachments = \Attachment\models\AttachmentModel::getOnView([
+        $attachments = \Attachment\models\AttachmentModel::get([
             'select'    => [
                 'res_id', 'title', 'identifier', 'attachment_type',
                 'status', 'typist', 'docserver_id', 'path', 'filename', 'creation_date',
-                'validation_date', 'relation', 'attachment_id_master'
+                'validation_date', 'relation', 'origin_id'
             ],
             'where'     => ["res_id_master = ?", "attachment_type not in (?)", "status not in ('DEL', 'OBS', 'FRZ', 'TMP')", "in_signature_book = 'true'"],
             'data'      => [$aArgs['resIdMaster'], ['converted_pdf', 'incoming_mail_attachment', 'print_folder', 'signed_response']]
@@ -287,7 +287,6 @@ class IxbusController
         foreach ($attachments as $value) {
             $resId  = $value['res_id'];
             $collId = 'attachments_coll';
-            $is_version = false;
 
             $adrInfo       = \Convert\controllers\ConvertPdfController::getConvertedPdfById(['resId' => $resId, 'collId' => $collId]);
             $docserverInfo = \Docserver\models\DocserverModel::getByDocserverId(['docserverId' => $adrInfo['docserver_id']]);
diff --git a/modules/visa/generatePdf.php b/modules/visa/generatePdf.php
index 482ca99f3e8..72a8fc4dfb2 100755
--- a/modules/visa/generatePdf.php
+++ b/modules/visa/generatePdf.php
@@ -10,12 +10,10 @@
 */
 
 if (isset($_REQUEST["res_id"]) && isset($_REQUEST["coll_id"]) && isset($_REQUEST["is_version"])) {
-    $isVersion = ($_REQUEST["is_version"] == 'true') ? true : false;
     $convertedDocument =  \Convert\controllers\ConvertPdfController::getConvertedPdfById([
         'select' => ['docserver_id', 'path', 'filename'],
         'resId' => $_REQUEST["res_id"],
         'collId' => $_REQUEST["coll_id"],
-        'isVersion' => $isVersion
     ]);
 
     if (!empty($convertedDocument['errors'])) {
diff --git a/src/app/action/controllers/ExternalSignatoryBookTrait.php b/src/app/action/controllers/ExternalSignatoryBookTrait.php
index b1503be72d0..2406adc0bcb 100644
--- a/src/app/action/controllers/ExternalSignatoryBookTrait.php
+++ b/src/app/action/controllers/ExternalSignatoryBookTrait.php
@@ -45,7 +45,7 @@ trait ExternalSignatoryBookTrait
             } elseif ($config['id'] == 'fastParapheur') {
                 // TODO
             } elseif ($config['id'] == 'maarchParapheur') {
-                $attachments = AttachmentModel::getOnView([
+                $attachments = AttachmentModel::get([
                     'select'    => [
                         'count(1) as nb'
                     ],
@@ -72,7 +72,7 @@ trait ExternalSignatoryBookTrait
 
                 $historyInfo = $sendedInfo['historyInfos'];
             } elseif ($config['id'] == 'xParaph') {
-                $attachments = AttachmentModel::getOnView([
+                $attachments = AttachmentModel::get([
                     'select'    => [
                         'count(1) as nb'
                     ],
diff --git a/src/app/action/controllers/PreProcessActionController.php b/src/app/action/controllers/PreProcessActionController.php
index 5f08f9ea6ec..5f119b76b44 100755
--- a/src/app/action/controllers/PreProcessActionController.php
+++ b/src/app/action/controllers/PreProcessActionController.php
@@ -443,11 +443,11 @@ class PreProcessActionController
                     }
 
                     // Check attachments
-                    $attachments = AttachmentModel::getOnView([
+                    $attachments = AttachmentModel::get([
                         'select'    => [
-                            'res_id', 'res_id_version', 'title', 'identifier', 'attachment_type',
+                            'res_id', 'title', 'identifier', 'attachment_type',
                             'status', 'typist', 'docserver_id', 'path', 'filename', 'creation_date',
-                            'validation_date', 'relation', 'attachment_id_master'
+                            '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'"],
                         'data'      => [$resId, ['converted_pdf', 'print_folder', 'signed_response']]
@@ -457,17 +457,10 @@ class PreProcessActionController
                         $additionalsInfos['noAttachment'][] = ['alt_identifier' => $noAttachmentsResource['alt_identifier'], 'res_id' => $resId, 'reason' => 'noAttachmentInSignatoryBook'];
                     } else {
                         foreach ($attachments as $value) {
-                            if (!empty($value['res_id'])) {
-                                $resIdAttachment  = $value['res_id'];
-                                $collId = 'attachments_coll';
-                                $is_version = false;
-                            } else {
-                                $resIdAttachment  = $value['res_id_version'];
-                                $collId = 'attachments_version_coll';
-                                $is_version = true;
-                            }
+                            $resIdAttachment  = $value['res_id'];
+                            $collId = 'attachments_coll';
                             
-                            $adrInfo = ConvertPdfController::getConvertedPdfById(['resId' => $resIdAttachment, 'collId' => $collId, 'isVersion' => $is_version]);
+                            $adrInfo = ConvertPdfController::getConvertedPdfById(['resId' => $resIdAttachment, 'collId' => $collId]);
                             if (empty($adrInfo['docserver_id'])) {
                                 $additionalsInfos['noAttachment'][] = ['alt_identifier' => $noAttachmentsResource['alt_identifier'], 'res_id' => $resIdAttachment, 'reason' => 'noAttachmentConversion'];
                                 break;
@@ -501,11 +494,11 @@ class PreProcessActionController
                     }
 
                     // Check attachments
-                    $attachments = AttachmentModel::getOnView([
+                    $attachments = AttachmentModel::get([
                         'select'    => [
-                            'res_id', 'res_id_version', 'title', 'identifier', 'attachment_type',
+                            'res_id', 'title', 'identifier', 'attachment_type',
                             'status', 'typist', 'docserver_id', 'path', 'filename', 'creation_date',
-                            'validation_date', 'relation', 'attachment_id_master'
+                            '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'"],
                         'data'      => [$resId, ['converted_pdf', 'print_folder', 'signed_response']]
@@ -515,17 +508,10 @@ class PreProcessActionController
                         $additionalsInfos['noAttachment'][] = ['alt_identifier' => $noAttachmentsResource['alt_identifier'], 'res_id' => $resId, 'reason' => 'noAttachmentInSignatoryBook'];
                     } else {
                         foreach ($attachments as $value) {
-                            if (!empty($value['res_id'])) {
-                                $resIdAttachment = $value['res_id'];
-                                $collId          = 'attachments_coll';
-                                $is_version      = false;
-                            } else {
-                                $resIdAttachment = $value['res_id_version'];
-                                $collId          = 'attachments_version_coll';
-                                $is_version      = true;
-                            }
+                            $resIdAttachment = $value['res_id'];
+                            $collId          = 'attachments_coll';
                             
-                            $adrInfo = ConvertPdfController::getConvertedPdfById(['resId' => $resIdAttachment, 'collId' => $collId, 'isVersion' => $is_version]);
+                            $adrInfo = ConvertPdfController::getConvertedPdfById(['resId' => $resIdAttachment, 'collId' => $collId]);
                             if (empty($adrInfo['docserver_id'])) {
                                 $additionalsInfos['noAttachment'][] = ['alt_identifier' => $noAttachmentsResource['alt_identifier'], 'res_id' => $resIdAttachment, 'reason' => 'noAttachmentConversion'];
                                 break;
@@ -729,21 +715,13 @@ class PreProcessActionController
                 foreach ($aAttachments as $key => $attachment) {
                     if ($attachment['res_id_master'] == $valueResId) {
                         $resIdFound = true;
-                        if (!empty($attachment['res_id'])) {
-                            $isVersion    = false;
-                            $attachmentId = $attachment['res_id'];
-                            $collId       = 'attachments_coll';
-                        } else {
-                            $isVersion    = true;
-                            $attachmentId = $attachment['res_id_version'];
-                            $collId       = 'attachments_version_coll';
-                        }
+                        $attachmentId = $attachment['res_id'];
+                        $collId       = 'attachments_coll';
                         $convertedDocument = ConvertPdfController::getConvertedPdfById([
                             'select'    => ['docserver_id','path', 'filename', 'fingerprint'],
                             'resId'     => $attachmentId,
                             'collId'    => $collId,
-                            'type'      => 'PDF',
-                            'isVersion' => $isVersion
+                            'type'      => 'PDF'
                         ]);
                         if (empty($convertedDocument['docserver_id'])) {
                             $resInfo = ResModel::getById(['select' => ['alt_identifier'], 'resId' => $valueResId]);
diff --git a/src/app/action/controllers/ShippingTrait.php b/src/app/action/controllers/ShippingTrait.php
index 6f15d9b950a..9a00946a2bf 100644
--- a/src/app/action/controllers/ShippingTrait.php
+++ b/src/app/action/controllers/ShippingTrait.php
@@ -54,8 +54,8 @@ trait ShippingTrait
         $shippingTemplate['account'] = json_decode($shippingTemplate['account'], true);
         $shippingTemplate['fee'] = json_decode($shippingTemplate['fee'], true);
 
-        $attachments = AttachmentModel::getOnView([
-            'select'    => ['res_id', 'res_id_version', 'title', 'dest_address_id', 'external_id'],
+        $attachments = AttachmentModel::get([
+            'select'    => ['res_id', 'title', 'dest_address_id', 'external_id'],
             'where'     => ['res_id_master = ?', 'in_send_attach = ?', 'status not in (?)', 'attachment_type not in (?)'],
             'data'      => [$args['resId'], true, ['OBS', 'DEL', 'TMP', 'FRZ'], ['print_folder']]
         ]);
@@ -65,13 +65,7 @@ trait ShippingTrait
 
         $contacts = [];
         foreach ($attachments as $attachment) {
-            if (!empty($attachment['res_id'])) {
-                $isVersion = false;
-                $attachmentId = $attachment['res_id'];
-            } else {
-                $isVersion = true;
-                $attachmentId = $attachment['res_id_version'];
-            }
+            $attachmentId = $attachment['res_id'];
 
             $convertedDocument = AdrModel::getConvertedDocumentById([
                 'select'    => ['docserver_id','path', 'filename', 'fingerprint'],
@@ -150,7 +144,7 @@ trait ShippingTrait
                 continue;
             }
 
-            $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $attachmentId, 'collId' => 'attachments_coll', 'isVersion' => $isVersion]);
+            $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $attachmentId, 'collId' => 'attachments_coll']);
             $docserver = DocserverModel::getByDocserverId(['docserverId' => $convertedDocument['docserver_id'], 'select' => ['path_template']]);
             if (empty($docserver['path_template']) || !file_exists($docserver['path_template'])) {
                 $errors[] = "Docserver does not exist for attachment {$attachmentId}";
diff --git a/src/app/attachment/controllers/AttachmentController.php b/src/app/attachment/controllers/AttachmentController.php
index c42bb8eafc3..07cd3145178 100755
--- a/src/app/attachment/controllers/AttachmentController.php
+++ b/src/app/attachment/controllers/AttachmentController.php
@@ -217,9 +217,9 @@ class AttachmentController
             return $response->withStatus(400)->withJson(['errors' => 'Route id is not an integer']);
         }
 
-        $attachment = AttachmentModel::getOnView([
-            'select'    => ['res_id', 'res_id_version', 'docserver_id', 'path', 'filename', 'res_id_master'],
-            'where'     => ['res_id = ? or res_id_version = ?', 'status not in (?)'],
+        $attachment = AttachmentModel::get([
+            'select'    => ['res_id', 'docserver_id', 'path', 'filename', 'res_id_master'],
+            'where'     => ['res_id = ?', 'status not in (?)'],
             'data'      => [$args['id'], $args['id'], ['DEL', 'OBS']],
             'limit'     => 1
         ]);
@@ -233,12 +233,7 @@ class AttachmentController
 
         $pathToThumbnail = 'apps/maarch_entreprise/img/noThumbnail.png';
         $attachmentTodisplay = $attachment[0];
-        $isVersion = empty($attachmentTodisplay['res_id']);
-        if ($isVersion) {
-            $collId = "attachments_version_coll";
-        } else {
-            $collId = "attachments_coll";
-        }
+        $collId = "attachments_coll";
 
         $tnlAdr = AdrModel::getTypedAttachAdrByResId([
             'select'    => ['docserver_id', 'path', 'filename'],
@@ -288,9 +283,9 @@ class AttachmentController
             return $response->withStatus(400)->withJson(['errors' => 'Route id is not an integer']);
         }
 
-        $attachment = AttachmentModel::getOnView([
-            'select'    => ['res_id', 'res_id_version', 'docserver_id', 'path', 'filename'],
-            'where'     => ['res_id = ? or res_id_version = ?', 'status not in (?)'],
+        $attachment = AttachmentModel::get([
+            'select'    => ['res_id', 'docserver_id', 'path', 'filename'],
+            'where'     => ['res_id = ?', 'status not in (?)'],
             'data'      => [$args['id'], $args['id'], ['DEL']],
             'limit'     => 1
         ]);
@@ -303,10 +298,9 @@ class AttachmentController
         }
 
         $attachmentTodisplay = $attachment[0];
-        $id = (empty($attachmentTodisplay['res_id']) ? $attachmentTodisplay['res_id_version'] : $attachmentTodisplay['res_id']);
-        $isVersion = empty($attachmentTodisplay['res_id']);
+        $id = $attachmentTodisplay['res_id'];
 
-        $convertedAttachment = ConvertPdfController::getConvertedPdfById(['resId' => $id, 'collId' => 'attachments_coll', 'isVersion' => $isVersion]);
+        $convertedAttachment = ConvertPdfController::getConvertedPdfById(['resId' => $id, 'collId' => 'attachments_coll']);
         if (empty($convertedAttachment['errors'])) {
             $attachmentTodisplay = $convertedAttachment;
         }
@@ -348,7 +342,7 @@ class AttachmentController
                         } elseif ($value == 'hour_now') {
                             $tmp = date('H:i');
                         } else {
-                            $backFromView = AttachmentModel::getOnView(['select' => [$value], 'where' => ['res_id = ?'], 'data' => [$args['id']]]);
+                            $backFromView = AttachmentModel::get(['select' => [$value], 'where' => ['res_id = ?'], 'data' => [$args['id']]]);
                             if (!empty($backFromView[0][$value])) {
                                 $tmp = $backFromView[0][$value];
                             }
@@ -429,9 +423,9 @@ class AttachmentController
             return $response->withStatus(400)->withJson(['errors' => 'Route id is not an integer']);
         }
 
-        $attachment = AttachmentModel::getOnView([
-            'select'    => ['res_id', 'res_id_version', 'docserver_id', 'path', 'filename', 'res_id_master'],
-            'where'     => ['res_id = ? or res_id_version = ?', 'status not in (?)'],
+        $attachment = AttachmentModel::get([
+            'select'    => ['res_id', 'docserver_id', 'path', 'filename', 'res_id_master'],
+            'where'     => ['res_id = ?', 'status not in (?)'],
             'data'      => [$args['id'], $args['id'], ['DEL']],
             'limit'     => 1
         ]);
@@ -444,7 +438,7 @@ class AttachmentController
         }
 
         $attachmentTodisplay = $attachment[0];
-        $id = (empty($attachmentTodisplay['res_id']) ? $attachmentTodisplay['res_id_version'] : $attachmentTodisplay['res_id']);
+        $id = $attachmentTodisplay['res_id'];
 
         $document['docserver_id'] = $attachmentTodisplay['docserver_id'];
         $document['path'] = $attachmentTodisplay['path'];
@@ -511,7 +505,7 @@ class AttachmentController
         $document = AttachmentModel::getById(['select' => ['docserver_id', 'path', 'filename', 'title'], 'id' => $aArgs['id']]);
 
         if (empty($aArgs['original'])) {
-            $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $aArgs['id'], 'collId' => 'attachments_coll', 'isVersion' => $aArgs['isVersion']]);
+            $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $aArgs['id'], 'collId' => 'attachments_coll']);
 
             if (empty($convertedDocument['errors'])) {
                 $document['docserver_id'] = $convertedDocument['docserver_id'];
@@ -557,7 +551,7 @@ class AttachmentController
 
     public static function generateAttachForMailing(array $aArgs)
     {
-        $attachments = AttachmentModel::getOnView([
+        $attachments = AttachmentModel::get([
             'select'    => ['*'],
             'where'     => ['res_id_master = ?', 'status = ?', 'in_signature_book = ?'],
             'data'      => [$aArgs['resIdMaster'], 'SEND_MASS', true]
@@ -645,7 +639,7 @@ class AttachmentController
                         'userId'           => $aArgs['userId'],
                         'res_id'           => $aArgs['resIdMaster'],
                         'coll_id'          => 'letterbox_coll',
-                        'res_view'         => 'res_view_attachments',
+                        'res_view'         => 'res_attachments',
                         'res_table'        => 'res_attachments',
                         'res_contact_id'   => $contactForMailing['contact_id'],
                         'res_address_id'   => $contactForMailing['address_id'],
@@ -688,7 +682,7 @@ class AttachmentController
             return ['errors' => 'Document out of perimeter'];
         }
 
-        $attachments = AttachmentModel::getOnView([
+        $attachments = AttachmentModel::get([
             'select' => ['res_id'],
             'where' => ['res_id_master = ?', 'status = ?'],
             'data' => [$aArgs['resIdMaster'],'SEND_MASS']
diff --git a/src/app/convert/controllers/ConvertPdfController.php b/src/app/convert/controllers/ConvertPdfController.php
index c5502ab5305..6e6fa4c69bc 100755
--- a/src/app/convert/controllers/ConvertPdfController.php
+++ b/src/app/convert/controllers/ConvertPdfController.php
@@ -177,21 +177,18 @@ class ConvertPdfController
     {
         ValidatorModel::notEmpty($aArgs, ['resId', 'collId']);
         ValidatorModel::intVal($aArgs, ['resId']);
-        ValidatorModel::boolType($aArgs, ['isVersion']);
 
         $convertedDocument = AdrModel::getConvertedDocumentById([
             'select'    => ['docserver_id','path', 'filename', 'fingerprint'],
             'resId'     => $aArgs['resId'],
             'collId'    => $aArgs['collId'],
-            'type'      => 'PDF',
-            'isVersion' => $aArgs['isVersion']
+            'type'      => 'PDF'
         ]);
         
         if (empty($convertedDocument)) {
             $convertedDocument = ConvertPdfController::convert([
                 'resId'     => $aArgs['resId'],
-                'collId'    => $aArgs['collId'],
-                'isVersion' => $aArgs['isVersion'],
+                'collId'    => $aArgs['collId']
             ]);
         }
 
diff --git a/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php b/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php
index 0575a49a742..b5e2d26bcc6 100755
--- a/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php
+++ b/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php
@@ -195,11 +195,11 @@ class MaarchParapheurController
 
             $excludeAttachmentTypes = ['converted_pdf', 'print_folder', 'signed_response'];
 
-            $attachments = AttachmentModel::getOnView([
+            $attachments = AttachmentModel::get([
                 'select'    => [
                     'res_id', 'title', 'identifier', 'attachment_type',
                     'status', 'typist', 'docserver_id', 'path', 'filename', 'creation_date',
-                    'validation_date', 'relation', 'attachment_id_master'
+                    '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'"],
                 'data'      => [$aArgs['resIdMaster'], $excludeAttachmentTypes]
@@ -211,7 +211,6 @@ class MaarchParapheurController
                 foreach ($attachments as $value) {
                     $resId  = $value['res_id'];
                     $collId = 'attachments_coll';
-                    $is_version = false;
                     
                     $adrInfo = ConvertPdfController::getConvertedPdfById(['resId' => $resId, 'collId' => $collId]);
                     if (empty($adrInfo['docserver_id'])) {
diff --git a/src/app/external/externalSignatoryBook/controllers/XParaphController.php b/src/app/external/externalSignatoryBook/controllers/XParaphController.php
index fb74be482bb..5079d5315ea 100755
--- a/src/app/external/externalSignatoryBook/controllers/XParaphController.php
+++ b/src/app/external/externalSignatoryBook/controllers/XParaphController.php
@@ -32,7 +32,7 @@ class XParaphController
 {
     public static function sendDatas($aArgs)
     {
-        $attachments = AttachmentModel::getOnView([
+        $attachments = AttachmentModel::get([
             'select'    => [
                 'res_id', 'title', 'docserver_id', 'path', 'filename'],
             'where'     => ["res_id_master = ?", "attachment_type not in (?)", "status not in ('DEL', 'OBS', 'FRZ', 'TMP', 'SEND_MASS')", "in_signature_book = 'true'"],
@@ -62,7 +62,6 @@ class XParaphController
         foreach ($attachments as $value) {
             $resId      = $value['res_id'];
             $collId     = 'attachments_coll';
-            $is_version = false;
             
             $adrInfo       = ConvertPdfController::getConvertedPdfById(['resId' => $resId, 'collId' => $collId]);
             $docserverInfo = DocserverModel::getByDocserverId(['docserverId' => $adrInfo['docserver_id']]);
diff --git a/src/app/folder/controllers/FolderController.php b/src/app/folder/controllers/FolderController.php
index 4f7b0716099..36312ea4880 100755
--- a/src/app/folder/controllers/FolderController.php
+++ b/src/app/folder/controllers/FolderController.php
@@ -527,7 +527,7 @@ class FolderController
                     $excludeAttachmentTypes[] = 'document_with_notes';
                 }
 
-                $attachments = AttachmentModel::getOnView([
+                $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 != ?)'],
                     'data'      => [$resIds, ['DEL', 'OBS'], $excludeAttachmentTypes, 'TMP', $GLOBALS['id'], 'TMP'],
diff --git a/src/app/resource/controllers/ExportController.php b/src/app/resource/controllers/ExportController.php
index 1c2043c68d4..247c3705c97 100644
--- a/src/app/resource/controllers/ExportController.php
+++ b/src/app/resource/controllers/ExportController.php
@@ -549,7 +549,7 @@ class ExportController
         }
 
         foreach ($args['chunkedResIds'] as $resIds) {
-            $attachments = AttachmentModel::getOnView([
+            $attachments = AttachmentModel::get([
                 'select'    => ['creation_date', 'res_id'],
                 'where'     => ['res_id in (?)', 'attachment_type = ?', 'status = ?'],
                 'data'      => [$resIds, 'signed_response', 'TRA'],
diff --git a/src/app/resource/controllers/ResController.php b/src/app/resource/controllers/ResController.php
index 73803b77e7b..bf6b5484e21 100755
--- a/src/app/resource/controllers/ResController.php
+++ b/src/app/resource/controllers/ResController.php
@@ -233,22 +233,18 @@ class ResController
         }
 
         if ($document['category_id'] == 'outgoing') {
-            $attachment = AttachmentModel::getOnView([
-                'select'    => ['res_id', 'res_id_version', 'docserver_id', 'path', 'filename'],
+            $attachment = AttachmentModel::get([
+                'select'    => ['res_id', 'docserver_id', 'path', 'filename'],
                 'where'     => ['res_id_master = ?', 'attachment_type = ?', 'status not in (?)'],
                 'data'      => [$aArgs['resId'], 'outgoing_mail', ['DEL', 'OBS']],
                 'limit'     => 1
             ]);
             if (!empty($attachment[0])) {
                 $attachmentTodisplay = $attachment[0];
-                $id = (empty($attachmentTodisplay['res_id']) ? $attachmentTodisplay['res_id_version'] : $attachmentTodisplay['res_id']);
-                $isVersion = empty($attachmentTodisplay['res_id']);
-                if ($isVersion) {
-                    $collId = "attachments_version_coll";
-                } else {
-                    $collId = "attachments_coll";
-                }
-                $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $id, 'collId' => $collId, 'isVersion' => $isVersion]);
+                $id = $attachmentTodisplay['res_id'];
+                $collId = "attachments_coll";
+                
+                $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $id, 'collId' => $collId]);
                 if (empty($convertedDocument['errors'])) {
                     $attachmentTodisplay = $convertedDocument;
                 }
@@ -258,7 +254,7 @@ class ResController
                 $document['fingerprint'] = $attachmentTodisplay['fingerprint'];
             }
         } else {
-            $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $aArgs['resId'], 'collId' => 'letterbox_coll', 'isVersion' => false]);
+            $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $aArgs['resId'], 'collId' => 'letterbox_coll']);
 
             if (empty($convertedDocument['errors'])) {
                 $documentTodisplay = $convertedDocument;
@@ -404,8 +400,8 @@ class ResController
         }
 
         if ($document['category_id'] == 'outgoing') {
-            $attachment = AttachmentModel::getOnView([
-                'select'    => ['res_id', 'res_id_version', 'docserver_id', 'path', 'filename', 'fingerprint'],
+            $attachment = AttachmentModel::get([
+                'select'    => ['res_id', 'docserver_id', 'path', 'filename', 'fingerprint'],
                 'where'     => ['res_id_master = ?', 'attachment_type = ?', 'status not in (?)'],
                 'data'      => [$aArgs['resId'], 'outgoing_mail', ['DEL', 'OBS']],
                 'limit'     => 1
@@ -487,8 +483,8 @@ class ResController
             if (empty($tnlAdr)) {
                 $document = ResModel::getById(['select' => ['category_id'], 'resId' => $aArgs['resId']]);
                 if ($document['category_id'] == 'outgoing') {
-                    $attachment = AttachmentModel::getOnView([
-                        'select'    => ['res_id', 'res_id_version'],
+                    $attachment = AttachmentModel::get([
+                        'select'    => ['res_id'],
                         'where'     => ['res_id_master = ?', 'attachment_type = ?', 'status not in (?)'],
                         'data'      => [$aArgs['resId'], 'outgoing_mail', ['DEL', 'OBS']],
                         'limit'     => 1
@@ -605,22 +601,17 @@ class ResController
 
         if (empty($aArgs['original'])) {
             if ($document['category_id'] == 'outgoing') {
-                $attachment = AttachmentModel::getOnView([
-                    'select'    => ['res_id', 'res_id_version', 'docserver_id', 'path', 'filename'],
+                $attachment = AttachmentModel::get([
+                    'select'    => ['res_id', 'docserver_id', 'path', 'filename'],
                     'where'     => ['res_id_master = ?', 'attachment_type = ?', 'status not in (?)'],
                     'data'      => [$aArgs['resId'], 'outgoing_mail', ['DEL', 'OBS']],
                     'limit'     => 1
                 ]);
                 if (!empty($attachment[0])) {
                     $attachmentTodisplay = $attachment[0];
-                    $id = (empty($attachmentTodisplay['res_id']) ? $attachmentTodisplay['res_id_version'] : $attachmentTodisplay['res_id']);
-                    $isVersion = empty($attachmentTodisplay['res_id']);
-                    if ($isVersion) {
-                        $collId = "attachments_version_coll";
-                    } else {
-                        $collId = "attachments_coll";
-                    }
-                    $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $id, 'collId' => $collId, 'isVersion' => $isVersion]);
+                    $id = $attachmentTodisplay['res_id'];
+                    $collId = "attachments_version_coll";
+                    $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $id, 'collId' => $collId]);
                     if (empty($convertedDocument['errors'])) {
                         $attachmentTodisplay = $convertedDocument;
                     }
@@ -630,7 +621,7 @@ class ResController
                     $document['fingerprint'] = $attachmentTodisplay['fingerprint'];
                 }
             } else {
-                $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $aArgs['resId'], 'collId' => 'letterbox_coll', 'isVersion' => false]);
+                $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $aArgs['resId'], 'collId' => 'letterbox_coll']);
 
                 if (empty($convertedDocument['errors'])) {
                     $document['docserver_id'] = $convertedDocument['docserver_id'];
diff --git a/src/app/resource/controllers/ResourceListController.php b/src/app/resource/controllers/ResourceListController.php
index 238a8b7941a..d61e8a7fec8 100644
--- a/src/app/resource/controllers/ResourceListController.php
+++ b/src/app/resource/controllers/ResourceListController.php
@@ -92,7 +92,7 @@ class ResourceListController
                 $excludeAttachmentTypes[] = 'document_with_notes';
             }
 
-            $attachments = AttachmentModel::getOnView([
+            $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 != ?)'],
                 'data'      => [$resIds, ['DEL', 'OBS'], $excludeAttachmentTypes, 'TMP', $GLOBALS['id'], 'TMP'],
diff --git a/src/app/shipping/controllers/ShippingTemplateController.php b/src/app/shipping/controllers/ShippingTemplateController.php
index 560506042d9..686f219c50f 100755
--- a/src/app/shipping/controllers/ShippingTemplateController.php
+++ b/src/app/shipping/controllers/ShippingTemplateController.php
@@ -290,14 +290,8 @@ class ShippingTemplateController
     {
         $fee = 0;
         foreach ($aArgs['resources'] as $value) {
-            if (!empty($value['res_id'])) {
-                $isVersion = false;
-                $attachmentId = $value['res_id'];
-            } else {
-                $isVersion = true;
-                $attachmentId = $value['res_id_version'];
-            }
-            $convertedAttachment = ConvertPdfController::getConvertedPdfById(['select' => ['docserver_id', 'path', 'filename'], 'resId' => $attachmentId, 'collId' => 'attachments_coll', 'isVersion' => $isVersion]);
+            $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'];
 
diff --git a/src/app/signatureBook/controllers/SignatureBookController.php b/src/app/signatureBook/controllers/SignatureBookController.php
index 4eba2939fb8..cab97709bcd 100755
--- a/src/app/signatureBook/controllers/SignatureBookController.php
+++ b/src/app/signatureBook/controllers/SignatureBookController.php
@@ -167,8 +167,8 @@ class SignatureBookController
             return ['error' => 'No Document Found'];
         }
 
-        $incomingMailAttachments = AttachmentModel::getOnView([
-            'select'      => ['res_id', 'res_id_version', 'title', 'format', 'attachment_type', 'path', 'filename'],
+        $incomingMailAttachments = AttachmentModel::get([
+            'select'      => ['res_id', 'title', 'format', 'attachment_type', 'path', 'filename'],
             'where'     => ['res_id_master = ?', 'attachment_type in (?)', "status not in ('DEL', 'TMP', 'OBS')"],
             'data'      => [$resId, ['incoming_mail_attachment', 'converted_pdf']]
         ]);
@@ -190,15 +190,9 @@ class SignatureBookController
             }
 
             $realId = 0;
-            if ($value['res_id'] == 0) {
-                $realId = $value['res_id_version'];
-                $isVersion = true;
-            } elseif ($value['res_id_version'] == 0) {
-                $realId = $value['res_id'];
-                $isVersion = false;
-            }
+            $realId = $value['res_id_version'];
 
-            $convertedAttachment = ConvertPdfController::getConvertedPdfById(['select' => ['docserver_id', 'path', 'filename'], 'resId' => $realId, 'collId' => 'attachments_coll', 'isVersion' => $isVersion]);
+            $convertedAttachment = ConvertPdfController::getConvertedPdfById(['select' => ['docserver_id', 'path', 'filename'], 'resId' => $realId, 'collId' => 'attachments_coll']);
 
             if (empty($convertedAttachment['errors'])) {
                 $isConverted = true;
@@ -236,12 +230,12 @@ class SignatureBookController
         }
         $orderBy .= " ELSE {$c} END, doc_date DESC NULLS LAST, creation_date DESC";
 
-        $attachments = AttachmentModel::getOnView([
+        $attachments = AttachmentModel::get([
             'select'    => [
-                'res_id', 'res_id_version', 'title', 'identifier', 'attachment_type',
+                'res_id', 'title', 'identifier', 'attachment_type',
                 'status', 'typist', 'path', 'filename', 'updated_by', 'creation_date',
                 'validation_date', 'format', 'relation', 'dest_user', 'dest_contact_id',
-                'dest_address_id', 'origin', 'doc_date', 'attachment_id_master'
+                'dest_address_id', 'origin', 'doc_date', 'origin_id'
             ],
             'where'     => ['res_id_master = ?', 'attachment_type not in (?)', "status not in ('DEL', 'OBS')", 'in_signature_book = TRUE'],
             'data'      => [$aArgs['resId'], ['incoming_mail_attachment', 'print_folder']],
@@ -256,21 +250,14 @@ class SignatureBookController
                 continue;
             }
 
-            $realId = 0;
-            if ($value['res_id'] == 0) {
-                $realId = $value['res_id_version'];
-                $isVersion = true;
-            } elseif ($value['res_id_version'] == 0) {
-                $realId = $value['res_id'];
-                $isVersion = false;
-            }
+            $realId = $value['res_id'];
 
             $viewerId       = $realId;
             $viewerNoSignId = $realId;
             $pathToFind     = $value['path'] . str_replace(strrchr($value['filename'], '.'), '.pdf', $value['filename']);
             $isConverted    = false;
 
-            $convertedAttachment = ConvertPdfController::getConvertedPdfById(['select' => [1], 'resId' => $realId, 'collId' => 'attachments_coll', 'isVersion' => $isVersion]);
+            $convertedAttachment = ConvertPdfController::getConvertedPdfById(['select' => [1], 'resId' => $realId, 'collId' => 'attachments_coll']);
 
             if (empty($convertedAttachment['errors'])) {
                 $isConverted = true;
@@ -337,8 +324,8 @@ class SignatureBookController
             }
         }
 
-        $obsAttachments = AttachmentModel::getOnView([
-            'select'    => ['res_id', 'res_id_version', 'attachment_id_master', 'relation', 'creation_date', 'title'],
+        $obsAttachments = AttachmentModel::get([
+            'select'    => ['res_id', 'origin_id', 'relation', 'creation_date', 'title'],
             'where'     => ['res_id_master = ?', 'attachment_type not in (?)', 'status = ?'],
             'data'      => [$aArgs['resId'], ['incoming_mail_attachment', 'print_folder', 'converted_pdf', 'signed_response'], 'OBS'],
             'orderBy'  => ['relation ASC']
@@ -349,7 +336,7 @@ class SignatureBookController
             if ($value['relation'] == 1) {
                 $obsData[$value['res_id']][] = ['resId' => $value['res_id'], 'title' => $value['title'], 'relation' => $value['relation'], 'creation_date' => $value['creation_date']];
             } else {
-                $obsData[$value['attachment_id_master']][] = ['resId' => $value['res_id_version'], 'title' => $value['title'], 'relation' => $value['relation'], 'creation_date' => $value['creation_date']];
+                $obsData[$value['origin_id']][] = ['resId' => $value['res_id_version'], 'title' => $value['title'], 'relation' => $value['relation'], 'creation_date' => $value['creation_date']];
             }
         }
 
@@ -360,12 +347,12 @@ class SignatureBookController
             }
 
             $attachments[$key]['obsAttachments'] = [];
-            if ($value['relation'] > 1 && !empty($obsData[$value['attachment_id_master']])) {
-                $attachments[$key]['obsAttachments'] = $obsData[$value['attachment_id_master']];
+            if ($value['relation'] > 1 && !empty($obsData[$value['origin_id']])) {
+                $attachments[$key]['obsAttachments'] = $obsData[$value['origin_id']];
             }
 
             unset($attachments[$key]['path'], $attachments[$key]['filename'], $attachments[$key]['dest_user'],
-                $attachments[$key]['dest_contact_id'], $attachments[$key]['dest_address_id'], $attachments[$key]['attachment_id_master']
+                $attachments[$key]['dest_contact_id'], $attachments[$key]['dest_address_id'], $attachments[$key]['origin_id']
             );
         }
 
@@ -397,7 +384,7 @@ class SignatureBookController
             $resIds[] = $value['res_id'];
         }
 
-        $attachmentsInResList = AttachmentModel::getOnView([
+        $attachmentsInResList = AttachmentModel::get([
             'select'    => ['res_id_master', 'status', 'attachment_type'],
             'where'     => ['res_id_master in (?)', "attachment_type not in ('incoming_mail_attachment', 'print_folder', 'converted_pdf', 'signed_response')", "status not in ('DEL', 'TMP', 'OBS')"],
             'data'      => [$resIds]
-- 
GitLab