ReportController.php 8.61 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;
use App\lib\phpexcel\PHPExcel;

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.reports.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;

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

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

			if(Input::has("dllogxls"))$this->downloadReportInExcel($filename, $reporthead, $reportArr);

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

			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->user_id != 0) $reportArr[$crmcallObj->client]['connects']++;
			if(strstr($crmcallObj->status, "NOANSWER"))$reportArr[$crmcallObj->client]['noanswer']++;
			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]['sittone']++;
		}

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

			$finalArr[$key]['connect_per']	= round(($report['connects'] / $report['dials']) * 100, 2);
			$finalArr[$key]['noanswer_per']	= round(($report['noanswer'] / $report['dials']) * 100, 2);
			$finalArr[$key]['busy_per']	= round(($report['busy'] / $report['dials']) * 100, 2);
			$finalArr[$key]['abandoned_per']	= round(($report['abandoned'] / $report['dials']) * 100, 2);
			$finalArr[$key]['sittone_per']	= round(($report['sittone'] / $report['dials']) * 100, 2);
		}

		$data["reportArr"] = $finalArr;

		return $data;
	}

	public function downloadReportInExcel($filename, $reporthead, $reportarray)
	{
		$inputFileType = "Excel5";
		$objReader = PHPExcel_IOFactory::createReader($inputFileType);
		$objPHPExcel = $objReader->load("assets/extras/blank.xls");
		$baseRow = 2;


		$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]]);
				}
				$row++;
		}

		header('Content-Type: application/vnd.ms-excel');
		header("Content-Disposition: attachment;filename=\"$filename.xls\"");
		header('Cache-Control: max-age=0');

		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $inputFileType);
		$objWriter->save('php://output');
	}
}