batch/scripts/clients/SfrSurv.php

139 lines
4.8 KiB
PHP

<?php
/*
* Crontab
* 01 19 * * 1-6 scores php /home/scores/batch/scripts/clients/SfrIndicateur.php --cron --load >> /home/scores/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');
try {
$opts = new Zend_Console_Getopt(
//Options
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) {
echo $e->getUsageMessage();
exit;
}
//Usage
if( isset($opts->help) || count($opts->getOptions())==0 )
{
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);
$seq = null;
// => Automatic mode
if ( $opts->cron ) {
//Guess the last Seq Number
try {
$db->setFetchMode(Zend_Db::FETCH_OBJ);
$result = $db->fetchRow('SELECT NumSeq, dateInsert FROM jo.sfr_data ORDER BY id DESC LIMIT 0,1');
} catch(Zend_Db_Adapter_Exception $e) {
echo $e->getMessage();
}
$seq = $result->NumSeq;
//Vérification date
$dateNow = new Zend_Date();
$dateInsert = new Zend_Date($result->dateInsert, 'yyyy-MM-dd HH:mm:ss');
$diff = $dateNow->sub($dateInsert);
$measure = new Zend_Measure_Time($diff->toValue(), Zend_Measure_Time::HOUR);
if ( $measure->getValue()<20 ) {
echo date('Y-m-d H:i:s')." : Aucun traitement à réaliser (Dernière date : ".$dateInsert->toString('yyyy-MM-dd HH:mm:ss').").".PHP_EOL;
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" . PHP_EOL;
$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 {
$db->query($sql);
} catch (Zend_Db_Exception $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." . PHP_EOL;
$sql = "UPDATE jo.surveillances_listes SET dateSuppr='".date('Y-m-d')."' WHERE idClient='".$idClient."'";
try {
$result = $db->query($sql);
$nbRowsAffected = $result->rowCount();
echo date('Y-m-d H:i:s')." - $nbRowsAffected lignes supprimés." . PHP_EOL;
} catch (Zend_Db_Exception $e) {
echo date('Y-m-d H:i:s')." - Erreur suppression des SIREN." . PHP_EOL;
}
/**
* Mise à jour des SIREN - Confirmation
*/
echo date('Y-m-d H:i:s')." - Marquage de confirmation des SIREN." . PHP_EOL;
$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 {
$result = $db->query($sql);
$nbRowsAffected = $result->rowCount();
echo date('Y-m-d H:i:s')." - $nbRowsAffected lignes confirmées." . PHP_EOL;
} catch (Zend_Db_Exception $e) {
echo date('Y-m-d H:i:s')." - Erreur confirmation des SIREN." . PHP_EOL;
}
}