batch/bin/clientSfrSurv.php
Michael RICOIS ce9b83194a PHP-CS-Fixer
2016-11-29 17:04:23 +01:00

162 lines
5.6 KiB
PHP

<?php
/*
* Crontab
* 01 19 * * 1-6 scores php /home/scores/batch/scripts/clients/SfrIndicateur.php --cron --load >> /home/scores/batch/shared/log/SfrSurv.log
*/
error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE & ~E_WARNING);
// --- 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.",
'in' => "",
'seq=s' => "Spécifier le numéro de séquence pour traitement",
'cron' => "Mandatory option for launch the cli in cron",
));
$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 "Execute prestation SFR.\n";
echo $opts->getUsageMessage();
exit;
}
//Chargement des éléments à surveiller
if ($opts->in) {
$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;
}
$execRef = new Scores_Exec_Ref('SFRSURV');
$execId = $execRef->start();
$seq = null;
// => Automatic mode
if ($opts->cron) {
// Guess the last Seq Number
try {
$stmt = $conn->executeQuery('SELECT NumSeq, dateInsert FROM jo.sfr_data ORDER BY id DESC LIMIT 0,1');
$result = $stmt->fetch(\PDO::FETCH_OBJ);
} catch (\Doctrine\DBAL\DBALException $e) {
echo $e->getMessage();
}
$seq = $result->NumSeq;
// Insertion de moins de 20 Heures
$dateLimit = new DateTime();
$dateLimit->sub(new DateInterval('PT20H'));
$dateInsert = DateTime::createFromFormat('Y-m-d H:i:s', $result->dateInsert);
if ($dateLimit > $dateInsert) {
echo date('Y-m-d H:i:s')." : Aucun traitement à réaliser (Dernière date : ".$dateInsert->format('Y-m-d').").\n";
exit;
}
}
if (is_string($opts->seq)) {
$seq = $opts->seq;
}
if ($seq===null) {
echo date('Y-m-d H:i:s')." - Aucune numéro de séquence.\n";
exit;
}
// Add the idClient from CRM Prestation (surveillances_listes.idClient)
$idClient = 'SURBODPRDSFTPSFRBT';
/**
* Ajouter les SIREN qui ne sont pas en surveillance
*/
echo date('Y-m-d H:i:s')." - Ajout des SIREN\n";
$sql = "INSERT IGNORE INTO jo.surveillances_listes (idClient, siren, dateAjout) (
SELECT '".$idClient."', siren, '".date('Y-m-d')."' FROM jo.sfr_data WHERE error=0 AND NumSeq='".$seq."'
AND siren NOT IN (SELECT siren FROM jo.surveillances_listes WHERE idClient='".$idClient."')
)";
try {
$conn->executeQuery($sql);
} catch (\Doctrine\DBAL\DBALException $e) {
echo date('Y-m-d H:i:s')." - Erreur ajout des SIREN.\n";
}
/**
* Marquer les SIREN supprimés
*/
echo date('Y-m-d H:i:s')." - Marquage de suppression des SIREN.\n";
$sql = "UPDATE jo.surveillances_listes SET dateSuppr='".date('Y-m-d')."' WHERE idClient='".$idClient."'";
try {
$stmt = $conn->executeQuery($sql);
$nbRowsAffected = $stmt->rowCount();
echo date('Y-m-d H:i:s')." - $nbRowsAffected lignes supprimés.\n";
} catch (\Doctrine\DBAL\DBALException $e) {
echo date('Y-m-d H:i:s')." - Erreur suppression des SIREN.\n";
}
/**
* Mise à jour des SIREN - Confirmation
*/
echo date('Y-m-d H:i:s')." - Marquage de confirmation des SIREN.\n";
$sql = "UPDATE jo.surveillances_listes AS surv INNER JOIN jo.sfr_data AS sfr ON surv.siren=sfr.siren
SET dateConf='".date('Y-m-d')."', dateSuppr='0000-00-00'
WHERE surv.idClient='".$idClient."' AND sfr.error=0 AND sfr.NumSeq='".$seq."'";
/*$sql = "UPDATE jo.surveillances_listes SET dateConf='".date('Y-m-d')."', dateSuppr='0000-00-00'
WHERE idClient='".$idClient."' AND siren IN (
SELECT CAST(siren AS UNSIGNED) FROM jo.sfr_data WHERE error=0 AND NumSeq='".$seq."'
) ";*/
try {
$stmt = $conn->executeQuery($sql);
$nbRowsAffected = $stmt->rowCount();
echo date('Y-m-d H:i:s')." - $nbRowsAffected lignes confirmées." . PHP_EOL;
} catch (\Doctrine\DBAL\DBALException $e) {
echo date('Y-m-d H:i:s')." - Erreur confirmation des SIREN." . PHP_EOL;
}
if ($execId !== null) {
$execRef->end($execId);
}
}