2016-11-24 19:05:42 +01:00

333 lines
15 KiB
PHP

<?php
class Metier_Partenaires_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;
protected $remote = false;
public function __construct($accesDist = true)
{
$this->accesDist = $accesDist;
$this->iDb = new Metier_Util_Db();
$this->iBodacc = new Metier_Bodacc_MBodacc();
$this->iInsee = new Metier_Insee_MInsee();
}
/**
* Active la récupération des données distantes
*/
public function setRemote()
{
$this->remote = true;
}
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/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")) {
$tabTmp = $this->iInsee->getIdentiteLight($siren);
if ($tabTmp['FJ']>=1000 && $tabTmp['FJ']<2000) {
return 1050;
}
}
return false;
}
}
public function getMaxQualibat()
{
$ret = $this->iDb->select('sdv1.qualibat', 'MAX(id) AS id', '1', false, MYSQL_ASSOC);
return $ret[0]['id'];
}
public function getTabQualibatManquants()
{
$tabRet = $tabQualibatCalc = $tabQualibatBase = array();
// Liste des Qualibat Calculés
$maxQualibat = $this->getMaxQualibat();
for ($i=1; $i<=$maxQualibat; $i++) {
$tabQualibatCalc[]=$i;
}
// Liste des Qualibat en base
$ret = $this->iDb->select('sdv1.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(
'sdv1.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);
// In database
if (!$this->force && count($ret) > 0) {
$this->enCache=true;
$tabRet = $ret[0];//array();
// Ajout des qualifications
$ret = $this->iDb->select(
'sdv1.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(
'sdv1.qualibatdir', 'civNomPrenom, civilite, nom, prenom, fonction',
$strWhere, false, MYSQL_ASSOC);
foreach ($ret as $i => $tabTmp) {
$tabRet['dirigeants'][] = $tabTmp;
}
}
// WebSite
elseif ($this->accesDist == true) {
$this->enCache = false;
$url = 'http://www.qualibat.com/Views/EntreprisesRechercheDetail.aspx?id='.$idQualibat;
$page = getUrl($url, '', '', '', false, '', '', 3);
if ($page['code'] != 200) {
return false;
}
$body = $page['body'];
$tabRet['id'] = $idQualibat;
if (preg_match('/<th>(?:.*)Raison sociale(?:.*)<\/th>(?:.*)<td>(.*)<\/td>/Uis', $body, $matches)) {
$tabRet['nom'] = trim(strip_tags($matches[1]));
if (preg_match('/<th>(?:.*)Adresse(?:.*)<\/th>(?:.*)<td>(.*)<br \/>(.*)<\/td>/Uis', $body, $matches)) {
$tabRet['adresse'] =strtoupper(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'] = trim(substr($adresse2, 5));
}
if (preg_match('/<th>(?:.*)Téléphone(?:.*)<\/th>(?:.*)<td>(.*)<\/td>/Uisu', $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'] = 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'] = 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'] = Metier_Util_Date::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'] = trim($matches[1]);
}
/** Qualifications ? **/
$qualifs = '';
if (preg_match('/<table class="recherche_entreprise_detail">(?:.*)<th colspan="4">(?:.*)(Qualifications professionnelles|Certifications m..tier)(?:.*)<tbody>(.*)<\/table>/Uisu', $body, $matches)) {
$typeQualifs = trim($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 = trim($matches[2][$i]);
if (preg_match("/\(Tech?nicit. (.*)\)/Uiu", $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' => 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;
}
}
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]);
}
if (preg_match_all('/<tr>(?:.*)<td>(.*)<\/td>(?:.*)<td>(.*)<\/td>(?:.*)<\/tr>/Uis', $dirs, $matches)) {
foreach ($matches[1] as $i => $nom) {
$strNom = 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(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),
);
}
}
/** 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('sdv1.qualibat', $tabInsert)) {
$this->iDb->update('sdv1.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('sdv1.qualibatqualif', $tabInsert)) {
$this->iDb->update('sdv1.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('sdv1.qualibatdir', $tabInsert)) {
$this->iDb->update('sdv1.qualibatdir', $tabInsert, "id=$idQualibat AND civNomPrenom='".addslashes($tabInsert['civNomPrenom'])."' AND fonction='".addslashes($tabInsert['fonction'])."'");
}
unset($tabRet['dirigeants'][$i]['civNomPrenom']);
}
}
return $tabRet;
} else {
$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()) {
$this->iDb->insert('sdv1.qualibat', $tabInsert);
}
return false;
}
}
return false;
}
}