VoiceBlasterController.php 5.35 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 App\Models\Campaign;
use DB;
use Log;
use Session;
$client='';

class VoiceBlasterController extends Controller {

	public function __construct()
	{
	  $this->middleware('auth');
	  //$this->middleware('module_access');
	}

	public function index()
	{
		$data 			= array();
		$data['wakka']	= new KHRMSLib();

		return view('layout.module.voiceblaster.index', $data);
	}
	public function create()
	{

	}
	public function store()
	{
		$data = array();
		$action = Input::get("action");

		if($action == "uploadfile")
		{	
			$sound =Input::file('file');

			if($sound->getClientOriginalExtension() == "gsm")
			{
				$name = 'vbsound.'.$sound->getClientOriginalExtension();
		    	$destinationPath = storage_path()."/vb";
		    	$sound->move($destinationPath, $name);

		    	return 1;
			}
			else
			{
				return 0;
			}
		}

	}
	public function show($id)
	{    
        if($id=="showcamapign")
		{
			$campaign = Input::get("campaign");
			if($campaign != ""){
				
					$data['campaignDetails'] = Campaign::where("mtype","=","company")->where("mkey","=",$campaign)->first();
					$data['dataCountByStatus'] = DB::table("records")->select(DB::Raw('status, count(*) as recordCount'))->where("client","=",$campaign)->where("status","=","New")->get();

				return view("layout.module.voiceblaster.blasterlist",$data);
				
			}else{
				return "<script>simpleNotification('error','topRight','Campaign name should not be blank.');</script><br/><p class='text-danger text-center'>Campaign name should not be blank.</p>";
			}
		}
		
		if($id=="vbdialmode")
		{ 
			$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["vbpace"];
		$dialedCalls	=	$this->getDialedCallCount($campaign);
//echo $ratio ."--".$dailedCalls;
	return $ratio-$dialedCalls;
	}
	

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

		return $cnt;
	}


	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','=', '10.4.105.141')->where("status","=","Free")->where("enabled","=","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"]="vbCall";
							$wakka->setPerson($users[0]['id'],$record);
						}

						$dialline->user_id=$user_id;
						$dialline->status="vbCall";
						$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="vbCall";
						$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->voiceBlasterCallOut($users[0]["mobile"],$callerid,$crmcall,$dialline);

					}
				}
				else break;
			}
		}

		//return 1;
	}

}