webservice/library/Metier/Rnvp/Adresse.php
Michael RICOIS fdfe3c3be9 Rnvp : DBAL
2016-10-11 16:18:52 +02:00

165 lines
5.2 KiB
PHP
Executable File

<?php
class Metier_Rnvp_Adresse
{
protected $source;
protected $sourceId;
protected $columns = array(
'L1rnvp',
'L2rnvp',
'L3rnvp',
'L4rnvp',
'L5rnvp',
'L6rnvp',
'L7rnvp',
'dateRetourRnvp',
'codeRetour',
'LPAD(Insee,5,0) as Insee',
'CorrectionImportante',
'CorrectionDouteuse',
'CQadrs',
'HexaCle',
'NumDept',
'IdHexavia',
'IdHexaposte',
'Iris_Rivoli',
'Iris_Ilot99',
'Iris_CodeIris',
'Iris_Canton',
'Cdx_Matricule',
'Cdx_V5Geo',
'Cdx_V6Geo',
'Cdx_V5Cdx',
'Cdx_V6Cdx',
'dateInsert',
'dateUpdate',
);
/**
* PDO Connection with Doctrine
* @var \Doctrine\DBAL\Connection
*/
protected $conn;
/**
* Récupération de l'adresse normalisé
* @param \Doctrine\DBAL\Connection $conn
*/
public function __construct($conn = null)
{
if ($conn === null) {
$this->conn = Zend_Registry::get('doctrine');
}
else {
$this->conn = $conn;
}
}
/**
*
* @param unknown $companyId
*/
public function setCompanyId($companyId)
{
$this->source = substr($companyId,0,3);
$this->sourceId = substr($companyId,3,20);
}
/**
* Id de l'entreprise à partir du siret
* @param string $companyId
* @throws Exception
* @return NULL
*/
public function setCompanyIdProxy($companyId)
{
try {
$sql = "SELECT LPAD(source,3,0) AS source AND LPAD(source_id,20,0) AS source_id
FROM jo.etablissements WHERE siren=:siren AND nic=:nic";
$stmt = $this->conn->prepare($sql);
$stmt->bindValue('siren', substr($companyId,0,9));
$stmt->bindValue('nic', substr($companyId,9,5));
$stmt->execute();
if ($stmt->rowCount() == 0) {
return null;
}
else {
$etabResult = $stmt->fetch(\PDO::FETCH_OBJ);
$this->source = $etabResult->source;
$this->sourceId = $etabResult->source_id;
}
} catch (\Doctrine\DBAL\DBALException $e) {
throw new Exception($e->getMessage(), 'ERR');
}
}
/**
* Formattage de l'adresse normalisé
* @return stdClass
* @throws Exception
*/
public function format()
{
try {
$qb = $this->conn->createQueryBuilder();
$qb->select($this->columns)
->from('villes.rnvp_sources')
->where('source = :source')->setParameter('source', $this->source)
->andWhere('source_id = :sourceId')->setParameter('sourceId', $this->sourceId);
$stmt = $qb->execute();
if ($stmt->rowCount() == 0) {
return null;
}
$rnvpResult = $stmt->fetch(\PDO::FETCH_OBJ);
$rnvp = new stdClass();
$rnvp->L1 = $rnvpResult->L1rnvp;
$rnvp->L2 = $rnvpResult->L2rnvp;
$rnvp->L3 = $rnvpResult->L3rnvp;
$rnvp->L4 = $rnvpResult->L4rnvp;
$rnvp->L5 = $rnvpResult->L5rnvp;
$rnvp->L6 = $rnvpResult->L6rnvp;
$rnvp->L7 = $rnvpResult->L7rnvp;
$rnvp->ProcessCode = $rnvpResult->codeRetour;
$process = new Metier_Rnvp_Process();
$correction = '';
if ($rnvpResult->CorrectionImportante == 'I') {
$correction = $rnvpResult->CorrectionImportante;
}
if ($rnvpResult->CorrectionDouteuse == 'D') {
$correction = $rnvpResult->CorrectionDouteuse;
}
$rnvp->ProcessLabel = $process->getLabel($rnvpResult->codeRetour, $correction);
$rnvp->ProcessDate = $rnvpResult->dateRetourRnvp;
// Label CQadrs
$rnvp->QualityCode = $rnvpResult->CQadrs;
$quality = new Metier_Rnvp_Quality();
$rnvp->QualityLabel = $quality->getLabel($rnvp->QualityCode);
$rnvp->GeoInseeCommune = $rnvpResult->Insee;
$rnvp->GeoHexavia = $rnvpResult->IdHexavia;
$rnvp->GeoHexapost = $rnvpResult->IdHexaposte;
$rnvp->GeoHexacle = $rnvpResult->HexaCle;
$rnvp->GeoDepartement = $rnvpResult->NumDept;
$rnvp->GeoRivoliCode = $rnvpResult->Iris_Rivoli;
$rnvp->GeoIlot = $rnvpResult->Iris_Ilot99;
$rnvp->GeoIris = $rnvpResult->Iris_CodeIris;
$rnvp->GeoCanton = $rnvpResult->Iris_Canton;
$rnvp->CedexaMatricule = $rnvpResult->Cdx_Matricule;
$rnvp->CedexaGeoL5 = $rnvpResult->Cdx_V5Geo;
$rnvp->CedexaGeoL6 = $rnvpResult->Cdx_V6Geo;
$rnvp->CedexaCdxL5 = $rnvpResult->Cdx_V5Cdx;
$rnvp->CedexaCdxL6 = $rnvpResult->Cdx_V6Cdx;
$rnvp->DateInsert = $rnvpResult->dateInsert;
$rnvp->DateUpdate = $rnvpResult->dateUpdate;
}
catch (\Doctrine\DBAL\DBALException $e) {
throw new Exception($e->getMessage(), 'ERR');
}
return $rnvp;
}
}