AgentTrackerController.php
22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;
use DateTime;
use Input;
use Response;
use App\Jobs\KHRMSLib;
use App\Models\Sipid;
use App\Models\Dialline;
use App\Models\UserLog;
use App\Models\User;
use DB;
use Log;
use Session;
use Config;
use App\Models\CRMCall;
use App\Models\CRMCallArchive;
use App\Models\Record;
use App\Models\Kqueue;
class AgentTrackerController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
if($id=="asmt"){
date_default_timezone_set('Asia/Kolkata');
$dashboarduser = Auth::user();
$nowts = time();
$talkarray = array();
$reportarray = array();
$sipidsfound = array();
$userarr = array();
$i = 1;
$selClient = Input::get("campaign");
$dialableLeads = 0;
if($selClient != "All")
{
$allusers = User::select('id','username','fullname')->where("status","=","Active")->where("usertype","!=","Admin")->where('data', 'like', '%'.$selClient.'%')->where("presence","=","1")->get();
$dialableLeads = DB::table('records')->where('client', '=', $selClient)->where('status', '=', 'New')->count();
}
else
{
$allusers = User::select('id','username','fullname')->where("status","=","Active")->where("usertype","!=","Admin")->orderBy('presence','desc')->where("presence","=","1")->get();
$dialableLeads = DB::table('records')->where('status', '=', 'New')->count();
}
foreach ($allusers as $users) {
$reportarray[$users->id]["id"] = $users->id;
$reportarray[$users->id]["username"] = $users->username;
$reportarray[$users->id]["fullname"] = $users->fullname;
$reportarray[$users->id]["login_status"]="Logout";
}
//$login_users = DB::table('sessions')->where('user_id','!=','Null')->groupBy('user_id')->get();
$trackarray=[];
foreach($allusers as $login_user){
$trackarray[] = $login_user->id;
}
$sipids=Sipid::whereIn("user",$trackarray)->where("status","=","1")->where('user', "!=", 1)->groupBy('user')->get();
foreach($sipids as $sipid)
{
$reportarray[$sipid->user]["login_status"]="Login";
$reportarray[$sipid->user]["callcount"]= DB::table('crmcalls')->where('user_id','=',$sipid->user)->where("updated_at", ">", date("Y-m-d"))->count();
}
$talkcalls=Dialline::where('user_id','!=','')->whereNotIn('status',['Free','Auto','AutoCall'])->where('channel','!=','')->orderBy('updated_at')->get();
foreach($talkcalls as $talkcall){
$currenttime = strtotime(date('Y-m-d H:i:s', time()-(330*60)));
$updatedate = $talkcall->updated_at->toDateTimeString();
$ringstarttime = strtotime($updatedate);
$reportarray[$talkcall->user_id]["tsduration"]=$this->secToDuration($currenttime - $ringstarttime);
$reportarray[$talkcall->user_id]["status"]="Ringing";
$reportarray[$talkcall->user_id]["exact_status"]="Ringing";
$reportarray[$talkcall->user_id]["img"]="talk";
$talkarray[] = $talkcall->user_id;
}
$newcalls=Dialline::whereNotIn('status',['Free','AutoCall'])->orderBy('updated_at')->get();
$acalls=Dialline::where('status','!=','Free')->where('conf','!=','')->orderBy('updated_at')->get();
$livecalls=Dialline::where('status','=','Auto')->where('conf','=','')->count();
foreach($newcalls as $newcall)
{
$tcall=CRMCall::where('dialline_id','=',$newcall->id)->orderBy('id','DESC')->first();
$un="";$uid="";$us="";
if($tcall->user_id>0)
{
$tuser=User::find($tcall->user_id);
$un=$tuser->username;
$uname=$tuser->fullname;
$uid=$this->userchatbox($tuser).$tuser->id;
$userlog=UserLog::where('user_id','=',$tuser->id)->orderBy("id","DESC")->first();
$stend=$userlog->getLastStatus();
$us="$stend[0]-$stend[1]";
$reportarray[$tcall->user_id]["ID"]=$uid;
$reportarray[$tcall->user_id]["User"]=$un;
$reportarray[$tcall->user_id]["Name"]=$uname;
}
if($tcall->state == 'DialEnd' || ($tcall->type == 'Auto' && $tcall->userstatus == ''))
{
$us = "Talk";
$currenttime = strtotime(date('Y-m-d H:i:s', time()-(330*60)));
$updatedate = $tcall->updated_at->toDateTimeString();
$ringstarttime = strtotime($updatedate);
$reportarray[$tcall->user_id]["tsduration"]=$this->secToDuration($currenttime - $ringstarttime);
$reportarray[$tcall->user_id]["status"]=$us;
$reportarray[$tcall->user_id]["exact_status"]=$us;
$reportarray[$tcall->user_id]["img"]="talk";
}
$reportarray[$tcall->user_id]["Campaign"]=$tcall->client;
$reportarray[$tcall->user_id]["Station"]=$tcall->sipid_id.$this->userSpyStr($tcall->sipid_id);
$reportarray[$tcall->user_id]["Status"]=$us;
$reportarray[$tcall->user_id]["CRMId"]=$tcall->crm_id;
$reportarray[$tcall->user_id]["Number"]=$tcall->number;
$reportarray[$tcall->user_id]["Type"]=$tcall->type;
$reportarray[$tcall->user_id]["State"]=$tcall->state;
$reportarray[$tcall->user_id]["Duration"]=$nowts-strtotime($tcall->created_at);
//$reportarray[$newcall->id]=$obj;
if(!empty($tcall->sipid_id))$sipidsfound[]=$tcall->sipid_id;
}
foreach($acalls as $acall)
{
$tcall=CRMCall::where('dialline_id','=',$acall->id)->orderBy('id','DESC')->first();
$tsipid=substr($acall->conf,4);
$tsip=Sipid::find($tsipid);
$clidata=json_decode($tsip->clients,true);
$tuser=User::find($tsip->user);
$userlog=UserLog::where('user_id','=',$tuser->id)->orderBy("id","DESC")->first();
$stend=$userlog->getLastStatus();
$reportarray[$tsip->user]["ID"]=$this->userchatbox($tuser).$tsip->user;
$reportarray[$tsip->user]["User"]=$tuser->username;
$reportarray[$tsip->user]["Name"]=$tuser->fullname;
$reportarray[$tsip->user]["Campaign"]=substr($clidata['r'],0,20);
$reportarray[$tsip->user]["Station"]=$tsip->id.$this->userSpyStr($tsip->id);
$reportarray[$tsip->user]["Status"]="$stend[0]-$stend[1]";
$reportarray[$tsip->user]["CRMId"]=$tcall->crm_id;
$reportarray[$tsip->user]["Number"]=$tcall->number;
$reportarray[$tsip->user]["Type"]=$tcall->type;
$reportarray[$tsip->user]["State"]=$tcall->state;
$reportarray[$tsip->user]["Duration"]=$nowts-strtotime($tcall->created_at);
$sipidsfound[]=$tsipid;
}
foreach($sipids as $sipid)
{
$clidata=json_decode($sipid->clients,true);
$tuser=User::find($sipid->user);
$userlog=UserLog::where('user_id','=',$tuser->id)->orderBy("id","DESC")->first();
$stend=$userlog->getLastStatus();
$reportarray[$sipid->user]["id"]=$tuser->id;
$reportarray[$sipid->user]["fullname"]=$tuser->fullname;
$reportarray[$sipid->user]["ID"]=$this->userchatbox($tuser).$sipid->user;
$reportarray[$sipid->user]["User"]=$tuser->username;
$reportarray[$sipid->user]["Name"]=$tuser->fullname;
$reportarray[$sipid->user]["Station"]=$sipid->id.$this->userSpyStr($sipid->id);
$reportarray[$sipid->user]["Status"]="$stend[0]-$stend[1]";
$reportarray[$sipid->user]["CRMId"]="";
$reportarray[$sipid->user]["Number"]="";
$reportarray[$sipid->user]["Type"]="";
$reportarray[$sipid->user]["State"]="";
$reportarray[$sipid->user]["Duration"]="";
}
foreach($sipids as $sipid){
if(!in_array($sipid->user,$talkarray)){
$userlogs = DB::table('userlogs')->where('user_id','=',$sipid->user)->orderBy('updated_at','DESC')->first();
$data=json_decode($userlogs->data,true);
$lastsip = end($data);
if(empty($lastsip['states']))$lastsip['states'][$lastsip['ts']]=array('Not-ready','Not-ready');
$currenttime = strtotime(date('Y-m-d H:i:s', time()));
$reportarray[$sipid->user]["tsduration"]=$this->secToDuration($currenttime - round((key(array_slice($lastsip['states'], -1, 1,true))/1000)));
$alllaststatus[$sipid->user] = end($lastsip['states']);
$reportarray[$sipid->user]["status"]=$alllaststatus[$sipid->user][0];
$reportarray[$sipid->user]["exact_status"]=$alllaststatus[$sipid->user][1];
$reportarray[$sipid->user]["img"]="idle";
if($reportarray[$sipid->user]["status"] == "Paused")
$reportarray[$sipid->user]["img"]="break";
if($reportarray[$sipid->user]["exact_status"] == "WRAPUP")
$reportarray[$sipid->user]["img"]="wrap";
}
}
$cntArr = array();
$loginCnt = 0;
$logoutCnt = 0;
foreach ($reportarray as $value) {
if(!empty($value['status']))
{
if(stristr($value['Status'], "Paused") || stristr($value['Status'], "Incoming") || stristr($value['Status'], "Predictive"))
$$value['exact_status'] = 0;
else
$$value['status'] = 0;
}
}
foreach ($reportarray as $key => $value) {
if(array_key_exists('fullname', $value)){
if(!empty($value['Status']))
{
if(stristr($value['Status'], "Paused") || stristr($value['Status'], "Incoming") || stristr($value['Status'], "Predictive"))
$cntArr[$value['exact_status']][$value['exact_status']] = ++$$value['exact_status'];
else
$cntArr[$value['status']][$value['status']] = ++$$value['status'];
}
if($value['login_status'] == "Login")
{
$loginCnt++;
}
if($value['login_status'] == "Logout")
{
$logoutCnt++;
}
}
}
ksort($cntArr);
function array_insert_before($key, array &$array, $new_key, $new_value) {
if (array_key_exists($key, $array)) {
$new = array();
foreach ($array as $k => $value) {
if ($k === $key) {
$new[$new_key] = $new_value;
}
$new[$k] = $value;
}
return $new;
}
return FALSE;
}
function array_insert_after($key, array &$array, $new_key, $new_value) {
if (array_key_exists($key, $array)) {
$new = array();
foreach ($array as $k => $value) {
$new[$k] = $value;
if ($k === $key) {
$new[$new_key] = $new_value;
}
}
return $new;
}
return FALSE;
}
$newreportarray = array();
foreach ($reportarray as $key => $value) {
if(array_key_exists('fullname', $value)){
if($value['login_status'] == "Logout")continue;
if(empty($newreportarray))
{
$newreportarray[$key] = $value;
}
else
{
foreach ($newreportarray as $key1 => $val) {
$exact_status = $value['exact_status'];
$exact_status1 = $val['exact_status'];
if($value['exact_status'] == "")
{
$exact_status = $value['status'];
}
if($val['exact_status'] == "")
{
$exact_status1 = $val['status'];
}
if(strcasecmp($exact_status, $exact_status1) < 0)
{
$appendarr = array();
$appendarr[$key] = $value;
$newreportarray = $appendarr + $newreportarray;
}
if(strcasecmp($exact_status, $exact_status1) == 0)
{
if(strtotime($value['tsduration']) >= strtotime($val['tsduration']))
{
$appendarr = array();
$appendarr[$key] = $value;
$newreportarray = array_insert_before($key1, $newreportarray, $key, $value);
}
else
{
$appendarr = array();
$appendarr[$key] = $value;
$newreportarray = array_insert_after($key1, $newreportarray, $key, $value);
}
}
if(strcasecmp($exact_status, $exact_status1) > 0)
{
$appendarr = array();
$appendarr[$key] = $value;
$newreportarray = $newreportarray + $appendarr;
}
}
}
}
}
foreach ($reportarray as $key => $value) {
if(array_key_exists('fullname', $value)){
if($value['login_status'] == "Logout")
{
array_push($newreportarray, $value);
}
}
}
$outstr="";
$divColor = "";
$selStr = "";
$s = "";
$wakka = new KHRMSLib();
$selStr .= "<option value='All'>All</option>";
$clients=explode(",",trim($wakka->GetBBBUserData("clientsownerlist")));
foreach($clients as $tclient){
if(trim($tclient)!="")
{
if(trim($tclient) == $selClient)$s="selected";
$selStr .="<option value='$tclient' $s>$tclient</option>";
$s = "";
}
}
$mastersdata=$wakka->getCompanyMaster($selClient);
$outstr.='<div class="row">
<label class="label-small_new" style="width:10%;float:left">Select Client: </label>
<select class="form_hrm_select" id="campSelAgent" style="width:10%;float:left">
'.$selStr.'
</select>
<script>
$("#campSelAgent").on("change",function(){
$client = $(this).val();
doAjax("agenttracker/asmt?campaign="+$client,"","agenttracker","ajax_dialer_reports","singlethis","GET");
});
</script>
<label class="label-small_new" style="width:10%;float:left">Dialable Leads => </label>
<b style="width:10%;float:left">'.$dialableLeads.'</b>
<label class="label-small_new" style="width:10%;float:left">Live Calls => </label>
<b style="width:10%;float:left">'.$livecalls.'</b>
<label class="label-small_new" style="width:10%;float:left">Dial Level => </label>';
if(!$mastersdata["autodialercampaign"])$mastersdata["autodialercampaign"]=0;
$outstr.='<b style="width:10%;float:left">'.$mastersdata["autodialercampaign"].'</b>
<br><br><br>
</div>
<div>
<span class="panel panel-info" style="padding:4px;">Total Logged In:<b style="padding:3px;">'.$loginCnt.'</b></span><span class="panel panel-info" style="padding:5px;background-color:#ff000087;color:#fff">Total Logged Out:<b style="padding:3px;">'.$logoutCnt.'</b></span>';
foreach ($cntArr as $key => $value) {
if($key=="Manual" || $key=="Progressive" || $key=="Incoming" || $key=="Predictive" || $key=="Not-ready" || $key=="DialNext")
$divColor="#5d9cec";//Blue Color
elseif($key=="Talk" || $key=="Ringing")$divColor="#80e580";//Green
elseif($key=="WRAPUP")$divColor="#efba5b";//Orange
else $divColor="#dd9ca7";//Pink
$outstr.='<span class="panel panel-info" style="margin-left:1px;padding:4px;background-color:'.$divColor.';color:#fff">'.$key.':<b style="padding:3px;">'.$value[$key].'</b></span>';
}
$outstr.='</div><div class="l-wrap" style="text-align:center">
<div class="three-col-grid" style="padding:5px;margin" >';
/*foreach ($newreportarray
as $reportarr => $value) {
if(array_key_exists('fullname', $value)){*/
//Changes for real time monitoring
$outstr.="<div class='container'>
<h2>Real Time Monitoring Report</h2>
<table class='table' border='1'>
<thead>
<tr>
<th>Agent Name</th>
<th>Status</th>
<th>Time</th>
<th>Calls</th>
<th>Station</th>
</tr>
</thead>";
foreach ($newreportarray
as $reportarr => $value) {
if(array_key_exists('fullname', $value)){
$outstr.="<tbody>
<tr>
<td>".$value['fullname']."</td>
<td>".$value["exact_status"]."</td>
<td>".$value["tsduration"]."</td>
<td>".$value["callcount"]."</td>
<td>".$value["Station"]."</td>
</tr>
</tbody>";
}
}
$outstr.= "</table></div>";
$outstr.='</div></div>';
return $outstr;
}
if($id=="mylogout"){
$user_id=Input::get('userid');
$sipids=Sipid::where("user","=",$user_id)->where("status","=","1")->get();
foreach($sipids as $tsip)
{
$newqueue=new Kqueue();
$newqueue->sipNotify($tsip,"adminCommand","user","logout","");
}
Dialline::where('user_id','=',$user_id)->where('status','!=','Free')->update(['status'=>'Free','conf'=>'','channel'=>'','server'=>'','updated_at'=>'0000-00-00 00:00:00']);
Sipid::where('user','=',$user_id)->where('status','!=','0')->update(['status'=>0,'user'=>0,'ready'=>0,'confup'=>0,'clients'=>'','server'=>'','updated_at'=>'0000-00-00 00:00:00']);
$serverarray=explode(",",Config::get("app.asterisk_slaves"));
foreach($serverarray as $server)
{
$sparts=explode(":",$server);
Sipid::where("user","=",$user_id)->where("id",">=",$sparts[1])->where("id","<=",$sparts[2])->update(['server' => $sparts[0],'updated_at'=>'0000-00-00 00:00:00']);
Dialline::where("user_id","=",$user_id)->where("id",">=",$sparts[3])->where("id","<=",$sparts[4])->update(['server' => $sparts[0],'updated_at'=>'0000-00-00 00:00:00']);
}
User::where("id","=",$user_id)->where('presence','>','0')->update(['presence'=>0]);
DB::table('sessions')->where('user_id', $user_id)->delete();
return Response::make("<script>simpleNotification('success','topRight','Agent Logout successfully!');</script>");
}
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
private function secToDuration($sec) {
return sprintf("%02d%s%02d%s%02d", floor($sec/3600), ':', ($sec/60)%60, ':', $sec%60);
}
private function userchatbox($tuser){
return "<a data-toggle='image-preview' data-title='".$tuser->dispname()."' data-content='<small></small>' data-image-preview='".$tuser->fetchphotothumb()."' width='100' href='#' class='innerAll half border-none' onclick='tchatbox(\"chatpop_".$tuser->id."\",\"".$tuser->dispname()."\");return false;' style='padding:0 !important'><img src='".$tuser->fetchphotothumb()."' alt='photo' width='22'></a> ";
// return $tuser;
}
private function userSpyStr($sipid){
if(!empty($sipid))return " <a href=# onclick='kDialerSpy(\"$sipid\",\"L\");return false;'>L</a> <a href=# onclick='kDialerSpy(\"$sipid\",\"B\");return false;'>B</a> <a href=# onclick='kDialerSpy(\"$sipid\",\"W\");return false;'>W</a>";
}
}