2014-03-07 16:52:24 +00:00
< ? php
/**
* Chargement des fichiers en provenance de GE FactoFrance
* Les fichiers sont disponible dans le répertoire de stockage avec une date pour historisation
* Liste des fichiers :
* - GESCDACH
* - GESCDCLT
* - GESCDMVT
* Execution entre le 5 et le 8 du mois toutes les 4 heures ?
*/
// Define path to application directory
2014-07-11 06:36:20 +00:00
define ( 'APPLICATION_PATH' , realpath ( dirname ( __FILE__ ) . '/../application' ));
2014-03-07 16:52:24 +00:00
// Define application environment
2014-07-11 06:36:20 +00:00
define ( 'APPLICATION_ENV' , 'production' );
2014-03-07 16:52:24 +00:00
// Ensure library/ is on include_path
set_include_path ( implode ( PATH_SEPARATOR , array (
realpath ( APPLICATION_PATH . '/../library' ),
get_include_path (),
)));
2014-08-04 14:45:48 +00: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' ,
'SdMetier' => __DIR__ . '/../library/SdMetier' ,
'Metier' => __DIR__ . '/../library/Metier' ,
),
'fallback_autoloader' => true
)
));
// Zend_Application - Use it if you don't have autoloaders
//require_once 'Zend/Application.php';
2014-03-07 16:52:24 +00:00
// Create application, bootstrap, and run
$application = new Zend_Application (
APPLICATION_ENV ,
APPLICATION_PATH . '/configs/application.ini'
);
//Options
try {
$opts = new Zend_Console_Getopt ( array (
2014-07-11 06:36:20 +00:00
'help|?' => " Displays usage information. " ,
'cron' => " Launch in cron " ,
'file=s' => " Specify file name to execute manually " ,
'debug' => " Mode debug " ,
2014-03-07 16:52:24 +00:00
));
$opts -> parse ();
} catch ( Zend_Console_Getopt_Exception $e ) {
echo $e -> getUsageMessage ();
exit ;
}
//Usage
if ( isset ( $opts -> help ) || count ( $opts -> getOptions ()) == 0 )
{
echo " Chargement des fichiers en provenance de GE FactoFrance. " . PHP_EOL ;
echo $opts -> getUsageMessage ();
exit ;
}
2014-07-11 06:36:20 +00:00
$c = new Zend_Config ( $application -> getOptions ());
2014-09-08 12:58:06 +00:00
$db = Zend_Db :: factory ( $c -> profil -> db -> metier );
Zend_Db_Table_Abstract :: setDefaultAdapter ( $db );
2014-03-07 16:52:24 +00:00
$tabFile = array ();
//Lire le nom du fichier depuis la ligne de commande
if ( $opts -> file ) {
if ( empty ( $opts -> file ) ) {
echo date ( 'Y-m-d H:i:s' ) . " - Aucun fichier définit ! " . PHP_EOL ;
exit ;
}
$tabFile [] = $opts -> file ;
}
2014-07-11 06:36:20 +00:00
//Lire la table flux_filein (Nom du fichier, nombre de ligne)
if ( $opts -> cron ) {
2014-03-07 16:52:24 +00:00
$fluxinM = new Application_Model_Sdv1FluxFileIn ();
$sql = $fluxinM -> select ()
-> where ( 'client=?' , 'gefacto' )
-> where ( 'name=?' , 'GEFACTO' )
-> where ( 'depotType=?' , 'SFTP' )
-> where ( 'dateExecute=?' , '0000-00-00 00:00:00' )
-> order ( 'dateInsert DESC' );
$pitems = $fluxinM -> fetchAll ( $sql );
foreach ( $pitems as $p ) {
2014-09-08 12:58:06 +00:00
$tabFile [] = $p -> depotFile ;
2014-03-07 16:52:24 +00:00
}
}
if ( count ( $tabFile ) == 0 ) {
echo date ( 'Y-m-d H:i:s' ) . " - Aucun fichier à traiter ! " . PHP_EOL ;
exit ;
}
2014-07-11 06:36:20 +00:00
require_once APPLICATION_PATH . '/configs/config.php' ;
require_once 'framework/fwk.php' ;
require_once 'Metier/insee/classMInsee.php' ;
require_once 'Metier/partenaires/classMFacto.php' ;
2014-03-07 16:52:24 +00:00
foreach ( $tabFile as $filename )
{
2014-07-11 06:36:20 +00:00
$path = $c -> profil -> path -> storage . '/clients/gefacto/send' ;
2014-03-07 16:52:24 +00:00
$file = $path . '/' . $filename ;
//Check fichier
2014-07-11 06:36:20 +00:00
if ( file_exists ( $file ) ) {
2014-03-07 16:52:24 +00:00
$dateFichier = date ( 'YmdHis' , filemtime ( $file ));
2014-07-11 06:36:20 +00:00
echo date ( 'Y-m-d H:i:s' ) . " - Chargement des Informations de Paiement " . basename ( $file ) . PHP_EOL ;
2014-03-07 16:52:24 +00:00
//Ouvrir le fichier
$fp = fopen ( $file , 'r' );
if ( ! $fp ) {
echo date ( 'Y-m-d H:i:s' ) . " - Impossible de lire le fichier ! " . PHP_EOL ;
exit ;
}
//Calcul du nombre de ligne du fichier
$nbLines = 0 ;
while (( $buffer = fgets ( $fp )) !== false ) {
$nbLines ++ ;
//Détection de la longueur de la première ligne
$lineLength = strlen ( $buffer );
if ( $lineLength <= 30 ) {
$fileType = 1 ; $dbTableUpdate = 'ge_cs2' ;
2014-09-08 12:58:06 +00:00
} elseif ( $lineLength == 122 ) {
2014-03-07 16:52:24 +00:00
$fileType = 0 ; $dbTableUpdate = 'ge_acheteurs' ;
2014-09-08 12:58:06 +00:00
} elseif ( $lineLength == 75 ) {
2014-03-07 16:52:24 +00:00
$fileType = 2 ; $dbTableUpdate = 'ge_paiements' ;
}
//Erreur
else {
echo date ( 'Y-m-d H:i:s' ) . " - Erreur : Première ligne de taille $lineLength non gérée ! " . PHP_EOL ;
exit ;
}
2014-07-11 06:36:20 +00:00
if ( $opts -> debug ) echo date ( 'Y-m-d H:i:s' ) . " - Ligne $nbLines " . PHP_EOL ;
2014-03-07 16:52:24 +00:00
}
//Retour au début du fichier
rewind ( $fp );
//Executer les chargements
$iDb = new WDB ( 'sdv1' );
$iDbH = new WDB ( 'historiques' );
$iInsee = new MInsee ();
$iFacto = new MFacto ();
$iFacto -> setTypeFic ( $fileType );
if ( $fileType == 1 ) {
$iDb -> update ( $dbTableUpdate , array ( 'indTrt' => 0 ), '1' );
}
$nbSiretInvalides = 0 ;
$nbSiretValides = 0 ;
$cptLine = 0 ;
while (( $buffer = fgets ( $fp )) !== false ) {
$cptLine ++ ;
2014-07-11 06:36:20 +00:00
if ( $opts -> debug ) echo date ( 'Y-m-d H:i:s' ) . " - Ligne $cptLine / $nbLines " . PHP_EOL ;
2014-03-07 16:52:24 +00:00
$dateInsert = date ( 'YmdHis' );
$a = $iFacto -> readFic ( $buffer );
//Vérification du Siren/Siret
if ( $dbTableUpdate == 'ge_acheteurs' ) {
if ( ! $iInsee -> valideSiren ( $a [ 'SIRENE' ])) {
$a [ 'sirenValide' ] = 0 ;
$nbSiretInvalides ++ ;
} else {
$a [ 'sirenValide' ] = 1 ;
$nbSiretValides ++ ;
}
}
//Insertion dans l'historique
if ( $dbTableUpdate == 'ge_cs2' ) {
2014-09-08 12:58:06 +00:00
$iDbH -> insert ( $dbTableUpdate , array_merge ( $a , array ( 'dateInsert' => $dateInsert , 'dateConf' => $dateFichier )));
2014-03-07 16:52:24 +00:00
if ( mysql_errno () > 0 && mysql_errno () <> 1062 ) die ( " Table = $dbTableUpdate " . PHP_EOL . mysql_error () . PHP_EOL );
}
if ( $iDb -> insert ( $dbTableUpdate , array_merge ( $a , array ( 'dateInsert' => $dateInsert ))) ){
$nbInsert ++ ;
} elseif ( $dbTableUpdate == 'ge_cs2' ) {
$ret = $iDb -> select ( $dbTableUpdate , 'cs, dateFin*1 AS dateFin' , 'siren=' . $a [ 'siren' ], false , MYSQL_ASSOC );
if ( count ( $ret ) > 0 ) {
$csPre = $ret [ 0 ][ 'cs' ];
$datePre = $ret [ 0 ][ 'dateFin' ];
if ( $a [ 'cs' ] <> $csPre || $a [ 'dateFin' ] <> $datePre ) {
$iDb -> update ( $dbTableUpdate , array_merge ( $a , array (
'dateConf' => $dateFichier ,
'csPre' => $csPre ,
'dateCsPre' => $datePre ,
'dateModifCS' => $dateFichier ,
'dateUpdate' => $dateInsert ,
'indTrt' => 1 )
), 'siren=' . $a [ 'siren' ]);
} else {
$iDb -> update ( $dbTableUpdate , array_merge ( $a , array (
'dateConf' => $dateFichier ,
'indTrt' => 1 )
), 'siren=' . $a [ 'siren' ]);
}
if ( mysql_errno () > 0 ) {
2014-09-08 12:58:06 +00:00
if ( $opts -> debug ) echo date ( 'Y-m-d H:i:s' ) . " - Erreur SQL " . mysql_errno () . ' : ' . mysql_error () . " sur $dbTableUpdate pour " . print_r ( $a );
2014-03-07 16:52:24 +00:00
}
$nbUpdate ++ ;
}
} elseif ( $dbTableUpdate == 'ge_acheteurs' ) {
$iDb -> update ( $dbTableUpdate , array_merge ( $a , array ( 'dateUpdate' => $dateInsert )), 'NUMACH=' . $a [ 'NUMACH' ]);
if ( mysql_errno () > 0 ) {
2014-09-08 12:58:06 +00:00
if ( $opts -> debug ) echo date ( 'Y-m-d H:i:s' ) . " - Erreur SQL " . mysql_errno () . ' : ' . mysql_error () . " sur $table pour " . print_r ( $a );
2014-03-07 16:52:24 +00:00
}
$nbUpdate ++ ;
} else {
2014-09-08 12:58:06 +00:00
if ( $opts -> debug ) echo date ( 'Y-m-d H:i:s' ) . " - Erreur SQL " . mysql_errno () . ' : ' . mysql_error () . " sur $table pour " . print_r ( $a );
2014-03-07 16:52:24 +00:00
}
}
//Fermeture du fichier
fclose ( $fp );
echo date ( 'Y-m-d H:i:s' ) . " - $nbLines lignes traitées dont $nbSiretInvalides siren/siret invalides ! " . PHP_EOL ;
2014-07-11 06:36:20 +00:00
echo date ( 'Y-m-d H:i:s' ) . " - Le fichier " . basename ( $file ) . " vient d'être chargé : $nbInsert ajouts et $nbUpdate MAJ ! " . PHP_EOL ;
2014-03-07 16:52:24 +00:00
if ( $dbTableUpdate == 'ge_cs2' ) {
$iDb -> update ( $table , array ( 'dateSuppr' => $dateInsert ), 'indTrt=0' );
}
2014-07-11 06:36:20 +00:00
$iDb -> update ( 'flux_filein' , array ( 'dateExecute' => date ( 'YmdHis' )), " name='GEFACTO' AND depotFile=' " . basename ( $file ) . " ' " , false );
echo date ( 'Y-m-d H:i:s' ) . " - Le fichier " . basename ( $file ) . " vient d'être marqué traité. " . PHP_EOL ;
2014-03-07 16:52:24 +00:00
2014-07-11 06:36:20 +00:00
$message .= " Le fichier " . basename ( $file ) . " vient d'être chargé : \r \n " ;
2014-03-07 16:52:24 +00:00
$message .= " $nbInsert ajouts et $nbUpdate mises à jours sur $nbLignes lignes ( $nbSiretInvalides siren/siret invalides). " . PHP_EOL ;
sendMail ( 'production@scores-decisions.com' , 'support@scores-decisions.com,ylenaour@scores-decisions.com' , '[CHARGEMENT] Informations de Paiement' , $message );
2014-07-11 06:36:20 +00:00
echo date ( 'Y-m-d H:i:s' ) . " - Fin du chargement des Informations de Paiement " . basename ( $file ) . PHP_EOL ;
2014-03-07 16:52:24 +00:00
} else {
echo date ( 'Y-m-d H:i:s' ) . " - Fichier $file inexistant ! " . PHP_EOL ;
}
}