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 ?
*/
2014-10-29 13:32:45 +00:00
ini_set ( 'auto_detect_line_endings' , true );
2014-03-07 16:52:24 +00:00
2015-09-14 20:31:06 +00:00
// --- Define path to application directory
defined ( 'APPLICATION_PATH' )
|| define ( 'APPLICATION_PATH' , realpath ( __DIR__ . '/../application' ));
2014-03-07 16:52:24 +00:00
2015-09-14 20:31:06 +00:00
// --- Define application environment
defined ( 'APPLICATION_ENV' )
|| define ( 'APPLICATION_ENV' , ( getenv ( 'APPLICATION_ENV' ) ? getenv ( 'APPLICATION_ENV' ) : 'production' ));
2014-03-07 16:52:24 +00:00
2015-09-18 10:01:04 +00:00
// --- Composer autoload
2015-09-14 20:31:06 +00:00
require_once realpath ( __DIR__ . '/../vendor/autoload.php' );
2014-08-04 14:45:48 +00:00
2015-09-14 20:31:06 +00:00
// --- Create application, bootstrap, and run
$application = new Zend_Application ( APPLICATION_ENV , APPLICATION_PATH . '/configs/application.ini' );
2014-03-07 16:52:24 +00:00
//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' ;
2014-09-30 15:00:06 +00:00
require_once 'framework/mail/sendMail.php' ;
2014-07-11 06:36:20 +00:00
2014-03-07 16:52:24 +00:00
foreach ( $tabFile as $filename )
{
2016-01-26 13:50:08 +00:00
$path = $c -> profil -> path -> shared . '/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 ;
2014-10-29 13:32:45 +00:00
while (( $buffer = fgets ( $fp )) !== false )
{
2014-03-07 16:52:24 +00:00
$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
2015-05-18 13:24:08 +00:00
$iDb = new WDB ();
2016-02-25 10:49:36 +00:00
$iInsee = new Metier_Insee_MInsee ( $iDb );
2014-03-07 16:52:24 +00:00
2016-02-25 10:49:36 +00:00
$iFacto = new Metier_Partenaires_MFacto ();
2014-03-07 16:52:24 +00:00
$iFacto -> setTypeFic ( $fileType );
if ( $fileType == 1 ) {
2015-05-18 13:24:08 +00:00
$iDb -> update ( 'sdv1.' . $dbTableUpdate , array ( 'indTrt' => 0 ), '1' );
2014-03-07 16:52:24 +00:00
}
$nbSiretInvalides = 0 ;
$nbSiretValides = 0 ;
2014-11-12 10:01:08 +00:00
$cptLine = $nbInsert = $nbUpdate = 0 ;
2014-03-07 16:52:24 +00:00
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
2015-05-18 13:24:08 +00:00
if ( $dbTableUpdate == 'sdv1.ge_acheteurs' )
2014-10-06 19:44:13 +00:00
{
if ( ! $iInsee -> valideSiren ( $a [ 'SIRENE' ]))
{
2014-03-07 16:52:24 +00:00
$a [ 'sirenValide' ] = 0 ;
$nbSiretInvalides ++ ;
2014-10-06 19:44:13 +00:00
}
else
{
2014-03-07 16:52:24 +00:00
$a [ 'sirenValide' ] = 1 ;
$nbSiretValides ++ ;
}
2014-10-06 19:44:13 +00:00
}
2014-03-07 16:52:24 +00:00
2014-10-06 19:44:13 +00:00
//Siren pour la table ge_paiements
if ( $dbTableUpdate == 'ge_paiements' )
{
2015-05-18 13:24:08 +00:00
$result = $iDb -> select ( 'sdv1.ge_acheteurs' , 'SIRENE' , 'NUMACH=' . $a [ 'NUMACH' ]);
2014-10-06 19:44:13 +00:00
if ( count ( $result ) > 0 )
{
$a [ 'siren' ] = $result [ 0 ][ 'SIRENE' ];
}
2014-03-07 16:52:24 +00:00
}
//Insertion dans l'historique
2014-10-06 19:44:13 +00:00
if ( $dbTableUpdate == 'ge_cs2' )
{
2015-05-18 13:24:08 +00:00
$iDb -> insert ( 'historiques.' . $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 );
}
2014-10-06 19:44:13 +00:00
//Insertion dans la table
2015-05-18 13:24:08 +00:00
if ( $iDb -> insert ( 'sdv1.' . $dbTableUpdate , array_merge ( $a , array ( 'dateInsert' => $dateInsert ))) )
2014-10-06 19:44:13 +00:00
{
2014-03-07 16:52:24 +00:00
$nbInsert ++ ;
2014-10-06 19:44:13 +00:00
}
elseif ( $dbTableUpdate == 'ge_cs2' )
{
2015-05-18 13:24:08 +00:00
$ret = $iDb -> select ( 'sdv1.' . $dbTableUpdate , 'cs, dateFin*1 AS dateFin' , 'siren=' . $a [ 'siren' ], false , MYSQL_ASSOC );
2014-10-06 19:44:13 +00:00
if ( count ( $ret ) > 0 )
{
2014-03-07 16:52:24 +00:00
$csPre = $ret [ 0 ][ 'cs' ];
$datePre = $ret [ 0 ][ 'dateFin' ];
if ( $a [ 'cs' ] <> $csPre || $a [ 'dateFin' ] <> $datePre ) {
2015-05-18 13:24:08 +00:00
$iDb -> update ( 'sdv1.' . $dbTableUpdate , array_merge ( $a , array (
2014-03-07 16:52:24 +00:00
'dateConf' => $dateFichier ,
'csPre' => $csPre ,
'dateCsPre' => $datePre ,
'dateModifCS' => $dateFichier ,
'dateUpdate' => $dateInsert ,
'indTrt' => 1 )
), 'siren=' . $a [ 'siren' ]);
2014-10-06 19:44:13 +00:00
}
else
{
2015-05-18 13:24:08 +00:00
$iDb -> update ( 'sdv1.' . $dbTableUpdate , array_merge ( $a , array (
2014-03-07 16:52:24 +00:00
'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 ++ ;
}
2014-10-06 19:44:13 +00:00
}
elseif ( $dbTableUpdate == 'ge_acheteurs' )
{
2015-05-18 13:24:08 +00:00
$iDb -> update ( 'sdv1.' . $dbTableUpdate , array_merge ( $a , array ( 'dateUpdate' => $dateInsert )), 'NUMACH=' . $a [ 'NUMACH' ]);
2014-03-07 16:52:24 +00:00
if ( mysql_errno () > 0 ) {
2014-11-12 10:01:08 +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 ++ ;
2014-10-06 19:44:13 +00:00
}
else
{
2014-11-12 10:01:08 +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
}
}
//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' ) {
2015-05-18 13:24:08 +00:00
$iDb -> update ( 'sdv1.' . $dbTableUpdate , array ( 'dateSuppr' => $dateInsert ), 'indTrt=0' );
2014-03-07 16:52:24 +00:00
}
2015-05-18 13:24:08 +00:00
$iDb -> update ( 'sdv1.flux_filein' , array ( 'dateExecute' => date ( 'YmdHis' )), " name='GEFACTO' AND depotFile=' " . basename ( $file ) . " ' " , false );
2014-07-11 06:36:20 +00:00
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-11-12 10:01:08 +00:00
$message = " Le fichier " . basename ( $file ) . " vient d'être chargé : \r \n " ;
$message .= " $nbInsert ajouts et $nbUpdate mises à jours sur $nbLines lignes ( $nbSiretInvalides siren/siret invalides). " . PHP_EOL ;
2014-03-07 16:52:24 +00:00
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 ;
}
}