2012-01-13 14:35:11 +01:00
#!/usr/bin/php
< ? 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|?' => " Aide. " ,
2012-01-15 22:13:32 +01:00
'id=s' => " Identifiant du traitement " ,
2012-01-13 14:35:11 +01:00
)
);
$opts -> parse ();
} catch ( Zend_Console_Getopt_Exception $e ) {
echo $e -> getUsageMessage ();
exit ;
}
//Usage
2012-01-15 22:13:32 +01:00
if ( isset ( $opts -> help ) || ! isset ( $opts -> id ))
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' );
$db = Zend_Db :: factory ( $dbConfig );
$commandesM = new Application_Model_Commandes ( $db );
$commande = $commandesM -> find ( intval ( $opts -> id ));
$file = basename ( $commande -> fichier );
2012-01-13 14:35:11 +01:00
$extension = strrchr ( $file , '.' );
$file = str_replace ( $extension , '' , $file );
$outFile = $file . '-' . date ( 'YmdHis' ) . '.csv' ;
2012-01-15 22:13:32 +01:00
$profilM = new Application_Model_Profil ( $db );
$profil = $profilM -> find ( $commande -> idProfil );
2012-01-13 14:35:11 +01:00
//Chargement des classes metier
require_once realpath ( dirname ( __FILE__ )) . '/../config/config.php' ;
require_once 'Metier/insee/classMInsee.php' ;
require_once 'framework/common/mysql.php' ;
$tabExtract = array ();
$tabEntete = array ();
$tabIdentifiant = array ();
$tabData = array ();
$posKeySiren = $posKeySiret = $posKeyNic = false ;
//Lire le fichier csv
$row = 0 ;
if (( $handle = fopen ( $opts -> file , 'r' )) !== FALSE ) {
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
$profilEntete = json_decode ( $profil -> criteres , true );
$tabExtract = array_merge ( $tabExtract , $profilEntete );
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
$tabEntete [] = 'SiretValide' ;
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 , ',' , '"' );
}
$commandesM -> update ( array ( 'dateStart' => date ( 'Y-m-d H:i:s' )), " id = " . $commande -> id );
2012-01-13 14:35:11 +01:00
foreach ( $tabIdentifiant as $item )
{
2012-01-15 22:13:32 +01:00
$commandesM -> update ( array ( 'nbLigne' => $row ), " id = " . $commande -> id );
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 ){
$stmt = $db -> query ( $sqlNic . ' AND nic=' . $nic );
$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' ;
$stmt = $db -> query ( $sqlNic . $strNic );
$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 ;
$stmt = $db -> query ( $ { $element . 'SQL' });
$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 = explode(',', ${$item.'Data'});
$values = $ { $item . 'Data' };
}
$tabData [ $row ] = array_merge ( $tabData [ $row ], call_user_func ( $item . 'Data' , $siren , $nicV , $values ));
}
}
}
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-13 14:35:11 +01:00
foreach ( $tabEntete as $key ){
$tabSortie [ $row ][] = isset ( $tabData [ $row ][ $key ]) ? $tabData [ $row ][ $key ] : '' ;
}
2012-01-15 22:13:32 +01:00
foreach ( $tabSortie as $fields ) {
fputcsv ( $fp , $fields , ',' , '"' );
}
2012-01-13 14:35:11 +01:00
$row ++ ;
}
2012-01-15 22:13:32 +01:00
fclose ( $fp );
$commandesM -> update ( array ( 'dateStop' => date ( 'Y-m-d H:i:s' )), " id = " . $commande -> id );
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' ;
$tabPostes = explode ( ',' , $values );
$tabData = array ();
$annee = 0 ;
$mBil = new MBilans ( $siren );
$tabBilans = $mBil -> listeBilans ( false , 3 );
if ( count ( $tabBilans ) > 0 ) {
$tabBil = array ();
foreach ( $tabBilans as $typeMil => $bilan ) {
$tabBil [] = $mBil -> getBilan ( substr ( $typeMil , 1 ), substr ( $typeMil , 0 , 1 ));
}
$tabBi = $tabBil [ $annee ];
$tabData [ 'bilanNdateCloture' ] = $tabBi [ 'DATE_CLOTURE' ];
$tabData [ 'bilanNdureeMois' ] = $tabBi [ 'DUREE_MOIS' ];
if ( count ( $tabPostes ) > 0 ){
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 );
$tabData = array ();
$annee = 1 ;
$mBil = new MBilans ( $siren );
$tabBilans = $mBil -> listeBilans ( false , 3 );
if ( count ( $tabBilans ) > 0 ) {
$tabBil = array ();
foreach ( $tabBilans as $typeMil => $bilan ) {
$tabBil [] = $mBil -> getBilan ( substr ( $typeMil , 1 ), substr ( $typeMil , 0 , 1 ));
}
$tabBi = $tabBil [ $annee ];
$tabData [ 'bilanN1dateCloture' ] = $tabBi [ 'DATE_CLOTURE' ];
$tabData [ 'bilanN1dureeMois' ] = $tabBi [ 'DUREE_MOIS' ];
if ( count ( $tabPostes ) > 0 ){
foreach ( $tabPostes as $poste ) {
if ( isset ( $tabBi [ $poste ])){
$tabData [ 'bilanN1' . $poste ] = $tabBi [ $poste ];
}
}
}
}
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 );
$tabData = array ();
$annee = 2 ;
$mBil = new MBilans ( $siren );
$tabBilans = $mBil -> listeBilans ( false , 3 );
if ( count ( $tabBilans ) > 0 ) {
$tabBil = array ();
foreach ( $tabBilans as $typeMil => $bilan ) {
$tabBil [] = $mBil -> getBilan ( substr ( $typeMil , 1 ), substr ( $typeMil , 0 , 1 ));
}
$tabBi = $tabBil [ $annee ];
$tabData [ 'bilanN2dateCloture' ] = $tabBi [ 'DATE_CLOTURE' ];
$tabData [ 'bilanN2dureeMois' ] = $tabBi [ 'DUREE_MOIS' ];
if ( count ( $tabPostes ) > 0 ){
foreach ( $tabPostes as $poste ) {
if ( isset ( $tabBi [ $poste ])){
$tabData [ 'bilanN2' . $poste ] = $tabBi [ $poste ];
}
}
}
}
return $tabData ;
}
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 )
{
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' ];
}
}
}
}
return $tabData ();
} else {
return $tabData ();
}
}
/* == 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 );
}