Nettoyage + Doctrine
This commit is contained in:
parent
553daa922a
commit
81a4903082
@ -1,27 +1,40 @@
|
||||
<?php
|
||||
require_once 'framework/common/curl.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;
|
||||
protected $iBodacc;
|
||||
protected $iInsee;
|
||||
|
||||
public $enCache=false;
|
||||
public $force=false;
|
||||
public $annee=0;
|
||||
/**
|
||||
* PDO Connection with Doctrine
|
||||
* @var \Doctrine\DBAL\Connection
|
||||
*/
|
||||
protected $conn;
|
||||
|
||||
/**
|
||||
* Logger
|
||||
* @var \Monolog\Logger
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* Enable remote
|
||||
* @var string
|
||||
*/
|
||||
protected $remote = false;
|
||||
|
||||
public function __construct($accesDist = true)
|
||||
/**
|
||||
* MQualibat
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->accesDist = $accesDist;
|
||||
$this->iDb = new Metier_Util_Db();
|
||||
$this->conn = Zend_Registry::get('doctrine');
|
||||
|
||||
if (Zend_Registry::isRegistered('logger')) {
|
||||
$this->logger = Zend_Registry::get('logger');
|
||||
}
|
||||
|
||||
$this->iBodacc = new Metier_Bodacc_MBodacc();
|
||||
$this->iInsee = new Metier_Insee_MInsee();
|
||||
}
|
||||
@ -77,23 +90,40 @@ class Metier_Partenaires_MQualibat
|
||||
|
||||
public function getMaxQualibat()
|
||||
{
|
||||
$ret = $this->iDb->select('sdv1.qualibat', 'MAX(id) AS id', '1', false, MYSQL_ASSOC);
|
||||
return $ret[0]['id'];
|
||||
$sql = "SELECT MAX(id) AS id FROM sdv1.qualibat";
|
||||
try {
|
||||
$stmt = $this->conn->executeQuery($sql);
|
||||
} catch (\Doctrine\DBAL\DBALException $e) {
|
||||
if ($this->logger !== null) {
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
$result = $stmt->fetch(\PDO::FETCH_OBJ);
|
||||
|
||||
return $result->id;
|
||||
}
|
||||
|
||||
public function getTabQualibatManquants()
|
||||
{
|
||||
$tabRet = $tabQualibatCalc = $tabQualibatBase = array();
|
||||
|
||||
// Liste des Qualibat Calculés
|
||||
$maxQualibat = $this->getMaxQualibat();
|
||||
for ($i=1; $i<=$maxQualibat; $i++) {
|
||||
$tabQualibatCalc[]=$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'];
|
||||
$sql = "SELECT id FROM sdv1.qualibat ORDER BY id ASC";
|
||||
try {
|
||||
$stmt = $this->conn->executeQuery($sql);
|
||||
while ($result = $stmt->fetch(PDO::FETCH_OBJ)) {
|
||||
$tabQualibatBase[] = $result->id;
|
||||
}
|
||||
} catch (\Doctrine\DBAL\DBALException $e) {
|
||||
if ($this->logger !== null) {
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return array_diff($tabQualibatCalc, $tabQualibatBase);
|
||||
@ -109,34 +139,56 @@ class Metier_Partenaires_MQualibat
|
||||
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);
|
||||
$sql = "SELECT siren, actif, id, nom, adresse, cp, ville, tel, fax, email, web, eff, teff,
|
||||
ca, tca, libFJ, nace, dateFondation, dateDeb, dateFin, dateInsert
|
||||
FROM sdv1.qualibat WHERE $strWhere";
|
||||
try {
|
||||
$stmt = $this->conn->executeQuery($sql);
|
||||
} catch (\Doctrine\DBAL\DBALException $e) {
|
||||
if ($this->logger !== null) {
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// In database
|
||||
if (!$this->force && count($ret) > 0) {
|
||||
$this->enCache=true;
|
||||
$tabRet = $ret[0];//array();
|
||||
if ($this->remote === false && $stmt->rowCount() > 0) {
|
||||
$tabRet = $stmt->fetch(\PDO::FETCH_ASSOC);
|
||||
|
||||
// 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;
|
||||
$sql = "SELECT code, periodQualif, niveauQualif, mentions, nomQualif, dateAttrib, dateEch
|
||||
FROM sdv1.qualibatqualif WHERE $strWhere";
|
||||
try {
|
||||
$stmt = $this->conn->executeQuery($sql);
|
||||
if ($stmt->rowCount() > 0) {
|
||||
while ($result = $stmt->fetch(\PDO::FETCH_ASSOC)) {
|
||||
$tabRet['qualifications'][] = $result;
|
||||
}
|
||||
}
|
||||
} catch (\Doctrine\DBAL\DBALException $e) {
|
||||
if ($this->logger !== null) {
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
$sql = "SELECT civNomPrenom, civilite, nom, prenom, fonction
|
||||
FROM sdv1.qualibatdir WHERE $strWhere";
|
||||
try {
|
||||
$stmt = $this->conn->executeQuery($sql);
|
||||
if ($stmt->rowCount() > 0) {
|
||||
while ($result = $stmt->fetch(\PDO::FETCH_ASSOC)) {
|
||||
$tabRet['dirigeants'][] = $result;
|
||||
}
|
||||
}
|
||||
} catch (\Doctrine\DBAL\DBALException $e) {
|
||||
if ($this->logger !== null) {
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WebSite
|
||||
elseif ($this->accesDist == true) {
|
||||
$this->enCache = false;
|
||||
if ($this->remote === true) {
|
||||
$url = 'http://www.qualibat.com/Views/EntreprisesRechercheDetail.aspx?id='.$idQualibat;
|
||||
$page = getUrl($url, '', '', '', false, '', '', 3);
|
||||
if ($page['code'] != 200) {
|
||||
@ -258,10 +310,10 @@ class Metier_Partenaires_MQualibat
|
||||
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;
|
||||
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(
|
||||
@ -282,8 +334,19 @@ class Metier_Partenaires_MQualibat
|
||||
'dateDeb' => @$tabDateAttrib[0],
|
||||
'dateFin' => @$tabDateFin[0]));
|
||||
unset($tabInsert['qualifications']);
|
||||
if (!$this->iDb->insert('sdv1.qualibat', $tabInsert)) {
|
||||
$this->iDb->update('sdv1.qualibat', $tabInsert, "id=$idQualibat");
|
||||
|
||||
$sql = "SELECT id FROM sdv1.qualibat WHERE id=$idQualibat";
|
||||
try {
|
||||
$stmt = $this->conn->executeQuery($sql);
|
||||
if ($stmt->rowCount() > 0) {
|
||||
$this->conn->update('sdv1.qualibat', $tabInsert, array('id' => $idQualibat));
|
||||
} else {
|
||||
$this->conn->insert('sdv1.qualibat', $tabInsert);
|
||||
}
|
||||
} catch (\Doctrine\DBAL\DBALException $e) {
|
||||
if ($this->logger !== null) {
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/** Insertion des qualifications **/
|
||||
@ -292,8 +355,24 @@ class Metier_Partenaires_MQualibat
|
||||
$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']."'");
|
||||
|
||||
$sql = "SELECT id FROM sdv1.qualibatqualif
|
||||
WHERE id=$idQualibat AND code='".$tabInsert['code']."' AND periodQualif='".$tabInsert['periodQualif']."'";
|
||||
try {
|
||||
$stmt = $this->conn->executeQuery($sql);
|
||||
if ($stmt->rowCount() > 0) {
|
||||
$this->conn->update('sdv1.qualibatqualif', $tabInsert, array(
|
||||
'id' => $idQualibat,
|
||||
'code' => $tabInsert['code'],
|
||||
'periodQualif' => $tabInsert['periodQualif'],
|
||||
));
|
||||
} else {
|
||||
$this->conn->insert('sdv1.qualibatqualif', $tabInsert);
|
||||
}
|
||||
} catch (\Doctrine\DBAL\DBALException $e) {
|
||||
if ($this->logger !== null) {
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -304,29 +383,66 @@ class Metier_Partenaires_MQualibat
|
||||
$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'])."'");
|
||||
|
||||
$sql = "SELECT id FROM sdv1.qualibatdir
|
||||
WHERE id=$idQualibat AND civNomPrenom='".addslashes($tabInsert['civNomPrenom'])."' AND fonction='".addslashes($tabInsert['fonction'])."'";
|
||||
try {
|
||||
$stmt = $this->conn->executeQuery($sql);
|
||||
if ($stmt->rowCount() > 0) {
|
||||
$this->conn->update('sdv1.qualibatdir', $tabInsert, array(
|
||||
'id' => $idQualibat,
|
||||
'civNomPrenom' => $tabInsert['civNomPrenom'],
|
||||
'fonction' => $tabInsert['fonction'],
|
||||
));
|
||||
} else {
|
||||
$this->conn->insert('sdv1.qualibatdir', $tabInsert);
|
||||
}
|
||||
} catch (\Doctrine\DBAL\DBALException $e) {
|
||||
if ($this->logger !== null) {
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
unset($tabRet['dirigeants'][$i]['civNomPrenom']);
|
||||
}
|
||||
}
|
||||
|
||||
return $tabRet;
|
||||
} else {
|
||||
$tabInsert = array(
|
||||
'actif' => 0,
|
||||
'id' => $idQualibat,
|
||||
'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);
|
||||
try {
|
||||
$stmt = $this->conn->insert('sdv1.qualibat', $tabInsert);
|
||||
} catch (\Doctrine\DBAL\DBALException $e) {
|
||||
if ($this->logger !== null) {
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Remote Infos
|
||||
* @param unknown $id
|
||||
*/
|
||||
protected function getRemote($id)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user