Modification du batch d'enrichissement

This commit is contained in:
Michael RICOIS 2012-05-23 14:07:24 +00:00
parent c343c5197a
commit a6f17922bd

View File

@ -45,16 +45,26 @@ if(isset($opts->help) || !isset($opts->id) && !isset($opts->file) )
exit;
}
$config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/configuration.ini');
//MetadataCache pour la base de données
$frontendOptions = array(
'lifetime' => 14400,
'automatic_serialization' => true
);
$backendOptions = array();
$cache = Zend_Cache::factory('Core','Apc', $frontendOptions, $backendOptions);
Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
//Définition bdd
$dbConfig = new Zend_Config_Ini(APPLICATION_PATH . '/configs/configuration.ini', 'databases');
try {
$db = Zend_Db::factory($dbConfig->db);
$db = Zend_Db::factory($config->databases->db);
} catch ( Exception $e ) {
exit ( $e->getMessage() );
}
//Définition bdd metier
try {
$dbMetier = Zend_Db::factory($dbConfig->jo);
$dbMetier = Zend_Db::factory($config->databases->jo);
} catch ( Exception $e ) {
exit ( $e->getMessage() );
}
@ -63,103 +73,107 @@ $liste = array();
if ($opts->id)
{
$sql = $db->select()
->from('enrichissement_identifiants')
->where('id = ?', intval($opts->id));
$commande = $db->fetchRow($sql);
$identifiants = json_decode($commande['identifiants'], true);
//Read SIRETs
$commandesM = new Application_Model_EnrichissementIdentifiants($db);
$commande = $commandesM->find(intval($opts->id))->current();
$identifiants = json_decode($commande->identifiants, true);
$sql = $db->select()
->from('enrichissement_profils')
->where('id = ?', $commande['idProfil']);
$profil = $db->fetchRow($sql);
$dataProfil = json_decode($profil['criteres'], true);
//Read profil for data extract
$profilM = new Application_Model_EnrichissementProfils($db);
$profil = $profilM->find(intval($commande->idProfil))->current();
$dataProfil = json_decode($profil->criteres, true);
}
else if ($opts->file)
{
}
if ( count($identifiants)>0 && count($dataProfil)>0 )
//Something is needed
if ( count($identifiants)==0 || count($dataProfil)==0 ) {
exit;
}
//Let's go
$mois = date('Y-m');
if(!file_exists($config->path->data.'/'.$mois))
mkdir($config->path->data.'/'.$mois);
$path = $config->path->data.'/'.$mois;
$outFile = $profil['login'].'_'.$opts->id.'_'.date('YmdHis').'.csv';
require_once 'Scores/Enrichissement.php';
$dico = new Enrichissement();
$fields = $dico->getFields();
//Entete, Valeur de remplacement et Requete SQL
$tabEntete = array('siren', 'nic');
$tabEnteteLabel = array('SIREN', 'NIC');
$sql = 'SELECT LPAD(siren, 9, 000000000) AS siren, LPAD(nic,5,00000) AS nic,';
foreach ( $dataProfil as $item ) {
//Définition de l'entete
$tabEnteteLabel[] = $fields[$item]['label'];
$tabEntete[] = $item;
//Construction de la requete SQL
if ( array_key_exists('sql', $fields[$item]) ) {
$sql.= ' '.$fields[$item]['sql'].', ';
} else {
$sql.= ' '.$fields[$item]['column'].' AS '.$item.',';
}
}
//Ajouter le champ presentRcs
$tabEntete[] = 'presentRcs';
$tabEnteteLabel[] = 'RCS';
$sql .= ' presentRcs';
//Pour chaque identifiant traiter les données
$row = 0;
$fp = fopen($path.'/'.$outFile, 'w');
//Ecrire l'entete
if (count($tabEnteteLabel)>0){
fputcsv($fp, $tabEnteteLabel, ',', '"');
}
//Mise à jour des éléments
if ($opts->id) {
$commandesM->update('enrichissement_identifiants', array(
'dateStart'=>date('Y-m-d H:i:s'),
'fichier' => basename($outFile)),
"id = ".$commande->id);
}
$model = $sql;
$traite = 0;
//Date de debut de traitement.
$dateStart = date('YmdHms');
foreach ($identifiants as $siret )
{
$config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/configuration.ini');
$mois = date('Y-m');
if(!file_exists($config->path->data.'/'.$mois))
mkdir($config->path->data.'/'.$mois);
$path = $config->path->data.'/'.$mois;
$outFile = $opts->id.'_'.$profil['login'].'_'.date('YmdHis').'.csv';
require_once 'Scores/Enrichissement.php';
$dico = new Enrichissement();
$fields = $dico->getFields();
//Entete, Valeur de remplacement et Requete SQL
$tabEntete = array('siren', 'nic');
$tabEnteteLabel = array('SIREN', 'NIC');
$sql = 'SELECT LPAD(siren, 9, 000000000) AS siren, LPAD(nic,5,00000) AS nic,';
foreach ( $dataProfil as $item ) {
//Définition de l'entete
$tabEnteteLabel[] = $fields[$item]['label'];
$tabEntete[] = $item;
//Construction de la requete SQL
if ( array_key_exists('sql', $fields[$item]) ) {
$sql.= ' '.$fields[$item]['sql'].', ';
} else {
$sql.= ' '.$fields[$item]['column'].' AS '.$item.',';
}
$sql = $model.' FROM etablissements_act WHERE siren='.substr($siret,0,9).' AND nic='.substr($siret,9,5);
try {
$result = $dbMetier->fetchAll($sql);
$traite++;
} catch(Exception $e) {
echo $sql;
}
//Ajouter le champ presentRcs
$tabEntete[] = 'presentRcs';
$tabEnteteLabel[] = 'RCS';
$sql .= ' presentRcs';
$tabData = $result[0];
//Trier pour la sortie
$tabSortie = array();
foreach($tabEntete as $key){
$tabSortie[] = isset($tabData[$key]) ? $tabData[$key] : '';
}
fputcsv($fp, $tabSortie, ',', '"');
//Pour chaque identifiant traiter les données
$row = 0;
$fp = fopen($path.'/'.$outFile, 'w');
//Ecrire l'entete
if (count($tabEnteteLabel)>0){
fputcsv($fp, $tabEnteteLabel, ',', '"');
//Mise à jour des lignes traitées dans la base
if ($opts->id) {
$commandesM->update(array('nbLigneTraites'=>$row), "id = ".$commande->id);
}
$model = $sql;
$traite = 0;
//Date de debut de traitement.
$dateStart = date('YmdHms');
foreach ($identifiants as $siret )
{
$sql = $model.' FROM etablissements_act WHERE siren='.substr($siret,0,9).' AND nic='.substr($siret,9,5);
try {
$result = $dbMetier->fetchAll($sql);
$traite++;
} catch(Exception $e) {
echo $sql;
}
$tabData = $result[0];
//Trier pour la sortie
$tabSortie = array();
foreach($tabEntete as $key){
$tabSortie[] = isset($tabData[$key]) ? $tabData[$key] : '';
}
fputcsv($fp, $tabSortie, ',', '"');
$row++;
}
//Gestion de l'erreur
$error = 0;
if($traite < $commande['nbLigneTotales'])
$error = 1;
//date de fin de traitement.
$dateStop = date('YmdHms');
//Update de la ligne dans la base de donnée.
$datas = array(
'nbLigneTraites' => $traite,
'dateStart' => $dateStart,
'dateStop' => $dateStop,
'error' => $error,
'fichier' => $outFile
);
$db->update('enrichissement_identifiants', $datas, 'id = '.$opts->id);
fclose($fp);
$row++;
}
fclose($fp);
if ($opts->id) {
$commandesM->update( array('dateStop' => date('Y-m-d H:i:s')) , "id = ".$commande->id);
}
@ -228,22 +242,4 @@ function siretValide($siret) {
$estValide = false;
}
return $estValide;
}
/**
* writeCSV
* @param array $list
* @param array $entete
* @param string $filename
*/
function writeCSV($list, $entete, $filename)
{
$fp = fopen($filename, 'w');
if (count($entete)>0){
fputcsv($fp, $entete, ',', '"');
}
foreach ($list as $fields) {
fputcsv($fp, $fields, ',', '"');
}
fclose($fp);
}