batch/includes/partenaires/classMQualibat.php

343 lines
13 KiB
PHP
Raw Normal View History

<?
class MQualibat {
private $referer ='';
private $body = '';
private $header = '';
private $cookie = '';
private $codeRetour = 0;
private $accesDist=true;
private $iDb;
private $iBodacc;
private $iInsee;
public $enCache=false;
public $force=false;
public $annee=0;
function __construct($accesDist=true) {// $adresse, $cp, $ville, $pays='France') {
$this->accesDist=$accesDist;
$this->iDb=new WDb('sdv1');
$this->iBodacc=new MBodacc();
$this->$iInsee=new MInsee();
}
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 {
if (preg_match("/directeur.{1,6}agence/i")) return 8;
elseif (preg_match("/directeur.{1,6}ETABLISSEMENT/i")) return 8;
elseif (preg_match("/directeur.{1,6}technique/i")) return 19;
elseif (preg_match("/chef.{1,8}agence/i")) return 38;
elseif (preg_match("/cadre.{1,8}Comptable/i")) return 55;
elseif (preg_match("/Charg.{1,6}affair/i")) return 60;
elseif (preg_match("/chef.{1,8}Atelier/i")) return 60;
elseif (preg_match("/(conducteur|chef).{1,9}travaux/i")) return 61;
elseif (preg_match("/directeur.{1,6}technique/i")) return 19;
elseif (preg_match("/resp.{1,16}technique/i")) return 49;
elseif (preg_match("/resp.{1,16}Expl/i")) return 37;
elseif (preg_match("/di.{1,16}Expl/i")) return 7;
elseif (preg_match("/Chef.{1,6}Ent/i")) {
$tabTmp=$this->iInsee->getIdentiteLight($siren);
if ($tabTmp['FJ']>=1000 && $tabTmp['FJ']<2000) return 1050;
else die($tabTmp);
}
else return false;
}
/*
"1","- DIRECTEUR GENERAL"
"1","- P.d.g."
"1","- PRESIDENT CONSEIL D''ADMINISTRATION"
"2","-co-gerant"
"1","Administrateur"
"2","Artisan"
"1","Associ<EFBFBD>"
"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<EFBFBD>gu<67>"
"61","Directeur G<>n<EFBFBD>ral"
"11","DIRECTEUR GENERAL DELEGUE"
"1","Directeur G<>n<EFBFBD>ral Unique"
"1","Directrice"
"2","DIRIG."
"4","Dirigeant"
"38","G<EFBFBD>r"
"857","Gerant"
"3","Gerant -"
"13","G<EFBFBD>rant Associ<63>"
"1","G<EFBFBD>rant Associ<63> En Non Collectif"
"1","GERANT MAJORITAIRE"
"2","G<EFBFBD>rant Non Associ<63>"
"56","G<EFBFBD>rante"
"4","P D G"
"1","P.d.g"
"1","PCA"
"168","PRESIDENT"
"1","President Conseil De Surveillance"
"1","PRESIDENT CONSEIL SURVEILLANCE"
"63","Pr<EFBFBD>sident Directeur G<>n<EFBFBD>ral"
"1","PRESIDENT DIRECTOIRE"
"16","Pr<EFBFBD>sident du conseil d'administration"
"1","Pr<EFBFBD>sident Du Conseil De Surveillance"
"5","President Du Directoire"
"10","Presidente"
"1","Repr<EFBFBD>sentant Soci<63>t<EFBFBD> Pr<50>sidente"
"1","Vice-pr<70>sident"
G<EFBFBD>rer en fonction du code CJ
"128","Chef D'entreprise"
"2","Chef Ent."
"3","Chef Entr."
"1","Conjoint Collaborateur"=>0500
"1","Metreur"
"1","Pr<EFBFBD>sident De Commission"
*/
}
public function getMaxQualibat() {
$ret=$this->iDb->select('qualibat','MAX(id) AS id', '1', false, MYSQL_ASSOC);
return $ret[0]['id'];
}
public function getTabQualibatManquants() {
$tabRet=$tabQualibatCalc=$tabQualibatBase=array();
// Liste des Qualibat Calcul<75>s
$maxQualibat=$this->getMaxQualibat();
for($i=1; $i<=$maxQualibat; $i++)
$tabQualibatCalc[]=$i;
// Liste des Qualibat en base
$ret=$this->iDb->select('qualibat','id', '1 ORDER BY id ASC', false, MYSQL_ASSOC);
foreach ($ret as $i=>$res)
$tabQualibatBase[]=$res['id'];
return array_diff($tabQualibatCalc, $tabQualibatBase);
}
public function getInfosQualibat($siren, $idQualibat=0) {
if ($siren*1>1000)
$strWhere="siren=$siren";
elseif ($idQualibat*1>0) {
$strWhere="id=$idQualibat";
} else return false;
$ret=$this->iDb->select('qualibat','siren, actif, id, nom, adresse, cp, ville, tel, fax, email, web,
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();
// Ajout des qualifications
$ret=$this->iDb->select('qualibatqualif','code, periodQualif, niveauQualif, mentions, nomQualif, dateAttrib, dateEch', $strWhere, false, MYSQL_ASSOC);
foreach ($ret as $i=>$tabTmp)
$tabRet['qualifications'][]=$tabTmp;
// Ajout des dirigeants
$ret=$this->iDb->select('qualibatdir','civNomPrenom, civilite, nom, prenom, fonction', $strWhere, false, MYSQL_ASSOC);
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<75>ro Qualibat inexistant';
// On enregistre ce num<75>ro comme non attribu<62> si < au dernier num<75>ro attribu<62>
if ($idQualibat<$this->getMaxQualibat())
$this->iDb->insert('qualibat', $tabInsert);
}
return false;
}
$body=$page['body'];
//die($body);
$tabRet['id']=$idQualibat;
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)));
}
if (preg_match('/<th>(?:.*)Téléphone(?:.*)<\/th>(?:.*)<td>(.*)<\/td>/Uis',$body, $matches))
$tabRet['tel']=trim(str_replace(' ','',$matches[1]));
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]));
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;
}
if (preg_match('/<th>(?:.*)Forme Juridique(?:.*)<\/th>(?:.*)<td>(.*)<\/td>/Uis',$body, $matches))
$tabRet['libFJ']=utf8_decode(trim($matches[1]));
if (preg_match('/<th>(?:.*)NACE(?:.*)<\/th>(?:.*)<td>(.*)<\/td>/Uis',$body, $matches))
$tabRet['nace']=trim($matches[1]);
if (preg_match('/<th>(?:.*)Fondation(?:.*)<\/th>(?:.*)<td>(.*)<\/td>/Uis',$body, $matches))
$tabRet['dateFondation']=Wdate::dateT('d/m/Y', 'Y-m-d', trim($matches[1]));
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='';
if (preg_match('/<table class="recherche_entreprise_detail">(?:.*)<th colspan="4">(?:.*)(Qualifications professionnelles|Certifications m..tier)(?:.*)<tbody>(.*)<\/table>/Uis',$body, $matches)) {
$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]));
}
$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]));
if (preg_match("/\(Tech?nicit. (.*)\)/Ui", $nomQualif, $matches2))
$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),
'dateAttrib'=>Wdate::dateT('d/m/Y', 'Y-m-d', trim($matches[3][$i])),
'dateEch'=>Wdate::dateT('d/m/Y', 'Y-m-d', trim($matches[4][$i])));
$tabDateAttrib[]=Wdate::dateT('d/m/Y', 'Ymd', trim($matches[3][$i]))*1;
$tabDateFin[]=Wdate::dateT('d/m/Y', 'Ymd', trim($matches[4][$i]))*1;
}
}
sort($tabDateAttrib);
rsort($tabDateFin);
/** 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>
Monsieur EPP Olivier
</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,
'fonctionCode'=>$this->getCodeFctDirigeant($libFonction, $siren),
);
}
//print_r($tabRet['dirigeants']);
//die();
}
/** Insertion de l'entreprise
**/
$tabInsert=array_merge($tabRet,
array( 'idQualibatAttribue'=>1,
'dateInsert'=>date('YmdHis'),
'dateDeb'=>@$tabDateAttrib[0],
'dateFin'=>@$tabDateFin[0]));
unset($tabInsert['qualifications']);
if (!$this->iDb->insert('qualibat', $tabInsert)) {
$this->iDb->update('qualibat', $tabInsert, "id=$idQualibat");
}
/** 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');
if (!$this->iDb->insert('qualibatqualif', $tabInsert)) {
$this->iDb->update('qualibatqualif', $tabInsert, "id=$idQualibat AND code='".$tabInsert['code']."' AND periodQualif='".$tabInsert['periodQualif']."'");
}
}
/** 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');
if (!$this->iDb->insert('qualibatdir', $tabInsert)) {
$this->iDb->update('qualibatdir', $tabInsert, "id=$idQualibat AND civNomPrenom='".addslashes($tabInsert['civNomPrenom'])."' AND fonction='".addslashes($tabInsert['fonction'])."'");
}
unset($tabRet['dirigeants'][$i]['civNomPrenom']);
}
}
return $tabRet;
}
}
?>