Complete use of league/csv

This commit is contained in:
Michael RICOIS 2016-11-14 17:47:53 +01:00
parent 64c0a80c18
commit 5b45740234

View File

@ -286,19 +286,18 @@ if ($opts->load) {
$stmt->execute();
echo "\n";
if (($rowCpt % $batchSize) === 0) {
$conn->rollBack();
$conn->commit();
}
}
$conn->commit();
}
catch(\Doctrine\DBAL\DBALException $e) {
$conn->rollBack();
echo date('Y-m-d H:i:s')." - Erreur ".$e->getMessage()."\n";
exit(1);
}
$conn->setAutoCommit(true);
exit;
$seq = execDbIn($opts->load, $fileOptionsIn);
// Marquer les SIREN qui n'existe pas dans la bdd
@ -397,32 +396,24 @@ if ($opts->evaluate) {
$nb = $stmt->rowCount();
if ($nb > 0) {
echo date('Y-m-d H:i:s')." - NbLignes=".$nb."\n";
echo date('Y-m-d H:i:s')." - NbLignes : ".$nb."\n";
if ($execId !== null) {
$execRef->total($execId, $nb);
}
$file = $fileOkOptionsOut['dir'] . '/' . $fileOkOptionsOut['name'] . $dateBegin . '.' . $fileOkOptionsOut['type'];
// Ouverture fichier RETOUR
$fp = fopen($file, 'w');
// Header
$line = array($seq, 'RCE');
fputcsv($fp, $line, $fileOkOptionsOut['delimiter'], $fileOkOptionsOut['enclosure']);
// Create the CSV
$csv = Writer::createFromPath($file, 'w');
$csv->setDelimiter($fileOkOptionsOut['delimiter']);
$csv->setEnclosure($fileOkOptionsOut['enclosure']);
// Insert the CSV header
$csv->insertOne(array($seq, 'RCE'));
// Insert data
$row = 0;
// Content
while ($item = $stmt->fetch(\PDO::FETCH_OBJ)) {
$row++;
echo date('Y-m-d H:i:s')." - Ligne $row : Siren=".$item->siren;
@ -431,7 +422,7 @@ if ($opts->evaluate) {
}
// Score non calculé
if ( $item->indiScore20 === null ) {
if ($item->indiScore20 === null) {
echo ", Non calculé";
$conn->update('jo.sfr_data', array('error' => 2020),
array("siren" => $item->siren, 'NumSeq' => $seq));
@ -442,7 +433,7 @@ if ($opts->evaluate) {
$sfrM = new Metier_Sfr_Scoring();
$sfrM->notCalculateInError = true;
//Set Values
// Set Values
$sfrM->setVal('ContratDate', $item->DateContrat);
$sfrM->setVal('IR', $item->IR);
$sfrM->setVal('NAF', $item->naf);
@ -457,43 +448,33 @@ if ($opts->evaluate) {
$sfrM->setVal('DateImmat',$item->dateImmat);
$sfrM->setVal('SituationJuridique',$item->procol);
//Evaluate
if ( $sfrM->evaluate($item->siren) ) {
// Evaluate
if ($sfrM->evaluate($item->siren)) {
$values = array();
$values['siren'] = str_pad($item->siren, 9, '0', STR_PAD_LEFT);
$values['idVOR'] = $sfrM->getIndicateur();
$values['commentaire'] = $sfrM->getComment();
$values['PO'] = $sfrM->getPo();
if ( $opts->debug ) {
$valuesDebug = $sfrM->getValDebug();
$values = array_merge($values, $valuesDebug);
}
echo ", ID=".$values['idVOR'].", commentaire=".$values['commentaire'];
//Ecrire fichier RETOUR
// Ecrire fichier RETOUR
$line = array();
foreach ( $fileOkOptionsOut['columns'] as $i => $column ) {
foreach ($fileOkOptionsOut['columns'] as $i => $column) {
$line[] = $values[$column];
}
fputcsv($fp, $line, $fileOkOptionsOut['delimiter'], $fileOkOptionsOut['enclosure']);
} else {
$csv->insertOne($line);
}
else {
echo ", Pas de calcul";
$conn->update('jo.sfr_data', array('error' => 2020),
array("siren" => $item->siren, 'NumSeq' => $seq));
}
}
echo "\n";
}
fclose($fp);
}
if ($fileOkOptionsOut['encoding']) {
echo "Encodage du fichier\n";
passthru("iconv -f UTF-8 -t ISO-8859-15 ".$file." -o ".$file.".enc");
@ -524,7 +505,7 @@ if ($opts->evaluate) {
}
// Recherche des siren en erreur
// Recherche des SIREN en erreur
echo date('Y-m-d H:i:s')." - Recherche des SIREN en erreur.\n";
$sql = "SELECT LPAD(sfr.siren, 9, 0) AS siren, sfr.error
FROM jo.sfr_data AS sfr WHERE sfr.error!=0 AND sfr.NumSeq=:seq";
@ -543,7 +524,7 @@ if ($opts->evaluate) {
$csv->setDelimiter($fileErrorOptionsOut['delimiter']);
$csv->setEnclosure($fileErrorOptionsOut['enclosure']);
// Insert the CSV header
$csv->insertOne($fileErrorOptionsOut['columns']);
$csv->insertOne(array($seq, 'RCE'));
// Insert all data
$csv->insertAll($rows);
;
@ -657,216 +638,6 @@ if ($opts->evaluate) {
}
}
/**
*
* @param array $values
* @param string $seq
* @param string $debug
* @return array
*/
function execInternal($values, $seq, $debug = false)
{
$error = 0;
$siren = $values['siren'];
echo "$siren";
$classInsee = new Metier_Insee_MInsee();
$values['code'] = '';
//Siren valide - 1010
if ( !$classInsee->valideSiren($siren) ) {
$error = 1;
$code = 'error';
$values['code'] = '1010';
echo " - Erreur 1010.";
}
//Siren existant - 1020
else if ( !$classInsee->sirenExiste($siren) ) {
$error = 1;
$code = 'error';
$values['code'] = '1020';
echo " - Erreur 1020.";
}
//Date incorrecte - 2010
try {
$dateContrat = DateTime::createFromFormat('Ymd', $values['dateContrat']);
} catch(Exception $e) {
if ($values['dateContrat']!='') {
$error = 1;
$code = 'error';
$values['code'] = '2010';
echo " - Erreur 2010 ";
}
}
if ( $error == 0 ) {
//Calculate data
$sfrM = new Metier_Sfr_Scoring();
$sfrM->notCalculateInError = false;
//Set Values
$sfrM->setVal('ContratDate', $values['dateContrat']);
$sfrM->setVal('IR', $values['ir']);
//Evaluate
if ( $sfrM->evaluate($siren) ) {
$values['idVOR'] = $sfrM->getIndicateur();
$values['commentaire'] = $sfrM->getComment();
$values['PO'] = $sfrM->getPo();
$valuesDebug = $sfrM->getValDebug();
if ($debug) {
$values = array_merge($values, $valuesDebug);
}
$code = 'ok';
echo " - VOR=".$values['idVOR']." , commentaire=".$values['commentaire']." , PO=".$values['PO'];
} else {
$error = 1;
$code = 'error';
$values['code'] = '2020';
echo " - Not calculated ";
}
}
echo PHP_EOL;
return array(
'values' => $values,
'code' => $code,
);
}
/**
*
* @param string $file
* @param array $fileOptionsIn
* @param array $filesOptionsOut
* @param boolean $debug
* @param boolean $resume
* @return array
*/
function execFileIn($file, $fileOptionsIn, $filesOptionsOut = array(), $debug = false, $resume = false)
{
global $dateBegin;
//Set the input file path
$filepathIn = $file;
//Open output file path
foreach ($filesOptionsOut as $code => $file) {
${'fp'.$code} = fopen($file['dir'] . DIRECTORY_SEPARATOR . $file['name'] . $dateBegin . '.' . $file['type'], 'w');
}
//Read file
if ( file_exists($filepathIn) ) {
if (($handle = fopen($filepathIn, 'r')) !== false) {
$row = 0;
while (($data = fgetcsv($handle, 0, $fileOptionsIn['delimiter'], $fileOptionsIn['enclosure'])) !== false)
{
$row++;
echo "Ligne $row : ";
//Header
if (count($fileOptionsIn['header']) > 0 && $row == 1) {
foreach ( $fileOptionsIn['header'] as $i => $column ) {
$header[$column] = $data[$i];
}
echo "Detection HEADER.".PHP_EOL;
if ($debug) {
$filesOptionsOut['error']['header'] = array();
$filesOptionsOut['ok']['header'] = array();
} else {
$filesOptionsOut['error']['header'] = $header;
$filesOptionsOut['ok']['header'] = $header;
}
if ( $resume ) {
fputcsv(${'fp'.$codeRetour}, $filesOptionsOut['ok']['line'][0], $filesOptionsOut['ok']['delimiter'], $filesOptionsOut['ok']['enclosure']);
}
}
//Content
else {
//Set values from file
$values = array();
foreach ( $fileOptionsIn['columns'] as $i => $column ) {
$values[$column] = $data[$i];
}
$isResume = false;
if ( $resume ) {
foreach ( $filesOptionsOut['ok']['line'] as $line ) {
if ( $line[0] == $data[0] ) {
fputcsv(${'fp'.$codeRetour}, $line, $filesOptionsOut['ok']['delimiter'], $filesOptionsOut['ok']['enclosure']);
$isResume = true;
break;
}
}
}
if ( !$isResume ) {
//Exec the process
$valuesClient = execInternal($values, $header['idDemande'], true);
//Format output
$codeRetour = $valuesClient['code'];
//$filesOptionsOut[$codeRetour]['line'][] = $valuesClient['values'];
if ($debug) {
$filesOptionsOut[$codeRetour]['columns'] = array_keys($valuesClient['values']);
}
if ($row == 2) {
foreach ($filesOptionsOut as $code => $file) {
$line = array();
//Dynamic Header
if ( count($file['header'])>0 ) {
foreach ( $file['header'] as $i => $column ) {
$line[] = $column;
}
}
//Static Header
else {
foreach ( $file['columns'] as $i => $column ) {
$line[] = $column;
}
}
fputcsv(${'fp'.$code}, $line, $filesOptionsOut[$code]['delimiter'], $filesOptionsOut[$code]['enclosure']);
}
}
//Write the line following the return code
$line = array();
foreach ( $filesOptionsOut[$codeRetour]['columns'] as $i => $column ) {
$line[] = $valuesClient['values'][$column];
}
fputcsv(${'fp'.$codeRetour}, $line, $filesOptionsOut[$codeRetour]['delimiter'], $filesOptionsOut[$codeRetour]['enclosure']);
}
}
}
}
fclose($handle);
foreach ($filesOptionsOut as $code => $file) {
fclose(${'fp'.$code});
}
}
}
/**
*
* @param string $filepathIn