c4d3920b by Manish Mihsra

Added inbound logic function with predictive function

1 parent 0fa8f57b
...@@ -76,7 +76,6 @@ class KPAMIListen implements IEventListener ...@@ -76,7 +76,6 @@ class KPAMIListen implements IEventListener
76 { 76 {
77 usleep(2000*1000); 77 usleep(2000*1000);
78 $this->connectAvailCalls(); 78 $this->connectAvailCalls();
79 //$this->redirectIncoming();
80 } 79 }
81 return; 80 return;
82 } 81 }
...@@ -1464,7 +1463,10 @@ class KPAMIListen implements IEventListener ...@@ -1464,7 +1463,10 @@ class KPAMIListen implements IEventListener
1464 1463
1465 private function connectAvailCalls() 1464 private function connectAvailCalls()
1466 { 1465 {
1467 $diallines=Dialline::where('server','=',$this->sipip)->where("status","=","Auto")->where("conf","=","")->get(); 1466 //TODO: Need to create static values for modes
1467 $status = array('Auto', 'Inbound'); //NOTE: Be Careful before changing indexes
1468
1469 $diallines=Dialline::where('server','=',$this->sipip)->whereIn("status", $status)->where("conf","=","")->get();
1468 if($diallines) 1470 if($diallines)
1469 { 1471 {
1470 foreach($diallines as $dialline) 1472 foreach($diallines as $dialline)
...@@ -1476,9 +1478,16 @@ class KPAMIListen implements IEventListener ...@@ -1476,9 +1478,16 @@ class KPAMIListen implements IEventListener
1476 $crmcalldata=json_decode($crmcall->data,true); 1478 $crmcalldata=json_decode($crmcall->data,true);
1477 if(!isset($crmcalldata["userbusyfile"]))$crmcalldata["userbusyfile"]=0; 1479 if(!isset($crmcalldata["userbusyfile"]))$crmcalldata["userbusyfile"]=0;
1478 1480
1481 if($dialline->status == $status[1])$this->checkMobileNumberExist($crmcall);
1482
1479 $sipids=array(); 1483 $sipids=array();
1480 1484
1481 $sipids=Sipid::where('server','=',$this->sipip)->where("status","=","1")->where("ready","=","1")->where("prepare_call","=","1")->orderBy("updated_at","desc")->get(); 1485 $sipids=Sipid::where('server', '=', $this->sipip)
1486 ->where("status", "=", "1")
1487 ->where("ready", "=", "1")
1488 if($dialline->status == $status[0])->where("prepare_call", "=", "1")
1489 ->orderBy("updated_at", "desc")
1490 ->get();
1482 1491
1483 if(!empty($sipids)) 1492 if(!empty($sipids))
1484 { 1493 {
...@@ -1486,6 +1495,12 @@ class KPAMIListen implements IEventListener ...@@ -1486,6 +1495,12 @@ class KPAMIListen implements IEventListener
1486 1495
1487 foreach($sipids as $sipid) 1496 foreach($sipids as $sipid)
1488 { 1497 {
1498 $userEntry = User::find($sipid->user);
1499
1500 if(!empty($userEntry)){
1501 if(!array_search($userEntry->current_dialmode, $status) || $sipid->ready != 1) return;
1502 }
1503
1489 $this->connectToAvailableSip($sipid, $dialline, $crmcall); 1504 $this->connectToAvailableSip($sipid, $dialline, $crmcall);
1490 } 1505 }
1491 } 1506 }
...@@ -1494,30 +1509,55 @@ class KPAMIListen implements IEventListener ...@@ -1494,30 +1509,55 @@ class KPAMIListen implements IEventListener
1494 } 1509 }
1495 } 1510 }
1496 1511
1512 private function checkMobileNumberExist($crmcall)
1513 {
1514 if($crmcall->client==".")
1515 {
1516 $wakka = new KHRMSLib();
1517 $users=$wakka->searchPhone("mobile",$crmcall->number);
1518 if(!empty($users))
1519 {
1520 $ppldata=unserialize($users[0]['peopledata']);
1521
1522 $crmcall->crm_id=$users[0]['id'];
1523 $crmcall->number=$users[0]['mobile'];
1524 $crmcall->client=($ppldata['client']?$ppldata['client']:"");
1525 $crmcall->department=($ppldata['department']?$ppldata['department']:"");
1526 $crmcall->call_flag="C";
1527 $wakka->setPersonKey($crmcall->crm_id,"dialer_lastcall",date("Y-m-d H:i:s"));
1528 echo $users[0]['id']." -- ID Found\n";
1529 }
1530 else
1531 {
1532 $crmcall->crm_id=0;
1533 $crmcall->client="";
1534 $crmcall->department="";
1535 $crmcall->call_flag="C";
1536 echo "User Not Found!!\n";
1537 }
1538 $crmcall->save();
1539
1540 return;
1541 }
1542 }
1543
1497 private function connectToAvailableSip($sipid, $dialline, $crmcall) 1544 private function connectToAvailableSip($sipid, $dialline, $crmcall)
1498 { 1545 {
1499 $nowts=microtime(true)*1000; 1546 $nowts=microtime(true)*1000;
1500 $found=$sipid;
1501 1547
1502 if($found) 1548 if($sipid)
1503 { 1549 {
1504 $alreadyconf=Dialline::where('server','=',$this->sipip)->where("status","=","Auto")->where("conf","=","1000".$found->id)->get(); 1550 $alreadyconf=Dialline::where('server','=',$this->sipip)->where("status", "=", $dialline->status)->where("conf","=","1000".$sipid->id)->get();
1505 if(sizeof($alreadyconf)>0) 1551 if(sizeof($alreadyconf)>0)
1506 { 1552 {
1507 echo $found->id." : Found but Already in Conf!!!\n"; 1553 echo $sipid->id." : Found but Already in Conf!!!\n";
1508 $found=false; 1554 $sipid=false;
1509 } 1555 }
1510 } 1556 }
1511 1557
1512 if($found) 1558 if($sipid)
1513 { 1559 {
1514 echo "sipids : $found->id \n"; 1560 echo "sipids : $sipid->id \n";
1515
1516 $userEntry = User::find($found->user);
1517
1518 if(!empty($userEntry)){
1519 if($userEntry->current_dialmode != "Predictive" || $found->ready != 1) return;
1520 }
1521 1561
1522 exec("/usr/sbin/asterisk -rx 'core show channel $dialline->channel'",$channelArr); 1562 exec("/usr/sbin/asterisk -rx 'core show channel $dialline->channel'",$channelArr);
1523 1563
...@@ -1545,337 +1585,49 @@ class KPAMIListen implements IEventListener ...@@ -1545,337 +1585,49 @@ class KPAMIListen implements IEventListener
1545 $newqueue->recordChannel($dialline->server,$dialline->channel,$recfile); 1585 $newqueue->recordChannel($dialline->server,$dialline->channel,$recfile);
1546 1586
1547 $newqueue=new Kqueue(); 1587 $newqueue=new Kqueue();
1548 $newqueue->userToConf($found); 1588 $newqueue->userToConf($sipid);
1549 1589
1550 $newqueue=new Kqueue(); 1590 $newqueue=new Kqueue();
1551 $newqueue->channelRedirectToExten($found->server,$dialline->channel,"1000".$found->id,"kstychDialer","1"); 1591 $newqueue->channelRedirectToExten($sipid->server,$dialline->channel,"1000".$sipid->id,"kstychDialer","1");
1552 1592
1553 $dialline->user_id = $found->user; 1593 $dialline->user_id = $sipid->user;
1554 $dialline->conf = "1000".$found->id; 1594 $dialline->conf = "1000".$sipid->id;
1555 $dialline->save(); 1595 $dialline->save();
1556 1596
1557 $found->ready=0; 1597 $sipid->ready=0;
1558 $found->prepare_call=0; 1598 if($dialline->status == $status[0])$sipid->prepare_call=0;
1559 $found->patched=1; 1599 $sipid->patched=1;
1560 $found->save(); 1600 $sipid->save();
1561 1601
1562 $userlog=UserLog::where('user_id','=',$found->user)->orderBy("id","DESC")->first(); 1602 $userlog=UserLog::where('user_id','=',$sipid->user)->orderBy("id","DESC")->first();
1563 $ts_Wait=$userlog->getLastTs($found->id,$nowts); 1603 $ts_Wait=$userlog->getLastTs($sipid->id,$nowts);
1564 $userlog->save(); 1604 $userlog->save();
1565 1605
1566 $crmcall->user_id=$found->user; 1606 $crmcall->user_id=$sipid->user;
1567 $crmcall->sipid_id=$found->id; 1607 $crmcall->sipid_id=$sipid->id;
1568 1608
1569 if($ts_Wait<=$crmcall->ts_Wait) //user waiting 1609 if($ts_Wait<=$crmcall->ts_Wait) //user waiting
1570 { 1610 {
1571 $crmcall->ts_Wait=$ts_Wait; 1611 $crmcall->ts_Wait=$ts_Wait;
1572 $crmcall->setTs('ts_Talk',$nowts); 1612 $crmcall->setTs('ts_Talk',$nowts);
1573 } 1613 }
1574 else //call waiting 1614 else //call waiting
1575 { 1615 {
1576 $crmcall->ts_Wait-=($nowts-$ts_Wait); 1616 $crmcall->ts_Wait-=($nowts-$ts_Wait);
1577 $crmcall->setTs('ts_Talk',$nowts); 1617 $crmcall->setTs('ts_Talk',$nowts);
1578 } 1618 }
1579 1619
1580 $crmcall->save(); 1620 $crmcall->setTs('ts_Recstart',$nowts);
1581 1621 $crmcall->save();
1582 1622
1583 if($crmcall->crm_id>0){ 1623 if($crmcall->crm_id>0){
1584 DB::table('records')->where('id',$crmcall->crm_id)->update(['filter_condition'=>'3','usr_id'=>$found->user]); 1624 DB::table('records')->where('id',$crmcall->crm_id)->update(['filter_condition'=>'3','usr_id'=>$sipid->user]);
1625 }
1585 1626
1586 } 1627 $newqueue=new Kqueue();
1587 1628 $newqueue->sipNotify($sipid,"incomingCall",$crmcall->id,$crmcall->crm_id,$crmcall->number."@".$dialline->channel);
1588 $crmcall->setTs('ts_Recstart',$nowts); 1629 }
1589 $crmcall->save();
1590
1591 $newqueue=new Kqueue();
1592 $newqueue->sipNotify($found,"incomingCall",$crmcall->id,$crmcall->crm_id,$crmcall->number."@".$dialline->channel);
1593 } 1630 }
1594
1595 }
1596
1597 // private function redirectIncoming()
1598 // {
1599 // $diallines=Dialline::where('server','=',$this->sipip)->whereIn("status",["Inbound","Auto"])->where("conf","=","")->get();
1600 // if($diallines)
1601 // {
1602 // foreach($diallines as $dialline)
1603 // {
1604 // $lastUpdatedTime = strtotime(date("Y-m-d H:i:s")) - strtotime($dialline->updated_at);
1605
1606 // if($lastUpdatedTime < 8) continue;
1607
1608 // exec("/usr/sbin/asterisk -rx 'core show channels'",$allChnnlsArr);
1609 // $cnt = 0;
1610 // foreach($allChnnlsArr as $allChnnls)
1611 // {
1612 // if(stristr($allChnnls, substr(stripslashes($dialline->channel),0, 20)))
1613 // {
1614 // $cnt++;
1615 // }
1616 // }
1617
1618 // if($cnt == 0)
1619 // {
1620 // Log::useFiles(storage_path()."/logs/Predictive/predictive_".date("Y_m_d").".log");
1621 // Log::info($dialline);
1622
1623 // $dialline->status="Free";
1624 // $dialline->conf="";
1625 // $dialline->number="";
1626 // $dialline->uniqueid="";
1627 // $dialline->src_channel="";
1628 // $dialline->channel="";
1629 // $dialline->regexstr="";
1630 // $dialline->save();
1631
1632 // continue;
1633 // }
1634
1635 // echo $dialline->number." waiting\n";
1636 // $crmcall=CRMCall::where("uniqueid","like","%".$dialline->uniqueid."%")->first();
1637
1638 // if($crmcall)
1639 // {
1640 // echo "Callid : ".$crmcall->id." : $crmcall->did\n";
1641
1642 // $nowts=microtime(true)*1000;
1643 // $crmcalldata=json_decode($crmcall->data,true);
1644 // if(!isset($crmcalldata["userbusyfile"]))$crmcalldata["userbusyfile"]=0;
1645 // $playnoroute=env('playnoroute',public_path("assets/sounds/astring"));
1646 // $playnoroutelen=env('playnoroutelen',4);
1647 // $playnouser=env('playnouser',public_path("assets/sounds/astring"));
1648 // $playnouserlen=env('playnouserlen',4);
1649 // $playuserbusy=env('playuserbusy',public_path("assets/sounds/astring"));
1650 // $playuserbusylen=env('playuserbusylen',4);
1651
1652 // if($crmcall->client==".")
1653 // {
1654 // $wakka = new KHRMSLib();
1655 // $users=$wakka->searchPhone("mobile",$crmcall->number);
1656 // if(!empty($users))
1657 // {
1658 // $ppldata=unserialize($users[0]['peopledata']);
1659
1660 // $crmcall->crm_id=$users[0]['id'];
1661 // $crmcall->number=$users[0]['mobile'];
1662 // $crmcall->client=($ppldata['client']?$ppldata['client']:"");
1663 // $crmcall->department=($ppldata['department']?$ppldata['department']:"");
1664 // $crmcall->call_flag="C";
1665 // $wakka->setPersonKey($crmcall->crm_id,"dialer_lastcall",date("Y-m-d H:i:s"));
1666 // echo $users[0]['id']." -- ID Found\n";
1667 // }
1668 // else
1669 // {
1670 // $crmcall->crm_id=0;
1671 // $crmcall->client="";
1672 // $crmcall->department="";
1673 // $crmcall->call_flag="C";
1674 // echo "User Not Found!!\n";
1675 // }
1676 // $crmcall->save();
1677 // }
1678
1679 // $sipids=array();
1680 // if($crmcall->client!="")
1681 // {
1682 // $sipids=Sipid::where('server','=',$this->sipip)->where("status","=","1")->where("clients","like","%".$crmcall->client."%")->where("clients","like","%".$crmcall->did."%")->orderBy("updated_at","desc")->get();
1683 // }
1684 // else if($crmcall->did!="")
1685 // {
1686 // $sipids=Sipid::where('server','=',$this->sipip)->where("status","=","1")->where("clients","like","%".$crmcall->did."%")->orderBy("updated_at","desc")->get();
1687 // }
1688 // else
1689 // {
1690 // $sipids=Sipid::where('server','=',$this->sipip)->where("status","=","1")->orderBy("updated_at","desc")->get();//send to all?
1691 // }
1692
1693 // if(!empty($sipids))
1694 // {
1695 // echo "sipids found ";
1696
1697 // //create map?
1698 // $first_r=0;$first_w=0;$first_a=0;$allsipids=array();
1699 // foreach($sipids as $sipid)
1700 // {
1701 // //if($sipid->ready==1)
1702 // if($sipid->ready==1&&$sipid->prepare_call==0&&$dialline->status=="Inbound")
1703 // {
1704 // $clientsarr=json_decode($sipid->clients,true);
1705 // if($crmcall->client=="")$first_a=$sipid;
1706 // else
1707 // {
1708 // if($clientsarr['a']!="")$clientaarr = explode(",", $clientsarr['a']);
1709 // if(isset($clientsarr['a'])&&in_array($crmcall->client, $clientaarr))$first_a=$sipid;
1710 // if($clientsarr['w']!="")$clientwarr = explode(",", $clientsarr['w']);
1711 // if(isset($clientsarr['w'])&&in_array($crmcall->client, $clientwarr))$first_w=$sipid;
1712 // if($clientsarr['r']!="")$clientrarr = explode(",", $clientsarr['r']);
1713 // if(isset($clientsarr['r'])&&in_array($crmcall->client, $clientrarr))$first_r=$sipid;
1714 // }
1715 // }
1716 // else
1717 // {
1718 // $allsipids[]=$sipid;
1719 // }
1720 // }
1721
1722 // foreach($sipids as $sipid)
1723 // {
1724 // if($sipid->ready==1&&$sipid->prepare_call==1&&$dialline->status=="Auto")
1725 // {
1726 // $clientsarr=json_decode($sipid->clients,true);
1727 // if($crmcall->client=="")$first_a=$sipid;
1728 // else
1729 // {
1730 // if($clientsarr['a']!="")$clientaarr = explode(",", $clientsarr['a']);
1731 // if(isset($clientsarr['a'])&&in_array($crmcall->client, $clientaarr))$first_a=$sipid;
1732 // if($clientsarr['w']!="")$clientwarr = explode(",", $clientsarr['w']);
1733 // if(isset($clientsarr['w'])&&in_array($crmcall->client, $clientwarr))$first_w=$sipid;
1734 // if($clientsarr['r']!="")$clientrarr = explode(",", $clientsarr['r']);
1735 // if(isset($clientsarr['r'])&&in_array($crmcall->client, $clientrarr))$first_r=$sipid;
1736 // }
1737 // }
1738 // else
1739 // {
1740 // $allsipids[]=$sipid;
1741 // }
1742 // }
1743
1744 // echo "Notifying Total ::".sizeof($allsipids)."\n";
1745
1746 // $found=false;
1747 // if($first_a)$found=$first_a;
1748 // else if($first_w)$found=$first_w;
1749 // else if($first_r)$found=$first_r;
1750
1751 // if($found)
1752 // {
1753 // $alreadyconf=Dialline::where('server','=',$this->sipip)->whereIn("status",["Inbound","Auto"])->where("conf","=","1000".$found->id)->get();
1754 // if(sizeof($alreadyconf)>0)
1755 // {
1756 // echo $found->id." : Found but Already in Conf!!!\n";
1757 // $found=false;
1758 // }
1759 // }
1760
1761 // if($found)
1762 // {
1763 // echo "sipids : $found->id \n";
1764
1765 // //get ts_Wait
1766 // $userlog=UserLog::where('user_id','=',$found->user)->orderBy("id","DESC")->first();
1767 // $ts_Wait=$userlog->getLastTs($found->id,$nowts);
1768 // $userlog->save();
1769
1770 // $crmcall->user_id=$found->user;
1771 // $crmcall->sipid_id=$found->id;
1772
1773 // if($ts_Wait<=$crmcall->ts_Wait) //user waiting
1774 // {
1775 // $crmcall->ts_Wait=$ts_Wait;
1776 // $crmcall->setTs('ts_Talk',$nowts);
1777 // }
1778 // else //call waiting
1779 // {
1780 // $crmcall->ts_Wait-=($nowts-$ts_Wait);
1781 // $crmcall->setTs('ts_Talk',$nowts);
1782 // }
1783
1784 // $crmcall->save();
1785
1786 // $dialline->user_id = $found->user;
1787 // $dialline->conf = "1000".$found->id;
1788 // $dialline->save();
1789
1790 // if($crmcall->crm_id>0){
1791 // DB::table('records')->where('id',$crmcall->crm_id)->update(['filter_condition'=>'3','usr_id'=>$found->user]);
1792
1793 // }
1794 // $found->ready=0;
1795 // $found->prepare_call=0;
1796 // $found->patched=1;
1797 // $found->save();
1798
1799 // $recfile=$crmcall->newRecFilePath();
1800
1801 // $crmcall->setTs('ts_Recstart',$nowts);
1802 // $crmcall->save();
1803
1804 // //start recording
1805 // $newqueue=new Kqueue();
1806 // $newqueue->recordChannel($dialline->server,$dialline->channel,$recfile);
1807
1808 // $newqueue=new Kqueue();
1809 // $newqueue->userToConf($found);
1810
1811 // $newqueue=new Kqueue();
1812 // $newqueue->channelRedirectToExten($found->server,$dialline->channel,"1000".$found->id,"kstychDialer","1");
1813
1814 // //notify UI (only the sip id)
1815 // $newqueue=new Kqueue();
1816 // $newqueue->sipNotify($found,"dialerUI","dialstart","c",$crmcall->number);
1817
1818 // $newqueue=new Kqueue();
1819 // $newqueue->sipNotify($found,"incomingCall",$crmcall->id,$crmcall->crm_id,$crmcall->number."@".$dialline->channel);
1820 // }
1821 // else
1822 // {
1823 // if(!empty($allsipids))
1824 // {
1825 // foreach($allsipids as $tsip)
1826 // {
1827 // //$newqueue=new Kqueue();
1828 // //$newqueue->sipNotify($tsip,"popupalert"," Incoming Calls",$crmcall->number."|".$crmcall->client,"");
1829 // if($dialline->status!="Auto")
1830 // {
1831 // $newqueue=new Kqueue();
1832 // $newqueue->sipNotify($tsip,"popupalert"," Incoming Calls",$crmcall->number."|".$crmcall->client,"");
1833 // }
1834 // }
1835 // }
1836 // else
1837 // {
1838 // //play no users available
1839 // if($crmcalldata["userbusyfile"]<$nowts)
1840 // {
1841 // $crmcalldata["userbusyfile"]=$nowts+(($playnouserlen+2)*1000);
1842 // $crmcall->data=json_encode($crmcalldata);
1843 // $crmcall->save();
1844 // }
1845
1846 // }
1847 // //no ready user, send notifications
1848 // }
1849 // }
1850 // else
1851 // {
1852 // //no users online
1853 // //play no routes file
1854 // if($crmcalldata["userbusyfile"]<$nowts)
1855 // {
1856 // $crmcalldata["userbusyfile"]=$nowts+(($playnoroutelen+2)*1000);
1857 // $crmcall->data=json_encode($crmcalldata);
1858 // $crmcall->save();
1859
1860 // //$newqueue=new Kqueue();
1861 // //$newqueue->playfile($playnoroute,$dialline->channel,"kstychDialerINB",$dialline->server);
1862 // }
1863 // }
1864
1865 // }
1866 // else
1867 // {
1868 // //$dialline->status='Free';
1869 // //$dialline->save();
1870 // }
1871 // //if client was matched then find users by client
1872 // //else find users by DID
1873 // //ele everyone
1874 // }
1875
1876 // }
1877
1878 // }
1879 1631
1880 private function sendAMICommands() 1632 private function sendAMICommands()
1881 { 1633 {
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!