batch/includes/partenaires/classMArtisanat.php

309 lines
17 KiB
PHP
Raw Normal View History

<?
define ('ARTISANAT_DISPO_WEB', 1);
include_once(FWK_PATH.'common/curl.php');
class MArtisanat {
public $body = '';
public $header = '';
public $codeRetour = 0;
public $referer='';
public $libErreur='';
public $cookie='';
public $iDb;
public $enCache=false;
function __construct() {
$this->iDb=new WDB();
}
function getIdentite($siren, $refresh=false) {
$siren=$siren*1;
$res=$this->iDb->select('artisanat', 'id, siren, actif, numRM, denomination, sigle, nomCommercial, enseigne, fj, effectif, aprm, debutActivite, activite, adresse, cp, ville, cessation, radiation, nbInscriptions, nom, prenom, nomUsage, dateNaiss, lieuNaiss, natio, qualite, qualif, dateQualif, dateFctDeb, dateFctFin, IF(dateInsert>dateUpdate,dateInsert,dateUpdate) AS dateUpdate', "siren=$siren", false, MYSQL_ASSOC);
if (count($res)>0 && !$refresh) {
$this->enCache=true;
$tabInsert=$res[0];
} elseif (ARTISANAT_DISPO_WEB) {
$this->enCache=false;
$url='http://www.cma-paris.fr/CMP/rm_recherche.php?dom=Gerer&fonction=recherche';
$page=getUrl($url, '', '', $this->referer, false, 'www.cma-paris.fr', false, '', '', 21);
$this->body=$page['body'];
$this->codeRetour=$page['code'];
if ($this->codeRetour<>'200') {
$this->libErreur='Erreur Artisanat 200a !';
return false;
}
$this->cookie=$page['header']['Set-Cookie'];
$this->referer=$url;
$url='http://www.cma-paris.fr/CMP/rm_recherche.php?dom=Gerer&fonction=cherche';
$postData=array( 'siren_op'=>'%3D',
'siren'=>$siren,
'den_rent_op'=>'%25',
'den_rent'=>'',
'adresse_op'=>'contient',
'adresse'=>'',
'bde_rent_op'=>'%25',
'bde_rent'=>'',
'nom_rdir_op'=>'%25',
'nom_rdir'=>'',
'pren_rdir_op'=>'%25',
'pren_rdir'=>'',
'table'=>'rentreprise',//'rentrad',
'x'=>56,
'y'=>14,
);
randsleep(1,2);
$page=getUrl($url, $this->cookie, $postData, $this->referer, false, 'www.cma-paris.fr', false, '', '', 21);
$this->body=$page['body'];
$this->codeRetour=$page['code'];
if ($this->codeRetour<>'200') {
$this->libErreur='Erreur Artisanat 200b !';
return false;
}
$this->referer=$url;
if (preg_match('/<span.class="rouge">aucune.entreprise.ne.correspond(?:.*)vos.crit(?:.*)de.recherche<\/span>/Uis', $this->body, $matches)) {
$url='http://www.cma-paris.fr/CMP/rm_recherche.php?dom=Gerer&fonction=cherche';
$postData=array( 'siren_op'=>'%3D',
'siren'=>$siren,
'den_rent_op'=>'%25',
'den_rent'=>'',
'adresse_op'=>'contient',
'adresse'=>'',
'bde_rent_op'=>'%25',
'bde_rent'=>'',
'nom_rdir_op'=>'%25',
'nom_rdir'=>'',
'pren_rdir_op'=>'%25',
'pren_rdir'=>'',
'table'=>'rentrad',
'x'=>56,
'y'=>14,
);
randsleep(1,2);
$page=getUrl($url, $this->cookie, $postData, $this->referer, false, 'www.cma-paris.fr', false, '', '', 21);
$this->body=$page['body'];
$this->codeRetour=$page['code'];
if ($this->codeRetour<>'200') {
$this->libErreur='Erreur Artisanat 200c !';
return false;
}
$this->referer=$url;
}
/** Gestion des multi-inscriptions au RM **/
if (preg_match('/<b class="gris">(.*)entreprises.correspondent(?:.*)vos.crit(?:.*)de.recherche<\/span>/Uis', $this->body, $matches))
{
$nbRep=trim($matches[1])*1;
$iRadMax=-1;
if (preg_match_all('/<a class="turquoise" href="(rm\.php\?dom=Gerer&amp;fonction=dossier&amp;(.*))">/Uis', $this->body, $matches)) {
$tabUrls=array_unique($matches[1]);
$anRadMax=0;
// Recherche de la derni<6E>re inscription
foreach ($tabUrls as $iUrl=>$url) {
// On prend la derni<6E>re inscription
$tabTmp=explode('&amp;',$url);
if (substr($tabTmp[3],0,8)=='an_rera=') {
$anRad=substr($tabTmp[3],8);
if ($anRad>$anRadMax) {
$anRadMax=$anRad;
$iRadMax=$iUrl;
}
}
}
}
if ($iRadMax==-1) die('$iRadMax==-1 Cas impossible !'.EOL.print_r($tabUrls,true).EOL.print_r($matches,true));
// Acc<63>s <20> la derni<6E>re fiche
$url='http://www.cma-paris.fr/CMP/'.strtr(trim($tabUrls[$iRadMax]),array('&amp;'=>'&'));
randsleep(1,2);
$page=getUrl($url, $this->cookie, '', $this->referer, false, 'www.cma-paris.fr', false, '', '', 21);
$this->body=$page['body'];
$this->codeRetour=$page['code'];
if ($this->codeRetour<>'200') {
$this->libErreur='Erreur Artisanat 200c !';
return false;
}
$this->referer=$url;
}
$tabInsert=array();
if (preg_match('/n&deg; d\'identification \(SIREN\)<\/span><\/td>(?:.*)<td align="left" nowrap>(.*)&nbsp;<\/td>((?:.*)n&deg; de gestion rm<\/span><\/td>(?:.*)<td align="left" nowrap>(.*)<\/td>)?/Uis', $this->body, $matches)) {
$sirenLu=$matches[1]*1;
if ($siren==$sirenLu) {
$tabInsert['siren']=$matches[1];
$tabInsert['numRM']=@$matches[2];
} else {
$this->libErreur='Erreur Siren $sirenLu<>$siren !';
return false;
}
} elseif (preg_match('/<span class="rouge">aucune entreprise ne correspond &agrave; vos crit&egrave;res de recherche<\/span>/Uis', $this->body, $matches)) {
$this->libErreur='Erreur Artisan absent de la base Artisanat !';
// die($this->body);
return false;
} else {
$this->libErreur='Erreur Siren absent dans la page !';
// die($this->body);
return false;
}
$actif=null;
if (preg_match("/Renseignements.relatif(?:.*)entreprise(.*)<\/b>/Uis", $this->body, $matches)) {
switch (trim(strtr($matches[1],array(chr(160)=>'')))) {
case 'en activit&eacute;': $actif=1; break;
case 'radi&eacute;e': $actif=0; break;
default: print_r($matches);die(); break;
}
//die(EOL.'$actif='.$actif.EOL.print_r($matches));
}
$tabInsert['actif']=$actif;
if ($actif===null) {
//die("siren=$siren".EOL."sirenLu=$sirenLu".EOL.$this->body);
$this->libErreur='Erreur Actif/Radi<64> non trouv<75> !';
return false;
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">d&eacute;nomination<\/span><\/td>(?:.*)<td align="left" colspan="5">(.*)<\/td>/Uis', $this->body, $matches)) {
$tabInsert['denomination']=$matches[1];
}
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">d&eacute;nomination<\/span><\/td>(?:.*)<td align="left" colspan="5">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['denomination']=$matches[1];
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">sigle<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['sigle']=$matches[1];
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">&nbsp;nom&nbsp;commercial<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['nomCommercial']=$matches[1];
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">enseigne<\/span><\/td>(?:.*)<td align="left" colspan="4">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['enseigne']=$matches[1];
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">forme&nbsp;juridique<\/span><\/td>(?:.*)<td align="left" colspan="5">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['fj']=$matches[1];
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">effectif<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['effectif']=$matches[1];
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">code APRM<\/span><\/td>(?:.*)<td align="left">(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['aprm']=$matches[1];
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">d&eacute;but&nbsp;d\'activit&eacute;<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['debutActivite']=WDate::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">&nbsp;activit&eacute;\(s\) exerc&eacute;e\(s\) donnant lieu &agrave; immatriculation<\/span>(?:.*)<td colspan="5">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['activite']=trim($matches[1]);
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">adresse&nbsp;de&nbsp;l\'entreprise<\/span><\/td>(?:.*)<td align="left" colspan="5">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['adresse']=trim($matches[1]);
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">code postal<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['cp']=trim($matches[1]);
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">ville<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['ville']=trim($matches[1]);
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">cessation d\'activit&eacute;<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['cessation']=WDate::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">radiation du RM<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['radiation']=WDate::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
$tabEtabs=array();
if (preg_match('/<!--deb Etablissements secondaires-->(?:.*)<b class="gris">Aucune inscription compl&eacute;mentaire<\/b>(?:.*)<!--fin Etablissements secondaires-->/Uis', $this->body, $matches))
$tabInsert['nbInscriptions']=0;
elseif (preg_match('/<!--deb Etablissements secondaires-->(?:.*)<b class="gris">Inscription compl&eacute;mentaire<\/b>(.*)<!--fin Etablissements secondaires-->/Uis', $this->body, $matches)) {
$tabInsert['nbInscriptions']=0;
if (preg_match_all('/<tr>(?:.*)<td><img alt="" src="inter\/pixtrans\.gif" width="30" height="1"><\/td>(?:.*)<td nowrap>(?:.*)<!-- adresse -->(.*)<\/td>(?:.*)<td nowrap>(?:.*)<!-- code postal et ville -->(.*)<\/td>(?:.*)<td nowrap>(?:.*)<!-- enseigne -->(.*)<\/td>(?:.*)<td>(?:.*)<!-- debut activite -->(.*)<\/td>(?:.*)<td>(?:.*)<!-- fin activite -->(.*)<\/td>(?:.*)<\/tr>/Uis', $matches[1], $matches2)) {
foreach ($matches2[1] as $i=>$adresse) {
$tabEtabs[$i]['siren'] =$siren;
$tabEtabs[$i]['num'] =$i;
$tabEtabs[$i]['adresse'] =trim(strtr(html_entity_decode($adresse),chr(160),' '));
$tabEtabs[$i]['cpVille'] =trim(strtr(html_entity_decode($matches2[2][$i]),chr(160),' '));
$tabEtabs[$i]['enseigne']=trim(strtr(html_entity_decode($matches2[3][$i]),chr(160),' '));
$tabEtabs[$i]['actDeb'] =WDate::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches2[4][$i]),chr(160),' ')));
$tabEtabs[$i]['actFin'] =WDate::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches2[5][$i]),chr(160),' ')));
$tabInsert['nbInscriptions']++;
}
}
}
/** Informations sur la Personne Physique **/
if (preg_match('/<a class="turquoise" href="(rm\.php\?dom=Gerer&amp;fonction=dirigeant&amp;dept_re(.*))" onclick="window\.open\(this\.href/Uis', $this->body, $matches)) {
$urlDir='http://www.cma-paris.fr/CMP/'.strtr(trim($matches[1]),array('&amp;'=>'&'));
//die($urlDir);
randsleep(1,2);
$page=getUrl($urlDir, $this->cookie, '', $this->referer, false, 'www.cma-paris.fr', false, '', '', 21);
$this->body=$page['body'];
$this->codeRetour=$page['code'];
if ($this->codeRetour<>'200') {
$this->libErreur='Erreur Artisanat 200d !';
return false;
}
$this->referer=$url;
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">nom<\/span><\/td>(?:.*)<td align="left" nowrap>(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['nom']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">pr&eacute;nom<\/span><\/td>(?:.*)<td align="left" nowrap>(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['prenom']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">nom d\'usage<\/span><\/td>(?:.*)<td align="left" nowrap>(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['nomUsage']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"(?:.*)<span class="turquoise">&nbsp;date&nbsp;de&nbsp;naissance<\/span><\/td>(?:.*)-->(?:.*)<td(?: +)align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['dateNaiss']=WDate::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
// if (preg_match('/date&nbsp;de&nbsp;naissance(.*)<td background="inter\/pix3_turquoise\.gif" align="right"/Uis', $this->body, $matches))
// die(print_r($matches));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">&nbsp;lieu&nbsp;de&nbsp;naissance<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['lieuNaiss']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">nationalit&eacute;<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['natio']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">qualit&eacute;<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['qualite']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right" nowrap><span class="turquoise">qualification&nbsp;artisanale<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['qualif']=trim(strtr(html_entity_decode($matches[1]),chr(160),' '));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">date&nbsp;d\'obtention<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['datequalif']=WDate::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">prise de fonction<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['dateFctDeb']=WDate::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
if (preg_match('/<td background="inter\/pix3_turquoise\.gif" align="right"><span class="turquoise">fin de fonction<\/span><\/td>(?:.*)<td align="left">(.*)<\/td>/Uis', $this->body, $matches))
$tabInsert['dateFctFin']=WDate::dateT('d/m/Y', 'Y-m-d', trim(strtr(html_entity_decode($matches[1]),chr(160),' ')));
}
// Insertion en base de donn<6E>es
$tabInsert['dateInsert']=date('Y-m-d H:i:s');
$id=$this->iDb->insert('artisanat', $tabInsert, true);
$tabInsert['id']=$id;
foreach ($tabEtabs as $tabInsert2) {
$tabInsert2['dateInsert']=$tabInsert['dateInsert'];
$id2=$this->iDb->insert('artisanat_etab', $tabInsert2, true);
}
$tabInsert['dateUpdate']=substr($tabInsert['dateInsert'],0,10);
unset($tabInsert['dateInsert']);
/*
$tabRet=array( 'id' => $id,// A calculer apr<70>s insert
'Pertinence' => 100,
'Siret' => $siren.'00000',
'Siege' => $siege,
'Nom' => $nom,
'Nom2' => $nomCom,
'Sigle' => '',//prepareString($etab['Sigle']),
'Enseigne' => $ens,
'Adresse' => $adr,
'Adresse2' => $adr2,//prepareString($etab['Adresse2']),
'CP' => $cp,
'Ville' => $ville,
'Tel' => '',//$etab['Tel'],
'Fax' => '',//$etab['Fax'],
'FJ' => $cj,
'FJLib' => $cjLib,
'Siren' => $siren,
'Nic' => '00000',
'Actif' => 0,
'NafEtab' => $naf,
'NafEtabLib' => $nafLib,
'NafEnt' => $naf,
'NafEntLib' => $nafLib,
'NumRC' => $numRC,
'NumRC2' => $numRC2,
'NumGreffe' => $greffe_num,
'DateCreation' => $dateCre,
'DateRadiation' => $dateRad,
'DateCloture' => $dateCloture,
'DateUpdate' => substr($dateInsert,0,10),
);
return $tabRet;
*/
}
return $tabInsert;
}
}
?>