AutodialController_28062018.php 4.18 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");
           $sipip=env('app_ip');
          	$wakka = new KHRMSLib();
          	
                for($i=0;$i<10;$i++)
		 		{
		 			
		 			usleep(2000*1000);
		 			//echo "prashant----".Auth::user()->id;
                    $dialmode = DB::table('users')->where('id','=',Auth::user()->id)->first();
                    //echo $dialmode->current_dialmode;
          			if($dialmode->current_dialmode!="Predictive")
          			{
          				break;
          				exit;
          			}
		 			 $autocompany=array();
					 $companyarr=$wakka->LoadAll("select * from hrms_masters where mtype='company' and mkey='$campaign'");
					foreach($companyarr as $tcompany)
					{
						$mastersdata=$wakka->getCompanyMaster($tcompany['mkey']);
						if(!isset($mastersdata["autodialercampaign"]))$mastersdata["autodialercampaign"]=0;
			
						if($mastersdata["autodialercampaign"]>0)
						{
						 $autocompany[$tcompany['mkey']]=$mastersdata;
						}
					}
          		    foreach($autocompany as $tcompany=>$mastersdata)
					{
						$asipids=Sipid::where('server','=',$sipip)->where("status","=","1")->where("ready","=","1")->where("clients","like","%$tcompany%")->count();

						if($asipids>0)
						{	

							$acalls=($asipids*$mastersdata["autodialercampaign"])-Dialline::where('server','=',$this->sipip)->whereIn("status",array("AutoCall","Auto"))->where("conf","=","")->where("regexstr","=",$tcompany)->count();

							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','=', $sipip)->where("status","=","Free")->where("enabled","=","1");
							       
									if($dspan!="")$dialline=$dialline->where('dspan','=',$dspan);

									$dialline=$dialline->orderBy('updated_at','ASC')->first();
							
									if($dialline)
									{
								
										$users=$wakka->getPersons("client='$campaign' and status='New' limit 1");
										if(sizeof($users)>=1)
										{
											$dialline->status="Free";
											$dialline->regexstr=$users[0]['client'];
											$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);
										}

										DB::update("update records set status='AutoDial' where id='".$users[0]["id"]."'");
									}
									else break;
								}
							}
						}
					}
				}
        }
	}
	

}