269 lines
13 KiB
PHP
269 lines
13 KiB
PHP
<?php
|
|
/**
|
|
* 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_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();
|
|
$iDbHisto=new WDB('historiques');
|
|
|
|
$iInsee=new MInsee();
|
|
|
|
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<DATE(NOW()) OR s.indiScoreDate=0)) GROUP BY siren",
|
|
/* 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 */
|
|
'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<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",
|
|
);
|
|
|
|
|
|
/**
|
|
* Liste des déclencheurs
|
|
*/
|
|
if ($listeDeclencheur) {
|
|
foreach($tabQueries as $key=>$querie)
|
|
echo "\t- $key".EOL;
|
|
die();
|
|
}
|
|
|
|
/**
|
|
* Lancement du programme
|
|
*/
|
|
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().'.'.PHP_EOL;
|
|
echo date('Y/m/d - H:i:s') ." - Recherche par la source '$declencheur'...";
|
|
|
|
$res=$iDb->query($query);
|
|
echo mysql_error().PHP_EOL;
|
|
|
|
$nbRows=mysql_num_rows($res);
|
|
$tabNbRows[$declencheur] = $nbRows;
|
|
$nbRowsTot+=$nbRows;
|
|
$tabNbScoresModif[$declencheur] = 0;
|
|
|
|
echo date('Y/m/d - H:i:s') ." - Il y a $nbRows scores à recalculer par '$declencheur'...".EOL;
|
|
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;
|
|
|
|
$indiScore = $tabScore['Indiscore']*1;
|
|
$naf = $tabScore['NafEnt'];
|
|
$encours = round($tabScore['encours']);
|
|
|
|
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;
|
|
} 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) !".EOL;
|
|
}
|
|
if (date('Hi')*1>=430 && date('Hi')*1<=700 &&!$modeDebug) break 2;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* 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.".EOL;
|
|
}
|
|
echo $message;
|
|
|
|
$duree=round(microtime(true)-$tDeb);
|
|
sendMail('supportdev@scores-decisions.com', 'suivi@scores-decisions.com', "[Chargement] Calculs des IndiScores ($nbCalc/$nbRowsTot en $duree s)", $message);
|
|
|
|
echo date('Y/m/d - H:i:s') ." - Fin du traitement.".EOL; |