Subversion Repositories SmartDukaan

Rev

Rev 20056 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
App::uses('AppController', 'Controller');
/**
 * Callhistories Controller
 *
 * @property Callhistory $Callhistory
 * @property PaginatorComponent $Paginator
 */
class CallhistoriesController extends AppController {

/**
 * Components
 *
 * @var array
 */
        public $components = array('Paginator');
                    public function beforeFilter() {
                        parent::beforeFilter();
                        $this->Auth->allow('admin_activations','admin_index');
                    }

/**
 * admin_index method
 *
 * @return void
 */
        public function admin_index($agent_id = null) {
//              throw new NotFoundException(__('Unauthorized access'));
                if ($this->request->is('post')) {
                        alert('Clicked');
                }
                $cur_date = date('Y-m-d', time());
                $newdate = date('Y-m-d', strtotime(' -1 day'));
                if(date('D',time()) == 'Sun'){
                        $newdate = date('Y-m-d', strtotime(' -2 day'));
                }elseif(date('D',strtotime(' -1 day')) == 'Sun'){
                        $newdate = date('Y-m-d', strtotime(' -2 day'));
                } 
                $this->Callhistory->recursive = 0;
                if($agent_id == null){
                        $this->Paginator->settings = array('order' => array('id'=>'desc'),'conditions' => array('Callhistory.call_time >'=>$newdate,'Callhistory.agent_id >' => 2),'limit' => 100);
                }else{
                        $this->Paginator->settings = array('order' => array('id'=>'desc'),'conditions' => array('Callhistory.call_time >'=>$newdate,'Callhistory.agent_id ' => $agent_id),'limit' => 100);
                }
                

                $this->set('callhistories', $this->Paginator->paginate());
        }

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_view($id = null) {
                if (!$this->Callhistory->exists($id)) {
                        throw new NotFoundException(__('Invalid callhistory'));
                }
                $options = array('conditions' => array('Callhistory.' . $this->Callhistory->primaryKey => $id));
                $this->set('callhistory', $this->Callhistory->find('first', $options));
        }

/**
 * admin_add method
 *
 * @return void
 */
        public function admin_add() {
                if ($this->request->is('post')) {
                        $this->Callhistory->create();
                        if ($this->Callhistory->save($this->request->data)) {
                                $this->Session->setFlash(__('The callhistory has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The callhistory could not be saved. Please, try again.'));
                        }
                }
                $retailers = $this->Callhistory->Retailer->find('list');
                $agents = $this->Callhistory->Agent->find('list');
                $this->set(compact('retailers', 'agents'));
        }

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_edit($id = null) {
                if (!$this->Callhistory->exists($id)) {
                        throw new NotFoundException(__('Invalid callhistory'));
                }
                if ($this->request->is(array('post', 'put'))) {
                        if ($this->Callhistory->save($this->request->data)) {
                                $this->Session->setFlash(__('The callhistory has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The callhistory could not be saved. Please, try again.'));
                        }
                } else {
                        $options = array('conditions' => array('Callhistory.' . $this->Callhistory->primaryKey => $id));
                        $this->request->data = $this->Callhistory->find('first', $options);
                }
                $retailers = $this->Callhistory->Retailer->find('list');
                $agents = $this->Callhistory->Agent->find('list');
                $this->set(compact('retailers', 'agents'));
        }

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_delete($id = null) {
                $this->Callhistory->id = $id;
                if (!$this->Callhistory->exists()) {
                        throw new NotFoundException(__('Invalid callhistory'));
                }
                $this->request->onlyAllow('post', 'delete');
                if ($this->Callhistory->delete()) {
                        $this->Session->setFlash(__('The callhistory has been deleted.'));
                } else {
                        $this->Session->setFlash(__('The callhistory could not be deleted. Please, try again.'));
                }
                return $this->redirect(array('action' => 'index'));
        }
        public function admin_search(){
                $type=$this->request->query('type');
                $date_from = $this->request->query('date_from');
                $date_to = $this->request->query('date_to');
                $searchTerm = $this->request->query('search');
                if(empty($searchTerm)){
                        if(!empty($date_from) && !empty($date_to)){
                                if(strtotime($date_from) > strtotime($date_to)){
                                        $userActions = array();
                                        $resul=$userActions;
                                        echo "Wrong date selected";
                                }else{
                                        $sqlQuery = "SELECT c.id,c.retailer_id,c.agent_id,c.mobile_number,c.call_type,c.call_time,c.duration_sec,c.sms_verified,c.call_disposition,c.disposition_description,c.created, a.name FROM callhistory c join agents a on a.id=c.agent_id where date(call_time) between '$date_from' and '$date_to' order by id desc";
                                        $resul=$this->Callhistory->query($sqlQuery);    
                                }
                        }else{
                                $userActions = array();
                                $resul=$userActions;
                        }

                }else if($type=='agent_name'){
                        if(!empty($date_from) && !empty($date_to)){
                                if(strtotime($date_from) > strtotime($date_to)){
                                        $userActions = array();
                                        $resul=$userActions;
                                        echo "Wrong date selected";
                                }else{
                                        $sqlQuery = "SELECT c.id,c.retailer_id,c.agent_id,c.mobile_number,c.call_type,c.call_time,c.duration_sec,c.sms_verified,c.call_disposition,c.disposition_description,c.created, a.name FROM callhistory c join agents a on a.id=c.agent_id where date(call_time) between '$date_from' and '$date_to' and a.name like lower('%$searchTerm%')order by id desc";
                                        $resul=$this->Callhistory->query($sqlQuery);    
                                }
                        }else{
                                $sqlQuery = "SELECT c.id,c.retailer_id,c.agent_id,c.mobile_number,c.call_type,c.call_time,c.duration_sec,c.sms_verified,c.call_disposition,c.disposition_description,c.created, a.name FROM callhistory c join agents a on a.id=c.agent_id where a.name like lower('%$searchTerm%')order by id desc";
                                $resul=$this->Callhistory->query($sqlQuery);    
                        }                       
                }else if($type=='agent_id'){
                        if(!empty($date_from) && !empty($date_to)){
                                if(strtotime($date_from) > strtotime($date_to)){
                                        $userActions = array();
                                        $resul=$userActions;
                                        echo "Wrong date selected";
                                }else{
                                        $sqlQuery = "SELECT c.id,c.retailer_id,c.agent_id,c.mobile_number,c.call_type,c.call_time,c.duration_sec,c.sms_verified,c.call_disposition,c.disposition_description,c.created, a.name FROM callhistory c join agents a on a.id=c.agent_id where date(call_time) between '$date_from' and '$date_to' and c.agent_id = '$searchTerm' order by id desc";
                                        $resul=$this->Callhistory->query($sqlQuery);    
                                }
                        }else{
                                $sqlQuery = "SELECT c.id,c.retailer_id,c.agent_id,c.mobile_number,c.call_type,c.call_time,c.duration_sec,c.sms_verified,c.call_disposition,c.disposition_description,c.created, a.name FROM callhistory c join agents a on a.id=c.agent_id where c.agent_id ='$searchTerm' order by id desc";
                                $resul=$this->Callhistory->query($sqlQuery);    
                        }                       
                }else if($type=='agent_role'){
                        if(!empty($date_from) && !empty($date_to)){
                                if(strtotime($date_from) > strtotime($date_to)){
                                        $userActions = array();
                                        $resul=$userActions;
                                        echo "Wrong date selected";
                                }else{
                                        $sqlQuery = "SELECT c.id,c.retailer_id,c.agent_id,c.mobile_number,c.call_type,c.call_time,c.duration_sec,c.sms_verified,c.call_disposition,c.disposition_description,c.created, a.name FROM callhistory c join agents a on a.id=c.agent_id where date(call_time) between '$date_from' and '$date_to' and c.call_type = '$searchTerm' order by id desc";
                                        $resul=$this->Callhistory->query($sqlQuery);    
                                }
                        }else{
                                $sqlQuery = "SELECT c.id,c.retailer_id,c.agent_id,c.mobile_number,c.call_type,c.call_time,c.duration_sec,c.sms_verified,c.call_disposition,c.disposition_description,c.created, a.name FROM callhistory c join agents a on a.id=c.agent_id where c.call_type = '$searchTerm' order by id desc";
                                $resul=$this->Callhistory->query($sqlQuery);    
                        }               
                }       
                $this->Callhistory->recursive = 0;
                
                $this->set('result', $resul);
        }


        public function admin_activations() {
                $date_from = $this->request->query('date_from');
                $date_to = $this->request->query('date_to');
                if(!empty($date_to)||!empty($date_from)){
                        $otherSql="SELECT a.name, COUNT( r.id ) AS count FROM  `retailerlinks` r LEFT JOIN agents a ON r.agent_id = a.id join users u on r.user_id=u.id WHERE DATE( r.activated ) BETWEEN '$date_from' AND '$date_to' and r.user_id is not null and date(u.activation_time)=BETWEEN '$date_from' AND '$date_to' GROUP BY a.id";
                        
                        $sql = "SELECT DATE( created ) AS date, utm_campaign,referrer , COUNT( id ) AS count FROM users WHERE DATE(activation_time)  BETWEEN '$date_from' AND '$date_to' AND  (utm_campaign IS NOT NULL OR referrer IS NOT NULL) GROUP BY DATE( created) ,utm_campaign,referrer";
                } else{
                        $otherSql="SELECT a.name, COUNT( r.id ) AS count FROM  `retailerlinks` r LEFT JOIN agents a ON r.agent_id = a.id join users u on r.user_id=u.id WHERE DATE( r.activated ) = CURDATE( ) and r.user_id is not null and date(u.activation_time)=curdate() GROUP BY a.id";
                        $sql = "SELECT DATE( created ) AS date, utm_campaign,referrer , COUNT( id ) AS count FROM users WHERE  DATE(activation_time) = CURDATE() AND  (utm_campaign IS NOT NULL OR referrer IS NOT NULL) GROUP BY DATE( created) ,utm_campaign,referrer";
                }
                $this->loadModel('User');               
                $activations = $this->User->query($sql);
                foreach ($activations as $key => $value) {
                        if(isset($value['users']['utm_campaign'])){
                                $val=$value['users']['utm_campaign'];
                                $sql = "SELECT name from agents where id = (select agent_id from retailerlinks where code=upper('$val'));";
                                $this->loadModel('Agent');      
                                $agentname = $this->Agent->query($sql);
                                array_push($activations[$key], $agentname[0]['agents']['name']);
                        }else{
                                $val=$value['users']['referrer'];
                                $sql1 = "SELECT name from agents where id = (select agent_id from retailerlinks where code=upper('$val'));";
                                $this->loadModel('Agent');      
                                $agentname = $this->Agent->query($sql1);
                                array_push($activations[$key], $agentname[0]['agents']['name']);
                        }
                }
                $agentActivations = $this->User->query($otherSql);
                $this->set('agentActivations',$agentActivations);
                $this->set(compact('activations','date_to','date_from'));
        }
        
        public function admin_callhistory(){
                $cquery = "select a.name ,count(c.id) as total from agents a join callhistory c on c.agent_id = a.id where c.call_time > CURDATE() and c.agent_id> 2 group by c.agent_id ";
                $res = $this->Callhistory->query($cquery);
                $this->set(compact('res'));
        }
}