extranet/scripts/jobs/sendBilanClient.php

225 lines
6.0 KiB
PHP
Raw Normal View History

2011-08-29 17:44:29 +02:00
<?php
// Define path to application directory
defined('APPLICATION_PATH')
2012-12-04 15:35:36 +01:00
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
2011-08-29 17:44:29 +02:00
// Define application environment
defined('APPLICATION_ENV')
2012-12-04 15:35:36 +01:00
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
2011-08-29 17:44:29 +02:00
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
2014-03-14 11:42:09 +01:00
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
2011-08-29 17:44:29 +02:00
)));
2014-03-14 11:42:09 +01:00
//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';
2011-08-29 17:44:29 +02:00
// Create application, bootstrap, and run
$application = new Zend_Application(
2014-03-14 11:42:09 +01:00
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
2011-08-29 17:44:29 +02:00
);
try {
$opts = new Zend_Console_Getopt(
//Options
array(
'help|?' => "Affiche l'aide.",
2012-01-25 11:17:03 +01:00
'list' => "Liste les bilans à saisir",
2011-08-29 17:44:29 +02:00
'send' => "Envoi les fichiers par FTP.",
'file=s' => "Specify a file to upload",
2011-08-29 17:44:29 +02:00
));
$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 11:56:12 +01:00
$report_txt = '';
define ('PATH_DATA', $c->profil->path->data);
2011-12-09 09:01:53 +01:00
2011-08-29 17:44:29 +02:00
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'].'_'.
2011-08-29 17:44:29 +02:00
$infos['format'].$date.'_'.
$infos['bilanDuree'].'_'.
$infos['confidentiel'].'_'.
$infos['utilisateurId'].'_'.$infos['ref'];
2012-06-19 09:27:53 +02:00
2011-08-29 17:44:29 +02: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 17:10:08 +01:00
/**
* Connexion à la base de données
*/
2012-12-04 15:35:36 +01:00
$db = Zend_Db::factory($c->profil->db->sdv1);
2012-01-17 17:10:08 +01:00
Zend_Db_Table_Abstract::setDefaultAdapter($db);
2011-08-29 17:44:29 +02:00
$tabFichier = array();
if ( $opts->send ) {
/**
* Liste les fichiers qui peuvent être traités
*/
$bilans = new Application_Model_BilanSaisie();
$listBilans = $bilans->listBilans();
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";
}
}
2011-08-29 17:44:29 +02:00
}
if ( !empty($opts->file) ) {
$bilans = new Application_Model_BilanSaisie();
$sql = $bilans->select()->where('fichier=?', $opts->file);
$result = $bilans->fetchRow($sql);
if ( $result !== null ) {
$infos = $result->toArray();
$path_parts = pathinfo(PATH_DATA . '/' . $opts->file);
$ext = $path_parts['extension'];
$filename = $infos['ref'].'-'.$infos['siren'];
$tabFichier[] = array(
'ref' => $infos['ref'],
'localFile' => $filename.'.'.$ext,
'remoteFile' => getRemoteFilename($infos).'.'.$ext,
);
}
}
2012-01-25 11:17:03 +01:00
if ($test) {
print_r($tabFichier);
exit;
}
2011-08-29 17:44:29 +02:00
/**
* Envoi sur le FTP
*/
foreach ($tabFichier as $fichier)
{
2012-12-07 12:14:33 +01: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 17:44:29 +02:00
if ( sendToFtp(PATH_DATA.'/bilanclient/'.$fichier['localFile'], $fichier['remoteFile']) ) {
2012-06-19 09:27:53 +02:00
$bilans->setDateEnvoi($fichier['ref']);
2012-12-07 12:14:33 +01:00
$txt = "Envoi terminé.";
2012-12-06 11:56:12 +01:00
$report_txt.= $txt."\n";
echo date('Y-m-d\TH:i:s')." - ".$txt."\n";
2011-08-29 17:44:29 +02:00
} else {
2012-12-07 12:14:33 +01:00
$txt = "Erreur d'envoi !";
2012-12-06 11:56:12 +01:00
$report_txt.= $txt."\n";
echo date('Y-m-d\TH:i:s')." - ".$txt."\n";
2011-08-29 17:44:29 +02:00
}
2012-12-06 11:56:12 +01:00
}
/**
* Rapport
*/
if (empty($report_txt)) {
$report_txt = "Aucun bilan client.";
}
$report_email = $c->profil->mail->email->supportdev;
2012-12-06 11:56:12 +01:00
$report_subject = 'Traitement Bilan Client '.date('Y-m-d H:i:s');
2012-12-06 21:18:26 +01:00
$report_txt.= "\n".__FILE__;
2012-12-06 11:56:12 +01: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 12:27:36 +01:00
echo date('Y-m-d\TH:i:s')." - Erreur lors de l'envoi du rapport !\n";
2012-12-06 11:56:12 +01:00
}