batch/scripts/scoring.php

173 lines
5.7 KiB
PHP
Raw Normal View History

<?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
*
*/
// --- 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');
// --- 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' => ".....",
'date' => "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
$dateEnd = new Zend_Date();
$dateBegin = new Zend_Date();
if ($opts->date) {
$dateBegin->set($opts->date, 'yyyyMMdd');
} else {
$dateBegin = $dateEnd->subDay(1);
}
$c = new Zend_Config($application->getOptions());
$db = Zend_Db::factory($c->profil->db->metier);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
// --- Requetes de selection (toujours avec retour date AS dateEven)
$sourcesList = array(
// --- 'declencheur' => "SQL '{DATEBEGIN}' AND '{DATEEND}'",
'ajout' => "SELECT LPAD(insSiren,9,0) AS siren FROM insee.insee_even WHERE insSIREN>1000 AND insSIEGE=1 AND insVMAJ='C' AND insEVE='CS' AND dateInsert BETWEEN '{DATEBEGIN}' AND '{DATEEND}'",
);
foreach($sourcesList as $declencheur => $sql)
{
if ($opts->source !== null && $declencheur != $opts->source) {
continue;
}
// --- Remplacement des variables dans la requete
$sql = str_replace(
array(
'{DATEBEGIN}',
'{DATEEND}',
),
array(
$dateBegin->toString('yyyyMMddHHmmss'),
$dateEnd->toString('yyyyMMddHHmmss'),
),
$sql
);
echo $sql;
exit;
// --- Selection
try {
// --- Execution de la requete
$itemResult = $db->fetchAll($sql, null, Zend_Db::FETCH_OBJ);
} catch(Zend_Db_Exception $e) {
echo date('Y-m-d H:i:s') . " - Erreur ".$e->getMessage().".\n";
exit;
}
$total = count($itemResult);
// --- Boucle sur les entités
if ($total > 0) {
$cpt = 0;
foreach($itemResult as $item) {
$cpt++;
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 {
$survM = new Application_Model_JoScoresSurveillance();
$survSql = $survM->select()->from($survM, array())->where('siren=?', $item->siren);
$survResult = $survM->fetchRow($survSql);
} catch(Zend_Db_Exception $e) {
echo " : ".$item->siren." - Erreur ".$e->getMessage()."\n";
continue;
}
$toCalculate = false;
if ($survResult === null) {
$toCalculate = true;
} else {
$calculDate = new Zend_Date();
$calculDate->set($survResult->dateUpdate, 'yyyyMMddHHmmss');
$evenDate = new Zend_Date();
$evenDate->set($itemResult->dateEven, 'yyyyMMddHHmmss');
if ($evenDate->compare($calculDate) == 1) {
$toCalculate = true;
}
}
// --- Calcul
if ( $toCalculate ) {
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';
$tabScore = calculIndiScore($siren, 0, false, 0, false, 'scores', $declencheur);
$indiScore = $tabScore['Indiscore'];
$encours = round($tabScore['encours']);
echo " : ".$item->siren." - Calcul $indiScore ($encours)\n";
} else {
echo " : ".$item->siren." - Pas de Calcul\n";
}
}
}
}