From 9f10b3e3386987b728f4765a33741e0a2acb965e Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Tue, 14 Jan 2020 11:23:24 +0100
Subject: [PATCH] FEAT #10633 TIME 1:30 action reject visa back to previous

---
 migration/19.12/1912.sql                      |  2 ++
 sql/data_fr.sql                               |  2 +-
 .../controllers/ActionMethodController.php    | 29 +++++++++++++++++++
 3 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/migration/19.12/1912.sql b/migration/19.12/1912.sql
index 58eac152b8d..c71e998918f 100644
--- a/migration/19.12/1912.sql
+++ b/migration/19.12/1912.sql
@@ -86,6 +86,8 @@ where group_id in (
 UPDATE actions SET component = 'confirmAction', action_page = 'confirm_status' WHERE action_page in ('validate_mail', 'process', 'visa_mail');
 DELETE FROM actions WHERE action_page = 'view' OR component = 'viewDoc';
 
+UPDATE actions SET component = 'rejectVisaBackToPrevious' WHERE action_page = 'rejection_visa_previous';
+
 
 /* FOLDERS */
 DO $$ BEGIN
diff --git a/sql/data_fr.sql b/sql/data_fr.sql
index 6edf576b484..f25df523f4a 100755
--- a/sql/data_fr.sql
+++ b/sql/data_fr.sql
@@ -996,7 +996,7 @@ INSERT INTO actions (id, keyword, label_action, id_status, is_system, action_pag
 INSERT INTO actions (id, keyword, label_action, id_status, is_system, action_page, history, component) VALUES (400, '', 'Envoyer un AR', '_NOSTATUS_', 'N', 'send_attachments_to_contact', 'Y', 'v1Action');
 INSERT INTO actions (id, keyword, label_action, id_status, is_system, action_page, history, component) VALUES (405, '', 'Viser le courrier', '_NOSTATUS_', 'N', 'confirm_status', 'Y', 'confirmAction');
 INSERT INTO actions (id, keyword, label_action, id_status, is_system, action_page, history, component) VALUES (407, '', 'Renvoyer pour traitement', 'COU', 'N', 'confirm_status', 'Y', 'confirmAction');
-INSERT INTO actions (id, keyword, label_action, id_status, is_system, action_page, history, component) VALUES (408, '', 'Refuser le visa et remonter le circuit', '_NOSTATUS_', 'N',  'rejection_visa_previous', 'N', 'v1Action');
+INSERT INTO actions (id, keyword, label_action, id_status, is_system, action_page, history, component) VALUES (408, '', 'Refuser le visa et remonter le circuit', '_NOSTATUS_', 'N',  'rejection_visa_previous', 'N', 'rejectVisaBackToPrevious');
 INSERT INTO actions (id, keyword, label_action, id_status, is_system, action_page, history, component) VALUES (410, '', 'Transmettre la réponse signée', 'EENV', 'N', 'interrupt_visa', 'Y', 'v1Action');
 INSERT INTO actions (id, keyword, label_action, id_status, is_system, action_page, history, component) VALUES (414, '', 'Envoyer au parapheur', '_NOSTATUS_', 'N', 'send_to_visa', 'Y', 'v1Action');
 INSERT INTO actions (id, keyword, label_action, id_status, is_system, action_page, history, component) VALUES (416, '', 'Valider et poursuivre le circuit', '_NOSTATUS_', 'N', 'visa_workflow', 'Y', 'v1Action');
diff --git a/src/app/action/controllers/ActionMethodController.php b/src/app/action/controllers/ActionMethodController.php
index 2eec67ad3ef..e153a703b68 100644
--- a/src/app/action/controllers/ActionMethodController.php
+++ b/src/app/action/controllers/ActionMethodController.php
@@ -50,6 +50,7 @@ class ActionMethodController
         'updateAcknowledgementSendDateAction'   => 'updateAcknowledgementSendDateAction',
         'sendShippingAction'                    => 'createMailevaShippings',
         'sendSignatureBookAction'               => 'sendSignatureBook',
+        'rejectVisaBackToPrevious'              => 'rejectVisaBackToPrevious',
         'noConfirmAction'                       => null
     ];
 
@@ -341,4 +342,32 @@ class ActionMethodController
 
         return ['history' => $historyInfo];
     }
+
+    public static function rejectVisaBackToPrevious(array $args)
+    {
+        ValidatorModel::notEmpty($args, ['resId']);
+        ValidatorModel::intVal($args, ['resId']);
+
+        $listInstances = ListInstanceModel::get([
+            'select'   => ['listinstance_id'],
+            'where'    => ['res_id = ?', 'difflist_type = ?', 'process_date is not null'],
+            'data'     => [$args['resId'], 'VISA_CIRCUIT'],
+            'order_by' => ['listinstance_id desc'],
+            'limit'    => 1
+        ]);
+
+        if (empty($listInstances)) {
+            return false;
+        }
+
+        $listInstances = $listInstances[0];
+
+        ListInstanceModel::update([
+            'set'   => ['process_date' => null],
+            'where' => ['listinstance_id = ?'],
+            'data'  => [$listInstances['listinstance_id']]
+        ]);
+
+        return true;
+    }
 }
-- 
GitLab