odea/application/controllers/EnrichissementController.php

343 lines
11 KiB
PHP
Raw Normal View History

2012-02-15 13:46:40 +01: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 21:06:36 +02:00
2012-02-15 13:46:40 +01:00
/**
* Enter description here ...
*/
public function indexAction(){}
2012-05-02 21:06:36 +02:00
2012-02-15 13:46:40 +01:00
public function fileformAction()
{
2012-08-22 15:02:58 +02:00
$this->view->headScript()->appendFile('/libs/form/jquery.form.js', 'text/javascript');
2012-02-15 13:46:40 +01:00
$this->view->headScript()->appendFile('/themes/default/scripts/jqueryprogressbar.js', 'text/javascript');
2012-05-02 21:06:36 +02:00
$this->view->assign('filesize', ini_get('upload_max_filesize'));
2012-02-15 13:46:40 +01:00
}
2012-05-02 21:06:36 +02:00
2012-02-15 13:46:40 +01:00
public function fileuploadAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
2012-05-02 21:06:36 +02:00
2012-12-06 17:51:14 +01:00
$c = Zend_Registrey::get('config');
$path = realpath($c->profil->path->data).'/clients';
2012-02-15 13:46:40 +01:00
if(!file_exists($path)) mkdir($path);
2012-05-02 21:06:36 +02:00
2012-02-15 13:46:40 +01: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 21:06:36 +02:00
2012-02-15 13:46:40 +01:00
$extValide = array('csv');
$extension = strrchr($n,'.');
$extension = substr($extension,1);
//@todo : vérifier l'extension du fichier
2012-05-02 21:06:36 +02:00
2012-02-15 13:46:40 +01:00
if (move_uploaded_file($tmp_name, $path.'/'.$name.'.'.$extension)){
echo "Uploadé !";
} else {
echo "Erreur : ".$_FILES['fichier']['error'];
}
2012-05-02 21:06:36 +02:00
}
2012-02-15 13:46:40 +01:00
}
2012-05-02 21:06:36 +02:00
2012-02-15 13:46:40 +01:00
/**
* Etat de progression de l'upload du fichier
*/
public function fileprogressAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
2012-05-02 21:06:36 +02:00
$request = $this->getRequest();
2012-02-15 13:46:40 +01:00
$key = $request->getParam('key', '');
if ( !empty($key) ) {
2012-02-15 13:46:40 +01: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 21:06:36 +02:00
2012-02-21 10:38:04 +01:00
/**
* Enregistrement de la commande pour l'ennrichissement
*/
public function commandeAction()
{
$this->_helper->layout()->disableLayout();
2012-05-02 21:06:36 +02:00
2012-02-22 10:30:25 +01:00
$request = $this->getRequest();
2012-05-02 21:06:36 +02:00
$idCriteres = $request->getParam('id', null);
$idProfil = $request->getParam('profil', null);
2012-06-01 11:51:25 +02:00
$ref = $request->getParam('ref');
//Si forfait check si le montant restant est suffisant
//Identifiant comptage
$comptage = new Application_Model_CiblageComptages();
$sql = $comptage->select()->where('idDefinition = ?', $idCriteres);
$result = $comptage->fetchRow($sql);
2012-04-13 09:33:23 +02:00
$idComptage = $result['id'];
2012-05-02 21:06:36 +02:00
2012-02-22 10:30:25 +01:00
//Récupération des critères du ciblage
$criteresM = new Application_Model_CiblageCriteres();
2012-03-20 09:24:41 +01:00
$criteresRow = $criteresM->find($idCriteres);
$criteres = $criteresRow->current();
2012-05-02 21:06:36 +02:00
2012-02-22 10:30:25 +01:00
$structure = json_decode($criteres->criteres, true);
2012-05-02 21:06:36 +02:00
//Informations utilisateur
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
//Récupération des SIRET
2012-02-22 10:30:25 +01:00
require_once 'Scores/Ciblage.php';
$ciblage = new Ciblage($structure, $user->globalRNCS);
2012-03-15 16:44:10 +01:00
$infosExtraction = $ciblage->execute(true);
2012-03-20 09:24:41 +01:00
2012-05-15 13:38:51 +02:00
/*
* @todo :
* Vérifier le nombre d'éléments avec le nombre de ligne
*
*
*/
//Attention calcul uniteInsee réelle
2012-02-22 10:30:25 +01:00
//car si donnée insee alors toutes les lignes doivent être comptés en unité insee
2012-03-20 09:24:41 +01:00
$data = array(
'idComptage' => $idComptage,
2012-06-01 11:42:24 +02:00
'reference' => $ref,
'idCriteres' => $criteres->id,
'idProfil' => $idProfil,
2012-03-20 09:24:41 +01:00
'identifiants' => json_encode($infosExtraction),
2012-02-22 10:30:25 +01:00
'idProfil' => $idProfil,
'fichier' => '',
2012-03-20 09:24:41 +01:00
'nbLigneTotales' => count($infosExtraction),
'nbLigneTraites' => 0,
2012-05-15 13:38:51 +02:00
'uniteInsee' => $result['uniteInsee'],
2012-02-22 10:30:25 +01:00
'error' => '',
2012-05-28 11:14:22 +02:00
'dateAdded' => date('YmdHis'),
2012-02-22 10:30:25 +01:00
);
$identifiantsM = new Application_Model_CiblageEnrichissementIdentifiants();
$idIdentifiant = $identifiantsM->insert($data);
2012-06-01 11:42:24 +02:00
//@todo : ajouter la valeur (prix) du fichier au compteur global afin de décompter du forfait
2012-06-01 11:42:24 +02:00
$this->view->assign('ref', $ref);
2012-02-21 10:38:04 +01:00
}
2012-05-02 21:06:36 +02:00
2012-02-22 12:38:57 +01:00
/**
* Demande de référence pour l'enrichissement
*/
public function referenceAction()
{
2013-05-27 14:53:46 +02:00
//Constantes
$resultatMax = 50000;
$redevanceInsee = 3.295/100;
$redevanceInseeMax = 52734; //Seuil de facturation 52 734 euros
$this->_helper->layout()->disableLayout();
2012-02-22 13:56:54 +01:00
$request = $this->getRequest();
2012-05-02 21:06:36 +02:00
$selectprofil = $request->getParam('profil', 'default');
2012-02-24 11:42:06 +01:00
//Récupération du profil de l'utilisateur
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
$priceLine = $user->priceLine;
//Other profil for this login ?
$profilsM = new Application_Model_CiblageEnrichissementProfils();
2012-02-24 11:42:06 +01:00
$sql = $profilsM->select()
->from($profilsM, array('id', 'reference', 'criteres'))
->where('actif=1')
->where('login=?', $user->username);
$profils = $profilsM->fetchAll($sql)->toArray();
$profilId = 0;
foreach($profils as $item) {
2012-06-29 14:17:21 +02:00
if ($selectprofil == $item['id']) {
$profilId = $item['id'];
}
if ($item['reference']=='default') {
$defaultProfilId = $item['id'];
}
}
2012-06-29 14:17:21 +02:00
if ($profilId == 0) {
$profilId = $defaultProfilId;
}
2012-06-29 14:17:21 +02:00
Zend_Registry::get('firebug')->info($profilId);
if ( count($profils)>0 ) {
$this->view->assign('profil', $profilId);
2012-04-04 15:15:56 +02:00
$idCritere = $request->getParam('id', null);
2012-05-28 11:10:26 +02:00
$comptagesM = new Application_Model_CiblageComptages();
2012-02-24 11:42:06 +01:00
$sql = $comptagesM->select()
->where('idDefinition = ?', $idCritere)
->order('dateAjout DESC')
->limit(1);
$comptages = $comptagesM->fetchAll($sql);
2012-05-22 08:10:32 +02:00
$criteresM = new Application_Model_CiblageCriteres();
2012-05-28 11:10:26 +02:00
$criteres = $criteresM->find($idCritere)->current();
$criteresValue = json_decode($criteres->criteres, true);
2012-02-24 11:42:06 +01:00
if ( $comptages->count()>0 ) {
$item = $comptages[0];
$date = explode(' ', $item->dateAjout);
$date = $date[0];
2012-05-28 11:10:26 +02:00
if($date != '0000-00-00' && $date != date('Y-m-d')) {
require_once 'Scores/Ciblage.php';
$count = new Ciblage($criteresValue);
$item['resultat'] = $count->execute();
$item['uniteInsee'] = $count->calculRedevanceInsee();
}
2012-02-24 11:42:06 +01:00
$this->view->assign('resultat', $item['resultat']);
$this->view->assign('uniteInsee', $item['uniteInsee']);
2012-05-02 21:06:36 +02:00
//Calcul du prix
//@todo : Si le client a déjà payé la redevance INSEE
2012-05-02 21:06:36 +02:00
$prixInsee = $item['uniteInsee']*$redevanceInsee;
2012-02-24 11:42:06 +01:00
$infoInsee = '';
if ($profil->dataInsee){
2013-05-27 14:53:46 +02:00
$prixInsee = $item['resultat']*$redevanceInsee;
$infoInsee = "Votre profil inclus au moins une donnée Insee, la redevance sera applicable sur chaque ligne.";
2012-02-24 11:42:06 +01:00
}
2013-05-27 14:53:46 +02:00
$this->view->prixInsee = round($prixInsee, 2);
$this->view->infoInsee = $infoInsee;
$prix = round($item['resultat'] * $priceLine + $prixInsee, 2);
2013-05-27 14:53:46 +02:00
if ( $item['resultat'] > $resultatMax ) {
$this->view->assign('resultatOver', true);
}
//Forfait - Liste des commandes et calcul
2013-05-27 14:53:46 +02:00
elseif ( $user->forfait > 0 ) {
$dateBegin = $user->dateContrat;
$dateEnd = date('YmdHis', mktime(0,0,0,substr($user->dateContrat,5,2)+$user->periodContrat, substr($user->dateContrat,8,2), substr($user->dateContrat,0,4)));
$commandesM = new Application_Model_CiblageEnrichissementIdentifiants();
$sql = $commandesM->select()->setIntegrityCheck(false)
->from( array('commande' => 'enrichissement_identifiants'), array('SUM(nbLigneTotales) as total', 'SUM(uniteInsee) as insee'))
->join( array('critere' => 'ciblage_criteres'), 'critere.id = commande.idCriteres', array())
->where("dateAdded BETWEEN '".$dateBegin."' AND '".$dateEnd."'")
->where('idClient = ?', $user->idClient);
$result = $commandesM->fetchRow($sql);
if ($result) {
$total = $result['total'];
$insee = $result['insee'];
$conso = round($total * $priceLine + $insee * $redevanceInsee, 2);
}
2012-06-29 14:17:21 +02:00
$this->view->forfaitRemain = $user->forfait - $conso - $prix;
}
2013-05-27 14:53:46 +02:00
//Fichier illimité (avec nombre de lignes définies)
elseif ( $user->forfait == 0 && $user->limitFiles == 0 ) {
//Nombres de lignes dépassées
if ( $item['resultat'] > $user->limitLines ) {
$this->view->assign('resultatOver', 'lines');
}
2012-10-02 11:21:55 +02:00
}
2013-05-27 14:53:46 +02:00
//Fichier limité (avec nombre de lignes définies)
elseif ($user->forfait==0 && $user->limitFiles>0) {
//Nombres de lignes dépassées
if ( $user->limitLines != 0 && $item['resultat'] > $user->limitLines ) {
$this->view->assign('resultatOver', 'lines');
} else {
//Nombre de fichier dépassés
$dateBegin = $user->dateContrat;
$dateEnd = date('YmdHis', mktime(0,0,0,substr($user->dateContrat,5,2)+$user->periodContrat, substr($user->dateContrat,8,2), substr($user->dateContrat,0,4)));
$commandesM = new Application_Model_CiblageEnrichissementIdentifiants();
$sql = $commandesM->select()
->where("dateAdded BETWEEN '".$dateBegin."' AND '".$dateEnd."'")
->where('idClient = ?', $user->idClient);
$result = $commandesM->fetchAll($sql);
if ( $result->count()+1 > $user->limitFiles) {
$this->view->assign('resultatOver', 'files');
}
}
2012-10-02 11:21:55 +02:00
}
$this->view->prix = $prix;
$this->view->id = $item['id'];
2012-05-28 11:10:26 +02:00
$this->view->ref = $criteres->reference.'-'.uniqid();
2012-05-02 21:06:36 +02:00
}
}
2012-05-02 21:06:36 +02:00
2012-02-22 12:38:57 +01:00
}
2012-05-02 21:06:36 +02:00
2012-04-12 11:25:37 +02:00
public function downloadAction()
{
2012-04-13 16:49:08 +02:00
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
2012-05-02 21:06:36 +02:00
2012-12-06 17:51:14 +01:00
$auth = Zend_Auth::getInstance();
$identity = $auth->getIdentity();
2012-04-13 10:13:22 +02:00
$id = $this->getRequest()->getParam('id');
2012-12-06 17:51:14 +01:00
/*
* Do not download file that not own by the user
* List profil
*/
$profilM = new Application_Model_CiblageEnrichissementProfils();
2012-12-06 17:51:14 +01:00
$sql = $profilM->select()
->from($profilM, array('id'))
->where('login=?',$identity->username);
2012-12-06 17:51:14 +01:00
$profils = $profilM->fetchAll($sql);
if ( $profils->count()>0 ) {
$profilList = array();
foreach ( $profils->toArray() as $item ) {
$profilList[] = $item['id'];
}
}
2012-12-06 17:51:14 +01:00
/*
* List finish file
*/
$table = new Application_Model_CiblageEnrichissementIdentifiants();
2012-04-13 10:13:22 +02:00
$sql = $table->select()
2012-12-06 17:51:14 +01:00
->where('idProfil IN('.join(',', $profilList).')')
2012-04-13 10:13:22 +02:00
->where('id = ?', $id);
$result = $table->fetchRow($sql);
if(!empty($result)) {
$result = $result->toArray();
2012-06-19 17:33:05 +02:00
$date = substr($result['dateAdded'],0,4).substr($result['dateAdded'],5,2);
2012-12-06 17:51:14 +01:00
$c = Zend_Registry::get('config');
$path = $c->profil->path->data.'/'.$date.'/';
2012-04-13 10:13:22 +02:00
$file = $result['fichier'];
}
2012-04-12 11:46:16 +02:00
$content_type = 'application/csv-tab-delimited-table';
//Envoi du fichier sur la sortie standard
2012-04-13 10:13:22 +02:00
if ( file_exists($path.$file) ) {
2012-04-12 11:46:16 +02:00
header('Content-Transfer-Encoding: none');
header('Content-type: ' . $content_type.'');
2012-04-13 10:13:22 +02: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 11:46:16 +02:00
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression', '0');
2012-04-13 10:13:22 +02:00
echo file_get_contents($path.$file);
2012-04-12 11:46:16 +02:00
} else {
echo 'Impossible de charger le fichier.';
2012-04-12 11:25:37 +02:00
}
}
2012-05-02 21:06:36 +02:00
2012-02-15 13:46:40 +01:00
}