odea/batch/extract.php

228 lines
6.2 KiB
PHP
Raw Normal View History

2012-03-15 16:44:10 +01:00
<?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(
2012-03-20 09:24:41 +01:00
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
2012-03-15 16:44:10 +01:00
)));
/** 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;
2012-05-02 21:06:36 +02:00
2012-03-15 16:44:10 +01:00
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() );
}
}
2012-05-02 21:06:36 +02:00
2012-03-15 16:44:10 +01:00
public function getHttp()
{
/* Depuis le navigateur */
}
2012-05-02 21:06:36 +02:00
2012-03-15 16:44:10 +01:00
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.'"');
2012-03-15 16:44:10 +01:00
}
$result = $this->db->fetchAll($sql);
foreach ($result as $code) {
if(strlen($code['fjCode']) > 2) {
$return[] = $code['fjCode'];
}
}
}
return ($return);
}
2012-05-02 21:06:36 +02:00
2012-03-20 09:24:41 +01:00
protected function localisationArbo($name, $valeur)
2012-03-15 16:44:10 +01:00
{
2012-03-20 09:24:41 +01:00
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;
2012-03-15 16:44:10 +01:00
}
}
return ($return);
}
2012-05-02 21:06:36 +02:00
2012-03-20 09:24:41 +01:00
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);
}
2012-05-02 21:06:36 +02:00
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);
}
2012-05-02 21:06:36 +02:00
2012-03-15 16:44:10 +01:00
public function getCli()
{
require_once('Scores/Ciblage.php');
2012-03-20 09:24:41 +01:00
//require_once('Scores/Field.php');
2012-05-02 21:06:36 +02:00
2012-03-15 16:44:10 +01:00
$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'];
2012-05-02 21:06:36 +02:00
2012-03-15 16:44:10 +01:00
$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']);
2012-03-20 09:24:41 +01:00
$element = array();
2012-03-15 16:44:10 +01:00
foreach ($structure as $name => $valeur){
2012-03-20 09:24:41 +01:00
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;
2012-03-15 16:44:10 +01:00
}
2012-03-20 09:24:41 +01:00
else if($name == 'cj') {
2012-03-15 16:44:10 +01:00
$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 {
2012-03-20 09:24:41 +01:00
$valeur = $this->getMinMax($name, $valeur);
$array[$name] = $valeur;
}
2012-03-15 16:44:10 +01:00
}
unset($array['vil']);
unset($array['adr_com']);
unset($array['adr_dept']);
$array['bilTca'] = array(4, 5, 6);
2012-03-20 09:24:41 +01:00
$ciblage = new Ciblage($array, true);
$res = $ciblage->execute(true);
2012-05-02 21:06:36 +02:00
2012-03-15 16:44:10 +01:00
$data = array(
'idComptage' => $idComptage,
'idCriteres' => $idCriteres,
2012-03-20 09:24:41 +01:00
'idProfil' => 1,
2012-03-15 16:44:10 +01:00
'reference' => 'batch',
'identifiants' => json_encode($res),
'nbLigneTotales' => count($res),
'nbLigneTraites' => count($res),
'uniteInsee' => $uniteInsee,
'error' => 0
);
2012-03-20 09:24:41 +01:00
$this->db->insert('enrichissement_identifiants',$data);
2012-03-15 16:44:10 +01:00
$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');
2012-03-15 16:44:10 +01:00
$a->getCli();