AutodialController.php 5.52 KB
<?php namespace App\Http\Controllers;

use Auth;
use Input;
use Response;
use App\Models\Group;
use App\Models\Master;
use App\Models\Record;
use App\Models\CRMCall;
use App\Models\CRMCallArchive;
use App\Models\CRM;
use App\Models\CRMCampaign;
use App\Models\CRMList;
use App\Jobs\KHRMSLib;
use App\Models\Sipid;
use App\Models\Dialline;
use App\Models\UserLog;
use App\Models\User;
use App\Models\Kqueue;
use App\Models\QCFeedback;
use DB;
use Log;
use Session;
$client='';

class AutodialController extends Controller {

	
	public function index()
	{

	}
	public function create()
	{

	}
	public function store()
	{
		

	}
	public function show($id)
	{
		if($id=="autodialmode")
		{
			$campaign=Input::get("client");
			$user_id = Auth::user()->id;

			$callsToDialled = $this->getCallsToBeDialled($campaign);
			if($callsToDialled>0)
			{
				$this->createCall($campaign, $callsToDialled, $user_id);
			}
		}
	}

	public function getCallsToBeDialled($campaign)
	{
		$wakka = new KHRMSLib();
		$mastersdata=$wakka->getCompanyMaster($campaign);

		$ratio 			=	$mastersdata["autodialercampaign"];
		$availableUsers	=	$this->getFreeUserCnt($campaign);
		$dialedCalls	=	$this->getDialedCallCount($campaign);

		return $availableUsers*$ratio-$dialedCalls;
	}
	
//To Get Number Of Free User which will be multiplied with Defined Ratio For Generating Number Of Calls
	public function getFreeUserCnt($campaign)
	{
		$count=0;
		$loggedInSips = Sipid::where('server','=',env('app_ip'))->where("user", "!=", 0)->where("status","=","1")->where("prepare_call","=","1")->where("clients","like","%$campaign%")->groupBy('user')->get();

		if(count($loggedInSips))
		foreach ($loggedInSips as $key => $loggedInSip) 
		{
			$loggedInUsers[] = $loggedInSip->user;
		}

		$freeDials = Dialline::whereIn("user_id",$loggedInUsers)->where("status","=","Blocked")->count();

		return count($loggedInSips)-$freeDials;
	}

	public function getDialedCallCount($campaign)
	{
		$cnt = Dialline::whereIn("status", ["Auto","AutoCall"])->where("conf","=","")->where("regexstr","=",$campaign)->count();

		return $cnt;
	}

	public function getAvailCallInPool($campaign)
	{
		//$cnt = CRMCall::where("client", "=", $campaign)->where("state","=","DialEnd")->where("type", "=", "Auto")->where("substatus","!=","")->count();

		$cnt = Dialline::where("status","=","Auto")->where("conf","=","")->where("regexstr","=",$campaign)->count();

		return $cnt;
	}

	public function getAvgTimeArray($user)
	{
		$avgdisposecObj = CRMCall::select(DB::Raw('(avg(disposec))/1000 as avgdisposec'))->where("user_id", "=", $user)->orderby("id","desc")->limit(50)->where("type","!=","Inbound")->first();

		$avgcallsecObj = CRMCall::select(DB::Raw('(avg(callsec))/1000 as avgcallsec'))->where("user_id", "=", $user)->orderby("id","desc")->limit(50)->where("type","!=","Inbound")->first();

		$returnArray['avgdisposec'] = round($avgdisposecObj->avgdisposec);
		$returnArray['avgcallsec'] = round($avgdisposecObj->avgcallsec);

		return $returnArray;
	}

	public function createCall($campaign, $acalls, $user_id)
	{
		$wakka = new KHRMSLib();
		$mastersdata=$wakka->getCompanyMaster($campaign);

		if($acalls>0)
		{
			for($i=0;$i<$acalls;$i++)
			{
				$callerid="";
				if(!empty($mastersdata["DialerDID"]))
					$callerid=$mastersdata["DialerDID"];

				$calleridarr=explode(":",$callerid);$dspan="1";
				if(isset($calleridarr[1]))
				{
					$callerid=$calleridarr[0];$dspan=$calleridarr[1];
				}	

				$dialline=Dialline::where('server','=', env('app_ip'))->where("status","=","Free")->where("enabled","=","1");
                                if($dspan!="")$dialline=$dialline->where('dspan','=',$dspan)->where('id','<=','30');
                                $dialline=$dialline->orderBy('updated_at','ASC')->first();

                                if(empty($dialline))
                                {
                                  $dialline=Dialline::where('server','=', env('app_ip'))->where("status","=","Free")->where("enabled","=","1");
                                  if($dspan!="")$dialline=$dialline->where('dspan','=', "2")->where('id','>','30');
                                  $dialline=$dialline->orderBy('id','ASC')->first();
                                }	

				if($dialline)
				{
					$users=$wakka->getPersons("client='$campaign' and status='New' and mobile!='' limit 1");
					if(sizeof($users)>=1)
					{
						$record=$wakka->getPerson($users[0]['id']);
						if($record)
						{
							$record["peopledata"]["status"]="AutoCall";
							$wakka->setPerson($users[0]['id'],$record);
						}

						$dialline->user_id=$user_id;
						$dialline->status="AutoCall";
						$dialline->regexstr=$users[0]['client'];
						$dialline->number=$users[0]["mobile"];
						$dialline->save();

						$nowts=microtime(true)*1000;

							//start the call log
						$crmcall=new CRMCall();
						$crmcall->number=$users[0]["mobile"];
						$crmcall->user_id=0;
						$crmcall->sipid_id=0;
						$crmcall->crm_id=$users[0]['id'];
						$crmcall->lan=$users[0]['lan'];
						$crmcall->client=$users[0]['client'];
						$crmcall->department=$users[0]['department'];
						$crmcall->state='New';
						$crmcall->type="AutoCall";
						$crmcall->dialline_id=$dialline->id;

						$crmcall->setTs('ts_Wait',$nowts);
						$crmcall->setTs('ts_Call',$nowts);

						$crmcall->did=$callerid;

						$tdata=array();
						$crmcall->data=json_encode($tdata);
						$crmcall->save();

							//start actual calls
						$newqueue=new Kqueue();
						$newqueue->autoCallOut($users[0]["mobile"],$callerid,$crmcall,$dialline);

					}
				}
				else break;
			}
		}

		//return 1;
	}

}