Scripts surveillance RRG

This commit is contained in:
Michael RICOIS 2015-01-19 16:49:15 +00:00
parent 3b9d1cd250
commit 70f3978d9f

View File

@ -0,0 +1,293 @@
<?php
//error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE & ~E_WARNING & ~E_DEPRECATED);
error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE & ~E_WARNING & ~E_DEPRECATED);
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
//Use classmap autoloader - useful with opcode and realpath cache
require_once 'Zend/Loader/AutoloaderFactory.php';
require_once 'Zend/Loader/ClassMapAutoloader.php';
Zend_Loader_AutoloaderFactory::factory(array(
'Zend_Loader_ClassMapAutoloader' => array(
__DIR__ . '/../../library/Zend/autoload_classmap.php',
__DIR__ . '/../../library/Application/autoload_classmap.php',
__DIR__ . '/../../library/Scores/autoload_classmap.php',
__DIR__ . '/../../application/autoload_classmap.php',
),
'Zend_Loader_StandardAutoloader' => array(
'prefixes' => array(
'Zend' => __DIR__ . '/../../library/Zend',
'Application' => __DIR__ . '/../../library/Application',
'Scores' => __DIR__ . '/../../library/Scores',
'SdMetier' => __DIR__ . '/../../library/SdMetier',
'Metier' => __DIR__ . '/../../library/Metier',
),
'fallback_autoloader' => true
)
));
// Zend_Application - Use it if you don't have autoloaders
//require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
try {
$opts = new Zend_Console_Getopt(
//Options
array(
'help|?' => "Displays usage information.",
'from=s' => "Depuis une date (format AAAAMMJJ)",
'debug' => "Génére uniquement un fichier sans télétransmission",
'cron' => "Mandatory option for launch the cli in cron",
)
);
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
}
//Usage
if( isset($opts->help) || count($opts->getOptions())==0 )
{
echo "Prestation RRG Surveillance.\n";
echo $opts->getUsageMessage();
exit;
}
$c = new Zend_Config($application->getOptions());
Zend_Registry::set('config', $c);
$login = 'mricois';
$client = 'rrg';
$typeDepot = 'FTP';
$prestation = 'HEXAVIA';
//Output path
$pathOut = $c->profil->path->storage . '/'. 'clients' . '/' . $client . '/' . 'recv';
$fileOptionsOut = array(
'dir' => $pathOut,
'type' => 'csv',
'delimiter' => '$',
'enclosure' => '"',
'header' => array(
'ID_S&D',
'SIRET',
'DATE_CREATION',
'CA',
'ANNEE_CA',
'CODE_PAIEMENT',
'LIBELLE_CODE_PAIE',
'INDISCORE20',
'ENCOURS'
),
'columns' => array(
'SourceID',
'Siret',
'DateCreaEn',
'BilanCA',
'BilanDateCloture',
'PaiementCS',
'PaiementInfo',
'Indiscore',
'Encours',
),
'name' => 'surv_',
//'encoding' => 'ISO-8859-15',
);
require_once APPLICATION_PATH.'/configs/config.php';
require_once 'framework/fwk.php';
require_once 'Metier/insee/classMInsee.php';
require_once 'Metier/scores/classMScores.php';
require_once 'Metier/partenaires/classMFacto.php';
$db = Zend_Db::factory($c->profil->db->metier);
$db->setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
$date = new Zend_Date();
if ($opts->from) {
$date->set($opts->from, 'yyyyMMdd');
}
if ( $date->getWeekday() == "1" ) {
$dateSql = $date->subDay(3)->toString('yyyy-MM-dd');
} else {
$dateSql = $date->toString('yyyy-MM-dd');
}
// --- Entités consultées via webservice (dateAjout) + Entités avec scores modifiés ou ajoutés (indiScoreDate)
echo date('Y-m-d H:i:s')." - Selection dans le portefeuille entre le $dateSql et ".date('Y-m-d')."\n";
$sql = "SELECT LPAD(surv.siren,9,0) AS siren, surv.dateAjout, score.indiScore20, score.indiScoreDate, score.encours, score.cs, score.dateBilan
FROM jo.surveillances_site AS surv LEFT JOIN jo.scores_surveillance AS score ON surv.siren = score.siren
WHERE surv.source='score' AND surv.login='".$login."' AND
( (surv.dateAjout BETWEEN '".$dateSql." 00:00:00' AND NOW())
OR (score.indiScoreDate BETWEEN '".$dateSql." 00:00:00' AND NOW()) )
GROUP BY siren ORDER BY siren, dateAjout DESC";
try {
$resultItem = $db->fetchAll($sql);
} catch (Zend_Db_Exception $e) {
echo $e->getMessage();
echo "\n";
}
if ( count($resultItem) == 0 ) {
echo date('Y-m-d H:i:s')." - Aucun élément.\n";
} else {
$dateBegin = date('YmdHis');
echo date('Y-m-d H:i:s')." - NbLignes=".count($resultItem)."\n";
// --- Ecriture du fichier
$file = $fileOptionsOut['dir'] . '/' .$fileOptionsOut['name'] . substr($dateBegin,0,8) . '.' . $fileOptionsOut['type'];
// --- 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']);
}
$iDb = new WDB();
$row = 0;
// --- Content
foreach ($resultItem as $item) {
$row++;
if ($opts->debug) { echo date('Y-m-d H:i:s')." - Ligne ".$row; }
// --- Data
$sql = $db->select()
->from('etablissements', array(
'LPAD(source_id,10,0) AS source_id',
'LPAD(siren,9,0) AS siren',
'LPAD(nic,5,0) AS nic'),
'jo')->where('siren=?', $item->siren)->where('siege=1')->order('actif DESC')->limit(1);
$etablissement = $db->fetchRow($sql, null, Zend_Db::FETCH_OBJ);
$DateCreaEn = '';
$BilanCA = '';
$BilanDateCloture = '';
if (/*true*/$item->indiScoreDate === null) {
if ($opts->debug) { echo " - Calcul du score"; }
$evaluation = calculIndiScore($item->siren, 0, false, 0);
if ( $evaluation['DateCreaEt'] > 0 && $evaluation['DateCreaEt'] < $evaluation['DateCreaEn'] ) {
$DateCreaEn = $evaluation['DateCreaEt'];
} elseif ( $evaluation['DateCreaEn'] > 0 ) {
$DateCreaEn = $evaluation['DateCreaEn'];
}
$PaiementCS = $evaluation['cs'];
if ($evaluation['NbBilansScore'] > 0) {
$bilan = $evaluation['tabBilan'][0];
$BilanCA = $bilan['FL'];
$BilanDateCloture = $bilan['DATE_CLOTURE'];
}
$Indiscore = $evaluation['Indiscore20'];
$Encours = round($evaluation['encours']/1000) * 1000;
} else {
if ($opts->debug) { echo " - Données en cache"; }
$iInsee = new MInsee($iDb);
$tabIdentite = $iInsee->getIdentiteEntreprise($item->siren, 0, 0, false, false);
$DateCreaEn = $tabIdentite['DateCreaEn'];
if ( $tabIdentite['DateCreaEt'] > 0 && $tabIdentite['DateCreaEt'] < $tabIdentite['DateCreaEn'] ) {
$DateCreaEn = $tabIdentite['DateCreaEt'];
} elseif ( $tabIdentite['DateCreaEn'] > 0 ) {
$DateCreaEn = $tabIdentite['DateCreaEn'];
}
$bilan = new MBilans($item->siren, $iDb);
$bilanDate = substr($item->dateBilan,8,2).'/'.substr($item->dateBilan,5,2).'/'.substr($item->dateBilan,0,4);
$bilanPostes = $bilan->getBilan($bilanDate);
if ( count($bilanPostes) > 0 ) {
$BilanCA = $bilanPostes['FL'];
$BilanDateCloture = substr($item->dateBilan,8,2).substr($item->dateBilan,5,2).substr($item->dateBilan,0,4);;
}
$PaiementCS = $item->cs;
$Indiscore = $item->indiScore20;
$Encours = round($item->encours/1000) * 1000;
}
if ( !empty($DateCreaEn) ) {
if (substr($DateCreaEn, -2) * 1 == 0) {
$DateCreaEn = substr($DateCreaEn,0,6).'01';
}
}
$PaiementInfo = '';
if ( in_array($PaiementCS, array('0', 'I', 'P', 'D')) ) {
$iFacto = new MFacto($iDb);
$info = $iFacto->getLibInfoCS($PaiementCS);
$PaiementInfo = $info['libExt'];
}
$values = array(
'SourceID' => $etablissement->source_id,
'Siret' => $etablissement->siren.$etablissement->nic,
'DateCreaEn' => $DateCreaEn,
'BilanCA' => $BilanCA,
'BilanDateCloture' => $BilanDateCloture,
'PaiementCS' => $PaiementCS,
'PaiementInfo' => $PaiementInfo,
'Indiscore' => $Indiscore,
'Encours' => $Encours,
);
// --- Trier données
$line = array();
foreach ( $fileOptionsOut['columns'] as $i => $column ) {
$line[] = $values[$column];
}
// --- Ecrire ligne
if ($opts->debug) { echo " - Ecriture ligne"; }
fputcsv($fp, $line, $fileOptionsOut['delimiter'], $fileOptionsOut['enclosure']);
if ($opts->debug) { echo "\n"; }
}
// --- Fermeture fichier
fclose($fp);
// --- Encoding
if ( array_key_exists('encoding', $fileOptionsOut) ) {
if ( $fileOptionsOut['encoding'] == 'ISO-8859-15' ) {
passthru("iconv -f UTF-8 -t ISO-8859-15 ".$file." -o ".$file.".enc");
passthru("cp -fv ".$file.".enc ".$file);
}
}
$nbLines = $row;
echo date('Y-m-d H:i:s')." - Nb Lignes Traitées : ".$nbLines."\n";
if ( $opts->cron ) {
$fluxoutM = new Application_Model_Sdv1FluxFileOut();
$dateEnd = date('YmdHis');
$fluxoutM->insert(array(
'client' => $client,
'name' => $prestation,
'nbLines' => $nbLines,
'dateBegin' => $dateBegin,
'dateEnd' => $dateEnd,
'fileOut' => basename($file),
'depotType' => $typeDepot,
'depotDate' => '0000-00-00 00:00:00',
));
}
}
echo date('Y-m-d H:i:s')." - Fin.\n";