2014-12-18 10:31:05 +00:00
< ? php
2016-01-27 21:10:45 +00:00
class Metier_Partenaires_MRnvp
2015-01-06 22:17:52 +00:00
{
protected $iDb ;
protected $iInsee ;
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
public $tabDevises = array ();
public $nomTronque = 0 ;
2015-07-28 14:39:52 +00:00
protected $tabAdrCQ = array (
10 => 'Adresse correcte' ,
20 => 'Adresse correcte (Voie non reconue dans un CEDEX ou BP)' ,
21 => 'Adresse correcte mais numéro de facade hors borne (petite ville)' ,
22 => 'Adresse correcte mais numéro de facade absent (petite ville)' ,
23 => 'Adresse correcte mais numéro de facade hors borne (grande ville)' ,
24 => 'Adresse correcte mais numéro de facade absent (grande ville)' ,
30 => 'Voie non reconnue (petite ville)' ,
31 => 'Voie non reconnue (petite ville, quartier reconnu)' ,
40 => 'Voie absente (petite ville, quartier reconnu)' ,
41 => 'Voie absente (petite ville)' ,
50 => 'Voie non reconnue (grande ville)' ,
51 => 'Voie non reconnue (grande ville, quartier reconnu)' ,
60 => 'Voie absente (grande ville, quartier reconnu)' ,
61 => 'Voie absente (grande ville)' ,
70 => 'Voie présente mais Cp/Ville non corrigeable' ,
80 => 'Voie absente et Cp/Ville non corrigeable' ,
90 => 'Adresse à l\'étranger' ,
);
function __construct () {
2016-07-18 15:21:48 +02:00
$this -> iDb = new Metier_Util_Db ();
2016-01-27 21:20:37 +00:00
$this -> iInsee = new Metier_Insee_MInsee ( $this -> iDb );
2014-12-18 10:31:05 +00:00
}
2015-01-06 22:17:52 +00:00
/**
* Initialisation du tableau privé des devises Inpi <=> ISO
* @ return multitype : Ambigous < multitype :>
*/
protected function getTabDevisesInpi ()
{
2015-04-27 15:05:08 +00:00
$rep = $this -> iDb -> select ( 'jo.tabDevises' , 'devInpi, devIso' , 'devInpi>0' , false , MYSQL_ASSOC );
2014-12-18 10:31:05 +00:00
$tabDevises = array ();
foreach ( $rep as $k => $dev )
$tabDevises [ $dev [ 'devInpi' ] * 1 ] = $dev [ 'devIso' ];
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
return $tabDevises ;
}
2015-07-28 14:39:52 +00:00
2015-01-06 22:17:52 +00:00
/**
* Récupération du code ISO de la devise numérique de l ' Inpi
* @ param integer $numDeviseInpi
* @ return multitype :| string
*/
public function getDeviseInpi ( $numDeviseInpi )
{
2014-12-18 10:31:05 +00:00
if ( $numDeviseInpi * 1 > 0 && isset ( $this -> tabDevises [ $numDeviseInpi * 1 ]))
return $this -> tabDevises [ $numDeviseInpi * 1 ];
else
return '' ;
}
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
/**
* @ todo Corriger les adresses présentes dans CEDEXA ( toutes les lignes )
* @ todo Ligne 3 , acheter HEXALIGNE3
2015-01-06 22:17:52 +00:00
* @ param unknown $adrL1
* @ param unknown $adrL2
* @ param unknown $adrL3
* @ param unknown $adrL4
* @ param unknown $adrL5
* @ param unknown $adrL6
* @ param string $adrL7
* @ param number $norme
* @ param string $debug
* @ return multitype : string number | string | unknown
2014-12-18 10:31:05 +00:00
*/
2015-01-06 22:17:52 +00:00
public function normaliseAdresse ( $adrL1 , $adrL2 , $adrL3 , $adrL4 , $adrL5 , $adrL6 , $adrL7 = '' , $norme = 38 , $debug = false )
{
2014-12-18 10:31:05 +00:00
$tDeb = microtime ( 1 );
$erreur = false ;
if ( $norme <> 32 && $norme <> 38 ) {
2015-01-06 22:17:52 +00:00
return array (
'dureeRnvp' => round ( microtime ( 1 ) - $tDeb , 3 ),
'errRNVPcode' => 'I8' ,
'errRNVPlib' => 'La norme doit être 32 ou 38 caractères (38 par défaut)'
);
2014-12-18 10:31:05 +00:00
}
$adrL = $tabRetI = $tabRetV = $tabRetR = $tabRetE = array ();
$L1tr = $L2tr = $L3tr = 0 ; // Par défaut, les lignes ne sont pas indiquées comme tronquées
$adrL [ 1 ] = $adrL1 = trim ( preg_replace ( '/\s+/' , ' ' , preg_replace ( '/[^a-z\d ]/i' , ' ' , strtoupper ( trimAccent ( $adrL1 )))));
$adrL [ 2 ] = $adrL2 = trim ( preg_replace ( '/\s+/' , ' ' , preg_replace ( '/[^a-z\d ]/i' , ' ' , strtoupper ( trimAccent ( $adrL2 )))));
$adrL [ 3 ] = $adrL3 = trim ( preg_replace ( '/\s+/' , ' ' , preg_replace ( '/[^a-z\d ]/i' , ' ' , strtoupper ( trimAccent ( $adrL3 )))));
$adrL [ 4 ] = $adrL4 = trim ( preg_replace ( '/\s+/' , ' ' , preg_replace ( '/[^a-z\d ]/i' , ' ' , strtoupper ( trimAccent ( $adrL4 )))));
$adrL [ 5 ] = $adrL5 = trim ( preg_replace ( '/\s+/' , ' ' , preg_replace ( '/[^a-z\d ]/i' , ' ' , strtoupper ( trimAccent ( $adrL5 )))));
$adrL [ 6 ] = $adrL6 = trim ( preg_replace ( '/\s+/' , ' ' , preg_replace ( '/[^a-z\d ]/i' , ' ' , strtoupper ( trimAccent ( $adrL6 )))));
$adrL [ 7 ] = $adrL7 = trim ( preg_replace ( '/\s+/' , ' ' , preg_replace ( '/[^a-z\d ]/i' , ' ' , strtoupper ( trimAccent ( $adrL7 )))));
$dureeM = round ( microtime ( 1 ) - $tDeb , 3 );
if ( $adrL7 <> '' || $adrL7 <> 'FRANCE' || $adrL7 <> 'MONACO' )
$L1 = $adrL1 ;
$L2 = $adrL2 ;
// Ligne 3, acheter HEXALIGNE3
$L3 = $adrL3 ;
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
// Ligne 5 et 7 par défaut
$L7 = $adrL7 ;
$L5 = $adrL5 ;
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
// Ligne 6 : CP + Localité
$idAdr56 = false ;
$tabAdr56k = $tabAdr56L = array ();
$cp = substr ( trim ( $adrL6 ), 0 , 5 );
2015-07-28 14:39:52 +00:00
if ( $cp * 1 > 0 ) {
$cp2 = substr ( $cp , 0 , 2 );
$ville = trim ( strtr ( substr ( $adrL6 , 5 ), array ( ' SAINT ' => ' ST ' , ' SAINTE ' => ' STE ' )));
} else {
$cp = $cp2 = '' ;
$ville = trim ( strtr ( $adrL6 , array ( ' SAINT ' => ' ST ' , ' SAINTE ' => ' STE ' )));
}
2014-12-18 10:31:05 +00:00
$ville = preg_replace ( '/ CEDEX\s?.*$/ui' , '' , $ville );
2015-01-06 22:17:52 +00:00
$tabRetI = array (
2015-07-28 14:39:52 +00:00
'operateurRnvp' => 'SED' ,
2015-01-06 22:17:52 +00:00
'in_cp' => $cp ,
'in_dep' => $cp2 ,
'in_ville' => $ville ,
'in_L1' => $adrL1 ,
'in_L2' => $adrL2 ,
'in_L3' => $adrL3 ,
'in_L4' => $adrL4 ,
'in_L5' => $adrL5 ,
'in_L6' => $adrL6 ,
'in_L7' => $adrL7 );
2014-12-18 10:31:05 +00:00
//$dureeM=round(microtime(1)-$tDeb,3);
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
$tD = microtime ( 1 );
$ret = $this -> iDb -> select ( 'villes.hexaviaVilles' ,
" idAdr56, codeInseeCom, libCom $norme , codeInseeGlobal, indPluridis, libLigne5n $norme , indRoudis, codePostal, libLigne6n $norme , codeInseePre, codeMaj $norme , dateMaj $norme , MATCH (codePostal, libCom38) AGAINST (' $cp $ville ' IN NATURAL LANGUAGE MODE) AS score " ,
" (MATCH (codePostal, libCom38) AGAINST (' $cp $ville ' IN NATURAL LANGUAGE MODE) OR MATCH (codePostal, libCom38) AGAINST (' $cp2 $ville ' IN NATURAL LANGUAGE MODE)) ORDER BY score DESC /*OR codePostal=' $cp ' AND libCom $norme =' $ville '*/ " , false , MYSQL_ASSOC );
$nbRet = count ( $ret );
if ( $nbRet == 0 ) {
2015-01-06 22:17:52 +00:00
$tabRetE = array (
'dureeV' => round ( microtime ( 1 ) - $tD , 3 ),
'errRNVPcode' => 'V0' ,
2015-07-28 14:39:52 +00:00
'errRNVPlib' => " Aucune correspondance CP VILLE (cp= $cp , ville= $ville ) " );
2014-12-18 10:31:05 +00:00
$erreur = true ;
} else {
foreach ( $ret as $i => $iRet ) {
2016-07-12 16:45:26 +02:00
if ( $debug ) echo " je compare ' $cp ' avec ' " . $iRet [ 'codePostal' ] . " ' et ' $ville ' avec ' " . $iRet [ " libCom $norme " ] . " ' ( " . $iRet [ 'idAdr56' ] . " , score= " . $iRet [ 'score' ] . " ) " . PHP_EOL ;
2014-12-18 10:31:05 +00:00
if (( $iRet [ 'codePostal' ] == $cp || substr ( $iRet [ 'codePostal' ], 0 , 2 ) == $cp2 ) && ( $iRet [ " libCom $norme " ] == $ville || preg_replace ( '/ 0/' , ' ' , $iRet [ " libCom $norme " ]) == $ville ) || ( strpos ( $iRet [ " libCom $norme " ], $ville ) > 0 && $nbRet == 1 )) {
$idAdr56 = $iRet [ 'idAdr56' ];
$dateMajHexavia = $iRet [ " dateMaj $norme " ];
$codeMajHexaviaVille = $iRet [ " codeMaj $norme " ];
$hexaViaComCod = $iRet [ 'codeInseeCom' ];
$hexaViaComLib = $iRet [ " libCom $norme " ];
$hexaViaCP = $iRet [ 'codePostal' ];
$L5 = $iRet [ " libLigne5n $norme " ];
$L6 = $iRet [ 'codePostal' ] . ' ' . $iRet [ " libLigne6n $norme " ];
$L7 = '' ;
$tabAdr56k [] = $idAdr56 ;
$tabAdr56L [ '_' . $idAdr56 ] = array ( 'L5' => $L5 , 'L6' => $L6 , 'L7' => $L7 ,);
$tabRetV = array (
'HexaviaDateRef' => $dateMajHexavia ,
'HexaviaCMAJVille' => $codeMajHexaviaVille ,
'HexaviaComCod' => $hexaViaComCod ,
'HexaviaComLib' => $hexaViaComLib ,
'HexaviaCP' => $hexaViaCP ,
'codeInseeGlobal' => $iRet [ 'codeInseeGlobal' ],
'codeInseePre' => $iRet [ 'codeInseePre' ],
'indPluridis' => $iRet [ 'indPluridis' ],
'indRoudis' => $iRet [ 'indRoudis' ],
'libLigne5' => $iRet [ " libLigne5n $norme " ],
'libLigne6' => $iRet [ " libLigne6n $norme " ],
'dureeV' => round ( microtime ( 1 ) - $tD , 3 ),
);
// if ($iRet['score']>17) break;
}
}
if ( ! $idAdr56 ) {
$iRet = $ret [ 0 ];
2016-07-12 16:45:26 +02:00
if ( $debug ) echo " On prend le score le + élevé s'il est > à 15 : je compare ' $cp ' avec ' " . $iRet [ 'codePostal' ] . " ' et ' $ville ' avec ' " . $iRet [ " libCom $norme " ] . " ' ( " . $iRet [ 'idAdr56' ] . " ) " . PHP_EOL ;
//echo '['.trim(preg_replace('/ (1ER|2EME|3EME|\d+)/', ' ',$iRet["libCom$norme"])) .'-v/s-'.trim(preg_replace('/ (1ER|2EME|3EME|\d+)/', ' ',$ville)).']'.PHP_EOL;
2014-12-18 10:31:05 +00:00
if (( $iRet [ 'codePostal' ] == $cp || substr ( $iRet [ 'codePostal' ], 0 , 2 ) == $cp2 ) && ( $iRet [ " libCom $norme " ] == $ville || ( substr ( trim ( preg_replace ( '/ (1ER|2EME|3EME|\d+)/' , ' ' , $iRet [ " libCom $norme " ])), 0 , 26 ) == trim ( preg_replace ( '/ (1ER|2EME|3EME|\d+)/' , ' ' , $ville )) && $iRet [ 'score' ] > 15 ) || ( strpos ( $iRet [ " libCom $norme " ], $ville ) > 0 && $nbRet == 1 ))) {
$idAdr56 = $iRet [ 'idAdr56' ];
$dateMajHexavia = $iRet [ " dateMaj $norme " ];
$hexaViaComCod = $iRet [ 'codeInseeCom' ];
$hexaViaComLib = $iRet [ " libCom $norme " ];
$hexaViaCP = $iRet [ 'codePostal' ];
$L5 = $iRet [ " libLigne5n $norme " ];
$L6 = $iRet [ 'codePostal' ] . ' ' . $iRet [ " libLigne6n $norme " ];
$L7 = '' ;
$tabAdr56k [] = $idAdr56 ;
$tabAdr56L [ '_' . $idAdr56 ] = array ( 'L5' => $L5 , 'L6' => $L6 , 'L7' => $L7 ,);
$tabRetV = array (
'HexaviaDateRef' => $dateMajHexavia ,
'HexaviaCMAJVille' => $codeMajHexaviaVille ,
'HexaviaComCod' => $hexaViaComCod ,
'HexaviaComLib' => $hexaViaComLib ,
'HexaviaCP' => $hexaViaCP ,
'codeInseeGlobal' => $iRet [ 'codeInseeGlobal' ],
'codeInseePre' => $iRet [ 'codeInseePre' ],
'indPluridis' => $iRet [ 'indPluridis' ],
'indRoudis' => $iRet [ 'indRoudis' ],
'libLigne5' => $iRet [ " libLigne5n $norme " ],
'libLigne6' => $iRet [ " libLigne6n $norme " ],
'dureeV' => round ( microtime ( 1 ) - $tD , 3 ),
);
} else {
if ( $debug ) print_r ( $ret );
2015-01-06 22:17:52 +00:00
$erreur = true ;
$tabRetE = array (
'dureeRnvp' => round ( microtime ( 1 ) - $tDeb , 3 ),
'errRNVPcode' => 'V2' ,
'errRNVPlib' => " Plusieurs correspondances CP VILLE pour $cp $ville "
);
2016-07-12 16:45:26 +02:00
//return 'Plusieurs correspondances CP VILLE'.PHP_EOL;
2014-12-18 10:31:05 +00:00
}
}
}
if ( $erreur ) return array_merge ( $tabRetI , $tabRetE );
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
// Ligne 4 : Découpage N° Répétition TypeVoie et LibelléVoie
$tD = microtime ( 1 );
$matriculeHexavia = false ;
$tabAdr = $this -> iInsee -> structureVoie ( $adrL4 );
$numVoie =@ $tabAdr [ 'num' ];
$indRep =@ $tabAdr [ 'indRep' ];
$typeVoie =@ $tabAdr [ 'typeVoie' ];
$libVoie =@ $tabAdr [ 'libVoie' ];
$libVoie5 = trim ( substr ( $libVoie , 0 , 5 ));
$derMot = '' ;
if ( preg_match ( '/([a-z\d]{1,20})$/i' , $libVoie , $matches2 ))
$derMot = trim ( $matches2 [ 1 ]);
$derMot5 = trim ( substr ( $derMot , 0 , 5 ));
$strAdr56 = implode ( " ',' " , $tabAdr56k );
2015-01-06 22:17:52 +00:00
$ret = $this -> iDb -> select (
'villes.hexaviaVoies' ,
" idAdr56, codeVoie, derElemVoie, libVoie $norme , typeVoie, descLibVoie, indStand $norme , indScind, indHomo, codePostal, codeRoudis, codeMaj $norme , dateMaj $norme ,
numImpMin , numImpMinExt , numImpMax , numImpMaxExt , numPairMin , numPairMinExt , numPairMax , numPairMaxExt , MATCH ( codeInseeCom , libVoie38 ) AGAINST ( '$hexaViaComCod $typeVoie $libVoie' IN NATURAL LANGUAGE MODE ) AS score " ,
" idAdr56 IN (' $strAdr56 ') AND (MATCH (codeInseeCom, libVoie38) AGAINST (' $hexaViaComCod $typeVoie $libVoie ' IN NATURAL LANGUAGE MODE) OR derElemVoie LIKE ' $derMot5 %') ORDER BY score DESC LIMIT 0,20
/*AND typeVoie='$typeVoie' AND derElemVoie='$derMot'*/ " ,false, MYSQL_ASSOC
);
2014-12-18 10:31:05 +00:00
$nbRet = count ( $ret );
if ( $nbRet == 0 ) {
// Vérifier si la commune à des voies normées 98816
$ret = $this -> iDb -> select ( 'villes.hexaviaVoies' , " codeVoie " , " idAdr56 IN(' $strAdr56 ') " , false , MYSQL_ASSOC );
$nbVoiesCom = count ( $ret );
if ( $nbVoiesCom == 0 ) {
$erreur = true ;
2016-07-12 16:45:26 +02:00
if ( $debug ) echo " Aucune Voie recensée dans cette commune (' $strAdr56 ') ! " . PHP_EOL ;
2015-01-06 22:17:52 +00:00
$tabRetE = array (
'dureeRnvp' => round ( microtime ( 1 ) - $tDeb , 3 ),
'errRNVPcode' => 'R0' ,
'errRNVPlib' => " Aucune Voie recensée dans cette commune (' $strAdr56 ') "
);
2014-12-18 10:31:05 +00:00
} else {
2015-01-06 22:17:52 +00:00
$tabRetE = array (
'dureeRnvp' => round ( microtime ( 1 ) - $tDeb , 3 ),
'errRNVPcode' => 'R1' ,
'errRNVPlib' => " Aucune correspondance Voie pour ' $adrL4 ', ' $libVoie ' ( $nbVoiesCom voies dans la commune # $idAdr56 ) "
);
2014-12-18 10:31:05 +00:00
}
return array_merge ( $tabRetI , $tabRetV , $tabRetE );
} else {
foreach ( $ret as $i => $iRet ) {
$libVoieRet = preg_replace ( '/^[A-Z]{1,4}\s+/' , '' , $iRet [ " libVoie $norme " ]);
if ( $debug ) echo " je compare ' $typeVoie $libVoie ' avec ' " . $iRet [ " typeVoie " ] . " $libVoieRet ' ainsi que ' $derMot ' avec ' " . $iRet [ 'derElemVoie' ] . " ' " ;
if ( $iRet [ 'typeVoie' ] == $typeVoie && ( $iRet [ " libVoie $norme " ] == $libVoie || $iRet [ 'derElemVoie' ] == $derMot || substr ( $iRet [ 'derElemVoie' ], 0 , 5 ) == $derMot5 )) {
// echo ' OK';
$idAdr56 = $iRet [ 'idAdr56' ];
$L4 = preg_replace ( '/\s+/' , ' ' , trim ( $numVoie . ' ' . $indRep . ' ' . $iRet [ " libVoie $norme " ]));
$L4 = preg_replace ( '/^0+/' , '' , $L4 );
$hexaViaVoie = $iRet [ " libVoie $norme " ];
2016-07-12 16:45:26 +02:00
if ( strlen ( $L4 ) > $norme ) return " Taille de la ligne 4 générée en sortie plus longue que $norme ! " . PHP_EOL ;
2014-12-18 10:31:05 +00:00
$matriculeHexavia = $iRet [ 'codeVoie' ];
$codeRoudis = $iRet [ 'codeRoudis' ];
$L5 = $tabAdr56L [ '_' . $idAdr56 ][ 'L5' ];
$L6 = $tabAdr56L [ '_' . $idAdr56 ][ 'L6' ];
$L7 = $tabAdr56L [ '_' . $idAdr56 ][ 'L7' ];
// Si c'est le meilleur score trouvé on sort
if ( $iRet [ 'score' ] > 17 && isset ( $ret [ $i + 1 ]) && $iRet [ 'score' ] > $ret [ $i + 1 ][ 'score' ]) break ;
}
}
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
$dureeR = round ( microtime ( 1 ) - $tD , 3 );
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
if ( ! $matriculeHexavia ) {
if ( $debug ) print_r ( $ret );
2016-07-12 16:45:26 +02:00
if ( $debug ) echo ( " Plusieurs correspondances Voies pour $adrL4 $adrL6 dans cette commune (' $strAdr56 ') ! " . PHP_EOL );
//die("Plusieurs correspondances Voies pour $adrL4 $adrL6 dans cette commune ('$strAdr56') !".PHP_EOL);
2014-12-18 10:31:05 +00:00
$tabRetE = array ( 'dureeRnvp' => round ( microtime ( 1 ) - $tDeb , 3 ),
'errRNVPcode' => 'R2' ,
'errRNVPlib' => " Plusieurs correspondances Voies pour $adrL4 $adrL6 dans cette commune (' $strAdr56 ') " );
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
return array_merge ( $tabRetI , $tabRetV , $tabRetE );
}
}
if ( ! $matriculeHexavia && @ strlen ( $L4 ) == 0 ) $L4 = $adrL4 ;
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
$tD = microtime ( 1 );
$tabLen = $tabMaxLen = array ();
$tabLen [ 1 ] = strlen ( $L1 );
if ( $tabLen [ 1 ] > $norme ) {
$L1 = $this -> normaliseRS ( $L1 , $norme );
if ( $this -> nomTronque == 1 ) $L1tr = 1 ;
$tabLen [ 1 ] = strlen ( $L1 ); if ( $tabLen [ 1 ] > $norme ) $tabMaxLen [] = 1 ;
}
$tabLen [ 2 ] = strlen ( $L2 );
if ( $tabLen [ 2 ] > $norme ) {
$L2 = $this -> normaliseRS ( $L2 , $norme );
if ( $this -> nomTronque == 1 ) $L2tr = 1 ;
$tabLen [ 2 ] = strlen ( $L2 ); if ( $tabLen [ 2 ] > $norme ) $tabMaxLen [] = 2 ;
}
$tabLen [ 3 ] = strlen ( $L3 );
if ( $tabLen [ 3 ] > $norme ) {
$L3 = $this -> normaliseRS ( $L3 , $norme );
if ( $this -> nomTronque == 1 ) $L3tr = 1 ;
$tabLen [ 3 ] = strlen ( $L3 ); if ( $tabLen [ 3 ] > $norme ) $tabMaxLen [] = 3 ;
}
$tabLen [ 4 ] = strlen ( $L4 ); if ( $tabLen [ 4 ] > $norme ) $tabMaxLen [] = 4 ;
$tabLen [ 5 ] = strlen ( $L5 ); if ( $tabLen [ 5 ] > $norme ) $tabMaxLen [] = 5 ;
$tabLen [ 6 ] = strlen ( $L6 ); if ( $tabLen [ 6 ] > $norme ) $tabMaxLen [] = 6 ;
$tabLen [ 7 ] = strlen ( $L7 ); if ( $tabLen [ 7 ] > $norme ) $tabMaxLen [] = 7 ;
2015-01-06 22:17:52 +00:00
$tabRetE = array (
'dureeRnvp' => round ( microtime ( 1 ) - $tDeb , 3 ),
'errRNVPcode' => '00' ,
'errRNVPlib' => " Normalisation OK "
);
2014-12-18 10:31:05 +00:00
if ( count ( $tabMaxLen ) > 0 ) {
2015-01-06 22:17:52 +00:00
/* foreach ( $tabMaxLen as $j ) {
2016-07-12 16:45:26 +02:00
echo " La ligne n° $j fait " . $tabLen [ $j ] . " caractères : ' " . $adrL [ $j ] . " ' " . PHP_EOL ;
2015-01-06 22:17:52 +00:00
} */
2014-12-18 10:31:05 +00:00
if ( $j > 1 ) {
2015-01-06 22:17:52 +00:00
$tabRetE = array (
'dureeRnvp' => round ( microtime ( 1 ) - $tDeb , 3 ),
'errRNVPcode' => 'O0' ,
'errRNVPlib' => " Une des lignes fait plus de $norme caractères ! "
);
2014-12-18 10:31:05 +00:00
}
}
$dureeN = round ( microtime ( 1 ) - $tD , 3 );
2015-07-28 14:39:52 +00:00
2015-01-06 22:17:52 +00:00
$tabRetR = array (
'L1' => $L1 ,
'L2' => $L2 ,
'L3' => $L3 ,
'L4' => $L4 ,
'L5' => $L5 ,
'L6' => $L6 ,
'L7' => $L7 ,
'L1_tr' => $L1tr ,
'L2_tr' => $L2tr ,
'L3_tr' => $L3tr ,
'HexaVia56' => $idAdr56 ,
'HexaViaVoie' => $hexaViaVoie ,
'HexaViaMat' => $matriculeHexavia ,
'RoudisId' => $codeRoudis ,
'dureeR' => $dureeR ,
'dureeN' => $dureeN ,
'dureeRnvp' => round ( microtime ( 1 ) - $tDeb , 3 ),
'dureeM' => $dureeM * 1.0 ,
);
2015-07-28 14:39:52 +00:00
2015-01-06 22:17:52 +00:00
$tabRet = array_merge ( $tabRetI , $tabRetV , $tabRetR , $tabRetE );
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
return $tabRet ;
}
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
/** Retourne le tableau des abbréviations existantes par type d ' abréviation
*/
2015-01-06 22:17:52 +00:00
function getAbreviations ( $typeAbrev )
{
2014-12-18 10:31:05 +00:00
$tabRet = array ();
if ( $typeAbrev == 'P' ) {
$ret = $this -> iDb -> select ( 'villes.tabPrenoms' ,
" prenom " ,
" LENGTH(prenom)>3 AND nbTot>0 ORDER BY LENGTH(prenom) DESC " , false , MYSQL_ASSOC );
foreach ( $ret as $iRet ) {
$tabTmp = explode ( '-' , $iRet [ 'prenom' ]);
$tabTmp2 = array ();
foreach ( $tabTmp as $subPrenom )
$tabTmp2 [] = substr ( $subPrenom , 0 , 1 );
$tabRet [ $iRet [ 'prenom' ]] = implode ( '-' , $tabTmp2 );
}
} else {
$ret = $this -> iDb -> select ( 'villes.tabAbreviations' ,
" abrCode, abrLib " ,
" abrType=' $typeAbrev ' AND dateSuppr=0 AND idSuppr=0 ORDER BY LENGTH(abrLib) DESC, LENGTH(abrCode) ASC " , false , MYSQL_ASSOC );
foreach ( $ret as $iRet ) {
$tabTmp = explode ( '/' , $iRet [ 'abrLib' ]);
foreach ( $tabTmp as $abrLib ) {
if ( $typeAbrev == 'A' )
$tabRet [ $abrLib ] = '' ;
else
$tabRet [ $abrLib ] = $iRet [ 'abrCode' ];
}
}
}
//print_r($tabRet);die();
return $tabRet ;
}
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
/** Normalise une raison sociale ou un nom
**/
2015-01-06 22:17:52 +00:00
function normaliseRS ( $nomLong , $taille = 38 , $debug = false )
{
2015-07-28 14:39:52 +00:00
$nomCourt = preg_replace ( '/[^A-Z0-9%@&\'\(\)\"\-\*\/\s\+]/' , '' , trim ( strtoupper ( $nomLong )));
2014-12-18 10:31:05 +00:00
$tabMots = split ( " [^[:alpha:]]+ " , $nomCourt );
$passage = 0 ;
$this -> nomTronque = 0 ;
2016-07-12 16:45:26 +02:00
echo $nomCourt . PHP_EOL ;
2014-12-18 10:31:05 +00:00
while ( strlen ( $nomCourt ) > $taille ) {
// 1. Remplacement des Libellés de Voies par leurs code Voie
$tabTmp = $this -> getAbreviations ( 'V' );
foreach ( $tabTmp as $lib => $abr ) {
$nomCourt = trim ( str_replace ( " $lib " . 'ES ' , ' ' . $abr . ' ' , " $nomCourt " ));
$nomCourt = trim ( str_replace ( " $lib " . 'E ' , ' ' . $abr . ' ' , " $nomCourt " ));
$nomCourt = trim ( str_replace ( " $lib " . 'S ' , ' ' . $abr . ' ' , " $nomCourt " ));
$nomCourt = trim ( str_replace ( " $lib " , ' ' . $abr . ' ' , " $nomCourt " ));
if ( strlen ( $nomCourt ) <= $taille ) break ;
}
2016-07-12 16:45:26 +02:00
if ( $debug ) echo " 1-Voies= $nomCourt " . PHP_EOL ;
2014-12-18 10:31:05 +00:00
if ( strlen ( $nomCourt ) <= $taille ) break ;
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
//print_r($tabMots);
// 2. Remplacement des Titres par leurs abréviation
$tabTmp = $this -> getAbreviations ( 'T' );
foreach ( $tabTmp as $lib => $abr ) {
$nomCourt = trim ( str_replace ( " $lib " . 'ES ' , ' ' . $abr . ' ' , " $nomCourt " ));
$nomCourt = trim ( str_replace ( " $lib " . 'E ' , ' ' . $abr . ' ' , " $nomCourt " ));
$nomCourt = trim ( str_replace ( " $lib " . 'S ' , ' ' . $abr . ' ' , " $nomCourt " ));
$nomCourt = trim ( str_replace ( " $lib " , ' ' . $abr . ' ' , " $nomCourt " ));
if ( strlen ( $nomCourt ) <= $taille ) break ;
}
2016-07-12 16:45:26 +02:00
if ( $debug ) echo " 2a-Titres= $nomCourt " . PHP_EOL ;
2014-12-18 10:31:05 +00:00
if ( strlen ( $nomCourt ) <= $taille ) break ;
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
// 2. Remplacement des Formes Juridiques
$tabTmp = $this -> getAbreviations ( 'J' );
foreach ( $tabTmp as $lib => $abr )
$nomCourt = trim ( str_replace ( " $lib " , ' ' . $abr . ' ' , " $nomCourt " ));
2016-07-12 16:45:26 +02:00
if ( $debug ) echo " 2b-FJ= $nomCourt " . PHP_EOL ;
2014-12-18 10:31:05 +00:00
if ( strlen ( $nomCourt ) <= $taille ) break ;
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
// 4. Suppression des articles
$tabTmp = $this -> getAbreviations ( 'A' );
foreach ( $tabTmp as $lib => $abr ) {
if ( strpos ( $lib , " ' " ) > 0 )
$nomCourt = trim ( str_replace ( ' ' . $lib , ' ' , " $nomCourt " ));
else
$nomCourt = trim ( str_replace ( " $lib " , ' ' , " $nomCourt " ));
if ( strlen ( $nomCourt ) <= $taille ) break ;
}
2016-07-12 16:45:26 +02:00
if ( $debug ) echo " 4-Articles= $nomCourt " . PHP_EOL ;
2014-12-18 10:31:05 +00:00
if ( strlen ( $nomCourt ) <= $taille ) break ;
// 3. Remplacement des Prénoms par leurs initiales
$tabTmp = $this -> getAbreviations ( 'P' );
foreach ( $tabTmp as $lib => $abr ) {
$nomCourt = trim ( str_replace ( " $lib " , ' ' . $abr . ' ' , " $nomCourt " ));
if ( strlen ( $nomCourt ) <= $taille ) break ;
}
2016-07-12 16:45:26 +02:00
if ( $debug ) echo " 3-Prenoms= $nomCourt " . PHP_EOL ;
2014-12-18 10:31:05 +00:00
if ( strlen ( $nomCourt ) <= $taille ) break ;
$tabTmp = $this -> getAbreviations ( 'N' );
foreach ( $tabTmp as $lib => $abr ) {
$nomCourt = trim ( str_replace ( " $lib " . 'ES ' , ' ' . $abr . ' ' , " $nomCourt " ));
$nomCourt = trim ( str_replace ( " $lib " . 'E ' , ' ' . $abr . ' ' , " $nomCourt " ));
$nomCourt = trim ( str_replace ( " $lib " . 'S ' , ' ' . $abr . ' ' , " $nomCourt " ));
$nomCourt = trim ( str_replace ( " $lib " , ' ' . $abr . ' ' , " $nomCourt " ));
if ( strlen ( $nomCourt ) <= $taille ) break ;
}
2016-07-12 16:45:26 +02:00
if ( $debug ) echo " 5-Autres Noms= $nomCourt " . PHP_EOL ;
2014-12-18 10:31:05 +00:00
if ( strlen ( $nomCourt ) <= $taille ) break ;
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
$nomCourt = substr ( $nomCourt , 0 , $taille );
$this -> nomTronque = 1 ;
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
//die($nomCourt);
2015-01-06 22:17:52 +00:00
/** @ todo A finir
Tronquer ou abréger dans cette ordre
- type de voie
- titres
- initiale du prénom
- supprimez les articles ( mais pas les particules dans un nom propre )
- tronquer à 4 caractères les types de voie non normalisés
- tronquer les extensions de voie
- réduire le nom de la voie en supprimant les mots de la gauche vers la droite
2015-07-28 14:39:52 +00:00
2015-01-06 22:17:52 +00:00
Gérer les pluriels pour les voies , nom , titres , et formes juridiques
**/
2014-12-18 10:31:05 +00:00
$passage ++ ;
}
return $nomCourt ;
}
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
// Code Insee de la commune libCom32 Libellé de la commune (Ancienne norme 32) libCom38 Libellé
function getLibCommune ( $codeInsee , $norme = 38 ) {
if ( $norme <> 32 && $norme <> 38 ) {
2016-07-12 16:45:26 +02:00
return 'La norme doit être 32 ou 38 caractères (38 par défaut)' . PHP_EOL ;
2014-12-18 10:31:05 +00:00
}
2015-07-28 14:39:52 +00:00
2014-12-18 10:31:05 +00:00
$ret = $this -> iDb -> select ( 'villes.hexaviaVilles' ,
" libCom32 , libCom38 " ,
" codeInseeCom=' $codeInsee ' LIMIT 0,1 " , false , MYSQL_ASSOC );
$nbRet = count ( $ret );
2015-01-13 20:52:54 +00:00
if ( $nbRet == 0 && $codeInsee > 99000 ) {
$codePaysInsee = substr ( $codeInsee , 2 , 3 );
$ret = $this -> iDb -> select ( 'jo.tabPays' ,
" SUBSTRING(libPays,1,32) AS libCom32, SUBSTRING(libPays,1,38) AS libCom38 " ,
" codePaysInsee=' $codeInsee ' ORDER BY dependance ASC LIMIT 0,1 " , false , MYSQL_ASSOC );
$nbRet = count ( $ret );
}
if ( $nbRet == 0 ) return 'Aucune correspondance VILLE' ;
elseif ( $norme == 32 ) return $ret [ 0 ][ 'libCom32' ];
else return $ret [ 0 ][ 'libCom38' ];
2014-12-18 10:31:05 +00:00
}
2015-07-28 14:39:52 +00:00
function getCPCommune ( $codeInsee )
{
$ret = $this -> iDb -> select ( 'villes.hexaviaVilles' ,
" codePostal " ,
" codeInseeCom=' $codeInsee ' GROUP BY codePostal " , false , MYSQL_ASSOC );
$nbRet = count ( $ret );
if ( $nbRet == 1 ) return $ret [ 0 ][ 'codePostal' ];
return false ;
}
2015-01-06 22:17:52 +00:00
function getCodCommune ( $libelleCommune , $depOuCp = '' , $debug = false )
{
2014-12-18 10:31:05 +00:00
$norme = 38 ;
$codeCommune = false ;
$cp = $depOuCp ;
$cp2 = substr ( $cp , 0 , 2 );
$ret = $this -> iDb -> select ( 'villes.hexaviaVilles' ,
" idAdr56, codeInseeCom, libCom $norme , codeInseeGlobal, indPluridis, libLigne5n $norme , indRoudis, codePostal, libLigne6n $norme , codeInseePre, codeMaj $norme , dateMaj $norme , MATCH (codePostal, libCom38) AGAINST (' $depOuCp $libelleCommune ' IN NATURAL LANGUAGE MODE) AS score " ,
" MATCH (codePostal, libCom38) AGAINST (' $depOuCp $libelleCommune ' IN NATURAL LANGUAGE MODE) ORDER BY score DESC " , false , MYSQL_ASSOC );
// print_r($ret);
$nbRet = count ( $ret );
if ( $nbRet == 0 )
return false ;
else {
foreach ( $ret as $i => $iRet ) {
2016-07-12 16:45:26 +02:00
if ( $debug ) echo " je compare ' $depOuCp ' avec ' " . $iRet [ 'codePostal' ] . " ' et ' $libelleCommune ' avec ' " . $iRet [ " libCom $norme " ] . " ' ( " . $iRet [ 'idAdr56' ] . " , score= " . $iRet [ 'score' ] . " ) " . PHP_EOL ;
2014-12-18 10:31:05 +00:00
if (( $depOuCp <> '' && ( $iRet [ 'codePostal' ] == $cp || substr ( $iRet [ 'codePostal' ], 0 , 2 ) == $cp2 )) && ( $iRet [ " libCom $norme " ] == $libelleCommune || preg_replace ( '/ 0/' , ' ' , $iRet [ " libCom $norme " ]) == $libelleCommune ) || ( strpos ( $iRet [ " libCom $norme " ], $libelleCommune ) > 0 && $nbRet == 1 )) {
$codeCommune = $iRet [ 'codeInseeCom' ];
if ( $iRet [ 'score' ] > 17 ) break ;
}
}
if ( ! $codeCommune ) {
$iRet = $ret [ 0 ];
2016-07-12 16:45:26 +02:00
if ( $debug ) echo " On prend le score le + élevé s'il est > à 15 : je compare ' $cp ' avec ' " . $iRet [ 'codePostal' ] . " ' et ' $libelleCommune ' avec ' " . $iRet [ " libCom $norme " ] . " ' ( " . $iRet [ 'idAdr56' ] . " ) " . PHP_EOL ;
if ( $debug ) echo '[' . trim ( preg_replace ( '/ (1ER|2EME|3EME|\d+)/' , ' ' , $iRet [ " libCom $norme " ])) . '-v/s-' . trim ( preg_replace ( '/ (1ER|2EME|3EME|\d+)/' , ' ' , $libelleCommune )) . ']' . PHP_EOL ;
2014-12-18 10:31:05 +00:00
if (( $depOuCp <> '' && ( $iRet [ 'codePostal' ] == $cp || substr ( $iRet [ 'codePostal' ], 0 , 2 ) == $cp2 )) && ( $iRet [ " libCom $norme " ] == $libelleCommune || ( substr ( trim ( preg_replace ( '/ (1ER|2EME|3EME|\d+)/' , ' ' , $iRet [ " libCom $norme " ])), 0 , 26 ) == trim ( preg_replace ( '/ (1ER|2EME|3EME|\d+)/' , ' ' , $libelleCommune )) && $iRet [ 'score' ] > 15 ) || ( strpos ( $iRet [ " libCom $norme " ], $libelleCommune ) > 0 && $nbRet == 1 ))) {
$codeCommune = $iRet [ 'codeInseeCom' ];
}
}
2016-07-12 16:45:26 +02:00
//die("Code commune de $libelleCommune ($depOuCp) = $codeCommune".PHP_EOL);
2014-12-18 10:31:05 +00:00
}
return $codeCommune ;
}
2015-07-28 14:39:52 +00:00
function normaliseAdresse76310 ( $L1 , $L2 , $L3 , $L4 , $L5 , $L6 , $L7 = '' )
{
$tDeb = microtime ( 1 );
$tabRetR = $tabRetE = array ();
$cp = substr ( trim ( $L6 ), 0 , 5 );
$cp2 = substr ( $cp , 0 , 2 );
$ville = trim ( strtr ( substr ( $L6 , 5 ), array ( ' SAINT ' => ' ST ' , ' SAINTE ' => ' STE ' )));
$ville = preg_replace ( '/ CEDEX\s?.*$/ui' , '' , $ville );
$tabRetI = array ( 'operateurRnvp' => '76310WEB' ,
'in_cp' => $cp ,
'in_dep' => $cp2 ,
'in_ville' => $ville ,
'in_L1' => trim ( $L1 ),
'in_L2' => trim ( $L2 ),
'in_L3' => trim ( $L3 ),
'in_L4' => trim ( $L4 ),
'in_L5' => trim ( $L5 ),
'in_L6' => trim ( $L6 ),
'in_L7' => trim ( $L7 ));
//$client = new SoapClient('http://www.rnvp-en-ligne.com/service.asmx?wsdl');
$client = new SoapClient ( 'http://www.rnvp-en-ligne.com/service_v5.asmx?wsdl' );
$nbEssais = 1 ;
2015-01-13 20:52:54 +00:00
$array = array (
'pi_session' => '-1' ,
'pi_user' => 'SDPROD' ,
'pi_password' => '7631014530' ,
'pi_codedossier' => '0001' ,
'pi_numfichier' => '1' ,
'pi_rsoc' => utf8_encode ( $L1 ),
//'pio_civ' => '',
//'pio_nom' => '',
//'pio_prenom' => '',
'pio_cnom' => utf8_encode ( $L2 ), // Ligne 2
'pio_cadrs' => utf8_encode ( $L3 ), // Ligne 3
'pio_adresse' => utf8_encode ( $L4 ), // Ligne 4
'pio_lieudit' => utf8_encode ( $L5 ), // Ligne 5
'pio_cpville' => utf8_encode ( $L6 ), // Ligne 6
'pio_pays' => utf8_encode ( $L7 ), // Ligne 7
/* 'po_tnp' => '' ,
2015-07-28 14:39:52 +00:00
'po_sex' => '' ,
'po_civlong' => '' ,
'po_cp' => '' ,
'po_ville' => '' ,
'po_insee' => '' ,
'po_cqtnp' => '' ,
'po_cqadrs' => '' ,
'po_risquerestru' => '' ,
'po_poidsmodif' => '' ,
'po_rejet' => '' ,
'po_etranger' => '' */
);
while ( 1 ) {
try {
//$result = $client->Elfyweb_RNVP_Standard($array);
$result = $client -> Elfyweb_RNVP_Expert_V50 ( $array );
//print_r($result);
$tabRetR = array ( 'L1' => $L1 ,
'L2' => $L2 ,
'L3' => strtoupper ( utf8_decode ( $result -> pio_cadrs )),
'L4' => strtoupper ( utf8_decode ( $result -> pio_adresse )),
'L5' => strtoupper ( utf8_decode ( $result -> pio_lieudit )),
'L6' => strtoupper ( utf8_decode ( $result -> pio_cpville )),
/* [ po_risquerestru ] => 0
[ po_poidsmodif ] => 0
[ po_rejet ] =>
[ po_etranger ] =>*/
'Cp' => $result -> po_cp ,
'Ville' => $result -> po_ville ,
'Insee' => $result -> po_insee ,
/* 'CQadrs' => $result -> po_cqadrs ,
'CQadrsLib' => $this -> tabAdrCQ [ $result -> po_cqadrs ],
'CQAdrRnvp' => $this -> getLibQualiteAdresse76310 ( $result -> po_cqadrs , $result -> rejet ), */
'dureeRnvp' => round ( microtime ( 1 ) - $tDeb , 3 ),
);
if ( @ $result -> pio_pays <> 'FRA' ) $tabRet [ 'L7' ] = $result -> pio_pays ;
break ;
} catch ( SoapFault $fault ) {
$nbEssais ++ ;
if ( $nbEssai < 5 ) continue ;
$tabRetE = array ( 'dureeRnvp' => round ( microtime ( 1 ) - $tDeb , 3 ),
'errRNVPcode' => 'S0' ,
'errRNVPlib' => " Erreur SOAP : " . print_r ( $fault , 1 ));
}
}
$tabRet = array_merge ( $tabRetI , $tabRetR , $tabRetE );
return $tabRet ;
}
function getLibQualiteAdresse76310 ( $cqadrs , $correctionDouteuse )
{
switch ( $cqadrs * 1 ) {
case 10 : // Adresse correcte
case 20 : // Adresse correcte (Voie non reconue dans un CEDEX ou BP)
case 21 : // Adresse correcte mais numéro de facade hors borne (petite ville)
case 22 : // Adresse correcte mais numéro de facade absent (petite ville)
case 23 : // Adresse correcte mais numéro de facade hors borne (grande ville)
case 24 : // Adresse correcte mais numéro de facade absent (grande ville)
$cqRnvpSed = 1 ;
break ;
case 31 : // Voie non reconnue (petite ville, quartier reconnu)
case 51 : // Voie non reconnue (grande ville, quartier reconnu)
$cqRnvpSed = 2 ;
break ;
case 30 : // Voie non reconnue (petite ville)
case 50 : // Voie non reconnue (grande ville)
$cqRnvpSed = 3 ;
break ;
case 40 : // Voie absente (petite ville, quartier reconnu)
case 41 : // Voie absente (petite ville)
case 60 : // Voie absente (grande ville, quartier reconnu)
case 61 : // Voie absente (grande ville)
$cqRnvpSed = 4 ;
break ;
case 70 : // Voie présente mais Cp/Ville non corrigeable
case 80 : // Voie absente et Cp/Ville non corrigeable
$cqRnvpSed = 5 ;
break ;
default :
$cqRnvpSed = 0 ;
break ;
}
if ( $correctionDouteuse == 'D' ) $cqRnvpSed = 0 ;
return $cqRnvpSed ;
}
function getAdresseRnvpSource ( $source , $source_id , $num = 0 )
{
$ret = $this -> iDb -> select (
'villes.rnvpSources' ,
' id , source , source_id , num , L1rnvp , L2rnvp , L3rnvp , L4rnvp , L5rnvp , L6rnvp , L7rnvp , Pays , dateInsert ,
operateurRnvp , dateEnvoiRnvp , dateRetourRnvp , codeRetour , NumVoie , BisTer , TypeVoieCourt , TypeVoieLong , LibVoie ,
Cp , Ville , Insee , CQadrs , CorrectionImportante , CorrectionDouteuse , HexaCle , CQL3 , InseeGlobal , OldInsee ,
IsInseeReconstitue , NumDept , IdHexavia , IdHexaposte , Iris_Rivoli , Iris_Ilot99 , Iris_CodeIris , Iris_Canton ,
Iris_Zus , Iris_Zfu , CqIris , dateUpdate ' ,
" source= $source AND source_id= $source_id AND num= $num LIMIT 0,1 " , false , MYSQL_ASSOC );
$tabRet = $ret [ 0 ];
$tabRet [ 'CQadrsLib' ] = $this -> tabAdrCQ [ $tabRet [ 'CQadrs' ]];
$tabRet [ 'CQAdrRnvp' ] = $this -> getLibQualiteAdresse76310 ( $tabRet [ 'CQadrs' ], $tabRet [ 'CorrectionDouteuse' ]);
return $tabRet ;
2015-01-13 20:52:54 +00:00
}
2014-12-18 10:31:05 +00:00
}
?>