2012-01-13 14:35:11 +01:00
#!/usr/bin/php
< ? php
2012-02-01 15:34:51 +01:00
error_reporting ( E_ALL ^ E_NOTICE ^ E_WARNING );
2012-01-13 14:35:11 +01:00
// Define path to application directory
defined ( 'APPLICATION_PATH' )
|| define ( 'APPLICATION_PATH' , realpath ( dirname ( __FILE__ ) . '/../application' ));
// Define application environment
defined ( 'APPLICATION_ENV' )
2012-01-30 22:09:14 +01:00
|| define ( 'APPLICATION_ENV' , ( getenv ( 'APPLICATION_ENV' ) ? getenv ( 'APPLICATION_ENV' ) : 'development' ));
2012-01-13 14:35:11 +01:00
// 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|?' => " Aide. " ,
2012-01-30 17:33:53 +01:00
'id=s' => " Identifiant du traitement " ,
'file=s' => " Traitement manuel avec spécification du fichier "
2012-01-13 14:35:11 +01:00
)
);
$opts -> parse ();
} catch ( Zend_Console_Getopt_Exception $e ) {
echo $e -> getUsageMessage ();
exit ;
}
//Usage
2012-01-30 17:34:44 +01:00
if ( isset ( $opts -> help ) || ! isset ( $opts -> id ) && ! isset ( $opts -> file ) )
2012-01-13 14:35:11 +01:00
{
echo $opts -> getUsageMessage ();
exit ;
}
//Traiter la demande en créant les requetes sql nécessaires
$tabDico = array (
'etablissements' => array (
'db' => 'jo.etablissements' ,
'cle' => array (
'idSd' => " CONCAT(source,'-',source_id) AS idSd " ,
'source' => '' ,
'triCode' => '' ,
'autre_id' => '' ,
'actif' => '' ,
'siege' => '' ,
'raisonSociale' => '' ,
'enseigne' => '' ,
'sigle' => '' ,
'marques' => '' ,
'adr_num' => '' ,
'adr_btq' => '' ,
'adr_typeVoie' => '' ,
'adr_libVoie' => '' ,
'adr_comp' => '' ,
'adr_cp' => '' ,
'adr_ville' => '' ,
'adr_dep' => '' ,
'adr_codeCom' => 'CONCAT(if (adr_dep>100,round(adr_dep/10),adr_dep*1),adr_com) AS adr_codeCom' ,
'telEtab' => 'tel as telEtab' ,
'faxEtab' => 'fax as faxEtab' ,
'cj' => '' ,
'capital' => '' ,
'capitalDev' => '' ,
'ape_etab' => '' ,
'ape_entrep' => '' ,
'SiretEnBase' => 'nic>0 as SiretEnBase' ,
),
'where' => ' WHERE siren=$siren AND nic=$nicV' ,
),
'identite' => array (
'db' => 'insee.identite' ,
'cle' => array (
'etActifInsee' => 'ACTIF as etActifInsee' ,
'nomInsee' => 'NOM as nomInsee' ,
'nomInsee2' => 'NOM2 as nomInsee2' ,
'sigleInsee' => 'SIGLE as sigleInsee' ,
'enseigneInsee' => 'ENSEIGNE as enseigneInsee' ,
'adrNumVoieInsee' => 'ADR_NUMVOIE as adrNumVoieInsee' ,
'adrBtqInsee' => 'ADR_BTQ as adrBtqInsee' ,
'adrTypeVoieInsee' => 'ADR_TYPVOIE as adrTypeVoieInsee' ,
'adrVoieInsee' => 'ADR_LIBVOIE as adrVoieInsee' ,
'adrCompInsee' => 'ADR_COMP as adrCompInsee' ,
'adrCPInsee' => 'ADR_CP as adrCPInsee' ,
'adrVilleInsee' => 'ADR_LIBCOM as adrVilleInsee' ,
'adrDistSPInsee' => 'ADR_DISTSP as adrDistSPInsee' ,
'CjInsee' => 'CJ as CjInsee' ,
'nafEnInsee' => 'APE_ENT as nafEnInsee' ,
'nafEtInsee' => 'APE_ETAB as nafEtInsee' ,
'APRM' => '' ,
'ACTIVNAT' => '' ,
'ORIGINE' => '' ,
'MODET' => '' ,
'EXPLET' => '' ,
'LIEUACT' => '' ,
'ACTISURF' => '' ,
'MODEN' => '' ,
'PRODPART' => '' ,
'EXPLEN' => '' ,
'MONOREG' => '' ,
'REGIMP' => '' ,
'MONOACT' => '' ,
'DCREN' => '' ,
'AUXILT' => '' ,
'SAISONAT' => '' ,
'CIVILITE' => '' ,
'TCA' => '' ,
'TCAEXP' => '' ,
'EFF_ENT' => '' ,
'DCRET' => '' ,
'EFF_ET' => '' ,
'CODEVOIE' => '' ,
'dateMajInsee' => 'dateUpdate as dateMajInsee' ,
),
'where' => ' WHERE SIREN=$siren AND NIC=$nicV' ,
),
'notice' => array (
'db' => 'insee.insee_notices' ,
'cle' => array (
'insRPET' => '' ,
'insDEPCOMEN' => '' ,
'insRPEN' => '' ,
'insARRONET' => '' ,
'insCTONET' => '' ,
'insTCD' => '' ,
'insZEMET' => '' ,
'insDU' => '' ,
'insTU' => '' ,
'insUU' => '' ,
'insRECME' => '' ,
'insEAEANT' => '' ,
'insEAEAPET' => '' ,
'insEAESEC1T' => '' ,
'insEAESEC2T' => '' ,
'insEAEANN' => '' ,
'insEAEAPEN' => '' ,
'insEAESEC1N' => '' ,
'insEAESEC2N' => '' ,
'insEAESEC3N' => '' ,
'insEAESEC4N' => '' ,
'dateMajNotice' => 'dateNotice as dateMajNotice' ,
),
'where' => ' WHERE insSIREN=$siren AND insNIC=$nicV' ,
),
'rncs_entrep' => array (
'db' => 'jo.rncs_entrep' ,
'cle' => array (
'entActiveRncs' => 'actif AS entActiveRncs' ,
'numRC2' => 'numRC2' ,
'raisonSocialeRncs' => 'raisonSociale AS raisonSocialeRncs' ,
'sigleRncs' => 'sigle AS sigleRncs' ,
'dateNaiss' => 'dateNaiss' ,
'lieuNaiss' => 'lieuNaiss' ,
'nationalite' => 'nationalite' ,
'pays' => 'pays' ,
'nafEnRncs' => 'naf AS nafEnRncs' ,
'cjRncs' => 'cj AS cjRncs' ,
'dateImma' => 'dateImma' ,
'dateRad' => 'dateRad' ,
'capitalType' => 'capitalType' ,
'dateMajEnRncs' => 'flux AS dateMajEnRncs' ,
),
'where' => ' WHERE siren=$siren' ,
),
'rncs_etab' => array (
'db' => 'jo.rncs_etab' ,
'cle' => array (
'etActifRncs' => 'actif AS etActifRncs' ,
'siegeRncs' => 'siege AS siegeRncs' ,
'enseigneRncs' => 'enseigne AS enseigneRncs' ,
'nomCommercialRncs' => 'nomCommercial AS nomCommercialRncs' ,
'adrNumVoieRncs' => 'adrNumVoie AS adrNumVoieRncs' ,
'adrIndRepRncs' => 'adrIndRep AS adrIndRepRncs' ,
'adrLibVoieRncs' => 'adrLibVoie AS adrLibVoieRncs' ,
'adrTypeVoieRncs' => 'adrTypeVoie AS adrTypeVoieRncs' ,
'adrVoieRncs' => 'adrVoie AS adrVoieRncs' ,
'cpRncs' => 'cp AS cpRncs' ,
'villeRncs' => 'commune AS villeRncs' ,
'adrCompRncs' => 'adrComp AS adrCompRncs' ,
'nafEtRncs' => 'naf AS nafEtRncs' ,
'dateMajEtRncs' => 'flux AS dateMajEtRncs' ,
),
'where' => ' WHERE siren=$siren AND nic=$nicV' ,
),
'infos_entrep' => array (
'db' => 'jo.infos_entrep' ,
'cle' => array (
'isin' => '' ,
'tel' => '' ,
'fax' => '' ,
'web' => '' ,
'mail' => '' ,
),
'where' => ' WHERE siren=$siren' ,
),
'data' => array (
'nicSiege' ,
'tva' ,
'dateFermetureEn' ,
'dateFermetureEt' ,
'activite' ,
'situationJuridique' ,
'situationParution' ,
'situationDernCode' ,
'situationDernEven' ,
'indiScore' ,
'dirigeant' ,
'encours' ,
'dateBilan' ,
'bilanN' ,
'bilanN1' ,
'bilanN2' ,
'annonces' ,
),
);
2012-01-15 22:13:32 +01:00
//Définition bdd
$dbConfig = new Zend_Config_Ini ( APPLICATION_PATH . '/configs/configuration.ini' , 'databases' );
2012-01-30 11:46:50 +01:00
try {
$db = Zend_Db :: factory ( $dbConfig -> db );
} catch ( Exception $e ) {
exit ( $e -> getMessage () );
}
2012-01-15 22:13:32 +01:00
2012-01-30 17:33:53 +01:00
if ( $opts -> id ) {
$commandesM = new Application_Model_Commandes ( $db );
$commande = $commandesM -> find ( intval ( $opts -> id )) -> current ();
$file = basename ( $commande -> fichier );
} else {
$file = $opts -> file ;
}
2012-01-13 14:35:11 +01:00
$extension = strrchr ( $file , '.' );
$file = str_replace ( $extension , '' , $file );
2012-01-16 15:20:08 +01:00
$path = new Zend_Config_Ini ( APPLICATION_PATH . '/configs/configuration.ini' , 'path' );
if ( ! file_exists ( $path -> data . '/export' )) mkdir ( $path -> data . '/export' );
$outFile = $path -> data . '/export/' . $file . '-' . date ( 'YmdHis' ) . '.csv' ;
2012-01-27 17:58:59 +01:00
$inFile = $path -> data . '/clients/' . $file . '.csv' ;
2012-01-13 14:35:11 +01:00
2012-01-30 17:33:53 +01:00
if ( $opts -> id ) {
$profilM = new Application_Model_Profil ( $db );
$profil = $profilM -> find ( $commande -> idProfil ) -> current ();
$profilEntete = json_decode ( $profil -> criteres , true );
}
2012-01-13 14:35:11 +01:00
//Chargement des classes metier
require_once realpath ( dirname ( __FILE__ )) . '/../config/config.php' ;
2012-01-16 15:20:08 +01:00
require_once 'framework/fwk.php' ;
2012-01-13 14:35:11 +01:00
require_once 'Metier/insee/classMInsee.php' ;
2012-01-30 11:46:50 +01:00
//Définition bdd metier
$dbConfig = array (
'host' => MYSQL_HOST ,
'port' => MYSQL_PORT ,
'username' => MYSQL_USER ,
'password' => MYSQL_PASS ,
'dbname' => MYSQL_DEFAULT_DB ,
'driver_options' => array ( MYSQLI_INIT_COMMAND => 'SET NAMES UTF8;' ),
);
try {
$dbMetier = Zend_Db :: factory ( 'Mysqli' , $dbConfig );
} catch ( Exception $e ) {
exit ( $e -> getMessage () );
}
2012-01-13 14:35:11 +01:00
$tabExtract = array ();
$tabEntete = array ();
$tabIdentifiant = array ();
$tabData = array ();
$posKeySiren = $posKeySiret = $posKeyNic = false ;
//Lire le fichier csv
$row = 0 ;
2012-01-27 17:58:59 +01:00
if (( $handle = fopen ( $inFile , 'r' )) !== FALSE ) {
2012-01-13 14:35:11 +01:00
while (( $data = fgetcsv ( $handle , 0 , ',' , '"' )) !== FALSE ) {
$num = count ( $data );
//Traitement de la première ligne
if ( $row == 0 ) {
for ( $c = 0 ; $c < $num ; $c ++ ) {
//Détection clé importante
switch ( strtolower ( $data [ $c ])) {
case 'siren' :
$posKeySiren = $c ;
break ;
case 'nic' :
$posKeyNic = $c ;
break ;
case 'siret' :
$posKeySiret = $c ;
break ;
}
$tabExtract [] = $data [ $c ];
}
if ( $posKeySiren !== false && $posKeyNic !== false && $posKeySiret !== false || $posKeySiren !== false && $posKeySiret !== false ){
die ( " Trop de clé! " );
}
if ( $posKeySiren === false && $posKeySiret === false ){
die ( " Aucune clé détecté! " );
}
//Traitement des identifiants
} else {
if ( $posKeySiret !== false ){
$tabIdentifiant [] = $data [ $posKeySiret ];
} elseif ( $posKeySiren !== false && $posKeyNic !== false ){
$tabIdentifiant [] = $data [ $posKeySiren ] . $data [ $posKeyNic ];
} elseif ( $posKeySiren !== false ){
$tabIdentifiant [] = $data [ $posKeySiren ];
}
for ( $c = 0 ; $c < $num ; $c ++ ) {
$colname = $tabExtract [ $c ];
if ( ! in_array ( strtolower ( $colname ), array ( 'siren' , 'siret' , 'nic' ))){
$tabData [ $row - 1 ][ $colname ] = $data [ $c ];
}
}
2012-01-15 22:13:32 +01:00
2012-01-13 14:35:11 +01:00
}
$row ++ ;
}
fclose ( $handle );
}
2012-01-15 22:13:32 +01:00
//Ajout des entetes provenant du profil
2012-01-30 17:33:53 +01:00
if ( $opts -> id ) {
$tabExtract = array_merge ( $tabExtract , $profilEntete );
}
2012-01-15 22:13:32 +01:00
2012-01-13 14:35:11 +01:00
$nbIdentifiant = count ( $tabIdentifiant );
2012-01-15 22:13:32 +01:00
//echo "Chargement de $row lignes dont $nbIdentifiant lignes avec identifiants.\n";
2012-01-13 14:35:11 +01:00
$rowTotal = $nbIdentifiant ;
//Liste des champs SQL
foreach ( $tabExtract as $key )
{
$strKey = false ;
//Détection element(VAL1,VAL2)
$values = false ;
if ( preg_match ( '/(.*)\((.*)\)/' , $key , $matches ))
{
$key = $matches [ 1 ];
$values = $matches [ 2 ];
}
//Traitement spécial pour le champs, ajout et remplacement sur entete
if ( function_exists ( $key . 'Entete' )){
$keys = call_user_func ( $key . 'Entete' , $values );
foreach ( $keys as $item ){
$tabEntete [] = $item ;
}
} else {
$tabEntete [] = $key ;
}
//Ajout à la requete SQL
foreach ( $tabDico as $element => $select )
{
if ( array_key_exists ( 'db' , $select ) && array_key_exists ( $key , $select [ 'cle' ]) )
{
if ( isset ( $ { $element . 'Fields' })){ $ { $element . 'Fields' } .= ', ' ; }
if ( $select [ 'cle' ][ $key ] != '' ){
$strKey = $select [ 'cle' ][ $key ];
}
if ( $strKey !== false ){
$ { $element . 'Fields' } .= $strKey ;
} else {
$ { $element . 'Fields' } .= $key ;
}
break ;
}
if ( $element == 'data' && in_array ( $key , $select ) )
{
$ { $key . 'Data' } = $values ;
}
}
}
//Ajout colonne obligatoire SiretValide
2012-01-31 16:26:08 +01:00
$tabEntete [] = 'SiretValide' ;
2012-01-13 14:35:11 +01:00
unset ( $tabExtract );
//Vérification et création des données
$row = 0 ;
2012-01-15 22:13:32 +01:00
$fp = fopen ( $outFile , 'w' );
if ( count ( $tabEntete ) > 0 ){
fputcsv ( $fp , $tabEntete , ',' , '"' );
}
2012-01-30 17:33:53 +01:00
if ( $opts -> id ) {
2012-02-01 12:06:01 +01:00
$commandesM -> update ( array (
'dateStart' => date ( 'Y-m-d H:i:s' ),
'fichierOut' => basename ( $outFile )),
" id = " . $commande -> id );
2012-01-30 17:33:53 +01:00
}
2012-01-13 14:35:11 +01:00
foreach ( $tabIdentifiant as $item )
{
2012-01-30 22:09:14 +01:00
echo " Ligne " . ( $row + 1 ) . " \n " ;
2012-01-30 11:13:19 +01:00
2012-01-13 14:35:11 +01:00
$siretValide = false ;
$sirenValide = false ;
$siren = substr ( $item , 0 , 9 );
$nic = substr ( $item , 9 );
//Vérification par algorithme
$siretValide = siretValide ( $item );
if ( $posKeySiret !== false ){
if ( $siretValide === false ){
$sirenValide = sirenValide ( $siren );
} else {
$sirenValide = true ;
}
}
if ( $posKeySiren !== false && $posKeyNic !== false ) {
$sirenValide = sirenValide ( $siren );
}
//On définit le tableau de retour
if ( $posKeySiret !== false ){
$tabData [ $row ][ 'siret' ] = $item ;
} elseif ( $posKeySiren !== false && $posKeyNic !== false ){
$tabData [ $row ][ 'siren' ] = $siren ;
$tabData [ $row ][ 'nic' ] = $nic ;
} elseif ( $posKeySiren !== false ){
$tabData [ $row ][ 'siren' ] = $siren ;
}
$tabData [ $row ][ 'SiretValide' ] = ( $siretValide === true ) ? 1 : 0 ;
if ( $sirenValide === false || intval ( $siren ) == 0 ){
$row ++ ;
continue ;
}
//Vérifier les nic, ou sélectionner le nic du siege actif
$nicV = $nic ; //Surcharge pour le nic
$strNic = '' ;
$sqlNic = 'SELECT nic as nicTmp, cj as cjTmp FROM jo.etablissements WHERE siren=' . $siren ;
if ( strlen ( $item ) == 14 && intval ( $nic ) > 0 ){
2012-01-30 11:46:50 +01:00
$stmt = $dbMetier -> query ( $sqlNic . ' AND nic=' . $nic );
2012-01-13 14:35:11 +01:00
$result = $stmt -> fetchAll ();
if ( count ( $result ) == 0 ){
$nicV = 0 ;
$fJur = '0000' ;
} else {
//nic : On considère qu'il est vrai si on a un résultat
$fJur = $result [ 0 ][ 'cjTmp' ];
}
}
if ( intval ( $nicV ) == 0 ){
$strNic = ' AND nic>-1 AND actif>-1 ORDER BY siege DESC, actif DESC, nicTmp DESC LIMIT 0,1' ;
2012-01-30 11:46:50 +01:00
$stmt = $dbMetier -> query ( $sqlNic . $strNic );
2012-01-13 14:35:11 +01:00
$result = $stmt -> fetchAll ();
if ( count ( $result ) > 0 ){
$nicV = $result [ 0 ][ 'nicTmp' ];
$fJur = $result [ 0 ][ 'cjTmp' ];
} else {
$nicV = '00000' ;
$fJur = '0000' ;
}
}
if ( $posKeySiren !== false && $posKeyNic !== false ){
$tabData [ $row ][ 'siren' ] = $siren ;
$tabData [ $row ][ 'nic' ] = $nic ;
}
//Récupération des données
foreach ( $tabDico as $element => $select )
{
//Traitement par requete SQL
if ( isset ( $ { $element . 'Fields' })){
$where = $select [ 'where' ];
eval ( " \$ where = \" $where\ " ; " );
$ { $element . 'SQL' } = 'SELECT ' . $ { $element . 'Fields' } . ' FROM ' . $select [ 'db' ] . $where ;
2012-01-30 11:46:50 +01:00
$stmt = $dbMetier -> query ( $ { $element . 'SQL' });
2012-01-13 14:35:11 +01:00
$result = $stmt -> fetchAll ();
if ( count ( $result ) > 0 ){
$tabData [ $row ] = array_merge ( $tabData [ $row ] , $result [ 0 ]);
}
}
//Traitement par fonction
if ( $element == 'data' )
{
foreach ( $select as $item )
{
if ( isset ( $ { $item . 'Data' }))
{
$values = false ;
if ( $ { $item . 'Data' } != false ){
$values = $ { $item . 'Data' };
}
2012-01-31 16:26:08 +01:00
$retour = call_user_func ( $item . 'Data' , $siren , $nicV , $values );
$tabData [ $row ] = $tabData [ $row ] + $retour ;
2012-01-13 14:35:11 +01:00
}
}
}
2012-01-15 22:13:32 +01:00
}
2012-01-13 14:35:11 +01:00
2012-01-15 22:13:32 +01:00
//Trier pour la sortie
2012-01-31 16:26:08 +01:00
$tabSortie = array ();
2012-01-13 14:35:11 +01:00
foreach ( $tabEntete as $key ){
2012-01-31 16:26:08 +01:00
$tabSortie [] = isset ( $tabData [ $row ][ $key ]) ? $tabData [ $row ][ $key ] : '' ;
2012-01-30 17:00:34 +01:00
}
2012-01-31 16:26:08 +01:00
fputcsv ( $fp , $tabSortie , ',' , '"' );
2012-01-13 14:35:11 +01:00
$row ++ ;
2012-01-30 17:33:53 +01:00
2012-01-30 17:00:34 +01:00
//Mise à jour des lignes traitées dans la base
2012-01-30 17:33:53 +01:00
if ( $opts -> id ) {
$commandesM -> update ( array ( 'nbLigneT' => $row ), " id = " . $commande -> id );
}
2012-01-13 14:35:11 +01:00
}
2012-01-15 22:13:32 +01:00
fclose ( $fp );
2012-01-30 17:33:53 +01:00
if ( $opts -> id ) {
2012-01-31 16:33:12 +01:00
$commandesM -> update ( array (
2012-02-01 12:06:01 +01:00
'dateStop' => date ( 'Y-m-d H:i:s' ),
2012-01-31 16:33:12 +01:00
), " id = " . $commande -> id );
2012-01-30 17:33:53 +01:00
}
2012-01-13 14:35:11 +01:00
/* == FUNCTION DATA == */
function nicSiegeData ( $siren , $nic , $values = false )
{
$tabData [ 'nicSiege' ] = $nic ;
return $tabData ;
}
function tvaData ( $siren , $nic , $values = false )
{
require_once 'Metier/partenaires/classMTva.php' ;
$iTva = @ new MTva ( $siren , false );
$tabData [ 'tva' ] = isset ( $iTva -> vatNumber ) ? $iTva -> vatNumber : '' ;
return $tabData ;
}
function dateFermetureEtEntete ( $values )
{
return array (
'dateFermetureEt' ,
'codeFermetureEt' ,
);
}
function dateFermetureEtData ( $siren , $nic , $values = false )
{
$iDb = new WDB ();
$ret = $iDb -> select ( 'insee.insee_even' , 'insEVE, insDATEVE' , 'insSIREN=' . $siren . ' AND insNIC=' . $nic . " AND insEVE IN('FF','OF','400','410','420','425','430','435') ORDER BY insDATEVE DESC LIMIT 1 " , false , MYSQL_ASSOC );
if ( mysql_errno () > 0 ) die ( 'Erreur B2 ' . mysql_errno () . ' : ' . mysql_error () );
if ( count ( $ret ) == 0 ) {
$siret = $siren . $nic ;
$ret = $iDb -> select ( 'insee.insee_even' , 'insEVE, insDATEVE' , " insSIRETPS= $siret AND insEVE IN('510','530') ORDER BY insDATEVE DESC LIMIT 1 " , false , MYSQL_ASSOC );
}
$tabData [ 'dateFermetureEt' ] = isset ( $ret [ 0 ][ 'insDATEVE' ]) ? $ret [ 0 ][ 'insDATEVE' ] : '' ;
$tabData [ 'codeFermetureEt' ] = isset ( $ret [ 0 ][ 'insEVE' ]) ? $ret [ 0 ][ 'insEVE' ] : '' ;
return $tabData ;
}
function dateFermetureEnEntete ( $values )
{
return array (
'dateFermetureEn' ,
'codeFermetureEn' ,
);
}
function dateFermetureEnData ( $siren , $nic , $values = false )
{
$iDb = new WDB ();
$ret = @ $iDb -> select ( 'insee.insee_even' , 'insEVE, insDATEVE' , 'insSIREN=' . $siren . " AND insEVE IN('FF','410','420','425') ORDER BY insDATEVE DESC " , false , MYSQL_ASSOC );
if ( mysql_errno () > 0 ) die ( 'Erreur B3 ' . mysql_errno () . ' : ' . mysql_error () );
$tabData [ 'dateFermetureEn' ] = isset ( $ret [ 0 ][ 'insDATEVE' ]) ? $ret [ 0 ][ 'insDATEVE' ] : '' ;
$tabData [ 'codeFermetureEn' ] = isset ( $ret [ 0 ][ 'insEVE' ]) ? $ret [ 0 ][ 'insEVE' ] : '' ;
return $tabData ;
}
function activiteData ( $siren , $nic , $values = false )
{
global $fJur ;
$tmp = @ $iBodacc -> getActiviteReelle ( $siren , $fJur );
$tabData [ 'activite' ] = @ preg_replace ( '/ +/' , ' ' , strtr ( $tmp , array ( " \r " => ' ' , " \n " => ' ' , " \t " => ' ' , ',' => ' ' , ';' => ' ' , '"' => ' ' , " ' " => ' ' )));
return $tabData ;
}
function situationJuridiqueEntete ( $values )
{
return array (
'situationJuridique' ,
'situationParution' ,
'situationDernCode' ,
'situationDernEven' ,
);
}
function situationJuridiqueData ( $siren , $nic , $values = false )
{
$tabTmp = array ( 'P' , 'A' , 'D' );
$tabData [ 'situationJuridique' ] = '' ;
foreach ( $tabTmp as $sit ) {
$iInsee = new MInsee ();
$tmp = $iInsee -> getAnnoncesLegales ( $cel [ $pos [ 'SIREN' ]], 0 , $sit , false );
if ( count ( $tmp ) > 0 ) {
$tmp = $tmp [ 0 ];
$tabData [ 'situationJuridique' ] = $sit ;
$tabData [ 'situationParution' ] =@ $tmp [ 'DateParution' ];
$tabData [ 'situationDernCode' ] =@ $tmp [ 0 ][ 'CodeEven' ];
$tabData [ 'situationDernEven' ] =@ preg_replace ( '/ +/' , ' ' , strtr ( $tmp [ 0 ][ 'LibEven' ], array ( " \r " => ' ' , " \n " => ' ' , " \t " => ' ' , ',' => ' ' , ';' => ' ' , '"' => ' ' , " ' " => ' ' )));
break ;
}
}
if ( $tabData [ 'situationJuridique' ] == '' ) {
$iDb = new WDB ();
$rep = $iDb -> select ( 'rncs_entrep' , 'actif, dateRad' , " siren= " . $cel [ $pos [ 'SIREN' ]], false , MYSQL_ASSOC );
$entrep = @ $rep [ 0 ];
if ( isset ( $entrep [ 'actif' ]) && $entrep [ 'actif' ] * 1 == 0 )
$tabData [ 'situationJuridique' ] = 'R' ;
$tabData [ 'situationParution' ] =@ $entrep [ 'dateRad' ];
$tabData [ 'situationDernCode' ] = '' ;
$tabData [ 'situationDernEven' ] = '' ;
}
return $tabData ;
}
function dirigeantEntete ( $values )
{
return array (
'dirigeant1Nom' ,
'dirigeant1Fonction' ,
'dirigeant1DateNaiss' ,
'dirigeant1LieuNaiss' ,
'dirigeant2Nom' ,
'dirigeant2Fonction' ,
'dirigeant2DateNaiss' ,
'dirigeant2LieuNaiss' ,
);
}
function dirigeantData ( $siren , $nic , $values = false )
{
$iInsee = new MInsee ();
$tmp = $iInsee -> getDirigeants ( $siren , false );
$tabData [ 'dirigeant1Nom' ] = '' ;
$tabData [ 'dirigeant1Fonction' ] = '' ;
$tabData [ 'dirigeant1DateNaiss' ] = '' ;
$tabData [ 'dirigeant1LieuNaiss' ] = '' ;
$tabData [ 'dirigeant2Nom' ] = '' ;
$tabData [ 'dirigeant2Fonction' ] = '' ;
$tabData [ 'dirigeant2DateNaiss' ] = '' ;
$tabData [ 'dirigeant2LieuNaiss' ] = '' ;
if ( count ( $tmp ) > 0 ) {
$dir = $tmp [ 0 ];
if ( $dir [ 'Societe' ] <> '' ) $tabData [ 'dirigeant1Nom' ] .= $dir [ 'Societe' ] . ' repr. par ' ;
$tabData [ 'dirigeant1Nom' ] .= trim ( $dir [ 'Civilite' ] . ' ' . $dir [ 'Nom' ] . ' ' . $dir [ 'Prenom' ]);
$tabData [ 'dirigeant1Fonction' ] = $dir [ 'Titre' ];
$tabData [ 'dirigeant1DateNaiss' ] = $dir [ 'NaissDate' ];
$tabData [ 'dirigeant1LieuNaiss' ] = $dir [ 'NaissVille' ];
}
if ( count ( $tmp ) > 1 ) {
$dir = $tmp [ 1 ];
if ( $dir [ 'Societe' ] <> '' ) $tabData [ 'dirigeant2Nom' ] .= $dir [ 'Societe' ] . ' repr. par ' ;
$tabData [ 'dirigeant2Nom' ] .= trim ( $dir [ 'Civilite' ] . ' ' . $dir [ 'Nom' ] . ' ' . $dir [ 'Prenom' ]);
$tabData [ 'dirigeant2Fonction' ] = $dir [ 'Titre' ];
$tabData [ 'dirigeant2DateNaiss' ] = $dir [ 'NaissDate' ];
$tabData [ 'dirigeant2LieuNaiss' ] = $dir [ 'NaissVille' ];
}
return $tabData ;
}
function bilanNEntete ( $values )
{
if ( $values !== false ) {
$values = explode ( ',' , $values );
$tmp = array ();
foreach ( $values as $value ){
$tmp [] = 'bilanN' . $value ;
}
return array_merge (
array ( 'bilanNdateCloture' , 'bilanNdureeMois' )
, $tmp
);
} else {
return array ( 'bilanNdateCloture' , 'bilanNdureeMois' );
}
}
function bilanNData ( $siren , $nic , $values = false )
{
require_once 'Metier/partenaires/classMBilans.php' ;
2012-01-30 22:09:14 +01:00
$tabPostes = explode ( ',' , $values );
2012-01-27 16:55:25 +01:00
$annee = 0 ;
2012-01-13 14:35:11 +01:00
$tabData = array ();
$mBil = new MBilans ( $siren );
2012-01-27 16:55:25 +01:00
$tabBilans = $mBil -> listeBilans ( false , 9 );
if ( count ( $tabBilans ) > 0 ) {
2012-01-30 22:09:14 +01:00
$tabBi = findBilan ( $siren , $tabBilans , $annee );
2012-01-13 14:35:11 +01:00
$tabData [ 'bilanNdateCloture' ] = $tabBi [ 'DATE_CLOTURE' ];
2012-01-27 16:55:25 +01:00
$tabData [ 'bilanNdureeMois' ] = $tabBi [ 'DUREE_MOIS' ];
if ( count ( $tabPostes ) > 0 ) {
2012-01-13 14:35:11 +01:00
foreach ( $tabPostes as $poste ) {
if ( isset ( $tabBi [ $poste ])){
$tabData [ 'bilanN' . $poste ] = $tabBi [ $poste ];
}
}
}
}
return $tabData ;
}
function bilanN1Entete ( $values )
{
if ( $values !== false ) {
$values = explode ( ',' , $values );
$tmp = array ();
foreach ( $values as $value ){
$tmp [] = 'bilanN1' . $value ;
}
return array_merge (
array ( 'bilanN1dateCloture' , 'bilanN1dureeMois' )
, $tmp
);
} else {
return array ( 'bilanN1dateCloture' , 'bilanN1dureeMois' );
}
}
function bilanN1Data ( $siren , $nic , $values = false )
{
require_once 'Metier/partenaires/classMBilans.php' ;
$tabPostes = explode ( ',' , $values );
$annee = 1 ;
2012-01-27 16:55:25 +01:00
$tabData = array ();
2012-01-13 14:35:11 +01:00
$mBil = new MBilans ( $siren );
2012-01-27 16:55:25 +01:00
$tabBilans = $mBil -> listeBilans ( false , 9 );
if ( count ( $tabBilans ) > 0 ) {
2012-01-30 22:09:14 +01:00
$tabBi = findBilan ( $siren , $tabBilans , $annee );
2012-01-31 16:26:08 +01:00
$tabData [ 'bilanN1dateCloture' ] = $tabBi [ 'DATE_CLOTURE' ];
$tabData [ 'bilanN1dureeMois' ] = $tabBi [ 'DUREE_MOIS' ];
2012-01-27 16:55:25 +01:00
if ( count ( $tabPostes ) > 0 ) {
2012-01-13 14:35:11 +01:00
foreach ( $tabPostes as $poste ) {
if ( isset ( $tabBi [ $poste ])){
2012-01-31 16:26:08 +01:00
$tabData [ 'bilanN1' . $poste ] = $tabBi [ $poste ];
2012-01-13 14:35:11 +01:00
}
}
}
}
return $tabData ;
}
function bilanN2Entete ( $values )
{
if ( $values !== false ) {
$values = explode ( ',' , $values );
$tmp = array ();
foreach ( $values as $value ){
$tmp [] = 'bilanN2' . $value ;
}
return array_merge (
array ( 'bilanN2dateCloture' , 'bilanN2dureeMois' )
, $tmp
);
} else {
return array ( 'bilanN2dateCloture' , 'bilanN2dureeMois' );
}
}
function bilanN2Data ( $siren , $nic , $values = false )
{
require_once 'Metier/partenaires/classMBilans.php' ;
$tabPostes = explode ( ',' , $values );
$annee = 2 ;
2012-01-27 16:55:25 +01:00
$tabData = array ();
2012-01-13 14:35:11 +01:00
$mBil = new MBilans ( $siren );
2012-01-27 16:55:25 +01:00
$tabBilans = $mBil -> listeBilans ( false , 9 );
if ( count ( $tabBilans ) > 0 ) {
2012-01-30 22:09:14 +01:00
$tabBi = findBilan ( $siren , $tabBilans , $annee );
2012-01-31 16:26:08 +01:00
$tabData [ 'bilanN2dateCloture' ] = $tabBi [ 'DATE_CLOTURE' ];
$tabData [ 'bilanN2dureeMois' ] = $tabBi [ 'DUREE_MOIS' ];
2012-01-27 16:55:25 +01:00
if ( count ( $tabPostes ) > 0 ) {
2012-01-13 14:35:11 +01:00
foreach ( $tabPostes as $poste ) {
if ( isset ( $tabBi [ $poste ])){
2012-01-31 16:26:08 +01:00
$tabData [ 'bilanN2' . $poste ] = $tabBi [ $poste ];
2012-01-13 14:35:11 +01:00
}
}
}
}
return $tabData ;
}
2012-01-30 22:09:14 +01:00
function findBilan ( $siren , $tabBilans , $position = 0 )
2012-01-27 16:55:25 +01:00
{
2012-01-30 22:09:14 +01:00
require_once 'Metier/partenaires/classMBilans.php' ;
$mBil = new MBilans ( $siren );
2012-01-27 16:55:25 +01:00
//Trier et Supprimer les bilans consolidés
$tabBilN = $tabBilS = array ();
2012-01-30 22:09:14 +01:00
foreach ( $tabBilans as $typeMil => $bilan ) {
$temp = $mBil -> getBilan ( substr ( $typeMil , 1 ), substr ( $typeMil , 0 , 1 ));
$annee = $bilan [ 'millesime' ];
if ( substr ( $typeMil , 0 , 1 ) == 'N' ) {
$tabBilN [ $annee ] = $temp ;
} elseif ( substr ( $typeMil , 0 , 1 ) == 'S' ) {
$tabBilS [ $annee ] = $temp ;
2012-01-27 16:55:25 +01:00
}
}
$tabBil = $tabBilN + $tabBilS ; //On comble les trous par des bilans simplifié
krsort ( $tabBil ); // Tri décroissant
$tabBi = array ();
$countBilan = 0 ;
foreach ( $tabBil as $annee => $bilan ){
$tabBi = $bilan ;
if ( $countBilan == $position ) break ;
$countBilan ++ ;
}
if ( $tabBi [ 'CONSOLIDE' ] == 'S' ){
$bilanT = bilanSimplifie2Normal ( $tabBi );
$tabBi = $tabBi + $bilanT ;
}
return $tabBi ;
}
function bilanSimplifie2Normal ( $bilanRS )
{
$tabBS2BN = array (
'AH' => '010' ,
'AI' => '012' ,
'AI1' => '013' ,
'AJ' => '014' ,
'AK' => '016' ,
'AK1' => '017' ,
'AT' => '028' ,
'AU' => '030' ,
'AU1' => '031' ,
'BH' => '040' ,
'BI' => '042' ,
'BI1' => '043' ,
'BJ' => '044' ,
'BK' => '048' ,
'BK1' => '049' ,
'BL' => '050' ,
'BM' => '052' ,
'BM1' => '053' ,
'BT' => '060' ,
'BU' => '062' ,
'BU1' => '063' ,
'BV' => '064' ,
'BW' => '066' ,
'BW1' => '067' ,
'BX' => '068' ,
'BY' => '070' ,
'BY1' => '071' ,
'BZ' => '072' ,
'CA' => '074' ,
'CA1' => '075' ,
'CD' => '080' ,
'CE' => '082' ,
'CE1' => '083' ,
'CF' => '084' ,
'CG' => '086' ,
'CG1' => '087' ,
'CH' => '092' ,
'CI' => '094' ,
'CI1' => '095' ,
'CJ' => '096' ,
'CK' => '098' ,
'CK1' => '099' ,
'CO' => '110' ,
'1A' => '112' ,
'1A1' => '113' ,
'DA' => '120' ,
'DC' => '124' ,
'DD' => '126' ,
'DF' => '130' ,
'DG' => '132' ,
'DH' => '134' ,
'DI' => '136' ,
'DK' => '140' ,
'DL' => '142' ,
'DR' => '154' ,
'DP' => '154' ,
'DU' => '156' ,
'DV' => '169' ,
'DW' => '164' ,
'DX' => '166' ,
'EA' => '172-169' ,
'EB' => '174' ,
'EC' => '176' ,
'EE' => '180' ,
'EH' => '156-195' ,
'FA' => '210-209' ,
'FB' => '209' ,
'FC' => '210' ,
'FD' => '214-215' ,
'FE' => '215' ,
'FF' => '214' ,
'FH' => '217' ,
'FI' => '218' ,
'FK' => '209+215+217' ,
'FL' => '210+214+218' ,
'FM' => '222' ,
'FN' => '224' ,
'FO' => '226' ,
'FQ' => '230' ,
'FR' => '232' ,
'FS' => '234' ,
'FT' => '236' ,
'FU' => '238' ,
'FV' => '240' ,
'FW' => '242' ,
'FX' => '244' ,
'FY' => '250' ,
'FZ' => '252' ,
'GA' => '254' ,
'GE' => '262' ,
'GF' => '264' ,
'GG' => '270' ,
'GP' => '280' ,
'GU' => '294' ,
'GW' => '270+280+294' ,
'HD' => '290' ,
'HH' => '300' ,
'HI' => '290-300' ,
'HK' => '306' ,
'HL' => '232+280+290' ,
'HM' => '264+294+300+306' ,
'HN' => '310' ,
'YY' => '374' ,
'YZ' => '378' ,
'YP' => '376' ,
);
$bilanRN = array ();
foreach ( $tabBS2BN as $posteRN => $formule ) {
if ( preg_match ( '/\+|\-/' , $formule )) {
$tabTmp = preg_split ( '/\+|\-/' , $formule , - 1 , PREG_SPLIT_OFFSET_CAPTURE );
//$bilanRN[$posteRN]=0;
$scalc = '' ;
foreach ( $tabTmp as $i => $tab ) {
if ( $i == 0 ) {
$bilanRN [ $posteRN ] = $bilanRS [ $tab [ 0 ]];
$scalc .= $bilanRS [ $tab [ 0 ]];
}
else {
$signe = $formule [ $tab [ 1 ] - 1 ];
$scalc .= $signe ;
if ( $signe == '+' ) $bilanRN [ $posteRN ] += $bilanRS [ $tab [ 0 ]];
elseif ( $signe == '-' ) $bilanRN [ $posteRN ] -= $bilanRS [ $tab [ 0 ]];
$scalc .= $bilanRS [ $tab [ 0 ]];
}
}
$bilanRN [ $posteRN ] = $bilanRN [ $posteRN ];
}
else $bilanRN [ $posteRN ] = $bilanRS [ $formule ];
}
if ( $bilanRS [ '240' ] <> 0 ) {
$bilanRN [ 'BL' ] = $bilanRS [ '050' ];
$bilanRN [ 'BM' ] = $bilanRS [ '052' ];
} else {
$bilanRN [ 'BN' ] = $bilanRS [ '050' ];
$bilanRN [ 'BO' ] = $bilanRS [ '052' ];
}
if ( $bilanRS [ '070' ] <> 0 || $bilanRS [ '074' ] <> 0 || $bilanRS [ '052' ] <> 0 || $bilanRS [ '062' ] <> 0 )
$bilanRN [ 'GC' ] = $bilanRS [ '256' ];
elseif ( $bilanRS [ '070' ] == 0 && $bilanRS [ '074' ] == 0 && $bilanRS [ '052' ] == 0 && $bilanRS [ '062' ] == 0 && $bilanRS [ '254' ] <> 0 )
$bilanRN [ 'GD' ] = $bilanRS [ '256' ];
if ( $bilanRS [ '584' ] <> 0 ) {
$bilanRN [ 'HB' ] = $bilanRS [ '584' ];
$bilanRN [ 'HA' ] = $bilanRS [ '290' ] - $bilanRS [ '584' ];
} else
$bilanRN [ 'HA' ] = $bilanRS [ '290' ];
if ( $bilanRS [ '582' ] <> 0 ) {
$bilanRN [ 'HF' ] = $bilanRS [ '582' ];
$bilanRN [ 'HE' ] = $bilanRS [ '582' ] - $bilanRS [ '300' ];
} else
$bilanRN [ 'HE' ] = $bilanRS [ '300' ];
return $bilanRN ;
}
2012-01-13 14:35:11 +01:00
function annoncesEntete ( $values )
{
if ( $values !== false ) {
$values = explode ( ',' , $values );
$tmp = array ();
foreach ( $values as $value ){
$tmp [] = 'Annonce' . $value . 'Lib' ;
$tmp [] = 'Annonce' . $value . 'Date' ;
$tmp [] = 'Annonce' . $value . 'DateParution' ;
}
return $tmp ;
} else {
return array ();
}
}
function annoncesData ( $siren , $nic , $values = false )
{
2012-02-01 15:34:51 +01:00
$tabData = array ();
2012-01-13 14:35:11 +01:00
if ( $values !== false ) {
$tabDeja = array ();
$tabTmp = explode ( ',' , $values );
$iInsee = new MInsee ();
$tabEven = $iInsee -> getAnnoncesLegales ( $siren , 0 , $tabTmp );
foreach ( $tabTmp as $even ){
foreach ( $tabEven as $tabAnn ){
foreach ( $tabAnn [ 'evenements' ] as $codEve ){
if ( $even == $codEve [ 'CodeEven' ] && ! in_array ( $even , $tabDeja )) {
$tabDeja [] = $even ;
$dateEven = $tabAnn [ 'dateJugement' ];
if ( $dateEven == '0000-00-00' ) $dateEven = $tabAnn [ 'dateEffet' ];
if ( $dateEven == '0000-00-00' ) $dateEven = $tabAnn [ 'DateParution' ];
$tabData [ 'Annonce' . $even . 'Lib' ] = strtr ( $codEve [ 'LibEven' ], array ( ',' => ' ' , " ' " => ' ' , '"' => ' ' , ';' => ' ' ));
$tabData [ 'Annonce' . $even . 'Date' ] = $dateEven ;
$tabData [ 'Annonce' . $even . 'DateParution' ] = $tabAnn [ 'DateParution' ];
}
}
}
}
2012-02-01 15:34:51 +01:00
}
return $tabData ;
2012-01-13 14:35:11 +01:00
}
/* == FUNCTION == */
/**
* Verifie si un SIREN est valide
* @ param Le code SIREN dont on veut vérifier la validité .
* @ return Un booléen qui vaut 'true' si le code SIREN passé en
* paramètre est valide , false sinon .
*/
function sirenValide ( $siren ) {
if ( ( strlen ( $siren ) != 9 ) || ( is_nan ( $siren )) )
$estValide = false ;
else {
// Donc le SIREN est un numérique à 9 chiffres
$somme = 0 ;
$tmp = 0 ;
for ( $cpt = 0 ; $cpt < strlen ( $siren ); $cpt ++ ) {
if (( $cpt % 2 ) == 1 ) { // Les positions paires : 2ème, 4ème, 6ème et 8ème chiffre
$tmp = substr ( $siren , $cpt , 1 ) * 2 ; // On le multiplie par 2
if ( $tmp > 9 )
$tmp -= 9 ; // Si le résultat est supérieur à 9, on lui soustrait 9
}
else
$tmp = substr ( $siren , $cpt , 1 );
$somme += intval ( $tmp );
}
if (( $somme % 10 ) == 0 )
$estValide = true ; // Si la somme est un multiple de 10 alors le SIREN est valide
else
$estValide = false ;
}
return $estValide ;
}
/**
* Verifie si un SIRET est valide
* @ param Le code SIRET dont on veut vérifier la validité .
* @ return Un booléen qui vaut 'true' si le code SIRET passé en
* paramètre est valide , false sinon .
*/
function siretValide ( $siret ) {
if ( ( strlen ( $siret ) != 14 ) || ( is_nan ( $siret )) )
$estValide = false ;
else {
// Donc le SIRET est un numérique à 14 chiffres
// Les 9 premiers chiffres sont ceux du SIREN (ou RCS), les 4 suivants
// correspondent au numéro d'établissement
// et enfin le dernier chiffre est une clef de LUHN.
$somme = 0 ;
$tmp = 0 ;
for ( $cpt = 0 ; $cpt < strlen ( $siret ); $cpt ++ ) {
if (( $cpt % 2 ) == 0 ) { // Les positions impaires : 1er, 3è, 5è, etc...
$tmp = substr ( $siret , $cpt , 1 ) * 2 ; // On le multiplie par 2
if ( $tmp > 9 )
$tmp -= 9 ; // Si le résultat est supérieur à 9, on lui soustrait 9
}
else
$tmp = substr ( $siret , $cpt , 1 );
$somme += intval ( $tmp );
}
if (( $somme % 10 ) == 0 )
$estValide = true ; // Si la somme est un multiple de 10 alors le SIRET est valide
else
$estValide = false ;
}
return $estValide ;
}
/**
* writeCSV
* @ param array $list
* @ param array $entete
* @ param string $filename
*/
function writeCSV ( $list , $entete , $filename )
{
$fp = fopen ( $filename , 'w' );
if ( count ( $entete ) > 0 ){
fputcsv ( $fp , $entete , ',' , '"' );
}
foreach ( $list as $fields ) {
fputcsv ( $fp , $fields , ',' , '"' );
}
fclose ( $fp );
}