2016-02-15 21:31:44 +00:00
< ? php
2013-11-05 11:18:30 +00:00
2016-01-27 21:10:45 +00:00
class Metier_Partenaires_MQualibat {
2013-11-05 11:18:30 +00:00
private $referer = '' ;
private $body = '' ;
private $header = '' ;
private $cookie = '' ;
private $codeRetour = 0 ;
private $accesDist = true ;
private $iDb ;
private $iBodacc ;
private $iInsee ;
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
public $enCache = false ;
public $force = false ;
public $annee = 0 ;
function __construct ( $accesDist = true ) { // $adresse, $cp, $ville, $pays='France') {
$this -> accesDist = $accesDist ;
2016-07-18 15:21:48 +02:00
$this -> iDb = new Metier_Util_Db ();
2016-01-27 21:20:37 +00:00
$this -> iBodacc = new Metier_Bodacc_MBodacc ();
$this -> $iInsee = new Metier_Insee_MInsee ();
2013-11-05 11:18:30 +00:00
}
private function getCodeFctDirigeant ( $strLibDirigeant , $siren = 0 ) {
$tabTmp = $this -> iBodacc -> getDirigeants ( $strLibDirigeant );
if ( isset ( $tabTmp [ 0 ][ 'fonction' ]) && $tabTmp [ 0 ][ 'fonction' ] > 0 )
return $tabTmp [ 0 ][ 'fonction' ];
else {
2014-04-03 12:18:00 +00:00
if ( preg_match ( " /directeur. { 1,6}agence/iu " )) return 8 ;
elseif ( preg_match ( " /directeur. { 1,6}ETABLISSEMENT/iu " )) return 8 ;
elseif ( preg_match ( " /directeur. { 1,6}technique/iu " )) return 19 ;
elseif ( preg_match ( " /chef. { 1,8}agence/iu " )) return 38 ;
elseif ( preg_match ( " /cadre. { 1,8}Comptable/iu " )) return 55 ;
elseif ( preg_match ( " /Charg. { 1,6}affair/iu " )) return 60 ;
elseif ( preg_match ( " /chef. { 1,8}Atelier/iu " )) return 60 ;
elseif ( preg_match ( " /(conducteur|chef). { 1,9}travaux/iu " )) return 61 ;
elseif ( preg_match ( " /directeur. { 1,6}technique/iu " )) return 19 ;
elseif ( preg_match ( " /resp. { 1,16}technique/iu " )) return 49 ;
elseif ( preg_match ( " /resp. { 1,16}Expl/iu " )) return 37 ;
elseif ( preg_match ( " /di. { 1,16}Expl/iu " )) return 7 ;
elseif ( preg_match ( " /Chef. { 1,6}Ent/iu " )) {
2013-11-05 11:18:30 +00:00
$tabTmp = $this -> iInsee -> getIdentiteLight ( $siren );
if ( $tabTmp [ 'FJ' ] >= 1000 && $tabTmp [ 'FJ' ] < 2000 ) return 1050 ;
else die ( $tabTmp );
}
2014-04-03 12:18:00 +00:00
else return false ;
2013-11-05 11:18:30 +00:00
}
/*
" 1 " , " - DIRECTEUR GENERAL "
" 1 " , " - P.d.g. "
" 1 " , " - PRESIDENT CONSEIL D''ADMINISTRATION "
" 2 " , " -co-gerant "
" 1 " , " Administrateur "
" 2 " , " Artisan "
" 1 " , " AssociÉ "
" 1 " , " Associes "
" 24 " , " Co Gerant "
" 1 " , " Co Gerante "
" 1 " , " CO GERANTS "
" 109 " , " Co-gérant "
" 12 " , " Co-gérante "
" 1 " , " Dg Delegue "
" 1 " , " Dir. General Delegue "
" 32 " , " Directeur "
" 1 " , " Directeur Adjoint "
" 1 " , " Directeur Administratif et Financier "
" 1 " , " Directeur Gal DÉlÉguÉ "
" 61 " , " Directeur Général "
" 11 " , " DIRECTEUR GENERAL DELEGUE "
" 1 " , " Directeur Général Unique "
" 1 " , " Directrice "
" 2 " , " DIRIG. "
" 4 " , " Dirigeant "
" 38 " , " Gér "
" 857 " , " Gerant "
" 3 " , " Gerant - "
" 13 " , " Gérant Associé "
" 1 " , " Gérant Associé En Non Collectif "
" 1 " , " GERANT MAJORITAIRE "
" 2 " , " Gérant Non Associé "
" 56 " , " Gérante "
" 4 " , " P D G "
" 1 " , " P.d.g "
" 1 " , " PCA "
" 168 " , " PRESIDENT "
" 1 " , " President Conseil De Surveillance "
" 1 " , " PRESIDENT CONSEIL SURVEILLANCE "
" 63 " , " Président Directeur Général "
" 1 " , " PRESIDENT DIRECTOIRE "
" 16 " , " Président du conseil d'administration "
" 1 " , " Président Du Conseil De Surveillance "
" 5 " , " President Du Directoire "
" 10 " , " Presidente "
" 1 " , " ReprÉsentant SociÉtÉ PrÉsidente "
" 1 " , " Vice-président "
2014-04-03 12:18:00 +00:00
Gérer en fonction du code CJ
2013-11-05 11:18:30 +00:00
" 128 " , " Chef D'entreprise "
" 2 " , " Chef Ent. "
" 3 " , " Chef Entr. "
" 1 " , " Conjoint Collaborateur " => 0500
" 1 " , " Metreur "
" 1 " , " Président De Commission "
*/
}
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
public function getMaxQualibat () {
2015-04-27 15:05:08 +00:00
$ret = $this -> iDb -> select ( 'sdv1.qualibat' , 'MAX(id) AS id' , '1' , false , MYSQL_ASSOC );
2013-11-05 11:18:30 +00:00
return $ret [ 0 ][ 'id' ];
}
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
public function getTabQualibatManquants () {
$tabRet = $tabQualibatCalc = $tabQualibatBase = array ();
// Liste des Qualibat Calculés
$maxQualibat = $this -> getMaxQualibat ();
for ( $i = 1 ; $i <= $maxQualibat ; $i ++ )
$tabQualibatCalc [] = $i ;
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
// Liste des Qualibat en base
2015-04-27 15:05:08 +00:00
$ret = $this -> iDb -> select ( 'sdv1.qualibat' , 'id' , '1 ORDER BY id ASC' , false , MYSQL_ASSOC );
2013-11-05 11:18:30 +00:00
foreach ( $ret as $i => $res )
$tabQualibatBase [] = $res [ 'id' ];
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
return array_diff ( $tabQualibatCalc , $tabQualibatBase );
}
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
public function getInfosQualibat ( $siren , $idQualibat = 0 ) {
if ( $siren * 1 > 1000 )
$strWhere = " siren= $siren " ;
elseif ( $idQualibat * 1 > 0 ) {
$strWhere = " id= $idQualibat " ;
} else return false ;
2015-04-27 15:05:08 +00:00
$ret = $this -> iDb -> select ( 'sdv1.qualibat' , ' siren , actif , id , nom , adresse , cp , ville , tel , fax , email , web ,
2013-11-05 11:18:30 +00:00
eff , teff , ca , tca , libFJ , nace , dateFondation , dateDeb , dateFin , dateInsert ' , $strWhere , false , MYSQL_ASSOC );
if ( ! $this -> force && count ( $ret ) > 0 ) {
$this -> enCache = true ;
$tabRet = $ret [ 0 ]; //array();
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
// Ajout des qualifications
2015-04-27 15:05:08 +00:00
$ret = $this -> iDb -> select ( 'sdv1.qualibatqualif' , 'code, periodQualif, niveauQualif, mentions, nomQualif, dateAttrib, dateEch' , $strWhere , false , MYSQL_ASSOC );
2013-11-05 11:18:30 +00:00
foreach ( $ret as $i => $tabTmp )
$tabRet [ 'qualifications' ][] = $tabTmp ;
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
// Ajout des dirigeants
2015-04-27 15:05:08 +00:00
$ret = $this -> iDb -> select ( 'sdv1.qualibatdir' , 'civNomPrenom, civilite, nom, prenom, fonction' , $strWhere , false , MYSQL_ASSOC );
2013-11-05 11:18:30 +00:00
foreach ( $ret as $i => $tabTmp )
$tabRet [ 'dirigeants' ][] = $tabTmp ;
} elseif ( $this -> accesDist == true ) {
$this -> enCache = false ;
// Initialisation Cookies
$this -> referer = 'http://www.qualibat.com/Views/EntreprisesRechercheDetail.aspx?id=' . $idQualibat ;
$page = getUrl ( $this -> referer , '' , '' , '' , false , '' , '' , 3 );
if ( $page [ 'code' ] <> 200 ) {
if ( $page [ 'code' ] == 500 ) {
$tabInsert = array ( 'actif' => 0 ,
'id' => $idQualibat ,
'idQualibatAttribue' => 0 ,
'dateInsert' => date ( 'YmdHis' ));
$this -> erreur = 'Numéro Qualibat inexistant' ;
// On enregistre ce numéro comme non attribué si < au dernier numéro attribué
if ( $idQualibat < $this -> getMaxQualibat ())
2015-04-27 14:25:04 +00:00
$this -> iDb -> insert ( 'sdv1.qualibat' , $tabInsert );
2013-11-05 11:18:30 +00:00
}
return false ;
}
$body = $page [ 'body' ];
//die($body);
$tabRet [ 'id' ] = $idQualibat ;
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
if ( preg_match ( '/<th>(?:.*)Raison sociale(?:.*)<\/th>(?:.*)<td>(.*)<\/td>/Uis' , $body , $matches ))
$tabRet [ 'nom' ] = utf8_decode ( trim ( strip_tags ( $matches [ 1 ])));
if ( preg_match ( '/<th>(?:.*)Adresse(?:.*)<\/th>(?:.*)<td>(.*)<br \/>(.*)<\/td>/Uis' , $body , $matches )) {
$tabRet [ 'adresse' ] = strtoupper ( utf8_decode ( trim ( $matches [ 1 ])));
$adresse2 = trim ( preg_replace ( '/ +/' , ' ' , strtr ( strip_tags ( $matches [ 2 ]), " \r \n \t " . chr ( 160 ), ' ' )));
$tabRet [ 'cp' ] = substr ( $adresse2 , 0 , 5 );
$tabRet [ 'ville' ] = utf8_decode ( trim ( substr ( $adresse2 , 5 )));
}
2014-04-03 12:18:00 +00:00
if ( preg_match ( '/<th>(?:.*)Téléphone(?:.*)<\/th>(?:.*)<td>(.*)<\/td>/Uisu' , $body , $matches ))
2013-11-05 11:18:30 +00:00
$tabRet [ 'tel' ] = trim ( str_replace ( ' ' , '' , $matches [ 1 ]));
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
if ( preg_match ( '/<th>(?:.*)Fax(?:.*)<\/th>(?:.*)<td>(.*)<\/td>/Uis' , $body , $matches ))
$tabRet [ 'fax' ] = trim ( str_replace ( ' ' , '' , $matches [ 1 ]));
if ( preg_match ( '/<th>E-mail<\/th><td>(?:.*)<a href= "mailto\:(.*)">/Uis' , $body , $matches ))
$tabRet [ 'email' ] = utf8_decode ( trim ( $matches [ 1 ]));
if ( preg_match ( '/<th>(?:.*)SIREN(?:.*)<\/th>(?:.*)<td>(.*)<\/td>/Uis' , $body , $matches ))
$tabRet [ 'siren' ] = $siren = trim ( str_replace ( ' ' , '' , $matches [ 1 ]));
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
if ( preg_match ( '/<th>(?:.*)Effectif total(?:.*)<\/th>(?:.*)<td>(.*)\(Classification - (.*)\)(?:.*)<\/td>/Uis' , $body , $matches )) {
$tabRet [ 'eff' ] = trim ( str_replace ( ' ' , '' , $matches [ 1 ]));
$tabRet [ 'teff' ] = trim ( $matches [ 2 ]);
}
if ( preg_match ( '/<th>(?:.*)CA total(?:.*)<\/th>(?:.*)<td >(.*)\(Classification - (.*)\)(?:.*)<\/td>/Uis' , $body , $matches )) {
$tabRet [ 'ca' ] = trim ( str_replace ( ' ' , '' , $matches [ 1 ]));
if ( $tabRet [ 'ca' ] == '' ) $tabRet [ 'ca' ] = NULL ;
$tabRet [ 'tca' ] = trim ( $matches [ 2 ]);
if ( $tabRet [ 'tca' ] == '' ) $tabRet [ 'tca' ] = NULL ;
}
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
if ( preg_match ( '/<th>(?:.*)Forme Juridique(?:.*)<\/th>(?:.*)<td>(.*)<\/td>/Uis' , $body , $matches ))
$tabRet [ 'libFJ' ] = utf8_decode ( trim ( $matches [ 1 ]));
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
if ( preg_match ( '/<th>(?:.*)NACE(?:.*)<\/th>(?:.*)<td>(.*)<\/td>/Uis' , $body , $matches ))
$tabRet [ 'nace' ] = trim ( $matches [ 1 ]);
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
if ( preg_match ( '/<th>(?:.*)Fondation(?:.*)<\/th>(?:.*)<td>(.*)<\/td>/Uis' , $body , $matches ))
2016-07-13 14:35:17 +02:00
$tabRet [ 'dateFondation' ] = Metier_Util_Date :: dateT ( 'd/m/Y' , 'Y-m-d' , trim ( $matches [ 1 ]));
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
if ( preg_match ( '/<tr><td align="center"><img src="(?:.*)internet_contact\.gif(?:.*)<\/td><td> <a href= "(.*)" target="_blank">/Uis' , $body , $matches ))
$tabRet [ 'web' ] = utf8_decode ( trim ( $matches [ 1 ]));
/** Qualifications ? **/
$qualifs = '' ;
2014-04-03 12:18:00 +00:00
if ( preg_match ( '/<table class="recherche_entreprise_detail">(?:.*)<th colspan="4">(?:.*)(Qualifications professionnelles|Certifications m..tier)(?:.*)<tbody>(.*)<\/table>/Uisu' , $body , $matches )) {
2013-11-05 11:18:30 +00:00
$typeQualifs = trim ( utf8_decode ( $matches [ 1 ]));
$qualifs = trim ( $matches [ 2 ]);
}
if ( preg_match ( '/<th>(?:.*)Fondation(?:.*)<\/th>(?:.*)<td>(.*)<\/td>(.*)<div class="buttons">/Uis' , $body , $matches )) {
$tabRet [ 'tmpDirQualif' ] = trim ( $matches [ 2 ]);
$tabRet [ 'tmpLenDirQualif' ] = strlen ( trim ( $matches [ 2 ]));
}
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
$tabDateAttrib = $tabDateFin = array ();
if ( preg_match_all ( '/<tr>(?:.*)<td>(.*)<\/td>(?:.*)<td>(.*)<\/td>(?:.*)<td>(.*)<\/td>(?:.*)<td>(.*)<\/td>(?:.*)<\/tr>/Uis' , $qualifs , $matches )) {
foreach ( $matches [ 1 ] as $i => $code ) {
$tabTmp = explode ( ' ' , trim ( $code ));
$nomQualif = utf8_decode ( trim ( $matches [ 2 ][ $i ]));
2014-04-03 12:18:00 +00:00
if ( preg_match ( " / \ (Tech?nicit. (.*) \ )/Uiu " , $nomQualif , $matches2 ))
2013-11-05 11:18:30 +00:00
$niveau = trim ( $matches2 [ 1 ]);
else $niveau = '' ;
$tabMentions = array ();
if ( preg_match_all ( '/Mention (.*)(?:,|$)/Uis' , $nomQualif , $matches2 )) {
foreach ( $matches2 [ 1 ] as $i2 => $mention )
$tabMentions [] = $mention ;
}
$tabRet [ 'qualifications' ][ $i ] = array (
'code' => trim ( $tabTmp [ 0 ]),
'periodQualif' => trim ( $tabTmp [ 1 ]),
'nomQualif' => $nomQualif ,
'niveauQualif' => $niveau ,
'typeQualif' => $typeQualifs ,
'mentions' => implode ( ', ' , $tabMentions ),
2016-07-13 14:35:17 +02:00
'dateAttrib' => Metier_Util_Date :: dateT ( 'd/m/Y' , 'Y-m-d' , trim ( $matches [ 3 ][ $i ])),
'dateEch' => Metier_Util_Date :: dateT ( 'd/m/Y' , 'Y-m-d' , trim ( $matches [ 4 ][ $i ])));
$tabDateAttrib [] = Metier_Util_Date :: dateT ( 'd/m/Y' , 'Ymd' , trim ( $matches [ 3 ][ $i ])) * 1 ;
$tabDateFin [] = Metier_Util_Date :: dateT ( 'd/m/Y' , 'Ymd' , trim ( $matches [ 4 ][ $i ])) * 1 ;
2013-11-05 11:18:30 +00:00
}
}
sort ( $tabDateAttrib );
rsort ( $tabDateFin );
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
/** Dirigeants ? **/
$dirs = '' ;
if ( preg_match ( '/<table class="recherche_entreprise_detail">(?:.*)<th colspan="2">(?:.*)Dirigeants(?:.*)<tbody>(.*)<\/table>/Uis' , $body , $matches ))
$dirs = trim ( $matches [ 1 ]);
/* < tr >
< td >
2014-04-03 12:18:00 +00:00
Monsieur EPP Olivier
2013-11-05 11:18:30 +00:00
</ td >
< td >
Gérant
</ td >
</ tr >*/
if ( preg_match_all ( '/<tr>(?:.*)<td>(.*)<\/td>(?:.*)<td>(.*)<\/td>(?:.*)<\/tr>/Uis' , $dirs , $matches )) {
foreach ( $matches [ 1 ] as $i => $nom ) {
$strNom = utf8_decode ( trim ( $nom ));
switch ( strtoupper ( substr ( $strNom , 0 , 6 ))) {
case 'MONSIE' : $civ = 'M' ; $pDeb = 8 ; break ;
case 'MADAME' : $civ = 'MME' ; $pDeb = 6 ; break ;
case 'MADEMO' : $civ = 'MLLE' ; $pDeb = 12 ; break ;
default : $civ = '' ; $pDeb = 0 ; break ;
}
$libFonction = ucwords ( strtolower ( utf8_decode ( trim ( $matches [ 2 ][ $i ]))));
$tabRet [ 'dirigeants' ][ $i ] = array (
'civNomPrenom' => $strNom ,
'civilite' => $civ ,
'nom' => strtoupper ( trim ( substr ( $strNom , $pDeb , 65 ))),
'prenom' => ucwords ( strtolower ( trim ( substr ( $strNom , $pDeb + 66 )))),
'fonction' => $libFonction ,
2014-04-03 12:18:00 +00:00
'fonctionCode' => $this -> getCodeFctDirigeant ( $libFonction , $siren ),
2013-11-05 11:18:30 +00:00
);
}
//print_r($tabRet['dirigeants']);
//die();
}
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
/** Insertion de l ' entreprise
**/
2014-04-03 12:18:00 +00:00
$tabInsert = array_merge ( $tabRet ,
2013-11-05 11:18:30 +00:00
array ( 'idQualibatAttribue' => 1 ,
'dateInsert' => date ( 'YmdHis' ),
'dateDeb' =>@ $tabDateAttrib [ 0 ],
'dateFin' =>@ $tabDateFin [ 0 ]));
unset ( $tabInsert [ 'qualifications' ]);
2015-04-27 14:25:04 +00:00
if ( ! $this -> iDb -> insert ( 'sdv1.qualibat' , $tabInsert )) {
$this -> iDb -> update ( 'sdv1.qualibat' , $tabInsert , " id= $idQualibat " );
2013-11-05 11:18:30 +00:00
}
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
/** Insertion des qualifications
**/
if ( isset ( $tabRet [ 'qualifications' ]) && count ( $tabRet [ 'qualifications' ]) > 0 )
foreach ( $tabRet [ 'qualifications' ] as $i => $tabInsert ) {
$tabInsert [ 'id' ] = $idQualibat ;
$tabInsert [ 'siren' ] = $siren ;
$tabInsert [ 'dateInsert' ] = date ( 'YmdHis' );
2015-04-27 14:25:04 +00:00
if ( ! $this -> iDb -> insert ( 'sdv1.qualibatqualif' , $tabInsert )) {
$this -> iDb -> update ( 'sdv1.qualibatqualif' , $tabInsert , " id= $idQualibat AND code=' " . $tabInsert [ 'code' ] . " ' AND periodQualif=' " . $tabInsert [ 'periodQualif' ] . " ' " );
2013-11-05 11:18:30 +00:00
}
}
2014-04-03 12:18:00 +00:00
2013-11-05 11:18:30 +00:00
/** Insertion des dirigeants
**/
if ( isset ( $tabRet [ 'dirigeants' ]) && count ( $tabRet [ 'dirigeants' ]) > 0 )
foreach ( $tabRet [ 'dirigeants' ] as $i => $tabInsert ) {
$tabInsert [ 'id' ] = $idQualibat ;
$tabInsert [ 'siren' ] = $siren ;
$tabInsert [ 'dateInsert' ] = date ( 'YmdHis' );
2015-04-27 14:25:04 +00:00
if ( ! $this -> iDb -> insert ( 'sdv1.qualibatdir' , $tabInsert )) {
$this -> iDb -> update ( 'sdv1.qualibatdir' , $tabInsert , " id= $idQualibat AND civNomPrenom=' " . addslashes ( $tabInsert [ 'civNomPrenom' ]) . " ' AND fonction=' " . addslashes ( $tabInsert [ 'fonction' ]) . " ' " );
2013-11-05 11:18:30 +00:00
}
unset ( $tabRet [ 'dirigeants' ][ $i ][ 'civNomPrenom' ]);
}
}
return $tabRet ;
}
}
?>