batch/scripts/calculScoreSurv.php
Michael RICOIS 14c3e6c1e7 Formattage
2015-02-09 08:32:40 +00:00

355 lines
15 KiB
PHP

<?php
/**
* Calcul des scores sur la base S&D
* Déclencheur
* - "bilans1" à 7h
* - "ajout" à 8h, 22h
* - "insee" à 8h
* - "dirigeants" à 9h
* - "bodacc" à 13h
* - "collecte" à 13h30
*
* Crontab
* 0 12 * * 1-5 /home/scores/batch/scripts/calculScoreSurv.php >> /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_STRICT ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
// 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(),
)));
//Use classmap autoloader - useful with opcode and realpath cache
require_once 'Zend/Loader/AutoloaderFactory.php';
require_once 'Zend/Loader/ClassMapAutoloader.php';
Zend_Loader_AutoloaderFactory::factory(array(
'Zend_Loader_ClassMapAutoloader' => array(
__DIR__ . '/../library/Zend/autoload_classmap.php',
__DIR__ . '/../library/Application/autoload_classmap.php',
__DIR__ . '/../library/Scores/autoload_classmap.php',
__DIR__ . '/../application/autoload_classmap.php',
),
'Zend_Loader_StandardAutoloader' => array(
'prefixes' => array(
'Zend' => __DIR__ . '/../library/Zend',
'Application' => __DIR__ . '/../library/Application',
'Scores' => __DIR__ . '/../library/Scores',
'SdMetier' => __DIR__ . '/../library/SdMetier',
'Metier' => __DIR__ . '/../library/Metier',
),
'fallback_autoloader' => true
)
));
// Zend_Application - Use it if you don't have autoloaders
//require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
//Options
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",
));
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
}
//Usage
if( isset($opts->help) || count($opts->getOptions())==0 )
{
echo "Scorer toute la base entreprise S&D.".PHP_EOL;
echo $opts->getUsageMessage();
exit;
}
$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";
$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 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 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 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<DATE(NOW()) OR s.indiScoreDate=0)) GROUP BY siren",
/* IndiScores demandés ce jour */
//'jour' => "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 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 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 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 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 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 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<DATE(NOW()) 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",
);
$tabQueriesOrder = array(
'siren' => " ORDER BY siren ASC LIMIT 10000",
'old' => " ORDER BY s.dateUpdate ASC LIMIT 10000",
);
$queryOrder = 'old';
/**
* Liste des déclencheurs
*/
if ( $opts->list ) {
foreach ( $tabQueries as $key => $query) {
echo "\t- $key".PHP_EOL;
}
exit;
}
$c = new Zend_Config($application->getOptions());
$db = Zend_Db::factory($c->profil->db->metier);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
/**
* 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() . PHP_EOL;
}
}
}
/**
* 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';
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='') {}
$message = '';
echo date('Y/m/d H:i:s') ." - DEBUT du programme de calcul des scores en surveillance...".PHP_EOL;
/**
* 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...".PHP_EOL;
$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...".PHP_EOL;
$nbScoresSans=$nbScoresModif=$nbScoresNouv=$nbScoresProcol=$iRow=$nbRowsTot=0;
$iInsee = new MInsee();
foreach ($tabQueries as $declencheur => $query)
{
//Arrêt de l'execution entre 4h30 ou 7h00
if ( date('Hi')*1>=430 && date('Hi')*1<=700 && !$opts->verbose ) break;
//Arrêt de l'execution à 18h30 pour le déclencheur tout
if ( date('Hi')*1>=1830 && $opts->tout ) break;
//Arrêt de l'execution à 23h45
if ( date('Hi')*1>=2345 ) break;
$tableName = $tableSurvTmp . '_' . $declencheur;
echo date('Y/m/d H:i:s') ." - Copie de la table ".$tableName.PHP_EOL;
try {
$db->query("CREATE TABLE $tableName LIKE jo.scores_surveillance;");
} catch(Zend_Db_Exception $e) {
echo $e->getMessage() . PHP_EOL;
//If the table exist an other scripts is running
$message.= "Lancement du calcul des scores ignoré $declencheur, vérifier qu'il y a bien eu un calcul récemment!".PHP_EOL;
continue;
}
try {
$db->query("INSERT IGNORE INTO $tableName SELECT * FROM jo.scores_surveillance;");
} catch(Zend_Db_Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
echo date('Y/m/d H:i:s') ." - Recherche par la source '$declencheur'...".PHP_EOL;
try {
$res = $db->fetchAll($query . $tabQueriesOrder[$queryOrder]);
} catch(Zend_Db_Exception $e) {
echo $e->getMessage() . PHP_EOL;
exit;
}
$nbRows=count($res);
echo date('Y/m/d H:i:s') ." - Il y a $nbRows scores à recalculer par '$declencheur'...".PHP_EOL;
$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;
$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++;
if ($opts->verbose) echo date('Y/m/d H:i:s') ." - $iRow/$nbRowsTot - Siren $siren : $indiScore / 100 ($encours EUR) inchangé depuis $dateScore (Source=$sourceModif).".PHP_EOL;
} 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) !".PHP_EOL;
}
//Arrêt de l'execution à 19h00
if ( date('Hi')*1>=1900 ) 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() . PHP_EOL;
}
}
/**
* Rapport
*/
$message.= date ('Y/m/d H:i:s') . " - Nombre de scores inchangés .................. $nbScoresSans / $nbRowsTot.".PHP_EOL;
$message.= date ('Y/m/d H:i:s') . " - Nombre de scores inchangés car procol ....... $nbScoresProcol / $nbRowsTot.".PHP_EOL;
$message.= date ('Y/m/d H:i:s') . " - Nombre de scores ajoutés .................... $nbScoresNouv / $nbRowsTot.".PHP_EOL;
$message.= date ('Y/m/d H:i:s') . " - Nombre de scores modifiées .................. $nbScoresModif / $nbRowsTot.".PHP_EOL;
$message.= PHP_EOL;
$nbCalc = $nbScoresSans + $nbScoresProcol + $nbScoresNouv + $nbScoresModif;
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.".PHP_EOL;
}
echo $message;
$duree=round(microtime(true)-$tDeb);
$subject = "[CHARGEMENT] Calculs des IndiScores ($nbCalc/$nbRowsTot en $duree s)";
$mail = new Zend_Mail('UTF-8');
$tr = new Zend_Mail_Transport_Smtp('smtp.celeste.fr');
$mail->setDefaultTransport($tr);
$mail->setBodyText($message);
$mail->setFrom('supportdev@scores-decisions.com', 'Machine');
$mail->addTo('suivi@scores-decisions.com', 'Suivi');
$mail->setSubject($subject);
$mail->send();
echo date('Y/m/d H:i:s') ." - Fin du traitement.".PHP_EOL;