Subversion Repositories SmartDukaan

Rev

Rev 15537 | Rev 19853 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
15537 manas 1
<?php
2
App::uses('AppController', 'Controller');
3
/**
4
 * Callhistories Controller
5
 *
6
 * @property Callhistory $Callhistory
7
 * @property PaginatorComponent $Paginator
8
 */
9
class CallhistoriesController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
18841 manas 17
		    public function beforeFilter() {
18
		    	parent::beforeFilter();
19
		    	$this->Auth->allow('admin_activations');
20
		    }
15537 manas 21
 
22
/**
23
 * admin_index method
24
 *
25
 * @return void
26
 */
27
	public function admin_index() {
28
		if ($this->request->is('post')) {
29
			alert('Clicked');
30
		}
31
		$this->Callhistory->recursive = 0;
32
		$this->Paginator->settings = array('order' => array('id'=>'desc'));
33
 
34
		$this->set('callhistories', $this->Paginator->paginate());
35
	}
36
 
37
/**
38
 * admin_view method
39
 *
40
 * @throws NotFoundException
41
 * @param string $id
42
 * @return void
43
 */
44
	public function admin_view($id = null) {
45
		if (!$this->Callhistory->exists($id)) {
46
			throw new NotFoundException(__('Invalid callhistory'));
47
		}
48
		$options = array('conditions' => array('Callhistory.' . $this->Callhistory->primaryKey => $id));
49
		$this->set('callhistory', $this->Callhistory->find('first', $options));
50
	}
51
 
52
/**
53
 * admin_add method
54
 *
55
 * @return void
56
 */
57
	public function admin_add() {
58
		if ($this->request->is('post')) {
59
			$this->Callhistory->create();
60
			if ($this->Callhistory->save($this->request->data)) {
61
				$this->Session->setFlash(__('The callhistory has been saved.'));
62
				return $this->redirect(array('action' => 'index'));
63
			} else {
64
				$this->Session->setFlash(__('The callhistory could not be saved. Please, try again.'));
65
			}
66
		}
67
		$retailers = $this->Callhistory->Retailer->find('list');
68
		$agents = $this->Callhistory->Agent->find('list');
69
		$this->set(compact('retailers', 'agents'));
70
	}
71
 
72
/**
73
 * admin_edit method
74
 *
75
 * @throws NotFoundException
76
 * @param string $id
77
 * @return void
78
 */
79
	public function admin_edit($id = null) {
80
		if (!$this->Callhistory->exists($id)) {
81
			throw new NotFoundException(__('Invalid callhistory'));
82
		}
83
		if ($this->request->is(array('post', 'put'))) {
84
			if ($this->Callhistory->save($this->request->data)) {
85
				$this->Session->setFlash(__('The callhistory has been saved.'));
86
				return $this->redirect(array('action' => 'index'));
87
			} else {
88
				$this->Session->setFlash(__('The callhistory could not be saved. Please, try again.'));
89
			}
90
		} else {
91
			$options = array('conditions' => array('Callhistory.' . $this->Callhistory->primaryKey => $id));
92
			$this->request->data = $this->Callhistory->find('first', $options);
93
		}
94
		$retailers = $this->Callhistory->Retailer->find('list');
95
		$agents = $this->Callhistory->Agent->find('list');
96
		$this->set(compact('retailers', 'agents'));
97
	}
98
 
99
/**
100
 * admin_delete method
101
 *
102
 * @throws NotFoundException
103
 * @param string $id
104
 * @return void
105
 */
106
	public function admin_delete($id = null) {
107
		$this->Callhistory->id = $id;
108
		if (!$this->Callhistory->exists()) {
109
			throw new NotFoundException(__('Invalid callhistory'));
110
		}
111
		$this->request->onlyAllow('post', 'delete');
112
		if ($this->Callhistory->delete()) {
113
			$this->Session->setFlash(__('The callhistory has been deleted.'));
114
		} else {
115
			$this->Session->setFlash(__('The callhistory could not be deleted. Please, try again.'));
116
		}
117
		return $this->redirect(array('action' => 'index'));
118
	}
119
	public function admin_search(){
120
		$type=$this->request->query('type');
121
		$date_from = $this->request->query('date_from');
122
		$date_to = $this->request->query('date_to');
123
		$searchTerm = $this->request->query('search');
124
		if(empty($searchTerm)){
125
			if(!empty($date_from) && !empty($date_to)){
126
				if(strtotime($date_from) > strtotime($date_to)){
127
					$userActions = array();
128
					$resul=$userActions;
129
					echo "Wrong date selected";
130
				}else{
131
					$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";
132
					$resul=$this->Callhistory->query($sqlQuery);	
133
				}
134
			}else{
135
				$userActions = array();
136
				$resul=$userActions;
137
			}
138
 
139
		}else if($type=='agent_name'){
140
			if(!empty($date_from) && !empty($date_to)){
141
				if(strtotime($date_from) > strtotime($date_to)){
142
					$userActions = array();
143
					$resul=$userActions;
144
					echo "Wrong date selected";
145
				}else{
146
					$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";
147
					$resul=$this->Callhistory->query($sqlQuery);	
148
				}
149
			}else{
150
				$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";
151
				$resul=$this->Callhistory->query($sqlQuery);	
152
			}			
153
		}else if($type=='agent_id'){
154
			if(!empty($date_from) && !empty($date_to)){
155
				if(strtotime($date_from) > strtotime($date_to)){
156
					$userActions = array();
157
					$resul=$userActions;
158
					echo "Wrong date selected";
159
				}else{
160
					$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";
161
					$resul=$this->Callhistory->query($sqlQuery);	
162
				}
163
			}else{
164
				$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";
165
				$resul=$this->Callhistory->query($sqlQuery);	
166
			}			
167
		}else if($type=='agent_role'){
168
			if(!empty($date_from) && !empty($date_to)){
169
				if(strtotime($date_from) > strtotime($date_to)){
170
					$userActions = array();
171
					$resul=$userActions;
172
					echo "Wrong date selected";
173
				}else{
174
					$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";
175
					$resul=$this->Callhistory->query($sqlQuery);	
176
				}
177
			}else{
178
				$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";
179
				$resul=$this->Callhistory->query($sqlQuery);	
180
			}		
181
		}	
182
		$this->Callhistory->recursive = 0;
183
 
184
		$this->set('result', $resul);
185
	}
186
 
187
 
188
	public function admin_activations() {
189
		$date_from = $this->request->query('date_from');
190
		$date_to = $this->request->query('date_to');
191
		if(!empty($date_to)||!empty($date_from)){
192
			$otherSql="SELECT a.name, COUNT( r.id ) AS count FROM  `retailerlinks` r LEFT JOIN agents a ON r.agent_id = a.id WHERE DATE( r.activated ) BETWEEN '$date_from' AND '$date_to' GROUP BY a.id";
193
			$sql = "SELECT DATE( created ) AS date, utm_campaign,referrer , COUNT( id ) AS count FROM users WHERE DATE(created)  BETWEEN '$date_from' AND '$date_to' AND  (utm_campaign IS NOT NULL OR referrer IS NOT NULL) GROUP BY DATE( created) ,utm_campaign,referrer";
194
		} else{
195
			$otherSql="SELECT a.name, COUNT( r.id ) AS count FROM  `retailerlinks` r LEFT JOIN agents a ON r.agent_id = a.id WHERE DATE( r.activated ) = CURDATE( ) GROUP BY a.id";
196
			$sql = "SELECT DATE( created ) AS date, utm_campaign,referrer , COUNT( id ) AS count FROM users WHERE  DATE( created ) = CURDATE() AND  (utm_campaign IS NOT NULL OR referrer IS NOT NULL) GROUP BY DATE( created) ,utm_campaign,referrer";
197
		}
198
		$this->loadModel('User');		
199
		$activations = $this->User->query($sql);
200
		foreach ($activations as $key => $value) {
201
			if(isset($value['users']['utm_campaign'])){
202
				$val=$value['users']['utm_campaign'];
203
				$sql = "SELECT name from agents where id = (select agent_id from retailerlinks where code=upper('$val'));";
204
				$this->loadModel('Agent');	
205
				$agentname = $this->Agent->query($sql);
206
				array_push($activations[$key], $agentname[0]['agents']['name']);
207
			}else{
208
				$val=$value['users']['referrer'];
209
				$sql1 = "SELECT name from agents where id = (select agent_id from retailerlinks where code=upper('$val'));";
210
				$this->loadModel('Agent');	
211
				$agentname = $this->Agent->query($sql1);
212
				array_push($activations[$key], $agentname[0]['agents']['name']);
213
			}
214
		}
215
		$agentActivations = $this->User->query($otherSql);
216
		$this->set('agentActivations',$agentActivations);
217
		$this->set(compact('activations','date_to','date_from'));
218
	}
219
}