extranet/scripts/jobs/sendBilanClient.php

176 lines
4.2 KiB
PHP
Raw Normal View History

2011-08-29 15:44:29 +00:00
<?php
// Define path to application directory
defined('APPLICATION_PATH')
2012-12-04 14:35:36 +00:00
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
2011-08-29 15:44:29 +00:00
// Define application environment
defined('APPLICATION_ENV')
2012-12-04 14:35:36 +00:00
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
2011-08-29 15:44:29 +00:00
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
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.",
2012-01-25 10:17:03 +00:00
'list' => "Liste les bilans à saisir",
2011-08-29 15:44:29 +00:00
'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);
2012-12-06 10:56:12 +00:00
$report_txt = '';
define ('PATH_DATA', $c->profil->path->data);
2011-12-09 08:01:53 +00:00
2011-08-29 15:44:29 +00:00
define('FTP_HOST', 'ftp.scores-decisions.com');
define('FTP_USER', 'bilansext');
define('FTP_PASS', 'j12azt78');
define('FTP_DIR', 'send');
require_once 'common/dates.php';
//==> Functions
function getRemoteFilename($infos)
{
$date = WDate::dateT('d/m/Y', 'Ymd', $infos['bilanCloture']);
$file = $infos['siren'].'_'.
2011-08-29 15:44:29 +00:00
$infos['format'].$date.'_'.
$infos['bilanDuree'].'_'.
$infos['confidentiel'].'_'.
$infos['utilisateurId'].'_'.$infos['ref'];
2012-06-19 07:27:53 +00:00
2011-08-29 15:44:29 +00:00
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
2012-01-17 16:10:08 +00:00
/**
* Connexion à la base de données
*/
2012-12-04 14:35:36 +00:00
$db = Zend_Db::factory($c->profil->db->sdv1);
2012-01-17 16:10:08 +00:00
Zend_Db_Table_Abstract::setDefaultAdapter($db);
2011-08-29 15:44:29 +00:00
/**
* Liste les fichiers qui peuvent être traités
*/
$bilans = new Application_Model_BilanSaisie();
2011-08-29 15:44:29 +00:00
$listBilans = $bilans->listBilans();
$tabFichier = array();
foreach ($listBilans as $infos)
{
$filename = $infos['ref'].'-'.$infos['siren'];
$extValide = array('pdf', 'tiff');
$fileExist = false;
2012-06-19 07:27:53 +00:00
foreach ($extValide as $ext) {
2011-08-29 15:44:29 +00:00
if (file_exists(PATH_DATA.'/bilanclient/'.$filename.'.'.$ext)) {
$fileExist = true;
$tabFichier[] = array(
'ref' => $infos['ref'],
'localFile' => $filename.'.'.$ext,
'remoteFile' => getRemoteFilename($infos).'.'.$ext,
);
2012-06-19 07:27:53 +00:00
2011-08-29 15:44:29 +00:00
break;
}
}
//Erreur fichier inexistant
if (!$fileExist) {
2012-12-06 10:56:12 +00:00
$txt = "Fichier manquant, Ref:".$infos['ref'];
$report_txt.= $txt."\n";
echo date('Y-m-d\TH:i:s')." - ".$txt."\n";
2011-08-29 15:44:29 +00:00
}
}
2012-01-25 10:17:03 +00:00
if ($test) {
print_r($tabFichier);
exit;
}
2011-08-29 15:44:29 +00:00
/**
* Envoi sur le FTP
*/
foreach ($tabFichier as $fichier)
{
2012-12-07 11:14:33 +00:00
$txt = "Envoi du fichier ".$fichier['localFile']." => ".$fichier['remoteFile']." (".$fichier['ref'].")";
$report_txt.= $txt." - ";
echo date('Y-m-d\TH:i:s')." - ".$txt." - ";
2011-08-29 15:44:29 +00:00
if ( sendToFtp(PATH_DATA.'/bilanclient/'.$fichier['localFile'], $fichier['remoteFile']) ) {
2012-06-19 07:27:53 +00:00
$bilans->setDateEnvoi($fichier['ref']);
2012-12-07 11:14:33 +00:00
$txt = "Envoi terminé.";
2012-12-06 10:56:12 +00:00
$report_txt.= $txt."\n";
echo date('Y-m-d\TH:i:s')." - ".$txt."\n";
2011-08-29 15:44:29 +00:00
} else {
2012-12-07 11:14:33 +00:00
$txt = "Erreur d'envoi !";
2012-12-06 10:56:12 +00:00
$report_txt.= $txt."\n";
echo date('Y-m-d\TH:i:s')." - ".$txt."\n";
2011-08-29 15:44:29 +00:00
}
2012-12-06 10:56:12 +00:00
}
/**
* Rapport
*/
if (empty($report_txt)) {
$report_txt = "Aucun bilan client.";
}
$report_email = $c->profil->mail->email->supportdev;
2012-12-06 10:56:12 +00:00
$report_subject = 'Traitement Bilan Client '.date('Y-m-d H:i:s');
2012-12-06 20:18:26 +00:00
$report_txt.= "\n".__FILE__;
2012-12-06 10:56:12 +00:00
if (mail($report_email, $report_subject, utf8_decode($report_txt))){
echo date('Y-m-d\TH:i:s')." - Rapport envoyé.\n";
} else {
2013-01-09 11:27:36 +00:00
echo date('Y-m-d\TH:i:s')." - Erreur lors de l'envoi du rapport !\n";
2012-12-06 10:56:12 +00:00
}