From 01b6c29e99a8add5316a0062d5e9e9308edd4cfb Mon Sep 17 00:00:00 2001
From: VESSILLER <lvessiller@open-dsi.fr>
Date: Wed, 6 May 2020 12:15:51 +0200
Subject: [PATCH] Module Dematpayslip - NEW nouveau decoupage des PDF des
 bulletins de paie

---
 lib/Controller/PageController.php | 118 +++++++++++++++++-------------
 1 file changed, 67 insertions(+), 51 deletions(-)

diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php
index ed9857b..9d318c1 100644
--- a/lib/Controller/PageController.php
+++ b/lib/Controller/PageController.php
@@ -28,12 +28,28 @@ class PageController extends Controller {
 
     const PROCESS_TIME_UNLOCK = 600; // seconds (max execution time)
 
-    const PAHSE_ID_UPLOAD = 0;
-    const PAHSE_ID_CHECK  = 1;
-    const PAHSE_ID_INDEXATION = 2;
+    const PHASE_ID_UPLOAD = 0;
+    const PHASE_ID_CHECK  = 1;
+    const PHASE_ID_INDEXATION = 2;
 
     const USER_REMOVE_UNKNOWN = 1; // remove unknown users from list (FIX SAML)
 
+    /**
+     * @var array Payslip PDF search field list
+     */
+//    private $_payslipPDFSearchFieldList = array(
+//        'siret'     => array(3),
+//        'matricule' => array(10),
+//        'fullname'  => array(9),
+//        'zip_city'  => array(19, 20),
+//    );
+    private $_payslipPDFSearchFieldList = array(
+        'siret'     => array(4, 5),
+        'matricule' => array(20, 21),
+        'fullname'  => array(19, 20),
+        'zip_city'  => array(34, 35, 36, 37),
+    );
+
     /**
      * @var string CSV delimiter
      */
@@ -186,15 +202,15 @@ class PageController extends Controller {
         $phaseName = '';
 
         switch ($phaseId) {
-            case self::PAHSE_ID_UPLOAD :
+            case self::PHASE_ID_UPLOAD :
                 $phaseName = 'Upload';
             break;
 
-            case self::PAHSE_ID_CHECK :
+            case self::PHASE_ID_CHECK :
                 $phaseName = 'Vérification';
             break;
 
-            case self::PAHSE_ID_INDEXATION :
+            case self::PHASE_ID_INDEXATION :
                 $phaseName = 'Indexation';
             break;
 
@@ -853,7 +869,7 @@ class PageController extends Controller {
         if ($this->userId === $this->_dematpayslipConfigList['user_id']) {
             $this->_initDematUser();
             $this->_processCheck();
-            if ($this->_process->getPhase() < self::PAHSE_ID_CHECK) {
+            if ($this->_process->getPhase() < self::PHASE_ID_CHECK) {
                 $this->_msgList['error'][] = $this->l->t('Opération impossible : phase en cours "' . self::_getPhaseName($this->_process->getPhase()) . '".');
             }
 
@@ -1086,53 +1102,53 @@ class PageController extends Controller {
                                 // pour chaque champ CSV a controler
                                 foreach ($checkCurrentValues as $checkField => $checkCurrentValue) {
                                     // verifier d'abord aux emplacements previsibles du PDF
-                                    $matchList = array();
-                                    if ($checkField == 'siret') {
-                                        if (preg_match('#SIRET:\s(\d+)\s\d+$#', $pdfPageTextList[3], $matchList)) {
-                                            if ($matchList[1] == $checkCurrentValue) {
-                                                $pdfPageValues[$checkField] = $checkCurrentValue;
-                                                continue;
+                                    //if ($checkField == 'siret') {
+                                    //    // siret
+                                    //    if (isset($this->_payslipPDFSearchFieldList[$checkField]) && !empty($this->_payslipPDFSearchFieldList[$checkField])) {
+                                    //        foreach ($this->_payslipPDFSearchFieldList[$checkField] as $payslipPDFSearchFieldIndex) {
+                                    //            $matchList = array();
+                                    //            if (preg_match('#SIRET:\s(\d+)\s\d+$#', $pdfPageTextList[$payslipPDFSearchFieldIndex], $matchList)) {
+                                    //               if ($matchList[1] == $checkCurrentValue) {
+                                    //                    $pdfPageValues[$checkField] = $checkCurrentValue;
+                                    //                    continue;
+                                    //                }
+                                    //            }
+                                    //        }
+                                    //    }
+                                    //} else {
+                                        // siret, matricule, fullname, zip_city
+                                        if (isset($this->_payslipPDFSearchFieldList[$checkField]) && !empty($this->_payslipPDFSearchFieldList[$checkField])) {
+                                            foreach ($this->_payslipPDFSearchFieldList[$checkField] as $payslipPDFSearchFieldIndex) {
+                                                if ($pdfPageTextList[$payslipPDFSearchFieldIndex] == $checkCurrentValue) {
+                                                    $pdfPageValues[$checkField] = $checkCurrentValue;
+                                                    break;
+                                                }
                                             }
                                         }
-                                    } else if ($checkField == 'matricule') {
-                                        if ($pdfPageTextList[10] == $checkCurrentValue) {
-                                            $pdfPageValues[$checkField] = $checkCurrentValue;
-                                            continue;
-                                        }
-                                    } else if ($checkField == 'fullname') {
-                                        if ($pdfPageTextList[9] == $checkCurrentValue) {
-                                            $pdfPageValues[$checkField] = $checkCurrentValue;
-                                            continue;
-                                        }
-                                    } else if ($checkField == 'zip_city') {
-                                        if ($pdfPageTextList[19] == $checkCurrentValue) {
-                                            $pdfPageValues[$checkField] = $checkCurrentValue;
-                                            continue;
-                                        } else if ($pdfPageTextList[20] == $checkCurrentValue) {
-                                            $pdfPageValues[$checkField] = $checkCurrentValue;
-                                            continue;
-                                        }
-                                    }
-
-                                    // si on a pas trouve aux emplacement previsibles du PDF
-                                    // pour chaque champ texte de la page du PDF
-                                    foreach ($pdfPageTextList as $pdfPageTextKey => $pdfPageText) {
-                                        // formater les champs textes du PDF (enlever les espaces)
-                                        $pdfPageTextValue = trim($pdfPageText);
-
-                                        if ($checkField == 'siret') {
-                                            $matchList = array();
-                                            if (preg_match('#SIRET:\s(\d+)\s\d+$#', $pdfPageTextValue, $matchList)) {
-                                                if ($matchList[1] == $checkCurrentValue) {
+                                    //}
+
+                                    if (!isset($pdfPageValues[$checkField])) {
+                                        // si on a pas trouve aux emplacement previsibles du PDF
+                                        // pour chaque champ texte de la page du PDF
+                                        foreach ($pdfPageTextList as $pdfPageTextKey => $pdfPageText) {
+                                            // formater les champs textes du PDF (enlever les espaces)
+                                            $pdfPageTextValue = trim($pdfPageText);
+
+                                            //if ($checkField == 'siret') {
+                                            //    $matchList = array();
+                                            //    if (preg_match('#SIRET:\s(\d+)\s\d+$#', $pdfPageTextValue, $matchList)) {
+                                            //        if ($matchList[1] == $checkCurrentValue) {
+                                            //            $pdfPageValues[$checkField] = $checkCurrentValue;
+                                            //            break;
+                                            //        }
+                                            //    }
+                                            //} else {
+                                                // siret, matricule, fullname, zip_city
+                                                if ($pdfPageTextValue == $checkCurrentValue) {
                                                     $pdfPageValues[$checkField] = $checkCurrentValue;
                                                     break;
                                                 }
-                                            }
-                                        } else {
-                                            if ($pdfPageTextValue == $checkCurrentValue) {
-                                                $pdfPageValues[$checkField] = $checkCurrentValue;
-                                                break;
-                                            }
+                                            //}
                                         }
                                     }
                                 }
@@ -1343,7 +1359,7 @@ class PageController extends Controller {
             }
 
             $this->_processCheck();
-            if ($this->_process->getPhase() < self::PAHSE_ID_INDEXATION) {
+            if ($this->_process->getPhase() < self::PHASE_ID_INDEXATION) {
                 $this->_msgList['error'][] = $this->l->t('Opération impossible : phase en cours "' . self::_getPhaseName($this->_process->getPhase()) . '".');
             }
 
@@ -1770,7 +1786,7 @@ class PageController extends Controller {
                                 $phaseStepNum++;
                                 if ($phaseStepNum >= $retryPhaseStepNum) {
                                     // recuperation du fichier de log cree lors de la phase de verification
-                                    $logCheckFileName = self::PAHSE_ID_CHECK . '_log.csv';
+                                    $logCheckFileName = self::PHASE_ID_CHECK . '_log.csv';
                                     if (!empty($logCheckFileName)) {
                                         $this->_logCsvLine($this->l->t('Déplacement du fichier de Log créé après vérification dans le dossier du rapport.'), self::LOG_DEBUG);
                                         $logCheckFileUserPath = $this->_checkDirUserPath . DIRECTORY_SEPARATOR . $logCheckFileName;
-- 
GitLab