> /home/data/sources/log/calculScoreSurv.log * 0 8 * * 0,6 /home/scores/batch/scripts/calculScoreSurv.php >> /home/data/sources/log/calculScoreSurv.log */ error_reporting(E_ALL & ~E_NOTICE); // Define path to application directory defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application')); // Define application environment defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production')); // Ensure library/ is on include_path set_include_path(implode(PATH_SEPARATOR, array( realpath(APPLICATION_PATH . '/../library'), get_include_path(), ))); /** Zend_Application */ require_once 'Zend/Application.php'; // Create application, bootstrap, and run $application = new Zend_Application( APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini' ); require_once APPLICATION_PATH.'/configs/config.php'; require_once 'framework/fwk.php'; require_once 'framework/common/chiffres.php'; require_once 'framework/common/dates.php'; require_once 'framework/mail/sendMail.php'; require_once 'Metier/insee/classMInsee.php'; require_once 'Metier/insee/classMSirene.php'; require_once 'Metier/partenaires/classMBilans.php'; require_once 'Metier/partenaires/classMBourse.php'; require_once 'Metier/partenaires/classMLiens.php'; require_once 'Metier/scores/classMFinancier.php'; require_once 'Metier/scores/classMSolvabilite.php'; require_once 'Metier/partenaires/classMTva.php'; require_once 'Metier/partenaires/classMMap.php'; require_once 'Metier/partenaires/classMGreffes.php'; require_once 'Metier/scores/classMScores.php'; $tDeb=microtime(true); $dateJour=date('Y-m-d'); function wsLog($service, $siret='', $ref='') {} $nbExercices=2; $sqlEncours=$modeDebug=$periAncien=$listeDeclencheur=$forceDeclencheur=false; $strInfoScript='Usage : '.basename($argv[0]). " [OPTION] Scorer toute la base entreprise S&D. Options : -t Calculer tous les scores (*) -v Mode bavard ou debug -r Recalculer tous les scores avec indiscore>0 et un encours à 0 ! -a Calculer tous les anciens ou nouveaux scores -l Lister les déclencheurs existants pour le recalcul des scores -d:XXX Lancer le calcul des scores pour le déclencheur XXX (*) Option par défaut si aucun argument n'est passé. ";/* -e:X Calculer pour les X derniers exercices (3 par défaut) -f Calculer les scores financiers */ for ($i=1; isset($argv[$i]); $i++) { if (substr($argv[$i],0,1)=='-') { switch (strtolower(substr($argv[$i],1,1))) { case 'a': $periAncien=true; break; case 't': break; case 'l': $listeDeclencheur=true; break; case 'd': $forceDeclencheur=strtolower(substr($argv[$i],3)); break; case 'r': $sqlEncours=true; break; case 'v': $modeDebug=true; break; case '-': case '?': die($strInfoScript); break; default: die('Option '. $argv[$i] . ' inconnue !'.EOL); break; } } } echo date('Y/m/d - H:i:s') ." - DEBUT du programme de calcul des scores en surveillance...".EOL; $iDb=new WDB(); $iDb2=new WDB(); $iDbHisto=new WDB('historiques'); $iInsee=new MInsee(); // $iWs=new WsEntreprise(); //$tabScore=@$iWs->getIndiScore(552144503, 0, false); //$tabScore=@$iWs->getIndiScore(27020395, 0, false); //print_r($tabScore);die(); if (!$listeDeclencheur && !$forceDeclencheur) { echo date('Y/m/d - H:i:s') ." - Mise en surveillance Scores de l'ensemble des demandes de scoring...".EOL; $query="INSERT IGNORE INTO scores_surveillance(siren) SELECT DISTINCT siren FROM sdv1.`logs` WHERE dateHeure> (SELECT max(dateUpdate) FROM scores_surveillance) AND page IN ('indiscore','scorecsf') AND siren>100 "; $res=$iDb->query($query); $query="INSERT IGNORE INTO scores_surveillance(siren) SELECT DISTINCT siren FROM jo.surveillances_site WHERE source='score' AND siren>=100000 AND siren NOT IN (SELECT siren FROM scores_surveillance) "; $res=$iDb->query($query); } $strSelect="s.siren, s.actif, s.procol, s.indiScore, s.indiScore20, s.encours, s.indiScoreDate, scoreSolv, scoreConf, scoreDir, scoreZ, scoreCH, scoreAfdcc1, scoreAfdcc2, scoreAfdcc2note, scoreAltman, scoreAltmanCote, scoreCCF, situFi, infoNote, noteStruct, noteFin, tendance, nbModifs, s.dateUpdate, s.cs, s.csMoisMaj, s.csMoisFin"; $tabQueries = array( /* Bilans Asso déposé(s) */ 'bilansasso' => "SELECT $strSelect, 'bilansasso' as source FROM scores_surveillance_tmp s, asso_bilans b WHERE b.siren>1000 AND s.siren=b.siren AND b.dateInsert>s.dateUpdate AND s.procol NOT IN('P') GROUP BY 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 scores_surveillance_tmp s, greffes_privileges p WHERE p.siren>1000 AND s.siren = p.siren AND (p.dateInsert>s.dateUpdate OR p.dateUpdate>s.dateUpdate) AND s.procol NOT IN('P') GROUP BY siren", /* Privileges Facto */ 'privileges2' => "SELECT $strSelect, 'privileges2' AS source FROM jo.scores_surveillance_tmp 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' AND s.procol NOT IN('P') GROUP BY siren", /* IMPAYES Facto */ 'impayes' => "SELECT $strSelect, 'impayes' AS source FROM jo.scores_surveillance_tmp 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' AND s.procol NOT IN('P') GROUP BY siren", /* Ajout d'un nouveau score */ 'ajout' => "SELECT $strSelect, 'ajout' AS source FROM scores_surveillance_tmp s WHERE s.siren>1000 AND ((DATEDIFF(NOW(),s.indiScoreDate) IS NULL AND s.dateUpdate "SELECT $strSelect, 'jour' AS source FROM scores_surveillance_tmp 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 scores_surveillance_tmp s, 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') AND s.procol NOT IN('P') GROUP BY siren", /* Evenements INSEE */ 'insee' => "SELECT $strSelect, 'insee' as source FROM jo.scores_surveillance_tmp s, insee.insee_even i WHERE i.insSIREN>1000 AND s.siren=i.insSIREN AND i.dateInsert>s.dateUpdate AND s.procol NOT IN('P') AND /*i.insEVE IN ('400','410','420','425') AND*/ i.insSIEGE=1 GROUP BY 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 scores_surveillance_tmp s, bodacc_detail b WHERE b.siren>1000 AND s.siren=b.siren AND( b.dateInsert>s.dateUpdate OR b.dateUpdate>s.dateUpdate) AND s.procol NOT IN('P') AND b.Rubrique NOT IN('creations','comptes') GROUP BY siren", /* Collecte */ 'collecte' => "SELECT $strSelect, 'collecte' as source FROM scores_surveillance_tmp s, annonces b WHERE b.siren>1000 AND s.siren=b.siren AND b.dateInsert>s.dateUpdate AND s.procol NOT IN('P') /*AND b.typeEven NOT IN(3200)*/ GROUP BY siren", /* Bilans en base */ 'bilans1' => "SELECT $strSelect, 'bilans1' as source FROM scores_surveillance_tmp s, 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) AND s.procol NOT IN('P') GROUP BY 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 jo.scores_surveillance_tmp 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') AND s.procol NOT IN('P') GROUP BY siren", /* RAS Facto */ 'regulier' => "SELECT $strSelect, 'regulier' AS source FROM jo.scores_surveillance_tmp 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 siren", /* Scores trop anciens */ 'ancien' => "SELECT $strSelect, 'ancien' AS source FROM scores_surveillance_tmp s WHERE s.siren>1000 AND (DATEDIFF(NOW(),s.indiScoreDate)>365 /*OR DATEDIFF(NOW(),s.indiScoreDate) IS NULL*/) AND s.procol NOT IN('P') AND s.dateUpdate "SELECT $strSelect, 'regulier' AS source FROM jo.scores_surveillance_tmp 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 siren", /** @todo A revoir **/ /* Bilans déposés */ // 'bilans2'=>"SELECT s.siren, s.actif, s.procol, s.indiScore, s.indiScore20, s.encours, s.indiScoreDate, scoreSolv, scoreConf, scoreDir, scoreZ, scoreCH, scoreAfdcc1, scoreAfdcc2, scoreAfdcc2note, scoreAltman, scoreAltmanCote, scoreCCF, situFi, infoNote, noteStruct, noteFin, tendance, nbModifs, '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", /* Liens fianciers */ // 'liens'=>"SELECT s.siren, s.actif, s.procol, s.indiScore, s.indiScore20, s.encours, s.indiScoreDate, scoreSolv, scoreConf, scoreDir, scoreZ, scoreCH, scoreAfdcc1, scoreAfdcc2, scoreAfdcc2note, scoreAltman, scoreAltmanCote, scoreCCF, situFi, infoNote, noteStruct, noteFin, tendance, nbModifs, 'liens' as source, s.dateUpdate FROM scores_surveillance_tmp s, liens l WHERE l.siren>1000 AND (s.siren=l.Siren1 OR s.siren=l.Siren2) AND (l.dateInsert>s.dateUpdate OR l.dateUpdate>s.dateUpdate) AND s.procol NOT IN('P') GROUP BY siren", ); /*$query="SELECT s.siren, s.indiScore, s.indiScore20, s.encours, s.indiScoreDate, scoreSolv, scoreConf, scoreDir, scoreZ, scoreCH, scoreAfdcc1, scoreAfdcc2, scoreAfdcc2note, scoreAltman, scoreAltmanCote, scoreCCF, situFi, infoNote, noteStruct, noteFin, tendance, 'bodacc' as source, s.dateUpdate FROM scores_surveillance_tmp s"; $query="SELECT s.siren, s.actif, s.procol, s.indiScore, s.indiScore20, s.encours, s.indiScoreDate, scoreSolv, scoreConf, scoreDir, scoreZ, scoreCH, scoreAfdcc1, scoreAfdcc2, scoreAfdcc2note, scoreAltman, scoreAltmanCote, scoreCCF, situFi, infoNote, noteStruct, noteFin, tendance, nbModifs, 'modifenc' as source, s.dateUpdate, c.cs FROM jo.scores_surveillance_tmp s WHERE s.siren>1000 AND s.actif=1 AND s.procol NOT IN('A','D','P') AND s.indiScore>0 AND s.indiScore20=0 ORDER BY s.siren ASC"; $query="SELECT s.siren, s.actif, s.procol, s.indiScore, s.indiScore20, s.encours, s.indiScoreDate, scoreSolv, scoreConf, scoreDir, scoreZ, scoreCH, scoreAfdcc1, scoreAfdcc2, scoreAfdcc2note, scoreAltman, scoreAltmanCote, scoreCCF, situFi, infoNote, noteStruct, noteFin, tendance, nbModifs, 'collecte' as source, s.dateUpdate FROM scores_surveillance_tmp s, annonces b WHERE b.siren>1000 AND s.siren=b.siren AND b.dateInsert>s.dateUpdate AND s.procol NOT IN('P') AND b.typeEven NOT IN(3200) GROUP BY siren"; if($periAncien) $query="/* Nouveaux scores OU Scores trop anciens * SELECT s.siren, s.actif, s.procol, s.indiScore, s.indiScore20, s.encours, s.indiScoreDate, scoreSolv, scoreConf, scoreDir, scoreZ, scoreCH, scoreAfdcc1, scoreAfdcc2, scoreAfdcc2note, scoreAltman, scoreAltmanCote, scoreCCF, situFi, infoNote, noteStruct, noteFin, tendance, nbModifs, 'ancien' AS source, s.dateUpdate FROM scores_surveillance_tmp s WHERE s.siren>1000 AND (DATEDIFF(now(),s.indiScoreDate)>365 OR DATEDIFF(now(),s.indiScoreDate) IS NULL) AND s.procol NOT IN('P') GROUP BY siren"; */ if ($listeDeclencheur) { foreach($tabQueries as $key=>$querie) echo "\t- $key".EOL; die(); } echo date('Y/m/d - H:i:s') ." - Recherche des scores susceptibles d'avoir changés par déclencheur...".EOL; $nbScoresSans=$nbScoresModif=$nbScoresNouv=$nbScoresProcol=$iRow=$nbRowsTot=0; foreach ($tabQueries as $declencheur=>$query) { if ($forceDeclencheur && $declencheur<>$forceDeclencheur) continue; echo date('Y/m/d - H:i:s') ." - Copie de la table scores_surveillance"; $res=$iDb->query("DROP TABLE IF EXISTS scores_surveillance_tmp;"); echo mysql_error().'.'; $res=$iDb->query("CREATE TABLE scores_surveillance_tmp LIKE scores_surveillance;"); echo mysql_error().'.'; $res=$iDb->query("INSERT IGNORE INTO scores_surveillance_tmp SELECT * FROM scores_surveillance;"); echo mysql_error().'.'.EOL; echo date('Y/m/d - H:i:s') ." - Recherche par la source '$declencheur'..."; $res=$iDb->query($query); echo mysql_error().EOL; $nbRows=mysql_num_rows($res); $nbRowsTot+=$nbRows; @$tabNbRows[$declencheur]++; echo date('Y/m/d - H:i:s') ." - Il y a $nbRows scores à recalculer par '$declencheur'...".EOL; //die(); while($entrep=$iDb->fetch(MYSQL_ASSOC)) { $iRow++; $siren=$entrep['siren']; // indiScorePre, encoursPre, indiScorePreDate',']; 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; if ($procol=='P') { $nbScoresProcol++; if ($modeDebug) echo date('Y/m/d - H:i:s') ." - Ligne $iRow/$nbRowsTot - Siren $siren : $indiScorePre / 100 ($encoursPre EUR) inchangé depuis $dateScore (Source=$sourceModif) car ProCol.".EOL; continue; } //echo date('Y/m/d - H:i:s') ." - Siren $siren : AVANT SCORE".EOL; $tabScore=@calculIndiScore($siren, $nic, false, 0); //echo date('Y/m/d - H:i:s') ." - Siren $siren : APRES SCORE".EOL; //print_r($tabScore); //die(); $indiScore = $tabScore['Indiscore']*1; $naf = $tabScore['NafEnt']; $encours = round($tabScore['encours']); /*if (!$iDb2->update('scores_surveillance', array('naf'=>$naf), "siren=$siren")) { die('prout'); } continue; */ if ( $indiScore == $indiScorePre && $encours == $encoursPre && $dateScore<>0 ) { $nbScoresSans++; if ($modeDebug) echo date('Y/m/d - H:i:s') ." - Ligne $iRow/$nbRowsTot - Siren $siren : $indiScore / 100 ($encours EUR) inchangé depuis $dateScore (Source=$sourceModif).".EOL; $tabUpdate2=array('sourceModif'=>$sourceModif, 'nbModifs'=>$nbModifs); if (!$iDb2->update('scores_surveillance', $tabUpdate2, "siren=$siren")) { print_r($tabUpdate2); die("Erreur SQL sur la mise à jour de la date de dernière vérif du score pour le siren $siren"); } /*print_r($tabUpdate2); echo 'Erreur SQL n° '.mysql_errno().' : '.mysql_error().EOL; */ } else { if ($dateScore==0) { $sourceModif='ajout'; $nbScoresNouv++; } else { $nbScoresModif++; @$tabNbScoresModif[$declencheur]++; } echo date('Y/m/d - H:i:s') ." - Ligne $iRow/$nbRowsTot - Siren $siren : $indiScore<>$indiScorePre OU $encours<>$encoursPre (Source=$sourceModif) !".EOL; $tabUpdate=array( 'siren' => $siren, 'naf' => $naf, 'actif' => $tabScore['Actif'], 'procol' => $tabScore['SituationJuridique'], 'indiScore' => $tabScore['Indiscore'], 'indiScore20' => $tabScore['Indiscore20'], 'encours' => $tabScore['encours'], 'indiScoreDate' => $dateJour, 'dateBilan' => @$tabScore['Bilans'][0]['Millesime'], 'scoreSolv' => $tabScore['ScoreSolvabilite'], 'scoreConf' => $tabScore['ScoreConfor'], 'scoreDir' => $tabScore['ScoreDirigeance'], 'scoreZ' => $tabScore['scores']['Z'], 'scoreCH' => $tabScore['scores']['ConanH'], 'scoreAfdcc1' => $tabScore['scores']['Afdcc1'], 'scoreAfdcc2' => $tabScore['scores']['Afdcc2'], 'scoreAfdcc2note' => $tabScore['scores']['Afdcc2note'], 'scoreAltman' => $tabScore['scores']['Altman'], 'scoreAltmanCote' => $tabScore['scores']['AltmanCote'], 'scoreCCF' => $tabScore['scores']['CCF'], 'situFi' => $tabScore['tabInfosNotations']['SituationFinanciere'], 'infoNote' => $tabScore['tabInfosNotations']['Notation'], 'noteStruct' => $tabScore['Note_Structure'], 'noteFin' => $tabScore['Note_Financiere'], 'tendance' => $tabScore['TendanceIndiscore'], 'cs' => $tabScore['cs'], 'csMoisMaj' => $tabScore['csMoisMaj'], 'csMoisFin' => $tabScore['csMoisFin'], 'adresseDom' => $tabScore['AdresseDom'], 'sourceModif' => $sourceModif, 'indiScorePre' => $entrep['indiScore'], 'indiScore20Pre' => $entrep['indiScore20'], 'indiScoreDatePre' => $entrep['indiScoreDate'], 'encoursPre' => $entrep['encours'], 'scoreSolvPre' => $entrep['scoreSolv'], 'scoreConfPre' => $entrep['scoreConf'], 'scoreDirPre' => $entrep['scoreDir'], 'scoreZPre' => $entrep['scoreZ'], 'scoreCHPre' => $entrep['scoreCH'], 'scoreAfdcc1Pre' => $entrep['scoreAfdcc1'], 'scoreAfdcc2Pre' => $entrep['scoreAfdcc2'], 'scoreAfdcc2notePre'=> $entrep['scoreAfdcc2note'], 'scoreAltmanPre' => $entrep['scoreAltman'], 'scoreAltmanCotePre'=> $entrep['scoreAltmanCote'], 'scoreCCFPre' => $entrep['scoreCCF'], 'situFiPre' => $entrep['situFi'], 'infoNotePre' => $entrep['infoNote'], 'noteStructPre' => $entrep['noteStruct'], 'noteFinPre' => $entrep['noteFin'], 'tendancePre' => $entrep['tendance'], 'csPre' => $entrep['cs'], 'csMoisMajPre' => $entrep['csMoisMaj'], 'csMoisFinPre' => $entrep['csMoisFin'], 'adresseDomPre' => $entrep['adresseDom'], 'nbModifs' => $nbModifs, ); if (!$iDb2->update('scores_surveillance', $tabUpdate, "siren=$siren")) { print_r($tabUpdate); echo mysql_error().EOL; die(date ('Y/m/d - H:i:s') . " - Erreur SQL sur la mise à jour du score pour le siren $siren".EOL); } // Historisation du score if (!$iDbHisto->insert('scores_surveillance', $tabUpdate)) { print_r($tabUpdate); echo mysql_error().EOL; die(date ('Y/m/d - H:i:s') . " - Erreur SQL sur l'historisation du score pour le siren $siren".EOL); } } if (date('Hi')*1>=430 && date('Hi')*1<=700 &&!$modeDebug) break 2; } } $message =date ('Y/m/d - H:i:s') . " - Nombre de scores inchangés .................. $nbScoresSans / $nbRowsTot.".EOL; $message.=date ('Y/m/d - H:i:s') . " - Nombre de scores inchangés car procol ....... $nbScoresProcol / $nbRowsTot.".EOL; $message.=date ('Y/m/d - H:i:s') . " - Nombre de scores ajoutés .................... $nbScoresNouv / $nbRowsTot.".EOL; $message.=date ('Y/m/d - H:i:s') . " - Nombre de scores modifiées .................. $nbScoresModif / $nbRowsTot.".EOL; foreach($tabNbRows as $declencheur=>$nbTot) { $nbModifs=$tabNbScoresModif[$declencheur]*1; $message.=date ('Y/m/d - H:i:s') . " - Nombre de modifs par '$declencheur'\t...... $nbModifs / $nbTot.".EOL; } echo $message; $duree=round(microtime(true)-$tDeb); sendMail('supportdev@scores-decisions.com', 'suivi@scores-decisions.com', "[Chargement] Calculs des IndiScores ($nbRowsTot en $duree s)", $message); echo date('Y/m/d - H:i:s') ." - Fin du traitement.".EOL;