242 lines
9.1 KiB
PHP
242 lines
9.1 KiB
PHP
<?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);
|
|
|
|
// --- Define path to application directory
|
|
defined('APPLICATION_PATH')
|
|
|| define('APPLICATION_PATH', realpath(__DIR__ . '/../application'));
|
|
|
|
// --- Define application environment
|
|
defined('APPLICATION_ENV')
|
|
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
|
|
|
|
// --- Composer autoload
|
|
require_once realpath(__DIR__ . '/../vendor/autoload.php');
|
|
|
|
// Create application, bootstrap, and run
|
|
$application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
|
|
|
|
// --- Options
|
|
$displayUsage = false;
|
|
try {
|
|
$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",
|
|
));
|
|
$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());
|
|
Zend_Registry::set('config', $c);
|
|
|
|
$db = Zend_Db::factory($c->profil->db->metier);
|
|
Zend_Db_Table_Abstract::setDefaultAdapter($db);
|
|
|
|
|
|
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 {
|
|
$fileinM = new Application_Model_Sdv1FluxFileIn();
|
|
$sql = $fileinM->select()
|
|
->where('client=?','jalpdfsed')
|
|
->where('name=?', 'PDF')
|
|
->where('dateExecute=?','0000-00-00 00:00:00');
|
|
$fileResult = $fileinM->fetchAll($sql);
|
|
} catch (Zend_Db_Exception $e) {
|
|
echo date('Y-m-d H:i:s')." - ".$e->getMessage()."\n"; exit;
|
|
}
|
|
|
|
if ( $fileResult === null ) {
|
|
echo date('Y-m-d H:i:s')." - Aucun fichier à traiter\n";
|
|
} else {
|
|
foreach( $fileResult as $item) {
|
|
|
|
$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;
|
|
$jalM = new Application_Model_OctdeJalpdfsed();
|
|
$sql = $jalM->select()->where('pdfName=?', $fileName);
|
|
$exist = $jalM->fetchRow($sql);
|
|
if ( $exist === null ) {
|
|
$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 = $jalM->insert($data);
|
|
} catch (Zend_Db_Exception $e) {
|
|
echo date('Y-m-d H:i:s')." - ".$e->getMessage()."\n";
|
|
continue;
|
|
}
|
|
} else {
|
|
// --- Vérification de la taille des fichiers
|
|
if ($fileSize != $exist->pdfSize) {
|
|
// --- Renseigner octde.jalpdfsed
|
|
try {
|
|
$isInsert = $jalM->update(array(
|
|
'pdfSize' => $fileSize,
|
|
'pdfPage' => $filePage,
|
|
'pdfVer' => $fileVersion,
|
|
'pdfDate' => $fileDate,
|
|
'dateDeliver' => '0000-00-00 00:00:00',
|
|
), "id=".$exist->id);
|
|
} catch (Zend_Db_Exception $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 {
|
|
$fileinM->update(array('dateExecute' => date('YmdHis')), 'id='.$item->id);
|
|
} catch (Zend_Db_Exception $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 {
|
|
$jalM = new Application_Model_OctdeJalpdfsed();
|
|
$sql = $jalM->select()->where('dateDeliver=?', '0000-00-00 00:00:00');
|
|
$fileList = $jalM->fetchAll($sql);
|
|
} catch(Zend_Db_Exception $e) {
|
|
echo date('Y-m-d H:i:s')." - ".$e->getMessage()."\n";
|
|
}
|
|
}
|
|
// --- Renvoyer tous les fichiers en date du AAAAMMJJ
|
|
else {
|
|
exit;
|
|
}
|
|
|
|
if ($fileList === null) {
|
|
echo date('Y-m-d H:i:s')." - Aucun nouveau fichier pour orone\n";
|
|
} else {
|
|
// --- Envoyer les fichiers pour orone, simplement marquer dans teletransmission
|
|
foreach ($fileList as $item) {
|
|
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 {
|
|
$fileoutM = new Application_Model_Sdv1FluxFileOut();
|
|
$isInsert = $fileoutM->insert(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 (Zend_Db_Exception $e) {
|
|
echo date('Y-m-d H:i:s')." - ".$e->getMessage()."\n";
|
|
continue;
|
|
}
|
|
|
|
// --- Definir octde.jalpdfsed dateDeliver
|
|
if ($isInsert) {
|
|
try {
|
|
$jalM->update(array('dateDeliver' => date('YmdHis')), 'id='.$item->id);
|
|
} catch(Zend_Db_Exception $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
|
|
}
|