2012-10-16 07:44:31 +00:00
#!/usr/bin/php -c/var/www/batch/config/php_batch_sd.ini
< ? php
include_once ( FWK_PATH . 'common/chiffres.php' );
include_once ( FWK_PATH . 'common/dates.php' );
include_once ( FWK_PATH . 'common/ftp.php' );
include_once ( INCLUDE_PATH . 'insee/classMInsee.php' );
include_once ( INCLUDE_PATH . 'partenaires/classMMarques.php' );
include_once ( FWK_PATH . 'mail/sendMail.php' );
$strInfoScript = 'Usage : ' . basename ( $argv [ 0 ]) . " [OPTIONS] marques1 marques2 ...
2013-06-19 08:24:49 +00:00
T<EFBFBD> l<EFBFBD> chargement et mise <EFBFBD> jour de Marques
2012-10-16 07:44:31 +00:00
Options :
2013-06-19 08:24:49 +00:00
- v Mode verbose ou debug
- f Forcer la mise <EFBFBD> jour depuis Qualibat
- m Retourne le dernier num<EFBFBD> ro Qualibat en base
- i Les param<EFBFBD> tres sont des num<EFBFBD> ros de marques
- r Les param<EFBFBD> tres sont des Raisons Sociales
- d = SSAAMMJJ R<EFBFBD> cup<EFBFBD> res les d<EFBFBD> pots du SSAAMMJJ
- p R<EFBFBD> cup<EFBFBD> re les PDF non encore en base
- b Mise <EFBFBD> jour des marques issues des PDF
- s Mise <EFBFBD> jour intelligente des siren
- a D<EFBFBD> coupage des adresses postales non encore d<EFBFBD> coup<EFBFBD> es
- t Decoupage des fichiers images
( * ) : Valeur par d<EFBFBD> faut !
2012-10-16 07:44:31 +00:00
" ;
$iMarque = new MMarques ();
2013-06-19 08:24:49 +00:00
$iInsee = new MInsee ();
2012-10-16 07:44:31 +00:00
2013-06-19 08:24:49 +00:00
$modeDebug = $force = $max = $annee = $majNum = $majNom = $majObj = $getPdf = $getMBopi = $majSiren = $majAdresses = $majDate = $majImages = false ;
$nbMajSiren = $nbMajSiren2 = $nbMajAdr = 0 ;
2012-10-16 07:44:31 +00:00
$tabMarques = array ();
$argv = $_SERVER [ 'argv' ];
if ( $_SERVER [ 'argc' ] > 1 )
{
for ( $i = 1 ; isset ( $argv [ $i ]); $i ++ ) {
if ( substr ( $argv [ $i ], 0 , 1 ) == '-' ) {
switch ( substr ( $argv [ $i ], 1 , 1 )) {
2013-06-19 08:24:49 +00:00
case 'v' : define ( MODE_VERBOSE , true );
$modeDebug = true ;
2012-10-16 07:44:31 +00:00
define ( 'TMIN' , 1 );
define ( 'TMAX' , 2 );
break ;
case 'f' : $force = true ; break ;
case 'm' : $max = true ;
$annee = substr ( $argv [ $i ], 3 ); break ;
case 'i' : $majNum = true ; break ;
case 'r' : $majNom = true ; break ;
case 'r' : $majObj = true ; break ;
2013-06-19 08:24:49 +00:00
case 'd' : $majDate = true ; break ;
2012-10-16 07:44:31 +00:00
case 'p' : $getPdf = true ; break ;
case 'b' : $getMBopi = true ; break ;
2013-06-19 08:24:49 +00:00
case 's' : $majSiren = true ; break ;
case 'a' : $majAdresses = true ; break ;
case 't' : $majImages = true ; break ;
2012-10-16 07:44:31 +00:00
case '-' :
case '?' : die ( $strInfoScript ); break ;
default : die ( 'Option ' . $argv [ $i ] . " inconnue ! \n " );
break ;
}
} else {
$tabMarques [] = $argv [ $i ];
}
}
}
2013-06-19 08:24:49 +00:00
if ( ! defined ( 'MODE_VERBOSE' ))
define ( 'MODE_VERBOSE' , false );
2012-10-16 07:44:31 +00:00
/*
if ( ! defined ( 'TMIN' )) define ( 'TMIN' , 31 );
if ( ! defined ( 'TMAX' )) define ( 'TMAX' , 160 );
if ( ! defined ( 'TMIN' )) define ( 'TMIN' , 21 );
if ( ! defined ( 'TMAX' )) define ( 'TMAX' , 60 );
*/
if ( ! defined ( 'TMIN' )) define ( 'TMIN' , 10 );
if ( ! defined ( 'TMAX' )) define ( 'TMAX' , 20 );
$iDb = new WDB ( 'bopi' );
if ( $getPdf ) {
2013-06-19 08:24:49 +00:00
if ( $annee ) $strAnnee = " nomPdf LIKE '% $annee %' " ;
else $strAnnee = '1' ;
2012-10-16 07:44:31 +00:00
$ret = $iDb -> select ( 'infos_bopi' ,
'MAX(nomPdf) as nomPdf' ,
2013-06-19 08:24:49 +00:00
$strAnnee , false , MYSQL_ASSOC );
2012-10-16 07:44:31 +00:00
$ret = $ret [ 0 ];
$pdf = $ret [ 'nomPdf' ];
2013-06-19 08:24:49 +00:00
echo date ( 'Y/m/d - H:i:s' ) . " - Dernier BOPI Marques t<> l<EFBFBD> charg<72> : $pdf ... " . EOL ;
2012-10-16 07:44:31 +00:00
$anneeMois = strtr ( $pdf , array ( 'bopi_marques_' => '' , 'v1' => '' , 'v2' => '' , '.pdf' => '' ));
$tabAnneeMois = explode ( '-' , $anneeMois );
$lastAnnee = $tabAnneeMois [ 0 ];
$numFin = $tabAnneeMois [ 1 ];
2013-06-19 08:24:49 +00:00
//$lastAnnee=2011;
2012-10-16 07:44:31 +00:00
for ( $annee = $lastAnnee ; $annee <= date ( 'Y' ); $annee ++ ) {
if ( $annee == date ( 'Y' )) {
2013-06-19 08:24:49 +00:00
$numDeb = $numFin ;
2012-10-16 07:44:31 +00:00
$numFin = date ( 'W' );
//echo 'A'.EOL;
} elseif ( $numFin == 52 && $annee + 1 == date ( 'Y' )) {
$annee ++ ;
$numDeb = 1 ;
$numFin = date ( 'W' );
//echo 'B'.EOL;
} else {
$numDeb = 1 ;
$numFin = 52 ;
//echo 'C'.EOL;
}
2013-06-19 08:24:49 +00:00
/* $annee = 2011 ;
$numDeb = 37 ;
//die($annee.EOL.$lastAnnee.EOL.date('Y').EOL.$numDeb.EOL.$numFin.EOL);*/
2012-10-16 07:44:31 +00:00
for ( $num = $numDeb ; $num <= $numFin ; $num ++ ) {
$taille = $iMarque -> getEditionPdf ( 'marques' , $annee , $num );
$taille = round ( $taille / ( 1024 * 1024 ), 2 );
echo date ( 'Y/m/d - H:i:s' ) . " - BOPI $annee - $num : $taille Mo. " . EOL ;
}
}
}
if ( $getMBopi ) {
$ret = $iDb -> select ( 'infos_bopi_marques' ,
'DISTINCT numeroMarque' ,
'dateRecup IS NULL OR nbResults IS NULL OR nbResults<1' , false , MYSQL_ASSOC );
foreach ( $ret as $i => $tabTmp )
$tabMarques [] = $tabTmp [ 'numeroMarque' ];
shuffle ( $tabMarques );
$majNum = true ;
}
$nb = count ( $tabMarques );
$nbAcces = $nbCache = 0 ;
foreach ( $tabMarques as $i => $marque ) {
2013-06-19 08:24:49 +00:00
echo date ( 'Y/m/d - H:i:s' ) . " - Marque $i / $nb : n<> $marque ... " ;
2012-10-16 07:44:31 +00:00
if ( $majNum ) {
$ret = $iMarque -> getMarques ( 0 , $marque );
if ( is_array ( $ret ) && count ( $ret ) > 0 ) {
print_r ( $ret );
$nbCache ++ ;
} else {
2013-06-19 08:24:49 +00:00
$majAdresses = $majSiren = true ;
$res = $iMarque -> getListeMarques ( $marque , '' , '' , '' , true );
2012-10-16 07:44:31 +00:00
if ( $getMBopi ) {
$iDb -> update ( 'infos_bopi_marques' ,
array ( 'dateRecup' => date ( 'Ymd' ),
'nbResults' => $res ),
" numeroMarque= $marque " , false );
$nbAcces ++ ;
randsleep ( TMIN , TMAX );
if ( ! $modeDebug && date ( 'Hi' ) * 1 > 1930 ) {
$nbTot = $nbAcces + $nbCache ;
$pct =@ round ( $nbCache / $nbTot * 100 , 2 );
2013-06-19 08:24:49 +00:00
@ sendMail ( 'production@scores-decisions.com' , 'ylenaour@scores-decisions.com' , " Pompe Marques - Traitement quotidien " , " $nbTot requ<71> tes trait<69> es dont $nbCache en cache ( $pct %) " );
break ;
2012-10-16 07:44:31 +00:00
}
}
}
}
elseif ( $majNom )
2013-06-19 08:24:49 +00:00
$iMarque -> getListeMarques ( '' , $marque , '' , '' , true );
2012-10-16 07:44:31 +00:00
elseif ( $majObj )
2013-06-19 08:24:49 +00:00
$iMarque -> getListeMarques ( '' , '' , $marque , '' , true );
elseif ( $majDate )
$iMarque -> getListeMarques ( '' , '' , '' , WDate :: dateT ( 'Ymd' , 'Y-m-d' , strtr ( $marque , array ( '-' => '' , '/' => '' ,))), true );
}
if ( $majSiren ) {
// Identifiants Textes Uniques
$nbSirened = 0 ;
// Tableau RS>Siren
global $tabRsSiren ;
$tabRsSiren = array ();
$tmp = $iDb -> select ( 'marques' , 'idTxtUnique, sirenDeposant' , " sirenDeposant>0 AND idTxtUnique<>'' GROUP BY idTxtUnique " , false , MYSQL_ASSOC );
foreach ( $tmp as $nbId => $tabTmp )
$tabRsSiren [ $tabTmp [ 'idTxtUnique' ]] = $tabTmp [ 'sirenDeposant' ];
echo date ( 'Y/m/d - H:i:s' ) . " - La table 'bopi.marques' idTxt/Siren contient $nbId lignes... " . EOL ;
$tmp = $iDb -> select ( 'marques' , 'id, idTxtUnique' , " sirenDeposant<100 AND idTxtUnique<>'' AND (detailDeposant<>'') AND sirenValide=0 " , false , MYSQL_ASSOC );
$nbMaj = count ( $tmp );
echo date ( 'Y/m/d - H:i:s' ) . " - Nombre de siren <20> mettre <20> jour : $nbMaj " . EOL ;
$iDb2 = new WDb ( 'jo' );
foreach ( $tmp as $iLot => $tabTmp ) {
$id = $tabTmp [ 'id' ];
$idTxtUnique = $tabTmp [ 'idTxtUnique' ];
if ( isset ( $tabRsSiren [ $idTxtUnique ])) {
$siren = $tabRsSiren [ $idTxtUnique ];
if ( $iDb -> update ( 'marques' , array ( 'sirenDeposant' => $siren ,
'sirenValide' => 3 ), 'id=' . $id ))
$nbMajSiren ++ ;
echo mysql_error ();
$iDb2 -> delete ( 'bodacc_sirenage' , " idTable='marques' AND idAnn= $id " );
echo mysql_error ();
echo date ( 'Y/m/d - H:i:s' ) . " - $iLot / $nbMaj : Mise <20> jour de la marque id= $id avec le siren $siren ... " . EOL ;
//die();
} /* else {
$siren2 = isSimilar ( $idTxtUnique );
if ( $siren2 <> false ) {
if ( $iDb -> update ( 'marques' , array ( 'sirenDeposant' => $siren2 ,
'sirenValide' => 4 ), 'id=' . $id ))
$nbMajSiren2 ++ ;
$nbMajSiren ++ ;
echo mysql_error ();
$iDb2 -> delete ( 'bodacc_sirenage' , " idTable='marques' AND idAnn= $id " );
echo mysql_error ();
echo date ( 'Y/m/d - H:i:s' ) . " - $iLot / $nbMaj : Mise <20> jour de la marque id= $id avec le siren $siren2 ... " . EOL ;
die ();
}
//else echo date ('Y/m/d - H:i:s')." - $iLot/$nbMaj : pas de similitudes pour la marque id=$id...".EOL;
} */
}
echo date ( 'Y/m/d - H:i:s' ) . " - $nbMajSiren siren ont <20> t<EFBFBD> mis <20> jour sur $nbMaj (dont $nbMajSiren2 par approximation). " . EOL ;
}
if ( $majAdresses ) {
$tmp = $iDb -> select ( 'marques' , " id, deposantMarque, SUBSTRING(detailDeposant, LENGTH(deposantMarque)+2, LENGTH(detailDeposant)-LENGTH(deposantMarque)) AS adresse, IF(SUBSTRING(detailDeposant,LENGTH(detailDeposant),1)=')', 'FR', TRIM(REPLACE(SUBSTRING(detailDeposant,LENGTH(detailDeposant)-2,4),')',''))) AS codPays2 " , " deposant_AdrCP IS NULL OR deposantPays IS NULL " , false , MYSQL_ASSOC );
$iDb2 = new WDb ( 'bopi' );
$nbLots = count ( $tmp );
foreach ( $tmp as $iLot => $tabTmp ) {
$id = $tabTmp [ 'id' ];
$adr = trim ( preg_replace ( '/\(SIREN \d\d\d\d\d\d\d\d\d\)/' , '' , $tabTmp [ 'adresse' ]));
$adr = trim ( preg_replace ( '/, FR$/' , '' , $adr ));
$adr = trim ( preg_replace ( '/(\d\d\d\d\d), /' , '$1 ' , $adr ));
$adr = trim ( preg_replace ( '/ F-/' , '' , $adr ));
$adr = trim ( preg_replace ( '/FORME JURIDIQUE|SOCI.T. ANONYME/i' , '' , $adr ));
$tabAdresse = $iInsee -> structureVoie ( $adr );
$tabUpdate = array ();
$tabUpdate [ 'deposant_AdrNum' ] =@ $tabAdresse [ 'num' ];
$tabUpdate [ 'deposant_AdrIndRep' ] =@ $tabAdresse [ 'indRep' ];
$tabUpdate [ 'deposant_AdrTypVoie' ] =@ $tabAdresse [ 'typeVoie' ];
$tabUpdate [ 'deposant_AdrLibVoie' ] =@ $tabAdresse [ 'libVoie' ];
$tabUpdate [ 'deposant_AdrCP' ] =@ $tabAdresse [ 'cp' ];
$tabUpdate [ 'deposant_AdrVille' ] =@ $tabAdresse [ 'ville' ];
$tabUpdate [ 'deposant_AdrComp' ] =@ trim ( $tabAdresse [ 'adrComp0' ] . ' ' . $tabAdresse [ 'adrComp1' ]);
$tabUpdate [ 'deposantPays' ] = strtoupper ( $tabTmp [ 'codPays2' ]);
//print_r($tabAdresse);
echo " $iLot / $nbLots\t $adr\t " ;
if ( ! $iDb2 -> update ( 'marques' , $tabUpdate , " id= $id " , false ))
echo " ERREUR !!! " . EOL ;
else {
$nbMajAdr ++ ;
echo " ok " . EOL ;
}
}
echo date ( 'Y/m/d - H:i:s' ) . " - $nbMajAdr adresses ont <20> t<EFBFBD> mis <20> jour sur $nbLots . " . EOL ;
}
if ( $majImages ) {
$nbLots = $iDb -> select ( 'marques' , " id, nomMarque, numeroMarque, dateDepot, YEAR(dateDepot) AS annee, dateDepot*1 AS dateDepotYMD, dateUpdate " ,
" urlImage<>'' AND fileName='' " , false , MYSQL_ASSOC , true );
$iDb2 = new WDb ( 'bopi' );
$tabExt = array ( 'gif' , 'png' , 'jpg' , 'jpeg' , 'bmp' , 'pcx' );
$i = 0 ;
while ( $tabTmp = $iDb -> fetch ( MYSQL_ASSOC )) {
$id = $tabTmp [ 'id' ];
$nom = $tabTmp [ 'nomMarque' ];
$dateDepot = $tabTmp [ 'dateDepot' ];
$dateDepotYMD = $tabTmp [ 'dateDepotYMD' ];
$annee = $tabTmp [ 'annee' ];
$num = $tabTmp [ 'numeroMarque' ];
$dateUpdate = $tabTmp [ 'dateUpdate' ];
$filename = DOC_WEB_LOCAL . " marques/ $num " ;
$dossierCible = DOC_WEB_LOCAL . " marques/ $annee / $dateDepotYMD / " ;
$ext = '' ;
echo " $i / $nbLots : $id , $num , $nom , $dateDepot " ;
$tabUpdate = array ();
foreach ( $tabExt as $ext ) {
$fileNameLong = $filename . " . $ext " ;
if ( file_exists ( $fileNameLong )) {
$fileName = basename ( $fileNameLong );
$tabUpdate = array ( 'fileName' => $fileName , 'dateUpdate' => $dateUpdate );
break ;
}
}
if ( count ( $tabUpdate ) > 0 ) {
@ mkdir ( DOC_WEB_LOCAL . " marques/ $annee " );
@ mkdir ( $dossierCible );
@ copy ( $fileNameLong , $dossierCible . $fileName );
echo " , $fileName " ;
//@mkdir(DOC_WEB_LOCAL."marques/old");
@ move ( $fileNameLong , DOC_WEB_LOCAL . " marques/old/ $fileName " );
if ( !@ $iDb2 -> update ( 'marques' , $tabUpdate , " id= $id " , false ))
echo " \t ERREUR SQL !!! " . EOL ;
else {
$nbMajAdr ++ ;
echo " \t ok " . EOL ;
}
} else {
$res = $iMarque -> getListeMarques ( $num , '' , '' , '' , true );
echo " \t FICHIER INEXISTANT $res !!! " . EOL ;
//die();
}
$i ++ ;
}
echo date ( 'Y/m/d - H:i:s' ) . " - $nbMajAdr adresses ont <20> t<EFBFBD> mis <20> jour sur $nbLots . " . EOL ;
2012-10-16 07:44:31 +00:00
}
2013-06-19 08:24:49 +00:00
die ();
$repriseIdTxt = false ;
if ( $repriseIdTxt ) {
$iDb2 = new WDB ( 'bopi' );
$ret = $iDb -> select ( 'marques' ,
'id, idTxtUnique, detailDeposant, dateUpdate' ,
" idTxtUnique IS NULL " , false , MYSQL_ASSOC );
foreach ( $ret as $i => $tabInsert ) {
$idTxtUnique = trim ( strtoupper ( preg_replace ( '/[^a-z0-9]/i' , '' , $tabInsert [ 'detailDeposant' ])));
if ( $idTxtUnique <> '' )
$tabInsert [ 'idTxtUnique' ] = $idTxtUnique ;
$id = $tabInsert [ 'id' ];
unset ( $tabInsert [ 'id' ]);
unset ( $tabInsert [ 'detailDeposant' ]);
$iDb2 -> update ( 'marques' , $tabInsert , " id= $id " , false );
//die($id);
}
}
2012-10-16 07:44:31 +00:00
die ();
2013-06-19 08:24:49 +00:00
2012-10-16 07:44:31 +00:00
$referer = $cookie = '' ;
$tDeb = microtime ( true );
$page = getUrl ( $url , $cookie , $post , $referer , false , '' , '' , 60 );
$duree = round ( microtime ( true ) - $tDeb , 3 );
$body = $page [ 'body' ];
die ();
if ( $max ) {
$idQualibat = $iQualibat -> getMaxQualibat ();
echo date ( 'Y/m/d - H:i:s' ) . " - Dernier id Qualibat : ' $idQualibat '. " . EOL ;
die ();
}
2013-06-19 08:24:49 +00:00
echo date ( 'Y/m/d - H:i:s' ) . " - DEBUT du programme de r<> cup<75> ration des entreprises Qualibat. " . EOL ;
2012-10-16 07:44:31 +00:00
if ( count ( $tabQualibat ) == 0 ) {
$partiel = false ;
$idQualibat = $iQualibat -> getMaxQualibat ();
if ( $modeDebug ) echo date ( 'Y/m/d - H:i:s' ) . " - Dernier id Qualibat : ' $idQualibat '. " . EOL ;
for ( $id = 1 ; $id <= $idQualibat ; $id ++ ) {
$numQualibatTmp = $id ;
$tabQualibat [] = $numQualibatTmp ;
}
2013-06-19 08:24:49 +00:00
// Il faudrait rechercher le dernier num<75> ro Orias attribu<62> non encore en base
2012-10-16 07:44:31 +00:00
$iQualibat -> force = $force ;
while ( true ) {
$numQualibatTmp ++ ;
2013-06-19 08:24:49 +00:00
echo date ( 'Y/m/d - H:i:s' ) . " - Recherche du dernier num<75> ro Qualibat attribu<62> : " ;
2012-10-16 07:44:31 +00:00
if ( $iQualibat -> getInfosQualibat ( 0 , $numQualibatTmp )) {
if ( ! $iQualibat -> enCache ) {
randsleep ( $tMin / 10 , $tMax / 10 );
$nbAcces ++ ;
} else
$nbCache ++ ;
echo " $numQualibatTmp " ;
} else {
echo -- $numQualibatTmp . ' !' . EOL ;
break ;
}
}
shuffle ( $tabQualibat );
2013-06-19 08:24:49 +00:00
echo date ( 'Y/m/d - H:i:s' ) . " - Mise <20> jour de la base Qualibat... " . EOL ;
2012-10-16 07:44:31 +00:00
} else {
$partiel = true ;
2013-06-19 08:24:49 +00:00
echo date ( 'Y/m/d - H:i:s' ) . " - Chargement de num<75> ros Qualibat... " . EOL ;
2012-10-16 07:44:31 +00:00
}
$nb = count ( $tabQualibat );
$nbAcces = $nbCache = 0 ;
foreach ( $tabQualibat as $i => $idQualibat ) {
echo date ( 'Y/m/d - H:i:s' ) . " - $i / $nb : $idQualibat ... " ;
$iQualibat -> force = $force ;
print_r ( $iQualibat -> getInfosQualibat ( 0 , $idQualibat ));
2013-06-19 08:24:49 +00:00
echo date ( 'Y/m/d - H:i:s' ) . " - $i / $nb : $idQualibat ... r<> cup<75> r<EFBFBD> " ;
2012-10-16 07:44:31 +00:00
if ( ! $partiel && date ( 'Hi' ) * 1 >= 1930 ) {
//if (date('Hi')*1>=2235) {
$nbTot = $nbAcces + $nbCache ;
$pct =@ round ( $nbCache / $nbTot * 100 , 2 );
2013-06-19 08:24:49 +00:00
@ sendMail ( 'production@scores-decisions.com' , 'ylenaour@scores-decisions.com' , " Pompe Qualibat - Traitement quotidien " , " $nbTot requ<71> tes trait<69> es dont $nbCache en cache ( $pct %) " );
2012-10-16 07:44:31 +00:00
die ();
}
if ( ! $iQualibat -> enCache ) {
echo " via WWW. Attente... " . EOL ;
if ( ! $partiel ) randsleep ( $tMin , $tMax );
$nbAcces ++ ;
} else {
echo " en cache ! " . EOL ;
$nbCache ++ ;
}
}
$nbTot = $nbAcces + $nbCache ;
$pct = round ( $nbCache / $nbTot * 100 , 2 );
if ( ! $partiel ) {
2013-06-19 08:24:49 +00:00
@ sendMail ( 'production@scores-decisions.com' , 'ylenaour@scores-decisions.com' , " Pompe Qualibat - Tout est fini ! " , " FIN des $nbTot requ<71> tes trait<69> es dont $nbCache en cache ( $pct %) " );
}
echo date ( 'Y/m/d - H:i:s' ) . " - FIN des $nbTot requ<71> tes dont $nbCache en cache ( $pct %) " . EOL ;
function isSimilar ( $idTxtUnique ) {
global $tabRsSiren ;
$taille = strlen ( $idTxtUnique );
if ( $taille < 40 ) return false ;
foreach ( $tabRsSiren as $idTxt => $siren ) {
if ( levenshtein ( $idTxtUnique , $idTxt ) < 2 )
return $siren ;
}
return false ;
2012-10-16 07:44:31 +00:00
}
2013-06-19 08:24:49 +00:00
2012-10-16 07:44:31 +00:00
die ();