batch/bin/sourceJalPdf.php

265 lines
10 KiB
PHP
Raw Normal View History

2015-04-01 10:07:23 +00:00
<?php
/**
* Livraison des fichiers pour orone
*
* Toutes les 10 minutes
* Lire les fichiers dans le repertoire clients/jalpdfsed/send
* Intégrer les informations dans la table octde.jalpdfsed
* Copier le fichier dans clients/orone/recv
* Integrer les informations dans flux_fileout pour livraison
* Mettre à jour la date de livraison
* Déplacer le fichier dans clients/jalpdfsed/send/done
*/
error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE & ~E_WARNING & ~E_DEPRECATED);
2015-09-14 20:31:06 +00:00
// --- Define path to application directory
2015-04-01 10:07:23 +00:00
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(__DIR__ . '/../application'));
2015-04-01 10:07:23 +00:00
2015-09-14 20:31:06 +00:00
// --- Define application environment
2015-04-01 10:07:23 +00:00
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// --- Composer autoload
require_once realpath(__DIR__ . '/../vendor/autoload.php');
2015-04-01 10:07:23 +00:00
// Create application, bootstrap, and run
2015-09-14 20:31:06 +00:00
$application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
2015-04-01 10:07:23 +00:00
2016-04-11 16:32:24 +02:00
// --- Options
$displayUsage = false;
2015-04-01 10:07:23 +00:00
try {
2016-04-11 16:32:24 +02:00
$opts = new Zend_Console_Getopt(array(
'help|?' => "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",
));
2015-04-01 10:07:23 +00:00
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
2016-04-11 16:32:24 +02:00
$displayUsage = true;
}
// --- Aide / Options
if (count($opts->getOptions())==0 || isset($opts->help)) {
$displayUsage = true;
2015-04-01 10:07:23 +00:00
}
2016-04-11 16:32:24 +02:00
// --- Usage
if ($displayUsage) {
echo "Gestion des PDFs JAL (Emission et Transmission).\n";
2015-04-01 10:07:23 +00:00
echo $opts->getUsageMessage();
exit;
}
$c = new Zend_Config($application->getOptions());
2016-11-02 14:48:59 +01:00
// 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;
}
2015-04-01 10:07:23 +00:00
if ($opts->cron) {
2015-07-01 09:35:42 +00:00
2015-04-01 10:07:23 +00:00
echo date('Y-m-d H:i:s')." - Récupération des fichiers jalpdfsed.\n";
2015-07-01 09:35:42 +00:00
2015-04-01 10:07:23 +00:00
// --- Lire les fichiers dans sdv1.flux_filein (jalpdfsed/PDF) avec dateExecute = 0
try {
2016-11-02 15:27:52 +01:00
$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) {
2015-04-01 10:07:23 +00:00
echo date('Y-m-d H:i:s')." - ".$e->getMessage()."\n"; exit;
}
2015-07-01 09:35:42 +00:00
2016-11-02 15:27:52 +01:00
if ($fileStmt->rowCount() > 0) {
2015-04-01 10:07:23 +00:00
echo date('Y-m-d H:i:s')." - Aucun fichier à traiter\n";
2016-11-02 15:27:52 +01:00
}
else {
while($item = $fileStmt->fetch(\PDO::FETCH_OBJ)) {
2015-04-01 10:07:23 +00:00
$fileName = $item->depotFile;
2016-01-26 13:50:08 +00:00
$filePath = $c->profil->path->shared . '/clients/jalpdfsed/send/' . $fileName;
2015-07-01 09:35:42 +00:00
2015-04-01 10:07:23 +00:00
echo date('Y-m-d H:i:s')." - Fichier $fileName\n";
2015-07-01 09:35:42 +00:00
2015-04-01 10:07:23 +00:00
if ( false !== ( $file = file_get_contents($filePath) ) ) {
2015-10-05 09:41:14 +00:00
if (preg_match('/^([0-9]{1,})_([0-9]{8})(_[0-9]{1,})?\.pdf$/i', $fileName, $t)) {
$idJalEd = $t[1];
$dateJalEd = $t[2];
2015-07-01 09:35:42 +00:00
//Version
preg_match( "/^\%PDF\-(.*)\s/U", $file, $matches );
2016-01-26 14:18:40 +00:00
$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));
2015-07-01 09:35:42 +00:00
$isInsert = false;
2016-11-02 15:27:52 +01:00
$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
2015-04-01 10:07:23 +00:00
try {
2016-11-02 15:27:52 +01:00
$isInsert = $conn->insert('octde.jalpdfsed', $data);
}
catch (\Doctrine\DBAL\DBALException $e) {
2015-04-01 10:07:23 +00:00
echo date('Y-m-d H:i:s')." - ".$e->getMessage()."\n";
continue;
}
2016-11-02 15:27:52 +01:00
}
else {
$exist = $jalStmt->fetch(\PDO::FETCH_OBJ);
// --- Vérification de la taille des fichiers
if ($fileSize != $exist->pdfSize) {
// --- Renseigner octde.jalpdfsed
try {
2016-11-02 15:27:52 +01:00
$isInsert = $conn->update('octde.jalpdfsed', array(
'pdfSize' => $fileSize,
'pdfPage' => $filePage,
'pdfVer' => $fileVersion,
'pdfDate' => $fileDate,
'dateDeliver' => '0000-00-00 00:00:00',
2016-11-02 15:27:52 +01:00
), array('id' => $exist->id));
}
catch (\Doctrine\DBAL\DBALException $e) {
echo date('Y-m-d H:i:s')." - ".$e->getMessage()."\n";
continue;
}
}
}
2015-07-01 09:35:42 +00:00
if ($isInsert) {
// --- Copier le fichier dans le dossier de stockage orone/recv
2016-01-26 13:50:08 +00:00
if (copy($filePath, $c->profil->path->shared . '/clients/orone/recv/' . $fileName)) {
// --- Definir sdv1.flux_filein dateExecute
try {
2016-11-02 15:27:52 +01:00
$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";
}
2015-04-01 10:07:23 +00:00
}
}
}
}
}
}
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";
2015-07-01 09:35:42 +00:00
2015-04-01 10:07:23 +00:00
$fileList = array();
$from = 'db';
if (isset($opts->from) && $opts->from != 'db') {
$from = $opts->from;
}
2015-07-01 09:35:42 +00:00
2015-04-01 10:07:23 +00:00
// --- Lire tous les fichiers avec dateDeliver = 0
if ($from == 'db') {
try {
2016-11-02 15:27:52 +01:00
$jalSql = "SELECT * FROM octde.jalpdfsed WHERE dateDeliver = '0000-00-00 00:00:00'";
$jalStmt = $conn->executeQuery($jalSql);
}
catch(\Doctrine\DBAL\DBALException $e) {
2015-04-01 10:07:23 +00:00
echo date('Y-m-d H:i:s')." - ".$e->getMessage()."\n";
}
}
// --- Renvoyer tous les fichiers en date du AAAAMMJJ
else {
exit;
}
2016-11-02 15:27:52 +01:00
if ($jalStmt->rowCount() == 0) {
2015-04-01 10:07:23 +00:00
echo date('Y-m-d H:i:s')." - Aucun nouveau fichier pour orone\n";
} else {
// --- Envoyer les fichiers pour orone, simplement marquer dans teletransmission
2016-11-02 15:27:52 +01:00
while($item = $jalStmt->fetch(\PDO::FETCH_OBJ)) {
2015-04-01 10:07:23 +00:00
echo date('Y-m-d H:i:s')." - Fichier $item->pdfName\n";
2016-01-26 13:50:08 +00:00
$filePath = $c->profil->path->shared . '/clients/orone/recv/' . $item->pdfName;
2016-11-02 15:27:52 +01:00
if (file_exists($filePath)) {
2015-04-01 10:07:23 +00:00
$isInsert = false;
$size = filesize($filePath);
try {
2016-11-02 15:29:01 +01:00
$isInsert = $conn->insert('sdv1.flux_fileout', array(
2015-04-01 10:07:23 +00:00
'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',
));
2016-11-02 15:27:52 +01:00
}
catch (\Doctrine\DBAL\DBALException $e) {
2015-04-01 10:07:23 +00:00
echo date('Y-m-d H:i:s')." - ".$e->getMessage()."\n";
continue;
}
2015-07-01 09:35:42 +00:00
2015-04-01 10:07:23 +00:00
// --- Definir octde.jalpdfsed dateDeliver
if ($isInsert) {
try {
2016-11-02 15:27:52 +01:00
$conn->update('octde.jalpdfsed',
array('dateDeliver' => date('YmdHis')),
array('id' => $item->id));
}
catch(\Doctrine\DBAL\DBALException $e) {
2015-04-01 10:07:23 +00:00
echo date('Y-m-d H:i:s')." - ".$e->getMessage()."\n";
}
}
}
}
}
2015-07-01 09:35:42 +00:00
2015-04-01 10:07:23 +00:00
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
}