diff --git a/core/Controllers/ResController.php b/core/Controllers/ResController.php
index 80dec8092dfb992b63b246d5a455e2b34e439024..c09d5d6e593f7c533d7042cf934ed40992751f68 100644
--- a/core/Controllers/ResController.php
+++ b/core/Controllers/ResController.php
@@ -60,7 +60,6 @@ class ResController
         if (empty($aArgs['fileFormat'])) {
 
             return ['errors' => 'fileFormat ' . _EMPTY];
-<<<<<<< HEAD
         }
 
         if (empty($aArgs['status'])) {
@@ -74,100 +73,6 @@ class ResController
         $fileFormat = $aArgs['fileFormat'];
         $status = $aArgs['status'];
 
-        try {
-            $count = count($data);
-            for ($i=0;$i<$count;$i++) {
-                $data[$i]['column'] = strtolower($data[$i]['column']);
-            }
-            
-            $returnCode = 0;
-            //copy sended file on tmp 
-            $fileContent = base64_decode($encodedFile);
-            $random = rand();
-            $fileName = 'tmp_file_' . $random . '.' . $fileFormat;
-            $Fnm = $_SESSION['config']['tmppath'] . $fileName;
-            $inF = fopen($Fnm,"w");
-            fwrite($inF, $fileContent);
-            fclose($inF);
-
-            //store resource on docserver
-            $ds = new DocserverController();
-            $aArgs = [
-                'collId' => $collId,
-                'fileInfos' => 
-                    [
-                        'tmpDir'        => $_SESSION['config']['tmppath'],
-                        'size'          => filesize($Fnm),
-                        'format'        => $fileFormat,
-                        'tmpFileName'   => $fileName,
-                    ]
-            ];
-            
-            $storeResult = array();
-            $storeResult = $ds->storeResourceOnDocserver($aArgs);
-            
-            if (!empty($storeResult['errors'])) {
-                
-                return ['errors' => $storeResult['errors']];
-            }
-
-            //store resource metadata in database
-            $aArgs = [
-                'data'        => $data,
-                'docserverId' => $storeResult['docserver_id'],
-                'status'      => $status,
-                'fileFormat'  => $fileFormat,
-            ];
-            
-            $data = $this->prepareStorage($aArgs);
-            
-            unlink($Fnm);
-            
-            require_once 'core/class/class_resource.php';
-            $resource = new \resource();
-            $resId = $resource->load_into_db(
-                $table, 
-                $storeResult['destination_dir'],
-                $storeResult['file_destination_name'],
-                $storeResult['path_template'],
-                $storeResult['docserver_id'], 
-                $data,
-                $_SESSION['config']['databasetype'],
-                true
-            );
-
-            if (!is_numeric($resId)) {
-
-                return ['errors' => 'Pb with SQL insertion : ' .$resId];
-            }
-
-            if ($resId == 0) {
-                $resId = '';
-            }
-
-            return [$resId];
-            
-        } catch (Exception $e) {
-
-            return ['errors' => 'unknown error' . $e->getMessage()];
-=======
->>>>>>> lgi-feat-attachments
-        }
-    }
-
-<<<<<<< HEAD
-=======
-        if (empty($aArgs['status'])) {
-
-            return ['errors' => 'status ' . _EMPTY];
-        }
-        $encodedFile = $aArgs['encodedFile'];
-        $data = $aArgs['data'];
-        $collId = $aArgs['collId'];
-        $table = $aArgs['table'];
-        $fileFormat = $aArgs['fileFormat'];
-        $status = $aArgs['status'];
-
         try {
             $count = count($data);
             for ($i=0;$i<$count;$i++) {
@@ -247,7 +152,6 @@ class ResController
         }
     }
 
->>>>>>> lgi-feat-attachments
     /**
      * Prepares storage on database.
      * @param  $data array
@@ -264,220 +168,6 @@ class ResController
         }
 
         if (empty($aArgs['docserverId'])) {
-<<<<<<< HEAD
-
-            return ['errors' => 'docserverId ' . _EMPTY];
-        }
-
-        if (empty($aArgs['status'])) {
-
-            return ['errors' => 'status ' . _EMPTY];
-        }
-
-        if (empty($aArgs['fileFormat'])) {
-
-            return ['errors' => 'fileFormat ' . _EMPTY];
-        }
-
-        $statusFound = false;
-        $typistFound = false;
-        $typeIdFound = false;
-        $toAddressFound = false;
-        $userPrimaryEntity = false;
-        $destinationFound = false;
-        $initiatorFound = false;
-        
-        $data = $aArgs['data'];
-        $docserverId = $aArgs['docserverId'];
-        $status = $aArgs['status'];
-        $fileFormat = $aArgs['fileFormat'];
-
-        $userModel = new UserModel();
-        $entityModel = new \Entities\Models\EntitiesModel();
-
-        $countD = count($data);
-        for ($i=0;$i<$countD;$i++) {
-
-            if (
-                strtoupper($data[$i]['type']) == 'INTEGER' || 
-                strtoupper($data[$i]['type']) == 'FLOAT'
-            ) {
-                if ($data[$i]['value'] == '') {
-                    $data[$i]['value'] = '0';
-                }
-            }
-
-            if (strtoupper($data[$i]['type']) == 'STRING') {
-               $data[$i]['value'] = $data[$i]['value'];
-               $data[$i]['value'] = str_replace(";", "", $data[$i]['value']);
-               $data[$i]['value'] = str_replace("--", "", $data[$i]['value']);
-            }
-
-            if (strtoupper($data[$i]['column']) == strtoupper('status')) {
-                $statusFound = true;
-            }
-
-            if (strtoupper($data[$i]['column']) == strtoupper('typist')) {
-                $typistFound = true;
-            }
-
-            if (strtoupper($data[$i]['column']) == strtoupper('type_id')) {
-                $typeIdFound = true;
-            }
-
-            if (strtoupper($data[$i]['column']) == strtoupper('custom_t10')) {
-                $mail = array();
-                $theString = str_replace(">", "", $data[$i]['value']);
-                $mail = explode("<", $theString);
-                $user = $userModel->getByEmail(['mail' => $mail[count($mail) -1]]);
-                $userIdFound = $user[0]['user_id'];
-                if (!empty($userIdFound)) {
-                    $toAddressFound = true;
-                    $destUser = $userIdFound;
-                    $entity = $entityModel->getByUserId(['user_id' => $destUser]);
-                    if (!empty($entity[0]['entity_id'])) {
-                        $userEntity = $entity[0]['entity_id'];
-                        $userPrimaryEntity = true;
-                    }
-                } else {
-                    $entity = $entityModel->getByEmail(['email' => $mail[count($mail) -1]]);
-                    if (!empty($entity[0]['entity_id'])) {
-                        $userPrimaryEntity = true;
-                    }
-                }
-            }
-        }
-
-        if (!$typistFound && !$toAddressFound) {
-            array_push(
-                $data,
-                array(
-                    'column' => 'typist',
-                    'value' => 'auto',
-                    'type' => 'string',
-                )
-            );
-        }
-
-        if (!$typeIdFound) {
-            array_push(
-                $data,
-                array(
-                    'column' => 'type_id',
-                    'value' => '10',
-                    'type' => 'string',
-                )
-            );
-        }
-        
-        if (!$statusFound) {
-            array_push(
-                $data,
-                array(
-                    'column' => 'status',
-                    'value' => $status,
-                    'type' => 'string',
-                )
-            );
-        }
-        
-        if ($toAddressFound) {
-            array_push(
-                $data,
-                array(
-                    'column' => 'dest_user',
-                    'value' => $destUser,
-                    'type' => 'string',
-                )
-            );
-            array_push(
-                $data,
-                array(
-                    'column' => 'typist',
-                    'value' => $destUser,
-                    'type' => 'string',
-                )
-            );
-        }
-        
-        if ($userPrimaryEntity) {
-            for ($i=0;$i<count($data);$i++) {
-                if (strtoupper($data[$i]['column']) == strtoupper('destination')) {
-                    if ($data[$i]['value'] == "") {
-                        $data[$i]['value'] = $userEntity;
-                    }
-                    $destinationFound = true;
-                    break;
-                }
-            }
-            if (!$destinationFound) {
-                array_push(
-                    $data,
-                    array(
-                        'column' => 'destination',
-                        'value' => $userEntity,
-                        'type' => 'string',
-                    )
-                );
-            }
-        }
-        
-        if ($userPrimaryEntity) {
-            for ($i=0;$i<count($data);$i++) {
-                if (strtoupper($data[$i]['column']) == strtoupper('initiator')) {
-                    if ($data[$i]['value'] == "") {
-                        $data[$i]['value'] = $userEntity;
-                    }
-                    $initiatorFound = true;
-                    break;
-                }
-            }
-            if (!$initiatorFound) {
-                array_push(
-                    $data,
-                    array(
-                        'column' => 'initiator',
-                        'value' => $userEntity,
-                        'type' => 'string',
-                    )
-                );
-            }
-        }    
-        array_push(
-            $data,
-            array(
-                'column' => 'format',
-                'value' => $fileFormat,
-                'type' => 'string',
-            )
-        );
-        array_push(
-            $data,
-            array(
-                'column' => 'offset_doc',
-                'value' => '',
-                'type' => 'string',
-            )
-        );
-        array_push(
-            $data,
-            array(
-                'column' => 'logical_adr',
-                'value' => '',
-                'type' => 'string',
-            )
-        );
-        array_push(
-            $data,
-            array(
-                'column' => 'docserver_id',
-                'value' => $docserverId,
-                'type' => 'string',
-            )
-        );
-
-        return $data;
-=======
 
             return ['errors' => 'docserverId ' . _EMPTY];
         }
@@ -858,6 +548,5 @@ class ResController
             );
             return $returnResArray;
         }
->>>>>>> lgi-feat-attachments
     }
 }
\ No newline at end of file
diff --git a/core/Test/ResControllerTest.php b/core/Test/ResControllerTest.php
index 7bdd3667806c020e3c2172d792fa57fd87f94bfa..14e3b8290387ac24c5e3d5443087f9068f442d2d 100644
--- a/core/Test/ResControllerTest.php
+++ b/core/Test/ResControllerTest.php
@@ -116,10 +116,8 @@ class ResControllerTest extends PHPUnit_Framework_TestCase
 
         $response = $action->storeResource($aArgs);
         
-        $this->assertGreaterThanOrEqual(0, $response);
+        $this->assertGreaterThanOrEqual(0, $response[0]);
     }
-<<<<<<< HEAD
-=======
 
     public function testStoreExtResource()
     {
@@ -192,5 +190,4 @@ class ResControllerTest extends PHPUnit_Framework_TestCase
         
         $this->assertGreaterThanOrEqual(0, $response);
     }
->>>>>>> lgi-feat-attachments
 }
\ No newline at end of file
diff --git a/modules/attachments/Controllers/AttachmentsController.php b/modules/attachments/Controllers/AttachmentsController.php
index 7d20149d5e5ad3c05f53e51f8163de27e6d694fd..cf9235259f8105f8d3937d2eaa306d448fc8b762 100644
--- a/modules/attachments/Controllers/AttachmentsController.php
+++ b/modules/attachments/Controllers/AttachmentsController.php
@@ -38,11 +38,21 @@ class AttachmentsController
             return ['errors' => 'encodedFile ' . _EMPTY];
         }
 
+        if (empty($aArgs['data'])) {
+
+            return ['errors' => 'data ' . _EMPTY];
+        }
+
         if (empty($aArgs['collId'])) {
 
             return ['errors' => 'collId ' . _EMPTY];
         }
 
+        if (empty($aArgs['collIdMaster'])) {
+
+            return ['errors' => 'collIdMaster ' . _EMPTY];
+        }
+
         if (empty($aArgs['table'])) {
 
             return ['errors' => 'table ' . _EMPTY];
@@ -53,81 +63,213 @@ class AttachmentsController
             return ['errors' => 'fileFormat ' . _EMPTY];
         }
 
-        if (empty($aArgs['title'])) {
-
-            return ['errors' => 'title ' . _EMPTY];
-        }
-
         $resId = $aArgs['resId'];
         $encodedFile = $aArgs['encodedFile'];
         $collId = $aArgs['collId'];
+        $collIdMaster = $aArgs['collIdMaster'];
         $table = $aArgs['table'];
         $fileFormat = $aArgs['fileFormat'];
-        $title = $aArgs['title'];
 
-        if (!empty($aArgs['data'])) {
+        $aArgs = [
+            'data'   => $aArgs['data'],
+            'collIdMaster' => $collIdMaster,
+            'resId'  => $resId,
+        ];
+        
+        $returnPrepare = $this->prepareStorage($aArgs);
+        
+        $aArgs = [
+            'encodedFile'   => $encodedFile,
+            'data'          => $returnPrepare['data'],
+            'collId'        => $collId,
+            'table'         => $table,
+            'fileFormat'    => $fileFormat,
+            'status'        => $returnPrepare['status'],
+        ];
+
+        $res = new \Core\Controllers\ResController();
+        $response = $res->storeResource($aArgs);
+
+        //return $response;
+        if (!is_numeric($response[0])) {
 
-            $data = $aArgs['data'];
+            return ['errors' => 'Pb with SQL insertion : ' . $response[0]];
         } else {
-            $data = [];
+            require_once 'core/class/class_history.php';
+            require_once 'core/class/class_security.php';
+            $hist = new \history();
+            $sec = new \security();
+            $view = $sec->retrieve_view_from_coll_id($collIdMaster);
+            
+            $hist->add(
+                $view, $resId, "ADD", 'attachadd',
+                ucfirst(_DOC_NUM) . $response[0] . ' '
+                . _NEW_ATTACH_ADDED . ' ' . _TO_MASTER_DOCUMENT
+                . $resId,
+                $_SESSION['config']['databasetype'],
+                'apps'
+            );
+            $hist->add(
+                $table, $response[0], "ADD",'attachadd',
+                _NEW_ATTACH_ADDED,
+                $_SESSION['config']['databasetype'],
+                'attachments'
+            );
+        }
+
+        if ($response[0] == 0) {
+            $response[0] = '';
+        }
+
+        return [$response[0]];
+    }
+
+    /**
+     * Prepares storage on database.
+     * @param  $data array
+     * @param  $resId bigint
+     * @param  $collIdMaster string
+     * @return $data
+     */
+    public function prepareStorage($aArgs)
+    {
+        if (empty($aArgs['data'])) {
+
+            return ['errors' => 'data ' . _EMPTY];
+        }
+
+        if (empty($aArgs['resId'])) {
+
+            return ['errors' => 'resId ' . _EMPTY];
+        }
+
+        if (empty($aArgs['collIdMaster'])) {
+
+            return ['errors' => 'collIdMaster ' . _EMPTY];
+        }
+
+        $statusFound = false;
+        $typistFound = false;
+        $typeIdFound = false;
+        $attachmentTypeFound = false;
+        
+        $data = $aArgs['data'];
+        
+        $countD = count($data);
+        for ($i=0;$i<$countD;$i++) {
+
+            if (
+                strtoupper($data[$i]['type']) == 'INTEGER' || 
+                strtoupper($data[$i]['type']) == 'FLOAT'
+            ) {
+                if ($data[$i]['value'] == '') {
+                    $data[$i]['value'] = '0';
+                }
+            }
+
+            if (strtoupper($data[$i]['type']) == 'STRING') {
+               $data[$i]['value'] = $data[$i]['value'];
+               $data[$i]['value'] = str_replace(";", "", $data[$i]['value']);
+               $data[$i]['value'] = str_replace("--", "", $data[$i]['value']);
+            }
+
+            if (strtoupper($data[$i]['column']) == strtoupper('status')) {
+                $statusFound = true;
+                $status = $data[$i]['value'];
+            }
+
+            if (strtoupper($data[$i]['column']) == strtoupper('typist')) {
+                $typistFound = true;
+            }
+
+            if (strtoupper($data[$i]['column']) == strtoupper('type_id')) {
+                $typeIdFound = true;
+            }
+
+            if (strtoupper($data[$i]['column']) == strtoupper('attachment_type')) {
+                $attachmentTypeFound = true;
+            }
+        }
+
+        if (!$typistFound) {
+            array_push(
+                $data,
+                array(
+                    'column' => 'typist',
+                    'value' => $_SESSION['user']['UserId'],
+                    'type' => 'string',
+                )
+            );
+        }
+
+        if (!$typeIdFound) {
+            array_push(
+                $data,
+                array(
+                    'column' => 'type_id',
+                    'value' => 0,
+                    'type' => 'int',
+                )
+            );
         }
         
-        array_push(
-            $data,
-            array(
-                'column' => "typist",
-                'value' => $_SESSION['user']['UserId'],
-                'type' => "string",
-            )
-        );
+        if (!$statusFound) {
+            array_push(
+                $data,
+                array(
+                    'column' => 'status',
+                    'value' => 'NEW',
+                    'type' => 'string',
+                )
+            );
+            $status = 'NEW';
+        }
+        
+        if (!$attachmentTypeFound) {
+            array_push(
+                $data,
+                array(
+                    'column' => 'attachment_type',
+                    'value' => 'response_project',
+                    'type' => 'string',
+                )
+            );
+        }
 
-        array_push(
-            $data,
-            array(
-                'column' => "title",
-                'value' => strtolower($title),
-                'type' => "string",
-            )
-        );
+        //BASICS
         array_push(
             $data,
             array(
                 'column' => "coll_id",
-                'value' => $collId,
+                'value' => $aArgs['collIdMaster'],
                 'type' => "string",
             )
         );
+
         array_push(
             $data,
             array(
                 'column' => "res_id_master",
-                'value' => $resId,
+                'value' => $aArgs['resId'],
                 'type' => "integer",
             )
         );
+
         array_push(
             $data,
             array(
-                'column' => "type_id",
-                'value' => 0,
-                'type' => "int",
+                'column' => "relation",
+                'value' => 1,
+                'type' => "integer",
             )
         );
 
-        $aArgs = [
-            'encodedFile'   => $encodedFile,
-            'data'          => $data,
-            'collId'        => $collId,
-            'table'         => $table,
-            'fileFormat'    => $fileFormat,
-            'status'        => 'NEW',
+        $return = [
+            'data'   => $data,
+            'status' => $status,
         ];
 
-        $res = new \Core\Controllers\ResController();
-        $response = $res->storeResource($aArgs);
-
-        print_r($response);exit;
-
+        return $return;
     }
 
 }
\ No newline at end of file
diff --git a/modules/attachments/Test/AttachmentsControllerTest.php b/modules/attachments/Test/AttachmentsControllerTest.php
index 6858cfef2e0cd017f5a2ccb686e968b500243a99..9e34ffb54c6af98f5437b611933199447c261b60 100644
--- a/modules/attachments/Test/AttachmentsControllerTest.php
+++ b/modules/attachments/Test/AttachmentsControllerTest.php
@@ -12,6 +12,50 @@ require_once 'core/Test/define.php';
 class AttachmentsControllerTest extends PHPUnit_Framework_TestCase
 {
 
+    public function testPrepareStorage()
+    {
+        $action = new \Attachments\Controllers\AttachmentsController();
+
+        $data = [];
+
+        array_push(
+            $data,
+            array(
+                'column' => 'title',
+                'value' => 'test pj',
+                'type' => 'string',
+            )
+        );
+
+        array_push(
+            $data,
+            array(
+                'column' => 'attachment_type',
+                'value' => 'response_project',
+                'type' => 'string',
+            )
+        );
+
+        array_push(
+            $data,
+            array(
+                'column' => 'status',
+                'value' => 'A_TRA',
+                'type' => 'string',
+            )
+        );
+
+        $aArgs = [
+            'data'         => $data,
+            'resId'        => 100,
+            'collIdMaster' => 'letterbox_coll',
+        ];
+
+        $response = $action->prepareStorage($aArgs);
+        
+        $this->assertArrayHasKey('column', $response['data'][0]);
+    }
+
     public function testStoreAttachmentResource()
     {
         $action = new \Attachments\Controllers\AttachmentsController();
@@ -24,24 +68,54 @@ class AttachmentsControllerTest extends PHPUnit_Framework_TestCase
 
         $fileSource = 'test_source.txt';
 
-        $fp = fopen($path . $fileSource, 'a');
+        $fp = fopen($path . $fileSource, 'w');
         fwrite($fp, 'a unit test');
         fclose($fp);
 
         $fileContent = file_get_contents($path . $fileSource, FILE_BINARY);
         $encodedFile = base64_encode($fileContent);
 
+        $data = [];
+
+        array_push(
+            $data,
+            array(
+                'column' => 'title',
+                'value' => 'test pj',
+                'type' => 'string',
+            )
+        );
+
+        array_push(
+            $data,
+            array(
+                'column' => 'attachment_type',
+                'value' => 'response_project',
+                'type' => 'string',
+            )
+        );
+
+        array_push(
+            $data,
+            array(
+                'column' => 'status',
+                'value' => 'A_TRA',
+                'type' => 'string',
+            )
+        );
+
         $aArgs = [
             'resId'         => 100,
             'encodedFile'   => $encodedFile,
+            'data'          => $data,
             'collId'        => 'attachments_coll',
+            'collIdMaster'  => 'letterbox_coll',
             'table'         => 'res_attachments',
             'fileFormat'    => 'txt',
-            'title'         => 'test pj',
         ];
 
         $response = $action->storeAttachmentResource($aArgs);
-        print_r($response);exit;
-        $this->assertGreaterThanOrEqual(0, $response);
+        
+        $this->assertGreaterThanOrEqual(0, $response[0]);
     }
 }
\ No newline at end of file