"Displays usage information.", 'parsefile' => "Parcours les fichiers", 'deliver' => "Relivrer les fichiers depuis (from)", 'from=s' => "db, date au format AAAAMMJJ", 'verbose' => "mode verbeux", 'cron' => "Mandatory option for launch the cli in cron", )); $opts->parse(); } catch (Zend_Console_Getopt_Exception $e) { $displayUsage = true; } // --- Aide / Options if (count($opts->getOptions())==0 || isset($opts->help)) { $displayUsage = true; } // --- Usage if ($displayUsage) { echo "Gestion des PDFs JAL (Emission et Transmission).\n"; echo $opts->getUsageMessage(); exit; } $c = new Zend_Config($application->getOptions()); // Database $config = new \Doctrine\DBAL\Configuration(); $connectionParams = array( 'dbname' => $c->profil->db->metier->params->dbname, 'user' => $c->profil->db->metier->params->username, 'password' => $c->profil->db->metier->params->password, 'host' => $c->profil->db->metier->params->host, 'charset' => 'utf8', 'driver' => 'pdo_mysql', ); try { $conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config); } catch (\Doctrine\DBAL\DBALException $e) { echo "Connection Database impossible.\n"; exit; } if ($opts->cron) { echo date('Y-m-d H:i:s')." - Récupération des fichiers jalpdfsed.\n"; // --- Lire les fichiers dans sdv1.flux_filein (jalpdfsed/PDF) avec dateExecute = 0 try { $fileSql = "SELECT * FROM sdv1.flux_filein WHERE client='jalpdfsed' AND name='PDF' AND dateExecute='0000-00-00 00:00:00'"; $fileStmt = $conn->executeQuery($fileSql); } catch (\Doctrine\DBAL\DBALException $e) { echo date('Y-m-d H:i:s')." - ".$e->getMessage()."\n"; exit; } if ($fileStmt->rowCount() > 0) { echo date('Y-m-d H:i:s')." - Aucun fichier à traiter\n"; } else { while($item = $fileStmt->fetch(\PDO::FETCH_OBJ)) { $fileName = $item->depotFile; $filePath = $c->profil->path->shared . '/clients/jalpdfsed/send/' . $fileName; echo date('Y-m-d H:i:s')." - Fichier $fileName\n"; if ( false !== ( $file = file_get_contents($filePath) ) ) { if (preg_match('/^([0-9]{1,})_([0-9]{8})(_[0-9]{1,})?\.pdf$/i', $fileName, $t)) { $idJalEd = $t[1]; $dateJalEd = $t[2]; //Version preg_match( "/^\%PDF\-(.*)\s/U", $file, $matches ); $fileVersion = ''; if (count($matches) > 0) { $fileVersion = $matches[1]; } //Number of page $filePage = preg_match_all( "/\/Page\W/", $file, $matches ); //Pdf size $fileSize = filesize($filePath); //Date de création $fileDate = date('Ymd', filectime($filePath)); $isInsert = false; $jalSql = "SELECT pdfName FROM octde.jalpdfsed WHERE pdfName=:file"; $jalStmt = $conn->prepare($jalSql); $jalStmt->bindValue('file', $fileName); $jalStmt->execute(); if ($jalStmt->rowCount()) { $data = array( 'pdfName' => $fileName, 'idJalEd' => $idJalEd, 'dateJalEd' => $dateJalEd, 'pdfSize' => $fileSize, 'pdfPage' => $filePage, 'pdfVer' => $fileVersion, 'pdfDate' => $fileDate, 'pdfText' => 0, 'dateInsert' => date('YmdHis'), ); // --- Renseigner octde.jalpdfsed try { $isInsert = $conn->insert('octde.jalpdfsed', $data); } catch (\Doctrine\DBAL\DBALException $e) { echo date('Y-m-d H:i:s')." - ".$e->getMessage()."\n"; continue; } } else { $exist = $jalStmt->fetch(\PDO::FETCH_OBJ); // --- Vérification de la taille des fichiers if ($fileSize != $exist->pdfSize) { // --- Renseigner octde.jalpdfsed try { $isInsert = $conn->update('octde.jalpdfsed', array( 'pdfSize' => $fileSize, 'pdfPage' => $filePage, 'pdfVer' => $fileVersion, 'pdfDate' => $fileDate, 'dateDeliver' => '0000-00-00 00:00:00', ), array('id' => $exist->id)); } catch (\Doctrine\DBAL\DBALException $e) { echo date('Y-m-d H:i:s')." - ".$e->getMessage()."\n"; continue; } } } if ($isInsert) { // --- Copier le fichier dans le dossier de stockage orone/recv if (copy($filePath, $c->profil->path->shared . '/clients/orone/recv/' . $fileName)) { // --- Definir sdv1.flux_filein dateExecute try { $conn->update('sdv1.flux_filein', array( 'dateExecute' => date('YmdHis')), array('id' => $item->id)); } catch (\Doctrine\DBAL\DBALException $e) { echo date('Y-m-d H:i:s')." - ".$e->getMessage()."\n"; } } } } } } } echo date('Y-m-d H:i:s')." - Fin Récupération des fichiers jalpdfsed.\n"; } if ($opts->deliver || $opts->cron) { echo date('Y-m-d H:i:s')." - Livraison des fichiers.\n"; $fileList = array(); $from = 'db'; if (isset($opts->from) && $opts->from != 'db') { $from = $opts->from; } // --- Lire tous les fichiers avec dateDeliver = 0 if ($from == 'db') { try { $jalSql = "SELECT * FROM octde.jalpdfsed WHERE dateDeliver = '0000-00-00 00:00:00'"; $jalStmt = $conn->executeQuery($jalSql); } catch(\Doctrine\DBAL\DBALException $e) { echo date('Y-m-d H:i:s')." - ".$e->getMessage()."\n"; } } // --- Renvoyer tous les fichiers en date du AAAAMMJJ else { exit; } if ($jalStmt->rowCount() == 0) { echo date('Y-m-d H:i:s')." - Aucun nouveau fichier pour orone\n"; } else { // --- Envoyer les fichiers pour orone, simplement marquer dans teletransmission while($item = $jalStmt->fetch(\PDO::FETCH_OBJ)) { echo date('Y-m-d H:i:s')." - Fichier $item->pdfName\n"; $filePath = $c->profil->path->shared . '/clients/orone/recv/' . $item->pdfName; if (file_exists($filePath)) { $isInsert = false; $size = filesize($filePath); try { $isInsert = $conn->insert('sdv1.flux_fileout', array( 'client' => 'orone', 'name' => 'PDF', 'nbLines' => 0, 'dateBegin' => date('YmdHis'), 'dateEnd' => date('YmdHis'), 'fileOut' => $item->pdfName, 'depotFileSize' => $size, 'depotType' => 'FTP', 'depotDate' => '0000-00-00 00:00:00', )); } catch (\Doctrine\DBAL\DBALException $e) { echo date('Y-m-d H:i:s')." - ".$e->getMessage()."\n"; continue; } // --- Definir octde.jalpdfsed dateDeliver if ($isInsert) { try { $conn->update('octde.jalpdfsed', array('dateDeliver' => date('YmdHis')), array('id' => $item->id)); } catch(\Doctrine\DBAL\DBALException $e) { echo date('Y-m-d H:i:s')." - ".$e->getMessage()."\n"; } } } } } echo date('Y-m-d H:i:s')." - Fin Livraison des fichiers.\n"; } if ($opts->parsefile) { // --- Lire les fichiers dans clients/jalpdfsed/send // --- Vérifier la présence en base }