ReportController.php 10.1 KB
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Auth;
use Input;
use Response;
use Config;
use App\Http\Requests;
use App\Models\CRMCall;
use App\Models\CRMCallArchive;
use App\Models\Cutoff;
use App\Jobs\KHRMSLib;
use App\Models\Sipid;
use App\Models\Dialline;
use App\Models\User;
use App\Models\UserLog;
use DB;
use Log;
use Session;

class ReportController extends Controller
{
    public function __construct()
	{
		$this->middleware('auth');
		$this->middleware('module_access');
	}

	public function show($id)
	{
		$data			= array();
		$wakka			= new KHRMSLib();
		$dashboarduser	= Auth::user();
		$timeoffset		= $dashboarduser->timezone; //-330; //$dashboarduser->timezone;
		$timeoffset		= $timeoffset*60;

		$data['user']			= $dashboarduser;

		if($id=="reports")
		{			
			return view("layout.module.reports.reports", $data);
		}	

		$logtime    =(isset($_GET['logtime']))  ? $_GET['logtime']  :9;
		$logtimeto  =(isset($_GET['logtimeto']))? $_GET['logtimeto']:20;

		$logdate    =(isset($_GET['logdate']))  ? strtotime($_GET['logdate']." ".$logtime.":00:00")
                                        : strtotime(date("Y-m-d")." 09:00:00");
		$logdateto  =(isset($_GET['logdateto']))? strtotime($_GET['logdateto']." ".$logtimeto.":00:00")
                                        : strtotime(date("Y-m-d")." 20:00:00");
        $campaign   =(isset($_GET['campaign']))  ? $_GET['campaign']  : "Select";

        $oclientlst		= $wakka->clientsReadAccess();
        sort($oclientlst); 
        $oclientlst[0]	= 'Select';

        $data['wakka']			= $wakka;
        $data['dashboarduser']	= $dashboarduser;
        $data['timeoffset']		= $timeoffset;
        $data['logtime']		= $logtime;
        $data['logtimeto']		= $logtimeto;
        $data['logdate']		= $logdate;
        $data['logdateto']		= $logdateto;
        $data['campaign']		= $campaign;
        $data['oclientlst']		= $oclientlst;

		if($id=="liveusers")
		{
			 /*$campStr 	= "";
			 $ratio		= 0;
			 $totalCall	= 0;
			 $dialedCall = 0;
			 $waitCall	= 0;
			 $inCall		= 0;
			 $availChnl	= 0;
			 $freeUsr	= 0;
			 $usrIdArr	= array();
			 $tabHeadStr	= "";
			 $tabBodyStr	= "";

			 foreach($oclientlst as $c)
			 {
			 	$s="";
			 	if($c==$campaign)$s='selected';
			 	$campStr .= "<option value='$c' $s>$c</option>";
			 }

			 if ($campaign != "Select") {

			 $mastersdata=$wakka->getCompanyMaster($campaign);
			 if(!empty($mastersdata["autodialercampaign"]))$ratio=$mastersdata["autodialercampaign"];

			 $userArr		= User::where('presence', '=', "1")->where('usertype', '=', 'User')->where('sel_campaign', '=', $campaign)->select('id','username','fullname', 'sel_campaign','current_dialmode')->get();
			 $diallineArr	= Dialline::where('server', '=', env('app_ip'))->where("enabled","=","1")->get();

			 $totalChnl	= $diallineArr->count();

			 foreach ($diallineArr as $dialline) {
			 	if($dialline->regexstr== $campaign){

			 	if($dialline->status!= 'Free')$totalCall++;
			 	if($dialline->status!= 'Free' && $dialline->status!= 'Auto')$dialedCall++;
			 	if($dialline->status== 'Auto' && $dialline->conf== '' && $dialline->src_channel!= '' && $dialline->channel!= '')$waitCall++;
			 	if($dialline->status!= 'Free' && $dialline->conf!= '')$inCall++;
			 	}				
			 }

			 $availChnl = $totalChnl - $totalCall;

			 $tabHeadStr .= "<tr>
			 <th>User</th>
			 <th>Campaign</th>
			 <th>Mode</th>
			 <th>Status</th>
			 </tr>";

			 foreach ($userArr as $usr) {
			 	$statusStr	= "<span class='btn btn-xs btn-warning'><strong>Free</strong></span>";

			 	$sipIdArr = Sipid::where('server', '=', env('app_ip'))->where('user', "=", $usr->id)->where('status', '=', 1)->select('id', 'status', 'ready', 'patched','prepare_call')->first();
			 	if($sipIdArr->user!=$usr->id) continue;
			 	if($sipIdArr->patched==0) $freeUsr++;

			 	if($sipIdArr->ready==1)$statusStr = "<span class='btn btn-xs btn-success'><strong>Available</strong></span>";
			 	if($sipIdArr->patched==1)$statusStr = "<span class='btn btn-xs btn-info'><strong>InCall</strong></span>";

			 	$wrapUp = DB::table('crmcalls')->where('user_id', '=', $usr->id)->where('state', '=', 'Hangup')->orderBy('id', 'desc')->select('id', 'userstatus')->first();
			 	if($wrapUp->userstatus=="")$statusStr = "<span class='btn btn-xs btn-info'><strong>WrapUp</strong></span>";

			 	$tabBodyStr .= "<tr><td>".$usr->fullname."</td>";
			 	$tabBodyStr .= "<td>".$usr->sel_campaign."</td>";
			 	$tabBodyStr .= "<td>".$usr->current_dialmode."</td>";
			 	$tabBodyStr .= "<td>".$statusStr."</td></tr>";
			 }

			 $data['ratio']		= $ratio;
			 $data['totalUsr']	= $userArr->count();
			 $data['totalChnl']	= $totalChnl;
			 $data['availChnl']	= $availChnl;
			 $data['dialedCall']	= $dialedCall;
			 $data['waitCall']	= $waitCall;
			 $data['inCall']		= $inCall;
			 $data['freeUsr']	= $freeUsr;
			 $data['tabHeadStr']	= $tabHeadStr;
			 $data['tabBodyStr']	= $tabBodyStr;

			 }

			 $data['campStr']	= $campStr;*/
			
			return view("layout.module.dialer.liveusers", $data);
		}

		if($id=="calllog")
		{
			return view("layout.module.reports.calllog",array());
		}

		if($id=="agenttime")
		{
			return view("layout.module.reports.agenttime",array());
		}

		if($id=="agenttimeAverage")
		{
			return view("layout.module.reports.agenttimeAverage",array());
		}

		if($id=="campreport")
		{
			return view("layout.module.reports.campreport",array());
		}

		if($id=="statusreport")
		{
			return view("layout.module.reports.statusreport",array());
		}

		if($id=="campaignwise")
		{
			$filename = $id;
			$dllogxls = Input::get('dllogxls');

			$reporthead = ["Campaign", "Dials", "Connects", "Contacts", "Callbacks", "Sales", "No Answer", "Busy", "Sit Tones", "Abandoned", "Connect %", "No Answer %", "Busy %", "Sit Tones %", "Abandoned %"];

			$reportArr = $this->getCampaignWisePredictiveDetails($data);

			$data["dllogxls"]	= $dllogxls;
			$data["filename"]	= $filename;
			$data["reporthead"]	= $reporthead;

			$newReportArr = array_map(function($val,$key){
				return ['campaign' => $key] + $val;
			}, $reportArr['reportArr'],array_keys($reportArr['reportArr']));

			$data["reportArr"]	= $newReportArr;

			return view("layout.module.reports.campaignwise", $data);
		}
	}

	public function getCampaignWisePredictiveDetails($basicArr)
	{
		$data		= array();
		$finalArr	= array();
		$reportArr	= array();
		$typeArr	= array('Auto', 'AutoCall');
		$timeoffset	= $basicArr["timeoffset"];
		$logdate	= $basicArr["logdate"];
		$logdateto	= $basicArr["logdateto"];


		$crmcallObjs = CRMCall::whereIn('type', $typeArr)->where('created_at','>=',date("Y-m-d H:i:s",$logdate+$timeoffset))->where('created_at','<=',date("Y-m-d H:i:s",$logdateto+$timeoffset))->get();

		foreach ($crmcallObjs as $key => $crmcallObj) {
			$reportArr[$crmcallObj->client]['dials']++;

			if($crmcallObj->status == "ANSWER") $reportArr[$crmcallObj->client]['connects']++;

			if($crmcallObj->user_id != 0)$reportArr[$crmcallObj->client]['contacts']++;
			if(stristr($crmcallObj->usersubstatus, "follow"))$reportArr[$crmcallObj->client]['callbacks']++;

			if(strstr($crmcallObj->status, "NOANSWER"))$reportArr[$crmcallObj->client]['no_answer']++;
			if(stristr($crmcallObj->status, "busy"))$reportArr[$crmcallObj->client]['busy']++;
			if($crmcallObj->status == "ANSWER" && $crmcallObj->user_id == 0)$reportArr[$crmcallObj->client]['abandoned']++;
			if($crmcallObj->status != "ANSWER" && $crmcallObj->status != "NOANSWER" && stristr($crmcallObj->status, "busy") == "")$reportArr[$crmcallObj->client]['sit_tones']++;
		}

		foreach ($reportArr as $key => $report) {
			$finalArr[$key] = $report;

			if(!isset($finalArr[$key]['dials']))	$finalArr[$key]['dials']=0;
			if(!isset($finalArr[$key]['connects']))	$finalArr[$key]['connects']=0;
			if(!isset($finalArr[$key]['contacts']))	$finalArr[$key]['contacts']=0;
			if(!isset($finalArr[$key]['callbacks']))	$finalArr[$key]['callbacks']=0;
			if(!isset($finalArr[$key]['sales']))	$finalArr[$key]['sales']=0;
			if(!isset($finalArr[$key]['no_answer']))	$finalArr[$key]['no_answer']=0;
			if(!isset($finalArr[$key]['busy']))	$finalArr[$key]['busy']=0;
			if(!isset($finalArr[$key]['sit_tones']))	$finalArr[$key]['sit_tones']=0;
			if(!isset($finalArr[$key]['abandoned']))	$finalArr[$key]['abandoned']=0;

			if($report['dials']>0)
			{
				$finalArr[$key]['connects_per']	= round(($report['connects'] / $report['dials']) * 100, 2);
				$finalArr[$key]['no_answer_per']	= round(($report['no_answer'] / $report['dials']) * 100, 2);
				$finalArr[$key]['busy_per']	= round(($report['busy'] / $report['dials']) * 100, 2);
				$finalArr[$key]['sit_tones_per']	= round(($report['sit_tones'] / $report['dials']) * 100, 2);
			}
			else
			{
				$finalArr[$key]['connects_per']		= 0;
				$finalArr[$key]['no_answer_per']	= 0;
				$finalArr[$key]['busy_per']			= 0;
				$finalArr[$key]['sit_tones_per']	= 0;
			}

			if($report['connects']>0)
			{
				$finalArr[$key]['abandoned_per']	= round(($report['abandoned'] / $report['connects']) * 100, 2);
			}
			else
			{
				$finalArr[$key]['abandoned_per']	= 0;
			}
		}

		$data["reportArr"] = $finalArr;

		return $data;
	}

	//TODO: Need To make common Download Function
	public function downloadReportInExcel($data)
	{
		$headerexcel = [];
		$filename 	= $data["filename"];
		$filename	= $filename.".xlsx";

		$reporthead = $data["reporthead"];
		$reportArr 	= $data["reportArr"];

		foreach($reporthead as $cachekey)
		{
			if($cachekey!='')
			{   
				$headerexcel[$cachekey] = 'string';
			}
		}
	
		header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
		header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		header('Content-Transfer-Encoding: binary');
		header('Cache-Control: must-revalidate');
		header('Pragma: public');   

		$writer = new XLSXWriter();
		$styles1 = array( 'font'=>'Arial','font-size'=>10,'font-style'=>'bold', 'fill'=>'#eee', 'halign'=>'center', 'border'=>'left,right,top,bottom');
		$writer->writeSheetHeader('Sheet1', $headerexcel,$styles1,['freeze_rows'=>1, 'freeze_columns'=>1]);

		if($reportArr){

			foreach($reportArr as $row)
			{	
				$writer->writeSheetRow('Sheet1', $row, $row_options = ['height'=>15]);
			}
		}
		//dd($writer);
		$writer->writeToStdOut();
	}
}