odea/application/controllers/GestionController.php
2013-03-19 10:14:40 +00:00

521 lines
15 KiB
PHP

<?php
class GestionController extends Zend_Controller_Action
{
protected $dbMetier;
public function preDispatch()
{
}
public function init()
{
$this->view->headLink()->appendStylesheet('/themes/default/styles/dashboard.css', 'all');
$this->view->headScript()->appendFile('/themes/default/scripts/dashboard.js', 'text/javascript');
//Get Database
$c = Zend_Registry::get('config');
try {
$this->dbMetier = Zend_Db::factory($c->profil->db->jo);
} catch ( Exception $e ) {
}
}
/**
* Display all links
*/
public function indexAction()
{
}
/**
* Display profils to get data
* Filter with login and idClient
*
*/
public function profilsAction()
{
$request = $this->getRequest();
$login = $request->getParam('login', '');
$idClient = $request->getParam('idClient', null);
$profilsM = new Application_Model_CiblageEnrichissementProfils();
$sql = $profilsM->select()
->from($profilsM, array('id', 'idClient', 'login', 'reference', 'tarifLigne', 'dateAjout', 'dateSuppr', 'actif'));
if ( !empty($login) ) {
$sql->where('login = ?', $login.'%');
}
if ( $idClient!=null ) {
$sql->where('idClient = ?', $idClient);
}
$profils = $profilsM->fetchAll($sql);
if ( count($profils)>0 ) {
$clientM = new Application_Model_Sdv1Clients();
$i = 0;
foreach ($profils as $profil) {
$infoClient = $clientM->find($profil->idClient);
$profils[$i]->nom = $infoClient->nom;
$i++;
}
}
$this->view->assign('profils', $profils);
}
/**
* Define fields for extraction to user
*/
public function profiladdAction()
{
$request = $this->getRequest();
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
//Sauvegarde du formulaire
if ( $request->isPost() )
{
$params = $request->getParams();
if ($params['reference']=='default') {
//Get default profil
$profilClientM = new Application_Model_CiblageCustomerParams();
$sql = $profilClientM->select()->where('idClient=?',$request->getParam('idClient'));
$profilClient = $profilClientM->fetchRow($sql);
if ( null !== $profilClient ) {
$params['criteres'] = json_decode($profilClient->criteres,true);
}
}
if (empty($params['criteres'])) {
$this->view->assign('message', "Erreur profil vide");
} else {
$data = array(
'idClient' => $params['idClient'],
'service' => $params['service'],
'login' => $params['login'],
'reference' => $params['reference'],
'criteres' => json_encode($params['criteres']),
'dataInsee' => 0,
'dateAjout' => date('Y-m-d H:i:s'),
'actif' => 1,
);
$profilM = new Application_Model_CiblageEnrichissementProfils();
if ( $profilM->insert($data) ) {
$this->view->assign('message', "Profil enregistré");
$this->view->assign('disableForm', true);
} else {
$this->view->assign('message', "Erreur lors de la sauvegarde");
}
}
}
//Affichage du formulaire
require_once 'Scores/Enrichissement.php';
$fieldsM = new Enrichissement();
$allFields = $fieldsM->getFields();
$this->view->assign('fields', $allFields);
}
/**
* Mark profil as deleted
*/
public function profildelAction()
{
}
/**
* List commands which must be done
*/
public function commandesAction()
{
$request = $this->getRequest();
$month = $request->getParam('month', date('m'));
$year = $request->getParam('year', date('Y'));
$this->view->assign('month', $month);
$this->view->assign('year', $year);
$commandesM = new Application_Model_CiblageEnrichissementIdentifiants();
$sql = $commandesM->select()->setIntegrityCheck(false)
->from('enrichissement_identifiants AS e', array(
'e.id',
'e.reference AS commandeReference',
'e.nbLigneTotales',
'e.uniteInsee',
"DATE_FORMAT(e.dateAdded, '%Y/%m/%d %H:%i:%s') AS dateAdded"
))
->join('ciblage_criteres', 'idCriteres = ciblage_criteres.id', array(
'ciblage_criteres.reference AS critereReference',
'ciblage_criteres.login',
))
->where('e.dateStart = ?', "0000-00-00 00:00:00")
->where('fichier = ""')
->where("e.dateAdded LIKE '".$year."-".$month."%'")
->order('e.dateAdded DESC');
$this->view->commandes = $commandesM->fetchAll($sql)->toArray();
}
/**
* List commands with a file
*/
public function enrichissementsAction()
{
$request = $this->getRequest();
$month = $request->getParam('month', date('m'));
$year = $request->getParam('year', date('Y'));
$this->view->assign('month', $month);
$this->view->assign('year', $year);
$commandesM = new Application_Model_CiblageEnrichissementIdentifiants();
$sql = $commandesM->select()->setIntegrityCheck(false)
->from('enrichissement_identifiants AS e', array(
'e.id',
'e.reference AS commandeReference',
'e.nbLigneTotales',
'e.uniteInsee',
"DATE_FORMAT(e.dateAdded, '%Y/%m/%d %H:%i:%s') AS dateAdded",
'e.fichier'
))
->join('ciblage_criteres', 'idCriteres = ciblage_criteres.id', array(
'ciblage_criteres.reference AS critereReference',
'ciblage_criteres.login',
))
->where('fichier != ""')
->where("e.dateAdded LIKE '".$year."-".$month."%'")
->order('e.dateAdded DESC');
$this->view->commandes = $commandesM->fetchAll($sql)->toArray();
}
/**
* Load enrichissement batch to complete data
*/
public function enrichitAction()
{
$request = $this->getRequest();
$id = $request->getParam('id');
exec('php '.APPLICATION_PATH.'/../batch/enrichissement.php --id '.$id.' &');
}
/**
* Extract only SIRETs from database
* @todo : To csv file
*/
public function extractAction()
{
$id = $this->getRequest()->getParam('id');
$table = new Application_Model_CiblageEnrichissementIdentifiants();
$sql = $table->select()->where('idComptage = ?', $id);
$result = $table->fetchRow($sql);
if(!empty($result)) {
$result = $result->toArray();
$sirets = json_decode($result['identifiants'], true);
$content = '';
foreach($sirets as $siret) {
$content.= $siret.",";
}
$this->view->assign('content', $content);
}else {
$this->view->assign('message', 'Aucune commande d\'enrichissement sur ce comptage');
}
}
/**
* List criteres and the last count
*/
public function ciblagesAction()
{
$request = $this->getRequest();
$month = $request->getParam('month', date('m'));
$year = $request->getParam('year', date('Y'));
$this->view->assign('month', $month);
$this->view->assign('year', $year);
$comptagesM = new Application_Model_CiblageComptages();
$sql = $comptagesM->select()->setIntegrityCheck(false)
->from('ciblage_comptages', array(
'ciblage_comptages.idDefinition',
'ciblage_comptages.resultat',
'ciblage_comptages.uniteInsee',
"DATE_FORMAT(ciblage_comptages.dateAjout, '%Y/%m/%d %H:%i:%s') as dateAjout"
))
->join('ciblage_criteres', 'ciblage_comptages.idDefinition = ciblage_criteres.id', array('ciblage_criteres.reference'))
->order('ciblage_comptages.dateAjout DESC')
->group('ciblage_criteres.id')
->where("ciblage_comptages.dateAjout LIKE '".$year."-".$month."%'")
->order('ciblage_comptages.dateAjout DESC');
$comptages = $comptagesM->fetchAll($sql)->toArray();
Zend_Registry::get('firebug')->info($comptages);
$this->view->comptages = $comptages;
}
/**
* Affichage des critères du ciblage
*/
public function ciblagecriteresAction()
{
$this->_helper->layout()->disableLayout();
$request = $this->getRequest();
$id = $request->getParam('id');
//Lecture des paramètres du ciblage
$criteresM = new Application_Model_CiblageCriteres();
$sql = $criteresM->select()
->where('id = ?', $id);
$criteres = $criteresM->fetchRow($sql)->toArray();
$decodeCriteres = json_decode($criteres['criteres'], true);
$fields = new Scores_Fields();
//Construction affichage des critères
foreach ( $decodeCriteres as $key => $item ) {
$inValue = $fields->getValueLabel($key, $item['in']);
$exValue = $fields->getValueLabel($key, $item['ex']);
//Add label to struct for display
$infosCriteres[] = array(
'label' => $fields->getLabel($key),
'in' => $inValue,
'ex' => $exValue,
);
}
$this->view->criteres = $infosCriteres;
}
/**
* List customers
*/
public function customerparamsAction()
{
//@todo : what's in database
$customersM = new Application_Model_CiblageCustomerParams();
$sql = $customersM->select()
->from($customersM,array('idClient', 'service' , 'dateContrat', 'periodContrat'))
->order('dateAdded DESC')
->group('idClient');
$customers = $customersM->fetchAll($sql)->toArray();
//List login
$customerlist = array();
$profilsM = new Application_Model_CiblageEnrichissementProfils();
$clientM = new Application_Model_Sdv1Clients();
foreach($customers as $item) {
$sql = $profilsM->select()
->from($profilsM, array('login'))
->where('idClient=?',$item['idClient']);
$result = $profilsM->fetchAll($sql)->toArray();
$logins = array();
if (count($result)>0) {
foreach($result as $login) {
$logins[] = $login['login'];
}
}
$item['logins'] = $logins;
$sql = $clientM->select()->where('id=?', $item['idClient']);
$result = $clientM->fetchRow($sql);
$item['nom'] = $result->nom;
$customerlist[] = $item;
}
$this->view->assign('customerlist', $customerlist);
}
/**
* List customer's params
*/
public function customerparamAction()
{
//Fields
require_once 'Scores/Enrichissement.php';
$fieldsM = new Enrichissement();
$allFields = $fieldsM->getFields();
$this->view->assign('fields', $allFields);
$request = $this->getRequest();
$idClient= $request->getParam('idClient', null);
$this->view->assign('idClient', $idClient);
if ($idClient !== null) {
$paramsM = new Application_Model_CiblageCustomerParams();
$sql = $paramsM->select()->where('idClient = ?', $idClient);
$params = $paramsM->fetchRow($sql);
$infos = null;
if ($params!==null){
$infos = $params->toArray();
$clientM = new Application_Model_Sdv1Clients();
$sql = $clientM->select()->where('id=?', $idClient);
$result = $clientM->fetchRow($sql);
$infos['nom'] = $result->nom;
$infos['criteres'] = json_decode($infos['criteres'], true);
}
$this->view->assign('infos',$infos);
$loginsM = new Application_Model_CiblageEnrichissementProfils();
$sql = $loginsM->select()->where('idClient = ?', $idClient);
$logins = $loginsM->fetchAll($sql)->toArray();
$this->view->assign('logins', $logins);
}
}
public function customerparamaddAction()
{
//Fields
require_once 'Scores/Enrichissement.php';
$fieldsM = new Enrichissement();
$allFields = $fieldsM->getFields();
$this->view->assign('fields', $allFields);
$request = $this->getRequest();
//Sauvegarde du formulaire
if ( $request->isPost() && in_array($request->getParam('submit'), array('Enregistrer','Modifier')) ) {
$params = $request->getParams();
//Vérifier le formulaire
$errForm = 0;
foreach ( $params as $key => $value ) {
if ($value=='' && $key!='service') {
$errForm++;
}
}
if (!$errForm) {
$checkValues = array(
'filterRNCS' => 0,
'licenceINSEE' => 0,
'immediatExtract' => 0,
);
foreach ($checkValues as $key => $value) {
if (!array_key_exists($key, $params)) {
$params[$key] = $value;
}
}
$data = array(
'idClient' => $params['idClient'],
'service' => $params['service'],
'filterRNCS' => $params['filterRNCS'],
'licenceINSEE' => $params['licenceINSEE'],
'immediatExtract' => $params['immediatExtract'],
'dateContrat' => $params['dateContrat'],
'periodContrat' => $params['periodContrat'],
'periodPaiement' => $params['periodPaiement'],
'priceLine' => $params['priceLine'],
'forfait' => $params['forfait'],
'limitLines' => $params['limitLines'],
'limitFiles' => $params['limitFiles'],
'criteres' => json_encode($params['criteres']),
'dateAdded' => date('Y-m-d H:i:s'),
);
if ($request->getParam('submit')=='Modifier') {
//Overwrite default profil for each login
try {
$profilM = new Application_Model_CiblageEnrichissementProfils();
$profilM->update(array(
'criteres' => json_encode($params['criteres'])
), "reference='default' AND idClient=".$params['idClient']);
} catch ( Zend_Db_Adapter_Exception $e) {
Zend_Registry::get('firebug')->info($e->getMessage());
}
//Set parameters
try {
$customerParamsM = new Application_Model_CiblageCustomerParams();
$customerParamsM->update($data, 'id='.$params['id']);
$this->view->assign('message', "Profil enregistré");
} catch ( Zend_Db_Adapter_Exception $e) {
Zend_Registry::get('firebug')->info($e->getMessage());
$this->view->assign('message', "Erreur lors de la sauvegarde");
}
} else {
//Set parameters
$customerParamsM = new Application_Model_CiblageCustomerParams();
if ( $customerParamsM->insert($data) ){
$this->view->assign('message', "Profil enregistré");
} else {
$this->view->assign('message', "Erreur lors de la sauvegarde");
}
}
} else {
$this->view->assign('message', "Erreur lors de la saisie");
}
} else {
$idClient = $request->getParam('idClient', null);
$clientM = new Application_Model_Sdv1Clients();
$sql = $clientM->select()->where('actif=?',1);
$selectClient = $clientM->fetchAll($sql);
$this->view->assign('selectClient',$selectClient);
if ($idClient !== null) {
$paramsM = new Application_Model_CiblageCustomerParams();
$sql = $paramsM->select()->where('idClient = ?', $idClient);
$params = $paramsM->fetchRow($sql);
if ( $params!==null ) {
foreach ( $params as $key => $value ) {
switch ( $key ) {
case 'criteres':
$this->view->assign($key, json_decode($value,true));
break;
case 'dateContrat':
$this->view->assign($key, substr($value,0,10));
break;
default:
$this->view->assign($key, $value);
break;
}
}
}
} else {
$this->view->assign('filterRNCS', 1);
$this->view->assign('licenceINSEE', 0);
$this->view->assign('immediatExtract', 0);
$this->view->assign('limitLines', 50000);
$this->view->assign('limitFiles', 0);
}
}
}
}