From 55f651bbf122a944dcf9d251dc17ae9f7f5a501f Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Thu, 16 May 2019 17:29:56 +0100
Subject: [PATCH] FEAT #8391 TIME 1 WIP retrieve doc with usergeneric in
 xparaph

---
 .../batch/process_mailsFromSignatoryBook.php  |   2 +-
 .../visa/xml/remoteSignatoryBooks.xml.default |   5 +
 .../controllers/XParaphController.php         | 168 ++++++++++--------
 3 files changed, 97 insertions(+), 78 deletions(-)

diff --git a/modules/visa/batch/process_mailsFromSignatoryBook.php b/modules/visa/batch/process_mailsFromSignatoryBook.php
index 3706531e921..bb0b4b83781 100755
--- a/modules/visa/batch/process_mailsFromSignatoryBook.php
+++ b/modules/visa/batch/process_mailsFromSignatoryBook.php
@@ -230,7 +230,7 @@ if (file_exists($GLOBALS['errorLckFile'])) {
 Bt_getWorkBatch();
 
 $GLOBALS['logger']->write('Retrieve attachments sent to remote signatory book', 'INFO');
-$query = "SELECT res_id, res_id_version, external_id->>'signatureBookId' as external_id, 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, res_id_version, external_id->>'signatureBookId' as external_id, external_id->>'xparaphDepot', format, res_id_master, title, identifier, type_id, attachment_type, dest_contact_id, dest_address_id, dest_user, typist, attachment_id_master, relation 
         FROM res_view_attachments WHERE status = 'FRZ' AND external_id->>'signatureBookId' IS NOT NULL AND external_id->>'signatureBookId' <> ''";
 $stmt = $GLOBALS['db']->query($query, []);
     
diff --git a/modules/visa/xml/remoteSignatoryBooks.xml.default b/modules/visa/xml/remoteSignatoryBooks.xml.default
index 221ad34d155..d0d5f14b13d 100644
--- a/modules/visa/xml/remoteSignatoryBooks.xml.default
+++ b/modules/visa/xml/remoteSignatoryBooks.xml.default
@@ -48,6 +48,11 @@
     <signatoryBook>
         <id>xParaph</id>
         <url>https://preprodssl.ssl.spl-xdemat.fr/Xparaph/wsdl/wsdl.php</url>
+        <userGeneric>
+            <siret></siret>
+            <login></login>
+            <password></password>
+        </userGeneric>
         <docutype>101</docutype>
         <docustype>9</docustype>
     </signatoryBook>
diff --git a/src/app/external/externalSignatoryBook/controllers/XParaphController.php b/src/app/external/externalSignatoryBook/controllers/XParaphController.php
index fba97523dd6..80818c8157f 100755
--- a/src/app/external/externalSignatoryBook/controllers/XParaphController.php
+++ b/src/app/external/externalSignatoryBook/controllers/XParaphController.php
@@ -368,84 +368,94 @@ class XParaphController
     public static function retrieveSignedMails($aArgs)
     {
         $tmpPath = CoreConfigModel::getTmpPath();
+
         foreach (['noVersion', 'isVersion'] as $version) {
-            $depotids = [];
+            $depotsBySiret = [];
             foreach ($aArgs['idsToRetrieve'][$version] as $resId => $value) {
-                $depotids[$value->external_id] = $resId;
-            }
-
-            if (!empty($depotids)) {
-                $avancements = XParaphController::getAvancement(['config' => $aArgs['config'], 'depotsIds' => $depotids]);
-            } else {
-                unset($aArgs['idsToRetrieve'][$version]);
-                continue;
+                $externalId = json_decode($value['xparaphDepot'], true);
+                $depotsBySiret[$externalId['siret']][$value->external_id] = ['resId' => $resId, 'login' => $externalId['login']];
             }
 
-            foreach ($aArgs['idsToRetrieve'][$version] as $resId => $value) {
-                $avancement = $avancements[$value->external_id];
-
-                $state = XParaphController::getState(['avancement' => $avancement]);
-
-                if ($state['id'] == 'refused') {
-                    $aArgs['idsToRetrieve'][$version][$resId]->status = 'refused';
-                    $aArgs['idsToRetrieve'][$version][$resId]->noteContent = $state['note'];
-
-                    $processedFile = XParaphController::getFile(['config' => $aArgs['config'], 'depotId' => $value->external_id]);
-                    if (!empty($processedFile['errors'])) {
-                        unset($aArgs['idsToRetrieve'][$version][$resId]);
-                        continue;
-                    }
-                    $file      = base64_decode($processedFile['zip']);
-                    $unzipName = 'tmp_file_' .rand(). '_xParaph_' .rand();
-                    $tmpName   = $unzipName . '.zip';
-            
-                    file_put_contents($tmpPath . $tmpName, $file);
-
-                    $zip = new \ZipArchive();
-                    $zip->open($tmpPath . $tmpName);
-                    $zip->extractTo($tmpPath . $unzipName);
-
-                    foreach (glob($tmpPath . $unzipName . '/*.xml') as $filename) {
-                        $log = base64_encode(file_get_contents($filename));
+            foreach ($depotsBySiret as $siret => $depotids) {
+                foreach ($aArgs['config']['userGeneric'] as $userGenericXml) {
+                    if ($userGenericXml['siret'] == $siret) {
+                        $userGeneric = $userGenericXml;
+                        break;
                     }
-                    unlink($tmpPath . $tmpName);
-
-                    $aArgs['idsToRetrieve'][$version][$resId]->log = $log;
-                } elseif ($state['id'] == 'validateSignature' || $state['id'] == 'validateOnlyVisa') {
-                    $processedFile = XParaphController::getFile(['config' => $aArgs['config'], 'depotId' => $value->external_id]);
-                    if (!empty($processedFile['errors'])) {
+                }
+                if (!empty($depotids)) {
+                    $avancements = XParaphController::getAvancement(['config' => $aArgs['config'], 'depotsIds' => $depotids, 'userGeneric' => $userGeneric]);
+                } else {
+                    unset($aArgs['idsToRetrieve'][$version]);
+                    continue;
+                }
+    
+                foreach ($aArgs['idsToRetrieve'][$version] as $resId => $value) {
+                    $avancement = $avancements[$value->external_id];
+    
+                    $state = XParaphController::getState(['avancement' => $avancement]);
+    
+                    if ($state['id'] == 'refused') {
+                        $aArgs['idsToRetrieve'][$version][$resId]->status = 'refused';
+                        $aArgs['idsToRetrieve'][$version][$resId]->noteContent = $state['note'];
+    
+                        $processedFile = XParaphController::getFile(['config' => $aArgs['config'], 'depotId' => $value->external_id, 'userGeneric' => $userGeneric, 'depotLogin' => $depotids['login']]);
+                        if (!empty($processedFile['errors'])) {
+                            unset($aArgs['idsToRetrieve'][$version][$resId]);
+                            continue;
+                        }
+                        $file      = base64_decode($processedFile['zip']);
+                        $unzipName = 'tmp_file_' .rand(). '_xParaph_' .rand();
+                        $tmpName   = $unzipName . '.zip';
+                
+                        file_put_contents($tmpPath . $tmpName, $file);
+    
+                        $zip = new \ZipArchive();
+                        $zip->open($tmpPath . $tmpName);
+                        $zip->extractTo($tmpPath . $unzipName);
+    
+                        foreach (glob($tmpPath . $unzipName . '/*.xml') as $filename) {
+                            $log = base64_encode(file_get_contents($filename));
+                        }
+                        unlink($tmpPath . $tmpName);
+    
+                        $aArgs['idsToRetrieve'][$version][$resId]->log = $log;
+                    } elseif ($state['id'] == 'validateSignature' || $state['id'] == 'validateOnlyVisa') {
+                        $processedFile = XParaphController::getFile(['config' => $aArgs['config'], 'depotId' => $value->external_id]);
+                        if (!empty($processedFile['errors'])) {
+                            unset($aArgs['idsToRetrieve'][$version][$resId]);
+                            continue;
+                        }
+                        $aArgs['idsToRetrieve'][$version][$resId]->status = 'validated';
+                        $aArgs['idsToRetrieve'][$version][$resId]->format = 'pdf';
+    
+                        $file      = base64_decode($processedFile['zip']);
+                        $unzipName = 'tmp_file_' .rand(). '_xParaph_' .rand();
+                        $tmpName   = $unzipName . '.zip';
+                
+                        file_put_contents($tmpPath . $tmpName, $file);
+    
+                        $zip = new \ZipArchive();
+                        $zip->open($tmpPath . $tmpName);
+                        $zip->extractTo($tmpPath . $unzipName);
+    
+                        foreach (glob($tmpPath . $unzipName . '/*.pdf') as $filename) {
+                            $encodedFile = base64_encode(file_get_contents($filename));
+                        }
+                        foreach (glob($tmpPath . $unzipName . '/*.xml') as $filename) {
+                            $log = base64_encode(file_get_contents($filename));
+                        }
+                        unlink($tmpPath . $tmpName);
+    
+                        $aArgs['idsToRetrieve'][$version][$resId]->encodedFile = $encodedFile;
+                        $aArgs['idsToRetrieve'][$version][$resId]->noteContent = $state['note'];
+                        if ($state['id'] == 'validateOnlyVisa') {
+                            $aArgs['idsToRetrieve'][$version][$resId]->onlyVisa = true;
+                        }
+                        $aArgs['idsToRetrieve'][$version][$resId]->log = $log;
+                    } else {
                         unset($aArgs['idsToRetrieve'][$version][$resId]);
-                        continue;
-                    }
-                    $aArgs['idsToRetrieve'][$version][$resId]->status = 'validated';
-                    $aArgs['idsToRetrieve'][$version][$resId]->format = 'pdf';
-
-                    $file      = base64_decode($processedFile['zip']);
-                    $unzipName = 'tmp_file_' .rand(). '_xParaph_' .rand();
-                    $tmpName   = $unzipName . '.zip';
-            
-                    file_put_contents($tmpPath . $tmpName, $file);
-
-                    $zip = new \ZipArchive();
-                    $zip->open($tmpPath . $tmpName);
-                    $zip->extractTo($tmpPath . $unzipName);
-
-                    foreach (glob($tmpPath . $unzipName . '/*.pdf') as $filename) {
-                        $encodedFile = base64_encode(file_get_contents($filename));
                     }
-                    foreach (glob($tmpPath . $unzipName . '/*.xml') as $filename) {
-                        $log = base64_encode(file_get_contents($filename));
-                    }
-                    unlink($tmpPath . $tmpName);
-
-                    $aArgs['idsToRetrieve'][$version][$resId]->encodedFile = $encodedFile;
-                    $aArgs['idsToRetrieve'][$version][$resId]->noteContent = $state['note'];
-                    if ($state['id'] == 'validateOnlyVisa') {
-                        $aArgs['idsToRetrieve'][$version][$resId]->onlyVisa = true;
-                    }
-                    $aArgs['idsToRetrieve'][$version][$resId]->log = $log;
-                } else {
-                    unset($aArgs['idsToRetrieve'][$version][$resId]);
                 }
             }
         }
@@ -495,15 +505,18 @@ class XParaphController
             $depotIds .= '<listDepotIds>'.$key.'</listDepotIds>';
         }
 
+        // use $step['login'] because only need anyone who exist in xparaph
+
         $xmlPostString = '<?xml version="1.0" encoding="utf-8"?>
         <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:parafwsdl" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
             <soapenv:Header/>
             <soapenv:Body>
                 <urn:XPRF_AvancementDepot soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
                     <params xsi:type="urn:XPRF_AvancementDepot_Param">
-                        <siret xsi:type="xsd:string">?</siret>
-                        <login xsi:type="xsd:string">?</login>
-                        <password xsi:type="xsd:string">?</password>
+                        <siret xsi:type="xsd:string">'.$aArgs['userGeneric']['siret'].'</siret>
+                        <login xsi:type="xsd:string">'.$step['login'].'</login>
+                        <logingen xsi:type="xsd:string">'.$aArgs['userGeneric']['login'].'</logingen>
+                        <password xsi:type="xsd:string">'.$aArgs['userGeneric']['password'].'</password>
                         <depotids xsi:type="urn:listDepotIds" soapenc:arrayType="xsd:string[]">' . $depotIds . '</depotids>
                         <withNote xsi:type="xsd:string">1</withNote>
                     </params>
@@ -536,9 +549,10 @@ class XParaphController
             <soapenv:Body>
                 <urn:XPRF_getFiles soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
                     <params xsi:type="urn:XPRF_getFiles_Param">
-                        <siret xsi:type="xsd:string">?</siret>
-                        <login xsi:type="xsd:string">?</login>
-                        <password xsi:type="xsd:string">?</password>
+                        <siret xsi:type="xsd:string">'.$aArgs['userGeneric']['siret'].'</siret>
+                        <login xsi:type="xsd:string">'.$aArgs['depotLogin'].'</login>
+                        <logingen xsi:type="xsd:string">'.$aArgs['userGeneric']['login'].'</logingen>
+                        <password xsi:type="xsd:string">'.$aArgs['userGeneric']['password'].'</password>
                         <depotid xsi:type="xsd:string">'.$aArgs['depotId'].'</depotid>
                     </params>
                 </urn:XPRF_getFiles>
-- 
GitLab