c4d3920b by Manish Mihsra

Added inbound logic function with predictive function

1 parent 0fa8f57b
......@@ -76,7 +76,6 @@ class KPAMIListen implements IEventListener
{
usleep(2000*1000);
$this->connectAvailCalls();
//$this->redirectIncoming();
}
return;
}
......@@ -1464,7 +1463,10 @@ class KPAMIListen implements IEventListener
private function connectAvailCalls()
{
$diallines=Dialline::where('server','=',$this->sipip)->where("status","=","Auto")->where("conf","=","")->get();
//TODO: Need to create static values for modes
$status = array('Auto', 'Inbound'); //NOTE: Be Careful before changing indexes
$diallines=Dialline::where('server','=',$this->sipip)->whereIn("status", $status)->where("conf","=","")->get();
if($diallines)
{
foreach($diallines as $dialline)
......@@ -1476,9 +1478,16 @@ class KPAMIListen implements IEventListener
$crmcalldata=json_decode($crmcall->data,true);
if(!isset($crmcalldata["userbusyfile"]))$crmcalldata["userbusyfile"]=0;
if($dialline->status == $status[1])$this->checkMobileNumberExist($crmcall);
$sipids=array();
$sipids=Sipid::where('server','=',$this->sipip)->where("status","=","1")->where("ready","=","1")->where("prepare_call","=","1")->orderBy("updated_at","desc")->get();
$sipids=Sipid::where('server', '=', $this->sipip)
->where("status", "=", "1")
->where("ready", "=", "1")
if($dialline->status == $status[0])->where("prepare_call", "=", "1")
->orderBy("updated_at", "desc")
->get();
if(!empty($sipids))
{
......@@ -1486,6 +1495,12 @@ class KPAMIListen implements IEventListener
foreach($sipids as $sipid)
{
$userEntry = User::find($sipid->user);
if(!empty($userEntry)){
if(!array_search($userEntry->current_dialmode, $status) || $sipid->ready != 1) return;
}
$this->connectToAvailableSip($sipid, $dialline, $crmcall);
}
}
......@@ -1494,30 +1509,55 @@ class KPAMIListen implements IEventListener
}
}
private function checkMobileNumberExist($crmcall)
{
if($crmcall->client==".")
{
$wakka = new KHRMSLib();
$users=$wakka->searchPhone("mobile",$crmcall->number);
if(!empty($users))
{
$ppldata=unserialize($users[0]['peopledata']);
$crmcall->crm_id=$users[0]['id'];
$crmcall->number=$users[0]['mobile'];
$crmcall->client=($ppldata['client']?$ppldata['client']:"");
$crmcall->department=($ppldata['department']?$ppldata['department']:"");
$crmcall->call_flag="C";
$wakka->setPersonKey($crmcall->crm_id,"dialer_lastcall",date("Y-m-d H:i:s"));
echo $users[0]['id']." -- ID Found\n";
}
else
{
$crmcall->crm_id=0;
$crmcall->client="";
$crmcall->department="";
$crmcall->call_flag="C";
echo "User Not Found!!\n";
}
$crmcall->save();
return;
}
}
private function connectToAvailableSip($sipid, $dialline, $crmcall)
{
$nowts=microtime(true)*1000;
$found=$sipid;
if($found)
if($sipid)
{
$alreadyconf=Dialline::where('server','=',$this->sipip)->where("status","=","Auto")->where("conf","=","1000".$found->id)->get();
$alreadyconf=Dialline::where('server','=',$this->sipip)->where("status", "=", $dialline->status)->where("conf","=","1000".$sipid->id)->get();
if(sizeof($alreadyconf)>0)
{
echo $found->id." : Found but Already in Conf!!!\n";
$found=false;
echo $sipid->id." : Found but Already in Conf!!!\n";
$sipid=false;
}
}
if($found)
if($sipid)
{
echo "sipids : $found->id \n";
$userEntry = User::find($found->user);
if(!empty($userEntry)){
if($userEntry->current_dialmode != "Predictive" || $found->ready != 1) return;
}
echo "sipids : $sipid->id \n";
exec("/usr/sbin/asterisk -rx 'core show channel $dialline->channel'",$channelArr);
......@@ -1545,337 +1585,49 @@ class KPAMIListen implements IEventListener
$newqueue->recordChannel($dialline->server,$dialline->channel,$recfile);
$newqueue=new Kqueue();
$newqueue->userToConf($found);
$newqueue->userToConf($sipid);
$newqueue=new Kqueue();
$newqueue->channelRedirectToExten($found->server,$dialline->channel,"1000".$found->id,"kstychDialer","1");
$newqueue->channelRedirectToExten($sipid->server,$dialline->channel,"1000".$sipid->id,"kstychDialer","1");
$dialline->user_id = $found->user;
$dialline->conf = "1000".$found->id;
$dialline->user_id = $sipid->user;
$dialline->conf = "1000".$sipid->id;
$dialline->save();
$found->ready=0;
$found->prepare_call=0;
$found->patched=1;
$found->save();
$sipid->ready=0;
if($dialline->status == $status[0])$sipid->prepare_call=0;
$sipid->patched=1;
$sipid->save();
$userlog=UserLog::where('user_id','=',$found->user)->orderBy("id","DESC")->first();
$ts_Wait=$userlog->getLastTs($found->id,$nowts);
$userlog=UserLog::where('user_id','=',$sipid->user)->orderBy("id","DESC")->first();
$ts_Wait=$userlog->getLastTs($sipid->id,$nowts);
$userlog->save();
$crmcall->user_id=$found->user;
$crmcall->sipid_id=$found->id;
$crmcall->user_id=$sipid->user;
$crmcall->sipid_id=$sipid->id;
if($ts_Wait<=$crmcall->ts_Wait) //user waiting
{
$crmcall->ts_Wait=$ts_Wait;
$crmcall->setTs('ts_Talk',$nowts);
}
else //call waiting
{
$crmcall->ts_Wait-=($nowts-$ts_Wait);
$crmcall->setTs('ts_Talk',$nowts);
}
if($ts_Wait<=$crmcall->ts_Wait) //user waiting
{
$crmcall->ts_Wait=$ts_Wait;
$crmcall->setTs('ts_Talk',$nowts);
}
else //call waiting
{
$crmcall->ts_Wait-=($nowts-$ts_Wait);
$crmcall->setTs('ts_Talk',$nowts);
}
$crmcall->save();
$crmcall->setTs('ts_Recstart',$nowts);
$crmcall->save();
if($crmcall->crm_id>0){
DB::table('records')->where('id',$crmcall->crm_id)->update(['filter_condition'=>'3','usr_id'=>$found->user]);
if($crmcall->crm_id>0){
DB::table('records')->where('id',$crmcall->crm_id)->update(['filter_condition'=>'3','usr_id'=>$sipid->user]);
}
}
$crmcall->setTs('ts_Recstart',$nowts);
$crmcall->save();
$newqueue=new Kqueue();
$newqueue->sipNotify($found,"incomingCall",$crmcall->id,$crmcall->crm_id,$crmcall->number."@".$dialline->channel);
$newqueue=new Kqueue();
$newqueue->sipNotify($sipid,"incomingCall",$crmcall->id,$crmcall->crm_id,$crmcall->number."@".$dialline->channel);
}
}
}
// private function redirectIncoming()
// {
// $diallines=Dialline::where('server','=',$this->sipip)->whereIn("status",["Inbound","Auto"])->where("conf","=","")->get();
// if($diallines)
// {
// foreach($diallines as $dialline)
// {
// $lastUpdatedTime = strtotime(date("Y-m-d H:i:s")) - strtotime($dialline->updated_at);
// if($lastUpdatedTime < 8) continue;
// exec("/usr/sbin/asterisk -rx 'core show channels'",$allChnnlsArr);
// $cnt = 0;
// foreach($allChnnlsArr as $allChnnls)
// {
// if(stristr($allChnnls, substr(stripslashes($dialline->channel),0, 20)))
// {
// $cnt++;
// }
// }
// if($cnt == 0)
// {
// Log::useFiles(storage_path()."/logs/Predictive/predictive_".date("Y_m_d").".log");
// Log::info($dialline);
// $dialline->status="Free";
// $dialline->conf="";
// $dialline->number="";
// $dialline->uniqueid="";
// $dialline->src_channel="";
// $dialline->channel="";
// $dialline->regexstr="";
// $dialline->save();
// continue;
// }
// echo $dialline->number." waiting\n";
// $crmcall=CRMCall::where("uniqueid","like","%".$dialline->uniqueid."%")->first();
// if($crmcall)
// {
// echo "Callid : ".$crmcall->id." : $crmcall->did\n";
// $nowts=microtime(true)*1000;
// $crmcalldata=json_decode($crmcall->data,true);
// if(!isset($crmcalldata["userbusyfile"]))$crmcalldata["userbusyfile"]=0;
// $playnoroute=env('playnoroute',public_path("assets/sounds/astring"));
// $playnoroutelen=env('playnoroutelen',4);
// $playnouser=env('playnouser',public_path("assets/sounds/astring"));
// $playnouserlen=env('playnouserlen',4);
// $playuserbusy=env('playuserbusy',public_path("assets/sounds/astring"));
// $playuserbusylen=env('playuserbusylen',4);
// if($crmcall->client==".")
// {
// $wakka = new KHRMSLib();
// $users=$wakka->searchPhone("mobile",$crmcall->number);
// if(!empty($users))
// {
// $ppldata=unserialize($users[0]['peopledata']);
// $crmcall->crm_id=$users[0]['id'];
// $crmcall->number=$users[0]['mobile'];
// $crmcall->client=($ppldata['client']?$ppldata['client']:"");
// $crmcall->department=($ppldata['department']?$ppldata['department']:"");
// $crmcall->call_flag="C";
// $wakka->setPersonKey($crmcall->crm_id,"dialer_lastcall",date("Y-m-d H:i:s"));
// echo $users[0]['id']." -- ID Found\n";
// }
// else
// {
// $crmcall->crm_id=0;
// $crmcall->client="";
// $crmcall->department="";
// $crmcall->call_flag="C";
// echo "User Not Found!!\n";
// }
// $crmcall->save();
// }
// $sipids=array();
// if($crmcall->client!="")
// {
// $sipids=Sipid::where('server','=',$this->sipip)->where("status","=","1")->where("clients","like","%".$crmcall->client."%")->where("clients","like","%".$crmcall->did."%")->orderBy("updated_at","desc")->get();
// }
// else if($crmcall->did!="")
// {
// $sipids=Sipid::where('server','=',$this->sipip)->where("status","=","1")->where("clients","like","%".$crmcall->did."%")->orderBy("updated_at","desc")->get();
// }
// else
// {
// $sipids=Sipid::where('server','=',$this->sipip)->where("status","=","1")->orderBy("updated_at","desc")->get();//send to all?
// }
// if(!empty($sipids))
// {
// echo "sipids found ";
// //create map?
// $first_r=0;$first_w=0;$first_a=0;$allsipids=array();
// foreach($sipids as $sipid)
// {
// //if($sipid->ready==1)
// if($sipid->ready==1&&$sipid->prepare_call==0&&$dialline->status=="Inbound")
// {
// $clientsarr=json_decode($sipid->clients,true);
// if($crmcall->client=="")$first_a=$sipid;
// else
// {
// if($clientsarr['a']!="")$clientaarr = explode(",", $clientsarr['a']);
// if(isset($clientsarr['a'])&&in_array($crmcall->client, $clientaarr))$first_a=$sipid;
// if($clientsarr['w']!="")$clientwarr = explode(",", $clientsarr['w']);
// if(isset($clientsarr['w'])&&in_array($crmcall->client, $clientwarr))$first_w=$sipid;
// if($clientsarr['r']!="")$clientrarr = explode(",", $clientsarr['r']);
// if(isset($clientsarr['r'])&&in_array($crmcall->client, $clientrarr))$first_r=$sipid;
// }
// }
// else
// {
// $allsipids[]=$sipid;
// }
// }
// foreach($sipids as $sipid)
// {
// if($sipid->ready==1&&$sipid->prepare_call==1&&$dialline->status=="Auto")
// {
// $clientsarr=json_decode($sipid->clients,true);
// if($crmcall->client=="")$first_a=$sipid;
// else
// {
// if($clientsarr['a']!="")$clientaarr = explode(",", $clientsarr['a']);
// if(isset($clientsarr['a'])&&in_array($crmcall->client, $clientaarr))$first_a=$sipid;
// if($clientsarr['w']!="")$clientwarr = explode(",", $clientsarr['w']);
// if(isset($clientsarr['w'])&&in_array($crmcall->client, $clientwarr))$first_w=$sipid;
// if($clientsarr['r']!="")$clientrarr = explode(",", $clientsarr['r']);
// if(isset($clientsarr['r'])&&in_array($crmcall->client, $clientrarr))$first_r=$sipid;
// }
// }
// else
// {
// $allsipids[]=$sipid;
// }
// }
// echo "Notifying Total ::".sizeof($allsipids)."\n";
// $found=false;
// if($first_a)$found=$first_a;
// else if($first_w)$found=$first_w;
// else if($first_r)$found=$first_r;
// if($found)
// {
// $alreadyconf=Dialline::where('server','=',$this->sipip)->whereIn("status",["Inbound","Auto"])->where("conf","=","1000".$found->id)->get();
// if(sizeof($alreadyconf)>0)
// {
// echo $found->id." : Found but Already in Conf!!!\n";
// $found=false;
// }
// }
// if($found)
// {
// echo "sipids : $found->id \n";
// //get ts_Wait
// $userlog=UserLog::where('user_id','=',$found->user)->orderBy("id","DESC")->first();
// $ts_Wait=$userlog->getLastTs($found->id,$nowts);
// $userlog->save();
// $crmcall->user_id=$found->user;
// $crmcall->sipid_id=$found->id;
// if($ts_Wait<=$crmcall->ts_Wait) //user waiting
// {
// $crmcall->ts_Wait=$ts_Wait;
// $crmcall->setTs('ts_Talk',$nowts);
// }
// else //call waiting
// {
// $crmcall->ts_Wait-=($nowts-$ts_Wait);
// $crmcall->setTs('ts_Talk',$nowts);
// }
// $crmcall->save();
// $dialline->user_id = $found->user;
// $dialline->conf = "1000".$found->id;
// $dialline->save();
// if($crmcall->crm_id>0){
// DB::table('records')->where('id',$crmcall->crm_id)->update(['filter_condition'=>'3','usr_id'=>$found->user]);
// }
// $found->ready=0;
// $found->prepare_call=0;
// $found->patched=1;
// $found->save();
// $recfile=$crmcall->newRecFilePath();
// $crmcall->setTs('ts_Recstart',$nowts);
// $crmcall->save();
// //start recording
// $newqueue=new Kqueue();
// $newqueue->recordChannel($dialline->server,$dialline->channel,$recfile);
// $newqueue=new Kqueue();
// $newqueue->userToConf($found);
// $newqueue=new Kqueue();
// $newqueue->channelRedirectToExten($found->server,$dialline->channel,"1000".$found->id,"kstychDialer","1");
// //notify UI (only the sip id)
// $newqueue=new Kqueue();
// $newqueue->sipNotify($found,"dialerUI","dialstart","c",$crmcall->number);
// $newqueue=new Kqueue();
// $newqueue->sipNotify($found,"incomingCall",$crmcall->id,$crmcall->crm_id,$crmcall->number."@".$dialline->channel);
// }
// else
// {
// if(!empty($allsipids))
// {
// foreach($allsipids as $tsip)
// {
// //$newqueue=new Kqueue();
// //$newqueue->sipNotify($tsip,"popupalert"," Incoming Calls",$crmcall->number."|".$crmcall->client,"");
// if($dialline->status!="Auto")
// {
// $newqueue=new Kqueue();
// $newqueue->sipNotify($tsip,"popupalert"," Incoming Calls",$crmcall->number."|".$crmcall->client,"");
// }
// }
// }
// else
// {
// //play no users available
// if($crmcalldata["userbusyfile"]<$nowts)
// {
// $crmcalldata["userbusyfile"]=$nowts+(($playnouserlen+2)*1000);
// $crmcall->data=json_encode($crmcalldata);
// $crmcall->save();
// }
// }
// //no ready user, send notifications
// }
// }
// else
// {
// //no users online
// //play no routes file
// if($crmcalldata["userbusyfile"]<$nowts)
// {
// $crmcalldata["userbusyfile"]=$nowts+(($playnoroutelen+2)*1000);
// $crmcall->data=json_encode($crmcalldata);
// $crmcall->save();
// //$newqueue=new Kqueue();
// //$newqueue->playfile($playnoroute,$dialline->channel,"kstychDialerINB",$dialline->server);
// }
// }
// }
// else
// {
// //$dialline->status='Free';
// //$dialline->save();
// }
// //if client was matched then find users by client
// //else find users by DID
// //ele everyone
// }
// }
// }
private function sendAMICommands()
{
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!