2013-08-26 07:08:21 +00:00
< ? php
2013-09-24 08:12:55 +00:00
/*
* Crontab
2016-01-26 13:55:08 +00:00
* 01 19 * * 1 - 6 scores php / home / scores / batch / scripts / clients / SfrIndicateur . php -- cron -- load >> / home / scores / batch / shared / log / SfrIndicateur . log
* 01 00 * * 1 - 6 scores php / home / scores / batch / scripts / clients / SfrIndicateur . php -- cron -- evaluate >> / home / scores / batch / shared / log / SfrIndicateur . log
2013-09-24 08:12:55 +00:00
*/
2015-01-19 15:25:39 +00:00
error_reporting ( E_ALL & ~ E_STRICT & ~ E_NOTICE & ~ E_WARNING & ~ E_DEPRECATED );
2013-10-21 09:43:01 +00:00
2017-02-16 10:40:46 +01:00
require_once __DIR__ . '/../application/bin.bootstrap.php' ;
2013-08-26 07:08:21 +00:00
2016-04-11 16:32:24 +02:00
// --- Options
$displayUsage = false ;
2013-08-26 07:08:21 +00:00
try {
2016-11-29 17:03:57 +01:00
$opts = new Zend_Console_Getopt ( array (
'help|?' => " Displays usage information. " ,
'load-s' => " Preload file in database and check SIREN " ,
'evaluate-s' => " Lauch evaluation (specify the Seq Number to regen) " ,
'manual-s' => " Provide manualy the file path to get result and debug values " ,
'debug' => " Debug " ,
'cron' => " Mandatory option for launch the cli in cron " ,
2016-04-11 16:32:24 +02:00
));
2016-11-29 17:03:57 +01:00
$opts -> parse ();
2013-08-26 07:08:21 +00:00
} catch ( Zend_Console_Getopt_Exception $e ) {
2016-04-11 16:32:24 +02:00
$displayUsage = true ;
2013-08-26 07:08:21 +00:00
}
2016-04-11 16:32:24 +02:00
// --- Aide / Options
if ( count ( $opts -> getOptions ()) == 0 || isset ( $opts -> help )) {
$displayUsage = true ;
}
// --- Usage
if ( $displayUsage ) {
echo " Execute prestation SFR. \n " ;
echo $opts -> getUsageMessage ();
exit ;
2013-08-26 07:08:21 +00:00
}
require_once APPLICATION_PATH . '/configs/config.php' ;
2013-10-02 15:29:06 +00:00
$client = 'sfrbtr' ;
2013-09-16 10:51:57 +00:00
$typeDepot = 'SFTP' ;
$prestation = 'FICH_RCE' ;
2013-09-02 10:12:16 +00:00
//Input path
2016-01-26 13:50:08 +00:00
$pathIn = $c -> profil -> path -> shared .
2016-11-29 17:03:57 +01:00
DIRECTORY_SEPARATOR . 'clients' .
DIRECTORY_SEPARATOR . $client .
DIRECTORY_SEPARATOR . 'send' ;
2013-09-02 10:12:16 +00:00
//Output path
2016-01-26 13:50:08 +00:00
$pathOut = $c -> profil -> path -> shared .
2016-11-29 17:03:57 +01:00
DIRECTORY_SEPARATOR . 'clients' .
DIRECTORY_SEPARATOR . $client .
DIRECTORY_SEPARATOR . 'recv' ;
2013-09-02 10:12:16 +00:00
2013-09-20 14:55:37 +00:00
/*
* File Configuration
* Possible encoding :
* Windows - 1252 //TRANSLIT
* ISO - 8859 - 15 //TRANSLIT
*
*/
2013-08-26 07:08:21 +00:00
$fileOptionsIn = array (
2016-11-29 17:03:57 +01:00
'dir' => $pathIn ,
'type' => 'csv' ,
'delimiter' => ';' ,
'enclosure' => '"' ,
'header' => array ( 'idDemande' , 'SSAEmetteur' ),
'columns' => array ( 'siren' , 'nbLignes' , 'dateContrat' , 'nbContrats' , 'ir' ),
'encoding' => 'ISO-8859-15' ,
2013-08-26 07:08:21 +00:00
);
$fileOkOptionsOut = array (
2016-11-29 17:03:57 +01:00
'dir' => $pathOut ,
'type' => 'csv' ,
'delimiter' => ';' ,
'enclosure' => '"' ,
'header' => array ( 'idDemande' , 'SSAEmetteur' ),
'columns' => array ( 'siren' , 'idVOR' , 'PO' , 'commentaire' ),
'name' => 'FICH_RCE_RETOUR_' ,
'encoding' => 'ISO-8859-15' ,
2013-08-26 07:08:21 +00:00
);
$fileErrorOptionsOut = array (
2016-11-29 17:03:57 +01:00
'dir' => $pathOut ,
'type' => 'csv' ,
'delimiter' => ';' ,
'enclosure' => '"' ,
'header' => array ( 'idDemande' , 'SSAEmetteur' ),
'columns' => array ( 'siren' , 'code' ),
'name' => 'FICH_RCE_ERREUR_' ,
'encoding' => 'ISO-8859-15' ,
2013-08-26 07:08:21 +00:00
);
2013-09-20 12:22:31 +00:00
$filePortefeuilleOptionsOut = array (
2016-11-29 17:03:57 +01:00
'dir' => $pathOut ,
'type' => 'csv' ,
'delimiter' => ',' ,
'enclosure' => '"' ,
'header' => array (),
'columns' => array (
'loginUti' ,
'source' ,
'email' ,
'siren' ,
'nic' ,
'ref' ,
'dateAjout' ,
'rs' ,
'cp' ,
'ville' ,
'dateDerEnvoi' ,
'encoursClient' ,
'actif' ,
'procol' ,
'indiScore20' ,
'encours' ,
'indiScoreDate' ,
'dateBilan' ,
'indiScore20Pre' ,
'encoursPre' ,
'indiScoreDatePre' ,
'sourceModif' ,
'scoreSolv' ,
'scoreSolvPre' ,
'scoreDir' ,
'scoreDirPre' ,
'scoreConf' ,
'scoreConfPre' ,
'cj' ,
'capital' ,
'capitalDev' ,
'ape_entrep' ,
'tca' ,
'teff_entrep' ,
'dateUpdate' ,
),
'name' => 'CLIENTSINDISCORE_' ,
'encoding' => 'ISO-8859-15' ,
2013-09-20 12:22:31 +00:00
);
2013-08-28 15:07:16 +00:00
// => Debug Mode - Create file with debug values
2016-11-29 17:03:57 +01:00
if ( $opts -> manual ) {
if ( ! empty ( $opts -> manual ) && $opts -> manual !== true ) {
$filepath = $opts -> manual ;
$pathOut = dirname ( realpath ( $filepath ));
} else {
$pathOut = dirname ( __FILE__ );
}
$fileOptionsIn [ 'dir' ] = $pathOut ;
$fileErrorOptionsOut [ 'dir' ] = $pathOut ;
$fileOkOptionsOut [ 'dir' ] = $pathOut ;
$dateBegin = date ( 'YmdHis' );
$filesOutOptions = array (
'ok' => $fileOkOptionsOut ,
'error' => $fileErrorOptionsOut
);
2013-08-26 07:08:21 +00:00
}
// => Automatic mode
2016-11-29 17:03:57 +01:00
if ( $opts -> cron ) {
/*
* Detecter si la date n ' est pas trop ancienne
*/
$dateNow = new DateTime ();
$dateIn1 = $dateNow -> format ( 'Y-m-d H:i:s' );
$dateNow -> sub ( new DateInterval ( 'PT20H' ));
$dateIn2 = $dateNow -> format ( 'Y-m-d H:i:s' );
//client, name, depotType, depotDate, depotFile, nbLines, dateInsert, dateExecute
$fluxinSql = " SELECT * FROM sdv1.flux_filein
2016-11-03 09:54:51 +01:00
WHERE client =: client AND name =: prestation AND depotType =: typeDepot
AND dateExecute = '0000-00-00 00:00:00' AND dateInsert BETWEEN : dateIn2 AND : dateIn1
ORDER BY dateInsert DESC " ;
2016-11-29 17:03:57 +01:00
try {
$stmt = $conn -> prepare ( $fluxinSql );
$stmt -> bindValue ( 'client' , $client );
$stmt -> bindValue ( 'prestation' , $prestation );
$stmt -> bindValue ( 'typeDepot' , $typeDepot );
$stmt -> bindValue ( 'dateIn2' , $dateIn2 );
$stmt -> bindValue ( 'dateIn1' , $dateIn1 );
$stmt -> execute ();
} catch ( \Doctrine\DBAL\DBALException $e ) {
echo $e -> getMessage ();
exit ( 1 );
}
if ( $stmt -> rowCount () == 0 ) {
echo date ( 'Y-m-d H:i:s' ) . " : Aucun traitement à réaliser. " . PHP_EOL ;
exit ;
}
$p = $stmt -> fetch ( \PDO :: FETCH_OBJ );
2016-11-03 09:54:51 +01:00
$filepath = $pathIn . DIRECTORY_SEPARATOR . $p -> depotFile ;
2013-08-26 07:08:21 +00:00
2016-11-29 17:03:57 +01:00
if ( ! file_exists ( $filepath )) {
echo date ( 'Y-m-d H:i:s' ) . " : Fichier inexistant ! " . PHP_EOL ;
exit ;
}
2013-08-28 15:07:16 +00:00
2016-11-29 17:03:57 +01:00
$opts -> load = $filepath ;
2013-09-13 15:45:54 +00:00
}
2013-09-02 10:12:16 +00:00
2013-10-02 14:24:09 +00:00
/**
* Chargement du fichier avec premières vérifications
*/
2013-10-02 15:00:57 +00:00
if ( $opts -> load && ! $opts -> evaluate ) {
2013-08-28 15:07:16 +00:00
2016-11-29 17:03:57 +01:00
// Lire et vérifier le fichier
if ( checkFile ( $opts -> load , $fileOptionsIn )) {
// Charger dans la table client AVEC vérification SIREN valide et existe
echo date ( 'Y-m-d H:i:s' ) . " - Début chargement... \n " ;
$seq = execDbIn ( $opts -> load , $fileOptionsIn );
// Marquer les SIREN qui n'existe pas dans la bdd
echo date ( 'Y-m-d H:i:s' ) . " - Marquage des SIREN inexistant \n " ;
$sql = " UPDATE jo.sfr_data SET error=1020 WHERE NumSeq=' " . $seq . " ' AND error=0 AND siren NOT IN (SELECT DISTINCT siren FROM jo.etablissements_old) " ;
try {
$conn -> executeQuery ( $sql );
} catch ( \Doctrine\DBAL\DBALException $e ) {
$sql = " UPDATE jo.sfr_data SET error=1020 WHERE NumSeq=' " . $seq . " ' AND error=0 AND siren NOT IN (SELECT DISTINCT siren FROM jo.etablissements) " ;
$conn -> executeQuery ( $sql );
}
2015-07-20 08:14:39 +00:00
2016-11-29 17:03:57 +01:00
//@todo : Mise à jour du portefeuille CFT (Nom de la prestation)
//Ajouter les SIREN valide qui ne sont pas en surveillance
echo date ( 'Y-m-d H:i:s' ) . " - Mise sous surveillance \n " ;
$sql = " INSERT IGNORE INTO jo.scores_surveillance (siren) (SELECT siren FROM jo.sfr_data WHERE error=0 AND NumSeq=' " . $seq . " ') " ;
$conn -> executeQuery ( $sql );
//Marquer les SIREN qui ne seront pas calculé
/* echo date ( 'Y-m-d H:i:s' ) . " - Marquage des SIREN imposssible à calculer \n " ;
$sql = " UPDATE jo.sfr_data SET error=2020 WHERE NumSeq=' " . $seq . " ' AND error=0 AND siren IN (SELECT siren FROM jo.scores_surveillance WHERE naf='' OR CJ IS NULL OR dateCrea IS NULL OR indiScoreDate='0000-00-00') " ;
$result = $db -> query ( $sql ); */
}
// --- Nettoyage
$date = new DateTime ();
if ( $date -> format ( 'w' ) == 6 ) {
$date -> sub ( new DateInterval ( 'P1D' )) -> setTime ( 23 , 59 , 59 );
echo date ( 'Y-m-d H:i:s' ) . " - Suppression ancienne référence. \n " ;
$sql = " DELETE FROM jo.sfr_data WHERE dateInsert < ' " . $date -> format ( 'Y-m-d H:i:s' ) . " ' " ;
try {
$conn -> executeQuery ( $sql );
} catch ( \Doctrine\DBAL\DBALException $e ) {
}
}
2013-08-26 07:08:21 +00:00
}
2013-10-02 14:24:09 +00:00
/**
* Lancement de l ' évaluation et création des 3 fichiers de sorties
*/
2016-11-29 17:03:57 +01:00
if ( $opts -> evaluate ) {
// Traitement automatique
if ( ! empty ( $opts -> manual ) && $opts -> manual !== true ) {
echo " Traitement Manuel... " . " \n " ;
execFileIn ( $filepath , $fileOptionsIn , array ( 'ok' => $fileOkOptionsOut , 'error' => $fileErrorOptionsOut ), $opts -> debug );
}
// Traitement manuel
else {
$execRef = new Scores_Exec_Ref ( 'SFRINDICATEUR' );
$execId = $execRef -> start ();
$nbLines = 0 ;
// Use Seq Number from command line
if ( $opts -> evaluate !== true ) {
$seq = $opts -> evaluate ;
}
// Guess the last Seq Number
else {
try {
$stmt = $conn -> executeQuery ( 'SELECT NumSeq, dateInsert FROM jo.sfr_data ORDER BY id DESC LIMIT 0,1' );
$result = $stmt -> fetch ( \PDO :: FETCH_OBJ );
} catch ( \Doctrine\DBAL\DBALException $e ) {
echo $e -> getMessage ();
}
$seq = $result -> NumSeq ;
// Insertion de moins de 20 Heures
$dateLimit = new DateTime ();
$dateLimit -> sub ( new DateInterval ( 'PT20H' ));
$dateInsert = DateTime :: createFromFormat ( 'Y-m-d H:i:s' , $result -> dateInsert );
if ( $dateLimit > $dateInsert ) {
echo date ( 'Y-m-d H:i:s' ) . " : Aucun traitement à réaliser (Dernière date : " . $dateInsert -> format ( 'Y-m-d H:i:s' ) . " ). \n " ;
exit ;
}
}
$dateBegin = date ( 'YmdHis' );
// Génération des fichiers
if ( $opts -> cron ) {
// Marquer les SIREN qui ne seront pas calculé
echo date ( 'Y-m-d H:i:s' ) . " - Marquage des SIREN imposssible à calculer \n " ;
$sql = " UPDATE jo.sfr_data SET error=2020 WHERE NumSeq=' " . $seq . " ' AND error=0 AND siren IN (SELECT siren FROM jo.scores_surveillance WHERE naf='' OR CJ IS NULL OR dateCrea IS NULL OR indiScoreDate='0000-00-00') " ;
$conn -> executeQuery ( $sql );
}
// Recherche des siren valides ainsi que leur scores
$sql = 'SELECT sfr.siren, sfr.DateContrat, sfr.IR,' .
' surv.actif, surv.procol, surv.naf, surv.CJ, surv.dateImmat, surv.dateCrea, surv.effectif, surv.indiScore20, surv.indiScoreDate, surv.dateUpdate' .
' FROM jo.sfr_data AS sfr' .
' LEFT JOIN jo.scores_surveillance AS surv ON sfr.siren=surv.siren' .
' WHERE sfr.error=0 AND sfr.NumSeq=\'' . $seq . '\'' ;
echo date ( 'Y-m-d H:i:s' ) . " - Recherche des SIREN valides avec leur score. \n " ;
$stmt = $conn -> executeQuery ( $sql );
$nb = $stmt -> rowCount ();
if ( $nb > 0 ) {
echo date ( 'Y-m-d H:i:s' ) . " - NbLignes= " . $nb . " \n " ;
if ( $execId !== null ) {
$execRef -> total ( $execId , $nb );
}
$file = $fileOkOptionsOut [ 'dir' ] . DIRECTORY_SEPARATOR . $fileOkOptionsOut [ 'name' ] . $dateBegin . '.' . $fileOkOptionsOut [ 'type' ];
// Ouverture fichier RETOUR
$fp = fopen ( $file , 'w' );
$sfrM = new Metier_Sfr_Scoring ();
$sfrM -> notCalculateInError = true ;
// Header
if ( $opts -> debug ) {
$keys = array_keys ( $sfrM -> getValDebug ());
$fileOkOptionsOut [ 'columns' ] = array_merge ( $fileOkOptionsOut [ 'columns' ], $keys );
$line = array ();
foreach ( $fileOkOptionsOut [ 'columns' ] as $i => $column ) {
$line [] = array ( $seq , 'RCE' );
}
} else {
$line = array ( $seq , 'RCE' );
}
fputcsv ( $fp , $line , $fileOkOptionsOut [ 'delimiter' ], $fileOkOptionsOut [ 'enclosure' ]);
$row = 0 ;
// Content
while ( $item = $stmt -> fetch ( \PDO :: FETCH_OBJ )) {
$row ++ ;
echo date ( 'Y-m-d H:i:s' ) . " - Ligne $row : Siren= " . $item -> siren ;
if ( $execId !== null ) {
$execRef -> increment ( $execId , $row );
}
//Score non calculé
if ( $item -> indiScore20 === null ) {
echo " , Non calculé " ;
$conn -> update ( 'jo.sfr_data' , array ( 'error' => 2020 ),
array ( " siren " => $item -> siren , 'NumSeq' => $seq ));
}
//Evaluation
else {
$sfrM = new Metier_Sfr_Scoring ();
$sfrM -> notCalculateInError = true ;
//Set Values
$sfrM -> setVal ( 'ContratDate' , $item -> DateContrat );
$sfrM -> setVal ( 'IR' , $item -> IR );
$sfrM -> setVal ( 'NAF' , $item -> naf );
$sfrM -> setVal ( 'FJ' , $item -> CJ );
$sfrM -> setVal ( 'InseeActif' , $item -> actif );
$sfrM -> setVal ( 'Effectif' , $item -> effectif );
$sfrM -> setVal ( 'Indiscore' , $item -> indiScore20 );
$sfrM -> setVal ( 'DateCalculIndiscore' , $item -> indiScoreDate );
$sfrM -> setVal ( 'DateUpdate' , $item -> dateUpdate );
$sfrM -> setVal ( 'DateCrea' , $item -> dateCrea );
$sfrM -> setVal ( 'DateImmat' , $item -> dateImmat );
$sfrM -> setVal ( 'SituationJuridique' , $item -> procol );
//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 ();
2016-01-06 14:45:15 +00:00
$values [ 'PO' ] = $sfrM -> getPo ();
2013-09-13 15:45:54 +00:00
2016-11-29 17:03:57 +01:00
if ( $opts -> debug ) {
$valuesDebug = $sfrM -> getValDebug ();
$values = array_merge ( $values , $valuesDebug );
}
echo " , ID= " . $values [ 'idVOR' ] . " , commentaire= " . $values [ 'commentaire' ];
//Ecrire fichier RETOUR
$line = array ();
foreach ( $fileOkOptionsOut [ 'columns' ] as $i => $column ) {
$line [] = $values [ $column ];
}
fputcsv ( $fp , $line , $fileOkOptionsOut [ 'delimiter' ], $fileOkOptionsOut [ 'enclosure' ]);
} 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 " );
passthru ( " cp -fv " . $file . " .enc " . $file );
}
2013-09-13 15:45:54 +00:00
2016-11-29 17:03:57 +01:00
$nbLines = $row ;
if ( $opts -> cron ) {
$dateEnd = date ( 'YmdHis' );
$conn -> insert ( 'sdv1.flux_fileout' , array (
'client' => $client ,
'name' => $prestation ,
'nbLines' => $nbLines ,
'dateBegin' => $dateBegin ,
'dateEnd' => $dateEnd ,
'fileOut' => basename ( $file ),
'depotType' => $typeDepot ,
'depotDate' => '0000-00-00 00:00:00' ,
));
// Ecrire la date d'execution dans flux_filein
$conn -> update ( 'sdv1.flux_filein' , array ( 'dateExecute' => $dateEnd ),
array ( 'id' => $p -> id ));
}
2013-09-13 15:45:54 +00:00
2016-11-29 17:03:57 +01:00
if ( $execId !== null ) {
$execRef -> end ( $execId );
}
// Recherche des siren en erreur
$sql = 'SELECT sfr.siren, sfr.error' .
' FROM jo.sfr_data AS sfr' .
' WHERE sfr.error!=0 AND sfr.NumSeq=\'' . $seq . '\'' ;
echo date ( 'Y-m-d H:i:s' ) . " - Recherche des SIREN en erreur. \n " ;
$stmt = $conn -> executeQuery ( $sql );
$nb = $stmt -> rowCount ();
if ( $nb > 0 ) {
echo date ( 'Y-m-d H:i:s' ) . " - NbLignes= " . $nb . " \n " ;
$file = $fileErrorOptionsOut [ 'dir' ] . DIRECTORY_SEPARATOR . $fileErrorOptionsOut [ 'name' ] . $dateBegin . '.' . $fileErrorOptionsOut [ 'type' ];
// Ouverture fichier ERREUR
$fp = fopen ( $file , 'w' );
// Header
$line = array ( $seq , 'RCE' );
fputcsv ( $fp , $line , $fileErrorOptionsOut [ 'delimiter' ], $fileErrorOptionsOut [ 'enclosure' ]);
$row = 0 ;
// Content
while ( $item = $stmt -> fetch ( \PDO :: FETCH_OBJ )) {
$row ++ ;
echo date ( 'Y-m-d H:i:s' ) . " - Ligne $row : Siren= " . $item -> siren ;
$values = array (
'siren' => str_pad ( $item -> siren , 9 , '0' , STR_PAD_LEFT ),
'code' => $item -> error ,
);
//Ecrire fichier ERREUR
$line = array ();
foreach ( $fileErrorOptionsOut [ 'columns' ] as $i => $column ) {
$line [] = $values [ $column ];
}
fputcsv ( $fp , $line , $fileErrorOptionsOut [ 'delimiter' ], $fileErrorOptionsOut [ 'enclosure' ]);
echo " \n " ;
}
fclose ( $fp );
if ( $fileErrorOptionsOut [ 'encoding' ]) {
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 ) {
$dateEnd = date ( 'YmdHis' );
$conn -> insert ( 'sdv1.flux_fileout' , array (
'client' => $client ,
'name' => $prestation ,
'nbLines' => $nbLines ,
'dateBegin' => $dateBegin ,
'dateEnd' => $dateEnd ,
'fileOut' => basename ( $file ),
'depotType' => $typeDepot ,
'depotDate' => '0000-00-00 00:00:00' ,
));
// Ecrire la date d'execution dans flux_filein
$conn -> update ( 'sdv1.flux_filein' , array ( 'dateExecute' => $dateEnd ),
array ( 'id' => $p -> id ));
}
}
//Extraction du fichier PORTEFEUILLE - @todo
$sql = 'SELECT sfr.siren,' .
' surv.actif, surv.procol, surv.indiScore20, surv.encours, surv.indiScoreDate,' .
' surv.dateBilan, surv.indiScore20Pre, surv.encoursPre, surv.indiScoreDatePre, surv.sourceModif,' .
' surv.scoreSolv, surv.scoreSolvPre, surv.scoreDir, surv.scoreDirPre, surv.scoreConf, surv.scoreConfPre,' .
' surv.CJ, surv.naf, surv.effectif, surv.dateUpdate' .
' FROM jo.sfr_data AS sfr, jo.scores_surveillance AS surv' .
' WHERE sfr.siren=surv.siren AND sfr.error=0 AND sfr.NumSeq=\'' . $seq . '\' AND indiScore20 IS NOT NULL' ;
echo date ( 'Y-m-d H:i:s' ) . " - Extraction du portefeuille. \n " ;
$stmt = $conn -> executeQuery ( $sql );
$nb = $stmt -> rowCount ();
if ( $nb > 0 ) {
echo date ( 'Y-m-d H:i:s' ) . " - NbLignes= " . $nb . " \n " ;
$file = $filePortefeuilleOptionsOut [ 'dir' ] . DIRECTORY_SEPARATOR . $filePortefeuilleOptionsOut [ 'name' ] . $dateBegin . '.' . $filePortefeuilleOptionsOut [ 'type' ];
// Ouverture fichier PORTEFEUILLE
$fp = fopen ( $file , 'w' );
// Header
fputcsv ( $fp , $filePortefeuilleOptionsOut [ 'columns' ], $filePortefeuilleOptionsOut [ 'delimiter' ], $filePortefeuilleOptionsOut [ 'enclosure' ]);
$row = 0 ;
// Content
while ( $item = $stmt -> fetch ( \PDO :: FETCH_OBJ )) {
$row ++ ;
$values = array (
'loginUti' => 'AUTO' ,
'source' => 'score' ,
'email' => 'AUTO' ,
'siren' => str_pad ( $item -> siren , 9 , '0' , STR_PAD_LEFT ),
'nic' => '00000' ,
'ref' => 'AUTO' ,
'dateAjout' => '' ,
'rs' => '' ,
'cp' => '' ,
'ville' => '' ,
'dateDerEnvoi' => '' ,
'encoursClient' => '' ,
'actif' => $item -> actif ,
'procol' => $item -> procol ,
'indiScore20' => $item -> indiScore20 ,
'encours' => $item -> encours ,
'indiScoreDate' => $item -> indiScoreDate ,
'dateBilan' => ( $item -> dateBilan == '0000-00-00 00:00:00' ) ? '' : $item -> dateBilan ,
'indiScore20Pre' => $item -> indiScore20Pre ,
'encoursPre' => $item -> encoursPre ,
'indiScoreDatePre' => ( $item -> indiScoreDatePre == '0000-00-00 00:00:00' ) ? '' : $item -> indiScoreDatePre ,
'sourceModif' => $item -> sourceModif ,
'scoreSolv' => $item -> scoreSolv ,
'scoreSolvPre' => $item -> scoreSolvPre ,
'scoreDir' => $item -> scoreDir ,
'scoreDirPre' => $item -> scoreDirPre ,
'scoreConf' => $item -> scoreConf ,
'scoreConfPre' => $item -> scoreConfPre ,
'cj' => $item -> CJ ,
'capital' => '' ,
'capitalDev' => '' ,
'ape_entrep' => $item -> naf ,
'tca' => '' ,
'teff_entrep' => $item -> effectif ,
'dateUpdate' => $item -> dateUpdate ,
);
//Ecrire fichier POTEFEUILLE
$line = array ();
foreach ( $filePortefeuilleOptionsOut [ 'columns' ] as $i => $column ) {
$line [] = $values [ $column ];
}
fputcsv ( $fp , $line , $filePortefeuilleOptionsOut [ 'delimiter' ], $filePortefeuilleOptionsOut [ 'enclosure' ]);
}
fclose ( $fp );
if ( $filePortefeuilleOptionsOut [ 'encoding' ]) {
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 ) {
$dateEnd = date ( 'YmdHis' );
$conn -> insert ( 'sdv1.flux_fileout' , array (
'client' => $client ,
'name' => $prestation ,
'nbLines' => $nbLines ,
'dateBegin' => $dateBegin ,
'dateEnd' => $dateEnd ,
'fileOut' => basename ( $file ),
'depotType' => $typeDepot ,
'depotDate' => '0000-00-00 00:00:00' ,
));
// Ecrire la date d'execution dans flux_filein
$conn -> update ( 'sdv1.flux_filein' , array ( 'dateExecute' => $dateEnd ),
array ( 'id' => $p -> id ));
}
}
}
2013-09-13 15:45:54 +00:00
}
2013-08-26 07:08:21 +00:00
/**
*
2013-08-27 08:50:25 +00:00
* @ param array $values
2013-08-28 15:07:16 +00:00
* @ param string $seq
2013-08-26 07:08:21 +00:00
* @ param string $debug
2013-08-28 15:07:16 +00:00
* @ return array
2013-08-26 07:08:21 +00:00
*/
2013-08-28 15:07:16 +00:00
function execInternal ( $values , $seq , $debug = false )
2013-08-26 07:08:21 +00:00
{
2016-11-29 17:03:57 +01:00
$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
elseif ( ! $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' ] != '' ) {
2016-10-21 09:38:39 +02:00
$error = 1 ;
$code = 'error' ;
$values [ 'code' ] = '2010' ;
echo " - Erreur 2010 " ;
2016-11-29 17:03:57 +01:00
}
}
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 ,
);
2013-08-26 07:08:21 +00:00
}
2013-09-13 15:45:54 +00:00
2013-08-26 07:08:21 +00:00
/**
*
2013-09-02 10:12:16 +00:00
* @ param string $file
2013-08-26 09:46:25 +00:00
* @ param array $fileOptionsIn
* @ param array $filesOptionsOut
* @ param boolean $debug
2013-09-02 10:12:16 +00:00
* @ param boolean $resume
* @ return array
2013-08-26 07:08:21 +00:00
*/
2013-09-02 10:12:16 +00:00
function execFileIn ( $file , $fileOptionsIn , $filesOptionsOut = array (), $debug = false , $resume = false )
2013-08-26 07:08:21 +00:00
{
2016-11-29 17:03:57 +01:00
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 });
}
}
2013-08-26 07:08:21 +00:00
}
2013-10-01 09:25:57 +00:00
/**
*
* @ param string $filepathIn
* @ param array $fileOptionsIn
* @ return boolean
*/
2016-11-29 17:03:57 +01:00
function checkFile ( $filepathIn , $fileOptionsIn )
2013-10-01 09:25:57 +00:00
{
2016-11-29 17:03:57 +01:00
if ( file_exists ( $filepathIn )) {
if (( $handle = fopen ( $filepathIn , 'r' )) !== false ) {
$row = 0 ;
while (( $data = fgetcsv ( $handle , 0 , $fileOptionsIn [ 'delimiter' ], $fileOptionsIn [ 'enclosure' ])) !== false ) {
$row ++ ;
//Header
if ( count ( $fileOptionsIn [ 'header' ]) > 0 && $row == 1 ) {
if ( count ( $fileOptionsIn [ 'header' ]) != count ( $data )) {
return false ;
}
}
//Content
else {
if ( count ( $fileOptionsIn [ 'columns' ]) != count ( $data )) {
return false ;
}
}
}
}
fclose ( $handle );
2013-09-13 15:45:54 +00:00
2016-11-29 17:03:57 +01:00
return true ;
}
2013-10-01 09:25:57 +00:00
}
2013-09-13 15:45:54 +00:00
2013-08-26 07:08:21 +00:00
/**
2013-09-13 15:45:54 +00:00
* Load in database
2013-08-27 08:50:25 +00:00
* @ param string $file
2013-09-13 15:45:54 +00:00
* @ param array $fileOptionsIn
* @ param array $filesOptionsOut
* @ param boolean $debug
* @ param boolean $resume
* @ return array
2013-08-26 07:08:21 +00:00
*/
2013-09-13 15:45:54 +00:00
function execDbIn ( $file , $fileOptionsIn , $debug = false )
2013-08-26 07:08:21 +00:00
{
2016-11-29 17:03:57 +01:00
//Set the input file path
//$filepathIn = $fileOptionsIn['dir'] . DIRECTORY_SEPARATOR . $file;
$filepathIn = $file ;
//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 date ( 'Y-m-d H:i:s' ) . " - 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 ;
}
}
//Content
else {
//Set values from file
$values = array ();
foreach ( $fileOptionsIn [ 'columns' ] as $i => $column ) {
$values [ $column ] = $data [ $i ];
}
//Exec the process
$values = execDbInternal ( $values );
$conn = Zend_Registry :: get ( 'doctrine' );
$conn -> insert ( 'jo.sfr_data' , array (
'NumSeq' => $header [ 'idDemande' ],
'siren' => $values [ 'siren' ],
'NbLignes' => $values [ 'nbLignes' ],
'NbContrats' => $values [ 'nbContrats' ],
'DateContrat' => $values [ 'dateContrat' ],
'IR' => $values [ 'ir' ],
'IdVOR' => '' ,
'comment' => '' ,
'PO' => 0 ,
'debug' => '' ,
'error' => $values [ 'code' ],
));
}
}
}
fclose ( $handle );
return $header [ 'idDemande' ];
}
2013-09-13 15:45:54 +00:00
}
function execDbInternal ( $values , $debug = false )
{
2016-11-29 17:03:57 +01:00
$error = 0 ;
2013-09-13 15:45:54 +00:00
2016-11-29 17:03:57 +01:00
$siren = $values [ 'siren' ];
2013-09-13 15:45:54 +00:00
2016-11-29 17:03:57 +01:00
echo " $siren " ;
2013-09-13 15:45:54 +00:00
2016-11-29 17:03:57 +01:00
$classInsee = new Metier_Insee_MInsee ();
2013-09-13 15:45:54 +00:00
2016-11-29 17:03:57 +01:00
$values [ 'code' ] = '' ;
2013-09-13 15:45:54 +00:00
2016-11-29 17:03:57 +01:00
//Siren valide - 1010
if ( ! $classInsee -> valideSiren ( $siren )) {
$error = 1 ;
$code = 'error' ;
$values [ 'code' ] = '1010' ;
echo " Erreur 1010. " ;
}
2013-08-26 07:08:21 +00:00
2016-11-29 17:03:57 +01:00
//Siren existant - 1020
/* else if ( ! $classInsee -> sirenExiste ( $siren ) ) {
$error = 1 ;
$code = 'error' ;
$values [ 'code' ] = '1020' ;
echo " - Erreur 1020. " ;
} */
2013-08-26 09:46:25 +00:00
2016-11-29 17:03:57 +01:00
//Date incorrecte - 2010
try {
$dateContrat = DateTime :: createFromFormat ( 'Ymd' , $values [ 'dateContrat' ]);
} catch ( Exception $e ) {
2016-10-21 09:38:39 +02:00
if ( $values [ 'dateContrat' ] != '' ) {
$error = 1 ;
$code = 'error' ;
$values [ 'code' ] = '2010' ;
echo " - Erreur 2010 " ;
}
2016-11-29 17:03:57 +01:00
}
2013-09-13 15:45:54 +00:00
2016-11-29 17:03:57 +01:00
echo PHP_EOL ;
2013-09-13 15:45:54 +00:00
2016-11-29 17:03:57 +01:00
return $values ;
2013-08-26 07:08:21 +00:00
}