Subversion Repositories SmartDukaan

Rev

Rev 19854 | Rev 19864 | 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() {
19853 manas 28
		throw new NotFoundException(__('Unauthorized access'));
15537 manas 29
		if ($this->request->is('post')) {
30
			alert('Clicked');
31
		}
32
		$this->Callhistory->recursive = 0;
33
		$this->Paginator->settings = array('order' => array('id'=>'desc'));
34
 
35
		$this->set('callhistories', $this->Paginator->paginate());
36
	}
37
 
38
/**
39
 * admin_view method
40
 *
41
 * @throws NotFoundException
42
 * @param string $id
43
 * @return void
44
 */
45
	public function admin_view($id = null) {
46
		if (!$this->Callhistory->exists($id)) {
47
			throw new NotFoundException(__('Invalid callhistory'));
48
		}
49
		$options = array('conditions' => array('Callhistory.' . $this->Callhistory->primaryKey => $id));
50
		$this->set('callhistory', $this->Callhistory->find('first', $options));
51
	}
52
 
53
/**
54
 * admin_add method
55
 *
56
 * @return void
57
 */
58
	public function admin_add() {
59
		if ($this->request->is('post')) {
60
			$this->Callhistory->create();
61
			if ($this->Callhistory->save($this->request->data)) {
62
				$this->Session->setFlash(__('The callhistory has been saved.'));
63
				return $this->redirect(array('action' => 'index'));
64
			} else {
65
				$this->Session->setFlash(__('The callhistory could not be saved. Please, try again.'));
66
			}
67
		}
68
		$retailers = $this->Callhistory->Retailer->find('list');
69
		$agents = $this->Callhistory->Agent->find('list');
70
		$this->set(compact('retailers', 'agents'));
71
	}
72
 
73
/**
74
 * admin_edit method
75
 *
76
 * @throws NotFoundException
77
 * @param string $id
78
 * @return void
79
 */
80
	public function admin_edit($id = null) {
81
		if (!$this->Callhistory->exists($id)) {
82
			throw new NotFoundException(__('Invalid callhistory'));
83
		}
84
		if ($this->request->is(array('post', 'put'))) {
85
			if ($this->Callhistory->save($this->request->data)) {
86
				$this->Session->setFlash(__('The callhistory has been saved.'));
87
				return $this->redirect(array('action' => 'index'));
88
			} else {
89
				$this->Session->setFlash(__('The callhistory could not be saved. Please, try again.'));
90
			}
91
		} else {
92
			$options = array('conditions' => array('Callhistory.' . $this->Callhistory->primaryKey => $id));
93
			$this->request->data = $this->Callhistory->find('first', $options);
94
		}
95
		$retailers = $this->Callhistory->Retailer->find('list');
96
		$agents = $this->Callhistory->Agent->find('list');
97
		$this->set(compact('retailers', 'agents'));
98
	}
99
 
100
/**
101
 * admin_delete method
102
 *
103
 * @throws NotFoundException
104
 * @param string $id
105
 * @return void
106
 */
107
	public function admin_delete($id = null) {
108
		$this->Callhistory->id = $id;
109
		if (!$this->Callhistory->exists()) {
110
			throw new NotFoundException(__('Invalid callhistory'));
111
		}
112
		$this->request->onlyAllow('post', 'delete');
113
		if ($this->Callhistory->delete()) {
114
			$this->Session->setFlash(__('The callhistory has been deleted.'));
115
		} else {
116
			$this->Session->setFlash(__('The callhistory could not be deleted. Please, try again.'));
117
		}
118
		return $this->redirect(array('action' => 'index'));
119
	}
120
	public function admin_search(){
121
		$type=$this->request->query('type');
122
		$date_from = $this->request->query('date_from');
123
		$date_to = $this->request->query('date_to');
124
		$searchTerm = $this->request->query('search');
125
		if(empty($searchTerm)){
126
			if(!empty($date_from) && !empty($date_to)){
127
				if(strtotime($date_from) > strtotime($date_to)){
128
					$userActions = array();
129
					$resul=$userActions;
130
					echo "Wrong date selected";
131
				}else{
132
					$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";
133
					$resul=$this->Callhistory->query($sqlQuery);	
134
				}
135
			}else{
136
				$userActions = array();
137
				$resul=$userActions;
138
			}
139
 
140
		}else if($type=='agent_name'){
141
			if(!empty($date_from) && !empty($date_to)){
142
				if(strtotime($date_from) > strtotime($date_to)){
143
					$userActions = array();
144
					$resul=$userActions;
145
					echo "Wrong date selected";
146
				}else{
147
					$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";
148
					$resul=$this->Callhistory->query($sqlQuery);	
149
				}
150
			}else{
151
				$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";
152
				$resul=$this->Callhistory->query($sqlQuery);	
153
			}			
154
		}else if($type=='agent_id'){
155
			if(!empty($date_from) && !empty($date_to)){
156
				if(strtotime($date_from) > strtotime($date_to)){
157
					$userActions = array();
158
					$resul=$userActions;
159
					echo "Wrong date selected";
160
				}else{
161
					$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";
162
					$resul=$this->Callhistory->query($sqlQuery);	
163
				}
164
			}else{
165
				$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";
166
				$resul=$this->Callhistory->query($sqlQuery);	
167
			}			
168
		}else if($type=='agent_role'){
169
			if(!empty($date_from) && !empty($date_to)){
170
				if(strtotime($date_from) > strtotime($date_to)){
171
					$userActions = array();
172
					$resul=$userActions;
173
					echo "Wrong date selected";
174
				}else{
175
					$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";
176
					$resul=$this->Callhistory->query($sqlQuery);	
177
				}
178
			}else{
179
				$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";
180
				$resul=$this->Callhistory->query($sqlQuery);	
181
			}		
182
		}	
183
		$this->Callhistory->recursive = 0;
184
 
185
		$this->set('result', $resul);
186
	}
187
 
188
 
189
	public function admin_activations() {
190
		$date_from = $this->request->query('date_from');
191
		$date_to = $this->request->query('date_to');
192
		if(!empty($date_to)||!empty($date_from)){
19853 manas 193
			$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";
194
 
19854 manas 195
			$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";
15537 manas 196
		} else{
19853 manas 197
			$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";
19854 manas 198
			$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";
15537 manas 199
		}
200
		$this->loadModel('User');		
201
		$activations = $this->User->query($sql);
202
		foreach ($activations as $key => $value) {
203
			if(isset($value['users']['utm_campaign'])){
204
				$val=$value['users']['utm_campaign'];
205
				$sql = "SELECT name from agents where id = (select agent_id from retailerlinks where code=upper('$val'));";
206
				$this->loadModel('Agent');	
207
				$agentname = $this->Agent->query($sql);
208
				array_push($activations[$key], $agentname[0]['agents']['name']);
209
			}else{
210
				$val=$value['users']['referrer'];
211
				$sql1 = "SELECT name from agents where id = (select agent_id from retailerlinks where code=upper('$val'));";
212
				$this->loadModel('Agent');	
213
				$agentname = $this->Agent->query($sql1);
214
				array_push($activations[$key], $agentname[0]['agents']['name']);
215
			}
216
		}
217
		$agentActivations = $this->User->query($otherSql);
218
		$this->set('agentActivations',$agentActivations);
219
		$this->set(compact('activations','date_to','date_from'));
220
	}
19863 naman 221
 
222
	public function admin_callhistory(){
223
		$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() group by c.agent_id ";
224
		$res = $this->Callhistory->query($cquery);
225
		$this->set(compact('res'));
226
	}
15537 manas 227
}