228 lines
6.2 KiB
PHP
228 lines
6.2 KiB
PHP
<?php
|
|
// Define path to application directory
|
|
defined('APPLICATION_PATH')
|
|
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
|
|
|
|
// Define application environment
|
|
define('APPLICATION_ENV', 'production');
|
|
|
|
// Ensure library/ is on include_path
|
|
set_include_path(implode(PATH_SEPARATOR, array(
|
|
realpath(APPLICATION_PATH . '/../library'),
|
|
get_include_path(),
|
|
)));
|
|
|
|
/** Zend_Application */
|
|
require_once 'Zend/Application.php';
|
|
|
|
// Create application, bootstrap, and run
|
|
$application = new Zend_Application(
|
|
APPLICATION_ENV,
|
|
APPLICATION_PATH . '/configs/application.ini'
|
|
);
|
|
require_once APPLICATION_PATH . '/../library/Zend/Loader/Autoloader.php';
|
|
|
|
Class extract
|
|
{
|
|
protected $db;
|
|
protected $dbMetier;
|
|
protected $reference;
|
|
|
|
public function __construct($reference)
|
|
{
|
|
$this->reference = $reference;
|
|
$dbConfig = new Zend_Config_Ini(APPLICATION_PATH . '/configs/configuration.ini', 'databases');
|
|
try {
|
|
$this->db = Zend_Db::factory($dbConfig->db);
|
|
} catch ( Exception $e ) {
|
|
exit ( $e->getMessage() );
|
|
}
|
|
}
|
|
|
|
public function getHttp()
|
|
{
|
|
/* Depuis le navigateur */
|
|
}
|
|
|
|
protected function getCj($liste)
|
|
{
|
|
foreach($liste as $valeur)
|
|
{
|
|
if(strlen($valeur) < 5) {
|
|
$sql = $this->db->select('formejuridique', array('fjCode'))
|
|
->from('formejuridique')
|
|
->where('fjCode LIKE "'.$valeur.'%"');
|
|
} else {
|
|
$sql = $this->db->select('formejuridique', array('fjCode'))
|
|
->from('formejuridique')
|
|
->where('fjCode = "'.$valeur.'"');
|
|
}
|
|
$result = $this->db->fetchAll($sql);
|
|
foreach ($result as $code) {
|
|
if(strlen($code['fjCode']) > 2) {
|
|
$return[] = $code['fjCode'];
|
|
}
|
|
}
|
|
}
|
|
return ($return);
|
|
}
|
|
|
|
protected function localisationArbo($name, $valeur)
|
|
{
|
|
if(strlen($valeur) == 2) {
|
|
switch($name) {
|
|
case 'adr_reg':
|
|
$return = array();
|
|
$sql = $this->db->select()
|
|
->from('departements')
|
|
->join(array('cp' => 'codepostaux'), 'cp.codepos LIKE CONCAT(departements.numdep, "%")', array('INSEE'))
|
|
->where('codeRegionInsee ='.$valeur);
|
|
$result = $this->db->fetchAll($sql);
|
|
foreach ($result as $res) {
|
|
$return[] = trim($res['INSEE']);
|
|
}
|
|
break;
|
|
case 'adr_dept':
|
|
$return = array();
|
|
$sql = $this->db->select()
|
|
->from('departements')
|
|
->join(array('cp' => 'codepostaux'), 'cp.codepos LIKE CONCAT(departements.numdep, "%")', array('INSEE'))
|
|
->where('numdep ='.$valeur);
|
|
$result = $this->db->fetchAll($sql);
|
|
foreach ($result as $res) {
|
|
$return[] = trim($res['INSEE']);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
return ($return);
|
|
}
|
|
|
|
public function getMinMax($name, $valeur)
|
|
{
|
|
$valeur = explode(',', $valeur);
|
|
if(count($valeur) > 1)
|
|
{
|
|
$sql = 'SELECT min, max FROM minmax WHERE cle ="'.$name.'"';
|
|
$result = $this->db->query($sql);
|
|
$result = $result->fetchAll();
|
|
if(empty($valeur[0])){
|
|
echo $name;
|
|
$valeur[0] = $result[0]['min'];
|
|
}
|
|
if(empty($valeur[1])){
|
|
$valeur[1] = $result[0]['max'];
|
|
}
|
|
}
|
|
return ($valeur);
|
|
}
|
|
|
|
protected function nafArbo($key, $valeur)
|
|
{
|
|
$value = array();
|
|
$valeur = trim($valeur);
|
|
|
|
if(strlen($valeur) == 1)
|
|
{
|
|
$sql = 'SELECT * FROM naf WHERE parent ='.$valeur;
|
|
$result =$this->db->query($sql);
|
|
$result = $result->fetchAll();
|
|
foreach($result as $code) $where .= " code LIKE '".trim($code['code'])."%' and niveau = 5 or ";
|
|
$where = substr($where, 0, (strlen($where)) - 3);
|
|
$sql = 'SELECT * FROM naf '.$where;
|
|
$result = $this->db->query($sql);
|
|
$result = $result->fetchAll();
|
|
foreach($result as $code) $value[] = $code['code'];
|
|
} else if(strlen($valeur) < 5){
|
|
//$sql = $table->select()->where('code LIKE "'.trim($valeur).'%" and niveau = 5');
|
|
$sql = 'SELECT * FROM naf WHERE code LIKE "'.trim($valeur).'%" and niveau = 5';
|
|
$result = $this->db->query($sql);
|
|
$result = $result->fetchAll();
|
|
foreach($result as $code) $value[] = $code['code'];
|
|
} else if(strlen($valeur) == 5) {
|
|
$value[] = $valeur;
|
|
}
|
|
return ($value);
|
|
}
|
|
|
|
public function getCli()
|
|
{
|
|
require_once('Scores/Ciblage.php');
|
|
//require_once('Scores/Field.php');
|
|
|
|
$sql = 'SELECT id, idDefinition, uniteInsee FROM comptages WHERE id = '.$this->reference;
|
|
$fetch = $this->db->query($sql);
|
|
$result = $fetch->fetchAll();
|
|
$idComptage = $result[0]['id'];
|
|
$uniteInsee = $result[0]['uniteInsee'];
|
|
|
|
$sql = 'SELECT id, criteres.criteres FROM criteres WHERE id ='.$result[0]['idDefinition'];
|
|
$fetch = $this->db->query($sql);
|
|
$result = $fetch->fetchAll();
|
|
$idCriteres = $result[0]['id'];
|
|
|
|
$structure = json_decode($result[0]['criteres']);
|
|
$element = array();
|
|
foreach ($structure as $name => $valeur){
|
|
if($name == 'adr_com') {
|
|
$liste = explode(',', $valeur);
|
|
$codes = array();
|
|
foreach ($liste as $element) {
|
|
$val = explode(':', $element);
|
|
if(!empty($val[1]))
|
|
$codes = array_merge($this->localisationArbo($val[0], $val[1]), $codes);
|
|
}
|
|
$array['adr_com'] = $codes;
|
|
}
|
|
else if($name == 'cj') {
|
|
$array['cj'] = $this->getCj(explode(',', $valeur));
|
|
}
|
|
else if($name == 'ape_etab') {
|
|
$values = array();
|
|
$nafs = explode(',', $structure->ape_etab);
|
|
foreach($nafs as $naf) {
|
|
if($naf != ' ') {
|
|
$values = array_merge($values, $this->nafArbo('ape_etab', $naf));
|
|
}
|
|
}
|
|
$array['ape_etab'] = $values;
|
|
}else {
|
|
$valeur = $this->getMinMax($name, $valeur);
|
|
$array[$name] = $valeur;
|
|
}
|
|
}
|
|
unset($array['vil']);
|
|
unset($array['adr_com']);
|
|
unset($array['adr_dept']);
|
|
$array['bilTca'] = array(4, 5, 6);
|
|
$ciblage = new Ciblage($array, true);
|
|
$res = $ciblage->execute(true);
|
|
|
|
$data = array(
|
|
'idComptage' => $idComptage,
|
|
'idCriteres' => $idCriteres,
|
|
'idProfil' => 1,
|
|
'reference' => 'batch',
|
|
'identifiants' => json_encode($res),
|
|
'nbLigneTotales' => count($res),
|
|
'nbLigneTraites' => count($res),
|
|
'uniteInsee' => $uniteInsee,
|
|
'error' => 0
|
|
);
|
|
$this->db->insert('enrichissement_identifiants',$data);
|
|
$fd = fopen($idComptage.'_batch'.date('Y-m-d-hms').'.csv', 'x+');
|
|
try {
|
|
$array = array();
|
|
foreach($res as $field) {
|
|
fputcsv($fd, array($field), ';');
|
|
}
|
|
} catch(Exception $e) {
|
|
echo $e->getMessage();
|
|
}
|
|
fclose($fd);
|
|
echo count($res).' Success !';
|
|
}
|
|
}
|
|
|
|
$a = new extract('38');
|
|
$a->getCli(); |