Traitement avec les fichiers .fin ou .end

This commit is contained in:
Michael RICOIS 2014-03-07 16:44:18 +00:00
parent 805d6b6eaa
commit fbbd02e749

View File

@ -54,31 +54,31 @@ if( isset($opts->help) || count($opts->getOptions())==0 )
//Get the file //Get the file
if ( isset($opts->file) ) if ( isset($opts->file) )
{ {
$c = new Zend_Config($application->getOptions()); $c = new Zend_Config($application->getOptions());
$dateFile = date('YmdHis', filectime($opts->file));
//Get the main directory name in FTP and SFTP //Get the main directory name in FTP and SFTP
$pathParts = pathinfo($opts->file); $pathParts = pathinfo($opts->file);
$filenameIn = $pathParts['basename'];
$filename = $pathParts['basename'];
$extension = ''; $extension = '';
if (array_key_exists('extension', $pathParts) ){ if (array_key_exists('extension', $pathParts) ){
$extension = $pathParts['extension']; $extension = $pathParts['extension'];
} }
$client = basename(dirname($pathParts['dirname'])); $client = basename(dirname($pathParts['dirname']));
$dateFile = date('YmdHis', filectime($opts->file));
$startpos = strlen( $c->profil->path->data . DIRECTORY_SEPARATOR ); $startpos = strlen( $c->profil->path->data . DIRECTORY_SEPARATOR );
if ( 'sftp' == substr($opts->file,$startpos,4) ) { if ( 'sftp' == substr($opts->file, $startpos, 4) ) {
$type = 'SFTP'; $type = 'SFTP';
} elseif ( 'ftp' == substr($opts->file,$startpos,3) ) { $fluxBasePath = $c->profil->path->sftp . '/' . $client;
} elseif ( 'ftp' == substr($opts->file, $startpos, 3) ) {
$type = 'FTP'; $type = 'FTP';
$fluxBasePath = $c->profil->path->ftp . '/' . $client;
} }
$lines = file($opts->file); $OptionsCopyAddDate = false;
$nbLines = count($lines); $OptionsCopyDeleteAfter = false;
$OptionsRunWithEndFile = false;
$copyOptionsAddDate = false;
$copyOptionsDeleteAfter = false;
/** /**
* @todo : links to prestation table and file rules * @todo : links to prestation table and file rules
@ -92,20 +92,48 @@ if ( isset($opts->file) )
break; break;
case 'gefacto': case 'gefacto':
$prestation = 'GEFACTO'; $prestation = 'GEFACTO';
$copyOptionsAddDate = true; $OptionsCopyAddDate = true;
break; break;
} }
if ($copyOptionsAddDate) { //Use ".fin" or ".end" files to do something
$extensionLength = 0; $runExtensions = array('fin', 'end');
if ($extension!='') { if ( in_array( $extension, $runExtensions) ) {
$extensionLength = strlen($extension)+1; if ( $OptionsRunWithEndFile ) {
$filename = substr($filename, 0, strlen($filename)-$extensionLength); $extToDelete = $extension;
$filename = $filename . '_' . date('YmdHis') . '.' . $extension; if (file_exists($fluxBasePath . '/' . $filenameIn)) {
} else { $pathParts = pathinfo($fluxBasePath . '/' . $filenameIn);
$filename = $filename . '_' . date('YmdHis'); $filenameIn = $pathParts['basename'];
} $extension = '';
if (array_key_exists('extension', $pathParts) ){
$extension = $pathParts['extension'];
}
} else {
echo "Fichier inexistant $filenameIn\n";
exit;
}
} else {
exit;
}
}
if ( $OptionsRunWithEndFile && !in_array( $extension, $runExtensions) ) {
exit;
}
$lines = file($fluxBasePath . '/' . $filenameIn);
$nbLines = count($lines);
//Add date to filename
if ( $OptionsCopyAddDate ) {
$extensionLength = 0;
if ( $extension != '' ) {
$extensionLength = strlen($extension)+1;
$filenameOut = substr($filenameIn, 0, strlen($filenameIn)-$extensionLength);
$filenameOut = $filenameOut . '_' . date('YmdHis') . '.' . $extension;
} else {
$filenameOut = $filenameIn . '_' . date('YmdHis');
}
} }
//Prepare mail //Prepare mail
@ -115,7 +143,7 @@ if ( isset($opts->file) )
$txt = "Réception d'un fichier pour traitement\n"; $txt = "Réception d'un fichier pour traitement\n";
$txt.= "Client : $client\n"; $txt.= "Client : $client\n";
$txt.= "Mode de transmission : $type\n"; $txt.= "Mode de transmission : $type\n";
$txt.= "Fichier : ".$opts->file."\n"; $txt.= "Fichier : ".$filenameIn."\n";
$txt.= "Nombre de Lignes : $nbLines\n"; $txt.= "Nombre de Lignes : $nbLines\n";
$mail = new Zend_Mail('UTF-8'); $mail = new Zend_Mail('UTF-8');
@ -134,43 +162,47 @@ if ( isset($opts->file) )
} }
//Execute
$db = Zend_Db::factory($c->profil->db->metier);
Zend_Db_Table::setDefaultAdapter($db);
try {
$fluxM = new Application_Model_Sdv1FluxFileIn();
$fluxM->insert(array(
'client' => $client,
'name' => $prestation,
'depotType' => $type,
'depotDate' => $dateFile,
'depotFile' => $filename,
'nbLines' => $nbLines,
'dateInsert' => date('YmdHis'),
'dateExecute' => '0000-00-00 00:00:00',
));
echo date('Y-m-d H:i:s')." - Enregistrement client:$client fichier:$filename\n";
} catch (Zend_Db_Exception $e) {
echo date('Y-m-d H:i:s')." - ERREUR Enregistrement client:$client fichier:$filename\n";
}
//Copie du fichier //Copie du fichier
$destDir = $c->profil->path->storage . $destDir = $c->profil->path->storage . '/' . $client . '/' . 'send';
DIRECTORY_SEPARATOR . $client .
DIRECTORY_SEPARATOR . 'send';
if ( !is_dir($destDir) ) { if ( !is_dir($destDir) ) {
mkdir($destDir, 0755, true); mkdir($destDir, 0755, true);
} }
if ( copy($opts->file, $destDir. DIRECTORY_SEPARATOR . $filename) ) { if ( copy($fluxBasePath . '/' . $filenameIn, $destDir. '/' . $filenameOut) ) {
echo date('Y-m-d H:i:s')." - Copie du fichier $filename dans $destDir\n"; echo date('Y-m-d H:i:s')." - Copie du fichier $filenameIn dans $destDir\n";
if ($copyOptionsDeleteAfter) { //Execute
unlink($opts->file); $db = Zend_Db::factory($c->profil->db->metier);
} Zend_Db_Table::setDefaultAdapter($db);
try {
$fluxM = new Application_Model_Sdv1FluxFileIn();
$fluxM->insert(array(
'client' => $client,
'name' => $prestation,
'depotType' => $type,
'depotDate' => $dateFile,
'depotFile' => $filenameOut,
'nbLines' => $nbLines,
'dateInsert' => date('YmdHis'),
'dateExecute' => '0000-00-00 00:00:00',
));
echo date('Y-m-d H:i:s')." - Enregistrement client:$client fichier:$filenameOut\n";
} catch (Zend_Db_Exception $e) {
echo date('Y-m-d H:i:s')." - ERREUR Enregistrement client:$client fichier:$filenameOut\n";
}
//Suppression des fichiers
if ( $OptionsCopyDeleteAfter ) {
unlink( $fluxBasePath . '/' . $filenameIn );
if ( $OptionsRunWithEndFile ) {
unlink( $fluxBasePath . '/' . $filenameIn . '.' . $extToDelete );
}
}
} else { } else {
echo date('Y-m-d H:i:s')." - ERREUR Copie du fichier $filename dans $destDir\n"; echo date('Y-m-d H:i:s')." - ERREUR Copie du fichier $filenameIn dans $destDir\n";
} }
} }