odea/application/controllers/EnrichissementController.php
2012-05-15 11:38:51 +00:00

256 lines
7.9 KiB
PHP

<?php
class EnrichissementController extends Zend_Controller_Action
{
public function init()
{
$this->view->headScript()->appendFile('/themes/default/scripts/enrichissement.js', 'text/javascript');
$this->view->headLink()->appendStylesheet('/themes/default/styles/enrichissement.css');
}
/**
* Enter description here ...
*/
public function indexAction(){}
public function fileformAction()
{
$this->view->headScript()->appendFile('/themes/default/scripts/jquery.form.js', 'text/javascript');
$this->view->headScript()->appendFile('/themes/default/scripts/jqueryprogressbar.js', 'text/javascript');
$this->view->assign('filesize', ini_get('upload_max_filesize'));
}
public function fileuploadAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$config = Zend_Registrey::get('configuration');
$path = realpath($config->path->data).'/clients';
if(!file_exists($path)) mkdir($path);
if ( isset($_FILES) && count($_FILES)==1 ){
$n = $_FILES['fichier']['name'];
$s = $_FILES['fichier']['size'];
$tmp_name = $_FILES['fichier']['tmp_name'];
$name = $_REQUEST['ref'];
$extValide = array('csv');
$extension = strrchr($n,'.');
$extension = substr($extension,1);
//@todo : vérifier l'extension du fichier
if (move_uploaded_file($tmp_name, $path.'/'.$name.'.'.$extension)){
echo "Uploadé !";
} else {
echo "Erreur : ".$_FILES['fichier']['error'];
}
}
}
/**
* Etat de progression de l'upload du fichier
*/
public function fileprogressAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$request = $this->getRequest();
$key = $request->getParam('key', '');
if ( !empty($key) ) {
//$rep sera égal à false si la clef n'existe pas dans le cache apc
$rep = apc_fetch('upload_'.$key);
echo json_encode($rep);
}
}
/**
* Enregistrement de la commande pour l'ennrichissement
*/
public function commandeAction()
{
set_time_limit(60);
$this->_helper->layout()->disableLayout();
$request = $this->getRequest();
$idCriteres = $request->getParam('id', null);
$idProfil = $request->getParam('profil', null);
//Vérifier les profils du client
if ( $idProfil===null ){
//Selection du profil du client
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
$profilsM = new Application_Model_EnrichissementProfils();
$sql = $profilsM->select()
->from($profilsM, array('id'))
->where('idClient=?', $user->idClient)
->where('login=?', $user->username)
->where('actif=?', 1);
$profil = $profilsM->fetchRow($sql);
$idProfil = $profil->id;
}
$comptage = new Application_Model_Comptages();
$sql = $comptage->select()->where('idDefinition = ?', $idCriteres);
$result = $comptage->fetchRow($sql);
$idComptage = $result['id'];
//Récupération des critères du ciblage
$criteresM = new Application_Model_Criteres();
$criteresRow = $criteresM->find($idCriteres);
$criteres = $criteresRow->current();
$structure = json_decode($criteres->criteres, true);
//Récupération des SIRET
require_once 'Scores/Ciblage.php';
$ciblage = new Ciblage($structure, true);
$infosExtraction = $ciblage->execute(true);
/*
* @todo :
* Vérifier le nombre d'éléments avec le nombre de ligne
*
*
*/
//Attention calcul uniteInsee réelle
//car si donnée insee alors toutes les lignes doivent être comptés en unité insee
$data = array(
'idComptage' => $idComptage,
'reference' => $this->getRequest()->getParam('ref'),
'idCriteres' => $criteres->id,
'idProfil' => $idProfil,
'identifiants' => json_encode($infosExtraction),
'idProfil' => $idProfil,
'fichier' => '',
'nbLigneTotales' => count($infosExtraction),
'nbLigneTraites' => 0,
'uniteInsee' => $result['uniteInsee'],
'error' => '',
'dateAdded' => date('YmdHms'),
);
$identifiantsM = new Application_Model_EnrichissementIdentifiants();
$idIdentifiant = $identifiantsM->insert($data);
//exec('php '.APPLICATION_PATH.'/../batch/enrichissement.php --id '.$idIdentifiant);
}
/**
* Demande de référence pour l'enrichissement
*/
public function referenceAction()
{
$this->_helper->layout()->disableLayout();
$request = $this->getRequest();
//Récupération du profil de l'utilisateur
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
$profilsM = new Application_Model_EnrichissementProfils();
$sql = $profilsM->select()
->from($profilsM, array('id', 'reference', 'tarifLigne', 'dataInsee'))
->where('idClient=?', $user->idClient)
->where('login=?', $user->username)
->where('actif=?', 1);
$profil = $profilsM->fetchRow($sql);
//
if ($profil!==null)
{
$this->view->assign('profil', true);
$idCritere = $request->getParam('id', null);
$comptagesM = new Application_Model_Comptages();
$sql = $comptagesM->select()
->where('idDefinition = ?', $idCritere)
->order('dateAjout DESC')
->limit(1);
$comptages = $comptagesM->fetchAll($sql);
$old = $comptages;
$oldResultat = $old[0]->resultat;
$oldResultatInsee = $old[0]->uniteInsee;
if ( $comptages->count()>0 ) {
$item = $comptages[0];
$date = explode(' ', $item->dateAjout);
$date = $date[0];
if($date != '0000-00-00') {
if($date != date('Y-m-d')) {
$criteres = new Application_Model_Criteres();
$sql = $criteres->select()->where('id = ?', $idCritere);
$result = $criteres->fetchRow($sql)->toArray();
$criteres = json_decode($result['criteres'], true);
require_once 'Scores/Ciblage.php';
$count = new Ciblage($criteres);
$item['resultat'] = $count->execute();
$item['uniteInsee'] = $count->calculRedevanceInsee();
}
}
$this->view->assign('resultat', $item['resultat']);
$this->view->assign('uniteInsee', $item['uniteInsee']);
//Calcul du prix
$redevanceInsee = 3.295/100; //Seuil de facturation 52 734 euros
$prixInsee = $item['uniteInsee']*$redevanceInsee;
$infoInsee = '';
if ($profil->dataInsee){
$prixInsee = $item['resultat']*$redevanceInsee;
$infoInsee = "Votre profil inclus au moins une donnée Insee, la redevance sera applicable sur chaque ligne.";
}
$prix = $item['resultat'] * $profil->tarifLigne + $prixInsee;
$this->view->prix = round($prix, 2);
$this->view->prixInsee = round($prixInsee, 2);
$this->view->infoInsee = $infoInsee;
$this->view->id = $item['id'];
/*$this->view->oldDate = $date;
$this->view->oldResult = $oldResultat;
$this->view->oldResultInsee = $oldResultatInsee;
$this->view->oldPrice = round(($oldResultat* $profil->tarifLigne)+ $oldResultatInsee);
$this->view->oldPriceInsee = round($oldResultatInsee*$redevanceInsee, 2);*/
$this->view->criteres = $criteres;
}
}
}
public function downloadAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$id = $this->getRequest()->getParam('id');
$table = new Application_Model_EnrichissementIdentifiants();
$sql = $table->select()
->where('id = ?', $id);
$result = $table->fetchRow($sql);
if(!empty($result)) {
$result = $result->toArray();
$date = explode(' ', $result['dateAdded']);
$path = $config->path->data.'/'.substr($date[0], 0, 7).'/';
$file = $result['fichier'];
}
$content_type = 'application/csv-tab-delimited-table';
//Envoi du fichier sur la sortie standard
if ( file_exists($path.$file) ) {
header('Content-Transfer-Encoding: none');
header('Content-type: ' . $content_type.'');
header('Content-Length: ' . filesize($path.$file));
header('Content-MD5: ' . base64_encode(md5_file($path.$file)));
header('Content-Disposition: filename="' . basename($path.$file) . '"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression', '0');
echo file_get_contents($path.$file);
} else {
echo 'Impossible de charger le fichier.';
}
}
}