batch/bin/sourceJalTable.php
2016-11-14 14:28:07 +01:00

199 lines
6.7 KiB
PHP

<?php
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.",
'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 "Orone Table.\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;
}
$requests = array(
'REF_CodeJournal_' => array(
'header' => array('id', 'nomJALedition', 'parution', 'parutionJours', 'aboDateDeb', 'aboDateFin', 'priorite'),
'columns' => array('id', 'nomJALedition', 'parution', 'parutionJours', 'aboDateDeb', 'aboDateFin', 'priorite'),
'sql' => "SELECT jal.id, jal.nomJALedition, jal.parution, jal.parutionJours, jalabo.aboDateDeb, jalabo.aboDateFin,
IF(jal.jalPrioritaire=1,'A',IF(jal.parution='Q','C','B')) AS priorite
FROM jo.tabJALed AS jal
LEFT JOIN ( SELECT abo.jalId, abo.aboDateDeb, abo.aboDateFin FROM jo.tabJALabo AS abo
WHERE abo.aboDateDeb < NOW() AND abo.aboDateFin > NOW()
GROUP BY abo.jalId ORDER BY abo.aboDateFin DESC
) AS jalabo ON jalabo.jalId = jal.id
WHERE jal.dateSuppr=0 ORDER BY jal.id",
),
'REF_CodeFormeJuridique_' => array(
'header' => array('code', 'libelle', 'libelleCourt'),
'columns' => array('code', 'libelle', 'libelleCourt'),
'sql' => array(
"SET @sumSirene := (SELECT SUM(nbSirene) FROM jo.tabFJur);",
"SELECT code, libelle, libelleCourt, (nbSirene/@sumSirene )*100 AS pourcentage
FROM jo.tabFJur
WHERE code>999 AND libelle <> ''
ORDER BY pourcentage DESC;"
),
),
'REF_CodeTribunal_' => array(
'header' => array('triId', 'triCode', 'triType', 'triNom', 'triVille'),
'columns' => array('triId', 'triCode', 'triType', 'triNom', 'triVille'),
'sql' => "SELECT triId, triCode, triType, triNom, triVille
FROM jo.tribunaux
WHERE triType IN ('L', 'C', 'I', 'G', 'M');",
),
/*'REF_Evenements' => array(
'header' => array('triId', 'triCode', 'triType', 'triNom', 'triVille'),
'columns' => array('triId', 'triCode', 'triType', 'triNom', 'triVille'),
'sql' => "SELECT triId, triCode, triType, triNom, triVille
FROM jo.tribunaux
WHERE triType IN ('L', 'C', 'I', 'G', 'M');",
),*/
);
$dateBegin = date('YmdHis');
$dateFile = date('Ymd');
//Output path
$client = 'orone';
$pathOut = $c->profil->path->shared . '/clients/' . $client . '/recv';
foreach($requests as $n => $r) {
$fileOptionsOut = array(
'dir' => $pathOut,
'type' => 'csv',
'delimiter' => ';',
'enclosure' => '"',
'header' => $r['header'],
'columns' => $r['columns'],
'name' => $n,
//'encoding' => 'ISO-8859-15',
);
// --- Requete SQL
if (is_array($r['sql'])){
$i = 0;
foreach ($r['sql'] as $request)
{
$sql = $request;
if ($i > count($r['sql']) - 1 ) {
break;
}
$conn->executeQuery($sql);
$i++;
}
} else {
$sql = $r['sql'];
}
$stmt = $conn->executeQuery($sql);
if ($stmt->rowCount() == 0) {
echo date('Y-m-d H:i:s')." - Pas de résultats\n";
}
else {
// --- Ecriture du fichier
$filename = $fileOptionsOut['name'] . $dateFile . '.' . $fileOptionsOut['type'];
$file = $fileOptionsOut['dir'] . '/' . $filename;
// --- Ouverture fichier
$fp = fopen($file, 'w');
if ( $fp === false ) {
echo date('Y-m-d H:i:s')." - ERREUR Impossible de créer le fichier ".$file."\n";
exit;
}
// --- Header
if ( count($fileOptionsOut['header']) > 0 ) {
fputcsv($fp, $fileOptionsOut['header'], $fileOptionsOut['delimiter'], $fileOptionsOut['enclosure']);
}
while ($item = $stmt->fetch(\PDO::FETCH_ASSOC)) {
// --- Trier données
$line = array();
foreach ( $fileOptionsOut['columns'] as $i => $column ) {
$line[] = $item[$column];
}
// --- Ecrire ligne
if ( empty($fileOptionsOut['enclosure']) ) {
fwrite($fp, join($fileOptionsOut['delimiter'], $line)."\n");
} else {
fputcsv($fp, $line, $fileOptionsOut['delimiter'], $fileOptionsOut['enclosure']);
}
}
// --- Fermeture fichier
fclose($fp);
// --- Taille du fichier
$size = filesize($file);
// --- Inscription pour livraison automatique dans le flux
try {
$conn->insert('sdv1.flux_fileout', array(
'client' => 'orone',
'name' => 'TABLE',
'nbLines' => 0,
'dateBegin' => $dateBegin,
'dateEnd' => date('YmdHis'),
'fileOut' => $filename,
'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;
}
}
}