ReportController.php 5.37 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;

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

		$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")
		{
			return view("layout.module.reports.campaignwise",array());
		}
	}
}