From 6383462cfe62330b89919259be95572437666aa7 Mon Sep 17 00:00:00 2001
From: lvessiller <lvessiller@open-dsi.fr>
Date: Wed, 30 Sep 2020 14:12:50 +0200
Subject: [PATCH] NEW add exception error on indexation process

---
 lib/Controller/PageController.php | 1030 +++++++++++++++--------------
 1 file changed, 518 insertions(+), 512 deletions(-)

diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php
index 336621e..c16d86b 100644
--- a/lib/Controller/PageController.php
+++ b/lib/Controller/PageController.php
@@ -1771,404 +1771,471 @@ class PageController extends Controller {
             }
 
             if (count($this->_msgList['error']) <= 0) {
-                $phaseStartDateTime = new DateTime('now', OC::$server->getDateTimeZone()->getTimeZone());
-                $phaseStartTime = $phaseStartDateTime->getTimestamp();
+                try {
+                    $phaseStartDateTime = new DateTime('now', OC::$server->getDateTimeZone()->getTimeZone());
+                    $phaseStartTime = $phaseStartDateTime->getTimestamp();
 
-                // log file
-                $logFilePath = $this->_reportDirUserPath . DIRECTORY_SEPARATOR . $phaseStartDateTime->format('Y-m-d_H-i-s') . '_' . $this->_process->getPhase() . '_log';
-                $this->_logFile = Filesystem::fopen($logFilePath, 'w+');
-                $this->_logCsvCols(array('date', 'heure', 'niveau', 'message', 'siret', 'matricule', 'nom_prenom', 'zip_city', 'date_debut', 'date_fin', 'email'));
-                $this->_logCsvLine('## ' . $this->l->t('Début du processus d\'indexation.') . ' ##', self::LOG_DEBUG);
+                    // log file
+                    $logFilePath = $this->_reportDirUserPath . DIRECTORY_SEPARATOR . $phaseStartDateTime->format('Y-m-d_H-i-s') . '_' . $this->_process->getPhase() . '_log';
+                    $this->_logFile = Filesystem::fopen($logFilePath, 'w+');
+                    $this->_logCsvCols(array('date', 'heure', 'niveau', 'message', 'siret', 'matricule', 'nom_prenom', 'zip_city', 'date_debut', 'date_fin', 'email'));
+                    $this->_logCsvLine('## ' . $this->l->t('Début du processus d\'indexation.') . ' ##', self::LOG_DEBUG);
 
-                $this->_process->setProcessing(1);
-                $this->_process->setPhase(2);
-                $this->_process->setPhaseStartTime($phaseStartTime);
-                if (!($retry > 0)) {
-                    $this->_process->setPhaseStepNum(0);
-                }
-                $this->_process->setMustTerminate(1);
-                $this->_process->setHasError(0);
-                $this->_process->setErrorCode(0);
-                $this->_process->setErrorMsg('');
-                $this->_process->setUserId($this->userId);
-                $this->_dematpayslipProcessMapper->update($this->_process);
-
-                $payslipsDirPath = $this->_dematpayslipConfigList['payslip_dir'];
-                $csvCheckFileName = $this->_process->getCsvCheck();
-
-                // recuperer et monter le dossier de l'utilisateur Nextcloud
-                $this->_logCsvLine($this->l->t('Récupération et montage du dossier de l\'utilisateur Nextcloud.'), self::LOG_DEBUG);
-                $userHomeFilesDirPath = '';
-                if ($this->_process->getHasError() <= 0) {
-                    $user = OC::$server->getUserManager()->get($this->userId);
-                    if (!$user) {
-                        $errorMsg = $this->l->t('Le dossier utilisateur "' . $this->userId . '" n\'existe pas.');
-                        $this->_logCsvLine($errorMsg, self::LOG_ERROR);
-                        $this->_processErrorMsg(-1, $errorMsg);
+                    $this->_process->setProcessing(1);
+                    $this->_process->setPhase(2);
+                    $this->_process->setPhaseStartTime($phaseStartTime);
+                    if (!($retry > 0)) {
+                        $this->_process->setPhaseStepNum(0);
                     }
+                    $this->_process->setMustTerminate(1);
+                    $this->_process->setHasError(0);
+                    $this->_process->setErrorCode(0);
+                    $this->_process->setErrorMsg('');
+                    $this->_process->setUserId($this->userId);
+                    $this->_dematpayslipProcessMapper->update($this->_process);
 
-                    $userHomeFilesDirPath = $user->getHome() . DIRECTORY_SEPARATOR . 'files';
-                }
-                if (empty($userHomeFilesDirPath) || !is_dir($userHomeFilesDirPath)) {
-                    $errorMsg = $this->l->t('Le dossier utilisateur "' . $this->userId . '" est introuvable.');
-                    $this->_logCsvLine($errorMsg, self::LOG_ERROR);
-                    $this->_processErrorMsg(-1, $errorMsg);
-                }
+                    $payslipsDirPath = $this->_dematpayslipConfigList['payslip_dir'];
+                    $csvCheckFileName = $this->_process->getCsvCheck();
 
-                // verifier l'existance du dossier des bulletins
-                $this->_logCsvLine($this->l->t('Vérification de l\'existance du dossier des bulletins.'), self::LOG_DEBUG);
-                if ($this->_process->getHasError() <= 0) {
-                    $isDir = Filesystem::is_dir($payslipsDirPath);
-                    if (!$isDir) {
-                        $errorMsg = $this->l->t('Le dossier des bulletins "' . $payslipsDirPath . '" n\'existe pas.');
-                        $this->_logCsvLine($errorMsg, self::LOG_ERROR);
-                        $this->_processErrorMsg(-1, $errorMsg);
-                    }
-                }
+                    // recuperer et monter le dossier de l'utilisateur Nextcloud
+                    $this->_logCsvLine($this->l->t('Récupération et montage du dossier de l\'utilisateur Nextcloud.'), self::LOG_DEBUG);
+                    $userHomeFilesDirPath = '';
+                    if ($this->_process->getHasError() <= 0) {
+                        $user = OC::$server->getUserManager()->get($this->userId);
+                        if (!$user) {
+                            $errorMsg = $this->l->t('Le dossier utilisateur "' . $this->userId . '" n\'existe pas.');
+                            $this->_logCsvLine($errorMsg, self::LOG_ERROR);
+                            $this->_processErrorMsg(-1, $errorMsg);
+                        }
 
-                // verifier l'existance du dossier des bulletins non indexes
-                $this->_logCsvLine($this->l->t('Vérification de l\'existance du dossier des bulletins non indexes.'), self::LOG_DEBUG);
-                if ($this->_process->getHasError() <= 0) {
-                    $isDir = Filesystem::is_dir($this->_payslipNoIndexDir);
-                    if (!$isDir) {
-                        $errorMsg = $this->l->t('Le dossier des bulletins non indexes "' . $this->_payslipNoIndexDir . '" n\'existe pas.');
+                        $userHomeFilesDirPath = $user->getHome() . DIRECTORY_SEPARATOR . 'files';
+                    }
+                    if (empty($userHomeFilesDirPath) || !is_dir($userHomeFilesDirPath)) {
+                        $errorMsg = $this->l->t('Le dossier utilisateur "' . $this->userId . '" est introuvable.');
                         $this->_logCsvLine($errorMsg, self::LOG_ERROR);
                         $this->_processErrorMsg(-1, $errorMsg);
                     }
-                }
 
-                if ($this->_process->getHasError() <= 0) {
-                    $phaseStep = 'csv_check';
-                    $csvCheckLineNum = 0;
-                    $retryCsvNumLine = 0;
-                    if ($retry > 0) {
-                        if ($this->_process->getPhaseStep() == $phaseStep) {
-                            $this->_process->setPhaseStep('');
+                    // verifier l'existance du dossier des bulletins
+                    $this->_logCsvLine($this->l->t('Vérification de l\'existance du dossier des bulletins.'), self::LOG_DEBUG);
+                    if ($this->_process->getHasError() <= 0) {
+                        $isDir = Filesystem::is_dir($payslipsDirPath);
+                        if (!$isDir) {
+                            $errorMsg = $this->l->t('Le dossier des bulletins "' . $payslipsDirPath . '" n\'existe pas.');
+                            $this->_logCsvLine($errorMsg, self::LOG_ERROR);
+                            $this->_processErrorMsg(-1, $errorMsg);
                         }
-                        $retryCsvNumLine = $this->_process->getPhaseStepNum();
-                        $this->_logCsvLine($this->l->t('Reprise à la ligne ' . ($retryCsvNumLine+1) . ' du fichier CSV créé lors de la phase de vérification.'), self::LOG_DEBUG);
                     }
-                    if (empty($this->_process->getPhaseStep())) {
-                        $rootFolder   = OC::$server->getRootFolder();
-                        $shareManager = OC::$server->getShareManager();
-                        $userManager  = OC::$server->getUserManager();
-                        $userFolder   = $rootFolder->getUserFolder($this->userId);
-
-                        // recuperer tous les partages realises par cet utilisateur
-                        $payslipShareNode = $userFolder->get($payslipsDirPath);
-                        $payslipShareList = $shareManager->getSharesInFolder($this->userId, $payslipShareNode);
-                        $payslipMatriculeShareNodeIdList = array();
-                        if (count($payslipShareList) > 0) {
-                            foreach ($payslipShareList as $payslipShareNodeId => $payslipShareArr) {
-                                $payslipMatriculeShareNodeIdList[] = $payslipShareNodeId;
-                            }
-                        }
 
-                        // ouverture du fichier CSV cree lors de l'etape de verification
-                        $this->_logCsvLine($this->l->t('Ouverture du fichier CSV créé lors de l\'étape de vérification.'), self::LOG_DEBUG);
-                        $csvCheckFilePath = $this->_checkDirFullPath . DIRECTORY_SEPARATOR . $csvCheckFileName;
-                        $csvCheckFile = fopen($csvCheckFilePath, 'r');
-                        if (!$csvCheckFile) {
-                            $errorMsg = $this->l->t('Impossible d\'ouvrir le fichier CSV "' . $csvCheckFileName . '".');
+                    // verifier l'existance du dossier des bulletins non indexes
+                    $this->_logCsvLine($this->l->t('Vérification de l\'existance du dossier des bulletins non indexes.'), self::LOG_DEBUG);
+                    if ($this->_process->getHasError() <= 0) {
+                        $isDir = Filesystem::is_dir($this->_payslipNoIndexDir);
+                        if (!$isDir) {
+                            $errorMsg = $this->l->t('Le dossier des bulletins non indexes "' . $this->_payslipNoIndexDir . '" n\'existe pas.');
                             $this->_logCsvLine($errorMsg, self::LOG_ERROR);
-                            $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
+                            $this->_processErrorMsg(-1, $errorMsg);
                         }
+                    }
 
-                        if ($this->_process->getHasError() <= 0) {
-                            $this->_logCsvLine($this->l->t('Lecture du fichier PDF.'), self::LOG_DEBUG);
-                            $pdfOriginFileFullPath = $this->_uploadDirFullPath . DIRECTORY_SEPARATOR . $this->_process->getPdfOrigin();
-
-                            // creer le PDF des bulletins de paie a ne pas indexer (ne pas deposer dans le dossier partage de l'agent)
-                            $pdfPayslipSkip = null;
-                            $payslipSkipPDFPath = $this->_payslipNoIndexDir . DIRECTORY_SEPARATOR .  $phaseStartDateTime->format('Y-m-d_H-i-s') . '_skip';
-
-                            // identifier les lignes du fichier CSV a ne pas indexer
-                            $dematpayslipProcessSkipLineIdList = array();
-                            $dematpayslipProcessSkipList = $this->_dematpayslipProcessSkipMapper->findAllByIdProcess($this->_process->getId());
-                            foreach ($dematpayslipProcessSkipList as $dematpayslipProcessSkip) {
-                                $dematpayslipProcessSkipLineIdList[] = $dematpayslipProcessSkip->getIdLine();
+                    if ($this->_process->getHasError() <= 0) {
+                        $phaseStep = 'csv_check';
+                        $csvCheckLineNum = 0;
+                        $retryCsvNumLine = 0;
+                        if ($retry > 0) {
+                            if ($this->_process->getPhaseStep() == $phaseStep) {
+                                $this->_process->setPhaseStep('');
+                            }
+                            $retryCsvNumLine = $this->_process->getPhaseStepNum();
+                            $this->_logCsvLine($this->l->t('Reprise à la ligne ' . ($retryCsvNumLine + 1) . ' du fichier CSV créé lors de la phase de vérification.'), self::LOG_DEBUG);
+                        }
+                        if (empty($this->_process->getPhaseStep())) {
+                            $rootFolder = OC::$server->getRootFolder();
+                            $shareManager = OC::$server->getShareManager();
+                            $userManager = OC::$server->getUserManager();
+                            $userFolder = $rootFolder->getUserFolder($this->userId);
+
+                            // recuperer tous les partages realises par cet utilisateur
+                            $this->_logCsvLine($this->l->t('Récupération des partages de "' . $payslipsDirPath . '".'), self::LOG_DEBUG);
+                            $payslipShareNode = $userFolder->get($payslipsDirPath);
+                            $payslipShareList = $shareManager->getSharesInFolder($this->userId, $payslipShareNode);
+                            $payslipMatriculeShareNodeIdList = array();
+                            if (count($payslipShareList) > 0) {
+                                foreach ($payslipShareList as $payslipShareNodeId => $payslipShareArr) {
+                                    $payslipMatriculeShareNodeIdList[] = $payslipShareNodeId;
+                                }
                             }
 
-                            // payslip archive mapper
-                            $dematpayslipArchiveMapper = new DematpayslipArchiveMapper(OC::$server->getDatabaseConnection());
+                            // ouverture du fichier CSV cree lors de l'etape de verification
+                            $this->_logCsvLine($this->l->t('Ouverture du fichier CSV créé lors de l\'étape de vérification.'), self::LOG_DEBUG);
+                            $csvCheckFilePath = $this->_checkDirFullPath . DIRECTORY_SEPARATOR . $csvCheckFileName;
+                            $csvCheckFile = fopen($csvCheckFilePath, 'r');
+                            if (!$csvCheckFile) {
+                                $errorMsg = $this->l->t('Impossible d\'ouvrir le fichier CSV "' . $csvCheckFileName . '".');
+                                $this->_logCsvLine($errorMsg, self::LOG_ERROR);
+                                $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
+                            }
 
-                            $this->_logCsvLine($this->l->t('Lecture des lignes du fichier CSV créé lors de l\'étape de vérification.'), self::LOG_DEBUG);
-                            while ($csvCheckLine = fgetcsv($csvCheckFile, $this->_csvLength, $this->_csvDelimiter, $this->_csvEnclosure)) {
-                                $this->_logCsvLine('-- ' . $this->l->t('Début de ligne CSV : ' . ($csvCheckLineNum + 1)) . ' --', self::LOG_DEBUG);
-
-                                if ($csvCheckLineNum >= $retryCsvNumLine) {
-                                    //$csvCheckArr[] = array(
-                                    //    'siret'         => $csvCheckLine[0],
-                                    //    'matricule_ori' => $csvCheckLine[1],
-                                    //    'lastname'      => $csvCheckLine[2],
-                                    //    'firstname'     => $csvCheckLine[3],
-                                    //    'zip_city'      => $csvCheckLine[4],
-                                    //    'start_date'    => $csvCheckLine[5],
-                                    //    'end_date'      => $csvCheckLine[6],
-                                    //    'edition_date'  => $csvCheckLine[7],
-                                    //    'matricule'     => $csvCheckLine[8],
-                                    //    'fullname'      => $csvCheckLine[9],
-                                    //    'email'         => $csvCheckLine[10],
-                                    //    'line_num'      => $csvCheckLine[11],
-                                    //    'pdf_pages'     => $csvCheckLine[12],
-                                    //);
-
-                                    // get CSV data values
-                                    $startDate      = $csvCheckLine[5];
-                                    $endDate        = $csvCheckLine[6];
-                                    $editionDate    = $csvCheckLine[7];
-                                    $startDateTime  = strtotime($startDate);
-                                    $year           = date('Y', $startDateTime);
-                                    $month          = strtolower(strftime('%B', $startDateTime));
-                                    $pdfPageNumList = explode('-', $csvCheckLine[12]);
-                                    $period         = $startDate . '_' . $endDate;
-
-                                    // agent user
-                                    $agentUser = new DematpayslipUser();
-                                    $agentUser->setId(0);
-                                    $agentUser->setUserId('');
-                                    $agentUser->setSiret($csvCheckLine[0]);
-                                    $agentUser->setMatricule($csvCheckLine[8]);
-                                    $agentUser->setLastname($csvCheckLine[2]);
-                                    $agentUser->setFirstname($csvCheckLine[3]);
-                                    $agentUser->setEmail($csvCheckLine[10]);
-                                    $agentUser->setZipCity($csvCheckLine[4]);
-
-                                    // pdf check file path
-                                    $pdfPayslipCheckFileName = ($csvCheckLineNum + 1) . '_' . $agentUser->getMatricule() . '.pdf';
-                                    $pdfPayslipCheckFilePath = $this->_checkDirUserPath . DIRECTORY_SEPARATOR . $pdfPayslipCheckFileName;
-                                    $pdfPayslipFilePath = '';
-
-                                    $this->_logCsvLine($this->l->t('Ligne CSV '), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
-
-                                    // process skip lines
-                                    if (in_array($csvCheckLineNum, $dematpayslipProcessSkipLineIdList)) {
-                                        $this->_logCsvLine('-- ' . $this->l->t('Saut de la ligne CSV : ' . ($csvCheckLineNum + 1)) . ' --', self::LOG_DEBUG, $agentUser, $startDate, $endDate);
-
-                                        if ($launchIndexation == 1) {
-                                            // creer le PDF des bulletins de paie a ne pas indexer
-                                            if ($pdfPayslipSkip === null) {
-                                                $pdfPayslipSkip = new \setasign\Fpdi\Fpdi();
-                                            }
-                                            // ajout des pages des bulletins de paie de l'agent a ne pas indexer
-                                            $this->_logCsvLine($this->l->t('Ajout des pages PDF au bulletin de paie à ne pas indexer.'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
-                                            foreach ($pdfPageNumList as $pdfPageNum) {
-                                                $this->_logCsvLine($this->l->t('Ajout de la page ' . $pdfPageNum . '.'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
-                                                $pdfPayslipSkip->AddPage();
-                                                $pdfPayslipSkip->setSourceFile($pdfOriginFileFullPath);
-                                                $tplPayslipSkipId = $pdfPayslipSkip->importPage($pdfPageNum);
-                                                $pdfPayslipSkip->useTemplate($tplPayslipSkipId);
-                                            }
-                                        }
+                            if ($this->_process->getHasError() <= 0) {
+                                $this->_logCsvLine($this->l->t('Lecture du fichier PDF.'), self::LOG_DEBUG);
+                                $pdfOriginFileFullPath = $this->_uploadDirFullPath . DIRECTORY_SEPARATOR . $this->_process->getPdfOrigin();
+
+                                // creer le PDF des bulletins de paie a ne pas indexer (ne pas deposer dans le dossier partage de l'agent)
+                                $pdfPayslipSkip = null;
+                                $payslipSkipPDFPath = $this->_payslipNoIndexDir . DIRECTORY_SEPARATOR . $phaseStartDateTime->format('Y-m-d_H-i-s') . '_skip';
+
+                                // identifier les lignes du fichier CSV a ne pas indexer
+                                $dematpayslipProcessSkipLineIdList = array();
+                                $dematpayslipProcessSkipList = $this->_dematpayslipProcessSkipMapper->findAllByIdProcess($this->_process->getId());
+                                foreach ($dematpayslipProcessSkipList as $dematpayslipProcessSkip) {
+                                    $dematpayslipProcessSkipLineIdList[] = $dematpayslipProcessSkip->getIdLine();
+                                }
 
-                                        $csvCheckLineNum++;
-                                        continue;
-                                    }
+                                // payslip archive mapper
+                                $dematpayslipArchiveMapper = new DematpayslipArchiveMapper(OC::$server->getDatabaseConnection());
+
+                                $this->_logCsvLine($this->l->t('Lecture des lignes du fichier CSV créé lors de l\'étape de vérification.'), self::LOG_DEBUG);
+                                while ($csvCheckLine = fgetcsv($csvCheckFile, $this->_csvLength, $this->_csvDelimiter, $this->_csvEnclosure)) {
+                                    $this->_logCsvLine('-- ' . $this->l->t('Début de ligne CSV : ' . ($csvCheckLineNum + 1)) . ' --', self::LOG_DEBUG);
+
+                                    if ($csvCheckLineNum >= $retryCsvNumLine) {
+                                        //$csvCheckArr[] = array(
+                                        //    'siret'         => $csvCheckLine[0],
+                                        //    'matricule_ori' => $csvCheckLine[1],
+                                        //    'lastname'      => $csvCheckLine[2],
+                                        //    'firstname'     => $csvCheckLine[3],
+                                        //    'zip_city'      => $csvCheckLine[4],
+                                        //    'start_date'    => $csvCheckLine[5],
+                                        //    'end_date'      => $csvCheckLine[6],
+                                        //    'edition_date'  => $csvCheckLine[7],
+                                        //    'matricule'     => $csvCheckLine[8],
+                                        //    'fullname'      => $csvCheckLine[9],
+                                        //    'email'         => $csvCheckLine[10],
+                                        //    'line_num'      => $csvCheckLine[11],
+                                        //    'pdf_pages'     => $csvCheckLine[12],
+                                        //);
+
+                                        // get CSV data values
+                                        $startDate = $csvCheckLine[5];
+                                        $endDate = $csvCheckLine[6];
+                                        $editionDate = $csvCheckLine[7];
+                                        $startDateTime = strtotime($startDate);
+                                        $year = date('Y', $startDateTime);
+                                        $month = strtolower(strftime('%B', $startDateTime));
+                                        $pdfPageNumList = explode('-', $csvCheckLine[12]);
+                                        $period = $startDate . '_' . $endDate;
+
+                                        // agent user
+                                        $agentUser = new DematpayslipUser();
+                                        $agentUser->setId(0);
+                                        $agentUser->setUserId('');
+                                        $agentUser->setSiret($csvCheckLine[0]);
+                                        $agentUser->setMatricule($csvCheckLine[8]);
+                                        $agentUser->setLastname($csvCheckLine[2]);
+                                        $agentUser->setFirstname($csvCheckLine[3]);
+                                        $agentUser->setEmail($csvCheckLine[10]);
+                                        $agentUser->setZipCity($csvCheckLine[4]);
+
+                                        // pdf check file path
+                                        $pdfPayslipCheckFileName = ($csvCheckLineNum + 1) . '_' . $agentUser->getMatricule() . '.pdf';
+                                        $pdfPayslipCheckFilePath = $this->_checkDirUserPath . DIRECTORY_SEPARATOR . $pdfPayslipCheckFileName;
+                                        $pdfPayslipFilePath = '';
+
+                                        $this->_logCsvLine($this->l->t('Ligne CSV '), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+
+                                        // process skip lines
+                                        if (in_array($csvCheckLineNum, $dematpayslipProcessSkipLineIdList)) {
+                                            $this->_logCsvLine('-- ' . $this->l->t('Saut de la ligne CSV : ' . ($csvCheckLineNum + 1)) . ' --', self::LOG_DEBUG, $agentUser, $startDate, $endDate);
 
-                                    // no user but archiving for 5 years "BulletinsNoIndex/HORODATE/SIRET-MATRICULE/PERIODE.pdf"
-                                    if (empty($agentUser->getEmail())) {
-                                        $this->_logCsvLine('-- ' . $this->l->t('Création du PDF non dématérialisé de l\'agent (aucun email) : ' . ($csvCheckLineNum + 1)) . ' --', self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+                                            if ($launchIndexation == 1) {
+                                                // creer le PDF des bulletins de paie a ne pas indexer
+                                                if ($pdfPayslipSkip === null) {
+                                                    $pdfPayslipSkip = new \setasign\Fpdi\Fpdi();
+                                                }
+                                                // ajout des pages des bulletins de paie de l'agent a ne pas indexer
+                                                $this->_logCsvLine($this->l->t('Ajout des pages PDF au bulletin de paie à ne pas indexer.'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+                                                foreach ($pdfPageNumList as $pdfPageNum) {
+                                                    $this->_logCsvLine($this->l->t('Ajout de la page ' . $pdfPageNum . '.'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+                                                    $pdfPayslipSkip->AddPage();
+                                                    $pdfPayslipSkip->setSourceFile($pdfOriginFileFullPath);
+                                                    $tplPayslipSkipId = $pdfPayslipSkip->importPage($pdfPageNum);
+                                                    $pdfPayslipSkip->useTemplate($tplPayslipSkipId);
+                                                }
+                                            }
 
-                                        // check if not already archived (return null if already archived with warning)
-                                        $dematpayslipArchive = null;
-                                        if ($launchArchive == 1) {
-                                            $dematpayslipArchive = $this->_archiveCheckBeforeSave($dematpayslipArchiveMapper, $agentUser, $startDate, $endDate);
+                                            $csvCheckLineNum++;
+                                            continue;
                                         }
 
-                                        if ($launchArchive != 1 || ($launchArchive==1 && !empty($dematpayslipArchive))) {
-                                            // add all agent pdf pages
-                                            $pdfPayslipNoDemat = new \setasign\Fpdi\Fpdi();
-                                            $this->_logCsvLine($this->l->t('Ajout des pages PDF au bulletin de paie à ne pas indexer.'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
-                                            foreach ($pdfPageNumList as $pdfPageNum) {
-                                                $this->_logCsvLine($this->l->t('Ajout de la page ' . $pdfPageNum . '.'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
-                                                $pdfPayslipNoDemat->AddPage();
-                                                $pdfPayslipNoDemat->setSourceFile($pdfOriginFileFullPath);
-                                                $pdfPayslipNoDematId = $pdfPayslipNoDemat->importPage($pdfPageNum);
-                                                $pdfPayslipNoDemat->useTemplate($pdfPayslipNoDematId);
+                                        // no user but archiving for 5 years "BulletinsNoIndex/HORODATE/SIRET-MATRICULE/PERIODE.pdf"
+                                        if (empty($agentUser->getEmail())) {
+                                            $this->_logCsvLine('-- ' . $this->l->t('Création du PDF non dématérialisé de l\'agent (aucun email) : ' . ($csvCheckLineNum + 1)) . ' --', self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+
+                                            // check if not already archived (return null if already archived with warning)
+                                            $dematpayslipArchive = null;
+                                            if ($launchArchive == 1) {
+                                                $dematpayslipArchive = $this->_archiveCheckBeforeSave($dematpayslipArchiveMapper, $agentUser, $startDate, $endDate);
                                             }
 
-                                            if ($launchIndexation == 1) {
-                                                // create PDF payslip
-                                                $payslipNoDematHorodateDirPath = $this->_payslipNoIndexDir . DIRECTORY_SEPARATOR . $phaseStartDateTime->format('Y-m-d_H-i-s');
-                                                $isDir = Filesystem::is_dir($payslipNoDematHorodateDirPath);
-                                                if (!$isDir) {
-                                                    $dirCreated = Filesystem::mkdir($payslipNoDematHorodateDirPath);
-                                                    if (!$dirCreated) {
-                                                        $errorMsg = $this->l->t('Impossible de créeer le dossier "' . $payslipNoDematHorodateDirPath . '".');
-                                                        $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
+                                            if ($launchArchive != 1 || ($launchArchive == 1 && !empty($dematpayslipArchive))) {
+                                                // add all agent pdf pages
+                                                $pdfPayslipNoDemat = new \setasign\Fpdi\Fpdi();
+                                                $this->_logCsvLine($this->l->t('Ajout des pages PDF au bulletin de paie à ne pas indexer.'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+                                                foreach ($pdfPageNumList as $pdfPageNum) {
+                                                    $this->_logCsvLine($this->l->t('Ajout de la page ' . $pdfPageNum . '.'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+                                                    $pdfPayslipNoDemat->AddPage();
+                                                    $pdfPayslipNoDemat->setSourceFile($pdfOriginFileFullPath);
+                                                    $pdfPayslipNoDematId = $pdfPayslipNoDemat->importPage($pdfPageNum);
+                                                    $pdfPayslipNoDemat->useTemplate($pdfPayslipNoDematId);
+                                                }
+
+                                                if ($launchIndexation == 1) {
+                                                    // create PDF payslip
+                                                    $payslipNoDematHorodateDirPath = $this->_payslipNoIndexDir . DIRECTORY_SEPARATOR . $phaseStartDateTime->format('Y-m-d_H-i-s');
+                                                    $isDir = Filesystem::is_dir($payslipNoDematHorodateDirPath);
+                                                    if (!$isDir) {
+                                                        $dirCreated = Filesystem::mkdir($payslipNoDematHorodateDirPath);
+                                                        if (!$dirCreated) {
+                                                            $errorMsg = $this->l->t('Impossible de créeer le dossier "' . $payslipNoDematHorodateDirPath . '".');
+                                                            $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
+                                                            $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
+                                                        }
+                                                    }
+                                                    $payslipNoDematHorodateAgentDirPath = $payslipNoDematHorodateDirPath . DIRECTORY_SEPARATOR . $agentUser->getSiret() . '-' . $agentUser->getMatricule();
+                                                    $isDir = Filesystem::is_dir($payslipNoDematHorodateAgentDirPath);
+                                                    if (!$isDir) {
+                                                        $dirCreated = Filesystem::mkdir($payslipNoDematHorodateAgentDirPath);
+                                                        if (!$dirCreated) {
+                                                            $errorMsg = $this->l->t('Impossible de créeer le dossier "' . $payslipNoDematHorodateAgentDirPath . '".');
+                                                            $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
+                                                            $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
+                                                        }
+                                                    }
+                                                    $payslipNoDematPDFPath = $payslipNoDematHorodateAgentDirPath . DIRECTORY_SEPARATOR . $startDate . '_' . $endDate;
+
+                                                    // create PDF payslip in no index folder
+                                                    $this->_logCsvLine($this->l->t('Créer le PDF du bulletin de paie non dematérialisé.'), self::LOG_DEBUG);
+                                                    $pdfPayslipNoDemat->Output('F', $userHomeFilesDirPath . DIRECTORY_SEPARATOR . $payslipNoDematPDFPath);
+                                                    $pdfPayslipFilePath = $payslipNoDematPDFPath . '.pdf';
+                                                    $renamed = Filesystem::rename($payslipNoDematPDFPath, $pdfPayslipFilePath);
+                                                    if (!$renamed) {
+                                                        $errorMsg = $this->l->t('Impossible de déplacer le PDF du bulletin de paie non dematérialisé "' . $payslipNoDematPDFPath . '".');
+                                                        $this->_logCsvLine($errorMsg, self::LOG_ERROR);
                                                         $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
                                                     }
                                                 }
-                                                $payslipNoDematHorodateAgentDirPath = $payslipNoDematHorodateDirPath . DIRECTORY_SEPARATOR . $agentUser->getSiret() . '-' . $agentUser->getMatricule();
-                                                $isDir = Filesystem::is_dir($payslipNoDematHorodateAgentDirPath);
-                                                if (!$isDir) {
-                                                    $dirCreated = Filesystem::mkdir($payslipNoDematHorodateAgentDirPath);
-                                                    if (!$dirCreated) {
-                                                        $errorMsg = $this->l->t('Impossible de créeer le dossier "' . $payslipNoDematHorodateAgentDirPath . '".');
+
+                                                // save archive and process
+                                                if ($launchArchive == 1) {
+                                                    $result = $this->_archiveSaveAndProcess($pdfPayslipCheckFilePath, $dematpayslipArchiveMapper, $dematpayslipArchive, $pdfPayslipFilePath, $agentUser, $startDate, $endDate, $year, $month, $editionDate, $phaseStartDateTime->getTimestamp());
+                                                    if ($result < 0) {
+                                                        $errorMsg = $this->l->t('Impossible de créer le fichier temporaire du bulletin de paie "' . $pdfPayslipCheckFilePath . '".');
                                                         $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
                                                         $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
                                                     }
                                                 }
-                                                $payslipNoDematPDFPath = $payslipNoDematHorodateAgentDirPath . DIRECTORY_SEPARATOR . $startDate . '_' . $endDate;
-
-                                                // create PDF payslip in no index folder
-                                                $this->_logCsvLine($this->l->t('Créer le PDF du bulletin de paie non dematérialisé.'), self::LOG_DEBUG);
-                                                $pdfPayslipNoDemat->Output('F', $userHomeFilesDirPath . DIRECTORY_SEPARATOR . $payslipNoDematPDFPath);
-                                                $pdfPayslipFilePath = $payslipNoDematPDFPath . '.pdf';
-                                                $renamed = Filesystem::rename($payslipNoDematPDFPath, $pdfPayslipFilePath);
-                                                if (!$renamed) {
-                                                    $errorMsg = $this->l->t('Impossible de déplacer le PDF du bulletin de paie non dematérialisé "' . $payslipNoDematPDFPath . '".');
-                                                    $this->_logCsvLine($errorMsg, self::LOG_ERROR);
-                                                    $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
-                                                }
                                             }
 
-                                            // save archive and process
-                                            if ($launchArchive == 1) {
-                                                $result = $this->_archiveSaveAndProcess($pdfPayslipCheckFilePath, $dematpayslipArchiveMapper, $dematpayslipArchive , $pdfPayslipFilePath, $agentUser, $startDate, $endDate, $year, $month, $editionDate, $phaseStartDateTime->getTimestamp());
-                                                if ($result < 0) {
-                                                    $errorMsg = $this->l->t('Impossible de créer le fichier temporaire du bulletin de paie "' . $pdfPayslipCheckFilePath . '".');
-                                                    $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
-                                                    $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
-                                                }
-                                            }
+                                            $csvCheckLineNum++;
+                                            continue;
                                         }
 
-                                        $csvCheckLineNum++;
-                                        continue;
-                                    }
-
-                                    // get agent from email
-                                    $this->_logCsvLine($this->l->t('Récupération de l\'utilisateur Nextcloud à partir de son email [' . $agentUser->getEmail() . '].'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
-                                    $userFindByEmailList = $userManager->getByEmail($agentUser->getEmail());
-
-                                    // /!\ remove all users not available (FIX for multiple users with SAML)
-                                    if (self::USER_REMOVE_UNKNOWN == 1) {
-                                        $userFindByEmailList = self::_removeUnknownUser($userFindByEmailList);
-                                    }
+                                        // get agent from email
+                                        $this->_logCsvLine($this->l->t('Récupération de l\'utilisateur Nextcloud à partir de son email [' . $agentUser->getEmail() . '].'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+                                        $userFindByEmailList = $userManager->getByEmail($agentUser->getEmail());
 
-                                    if (count($userFindByEmailList) != 1) {
-                                        $errorMsg = $this->l->t('Impossible de trouver l\'unique utilisateur Nextcloud dont l\'email est "' . $agentUser->getEmail() . '" [matricule="' . $agentUser->getMatricule() . '"].');
-                                        $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
-                                        $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
-                                    }
-                                    $userFindByEmail = current($userFindByEmailList);
-                                    $userFindByEmailId = $userFindByEmail->getUID();
-
-                                    if ($this->_process->getHasError() <= 0) {
-                                        // dossier "SIRET-MATRICULE"
-                                        $siretMatriculeDirName = $agentUser->getSiret() . '-' . $agentUser->getMatricule();
-                                        $this->_logCsvLine($this->l->t('Vérification l\'existance du dossier matricule [' . $siretMatriculeDirName . '].'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
-                                        $payslipsMatriculeDirPath = $payslipsDirPath . DIRECTORY_SEPARATOR . $siretMatriculeDirName;
-                                        $isDir = Filesystem::is_dir($payslipsMatriculeDirPath);
-                                        if (!$isDir) {
-                                            $this->_logCsvLine($this->l->t('Création du dossier matricule [' . $siretMatriculeDirName . '].'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
-                                            $dirCreated = Filesystem::mkdir($payslipsMatriculeDirPath);
-                                            if (!$dirCreated) {
-                                                $errorMsg = $this->l->t('Impossible de créer le dossier "' . $payslipsMatriculeDirPath . '" [matricule="' . $agentUser->getMatricule() . '"].');
-                                                $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
-                                                $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
-                                            }
+                                        // /!\ remove all users not available (FIX for multiple users with SAML)
+                                        if (self::USER_REMOVE_UNKNOWN == 1) {
+                                            $userFindByEmailList = self::_removeUnknownUser($userFindByEmailList);
+                                        }
 
-                                            // inserer l'utilisateur de la fiche de paie (association avec son siret, matricule et email)
-                                            $this->_logCsvLine($this->l->t('Insertion de l\'utilisateur de la fiche de paie : association dossier [' . $siretMatriculeDirName . '] avec email [' . $agentUser->getEmail() . '].'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
-                                            $dematpayslipUserList = $this->_dematpayslipUserMapper->findAllBySiretAndMatricule($agentUser->getSiret(), $agentUser->getMatricule());
-                                            if (empty($dematpayslipUserList)) {
-                                                $dematpayslipUser = new DematpayslipUser();
-                                                $dematpayslipUser->setUserId($userFindByEmailId);
-                                                $dematpayslipUser->setSiret($agentUser->getSiret());
-                                                $dematpayslipUser->setMatricule($agentUser->getMatricule());
-                                                $dematpayslipUser->setEmail($agentUser->getEmail());
-                                                $dematpayslipUser->setLastname($agentUser->getLastname());
-                                                $dematpayslipUser->setFirstname($agentUser->getFirstname());
-                                                $dematpayslipUser->setZipCity($agentUser->getZipCity());
-                                                $this->_dematpayslipUserMapper->insert($dematpayslipUser);
-                                            } else {
-                                                $errorMsg = $this->l->t('L\'utilisateur "' . $userFindByEmailId . '" est déjà associé à un dossier SIRET-MATRICULE [siret="' . $agentUser->getSiret() . '", matricule="' . $agentUser->getMatricule() . '", email="' . $agentUser->getEmail() . '"].');
-                                                $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
-                                                $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
-                                            }
+                                        if (count($userFindByEmailList) != 1) {
+                                            $errorMsg = $this->l->t('Impossible de trouver l\'unique utilisateur Nextcloud dont l\'email est "' . $agentUser->getEmail() . '" [matricule="' . $agentUser->getMatricule() . '"].');
+                                            $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
+                                            $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
                                         }
+                                        $userFindByEmail = current($userFindByEmailList);
+                                        $userFindByEmailId = $userFindByEmail->getUID();
 
                                         if ($this->_process->getHasError() <= 0) {
-                                            // recuperer l'utilisateur associe au dossier "SIRET-MATRICULE" cree
-                                            $this->_logCsvLine($this->l->t('Récupération de l\'utilisateur associé au dossier SIRET-MATRICULE [' . $siretMatriculeDirName . '].'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
-                                            $dematpayslipUserList = $this->_dematpayslipUserMapper->findAllBySiretAndMatricule($agentUser->getSiret(), $agentUser->getMatricule());
-                                            if (count($dematpayslipUserList) === 1) {
-                                                $dematpayslipUser = $dematpayslipUserList[0];
-
-                                                // update user for new version
-                                                if (empty($dematpayslipUser->getLastname()) || empty($dematpayslipUser->getFirstname()) || empty($dematpayslipUser->getZipCity())) {
+                                            // dossier "SIRET-MATRICULE"
+                                            $siretMatriculeDirName = $agentUser->getSiret() . '-' . $agentUser->getMatricule();
+                                            $this->_logCsvLine($this->l->t('Vérification l\'existance du dossier matricule [' . $siretMatriculeDirName . '].'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+                                            $payslipsMatriculeDirPath = $payslipsDirPath . DIRECTORY_SEPARATOR . $siretMatriculeDirName;
+                                            $isDir = Filesystem::is_dir($payslipsMatriculeDirPath);
+                                            if (!$isDir) {
+                                                $this->_logCsvLine($this->l->t('Création du dossier matricule [' . $siretMatriculeDirName . '].'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+                                                $dirCreated = Filesystem::mkdir($payslipsMatriculeDirPath);
+                                                if (!$dirCreated) {
+                                                    $errorMsg = $this->l->t('Impossible de créer le dossier "' . $payslipsMatriculeDirPath . '" [matricule="' . $agentUser->getMatricule() . '"].');
+                                                    $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
+                                                    $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
+                                                }
+
+                                                // inserer l'utilisateur de la fiche de paie (association avec son siret, matricule et email)
+                                                $this->_logCsvLine($this->l->t('Insertion de l\'utilisateur de la fiche de paie : association dossier [' . $siretMatriculeDirName . '] avec email [' . $agentUser->getEmail() . '].'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+                                                $dematpayslipUserList = $this->_dematpayslipUserMapper->findAllBySiretAndMatricule($agentUser->getSiret(), $agentUser->getMatricule());
+                                                if (empty($dematpayslipUserList)) {
+                                                    $dematpayslipUser = new DematpayslipUser();
+                                                    $dematpayslipUser->setUserId($userFindByEmailId);
+                                                    $dematpayslipUser->setSiret($agentUser->getSiret());
+                                                    $dematpayslipUser->setMatricule($agentUser->getMatricule());
+                                                    $dematpayslipUser->setEmail($agentUser->getEmail());
                                                     $dematpayslipUser->setLastname($agentUser->getLastname());
                                                     $dematpayslipUser->setFirstname($agentUser->getFirstname());
                                                     $dematpayslipUser->setZipCity($agentUser->getZipCity());
-                                                    $this->_dematpayslipUserMapper->update($dematpayslipUser);
+                                                    $this->_dematpayslipUserMapper->insert($dematpayslipUser);
+                                                } else {
+                                                    $errorMsg = $this->l->t('L\'utilisateur "' . $userFindByEmailId . '" est déjà associé à un dossier SIRET-MATRICULE [siret="' . $agentUser->getSiret() . '", matricule="' . $agentUser->getMatricule() . '", email="' . $agentUser->getEmail() . '"].');
+                                                    $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
+                                                    $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
                                                 }
+                                            }
+
+                                            if ($this->_process->getHasError() <= 0) {
+                                                // recuperer l'utilisateur associe au dossier "SIRET-MATRICULE" cree
+                                                $this->_logCsvLine($this->l->t('Récupération de l\'utilisateur associé au dossier SIRET-MATRICULE [' . $siretMatriculeDirName . '].'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+                                                $dematpayslipUserList = $this->_dematpayslipUserMapper->findAllBySiretAndMatricule($agentUser->getSiret(), $agentUser->getMatricule());
+                                                if (count($dematpayslipUserList) === 1) {
+                                                    $dematpayslipUser = $dematpayslipUserList[0];
+
+                                                    // update user for new version
+                                                    if (empty($dematpayslipUser->getLastname()) || empty($dematpayslipUser->getFirstname()) || empty($dematpayslipUser->getZipCity())) {
+                                                        $dematpayslipUser->setLastname($agentUser->getLastname());
+                                                        $dematpayslipUser->setFirstname($agentUser->getFirstname());
+                                                        $dematpayslipUser->setZipCity($agentUser->getZipCity());
+                                                        $this->_dematpayslipUserMapper->update($dematpayslipUser);
+                                                    }
+
+                                                    // set agent user id
+                                                    $agentUser->setId($dematpayslipUser->getId());
+                                                    $agentUser->setUserId($dematpayslipUser->getUserId());
 
-                                                // set agent user id
-                                                $agentUser->setId($dematpayslipUser->getId());
-                                                $agentUser->setUserId($dematpayslipUser->getUserId());
-
-                                                // si l'utilisateur n'a pas desactive la dematerialisation de son bulletin de paie
-                                                if ($dematpayslipUser->getDisabled() != 1) {
-                                                    $payslipsMatriculeYearDirPath = $payslipsMatriculeDirPath . DIRECTORY_SEPARATOR . $year;
-
-                                                    if ($launchIndexation == 1) {
-                                                        // creer le partage du dossier "SIRET-MATRICULE"
-                                                        $payslipsMatriculeDirPathNode = $userFolder->get($payslipsMatriculeDirPath);
-
-                                                        // verifier si le dossier n'est pas deja partage
-                                                        if (!in_array($payslipsMatriculeDirPathNode->getId(), $payslipMatriculeShareNodeIdList)) {
-                                                            // partager le dossier MATRICULE en lecture seule avec l'utilisateur trouve a partir de son email
-                                                            $this->_logCsvLine($this->l->t('Partage du dossier SIRET-MATRICULE [' . $siretMatriculeDirName . '] en lecture seule.'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
-                                                            $payslipMatriculeShare = $shareManager->newShare();
-                                                            $payslipMatriculeShare->setNode($payslipsMatriculeDirPathNode);
-                                                            $payslipMatriculeShare->setShareType(\OC\Share\Share::SHARE_TYPE_USER);
-                                                            $payslipMatriculeShare->setSharedWith($userFindByEmailId);
-                                                            $payslipMatriculeShare->setPermissions(\OCP\Constants::PERMISSION_READ);
-                                                            $payslipMatriculeShare->setSharedBy($this->userId);
-                                                            $payslipMatriculeShare = $shareManager->createShare($payslipMatriculeShare);
-                                                        } else {
-                                                            $this->_logCsvLine($this->l->t('Dossier SIRET-MATRICULE [' . $siretMatriculeDirName . '] est déjà partagé.'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+                                                    // si l'utilisateur n'a pas desactive la dematerialisation de son bulletin de paie
+                                                    if ($dematpayslipUser->getDisabled() != 1) {
+                                                        $payslipsMatriculeYearDirPath = $payslipsMatriculeDirPath . DIRECTORY_SEPARATOR . $year;
+
+                                                        if ($launchIndexation == 1) {
+                                                            // creer le partage du dossier "SIRET-MATRICULE"
+                                                            $payslipsMatriculeDirPathNode = $userFolder->get($payslipsMatriculeDirPath);
+
+                                                            // verifier si le dossier n'est pas deja partage
+                                                            if (!in_array($payslipsMatriculeDirPathNode->getId(), $payslipMatriculeShareNodeIdList)) {
+                                                                // partager le dossier MATRICULE en lecture seule avec l'utilisateur trouve a partir de son email
+                                                                $this->_logCsvLine($this->l->t('Partage du dossier SIRET-MATRICULE [' . $siretMatriculeDirName . '] en lecture seule.'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+                                                                $payslipMatriculeShare = $shareManager->newShare();
+                                                                $payslipMatriculeShare->setNode($payslipsMatriculeDirPathNode);
+                                                                $payslipMatriculeShare->setShareType(\OC\Share\Share::SHARE_TYPE_USER);
+                                                                $payslipMatriculeShare->setSharedWith($userFindByEmailId);
+                                                                $payslipMatriculeShare->setPermissions(\OCP\Constants::PERMISSION_READ);
+                                                                $payslipMatriculeShare->setSharedBy($this->userId);
+                                                                $payslipMatriculeShare = $shareManager->createShare($payslipMatriculeShare);
+                                                            } else {
+                                                                $this->_logCsvLine($this->l->t('Dossier SIRET-MATRICULE [' . $siretMatriculeDirName . '] est déjà partagé.'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+                                                            }
+
+                                                            // dossier "SIRET-MATRICULE/ANNEE"
+                                                            $this->_logCsvLine($this->l->t('Vérification de l\'existance du dossier de l\'annéee [' . $year . '].'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+                                                            $isDir = Filesystem::is_dir($payslipsMatriculeYearDirPath);
+                                                            if (!$isDir) {
+                                                                $this->_logCsvLine($this->l->t('Création du dossier de l\'annéee [' . $year . '].'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+                                                                $dirCreated = Filesystem::mkdir($payslipsMatriculeYearDirPath);
+                                                                if (!$dirCreated) {
+                                                                    $errorMsg = $this->l->t('Impossible de créer le dossier "' . $payslipsMatriculeYearDirPath . '[siret="' . $agentUser->getSiret() . '", matricule="' . $agentUser->getMatricule() . '", année="' . $year . '"].');
+                                                                    $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
+                                                                    $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
+                                                                }
+                                                            }
                                                         }
 
-                                                        // dossier "SIRET-MATRICULE/ANNEE"
-                                                        $this->_logCsvLine($this->l->t('Vérification de l\'existance du dossier de l\'annéee [' . $year . '].'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
-                                                        $isDir = Filesystem::is_dir($payslipsMatriculeYearDirPath);
-                                                        if (!$isDir) {
-                                                            $this->_logCsvLine($this->l->t('Création du dossier de l\'annéee [' . $year . '].'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
-                                                            $dirCreated = Filesystem::mkdir($payslipsMatriculeYearDirPath);
-                                                            if (!$dirCreated) {
-                                                                $errorMsg = $this->l->t('Impossible de créer le dossier "' . $payslipsMatriculeYearDirPath . '[siret="' . $agentUser->getSiret() . '", matricule="' . $agentUser->getMatricule() . '", année="' . $year . '"].');
-                                                                $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
-                                                                $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
+                                                        if ($this->_process->getHasError() <= 0) {
+                                                            // check if not already archived (return null if already archived with warning)
+                                                            $dematpayslipArchive = null;
+                                                            if ($launchArchive == 1) {
+                                                                $dematpayslipArchive = $this->_archiveCheckBeforeSave($dematpayslipArchiveMapper, $agentUser, $startDate, $endDate);
+                                                            }
+
+                                                            if ($launchArchive != 1 || ($launchArchive == 1 && !empty($dematpayslipArchive))) {
+                                                                if ($launchIndexation == 1) {
+                                                                    // deplacer le fichier PDF du bulletin de paie de l'agent dans "SIRET-MATRICULE/ANNEE/PERIODE.pdf"
+                                                                    $pdfPayslipFilePath = $payslipsMatriculeYearDirPath . DIRECTORY_SEPARATOR . $period . '.pdf';
+                                                                    $this->_logCsvLine($this->l->t('Déplacement du fichier PDF du bulletin de paie de la période [' . $period . '].'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+                                                                    $renamed = Filesystem::rename($pdfPayslipCheckFilePath, $pdfPayslipFilePath);
+                                                                    if (!$renamed) {
+                                                                        $errorMsg = $this->l->t('Impossible de déplacer le bulletin de paie "' . $pdfPayslipCheckFileName . '" dans le dossier "' . $pdfPayslipFilePath . '".');
+                                                                        $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
+                                                                        $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
+                                                                    }
+                                                                }
+
+                                                                // save archive and process
+                                                                if ($launchArchive == 1) {
+                                                                    $result = $this->_archiveSaveAndProcess($pdfPayslipCheckFilePath, $dematpayslipArchiveMapper, $dematpayslipArchive, $pdfPayslipFilePath, $agentUser, $startDate, $endDate, $year, $month, $editionDate, $phaseStartDateTime->getTimestamp());
+                                                                    if ($result < 0) {
+                                                                        $errorMsg = $this->l->t('Impossible de créer le fichier temporaire du bulletin de paie "' . $pdfPayslipCheckFilePath . '".');
+                                                                        $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
+                                                                        $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
+                                                                    }
+                                                                }
                                                             }
                                                         }
-                                                    }
+                                                    } else {
+                                                        // agent disabled payslip
+                                                        $this->_logCsvLine($this->l->t('L\'utlisateur a désactivé la dématérialisation des fiches de paie [email=' . $agentUser->getEmail() . '].'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
 
-                                                    if ($this->_process->getHasError() <= 0) {
                                                         // check if not already archived (return null if already archived with warning)
                                                         $dematpayslipArchive = null;
                                                         if ($launchArchive == 1) {
                                                             $dematpayslipArchive = $this->_archiveCheckBeforeSave($dematpayslipArchiveMapper, $agentUser, $startDate, $endDate);
                                                         }
 
-                                                        if ($launchArchive != 1 || ($launchArchive==1 && !empty($dematpayslipArchive))) {
+                                                        if ($launchArchive != 1 || ($launchArchive == 1 && !empty($dematpayslipArchive))) {
                                                             if ($launchIndexation == 1) {
-                                                                // deplacer le fichier PDF du bulletin de paie de l'agent dans "SIRET-MATRICULE/ANNEE/PERIODE.pdf"
-                                                                $pdfPayslipFilePath = $payslipsMatriculeYearDirPath . DIRECTORY_SEPARATOR . $period . '.pdf';
-                                                                $this->_logCsvLine($this->l->t('Déplacement du fichier PDF du bulletin de paie de la période [' . $period . '].'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
-                                                                $renamed = Filesystem::rename($pdfPayslipCheckFilePath, $pdfPayslipFilePath);
+                                                                // create PDF payslip
+                                                                $payslipNoDematHorodateDirPath = $this->_payslipNoIndexDir . DIRECTORY_SEPARATOR . $phaseStartDateTime->format('Y-m-d_H-i-s');
+                                                                $isDir = Filesystem::is_dir($payslipNoDematHorodateDirPath);
+                                                                if (!$isDir) {
+                                                                    $dirCreated = Filesystem::mkdir($payslipNoDematHorodateDirPath);
+                                                                    if (!$dirCreated) {
+                                                                        $this->_msgList['error'][] = $this->l->t('Impossible de créeer le dossier "' . $payslipNoDematHorodateDirPath . '".');
+                                                                    }
+                                                                }
+                                                                $payslipNoDematHorodateAgentDirPath = $payslipNoDematHorodateDirPath . DIRECTORY_SEPARATOR . $agentUser->getSiret() . '-' . $agentUser->getMatricule();
+                                                                $isDir = Filesystem::is_dir($payslipNoDematHorodateAgentDirPath);
+                                                                if (!$isDir) {
+                                                                    $dirCreated = Filesystem::mkdir($payslipNoDematHorodateAgentDirPath);
+                                                                    if (!$dirCreated) {
+                                                                        $this->_msgList['error'][] = $this->l->t('Impossible de créeer le dossier "' . $payslipNoDematHorodateAgentDirPath . '".');
+                                                                    }
+                                                                }
+                                                                $payslipNoDematPDFPath = $payslipNoDematHorodateAgentDirPath . DIRECTORY_SEPARATOR . $startDate . '_' . $endDate;
+                                                                $pdfPayslipNoDemat = new \setasign\Fpdi\Fpdi();
+
+                                                                // add all agent pdf pages
+                                                                $this->_logCsvLine($this->l->t('Ajout des pages PDF au bulletin de paie à ne pas indexer.'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+                                                                foreach ($pdfPageNumList as $pdfPageNum) {
+                                                                    $this->_logCsvLine($this->l->t('Ajout de la page ' . $pdfPageNum . '.'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
+                                                                    $pdfPayslipNoDemat->AddPage();
+                                                                    $pdfPayslipNoDemat->setSourceFile($pdfOriginFileFullPath);
+                                                                    $pdfPayslipNoDematId = $pdfPayslipNoDemat->importPage($pdfPageNum);
+                                                                    $pdfPayslipNoDemat->useTemplate($pdfPayslipNoDematId);
+                                                                }
+
+                                                                // create PDF payslip in no index folder
+                                                                $this->_logCsvLine($this->l->t('Créer le PDF du bulletin de paie non dematérialisé.'), self::LOG_DEBUG);
+                                                                $pdfPayslipNoDemat->Output('F', $userHomeFilesDirPath . DIRECTORY_SEPARATOR . $payslipNoDematPDFPath);
+                                                                $pdfPayslipFilePath = $payslipNoDematPDFPath . '.pdf';
+                                                                $renamed = Filesystem::rename($payslipNoDematPDFPath, $pdfPayslipFilePath);
                                                                 if (!$renamed) {
-                                                                    $errorMsg = $this->l->t('Impossible de déplacer le bulletin de paie "' . $pdfPayslipCheckFileName . '" dans le dossier "' . $pdfPayslipFilePath . '".');
-                                                                    $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
+                                                                    $errorMsg = $this->l->t('Impossible de déplacer le PDF du bulletin de paie non dematérialisé "' . $payslipNoDematPDFPath . '".');
+                                                                    $this->_logCsvLine($errorMsg, self::LOG_ERROR);
                                                                     $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
                                                                 }
                                                             }
 
                                                             // save archive and process
                                                             if ($launchArchive == 1) {
-                                                                $result = $this->_archiveSaveAndProcess($pdfPayslipCheckFilePath, $dematpayslipArchiveMapper, $dematpayslipArchive , $pdfPayslipFilePath, $agentUser, $startDate, $endDate, $year, $month, $editionDate, $phaseStartDateTime->getTimestamp());
+                                                                $result = $this->_archiveSaveAndProcess($pdfPayslipCheckFilePath, $dematpayslipArchiveMapper, $dematpayslipArchive, $pdfPayslipFilePath, $agentUser, $startDate, $endDate, $year, $month, $editionDate, $phaseStartDateTime->getTimestamp());
                                                                 if ($result < 0) {
                                                                     $errorMsg = $this->l->t('Impossible de créer le fichier temporaire du bulletin de paie "' . $pdfPayslipCheckFilePath . '".');
                                                                     $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
@@ -2178,217 +2245,156 @@ class PageController extends Controller {
                                                         }
                                                     }
                                                 } else {
-                                                    // agent disabled payslip
-                                                    $this->_logCsvLine($this->l->t('L\'utlisateur a désactivé la dématérialisation des fiches de paie [email=' . $agentUser->getEmail() . '].'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
-
-                                                    // check if not already archived (return null if already archived with warning)
-                                                    $dematpayslipArchive = null;
-                                                    if ($launchArchive == 1) {
-                                                        $dematpayslipArchive = $this->_archiveCheckBeforeSave($dematpayslipArchiveMapper, $agentUser, $startDate, $endDate);
-                                                    }
-
-                                                    if ($launchArchive != 1 || ($launchArchive == 1 && !empty($dematpayslipArchive))) {
-                                                        if ($launchIndexation == 1) {
-                                                            // create PDF payslip
-                                                            $payslipNoDematHorodateDirPath = $this->_payslipNoIndexDir . DIRECTORY_SEPARATOR . $phaseStartDateTime->format('Y-m-d_H-i-s');
-                                                            $isDir = Filesystem::is_dir($payslipNoDematHorodateDirPath);
-                                                            if (!$isDir) {
-                                                                $dirCreated = Filesystem::mkdir($payslipNoDematHorodateDirPath);
-                                                                if (!$dirCreated) {
-                                                                    $this->_msgList['error'][] = $this->l->t('Impossible de créeer le dossier "' . $payslipNoDematHorodateDirPath . '".');
-                                                                }
-                                                            }
-                                                            $payslipNoDematHorodateAgentDirPath = $payslipNoDematHorodateDirPath . DIRECTORY_SEPARATOR . $agentUser->getSiret() . '-' . $agentUser->getMatricule();
-                                                            $isDir = Filesystem::is_dir($payslipNoDematHorodateAgentDirPath);
-                                                            if (!$isDir) {
-                                                                $dirCreated = Filesystem::mkdir($payslipNoDematHorodateAgentDirPath);
-                                                                if (!$dirCreated) {
-                                                                    $this->_msgList['error'][] = $this->l->t('Impossible de créeer le dossier "' . $payslipNoDematHorodateAgentDirPath . '".');
-                                                                }
-                                                            }
-                                                            $payslipNoDematPDFPath = $payslipNoDematHorodateAgentDirPath . DIRECTORY_SEPARATOR . $startDate . '_' . $endDate;
-                                                            $pdfPayslipNoDemat = new \setasign\Fpdi\Fpdi();
-
-                                                            // add all agent pdf pages
-                                                            $this->_logCsvLine($this->l->t('Ajout des pages PDF au bulletin de paie à ne pas indexer.'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
-                                                            foreach ($pdfPageNumList as $pdfPageNum) {
-                                                                $this->_logCsvLine($this->l->t('Ajout de la page ' . $pdfPageNum . '.'), self::LOG_DEBUG, $agentUser, $startDate, $endDate);
-                                                                $pdfPayslipNoDemat->AddPage();
-                                                                $pdfPayslipNoDemat->setSourceFile($pdfOriginFileFullPath);
-                                                                $pdfPayslipNoDematId = $pdfPayslipNoDemat->importPage($pdfPageNum);
-                                                                $pdfPayslipNoDemat->useTemplate($pdfPayslipNoDematId);
-                                                            }
-
-                                                            // create PDF payslip in no index folder
-                                                            $this->_logCsvLine($this->l->t('Créer le PDF du bulletin de paie non dematérialisé.'), self::LOG_DEBUG);
-                                                            $pdfPayslipNoDemat->Output('F', $userHomeFilesDirPath . DIRECTORY_SEPARATOR . $payslipNoDematPDFPath);
-                                                            $pdfPayslipFilePath = $payslipNoDematPDFPath . '.pdf';
-                                                            $renamed = Filesystem::rename($payslipNoDematPDFPath, $pdfPayslipFilePath);
-                                                            if (!$renamed) {
-                                                                $errorMsg = $this->l->t('Impossible de déplacer le PDF du bulletin de paie non dematérialisé "' . $payslipNoDematPDFPath . '".');
-                                                                $this->_logCsvLine($errorMsg, self::LOG_ERROR);
-                                                                $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
-                                                            }
-                                                        }
-
-                                                        // save archive and process
-                                                        if ($launchArchive == 1) {
-                                                            $result = $this->_archiveSaveAndProcess($pdfPayslipCheckFilePath, $dematpayslipArchiveMapper, $dematpayslipArchive , $pdfPayslipFilePath, $agentUser, $startDate, $endDate, $year, $month, $editionDate, $phaseStartDateTime->getTimestamp());
-                                                            if ($result < 0) {
-                                                                $errorMsg = $this->l->t('Impossible de créer le fichier temporaire du bulletin de paie "' . $pdfPayslipCheckFilePath . '".');
-                                                                $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
-                                                                $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
-                                                            }
-                                                        }
-                                                    }
+                                                    $errorMsg = $this->l->t('L\'utilisateur "' . $userFindByEmailId . '" n\'a pas de dossier SIRET-MATRICULE associé [siret="' . $agentUser->getSiret() . '", matricule="' . $agentUser->getMatricule() . '", email="' . $agentUser->getEmail() . '"].');
+                                                    $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
+                                                    $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
                                                 }
-                                            } else {
-                                                $errorMsg = $this->l->t('L\'utilisateur "' . $userFindByEmailId . '" n\'a pas de dossier SIRET-MATRICULE associé [siret="' . $agentUser->getSiret() . '", matricule="' . $agentUser->getMatricule() . '", email="' . $agentUser->getEmail() . '"].');
-                                                $this->_logCsvLine($errorMsg, self::LOG_ERROR, $agentUser, $startDate, $endDate);
-                                                $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
                                             }
                                         }
                                     }
-                                }
 
-                                if ($this->_process->getHasError() > 0) {
-                                    $this->_logCsvLine('-- ' . $this->l->t('Fin de ligne CSV : ' . ($csvCheckLineNum + 1)) . ' --', self::LOG_ERROR);
-                                    break;
-                                } else {
-                                    $this->_logCsvLine('-- ' . $this->l->t('Fin de ligne CSV : ' . ($csvCheckLineNum + 1)) . ' --', self::LOG_SUCCESS);
-                                }
-
-                                $csvCheckLineNum++;
-                            }
-                            fclose($csvCheckFile);
+                                    if ($this->_process->getHasError() > 0) {
+                                        $this->_logCsvLine('-- ' . $this->l->t('Fin de ligne CSV : ' . ($csvCheckLineNum + 1)) . ' --', self::LOG_ERROR);
+                                        break;
+                                    } else {
+                                        $this->_logCsvLine('-- ' . $this->l->t('Fin de ligne CSV : ' . ($csvCheckLineNum + 1)) . ' --', self::LOG_SUCCESS);
+                                    }
 
-                            // creer le PDF des bulletins de paie a ne pas indexer
-                            if ($pdfPayslipSkip !== null) {
-                                $this->_logCsvLine($this->l->t('Créer le PDF des bulletins de paie a ne pas indexer.'), self::LOG_DEBUG);
-                                $pdfPayslipSkip->Output('F', $userHomeFilesDirPath . DIRECTORY_SEPARATOR . $payslipSkipPDFPath);
-                                $renamed = Filesystem::rename($payslipSkipPDFPath, $payslipSkipPDFPath . '.pdf');
-                                if (!$renamed) {
-                                    $errorMsg = $this->l->t('Impossible de déplacer le PDF des bulletins de paie a ne pas indexer "' . $payslipSkipPDFPath . '".');
-                                    $this->_logCsvLine($errorMsg, self::LOG_ERROR);
-                                    $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
+                                    $csvCheckLineNum++;
+                                }
+                                fclose($csvCheckFile);
+
+                                // creer le PDF des bulletins de paie a ne pas indexer
+                                if ($pdfPayslipSkip !== null) {
+                                    $this->_logCsvLine($this->l->t('Créer le PDF des bulletins de paie a ne pas indexer.'), self::LOG_DEBUG);
+                                    $pdfPayslipSkip->Output('F', $userHomeFilesDirPath . DIRECTORY_SEPARATOR . $payslipSkipPDFPath);
+                                    $renamed = Filesystem::rename($payslipSkipPDFPath, $payslipSkipPDFPath . '.pdf');
+                                    if (!$renamed) {
+                                        $errorMsg = $this->l->t('Impossible de déplacer le PDF des bulletins de paie a ne pas indexer "' . $payslipSkipPDFPath . '".');
+                                        $this->_logCsvLine($errorMsg, self::LOG_ERROR);
+                                        $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $csvCheckLineNum);
+                                    }
                                 }
                             }
                         }
-                    }
 
-                    if ($this->_process->getHasError() <= 0) {
-                        $phaseStep = 'report';
-                        $phaseStepNum = 0;
-                        $retryPhaseStepNum = $phaseStepNum;
-                        if ($retry > 0) {
-                            if ($this->_process->getPhaseStep() == $phaseStep) {
-                                $this->_process->setPhaseStep('');
-                            }
-                            $retryPhaseStepNum = $this->_process->getPhaseStepNum();
-                        }
-                        if (empty($this->_process->getPhaseStep())) {
-                            $reportSubDirName = $phaseStartDateTime->format('Y-m-d_H-i-s');
-                            $reportSubDirPath = $this->_reportDirUserPath . DIRECTORY_SEPARATOR . $reportSubDirName;
-
-                            // creer le sous dossier du rapport
-                            if ($phaseStepNum >= $retryPhaseStepNum) {
-                                $this->_logCsvLine($this->l->t('Création du dossier de rapport.'), self::LOG_DEBUG);
-                                $dirCreated = Filesystem::mkdir($reportSubDirPath);
-                                if (!$dirCreated) {
-                                    $errorMsg = $this->l->t('Impossible de créer le dossier du rapport "' . $reportSubDirName . '".');
-                                    $this->_logCsvLine($errorMsg, self::LOG_ERROR);
-                                    $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $phaseStepNum);
+                        if ($this->_process->getHasError() <= 0) {
+                            $phaseStep = 'report';
+                            $phaseStepNum = 0;
+                            $retryPhaseStepNum = $phaseStepNum;
+                            if ($retry > 0) {
+                                if ($this->_process->getPhaseStep() == $phaseStep) {
+                                    $this->_process->setPhaseStep('');
                                 }
+                                $retryPhaseStepNum = $this->_process->getPhaseStepNum();
                             }
+                            if (empty($this->_process->getPhaseStep())) {
+                                $reportSubDirName = $phaseStartDateTime->format('Y-m-d_H-i-s');
+                                $reportSubDirPath = $this->_reportDirUserPath . DIRECTORY_SEPARATOR . $reportSubDirName;
 
-                            if ($this->_process->getHasError() <= 0) {
-                                // deplacer le fichier CSV Upload dans le dossier Report
-                                $phaseStepNum++;
+                                // creer le sous dossier du rapport
                                 if ($phaseStepNum >= $retryPhaseStepNum) {
-                                    $csvUploadFileName = $this->_process->getCsvOrigin();
-                                    if (!empty($csvUploadFileName)) {
-                                        $this->_logCsvLine($this->l->t('Déplacement du fichier CSV d\'upload dans le dossier du rapport.'), self::LOG_DEBUG);
-                                        $csvUploadFileUserPath = $this->_uploadDirUserPath . DIRECTORY_SEPARATOR . $csvUploadFileName;
-                                        $reportFileUserPath = $reportSubDirPath . DIRECTORY_SEPARATOR . $csvUploadFileName;
-                                        $renamed = Filesystem::rename($csvUploadFileUserPath, $reportFileUserPath);
-                                        if (!$renamed) {
-                                            $errorMsg = $this->l->t('Impossible de déplacer le fichier CSV d\'upload "' . $csvUploadFileName . '" dans le dossier du rapport "' . $reportSubDirName . '".');
-                                            $this->_logCsvLine($errorMsg, self::LOG_ERROR);
-                                            $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $phaseStepNum);
-                                        }
+                                    $this->_logCsvLine($this->l->t('Création du dossier de rapport.'), self::LOG_DEBUG);
+                                    $dirCreated = Filesystem::mkdir($reportSubDirPath);
+                                    if (!$dirCreated) {
+                                        $errorMsg = $this->l->t('Impossible de créer le dossier du rapport "' . $reportSubDirName . '".');
+                                        $this->_logCsvLine($errorMsg, self::LOG_ERROR);
+                                        $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $phaseStepNum);
                                     }
                                 }
 
-                                // deplacer le fichier PDF Upload dans le dossier Report
-                                $phaseStepNum++;
-                                if ($phaseStepNum >= $retryPhaseStepNum) {
-                                    $pdfUploadFileName = $this->_process->getPdfOrigin();
-                                    if (!empty($pdfUploadFileName)) {
-                                        $this->_logCsvLine($this->l->t('Déplacement du fichier PDF d\'upload dans le dossier du rapport.'), self::LOG_DEBUG);
-                                        $pdfUploadFileUserPath = $this->_uploadDirUserPath . DIRECTORY_SEPARATOR . $pdfUploadFileName;
-                                        $reportFileUserPath = $reportSubDirPath . DIRECTORY_SEPARATOR . $pdfUploadFileName;
-                                        $renamed = Filesystem::rename($pdfUploadFileUserPath, $reportFileUserPath);
-                                        if (!$renamed) {
-                                            $errorMsg = $this->l->t('Impossible de déplacer le fichier PDF d\'upload "' . $pdfUploadFileName . '" dans le dossier du rapport "' . $reportSubDirName . '".');
-                                            $this->_logCsvLine($errorMsg, self::LOG_ERROR);
-                                            $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $phaseStepNum);
+                                if ($this->_process->getHasError() <= 0) {
+                                    // deplacer le fichier CSV Upload dans le dossier Report
+                                    $phaseStepNum++;
+                                    if ($phaseStepNum >= $retryPhaseStepNum) {
+                                        $csvUploadFileName = $this->_process->getCsvOrigin();
+                                        if (!empty($csvUploadFileName)) {
+                                            $this->_logCsvLine($this->l->t('Déplacement du fichier CSV d\'upload dans le dossier du rapport.'), self::LOG_DEBUG);
+                                            $csvUploadFileUserPath = $this->_uploadDirUserPath . DIRECTORY_SEPARATOR . $csvUploadFileName;
+                                            $reportFileUserPath = $reportSubDirPath . DIRECTORY_SEPARATOR . $csvUploadFileName;
+                                            $renamed = Filesystem::rename($csvUploadFileUserPath, $reportFileUserPath);
+                                            if (!$renamed) {
+                                                $errorMsg = $this->l->t('Impossible de déplacer le fichier CSV d\'upload "' . $csvUploadFileName . '" dans le dossier du rapport "' . $reportSubDirName . '".');
+                                                $this->_logCsvLine($errorMsg, self::LOG_ERROR);
+                                                $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $phaseStepNum);
+                                            }
                                         }
                                     }
-                                }
 
-                                // deplacer le fichier CSV de traitement dans le dossier Report
-                                $phaseStepNum++;
-                                if ($phaseStepNum >= $retryPhaseStepNum) {
-                                    if (!empty($csvCheckFileName)) {
-                                        $this->_logCsvLine($this->l->t('Déplacement du fichier CSV créé après vérification dans le dossier du rapport.'), self::LOG_DEBUG);
-                                        $csvCheckFileUserPath = $this->_checkDirUserPath . DIRECTORY_SEPARATOR . $csvCheckFileName;
-                                        $reportFileUserPath = $reportSubDirPath . DIRECTORY_SEPARATOR . $csvCheckFileName;
-                                        $renamed = Filesystem::rename($csvCheckFileUserPath, $reportFileUserPath);
-                                        if (!$renamed) {
-                                            $errorMsg = $this->l->t('Impossible de déplacer le fichier CSV de vérification "' . $csvCheckFileName . '" dans le dossier du rapport "' . $reportSubDirName . '".');
-                                            $this->_logCsvLine($errorMsg, self::LOG_ERROR);
-                                            $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $phaseStepNum);
+                                    // deplacer le fichier PDF Upload dans le dossier Report
+                                    $phaseStepNum++;
+                                    if ($phaseStepNum >= $retryPhaseStepNum) {
+                                        $pdfUploadFileName = $this->_process->getPdfOrigin();
+                                        if (!empty($pdfUploadFileName)) {
+                                            $this->_logCsvLine($this->l->t('Déplacement du fichier PDF d\'upload dans le dossier du rapport.'), self::LOG_DEBUG);
+                                            $pdfUploadFileUserPath = $this->_uploadDirUserPath . DIRECTORY_SEPARATOR . $pdfUploadFileName;
+                                            $reportFileUserPath = $reportSubDirPath . DIRECTORY_SEPARATOR . $pdfUploadFileName;
+                                            $renamed = Filesystem::rename($pdfUploadFileUserPath, $reportFileUserPath);
+                                            if (!$renamed) {
+                                                $errorMsg = $this->l->t('Impossible de déplacer le fichier PDF d\'upload "' . $pdfUploadFileName . '" dans le dossier du rapport "' . $reportSubDirName . '".');
+                                                $this->_logCsvLine($errorMsg, self::LOG_ERROR);
+                                                $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $phaseStepNum);
+                                            }
                                         }
                                     }
-                                }
 
-                                // deplacer le fichier de log cree lors de la phase de verification
-                                $phaseStepNum++;
-                                if ($phaseStepNum >= $retryPhaseStepNum) {
-                                    // recuperation du fichier de log cree lors de la phase de verification
-                                    $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;
-                                        $reportFileUserPath = $reportSubDirPath . DIRECTORY_SEPARATOR . $logCheckFileName;
-                                        $renamed = Filesystem::rename($logCheckFileUserPath, $reportFileUserPath);
-                                        if (!$renamed) {
-                                            $errorMsg = $this->l->t('Impossible de déplacer le fichier de Log de vérification "' . $logCheckFileName . '" dans le dossier du rapport "' . $reportSubDirName . '".');
-                                            $this->_logCsvLine($errorMsg, self::LOG_ERROR);
-                                            $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $phaseStepNum);
+                                    // deplacer le fichier CSV de traitement dans le dossier Report
+                                    $phaseStepNum++;
+                                    if ($phaseStepNum >= $retryPhaseStepNum) {
+                                        if (!empty($csvCheckFileName)) {
+                                            $this->_logCsvLine($this->l->t('Déplacement du fichier CSV créé après vérification dans le dossier du rapport.'), self::LOG_DEBUG);
+                                            $csvCheckFileUserPath = $this->_checkDirUserPath . DIRECTORY_SEPARATOR . $csvCheckFileName;
+                                            $reportFileUserPath = $reportSubDirPath . DIRECTORY_SEPARATOR . $csvCheckFileName;
+                                            $renamed = Filesystem::rename($csvCheckFileUserPath, $reportFileUserPath);
+                                            if (!$renamed) {
+                                                $errorMsg = $this->l->t('Impossible de déplacer le fichier CSV de vérification "' . $csvCheckFileName . '" dans le dossier du rapport "' . $reportSubDirName . '".');
+                                                $this->_logCsvLine($errorMsg, self::LOG_ERROR);
+                                                $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $phaseStepNum);
+                                            }
+                                        }
+                                    }
+
+                                    // deplacer le fichier de log cree lors de la phase de verification
+                                    $phaseStepNum++;
+                                    if ($phaseStepNum >= $retryPhaseStepNum) {
+                                        // recuperation du fichier de log cree lors de la phase de verification
+                                        $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;
+                                            $reportFileUserPath = $reportSubDirPath . DIRECTORY_SEPARATOR . $logCheckFileName;
+                                            $renamed = Filesystem::rename($logCheckFileUserPath, $reportFileUserPath);
+                                            if (!$renamed) {
+                                                $errorMsg = $this->l->t('Impossible de déplacer le fichier de Log de vérification "' . $logCheckFileName . '" dans le dossier du rapport "' . $reportSubDirName . '".');
+                                                $this->_logCsvLine($errorMsg, self::LOG_ERROR);
+                                                $this->_processErrorMsg(-1, $errorMsg, $phaseStep, $phaseStepNum);
+                                            }
                                         }
                                     }
                                 }
                             }
                         }
                     }
-                }
 
-                if ($this->_process->getHasError() <= 0) {
-                    $successMsg = 'Opération "' . self::_getPhaseName($this->_process->getPhase()) . '" terminée avec succès.';
-                    $this->_logCsvLine($this->l->t($successMsg), self::LOG_SUCCESS);
-                    $this->_msgList['success'][] = $successMsg;
-                    $this->_processInit();
-                }
+                    if ($this->_process->getHasError() <= 0) {
+                        $successMsg = 'Opération "' . self::_getPhaseName($this->_process->getPhase()) . '" terminée avec succès.';
+                        $this->_logCsvLine($this->l->t($successMsg), self::LOG_SUCCESS);
+                        $this->_msgList['success'][] = $successMsg;
+                        $this->_processInit();
+                    }
 
-                $this->_logCsvLine('## ' . $this->l->t('Fin du processus d\'indexation.') . ' ##', self::LOG_DEBUG);
-                fclose($this->_logFile);
-                // rename (sync file)
-                $renamed = Filesystem::rename($logFilePath, $logFilePath . '.csv');
-                $this->_process->setProcessing(0);
-                $this->_dematpayslipProcessMapper->update($this->_process);
-                // vider les lignes du process a sauter
-                $this->_dematpayslipProcessSkipMapper->deleteAllByIdProcess($this->_process->getId());
+                    $this->_logCsvLine('## ' . $this->l->t('Fin du processus d\'indexation.') . ' ##', self::LOG_DEBUG);
+                    fclose($this->_logFile);
+                    // rename (sync file)
+                    $renamed = Filesystem::rename($logFilePath, $logFilePath . '.csv');
+                    $this->_process->setProcessing(0);
+                    $this->_dematpayslipProcessMapper->update($this->_process);
+                    // vider les lignes du process a sauter
+                    $this->_dematpayslipProcessSkipMapper->deleteAllByIdProcess($this->_process->getId());
+                } catch (Exception $e) {
+                    $errorMsg = 'Exception ' . __METHOD__ . ' : error=' . $e->getMessage();
+                    $this->_msgList['error'][] = $errorMsg;
+                }
             }
 
             return new TemplateResponse('dematpayslip', 'index', $this->_assignParamsDemat());
-- 
GitLab