2016-01-15 08:51:50 +00:00
< ? php
/**
* Lister les événéments , table => faire une requête SQL
* Lundi au Vendredi : à H du jour , prendre de - 24 h à H - 2
* Samedi : Reprendre les événements de la semaine , du mois ?
* Dimanche : Reprendre les événements du mois
*
* Processus :
* Une fois la liste des sirens obtenues , vérifier dans la table des surveillances l ' horodatage du dernier calcul
* Si plus récent faire le calcul
* Si plus vieux suivant
*
*/
2016-02-01 20:09:11 +00:00
error_reporting ( E_ALL ^ E_STRICT ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED );
2016-01-15 08:51:50 +00:00
// --- Define path to application directory
defined ( 'APPLICATION_PATH' )
|| define ( 'APPLICATION_PATH' , realpath ( __DIR__ . '/../application' ));
// --- Define application environment
defined ( 'APPLICATION_ENV' )
|| define ( 'APPLICATION_ENV' , ( getenv ( 'APPLICATION_ENV' ) ? getenv ( 'APPLICATION_ENV' ) : 'production' ));
// --- Composer autoload
require_once realpath ( __DIR__ . '/../vendor/autoload.php' );
2016-10-21 02:29:22 +02:00
require_once APPLICATION_PATH . '/configs/config.php' ;
2016-01-15 08:51:50 +00:00
// --- Create application, bootstrap, and run
$application = new Zend_Application ( APPLICATION_ENV , APPLICATION_PATH . '/configs/application.ini' );
// --- Options
$displayUsage = false ;
try {
$opts = new Zend_Console_Getopt ( array (
'help|?' => " Displays usage information. " ,
'verbose|v' => " Mode bavard ou debug " ,
2016-02-01 20:09:11 +00:00
'source-s' => " ..... " ,
'date-s' => " AAAAMMJJ, re-calcul depuis la date " ,
2016-01-15 08:51:50 +00:00
));
$opts -> parse ();
} catch ( Zend_Console_Getopt_Exception $e ) {
$displayUsage = true ;
}
// --- Aide / Options
if ( count ( $opts -> getOptions ()) == 0 || isset ( $opts -> help )) {
$displayUsage = true ;
}
// --- Usage
if ( $displayUsage ) {
echo " Scoring. \n " ;
echo $opts -> getUsageMessage ();
exit ;
}
if ( empty ( $opts -> source )) {
echo date ( 'Y-md-d H:i:s' ) . " - Aucune source. \n " ;
exit ;
}
// --- Date pour la sélection
2016-10-20 15:25:05 +02:00
$dateBegin = null ;
2016-09-30 11:46:43 +02:00
$dateEnd = new DateTime ();
2016-01-15 08:51:50 +00:00
if ( $opts -> date ) {
2016-09-30 11:46:43 +02:00
$dateBegin = DateTime :: createFromFormat ( 'Ymd' , $opts -> date );
2016-10-20 15:25:05 +02:00
}
// --- Lecture dans la base de données
else {
}
// Date de debut par default
if ( $dateBegin === null ) {
2016-09-30 11:46:43 +02:00
$dateBegin = new DateTime ();
$dateBegin -> sub ( new DateInterval ( 'P1D' ));
2016-10-24 14:37:05 +02:00
// Lundi prendre aussi dimanche
if ( $dateBegin -> format ( 'w' ) == 1 ) {
$dateBegin -> sub ( new DateInterval ( 'P1D' ));
}
2016-01-15 08:51:50 +00:00
}
2016-10-20 15:25:05 +02:00
$dateBegin -> setTime ( 0 , 0 , 0 );
2016-01-15 08:51:50 +00:00
$c = new Zend_Config ( $application -> getOptions ());
$db = Zend_Db :: factory ( $c -> profil -> db -> metier );
Zend_Db_Table_Abstract :: setDefaultAdapter ( $db );
2016-10-20 15:25:05 +02:00
// Database
$config = new \Doctrine\DBAL\Configuration ();
$connectionParams = array (
'dbname' => $c -> profil -> db -> metier -> params -> dbname ,
'user' => $c -> profil -> db -> metier -> params -> username ,
'password' => $c -> profil -> db -> metier -> params -> password ,
'host' => $c -> profil -> db -> metier -> params -> host ,
'charset' => 'utf8' ,
'driver' => 'pdo_mysql' ,
);
try {
$conn = \Doctrine\DBAL\DriverManager :: getConnection ( $connectionParams , $config );
2016-11-02 11:42:13 +01:00
Zend_Registry :: set ( 'doctrine' , $conn );
2016-10-20 15:25:05 +02:00
} catch ( \Doctrine\DBAL\DBALException $e ) {
echo " Connection Database impossible. \n " ;
exit ;
}
2016-01-15 08:51:50 +00:00
// --- Requetes de selection (toujours avec retour date AS dateEven)
$sourcesList = array (
// --- 'declencheur' => "SQL '{DATEBEGIN}' AND '{DATEEND}'",
2016-10-20 15:25:05 +02:00
// Ajout Insee
'inseeadd' => array (
'declencheur' => 'ajout' ,
2016-10-21 14:22:05 +02:00
'sql' => " SELECT LPAD(insSIREN,9,0) AS siren, dateInsert AS dateEven FROM insee.insee_even WHERE insSIREN>1000 AND insSIEGE=1 AND insVMAJ='C' AND insEVE IN('CS', 'CC', 'I', 'RS') AND DATE_FORMAT(idFlux, '%Y%m%d%H%i%s') BETWEEN ' { DATEBEGIN}' AND ' { DATEEND}' " ,
2016-10-20 15:25:05 +02:00
),
2016-10-21 02:29:22 +02:00
// Scores ajouté manuellement
2016-10-20 15:25:05 +02:00
'manualadd' => array (
'declencheur' => 'ajout' ,
2016-10-21 02:29:22 +02:00
'sql' => " SELECT LPAD(siren,9,0) AS siren, dateUpdate AS dateEven FROM jo.scores_surveillance WHERE siren>1000 AND dateUpdate BETWEEN ' { DATEBEGIN}' AND ' { DATEEND}' AND indiScoreDate=0 " ,
2016-10-20 15:25:05 +02:00
),
// Bilan Association
'bilanasso' => array (
'declencheur' => 'bilanasso' ,
'sql' => " SELECT LPAD(siren,9,0) AS siren,dateInsert AS dateEven FROM jo.asso_bilans WHERE siren>1000 AND dateInsert BETWEEN ' { DATEBEGIN}' AND ' { DATEEND}' " ,
),
// Bilan
'bilan' => array (
'declencheur' => 'bilans1' ,
'sql' => " SELECT LPAD(siren,9,0) AS siren, dateInsert AS dateEven FROM jo.bilans WHERE siren>1000 AND dateInsert BETWEEN ' { DATEBEGIN}' AND ' { DATEEND}' " ,
),
// Privilege
'privilege' => array (
'declencheur' => 'privileges' ,
'sql' => " SELECT LPAD(siren,9,0) AS siren, IF(dateUpdate=0,dateInsert,dateUpdate) AS dateEven FROM jo.greffes_privileges WHERE siren>1000 AND (dateInsert BETWEEN ' { DATEBEGIN}' AND ' { DATEEND}' OR dateUpdate BETWEEN ' { DATEBEGIN}' AND ' { DATEEND}') " ,
),
// Dirigeant
'dirigeant' => array (
'declencheur' => 'dirigeants' ,
'sql' => " SELECT LPAD(siren,9,0) AS siren, IF(dateUpdate=0,dateInsert,dateUpdate) AS dateEven FROM jo.rncs_dirigeants WHERE siren>1000 AND (dateInsert BETWEEN ' { DATEBEGIN}' AND ' { DATEEND}' OR dateUpdate BETWEEN ' { DATEBEGIN}' AND ' { DATEEND}') " ,
),
// Insee
'insee' => array (
'declencheur' => 'insee' ,
2016-10-21 14:22:05 +02:00
'sql' => " SELECT LPAD(insSIREN,9,0) AS siren, dateInsert AS dateEven FROM insee.insee_even WHERE insSIREN>1000 AND insSIEGE=1 AND DATE_FORMAT(idFlux, '%Y%m%d%H%i%s') BETWEEN ' { DATEBEGIN}' AND ' { DATEEND}' " ,
2016-10-20 15:25:05 +02:00
),
// Bodacc
'bodacc' => array (
'declencheur' => 'bodacc' ,
'sql' => " SELECT LPAD(siren,9,0) AS siren, IF(dateUpdate=0,dateInsert,dateUpdate) AS dateEven FROM jo.bodacc_detail WHERE siren>1000 AND Rubrique NOT IN('creations','comptes') AND (dateInsert BETWEEN ' { DATEBEGIN}' AND ' { DATEEND}' OR dateUpdate BETWEEN ' { DATEBEGIN}' AND ' { DATEEND}') " ,
),
2016-10-21 02:29:22 +02:00
// Collecte
2016-10-20 15:25:05 +02:00
'collecte' => array (
'declencheur' => 'collecte' ,
2016-10-21 02:29:22 +02:00
'sql' => " SELECT LPAD(siren,9,0) AS siren, dateInsert AS dateEven FROM jo.annonces WHERE siren>1000 AND dateInsert BETWEEN ' { DATEBEGIN}' AND ' { DATEEND}' " ,
2016-10-20 15:25:05 +02:00
),
// Ancien
'ancien' => array (
'declencheur' => 'ancien' ,
2016-10-21 02:29:22 +02:00
'sql' => " SELECT LPAD(siren,9,0) AS siren, dateUpdate AS dateEven FROM jo.scores_surveillance WHERE siren>1000 AND (DATEDIFF(NOW(), indiScoreDate) > 365) AND procol NOT IN('P') AND dateUpdate < ' { DATEBEGIN}' " ,
2016-10-20 15:25:05 +02:00
),
2016-01-15 08:51:50 +00:00
);
2016-10-20 15:25:05 +02:00
foreach ( $sourcesList as $source => $item ) {
if ( $opts -> source !== null && $source != $opts -> source ) {
2016-01-15 08:51:50 +00:00
continue ;
}
2016-10-20 15:25:05 +02:00
$declencheur = $item [ 'declencheur' ];
$sql = $item [ 'sql' ];
2016-11-10 13:55:11 +01:00
echo date ( 'Y-m-d H:i:s' ) . " - Selection $declencheur entre " . $dateBegin -> format ( 'YmdHis' ) . " et " . $dateEnd -> format ( 'YmdHis' ) . " \n " ;
2016-10-24 14:28:58 +02:00
2016-10-20 15:25:05 +02:00
// Date Last Declencheur
// @todo : Lire dans une table le dernier declencheur
2016-01-15 08:51:50 +00:00
// --- Remplacement des variables dans la requete
$sql = str_replace (
array (
'{DATEBEGIN}' ,
'{DATEEND}' ,
),
array (
2016-09-30 11:46:43 +02:00
$dateBegin -> format ( 'YmdHis' ),
$dateEnd -> format ( 'YmdHis' ),
2016-01-15 08:51:50 +00:00
),
$sql
);
// --- Selection
try {
// --- Execution de la requete
2016-10-20 15:25:05 +02:00
$stmtItem = $conn -> executeQuery ( $sql );
}
catch ( \Doctrine\DBAL\DBALException $e ) {
2016-01-15 08:51:50 +00:00
echo date ( 'Y-m-d H:i:s' ) . " - Erreur " . $e -> getMessage () . " . \n " ;
exit ;
}
2016-10-20 15:25:05 +02:00
$total = $stmtItem -> rowCount ();
2016-01-15 08:51:50 +00:00
// --- Boucle sur les entités
2016-10-24 14:28:58 +02:00
$cpt = 0 ;
$cptCalc = 0 ;
2016-01-15 08:51:50 +00:00
if ( $total > 0 ) {
2016-10-20 15:25:05 +02:00
while ( $item = $stmtItem -> fetch ( \PDO :: FETCH_OBJ )) {
2016-01-15 08:51:50 +00:00
$cpt ++ ;
2016-10-24 14:28:58 +02:00
if ( $opts -> verbose ) {
echo date ( 'Y-m-d H:i:s' ) . " - Ligne " . str_pad ( $cpt , strlen ( $total ), ' ' , STR_PAD_LEFT ) . " / " . $total ;
}
2016-01-15 08:51:50 +00:00
// --- Vérification si score déjà calculé
try {
2016-10-20 15:25:05 +02:00
$sql = " SELECT siren, dateUpdate FROM jo.scores_surveillance WHERE siren = :siren " ;
$stmt = $conn -> prepare ( $sql );
$stmt -> bindValue ( 'siren' , $item -> siren );
$stmt -> execute ();
}
catch ( \Doctrine\DBAL\DBALException $e ) {
2016-01-15 08:51:50 +00:00
echo " : " . $item -> siren . " - Erreur " . $e -> getMessage () . " \n " ;
continue ;
}
$toCalculate = false ;
2016-10-20 15:25:05 +02:00
if ( $stmt -> rowCount () == 0 ) {
2016-01-15 08:51:50 +00:00
$toCalculate = true ;
2016-10-20 15:25:05 +02:00
}
else {
$survResult = $stmt -> fetch ( \PDO :: FETCH_OBJ );
2016-10-21 02:29:22 +02:00
if ( empty ( $item -> dateEven )) {
2016-10-24 14:28:58 +02:00
if ( $opts -> verbose ) {
echo " : dateEven = NULL \n " ;
}
2016-02-01 20:09:11 +00:00
continue ;
}
2016-09-30 11:46:43 +02:00
$calculDate = DateTime :: createFromFormat ( 'YmdHis' , $survResult -> dateUpdate );
2016-10-21 02:29:22 +02:00
$evenDate = DateTime :: createFromFormat ( 'YmdHis' , $item -> dateEven );
2016-09-30 11:46:43 +02:00
if ( $evenDate > $calculDate ) {
2016-01-15 08:51:50 +00:00
$toCalculate = true ;
}
}
// --- Calcul
2016-10-20 15:25:05 +02:00
if ( $toCalculate ) {
2016-02-25 10:49:36 +00:00
require_once 'Metier/Scores/MScores.php' ;
2016-10-20 15:25:05 +02:00
$tabScore = calculIndiScore ( $item -> siren , 0 , false , 0 , false , 'scores' , $declencheur );
2016-01-15 08:51:50 +00:00
2016-10-20 15:25:05 +02:00
$indiScore = $tabScore [ 'Indiscore' ];
$encours = round ( $tabScore [ 'encours' ]);
2016-10-24 14:28:58 +02:00
if ( $opts -> verbose ) {
echo " : " . $item -> siren . " - Calcul $indiScore ( $encours ) \n " ;
}
$cptCalc ++ ;
2016-10-20 15:25:05 +02:00
}
else {
2016-10-24 14:28:58 +02:00
if ( $opts -> verbose ) {
echo " : " . $item -> siren . " - Pas de Calcul \n " ;
}
2016-01-15 08:51:50 +00:00
}
}
}
2016-10-24 14:28:58 +02:00
echo date ( 'Y-m-d H:i:s' ) . " - Fin $declencheur calcul de $cptCalc / $cpt\n " ;
2016-01-15 08:51:50 +00:00
}