odea/application/controllers/EnrichissementController.php
2012-10-02 09:21:55 +00:00

290 lines
9.0 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('/libs/form/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()
{
$this->_helper->layout()->disableLayout();
$request = $this->getRequest();
$idCriteres = $request->getParam('id', null);
$idProfil = $request->getParam('profil', null);
$ref = $request->getParam('ref');
//Si forfait check si le montant restant est suffisant
//Identifiant comptage
$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, $user->globalRNCS);
$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' => $ref,
'idCriteres' => $criteres->id,
'idProfil' => $idProfil,
'identifiants' => json_encode($infosExtraction),
'idProfil' => $idProfil,
'fichier' => '',
'nbLigneTotales' => count($infosExtraction),
'nbLigneTraites' => 0,
'uniteInsee' => $result['uniteInsee'],
'error' => '',
'dateAdded' => date('YmdHis'),
);
$identifiantsM = new Application_Model_EnrichissementIdentifiants();
$idIdentifiant = $identifiantsM->insert($data);
//@todo : ajouter la valeur (prix) du fichier au compteur global afin de décompter du forfait
$this->view->assign('ref', $ref);
}
/**
* Demande de référence pour l'enrichissement
*/
public function referenceAction()
{
$this->_helper->layout()->disableLayout();
$request = $this->getRequest();
$selectprofil = $request->getParam('profil', 'default');
//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_EnrichissementProfils();
$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) {
if ($selectprofil == $item['id']) {
$profilId = $item['id'];
}
if ($item['reference']=='default') {
$defaultProfilId = $item['id'];
}
}
if ($profilId == 0) {
$profilId = $defaultProfilId;
}
Zend_Registry::get('firebug')->info($profilId);
if ( count($profils)>0 ) {
$this->view->assign('profil', $profilId);
$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);
$criteresM = new Application_Model_Criteres();
$criteres = $criteresM->find($idCritere)->current();
$criteresValue = json_decode($criteres->criteres, true);
if ( $comptages->count()>0 ) {
$item = $comptages[0];
$date = explode(' ', $item->dateAjout);
$date = $date[0];
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();
}
$this->view->assign('resultat', $item['resultat']);
$this->view->assign('uniteInsee', $item['uniteInsee']);
//Calcul du prix
//@todo : Put it somewhere we can modify it
$redevanceInsee = 3.295/100; //Seuil de facturation 52 734 euros
//@todo : Si le client a déjà payé la redevance INSEE
$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.";
}
$this->view->prixInsee = round($prixInsee, 2);
$this->view->infoInsee = $infoInsee;
$prix = round($item['resultat'] * $priceLine + $prixInsee, 2);
//Forfait - Liste des commandes et calcul
if ($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_EnrichissementIdentifiants();
$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);
}
$this->view->forfaitRemain = $user->forfait - $conso - $prix;
}
//Limite lignes totales
elseif ($user->forfait==0 && $user->limitFiles>0) {
//@todo : create the rule
}
//Illimité
elseif ($user->forfait==0 && $user->limitFiles==0) {
}
$this->view->prix = $prix;
$this->view->id = $item['id'];
$this->view->ref = $criteres->reference.'-'.uniqid();
}
}
}
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 = substr($result['dateAdded'],0,4).substr($result['dateAdded'],5,2);
$config = Zend_Registry::get('configuration');
$path = $config->path->data.'/'.$date.'/';
$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.';
}
}
}