extranet/batch/sendBilanClient.php

145 lines
3.4 KiB
PHP
Raw Normal View History

2011-08-29 15:44:29 +00:00
#!/usr/bin/php -q
<?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(),
)));
/** 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.",
'list' => "Liste les actes en attente disponible sur le FTP et affiche les informations",
'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;
}
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'].'_'.
$infos['format'].$date.'_'.
$infos['bilanDuree'].'_'.
$infos['confidentiel'].'_'.
$infos['utilisateurId'];
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
*/
$dbConfig = new Zend_Config_Ini(APPLICATION_PATH.'/configs/databases.ini');
$db = Zend_Db::factory($dbConfig->db->sdv1);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
/**
* Liste les fichiers qui peuvent être traités
*/
$bilans = new Application_Model_BilanSaisie($db);
$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) {
echo "Fichier manquant, Ref:".$infos['ref']."\n";
}
}
if ($test) exit;
/**
* Envoi sur le FTP
*/
foreach ($tabFichier as $fichier)
{
echo "Envoi du fichier ".$fichier['localFile']." => ".
$fichier['remoteFile'].
" (".$fichier['ref'].")\n";
if ( sendToFtp(PATH_DATA.'/bilanclient/'.$fichier['localFile'], $fichier['remoteFile']) ) {
$bilans->setDateEnvoi($fichier['ref']);
echo "Envoi terminé.\n";
} else {
echo "Erreur d'envoi !\n";
}
}