batch/bin/dataScoring.php
2016-12-06 10:50:09 +01:00

262 lines
9.5 KiB
PHP

<?php
/**
* Lister les événéments, table => faire une requête SQL
* Lundi au Vendredi : à H du jour, prendre de -24h à H-2
* Samedi : Reprendre les événements de la semaine, du mois ?
* Dimanche : Reprendre les événements du mois
*
* Processus :
* Une fois la liste des sirens obtenues, vérifier dans la table des surveillances l'horodatage du dernier calcul
* Si plus récent faire le calcul
* Si plus vieux suivant
*
*/
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');
require_once APPLICATION_PATH.'/configs/config.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.",
'verbose|v' => "Mode bavard ou debug",
'source-s' => ".....",
'date-s' => "AAAAMMJJ, re-calcul depuis la date",
));
$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 "Scoring.\n";
echo $opts->getUsageMessage();
exit;
}
if (empty($opts->source)) {
echo date('Y-md-d H:i:s') . " - Aucune source.\n";
exit;
}
// --- Date pour la sélection
$dateBegin = null;
$dateEnd = new DateTime();
if ($opts->date) {
$dateBegin = DateTime::createFromFormat('Ymd', $opts->date);
}
// --- Lecture dans la base de données
else {
}
// Date de debut par default
if ($dateBegin === null) {
$dateBegin = new DateTime();
$dateBegin->sub(new DateInterval('P1D'));
// Lundi prendre aussi dimanche
if ($dateBegin->format('w') == 1) {
$dateBegin->sub(new DateInterval('P1D'));
}
}
$dateBegin->setTime(0, 0, 0);
$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;
}
// --- Requetes de selection (toujours avec retour date AS dateEven)
$sourcesList = array(
// --- 'declencheur' => "SQL '{DATEBEGIN}' AND '{DATEEND}'",
// Ajout Insee
'inseeadd' => array(
'declencheur' => 'ajout',
'sql' => "SELECT LPAD(insSIREN,9,0) AS siren, dateInsert AS dateEven FROM insee.insee_even WHERE insSIREN>1000 AND insSIEGE=1 AND insVMAJ='C' AND insEVE IN('CS', 'CC', 'I', 'RS') AND DATE_FORMAT(idFlux, '%Y%m%d%H%i%s') BETWEEN '{DATEBEGIN}' AND '{DATEEND}'",
),
// Scores ajouté manuellement
'manualadd' => array(
'declencheur' => 'ajout',
'sql' => "SELECT LPAD(siren,9,0) AS siren, dateUpdate AS dateEven FROM jo.scores_surveillance WHERE siren>1000 AND dateUpdate BETWEEN '{DATEBEGIN}' AND '{DATEEND}' AND indiScoreDate=0",
),
// Bilan Association
'bilanasso' => array(
'declencheur' => 'bilanasso',
'sql' => "SELECT LPAD(siren,9,0) AS siren,dateInsert AS dateEven FROM jo.asso_bilans WHERE siren>1000 AND dateInsert BETWEEN '{DATEBEGIN}' AND '{DATEEND}'",
),
// Bilan
'bilan' => array(
'declencheur' => 'bilans1',
'sql' => "SELECT LPAD(siren,9,0) AS siren, dateInsert AS dateEven FROM jo.bilans WHERE siren>1000 AND dateInsert BETWEEN '{DATEBEGIN}' AND '{DATEEND}'",
),
// Privilege
'privilege' => array(
'declencheur' => 'privileges',
'sql' => "SELECT LPAD(siren,9,0) AS siren, IF(dateUpdate=0,dateInsert,dateUpdate) AS dateEven FROM jo.greffes_privileges WHERE siren>1000 AND (dateInsert BETWEEN '{DATEBEGIN}' AND '{DATEEND}' OR dateUpdate BETWEEN '{DATEBEGIN}' AND '{DATEEND}')",
),
// Dirigeant
'dirigeant' => array(
'declencheur' => 'dirigeants',
'sql' => "SELECT LPAD(siren,9,0) AS siren, IF(dateUpdate=0,dateInsert,dateUpdate) AS dateEven FROM jo.rncs_dirigeants WHERE siren>1000 AND (dateInsert BETWEEN '{DATEBEGIN}' AND '{DATEEND}' OR dateUpdate BETWEEN '{DATEBEGIN}' AND '{DATEEND}')",
),
// Insee
'insee' => array(
'declencheur' => 'insee',
'sql' => "SELECT LPAD(insSIREN,9,0) AS siren, dateInsert AS dateEven FROM insee.insee_even WHERE insSIREN>1000 AND insSIEGE=1 AND DATE_FORMAT(idFlux, '%Y%m%d%H%i%s') BETWEEN '{DATEBEGIN}' AND '{DATEEND}'",
),
// Bodacc
'bodacc' => array(
'declencheur' => 'bodacc',
'sql' => "SELECT LPAD(siren,9,0) AS siren, IF(dateUpdate=0,dateInsert,dateUpdate) AS dateEven FROM jo.bodacc_detail WHERE siren>1000 AND Rubrique NOT IN('creations','comptes') AND (dateInsert BETWEEN '{DATEBEGIN}' AND '{DATEEND}' OR dateUpdate BETWEEN '{DATEBEGIN}' AND '{DATEEND}')",
),
// Collecte
'collecte' => array(
'declencheur' => 'collecte',
'sql' => "SELECT LPAD(siren,9,0) AS siren, dateInsert AS dateEven FROM jo.annonces WHERE siren>1000 AND dateInsert BETWEEN '{DATEBEGIN}' AND '{DATEEND}'",
),
// Ancien
'ancien' => array(
'declencheur' => 'ancien',
'sql' => "SELECT LPAD(siren,9,0) AS siren, dateUpdate AS dateEven FROM jo.scores_surveillance WHERE siren>1000 AND (DATEDIFF(NOW(), indiScoreDate) > 365) AND procol NOT IN('P') AND dateUpdate < '{DATEBEGIN}'",
),
);
foreach ($sourcesList as $source => $item) {
if ($opts->source !== null && $source != $opts->source) {
continue;
}
$declencheur = $item['declencheur'];
$sql = $item['sql'];
echo date('Y-m-d H:i:s') . " - Selection $declencheur entre " .$dateBegin->format('Y-m-d H:i:s') . " et ". $dateEnd->format('Y-m-d H:i:s')."\n";
// Date Last Declencheur
// @todo : Lire dans une table le dernier declencheur
// --- Remplacement des variables dans la requete
$sql = str_replace(
array(
'{DATEBEGIN}',
'{DATEEND}',
),
array(
$dateBegin->format('YmdHis'),
$dateEnd->format('YmdHis'),
),
$sql
);
// --- Selection
try {
// --- Execution de la requete
$stmtItem = $conn->executeQuery($sql);
} catch (\Doctrine\DBAL\DBALException $e) {
echo date('Y-m-d H:i:s') . " - Erreur ".$e->getMessage().".\n";
exit;
}
$total = $stmtItem->rowCount();
// --- Boucle sur les entités
$cpt = 0;
$cptCalc = 0;
if ($total > 0) {
while ($item = $stmtItem->fetch(\PDO::FETCH_OBJ)) {
$cpt++;
if ($opts->verbose) {
echo date('Y-m-d H:i:s')." - Ligne ".str_pad($cpt, strlen($total), ' ', STR_PAD_LEFT)." / ".$total;
}
// --- Vérification si score déjà calculé
try {
$sql = "SELECT siren, dateUpdate FROM jo.scores_surveillance WHERE siren = :siren";
$stmt = $conn->prepare($sql);
$stmt->bindValue('siren', $item->siren);
$stmt->execute();
} catch (\Doctrine\DBAL\DBALException $e) {
echo " : ".$item->siren." - Erreur ".$e->getMessage()."\n";
continue;
}
$toCalculate = false;
if ($stmt->rowCount() == 0) {
$toCalculate = true;
} else {
$survResult = $stmt->fetch(\PDO::FETCH_OBJ);
if (empty($item->dateEven)) {
if ($opts->verbose) {
echo " : dateEven = NULL\n";
}
continue;
}
if ($survResult->dateUpdate == '0000-00-00 00:00:00') {
$toCalculate = true;
} else {
$calculDate = DateTime::createFromFormat('YmdHis', $survResult->dateUpdate);
$evenDate = DateTime::createFromFormat('YmdHis', $item->dateEven);
if ($evenDate > $calculDate) {
$toCalculate = true;
}
}
}
// --- Calcul
if ($toCalculate) {
require_once 'Metier/Scores/MScores.php';
$tabScore = calculIndiScore($item->siren, 0, false, 0, false, 'scores', $declencheur);
$indiScore = $tabScore['Indiscore'];
$encours = round($tabScore['encours']);
if ($opts->verbose) {
echo " : ".$item->siren." - Calcul $indiScore ($encours)\n";
}
$cptCalc++;
} else {
if ($opts->verbose) {
echo " : ".$item->siren." - Pas de Calcul\n";
}
}
}
}
echo date('Y-m-d H:i:s') . " - Fin $declencheur calcul de $cptCalc / $cpt\n";
}