From 132726afd1629c4575f456775c8f1fc0b6555632 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Fri, 31 Aug 2018 19:25:51 +0200
Subject: [PATCH] fix remove signed file if unsign file

---
 modules/visa/save_attach_res_from_cm.php         | 15 ++++++++++-----
 modules/visa/sign_file.php                       | 16 ++++++----------
 .../models/AttachmentModelAbstract.php           |  2 +-
 3 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/modules/visa/save_attach_res_from_cm.php b/modules/visa/save_attach_res_from_cm.php
index 59a45e00e5a..eb4e3cda7c8 100755
--- a/modules/visa/save_attach_res_from_cm.php
+++ b/modules/visa/save_attach_res_from_cm.php
@@ -54,10 +54,10 @@ if (empty($docserver)) {
 			$db = new Database();
 			$signatoryUser = \User\models\UserModel::getByUserId(['userId' => $_SESSION['user']['UserId'], 'select' => ['id']]);
 			if ($_SESSION['visa']['repSignRel'] > 1) {
-                //$target_table = 'res_version_attachments';
+                $target_table = 'res_version_attachments';
                 $db->query("UPDATE res_version_attachments set status = 'SIGN', signatory_user_serial_id = ? WHERE res_id = ?",[$signatoryUser['id'], $_SESSION['visa']['repSignId']]);
             } else {
-                //$target_table = 'res_attachments';
+                $target_table = 'res_attachments';
 				$db->query("UPDATE res_attachments set status = 'SIGN', signatory_user_serial_id = ? WHERE res_id = ?",[$signatoryUser['id'], $_SESSION['visa']['repSignId']]);
             }
 			unset($_SESSION['visa']['repSignRel']);
@@ -196,9 +196,14 @@ if (empty($docserver)) {
                 'type' => 'string'
             ];
 
-            if (empty($_REQUEST['id'])) $id_origin = $objectId;
-            else $id_origin = $_REQUEST['id'];
-            if (empty($target_table)) $target_table = "res_attachments";
+            if (empty($_REQUEST['id'])) {
+                $id_origin = $objectId;
+            } else {
+                $id_origin = $_REQUEST['id'];
+            }
+            if (empty($target_table)) {
+                $target_table = "res_attachments";
+            }
             array_push(
                 $_SESSION['data'],
                 array(
diff --git a/modules/visa/sign_file.php b/modules/visa/sign_file.php
index 31564b081c1..7644b7472a9 100755
--- a/modules/visa/sign_file.php
+++ b/modules/visa/sign_file.php
@@ -58,21 +58,21 @@ if (!empty($_REQUEST['id']) && !empty($_REQUEST['collId'])) {
     $tableName = 'res_view_attachments';
     if (isset($_REQUEST['isOutgoing'])) {
         if (isset($_REQUEST['isVersion'])) {
-            $stmt = $db->query("select res_id_version, format, res_id_master, title, identifier, type_id, attachment_type, dest_contact_id, dest_address_id, dest_user from "
+            $stmt = $db->query("select relation, res_id_version, format, res_id_master, title, identifier, type_id, attachment_type, dest_contact_id, dest_address_id, dest_user from "
                 . $tableName
                 . " where attachment_type = ? and res_id_version = ?", ['outgoing_mail', $objectId]);
         } else {
-            $stmt = $db->query("select res_id, format, res_id_master, title, identifier, type_id, attachment_type, dest_contact_id, dest_address_id, dest_user from "
+            $stmt = $db->query("select relation, res_id, format, res_id_master, title, identifier, type_id, attachment_type, dest_contact_id, dest_address_id, dest_user from "
                 . $tableName
                 . " where attachment_type = ? and res_id = ?", ['outgoing_mail', $objectId]);
         }
     } else {
         if (isset($_REQUEST['isVersion'])) {
-            $stmt = $db->query("select res_id_version, format, res_id_master, title, identifier, type_id, attachment_type, dest_contact_id, dest_address_id, dest_user from "
+            $stmt = $db->query("select relation, res_id_version, format, res_id_master, title, identifier, type_id, attachment_type, dest_contact_id, dest_address_id, dest_user from "
                 . $tableName
                 . " where attachment_type NOT IN ('converted_pdf','print_folder') and res_id_version = ?", array($objectId));
         } else {
-            $stmt = $db->query("select res_id, format, res_id_master, title, identifier, type_id, attachment_type, dest_contact_id, dest_address_id, dest_user from "
+            $stmt = $db->query("select relation, res_id, format, res_id_master, title, identifier, type_id, attachment_type, dest_contact_id, dest_address_id, dest_user from "
                 . $tableName
                 . " where (attachment_type NOT IN ('converted_pdf','print_folder')) and res_id = ?", array($objectId));
         }
@@ -111,6 +111,8 @@ if (!empty($_REQUEST['id']) && !empty($_REQUEST['collId'])) {
             exit;
         }
 
+        $_SESSION['visa']['repSignRel'] = $line->relation;
+        $_SESSION['visa']['repSignId'] = $attachResId;
         $docserver = \Docserver\models\DocserverModel::getByDocserverId(['docserverId' => $convertedAttachment["docserver_id"], 'select' => ['path_template']]);
 
         $fileOnDs = $docserver["path_template"] . $convertedAttachment["path"] . $convertedAttachment["filename"];
@@ -192,12 +194,6 @@ if (!empty($_REQUEST['id']) && !empty($_REQUEST['collId'])) {
 
         include 'modules/visa/save_attach_res_from_cm.php';
         $db->query('UPDATE listinstance set signatory = TRUE WHERE listinstance_id = (SELECT listinstance_id FROM listinstance WHERE res_id = ? AND item_id = ? AND difflist_type = ? AND process_date is null ORDER BY listinstance_id LIMIT 1)', [$objectResIdMaster, $_SESSION['user']['UserId'], 'VISA_CIRCUIT']);
-        
-        if (isset($_REQUEST['isVersion'])) {
-            $db->query("UPDATE res_version_attachments set status = 'SIGN' WHERE res_id = ?", [$attachResId]);
-        } else {
-            $db->query("UPDATE res_attachments set status = 'SIGN' WHERE res_id = ?", [$attachResId]);
-        }
 
         echo "{\"status\": 0, \"new_id\": $id}";
         exit;
diff --git a/src/app/attachment/models/AttachmentModelAbstract.php b/src/app/attachment/models/AttachmentModelAbstract.php
index a74896a0618..ff02efb1592 100644
--- a/src/app/attachment/models/AttachmentModelAbstract.php
+++ b/src/app/attachment/models/AttachmentModelAbstract.php
@@ -166,7 +166,7 @@ abstract class AttachmentModelAbstract
         ]);
 
         DatabaseModel::update([
-            'table'     => $aArgs['table'],
+            'table'     => 'res_attachments',
             'set'       => ['status' => 'DEL'],
             'where'     => ['origin = ?', 'status != ?'],
             'data'      => ["{$aArgs['resId']},{$aArgs['table']}", 'DEL']
-- 
GitLab