Ajout remontée des erreurs + amélioration pour la suppression du script cron.php

This commit is contained in:
Michael RICOIS 2015-01-22 16:37:52 +00:00
parent 403411ec85
commit 2b510ba1eb

View File

@ -1,34 +1,37 @@
<?php <?php
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING); error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
// Define path to application directory // --- Define path to application directory
defined('APPLICATION_PATH') defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application')); || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
// Define application environment // --- Define application environment
define('APPLICATION_ENV', 'production'); define('APPLICATION_ENV', 'production');
// Ensure library/ is on include_path // --- Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array( set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'), realpath(APPLICATION_PATH . '/../library'),
get_include_path(), get_include_path(),
))); )));
/** Zend_Application */
// --- Zend_Application
require_once 'Zend/Application.php'; require_once 'Zend/Application.php';
// Create application, bootstrap, and run // --- Create application, bootstrap, and run
$application = new Zend_Application( $application = new Zend_Application(
APPLICATION_ENV, APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini' APPLICATION_PATH . '/configs/application.ini'
); );
// --- Console
try { try {
$opts = new Zend_Console_Getopt( $opts = new Zend_Console_Getopt(
//Options // --- Options
array( array(
'help|?' => "Aide.", 'help|?' => "Aide.",
'id=s' => "Identifiant du traitement", 'id=s' => "Identifiant du traitement",
'file=s' => "Identifiant pour les traitements par fichier", 'file=s' => "Identifiant pour les traitements par fichier",
'cron' => "Lancement avec cron",
'verbose' => "Mode verbeux", 'verbose' => "Mode verbeux",
) )
); );
@ -38,15 +41,15 @@ try {
exit; exit;
} }
//Usage // --- Usage
if(isset($opts->help) || !isset($opts->id) && !isset($opts->file) ) if( isset($opts->help) )
{ {
echo $opts->getUsageMessage(); exit; echo $opts->getUsageMessage(); exit;
} }
$config = new Zend_Config($application->getOptions()); $config = new Zend_Config($application->getOptions());
//Définition bdd // --- Définition bdd
try { try {
$db = Zend_Db::factory($config->profil->db->metier); $db = Zend_Db::factory($config->profil->db->metier);
Zend_Db_Table::setDefaultAdapter($db); Zend_Db_Table::setDefaultAdapter($db);
@ -54,71 +57,99 @@ try {
exit ( $e->getMessage() ); exit ( $e->getMessage() );
} }
$liste = array(); $id = null;
if ($opts->cron)
{
$commandesM = new Application_Model_CiblageEnrichissementIdentifiants();
$sql = $commandesM->select()
->where('idProfil != ?', 0)
->where("dateStart != '0000-00-00 00:00:00'")
->where("dateStop = '0000-00-00 00:00:00'");
$result = $commandesM->fetchAll($sql);
if (count($result)>0){
exit;
}
//Si pas de traitement en cours alors on lance
$sql = $commandesM->select()
->where('idProfil != ?', 0)
->where("dateStart = '0000-00-00 00:00:00'")
->where("dateStop = '0000-00-00 00:00:00'")
->order('dateAdded ASC')->limit(1);
$result = $commandesM->fetchAll($sql);
if (count($result)>0) {
$info = $result->current();
echo date('Y-m-d H:i:s') . " - Lancement enrichissement $info->id\n";
$id = $info->id;
}
}
if ($opts->id) if ($opts->id)
{ {
//Read SIRETs $id = $opts->id;
}
if ($id !== null)
{
// --- Read SIRETs
$commandesM = new Application_Model_CiblageEnrichissementIdentifiants(); $commandesM = new Application_Model_CiblageEnrichissementIdentifiants();
$commande = $commandesM->find(intval($opts->id))->current(); $commande = $commandesM->find(intval($id))->current();
$identifiants = json_decode($commande->identifiants, true); $identifiants = json_decode($commande->identifiants, true);
//Read profil for data extract // --- Read profil for data extract
$profilM = new Application_Model_CiblageEnrichissementProfils(); $profilM = new Application_Model_CiblageEnrichissementProfils();
$profil = $profilM->find(intval($commande->idProfil))->current(); $profil = $profilM->find(intval($commande->idProfil))->current();
$dataProfil = json_decode($profil->criteres, true); $dataProfil = json_decode($profil->criteres, true);
} }
else if ($opts->file)
{
} // --- Something is needed
//Something is needed
if ( count($identifiants)==0 || count($dataProfil)==0 ) { if ( count($identifiants)==0 || count($dataProfil)==0 ) {
echo "Identifiants:".count($identifiants).", profil:".$count($profil)."\n"; //echo "Identifiants:".count($identifiants).", profil:".$count($profil)."\n";
exit; exit;
} }
//Let's go // --- Let's go
$mois = substr($commande->dateAdded,0,4).substr($commande->dateAdded,5,2); $mois = substr($commande->dateAdded,0,4).substr($commande->dateAdded,5,2);
$path = $config->profil->path->data.'/'.$mois; $path = $config->profil->path->data.'/'.$mois;
if(!file_exists($path)) if( !file_exists($path) )
mkdir($path); mkdir($path);
require_once 'Scores/Enrichissement.php'; require_once 'Scores/Enrichissement.php';
$dico = new Enrichissement(); $dico = new Enrichissement();
$fields = $dico->getFields(); $fields = $dico->getFields();
//Entete, Valeur de remplacement et Requete SQL // --- Entete, Valeur de remplacement et Requete SQL
$tabEntete = array('siren', 'nic'); $tabEntete = array('siren', 'nic');
$tabEnteteLabel = array('SIREN', 'NIC'); $tabEnteteLabel = array('SIREN', 'NIC');
$columns = array('LPAD(siren, 9, 000000000) AS siren', 'LPAD(nic,5,00000) AS nic'); $columns = array('LPAD(siren, 9, 000000000) AS siren', 'LPAD(nic,5,00000) AS nic');
$joins = array(); $joins = array();
foreach ( $dataProfil as $item ) { foreach ( $dataProfil as $item ) {
//Get item // --- Get item
if ( array_key_exists($item, $fields) ) { if ( array_key_exists($item, $fields) ) {
$field = $fields[$item]; $field = $fields[$item];
//Définition de l'entete // --- Définition de l'entete
$tabEnteteLabel[] = $field['label']; $tabEnteteLabel[] = $field['label'];
$tabEntete[] = $item; $tabEntete[] = $item;
//Construction de la requete SQL // --- Construction de la requete SQL
if ( array_key_exists('sql', $field) ) { if ( array_key_exists('sql', $field) ) {
$columns[] = $field['sql']; $columns[] = $field['sql'];
} else { } else {
$columns[] = $field['column'].' AS '.$item; $columns[] = $field['column'].' AS '.$item;
} }
//Pour les champs de type "code", ajouter le libellé // --- Pour les champs de type "code", ajouter le libellé
if ( array_key_exists('join', $field) ) { if ( array_key_exists('join', $field) ) {
$tabEnteteLabel[] = $field['join']['label']; $tabEnteteLabel[] = $field['join']['label'];
$joinColumn = $item.'Lib'; $joinColumn = $item.'Lib';
$tabEntete[] = $joinColumn; $tabEntete[] = $joinColumn;
//Sql // --- Sql
$tableAlias = $item.'L'; $tableAlias = $item.'L';
$join['name'] = $field['join']['table'].' AS '.$tableAlias; $join['name'] = $field['join']['table'].' AS '.$tableAlias;
$join['col'] = $field['join']['column'].' AS '.$joinColumn; $join['col'] = $field['join']['column'].' AS '.$joinColumn;
@ -129,34 +160,35 @@ foreach ( $dataProfil as $item ) {
} }
} }
//Ajouter le champ presentRcs // --- Ajouter le champ presentRcs
$columns[] = 'presentRcs'; $columns[] = 'presentRcs';
$tabEntete[] = 'presentRcs'; $tabEntete[] = 'presentRcs';
$tabEnteteLabel[] = 'RCS'; $tabEnteteLabel[] = "Présent au RNCS";
$outFile = $profil->login.'_'.$opts->id.'_'.date('YmdHis').'.csv'; $outFile = $profil->login.'_'.$id.'_'.date('YmdHis').'.csv';
$fp = fopen($path.'/'.$outFile, 'w'); $fp = fopen($path.'/'.$outFile, 'w');
//Ecrire l'entete // --- Ecrire l'entete
if (count($tabEnteteLabel)>0){ if ( count($tabEnteteLabel) > 0 ) {
fputcsv($fp, $tabEnteteLabel, ',', '"'); fputcsv($fp, $tabEnteteLabel, ',', '"');
} }
//Mise à jour des éléments // --- Mise à jour des éléments
if ($opts->id) { if ($id !== null) {
$commandesM->update(array( $commandesM->update(array(
'dateStart'=> date('Y-m-d H:i:s'), 'dateStart'=> date('Y-m-d H:i:s'),
'fichier' => basename($outFile) 'fichier' => basename($outFile)
), "id = ".$commande->id); ), "id = ".$commande->id);
} }
//Pour chaque identifiant traiter les données // --- Pour chaque identifiant traiter les données
$row = 1; $row = 1;
//Date de debut de traitement. // --- Date de debut de traitement.
$dateStart = date('YmdHis'); $dateStart = date('YmdHis');
if ($opts->verbose) echo "Nb Lines :".count($identifiants)."\n"; $nbLineTotal = count($identifiants);
if ($opts->verbose) echo "Nb Lines :".$nbLineTotal."\n";
foreach ($identifiants as $siret) foreach ($identifiants as $siret)
{ {
if ($opts->verbose) echo "Line $row.\n"; if ($opts->verbose) echo "Line $row / $nbLineTotal : ";
$sql = $db->select()->from('etablissements_act', $columns, 'jo'); $sql = $db->select()->from('etablissements_act', $columns, 'jo');
$sql->where("siren='".substr($siret,0,9)."' AND nic='".substr($siret,9,5)."'"); $sql->where("siren='".substr($siret,0,9)."' AND nic='".substr($siret,9,5)."'");
@ -166,41 +198,74 @@ foreach ($identifiants as $siret)
} }
} }
$tabData = null;
// --- Get data in database
try { try {
$result = $db->fetchRow($sql, null, Zend_Db::FETCH_ASSOC); $tabData = $db->fetchRow($sql, null, Zend_Db::FETCH_ASSOC);
} catch(Exception $e) { } catch(Exception $e) {
echo $sql."\n"; echo date('Y-m-d H:i:s') . ' - Ligne '. $row . ' : Erreur ' . $sql . " : " . $e->getMessage() ."\n";
} }
$tabData = $result; if ($opts->verbose) {
//Trier pour la sortie echo "IN=".$siret." : OUT=".$tabData['siren'];
}
// --- Ligne vide
if ( $tabData === false ) {
if ($opts->verbose) {
echo " - Ligne vide";
} else {
echo date('Y-m-d H:i:s') . ' - Ligne '. $row ." - Siret $siret introuvable dans la table.\n";
}
}
// --- Export des données
else {
// --- Trier pour la sortie
$tabSortie = array(); $tabSortie = array();
foreach($tabEntete as $key) { foreach($tabEntete as $key) {
//Add static values // --- Add static values
if ( array_key_exists($key, $fields) ) { if ( array_key_exists($key, $fields) ) {
if ( array_key_exists('values', $fields[$key]) ) { if ( array_key_exists('values', $fields[$key]) ) {
$values = $fields[$key]['values']; $valuesRef = $fields[$key]['values'];
//Remplace value if exist $value = $tabData[$key];
if ( array_key_exists($tabData[$key], $values) ) { // --- Remplace value if exist
$tabData[$key] = $values[$tabData[$key]]; if ( array_key_exists($value, $valuesRef) ) {
$tabData[$key] = $valuesRef[$tabData[$key]];
} }
} }
} }
//Order data for CSV file if ( $key == 'presentRcs' ) {
$valuesRef = array(
'0' => "Non",
'1' => "Oui",
);
$value = $tabData[$key];
// --- Remplace value if exist
if ( array_key_exists($value, $valuesRef) ) {
$tabData[$key] = $valuesRef[$tabData[$key]];
}
}
// --- Order data for CSV file
$tabSortie[] = isset($tabData[$key]) ? $tabData[$key] : ''; $tabSortie[] = isset($tabData[$key]) ? $tabData[$key] : '';
} }
fputcsv($fp, $tabSortie, ',', '"'); fputcsv($fp, $tabSortie, ',', '"');
}
//Mise à jour des lignes traitées dans la base // --- Mise à jour des lignes traitées dans la base
if ($opts->id) { if ( $id !== null ) {
$commandesM->update(array('nbLigneTraites'=>$row), "id = ".$commande->id); $commandesM->update(array('nbLigneTraites'=>$row), "id = ".$commande->id);
} }
if ( $opts->verbose ) {
echo "\n";
}
$row++; $row++;
} }
fclose($fp); fclose($fp);
if ($opts->id) { if ($id !== null) {
$commandesM->update( array('dateStop' => date('Y-m-d H:i:s')) , "id = ".$commande->id); $commandesM->update( array('dateStop' => date('Y-m-d H:i:s')) , "id = ".$commande->id);
} }