odea/application/controllers/EnrichissementController.php

256 lines
7.9 KiB
PHP
Raw Normal View History

2012-02-15 12:46:40 +00:00
<?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');
}
2012-05-02 19:06:36 +00:00
2012-02-15 12:46:40 +00:00
/**
* Enter description here ...
*/
public function indexAction(){}
2012-05-02 19:06:36 +00:00
2012-02-15 12:46:40 +00:00
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');
2012-05-02 19:06:36 +00:00
$this->view->assign('filesize', ini_get('upload_max_filesize'));
2012-02-15 12:46:40 +00:00
}
2012-05-02 19:06:36 +00:00
2012-02-15 12:46:40 +00:00
public function fileuploadAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
2012-05-02 19:06:36 +00:00
$config = Zend_Registrey::get('configuration');
2012-02-15 12:56:55 +00:00
$path = realpath($config->path->data).'/clients';
2012-02-15 12:46:40 +00:00
if(!file_exists($path)) mkdir($path);
2012-05-02 19:06:36 +00:00
2012-02-15 12:46:40 +00:00
if ( isset($_FILES) && count($_FILES)==1 ){
$n = $_FILES['fichier']['name'];
$s = $_FILES['fichier']['size'];
$tmp_name = $_FILES['fichier']['tmp_name'];
$name = $_REQUEST['ref'];
2012-05-02 19:06:36 +00:00
2012-02-15 12:46:40 +00:00
$extValide = array('csv');
$extension = strrchr($n,'.');
$extension = substr($extension,1);
//@todo : vérifier l'extension du fichier
2012-05-02 19:06:36 +00:00
2012-02-15 12:46:40 +00:00
if (move_uploaded_file($tmp_name, $path.'/'.$name.'.'.$extension)){
echo "Uploadé !";
} else {
echo "Erreur : ".$_FILES['fichier']['error'];
}
2012-05-02 19:06:36 +00:00
}
2012-02-15 12:46:40 +00:00
}
2012-05-02 19:06:36 +00:00
2012-02-15 12:46:40 +00:00
/**
* Etat de progression de l'upload du fichier
*/
public function fileprogressAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
2012-05-02 19:06:36 +00:00
$request = $this->getRequest();
2012-02-15 12:46:40 +00:00
$key = $request->getParam('key', '');
if ( !empty($key) ) {
2012-02-15 12:46:40 +00:00
//$rep sera égal à false si la clef n'existe pas dans le cache apc
$rep = apc_fetch('upload_'.$key);
echo json_encode($rep);
}
}
2012-05-02 19:06:36 +00:00
2012-02-21 09:38:04 +00:00
/**
* Enregistrement de la commande pour l'ennrichissement
*/
public function commandeAction()
{
2012-04-12 09:25:37 +00:00
set_time_limit(60);
$this->_helper->layout()->disableLayout();
2012-05-02 19:06:36 +00:00
2012-02-22 09:30:25 +00:00
$request = $this->getRequest();
2012-05-02 19:06:36 +00:00
$idCriteres = $request->getParam('id', null);
$idProfil = $request->getParam('profil', null);
//Vérifier les profils du client
if ( $idProfil===null ){
2012-05-02 19:06:36 +00:00
//Selection du profil du client
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
2012-05-02 19:06:36 +00:00
$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);
2012-05-02 19:06:36 +00:00
$idProfil = $profil->id;
}
$comptage = new Application_Model_Comptages();
$sql = $comptage->select()->where('idDefinition = ?', $idCriteres);
$result = $comptage->fetchRow($sql);
2012-04-13 07:33:23 +00:00
$idComptage = $result['id'];
2012-05-02 19:06:36 +00:00
2012-02-22 09:30:25 +00:00
//Récupération des critères du ciblage
2012-05-02 19:06:36 +00:00
$criteresM = new Application_Model_Criteres();
2012-03-20 08:24:41 +00:00
$criteresRow = $criteresM->find($idCriteres);
$criteres = $criteresRow->current();
2012-05-02 19:06:36 +00:00
2012-02-22 09:30:25 +00:00
$structure = json_decode($criteres->criteres, true);
2012-05-02 19:06:36 +00:00
//Récupération des SIRET
2012-02-22 09:30:25 +00:00
require_once 'Scores/Ciblage.php';
2012-05-15 11:38:51 +00:00
$ciblage = new Ciblage($structure, true);
2012-03-15 15:44:10 +00:00
$infosExtraction = $ciblage->execute(true);
2012-03-20 08:24:41 +00:00
2012-05-15 11:38:51 +00:00
/*
* @todo :
* Vérifier le nombre d'éléments avec le nombre de ligne
*
*
*/
//Attention calcul uniteInsee réelle
2012-02-22 09:30:25 +00:00
//car si donnée insee alors toutes les lignes doivent être comptés en unité insee
2012-03-20 08:24:41 +00:00
$data = array(
'idComptage' => $idComptage,
2012-03-20 08:24:41 +00:00
'reference' => $this->getRequest()->getParam('ref'),
'idCriteres' => $criteres->id,
'idProfil' => $idProfil,
2012-03-20 08:24:41 +00:00
'identifiants' => json_encode($infosExtraction),
2012-02-22 09:30:25 +00:00
'idProfil' => $idProfil,
'fichier' => '',
2012-03-20 08:24:41 +00:00
'nbLigneTotales' => count($infosExtraction),
'nbLigneTraites' => 0,
2012-05-15 11:38:51 +00:00
'uniteInsee' => $result['uniteInsee'],
2012-02-22 09:30:25 +00:00
'error' => '',
2012-04-05 08:20:14 +00:00
'dateAdded' => date('YmdHms'),
2012-02-22 09:30:25 +00:00
);
2012-05-02 19:06:36 +00:00
$identifiantsM = new Application_Model_EnrichissementIdentifiants();
$idIdentifiant = $identifiantsM->insert($data);
2012-05-15 11:38:51 +00:00
//exec('php '.APPLICATION_PATH.'/../batch/enrichissement.php --id '.$idIdentifiant);
2012-02-21 09:38:04 +00:00
}
2012-05-02 19:06:36 +00:00
2012-02-22 11:38:57 +00:00
/**
* Demande de référence pour l'enrichissement
*/
public function referenceAction()
{
2012-05-02 19:06:36 +00:00
$this->_helper->layout()->disableLayout();
2012-02-22 12:56:54 +00:00
$request = $this->getRequest();
2012-05-02 19:06:36 +00:00
2012-02-24 10:42:06 +00:00
//Récupération du profil de l'utilisateur
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
2012-05-02 19:06:36 +00:00
$profilsM = new Application_Model_EnrichissementProfils();
2012-02-24 10:42:06 +00:00
$sql = $profilsM->select()
->from($profilsM, array('id', 'reference', 'tarifLigne', 'dataInsee'))
->where('idClient=?', $user->idClient)
->where('login=?', $user->username)
->where('actif=?', 1);
2012-05-02 19:06:36 +00:00
$profil = $profilsM->fetchRow($sql);
2012-04-12 08:07:35 +00:00
//
2012-03-07 10:41:49 +00:00
if ($profil!==null)
2012-02-24 10:42:06 +00:00
{
2012-03-07 10:41:49 +00:00
$this->view->assign('profil', true);
2012-04-04 13:15:56 +00:00
$idCritere = $request->getParam('id', null);
2012-05-02 19:06:36 +00:00
$comptagesM = new Application_Model_Comptages();
2012-02-24 10:42:06 +00:00
$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;
2012-02-24 10:42:06 +00:00
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')) {
2012-05-02 19:06:36 +00:00
$criteres = new Application_Model_Criteres();
$sql = $criteres->select()->where('id = ?', $idCritere);
$result = $criteres->fetchRow($sql)->toArray();
$criteres = json_decode($result['criteres'], true);
2012-05-15 11:38:51 +00:00
require_once 'Scores/Ciblage.php';
$count = new Ciblage($criteres);
$item['resultat'] = $count->execute();
$item['uniteInsee'] = $count->calculRedevanceInsee();
}
}
2012-02-24 10:42:06 +00:00
$this->view->assign('resultat', $item['resultat']);
$this->view->assign('uniteInsee', $item['uniteInsee']);
2012-05-02 19:06:36 +00:00
//Calcul du prix
$redevanceInsee = 3.295/100; //Seuil de facturation 52 734 euros
2012-05-02 19:06:36 +00:00
$prixInsee = $item['uniteInsee']*$redevanceInsee;
2012-02-24 10:42:06 +00:00
$infoInsee = '';
if ($profil->dataInsee){
$prixInsee = $item['resultat']*$redevanceInsee;
2012-05-02 19:06:36 +00:00
$infoInsee = "Votre profil inclus au moins une donnée Insee, la redevance sera applicable sur chaque ligne.";
2012-02-24 10:42:06 +00:00
}
$prix = $item['resultat'] * $profil->tarifLigne + $prixInsee;
2012-05-02 19:06:36 +00:00
$this->view->prix = round($prix, 2);
$this->view->prixInsee = round($prixInsee, 2);
$this->view->infoInsee = $infoInsee;
$this->view->id = $item['id'];
2012-05-02 19:06:36 +00:00
2012-04-12 13:54:45 +00:00
/*$this->view->oldDate = $date;
$this->view->oldResult = $oldResultat;
$this->view->oldResultInsee = $oldResultatInsee;
2012-04-12 13:54:45 +00:00
$this->view->oldPrice = round(($oldResultat* $profil->tarifLigne)+ $oldResultatInsee);
$this->view->oldPriceInsee = round($oldResultatInsee*$redevanceInsee, 2);*/
2012-05-02 19:06:36 +00:00
$this->view->criteres = $criteres;
2012-05-02 19:06:36 +00:00
}
}
2012-05-02 19:06:36 +00:00
2012-02-22 11:38:57 +00:00
}
2012-05-02 19:06:36 +00:00
2012-04-12 09:25:37 +00:00
public function downloadAction()
{
2012-04-13 14:49:08 +00:00
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
2012-05-02 19:06:36 +00:00
2012-04-13 08:13:22 +00:00
$id = $this->getRequest()->getParam('id');
2012-05-02 19:06:36 +00:00
$table = new Application_Model_EnrichissementIdentifiants();
2012-04-13 08:13:22 +00:00
$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'];
}
2012-04-12 09:46:16 +00:00
$content_type = 'application/csv-tab-delimited-table';
//Envoi du fichier sur la sortie standard
2012-04-13 08:13:22 +00:00
if ( file_exists($path.$file) ) {
2012-04-12 09:46:16 +00:00
header('Content-Transfer-Encoding: none');
header('Content-type: ' . $content_type.'');
2012-04-13 08:13:22 +00:00
header('Content-Length: ' . filesize($path.$file));
header('Content-MD5: ' . base64_encode(md5_file($path.$file)));
header('Content-Disposition: filename="' . basename($path.$file) . '"');
2012-04-12 09:46:16 +00:00
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression', '0');
2012-04-13 08:13:22 +00:00
echo file_get_contents($path.$file);
2012-04-12 09:46:16 +00:00
} else {
echo 'Impossible de charger le fichier.';
2012-04-12 09:25:37 +00:00
}
}
2012-05-02 19:06:36 +00:00
2012-02-15 12:46:40 +00:00
}