batch/scripts/calculScoreSurv.php

277 lines
14 KiB
PHP
Raw Normal View History

2013-07-22 14:55:04 +00:00
<?php
/**
2013-10-01 09:27:08 +00:00
* Calcul des scores sur la base S&D
* Le script utilise de nombreuses ressources processeurs (1 thread à 100%) et mémoire jusqu'à 4 Go
*
2013-07-22 14:55:04 +00:00
* Crontab
2013-07-30 08:09:17 +00:00
* 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
2013-07-22 14:55:04 +00:00
*/
2013-09-11 10:35:24 +00:00
error_reporting(E_ALL ^ E_STRICT ^ E_NOTICE ^ E_WARNING);
2013-07-22 14:55:04 +00:00
2013-09-17 07:32:56 +00:00
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
2013-07-30 08:09:17 +00:00
// Define application environment
defined('APPLICATION_ENV')
2013-09-17 07:32:56 +00:00
|| 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'
);
//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",
));
$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_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";
2013-09-17 07:32:56 +00:00
$tabQueries = array(
/* Bilans Asso déposé(s) */
2013-09-18 11:54:29 +00:00
'bilansasso' => "SELECT $strSelect, 'bilansasso' as source FROM jo.scores_surveillance_tmp s, jo.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",
2013-09-17 07:32:56 +00:00
//asso_bilans', 'dateCloture, dateCloture*1 as dateClotureNum', "siren=$siren ORDER BY DESC LIMIT 0,5", true, MYSQL_ASSOC);
/* Privileges Infogreffe */
2013-09-18 11:54:29 +00:00
'privileges' => "SELECT $strSelect, 'privileges' AS source FROM jo.scores_surveillance_tmp s, jo.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",
2013-09-17 07:32:56 +00:00
/* 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 */
2013-09-18 11:54:29 +00:00
'ajout' => "SELECT $strSelect, 'ajout' AS source FROM jo.scores_surveillance_tmp s WHERE s.siren>1000 AND ((DATEDIFF(NOW(),s.indiScoreDate) IS NULL AND s.dateUpdate<DATE(NOW()) OR s.indiScoreDate=0)) GROUP BY siren",
2013-09-17 07:32:56 +00:00
/* IndiScores demandés ce jour */
//'jour' => "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 */
2013-09-18 11:54:29 +00:00
'dirigeants' => "SELECT $strSelect, 'dirigeants' AS source FROM jo.scores_surveillance_tmp 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') AND s.procol NOT IN('P') GROUP BY siren",
2013-09-17 07:32:56 +00:00
/* 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 */
2013-09-18 11:54:29 +00:00
'bodacc' => "SELECT $strSelect, 'bodacc' as source FROM jo.scores_surveillance_tmp 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 s.procol NOT IN('P') AND b.Rubrique NOT IN('creations','comptes') GROUP BY siren",
2013-09-17 07:32:56 +00:00
/* Collecte */
2013-09-18 11:54:29 +00:00
'collecte' => "SELECT $strSelect, 'collecte' as source FROM jo.scores_surveillance_tmp s, jo.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",
2013-09-17 07:32:56 +00:00
/* Bilans en base */
2013-09-18 11:54:29 +00:00
'bilans1' => "SELECT $strSelect, 'bilans1' as source FROM jo.scores_surveillance_tmp 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) AND s.procol NOT IN('P') GROUP BY siren",
2013-09-17 07:32:56 +00:00
/* 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",
// Rajouter AND (DATEDIFF(NOW(),s.indiScoreDate)>365
// et vérifier si ça en enlève en recalcul régulier
/* Scores trop anciens */
2013-09-18 11:54:29 +00:00
'ancien' => "SELECT $strSelect, 'ancien' AS source FROM jo.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<DATE(NOW()) GROUP BY siren",
2013-09-17 07:32:56 +00:00
/** @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",
2013-09-11 10:35:24 +00:00
);
2013-09-17 07:32:56 +00:00
/**
* Liste des déclencheurs
*/
if ($opts->list) {
foreach($tabQueries as $key=>$querie)
echo "\t- $key".PHP_EOL;
exit;
}
/**
* Force declencheur
*/
if ($opts->declencheur) {
if ( in_array($opts->declencheur, array_keys($tabQueries)) ) {
$tabQueries = array($opts->declencheur => $tabQueries[$opts->declencheur]);
} else {
exit;
}
}
2013-09-11 10:35:24 +00:00
$c = new Zend_Config($application->getOptions());
$db = Zend_Db::factory($c->profil->db->metier);
2013-09-17 07:32:56 +00:00
Zend_Db_Table_Abstract::setDefaultAdapter($db);
2013-07-22 14:55:04 +00:00
require_once APPLICATION_PATH.'/configs/config.php';
2013-09-17 07:32:56 +00:00
require_once 'framework/fwk.php';
require_once 'framework/common/chiffres.php';
require_once 'framework/common/dates.php';
2013-07-22 14:55:04 +00:00
require_once 'framework/mail/sendMail.php';
2013-09-17 07:32:56 +00:00
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';
2013-07-22 14:55:04 +00:00
$tDeb=microtime(true);
$dateJour=date('Y-m-d');
function wsLog($service, $siret='', $ref='') {}
2013-09-17 07:32:56 +00:00
echo date('Y/m/d - H:i:s') ." - DEBUT du programme de calcul des scores en surveillance...".PHP_EOL;
2013-09-10 20:02:57 +00:00
2013-09-17 07:32:56 +00:00
/**
* 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;
2013-07-22 14:55:04 +00:00
2013-09-17 07:32:56 +00:00
$query="INSERT IGNORE INTO jo.scores_surveillance(siren)
2013-07-22 14:55:04 +00:00
SELECT DISTINCT siren FROM jo.surveillances_site
WHERE source='score' AND siren>=100000 AND siren NOT IN
2013-09-18 11:54:29 +00:00
(SELECT siren FROM jo.scores_surveillance) ";
2013-09-17 07:32:56 +00:00
2013-09-18 11:54:29 +00:00
$res = $db->query($query);
2013-07-22 14:55:04 +00:00
}
2013-09-09 06:33:15 +00:00
/**
* Lancement du programme
*/
2013-09-17 07:32:56 +00:00
echo date('Y/m/d - H:i:s') ." - Recherche des scores susceptibles d'avoir changés par déclencheur...".PHP_EOL;
2013-07-22 14:55:04 +00:00
$nbScoresSans=$nbScoresModif=$nbScoresNouv=$nbScoresProcol=$iRow=$nbRowsTot=0;
2013-09-17 07:32:56 +00:00
$iInsee=new MInsee();
foreach ($tabQueries as $declencheur=>$query)
2013-07-22 14:55:04 +00:00
{
2013-09-17 07:32:56 +00:00
2013-07-22 14:55:04 +00:00
echo date('Y/m/d - H:i:s') ." - Copie de la table scores_surveillance";
2013-09-17 07:32:56 +00:00
try {
2013-09-18 11:54:29 +00:00
$res = $db->query("DROP TABLE IF EXISTS jo.scores_surveillance_tmp;");
2013-09-17 07:32:56 +00:00
} catch(Zend_Db_Adapter_Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
2013-09-18 11:54:29 +00:00
$res = $db->query("CREATE TABLE jo.scores_surveillance_tmp LIKE jo.scores_surveillance;");
2013-09-17 07:32:56 +00:00
} catch(Zend_Db_Adapter_Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
2013-07-22 14:55:04 +00:00
2013-09-17 07:32:56 +00:00
try {
2013-09-18 11:54:29 +00:00
$res = $db->query("INSERT IGNORE INTO jo.scores_surveillance_tmp SELECT * FROM jo.scores_surveillance;");
2013-09-17 07:32:56 +00:00
} catch(Zend_Db_Adapter_Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
2013-07-22 14:55:04 +00:00
echo date('Y/m/d - H:i:s') ." - Recherche par la source '$declencheur'...";
try {
$res = $db->fetchAll($query);
} catch(Zend_Db_Adapter_Exception $e) {
echo $e->getMessage() . PHP_EOL;
exit;
}
2013-09-17 07:32:56 +00:00
$nbRows=count($res);
echo date('Y/m/d - H:i:s') ." - Il y a $nbRows scores à recalculer par '$declencheur'...".PHP_EOL;
2013-09-09 06:33:15 +00:00
$tabNbRows[$declencheur] = $nbRows;
2013-07-22 14:55:04 +00:00
$nbRowsTot+=$nbRows;
2013-09-10 20:02:57 +00:00
$tabNbScoresModif[$declencheur] = 0;
2013-07-22 14:55:04 +00:00
2013-09-17 07:32:56 +00:00
if ( $nbRows > 0 ) {
foreach ( $res as $entrep ) {
$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 ($opts->verbose) echo date('Y/m/d - H:i:s') ." - Ligne $iRow/$nbRowsTot - Siren $siren : $indiScorePre / 100 ($encoursPre EUR) inchangé depuis $dateScore (Source=$sourceModif) car ProCol.".PHP_EOL;
continue;
2013-07-22 14:55:04 +00:00
}
2013-09-17 07:32:56 +00:00
//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') ." - Ligne $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') ." - Ligne $iRow/$nbRowsTot - Siren $siren : $indiScore<>$indiScorePre OU $encours<>$encoursPre (Source=$sourceModif) !".PHP_EOL;
2013-07-22 14:55:04 +00:00
}
2013-09-17 07:32:56 +00:00
if (date('Hi')*1>=430 && date('Hi')*1<=700 && !$opts->verbose) break 2;
2013-07-22 14:55:04 +00:00
}
}
}
2013-09-17 07:32:56 +00:00
/**
2013-09-09 06:33:15 +00:00
* Rapport
*/
2013-09-17 07:32:56 +00:00
$message =date ('Y/m/d - H:i:s') . " - Nombre de scores inchangés .................. $nbScoresSans / $nbRowsTot.".PHP_PHP_EOL;
$message.=date ('Y/m/d - H:i:s') . " - Nombre de scores inchangés car procol ....... $nbScoresProcol / $nbRowsTot.".PHP_PHP_EOL;
$message.=date ('Y/m/d - H:i:s') . " - Nombre de scores ajoutés .................... $nbScoresNouv / $nbRowsTot.".PHP_PHP_EOL;
$message.=date ('Y/m/d - H:i:s') . " - Nombre de scores modifiées .................. $nbScoresModif / $nbRowsTot.".PHP_PHP_EOL;
$message.=PHP_PHP_EOL;
2013-09-10 20:02:57 +00:00
$nbCalc = $nbScoresSans + $nbScoresProcol + $nbScoresNouv + $nbScoresModif;
2013-07-22 14:55:04 +00:00
foreach($tabNbRows as $declencheur=>$nbTot) {
$nbModifs=$tabNbScoresModif[$declencheur]*1;
2013-09-17 07:32:56 +00:00
$message.=date ('Y/m/d - H:i:s') . " - Nombre de modifs par '$declencheur'\t...... $nbModifs / $nbTot.".PHP_EOL;
2013-07-22 14:55:04 +00:00
}
echo $message;
$duree=round(microtime(true)-$tDeb);
2013-09-17 07:32:56 +00:00
sendMail('supportdev@scores-decisions.com', 'suivi@scores-decisions.com', "[CHARGEMENT] Calculs des IndiScores ($nbCalc/$nbRowsTot en $duree s)", $message);
2013-07-22 14:55:04 +00:00
2013-09-17 07:32:56 +00:00
echo date('Y/m/d - H:i:s') ." - Fin du traitement.".PHP_EOL;