extranet/scripts/jobs/sendBilanClient.php
2014-03-14 10:42:09 +00:00

197 lines
5.2 KiB
PHP

<?php
// 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(),
)));
//Use classmap autoloader - useful with opcode and realpath cache
require_once 'Zend/Loader/AutoloaderFactory.php';
require_once 'Zend/Loader/ClassMapAutoloader.php';
Zend_Loader_AutoloaderFactory::factory(array(
'Zend_Loader_ClassMapAutoloader' => array(
__DIR__ . '/../../library/Zend/autoload_classmap.php',
__DIR__ . '/../../library/Application/autoload_classmap.php',
__DIR__ . '/../../library/Scores/autoload_classmap.php',
__DIR__ . '/../../application/autoload_classmap.php',
),
'Zend_Loader_StandardAutoloader' => array(
'prefixes' => array(
'Zend' => __DIR__ . '/../../library/Zend',
'Application' => __DIR__ . '/../../library/Application',
'Scores' => __DIR__ . '/../../library/Scores',
'Metier' => __DIR__ . '/../../library/Metier',
),
'fallback_autoloader' => true
)
));
// Zend_Application - Use it if you don't have autoloaders
//require_once 'Zend/Application.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|?' => "Affiche l'aide.",
'list' => "Liste les bilans à saisir",
'send' => "Envoi les fichiers par FTP.",
));
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
}
//Usage
if(count($opts->getOptions())==0 || isset($opts->help))
{
echo "Envoi les bilans saisie par les clients";
echo "\n\n";
echo $opts->getUsageMessage();
echo "\n";
exit;
}
$test = false;
if ( isset($opts->list) ) {
$test = true;
}
$c = new Zend_Config($application->getOptions());
Zend_Registry::set('config', $c);
$report_txt = '';
define ('PATH_DATA', $c->profil->path->data);
define('FTP_HOST', 'ftp.scores-decisions.com');
define('FTP_USER', 'bilansext');
define('FTP_PASS', 'j12azt78');
define('FTP_DIR', 'send');
//==> Functions
function getRemoteFilename($infos)
{
Zend_Date::setOptions(array('extend_month' => true));
$dateCloture = new Zend_Date($infos['bilanCloture'], 'dd/MM/yyyy');
$date = $dateCloture->toString('yyyyMMdd');
$file = $infos['siren'].'_'.
$infos['format'].$date.'_'.
$infos['bilanDuree'].'_'.
$infos['confidentiel'].'_'.
$infos['utilisateurId'].'_'.$infos['ref'];
if ($infos['env']=='PRD') {
return $file;
}
return $file.'_'.$infos['env'];
}
function sendToFtp($localFile, $remoteFile)
{
$conn_id = ftp_connect(FTP_HOST);
$login_result = ftp_login($conn_id, FTP_USER, FTP_PASS);
ftp_chdir($conn_id, FTP_DIR);
if (ftp_put($conn_id, $remoteFile, $localFile, FTP_BINARY)) {
return true;
}
ftp_close($conn_id);
return false;
}
//==> Début programme
/**
* Connexion à la base de données
*/
$db = Zend_Db::factory($c->profil->db->sdv1);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
/**
* Liste les fichiers qui peuvent être traités
*/
$bilans = new Application_Model_BilanSaisie();
$listBilans = $bilans->listBilans();
$tabFichier = array();
foreach ($listBilans as $infos)
{
$filename = $infos['ref'].'-'.$infos['siren'];
$extValide = array('pdf', 'tiff');
$fileExist = false;
foreach ($extValide as $ext) {
if (file_exists(PATH_DATA.'/bilanclient/'.$filename.'.'.$ext)) {
$fileExist = true;
$tabFichier[] = array(
'ref' => $infos['ref'],
'localFile' => $filename.'.'.$ext,
'remoteFile' => getRemoteFilename($infos).'.'.$ext,
);
break;
}
}
//Erreur fichier inexistant
if (!$fileExist) {
$txt = "Fichier manquant, Ref:".$infos['ref'];
$report_txt.= $txt."\n";
echo date('Y-m-d\TH:i:s')." - ".$txt."\n";
}
}
if ($test) {
print_r($tabFichier);
exit;
}
/**
* Envoi sur le FTP
*/
foreach ($tabFichier as $fichier)
{
$txt = "Envoi du fichier ".$fichier['localFile']." => ".$fichier['remoteFile']." (".$fichier['ref'].")";
$report_txt.= $txt." - ";
echo date('Y-m-d\TH:i:s')." - ".$txt." - ";
if ( sendToFtp(PATH_DATA.'/bilanclient/'.$fichier['localFile'], $fichier['remoteFile']) ) {
$bilans->setDateEnvoi($fichier['ref']);
$txt = "Envoi terminé.";
$report_txt.= $txt."\n";
echo date('Y-m-d\TH:i:s')." - ".$txt."\n";
} else {
$txt = "Erreur d'envoi !";
$report_txt.= $txt."\n";
echo date('Y-m-d\TH:i:s')." - ".$txt."\n";
}
}
/**
* Rapport
*/
if (empty($report_txt)) {
$report_txt = "Aucun bilan client.";
}
$report_email = $c->profil->mail->email->supportdev;
$report_subject = 'Traitement Bilan Client '.date('Y-m-d H:i:s');
$report_txt.= "\n".__FILE__;
if (mail($report_email, $report_subject, utf8_decode($report_txt))){
echo date('Y-m-d\TH:i:s')." - Rapport envoyé.\n";
} else {
echo date('Y-m-d\TH:i:s')." - Erreur lors de l'envoi du rapport !\n";
}