Subversion Repositories SmartDukaan

Rev

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