Prise en compte des nouvelles options pour le format du fichier en sortie

This commit is contained in:
Michael RICOIS 2015-06-11 12:05:08 +00:00
parent 996db51afc
commit eaf4d1c699

View File

@ -80,7 +80,7 @@ $id = null;
// --- Execute by cron
if ($opts->cron) {
$commandesM = new Application_Model_CiblageEnrichissementIdentifiants();
$sql = $commandesM->select()
->where('idProfil != ?', 0)
->where("dateStart != '0000-00-00 00:00:00'")
@ -117,7 +117,12 @@ if ( $id === null ) {
// --- Read SIRETs from order
$commandesM = new Application_Model_CiblageEnrichissementIdentifiants();
$commande = $commandesM->find(intval($id))->current();
try {
$commande = $commandesM->find(intval($id))->current();
} catch (Zend_Db_Exception $e) {
echo date('Y-m-d H:i:s') . " - Erreur chargement commande...\n";
exit;
}
$identifiants = json_decode($commande->identifiants, true);
// --- Read profil for data extract
@ -125,7 +130,17 @@ $profilM = new Application_Model_CiblageEnrichissementProfils();
$profil = $profilM->find(intval($commande->idProfil))->current();
$dataProfil = json_decode($profil->criteres, true);
// --- Options du fichier
$fileFormat = 'csv';
$fileSeparator = ',';
$fileDbParams = $commande->fichierParams;
if ( !empty($fileDbParams) ) {
$options = json_decode($fileDbParams);
$fileFormat = $options->type;
if ( $fileFormat == 'csv' && isset($options->separator) ) {
$fileSeparator = $options->separator;
}
}
// --- Something is needed
if ( count($identifiants)==0 || count($dataProfil)==0 ) {
@ -195,10 +210,17 @@ if ($fileFormat == 'csv')
{
$outFile = $profil->login.'_'.$id.'_'.date('YmdHis').'.csv';
$fp = fopen($path.'/'.$outFile, 'w');
// ---- Ecrire l'entete
if ($fp === false) {
echo "Erreur création fichier.\n";
exit;
}
if ($opts->verbose) {
echo "Ecriture fichier : $outFile\n";
}
// --- Ecrire l'entete
if (count($tabEnteteLabel) > 0)
{
fputcsv($fp, $tabEnteteLabel, $fileOptions['separator'], '"');
fputcsv($fp, $tabEnteteLabel, $fileSeparator, '"');
}
}
@ -231,11 +253,11 @@ $row = 1;
// --- Date de debut de traitement.
$dateStart = date('YmdHis');
$nbLineTotal = count($identifiants);
if ($opts->verbose) echo "Nb Lines :".$nbLineTotal."\n";
if ($opts->verbose) echo "Nb Lines : ".$nbLineTotal."\n";
foreach ($identifiants as $siret)
{
if ($opts->verbose) echo "Line $row / $nbLineTotal : ";
$sql = $db->select()->from('etablissements_act', $columns, 'jo');
$sql->where("siren='".substr($siret,0,9)."' AND nic='".substr($siret,9,5)."'");
if (count($joins)) {
@ -245,7 +267,7 @@ foreach ($identifiants as $siret)
}
$tabData = null;
// --- Get data in database
try {
$tabData = $db->fetchRow($sql, null, Zend_Db::FETCH_ASSOC);
@ -256,7 +278,7 @@ foreach ($identifiants as $siret)
if ($opts->verbose) {
echo "IN=".$siret." : OUT=".$tabData['siren'];
}
// --- Ligne vide
if ( $tabData === false ) {
if ($opts->verbose) {
@ -295,12 +317,12 @@ foreach ($identifiants as $siret)
// --- Order data for CSV file
$tabSortie[] = isset($tabData[$key]) ? $tabData[$key] : '';
}
// --- Ecriture dans le fichier
if ($fileFormat == 'csv') {
fputcsv($fp, $tabSortie, ',', '"');
fputcsv($fp, $tabSortie, $fileSeparator, '"');
}
if ($fileFormat == 'xlsx') {
foreach($tabSortie as $col => $value) {
$sheet->setCellValueByColumnAndRow($col, $row+1, $value);
@ -312,7 +334,7 @@ foreach ($identifiants as $siret)
if ( $id !== null ) {
$commandesM->update(array('nbLigneTraites'=>$row), "id = ".$commande->id);
}
if ( $opts->verbose ) {
echo "\n";
}