Subversion Repositories SmartDukaan

Rev

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