agentreport.blade.php 10.2 KB
<?php 
use App\Models\Sipid;
use App\Models\UserLog;
use App\Models\CRMCall;
use App\Models\User;
use App\Jobs\KHRMSLib;

$wakka = new KHRMSLib();
$dashboarduser=Auth::user();
$uidlist=array();$onlineusers=array();

if(isset($_GET['logdate']))$logdate=strtotime($_GET['logdate']." 00:00:00");
else $logdate=strtotime(date('Y-m-d')." 00:00:00");

$alist=UserLog::where('created_at','>=',date("Y-m-d H:i:s",$logdate))->where('created_at','<=',date("Y-m-d H:i:s",$logdate+24*60*60))->where('durationsec','>',0);

if(!$dashboarduser->moduleACL("Admin",false,false,true))
{
	$uidlist[]=$dashboarduser->id;
	$allusers=User::where(function ($query)  use($dashboarduser) {
			$query->where('supervisor','=',$dashboarduser->username)
			->orWhere('lteam2','=',$dashboarduser->username)
			->orWhere('lteam','=',$dashboarduser->username);
	})->get();
	
	foreach($allusers as $tuser)
	{
		$uidlist[]=$tuser->id;
		if($tuser->presence==1)$onlineusers[]=$tuser;
	}

	$alist=$alist->whereIn('user_id',$uidlist);
}
else
{
	$allusers=User::all();
	foreach($allusers as $tuser)
	{
		$uidlist[]=$tuser->id;
		if($tuser->presence==1)$onlineusers[]=$tuser;
	}
}

//$sipids=Sipid::whereIn("user",$uidlist)->where("status","=","1")->get();

$alist=$alist->get();

$userarr=array();$i=1;
$reporthead=array("#","UserId","UserName","In","Out","InOut","Sessions","Duration");
$reportarray=array();
foreach($alist as $aline)
{
	if(!isset($userarr[$aline->user_id]))$userarr[$aline->user_id]=User::find($aline->user_id);
	$tuser=$userarr[$aline->user_id];

	if(!isset($reportarray[$aline->user_id]))$reportarray[$aline->user_id]=array("#"=>$i++,"UserId"=>$aline->user_id,"UserName"=>$tuser->username,"In"=>"","Out"=>"","InOut"=>0,"Sessions"=>0,"Duration"=>0);
	
	if($reportarray[$aline->user_id]["In"]=="")$reportarray[$aline->user_id]["In"]=date("Y-m-d H:i:s",strtotime($aline->created_at)-($dashboarduser->timezone*60));
	$reportarray[$aline->user_id]["Out"]=date("Y-m-d H:i:s",strtotime($aline->updated_at)-($dashboarduser->timezone*60));
	$reportarray[$aline->user_id]["InOut"]=strtotime($reportarray[$aline->user_id]["Out"])-strtotime($reportarray[$aline->user_id]["In"]);
	
	$data=json_decode($aline->data,true);
	foreach($data as $sipid=>$sdata)
	{
		$reportarray[$aline->user_id]["Sessions"]++;
		$reportarray[$aline->user_id]["Duration"]+=$aline->durationsec;
		if(isset($sdata['states']))
		{
			$previous="Login";
			$prets=strtotime($aline->startdate." ".$aline->starttime);
			foreach($sdata['states'] as $ts=>$states)
			{
				$fts=$ts/1000;
				if(!in_array($previous,$reporthead))$reporthead[]=$previous;
				$reportarray[$aline->user_id][$previous]+=round($fts-$prets,2);
				$previous=$states[0]."-".$states[1];$prets=$fts;
			}
			if(!in_array($previous,$reporthead))$reporthead[]=$previous;
			$reportarray[$aline->user_id][$previous]+=round((strtotime($aline->enddate." ".$aline->endtime))-$prets,2);
		}
		else
		{
			$reportarray[$aline->user_id]["Login"]+=$aline->durationsec;
		}
	}
}

if(Input::has("dllogxls"))
{
	include_once(app_path().'/lib/newphpexcel/Classes/PHPExcel.php');
    $objPHPExcel = new PHPExcel();
	$baseRow = 3;


	$highestColumn = sizeof($reporthead);
	for ($head = 0; $head < $highestColumn; $head++){
		$colstr=PHPExcel_Cell::stringFromColumnIndex($head);
		$objPHPExcel->getActiveSheet()->setCellValue($colstr."1", $reporthead[$head]);
	}

	foreach($reportarray as $uid=>$uarr)
	{
			$row = $baseRow++;
			$col = 0;
			
			for ($head = 0; $head < $highestColumn; $head++){
				$colstr=PHPExcel_Cell::stringFromColumnIndex($head);
				$objPHPExcel->getActiveSheet()->setCellValue($colstr.$row, $uarr[$reporthead[$head]]);
			}
	}

	$objPHPExcel->getActiveSheet()->setTitle('AgentLog');
    $objPHPExcel->setActiveSheetIndex(0);
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="AgentLog.xls"');
    header('Cache-Control: max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save('php://output');
    exit;
	
	return ;
}


$highestColumn = sizeof($reporthead);
$outhead="<tr>";$outstr="";
for ($head = 0; $head < $highestColumn; $head++){
	$outhead.="<td>".$reporthead[$head]."</td>";
}
$outhead.="</tr>";
foreach($reportarray as $uid=>$uarr)
{
	$outstr.="<tr>";
	for ($head = 0; $head < $highestColumn; $head++){
		$outstr.="<td>".$uarr[$reporthead[$head]]."</td>";
	}
	$outstr.="</tr>";
}




//charts
$cat1=array();
$cat1arr=array();
foreach($reportarray as $uid=>$uarr)
{
	$cat1[]="'$uarr[UserName]'";
	
	for ($head = 8; $head < $highestColumn; $head++){
		if(!isset($cat1arr[$reporthead[$head]]))$cat1arr[$reporthead[$head]]=array();
		
		$cat1arr[$reporthead[$head]][$uarr["#"]]=round($uarr[$reporthead[$head]]/60,1);
		if(empty($cat1arr[$reporthead[$head]][$uarr["#"]]))$cat1arr[$reporthead[$head]][$uarr["#"]]=0;
	}
}

$cat1arrstr="";
foreach($cat1arr as $name=>$data)
{
	$cat1arrstr.="{ name: '$name',\ndata : [";
	$cat1arrstr.=implode(",",$data);
	$cat1arrstr.="] },\n";
}


$cat2=array();
foreach($reportarray as $uid=>$uarr)
{
	for ($head = 8; $head < $highestColumn; $head++){
		$cat2["'".$reporthead[$head]."'"]+=round($uarr[$reporthead[$head]]/60,1);
	}
}
?>


            <div class=innerAll>


<style>#logtable.td{vertical-align:top;}#logtable.tr{height:28px;overflow-y:hidden;}</style>
<script>
$(document).ready(function()
{
    if(!$('#dialoglog').hasClass('ui-dialog-content'))
    {
  $('#dialoglog').dialog({
    autoOpen: false,
    width: '70%',
    buttons: {
      "Ok": function() { 
	$(this).dialog("close"); 
      }, 
      "Cancel": function() { 
	$(this).dialog("close"); 
      } 
    }
  });
    }

  waitKeyUpRun("filter",function() { filter2(document.getElementById('filter'), 'logtable') },"2000");
  $('#modfrom').datepicker({dateFormat: 'yy-mm-dd',inline: true,changeYear: true,selectOtherMonths: true,yearRange: '<?php echo (date('Y')-70).":".(date('Y')+5); ?>'});
});
</script>
<div style="float:left;width:50%">

Search <input id=filter name="filter" type="text"> 
Date : <input size=10 id='modfrom' name='modfrom' type='text' value='<?php echo date("Y-m-d",$logdate); ?>' onchange='agentLogReloadFun("");' /> 

</div>
<div style="float:left;width:50%">
<button class='pull-right btn btn-sm btn-default' onclick='dlAgentlogXls();return false;' title='Download' style='margin-top:-8px'><i class='fa fa-download'></i> Download</button>

</div>
<div style="clear:both"></div>
<hr>

<div class=col-md-12>


<div id=userlogschart1 style='min-height:300px;width:100%'></div>
<script>
$(function () {
    $('#userlogschart1').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: ''
        },
        xAxis: {
                    labels:{
                        enabled:false//default is true
                    },
            categories: [<?php echo implode(",",$cat1); ?>]
        },
        yAxis: {
            min: 0,
            title: {
                text: ''
            },
            stackLabels: {
                enabled: false,
//                 style: {
//                     fontWeight: '',
//                     color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
//                 }
            }
        },
//         legend: {
//             align: 'right',
//             x: -30,
//             verticalAlign: 'top',
//             y: 25,
//             floating: true,
//             backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white',
//             borderColor: '#CCC',
//             borderWidth: 1,
//             shadow: false
//         },
        tooltip: {
            headerFormat: '<b>{point.x}</b><br/>',
            pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}'
        },
        plotOptions: {
            column: {
                stacking: 'normal',
                dataLabels: {
                    enabled: false,
//                     color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
//                     style: {
//                         textShadow: '0 0 3px black'
//                     }
                }
            }
        },
        series: [<?php echo $cat1arrstr; ?>]
    });
});




function agentLogReloadFun(sortby)
{
	var sortstr='';if(sortby!="")sortstr="&sort="+sortby
	
	doAjax('admin/agentreport?logdate='+$("#modfrom").val()+sortstr,'','rightmainreportdiv','ajax_admin_reports','singlethis','GET');
}
function dlAgentlogXls()
{
	window.open('admin/agentreport?dllogxls=1&logdate='+$("#modfrom").val());return false;
}
</script>

</div>

<div style='clear:both'></div>

<div class=col-md-12>


<div id=userlogschart2 style='min-height:300px;width:100%'></div>
<script>
$(function () {
    $('#userlogschart2').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: ''
        },
        subtitle: {
            text: ''
        },
        xAxis: {
            categories: [<?php echo implode(",",array_keys($cat2)); ?>],
            crosshair: true
        },
        yAxis: {
            min: 0,
            title: {
                text: ''
            }
        },
        tooltip: {
            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
            pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                '<td style="padding:0"><b>{point.y:.1f}</b></td></tr>',
            footerFormat: '</table>',
            shared: true,
            useHTML: true
        },
        plotOptions: {
            column: {
                pointPadding: 0.2,
                borderWidth: 0
            }
        },
        legend : {
            floating: true,
            align: 'right',
            x: -30,
            verticalAlign: 'top',
        },
        series: [{
            name: 'Mins',
            data: [<?php echo implode(",",array_values($cat2)); ?>]

        },]
    });
});

</script>

</div>



<div style='clear:both'></div>

<div style='overflow:auto'>
<table id=logtable class='footable table table-striped table-bordered table-white table-primary footable-loaded' style='font-size:12px'>
<thead>
	<?php echo $outhead; ?>
</thead>
<?php echo $outstr; ?>
</table>
</div>

<div id=dialoglog></div>

                
                
                
                
            </div>