> /home/scores/batch/shared/sources/log/calculScoreSurv.log * 0 8 * * 0,6 /home/scores/batch/scripts/calculScoreSurv.php >> /home/scores/batch/shared/sources/log/calculScoreSurv.log * * TODO * - Option lecture sur bdd slave et ecriture sur bdd master * - Modifier les requetes sur les sources pour * Sélectionner suivant des règles toutes les x heures les nouveaux éléments sur x heures + 1 * Si déjà calculé du jour, alors on passe sinon on calcul * - Vérifier les éléments déjà calculés si il n'y a pas de changement * * */ error_reporting(E_ALL ^ E_STRICT ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED); // --- 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'); // --- 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.", 'tout|t' => "Calculer tous les scores", 'verbose|v' => "Mode bavard ou debug", 'ancien|a' => "Recalculer tous les scores avec indiscore>0 et un encours à 0 !", 'list|l' => "Lister les déclencheurs existants pour le recalcul des scores", 'declencheur|d=s' => "Lancer le calcul des scores pour le déclencheur XXX", 'max=s' => "Nombre max d'unités à calculer pour un déclencheur", 'nostop' => "Ne pas stopper", 'dbread' => "choix de la database pour la lecture des informations (master, slave)", )); $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 "Scorer toute la base entreprise S&D.\n"; echo $opts->getUsageMessage(); exit; } $strSelect="s.siren, s.actif, s.procol, s.indiScore, s.indiScore20, s.encours, s.indiScoreDate, s.scoreSolv, s.scoreConf, s.scoreDir, s.scoreZ, s.scoreCH, s.scoreAfdcc1, s.scoreAfdcc2, s.scoreAfdcc2note, s.scoreAltman, s.scoreAltmanCote, s.scoreCCF, s.situFi, s.infoNote, s.noteStruct, s.noteFin, s.tendance, s.nbModifs, s.dateUpdate, s.cs, s.csMoisMaj, s.csMoisFin"; $tableSurvTmp = 'tmp.scores_surveillance'; $tabQueries = array( /* Bilans Asso déposé(s) */ 'bilansasso' => "SELECT $strSelect, 'bilansasso' as source FROM ".$tableSurvTmp."_bilansasso s, jo.asso_bilans b WHERE b.siren>1000 AND s.siren=b.siren AND b.dateInsert>s.dateUpdate GROUP BY s.siren", //asso_bilans', 'dateCloture, dateCloture*1 as dateClotureNum', "siren=$siren ORDER BY DESC LIMIT 0,5", true, MYSQL_ASSOC); /* Privileges Infogreffe */ 'privileges' => "SELECT $strSelect, 'privileges' AS source FROM ".$tableSurvTmp."_privileges s, jo.greffes_privileges p WHERE p.siren>1000 AND s.siren=p.siren AND (p.dateInsert>s.dateUpdate OR p.dateUpdate>s.dateUpdate) GROUP BY s.siren", /* Privileges Facto */ //'privileges2' => "SELECT $strSelect, 'privileges2' AS source FROM ".$tableSurvTmp."_privileges2 s, sdv1.ge_cs2 p WHERE p.siren>1000 AND s.siren = p.siren AND (p.dateInsert>=s.dateUpdate OR p.dateConf>=s.dateUpdate) AND p.cs='P' GROUP BY s.siren", /* IMPAYES Facto */ /*'impayes' => "SELECT $strSelect, 'impayes' AS source FROM ".$tableSurvTmp."_impayes s, sdv1.ge_cs2 p WHERE p.siren>1000 AND s.siren = p.siren AND (p.dateInsert>=s.dateUpdate OR p.dateConf>=s.dateUpdate) AND p.cs='I' GROUP BY siren",*/ /* Ajout d'un nouveau score */ 'ajout' => "SELECT $strSelect, 'ajout' AS source FROM ".$tableSurvTmp."_ajout s WHERE s.siren>1000 AND ((DATEDIFF(NOW(),s.indiScoreDate) IS NULL AND s.dateUpdate "SELECT $strSelect, 'jour' AS source FROM ".$tableSurvTmp."_jour s, sdv1.`logs` l WHERE l.siren>1000 AND s.siren=l.siren AND l.page='indiscore' AND l.dateHeure>s.dateUpdate AND s.procol NOT IN('P') GROUP BY siren", /* Dirigeants */ 'dirigeants' => "SELECT $strSelect, 'dirigeants' AS source FROM ".$tableSurvTmp."_dirigeants s, jo.rncs_dirigeants d WHERE d.siren>1000 AND s.siren = d.siren AND (d.dateInsert>s.dateUpdate OR d.dateUpdate>s.dateUpdate) AND (d.dateInsert>'2013-03-15 00:00:00' OR d.dateUpdate>'2013-03-15 00:00:00') GROUP BY s.siren", /* Evenements INSEE */ 'insee' => "SELECT $strSelect, 'insee' as source FROM ".$tableSurvTmp."_insee s, insee.insee_even i WHERE i.insSIREN>1000 AND s.siren=i.insSIREN AND i.dateInsert>s.dateUpdate AND i.insSIEGE=1 GROUP BY s.siren", /* Entreprises RNCS */ //'rncs' => "SELECT $strSelect, 'rncs' as source FROM scores_surveillance_tmp s, rncs_entrep r WHERE s.siren>1000 AND s.siren=r.siren AND (r.dateInsert>s.dateUpdate OR r.dateUpdate>s.dateUpdate) /*AND (d.dateInsert>'2013-03-15 00:00:00' OR d.dateUpdate>'2013-03-15 00:00:00')*/ AND s.procol NOT IN('P') GROUP BY siren", /* Bodacc */ 'bodacc' => "SELECT $strSelect, 'bodacc' as source FROM ".$tableSurvTmp."_bodacc s, jo.bodacc_detail b WHERE b.siren>1000 AND s.siren=b.siren AND( b.dateInsert>s.dateUpdate OR b.dateUpdate>s.dateUpdate) AND b.Rubrique NOT IN('creations','comptes') GROUP BY s.siren", /* Collecte */ 'collecte' => "SELECT $strSelect, 'collecte' as source FROM ".$tableSurvTmp."_collecte s, jo.annonces b WHERE b.siren>1000 AND s.siren=b.siren AND b.dateInsert>s.dateUpdate GROUP BY siren", /* Bilans en base */ 'bilans1' => "SELECT $strSelect, 'bilans1' as source FROM ".$tableSurvTmp."_bilans1 s, jo.bilans b WHERE b.siren>1000 AND s.siren=b.siren AND b.dateInsert>s.dateUpdate AND (DATEDIFF(b.dateExercice,s.dateBilan)>1 OR DATEDIFF(b.dateExercice,s.dateBilan) IS NULL) GROUP BY s.siren", /* Défaut divers Facto (D:Défaut, 24:Groupe en diff, 31:Cessation Annoncée, 50:Terrorisme)*/ //'defaut' => "SELECT $strSelect, 'defaut' AS source FROM ".$tableSurvTmp."_defaut s, sdv1.ge_cs2 p WHERE p.siren>1000 AND s.siren = p.siren AND (p.dateInsert>=s.dateUpdate OR p.dateConf>=s.dateUpdate) AND p.cs IN ('D','24','31','50') GROUP BY s.siren", /* RAS Facto */ //'regulier' => "SELECT $strSelect, 'regulier' AS source FROM ".$tableSurvTmp."_regulier s, sdv1.ge_cs2 p WHERE p.siren>1000 AND s.siren = p.siren AND (p.dateInsert>=s.dateUpdate OR p.dateConf>=s.dateUpdate) AND p.cs='00' AND s.procol NOT IN('P') GROUP BY s.siren", // Rajouter AND (DATEDIFF(NOW(),s.indiScoreDate)>365 // et vérifier si ça en enlève en recalcul régulier /* Scores trop anciens */ 'ancien' => "SELECT $strSelect, 'ancien' AS source FROM ".$tableSurvTmp."_ancien s WHERE s.siren>1000 AND (DATEDIFF(NOW(),s.indiScoreDate)>365) AND s.procol NOT IN('P') AND s.dateUpdate"SELECT $strSelect, 'bilans2' as source, s.dateUpdate FROM scores_surveillance_tmp s, bilans_deposes b WHERE b.siren>1000 AND s.siren=b.siren AND b.dateInsert>s.dateUpdate AND s.procol NOT IN('P') GROUP BY siren", ); $tabQueriesOrder = array( 'siren' => " ORDER BY s.siren ASC LIMIT 5000", 'old' => " ORDER BY s.dateUpdate ASC LIMIT 5000", ); $queryOrder = 'old'; /** * Liste des déclencheurs */ if ( $opts->list ) { foreach ( $tabQueries as $key => $query) { echo "\t- $key\n"; } exit; } $c = new Zend_Config($application->getOptions()); $db = Zend_Db::factory($c->profil->db->metier); Zend_Db_Table_Abstract::setDefaultAdapter($db); // 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); Zend_Registry::set('doctrine', $conn); } catch (\Doctrine\DBAL\DBALException $e) { echo "Connection Database impossible.\n"; exit; } /** * Delete all temporary table */ $day = date('N'); if ( $opts->tout && $day == 6) { foreach ( $tabQueries as $key => $query) { try { $db->query("DROP TABLE IF EXISTS $key;"); } catch(Zend_Db_Adapter_Exception $e) { echo $e->getMessage() . "\n"; } } } /** * Force declencheur */ if ( $opts->declencheur ) { if ( in_array($opts->declencheur, array_keys($tabQueries)) ) { $tabQueries = array($opts->declencheur => $tabQueries[$opts->declencheur]); } else { exit; } } require_once APPLICATION_PATH.'/configs/config.php'; $tDeb = microtime(true); $dateJour = date('Y-m-d'); function wsLog($service, $siret='', $ref='') {} $message = $error = ''; echo date('Y/m/d H:i:s') ." - DEBUT du programme de calcul des scores en surveillance...\n"; /** * Mise en surveillance de l'ensemble des scores */ if ( $opts->tout ) { echo date('Y/m/d H:i:s') ." - Mise en surveillance Scores de l'ensemble des demandes de scoring...\n"; $query = "INSERT IGNORE INTO jo.scores_surveillance(siren) SELECT DISTINCT siren FROM jo.surveillances_site WHERE source='score' AND siren>=100000 AND siren NOT IN (SELECT siren FROM jo.scores_surveillance) "; $db->query($query); } /** * Lancement du programme */ echo date('Y/m/d H:i:s') ." - Recherche des scores susceptibles d'avoir changés par déclencheur...\n"; $nbScoresSans=$nbScoresModif=$nbScoresNouv=$nbScoresProcol=$iRow=$nbRowsTot=0; $iInsee = new Metier_Insee_MInsee(); foreach ($tabQueries as $declencheur => $query) { $tableName = $tableSurvTmp . '_' . $declencheur; echo date('Y/m/d H:i:s') ." - Copie de la table ".$tableName."\n"; try { $db->query("CREATE TABLE $tableName ENGINE = MEMORY SELECT * FROM jo.scores_surveillance;"); //$db->query("ALTER TABLE scores_surveillance_insee ADD PRIMARY KEY(siren), ADD INDEX(dateUpdate)"); } catch(Zend_Db_Exception $e) { echo $e->getMessage() . "\n"; //If the table exist an other scripts is running $error.= "Impossible de lancer le calcul des scores $declencheur, vérifier qu'il y a bien eu un calcul récemment!\n"; continue; } echo date('Y/m/d H:i:s') ." - Recherche par la source '$declencheur'...\n"; try { $res = $db->fetchAll($query . $tabQueriesOrder[$queryOrder]); } catch(Zend_Db_Exception $e) { echo $e->getMessage() . "\n"; exit; } $nbRows=count($res); echo date('Y/m/d H:i:s') ." - Il y a $nbRows scores à recalculer par '$declencheur'...\n"; $tabNbRows[$declencheur] = $nbRows; $nbRowsTot+=$nbRows; $tabNbScoresModif[$declencheur] = 0; if ( $nbRows > 0 ) { foreach ( $res as $entrep ) { $iRow++; //Traitement $siren = $entrep['siren']; if ( !$iInsee->valideSiren($siren) ) continue; $nic=0; $indiScorePre = $entrep['indiScore']*1; $encoursPre = $entrep['encours']*1; $dateScore = str_replace('-','',$entrep['indiScoreDate'])*1; $sourceModif = $entrep['source']; $procol = $entrep['procol']; $nbModifs = ($entrep['nbModifs']*1)+1; //echo date('Y/m/d - H:i:s') ." - Siren $siren : AVANT SCORE".PHP_EOL; require_once 'Metier/Scores/MScores.php'; $tabScore = calculIndiScore($siren, $nic, false, 0, false, 'scores', $declencheur); //echo date('Y/m/d - H:i:s') ." - Siren $siren : APRES SCORE".PHP_EOL; $indiScore = $tabScore['Indiscore']*1; $naf = $tabScore['NafEnt']; $encours = round($tabScore['encours']); if ( $indiScore == $indiScorePre && $encours == $encoursPre && $dateScore!=0 ) { $nbScoresSans++; echo date('Y/m/d H:i:s') ." - $iRow/$nbRowsTot - Siren $siren : $indiScore / 100 ($encours EUR) inchangé depuis $dateScore (Source=$sourceModif).\n"; } else { if ($dateScore == 0) { $nbScoresNouv++; } else { $nbScoresModif++; $tabNbScoresModif[$declencheur]++; } echo date('Y/m/d H:i:s') ." - $iRow/$nbRowsTot - Siren $siren : $indiScorePre=>$indiScore | $encoursPre=>$encours (Source=$sourceModif) !\n"; } //Arrêt de l'execution à 19h00 if ( date('Hi')*1>=1900 && !$opts->nostop) break; // Arret sur indicateur maximum if ( $opts->max && $iRow >= $opts->max) break; } } //Delete the remporary table try { $db->query("DROP TABLE IF EXISTS $tableName;"); } catch(Zend_Db_Adapter_Exception $e) { echo $e->getMessage() . "\n"; } } /** * Rapport */ $duree = round(microtime(true) - $tDeb); $nbCalc = $nbScoresSans + $nbScoresProcol + $nbScoresNouv + $nbScoresModif; $subject = "[CHARGEMENT] Calculs IndiScores"; if ($opts->declencheur) { $subject.= " - Source ".$opts->declencheur; $message = "Calcul par la source ".$opts->declencheur."\n"; } if ($opts->tout) { $subject.= " - Toutes sources"; $message = "Calcul pour toutes les sources\n"; } $message.= "($nbCalc/$nbRowsTot en $duree s)\n"; $message.= $error; $message.= "\n"; $message.= "Nombre de scores inchangés .................. $nbScoresSans / $nbRowsTot.\n"; $message.= "Nombre de scores inchangés car procol ....... $nbScoresProcol / $nbRowsTot.\n"; $message.= "Nombre de scores ajoutés .................... $nbScoresNouv / $nbRowsTot.\n"; $message.= "Nombre de scores modifiées .................. $nbScoresModif / $nbRowsTot.\n"; $message.= "\n"; foreach($tabNbRows as $declencheur => $nbTot) { $nbModifs = $tabNbScoresModif[$declencheur]*1; $message.= "Nombre de modifs par '$declencheur'\t...... $nbModifs / $nbTot.\n"; } // Envoi Mail $mail = new Scores_Mail_Method($c->profil->mail); $mail->setBodyTextC($message); $mail->setFrom('supportdev@scores-decisions.com', 'Machine'); $mail->addTo('suivi@scores-decisions.com', 'Suivi'); $mail->setSubjectC($subject); $mail->execute(); echo date('Y/m/d H:i:s') ." - Fin du traitement.\n";