1546 lines
65 KiB
PHP
1546 lines
65 KiB
PHP
<?php
|
|
|
|
class CallEvents
|
|
{
|
|
public $table = 'call_events';
|
|
public $index = 'id';
|
|
public $fields = array(
|
|
'operator',
|
|
'operator_call_id',
|
|
'internal_number',
|
|
'external_number',
|
|
'transit_number',
|
|
'status',
|
|
'user_id',
|
|
'client_id',
|
|
'agency_id',
|
|
'start_time',
|
|
'answer_time',
|
|
'release_time',
|
|
'uuid_call_id',
|
|
'uuid',
|
|
|
|
|
|
);
|
|
private $funnel_telephony = array();
|
|
protected $_inputData;
|
|
private $dbpg = null;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->dbpg = new MysqlPdo(hstpg, ndbpg, 'u0305746_jw', passpg, false, '5432', true);
|
|
}
|
|
|
|
public function insert($data)
|
|
{
|
|
|
|
$query = "INSERT INTO ".$this->table." SET ";
|
|
$query_set = array();
|
|
$array_val = array();
|
|
$array_field = array();
|
|
foreach($this->fields as $field){
|
|
$query_set[] = $field." = '". $data[$field]."'";
|
|
$_data[$field] = $data[$field];
|
|
if(!empty($data[$field])){
|
|
$array_val[] = $data[$field];
|
|
$array_field[] = $field;
|
|
}
|
|
}
|
|
$query .= implode(', ', $query_set);
|
|
|
|
mysql_query($query);
|
|
$_data['id'] = mysql_insert_id();
|
|
$querypg = "INSERT INTO ".$this->table." (".implode(',',$array_field).") VALUES ('".implode('\',\'', $array_val)."')";
|
|
// file_put_contents($_SERVER['DOCUMENT_ROOT']."/webhooks/log_call_sql.txt", $querypg."\n", FILE_APPEND);
|
|
$this->dbpg->query($querypg,true);
|
|
$_data['idpg'] = $this->dbpg->insert_id();
|
|
return $_data;
|
|
}
|
|
|
|
public function save($data,$id, $idpg)
|
|
{
|
|
|
|
//var_dump($data);
|
|
$query = $querypg = "UPDATE ".$this->table." SET ";
|
|
$query_set = array();
|
|
if($data['redirect']){
|
|
$this->saveRederect($data['redirect'], $idpg);
|
|
}
|
|
unset($data['redirect']);
|
|
if(isset($data['operator_call_id']) && $data['operator'] != 'TELPHIN' && $data['operator'] != 'MEGAFON' ){
|
|
$data['uuid_call_id'] = (int)str_replace(['callhalf-',':'],'', $data['operator_call_id']);
|
|
}
|
|
/*if(isset($data['release_time'])){
|
|
$duration = (int)($data['release_time'] - $data['start_time']);
|
|
var_dump($duration);
|
|
$data['uuid'] = $data['start_time'] + $duration;
|
|
}*/
|
|
foreach($data as $field=>$value){
|
|
if($field != 'id' && $field != 'idpg'){
|
|
$query_set[] = $field." = '". $value."'";
|
|
}
|
|
}
|
|
$query .= implode(', ', $query_set) . " WHERE " . $this->index . " = " .$id;
|
|
$querypg .= implode(', ', $query_set) . " WHERE " . $this->index . " = " .$idpg;
|
|
//file_put_contents($_SERVER['DOCUMENT_ROOT']."/webhooks/log_call_ev_sql.txt", $querypg."\n", FILE_APPEND);
|
|
// echo $querypg."\n";
|
|
// echo $idpg."\n";
|
|
if($idpg>0){
|
|
$this->dbpg->query($querypg, true);
|
|
}
|
|
if($id>0){
|
|
mysql_query($query);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function saveRederect($data, $call_id){
|
|
|
|
$time = floor($data['xsi:redirectTime']/1000);
|
|
$phone = str_replace('tel:', '',$data['xsi:address']['@content']);
|
|
$user = $this->getUserByField('phone', $phone);
|
|
$sql = "SELECT id FROM `call_events_redirect` WHERE call_id = $call_id and transfer = $time and user_id = $user[id]";
|
|
|
|
$q = mysql_query($sql);
|
|
if(mysql_num_rows($q) == 0){
|
|
$sql_u = "INSERT INTO `call_events_redirect` SET call_id = $call_id, transfer = $time, user_id = $user[id], phone = '$phone'";
|
|
mysql_query($sql_u);
|
|
}
|
|
|
|
}
|
|
|
|
public function selectByField($field, $value, $table = false)
|
|
{
|
|
$result = false;
|
|
$table_table = $this->table;
|
|
if($table){
|
|
$table_table = $table;
|
|
}
|
|
$query = "SELECT * FROM ".$table_table." WHERE $field = '$value'";
|
|
|
|
if($table != 'telphin_operator_id')
|
|
$arrpg = $this->dbpg->fetch_assoc($this->dbpg->query($query));
|
|
|
|
$arr = mysql_fetch_assoc(mysql_query($query));
|
|
if(!empty($arr)){
|
|
if(isset($arrpg))
|
|
$arr['idpg'] = $arrpg['id'];
|
|
$result = $arr;
|
|
} else if (!empty($arrpg)){
|
|
$arrpg['idpg'] = $arrpg['id'];
|
|
$arrpg['id'] = 0;
|
|
$result = $arrpg;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function selectByField_pg($field, $value, $table = false)
|
|
{
|
|
$table_table = $this->table;
|
|
if($table){
|
|
$table_table = $table;
|
|
}
|
|
$query = "SELECT * FROM ".$table_table." WHERE $field = '$value'";
|
|
//echo $query."\n";
|
|
return $this->dbpg->fetch_assoc($this->dbpg->query($query));
|
|
}
|
|
|
|
public function process($data)
|
|
{
|
|
$this->_inputData = $data;
|
|
//var_dump($data);
|
|
$eventCalls = false;
|
|
|
|
if($data['operator'] == 'MTS') {
|
|
$data = $this->getAbonent($data);
|
|
// var_dump($data);
|
|
//die();
|
|
}
|
|
|
|
if($data['operator'] == 'MEGAFON') {
|
|
if($data['internal_number'] == '+' && !empty($data['vpbx_id'])){
|
|
$data['internal_number'] = $data['vpbx_id'];
|
|
}
|
|
$this->_inputData = $data;
|
|
}
|
|
|
|
if($_data = $this->checkCalls($data['operator_call_id'], $data['operator'], $data['internal_number'])){
|
|
|
|
$_data['status'] = $_new_data['status'] = $data['status'];
|
|
if($data['release_time'] != 0){
|
|
$_data['release_time'] = $_new_data['release_time'] = $data['release_time'];
|
|
}
|
|
if($data['answer_time'] != 0){
|
|
$_data['answer_time'] = $_new_data['answer_time'] = $data['answer_time'];
|
|
}
|
|
$_data['redirect'] = $_new_data['redirect'] = $data['redirect'];
|
|
|
|
$this->save($_new_data,$_data['id'], $_data['idpg']);
|
|
|
|
if($data['operator'] == 'TELPHIN'){
|
|
$this->saveCall($data);
|
|
} else if($data['operator'] == 'MEGAFON'){
|
|
|
|
$this->saveCallMegafon($data);
|
|
|
|
}
|
|
|
|
} else {
|
|
if($data['operator'] == 'TELPHIN'){
|
|
//var_dump($data);
|
|
$data['uuid_call_id'] = $this->checkID($data);
|
|
|
|
} else if ($data['operator'] == 'MEGAFON'){
|
|
$data['uuid_call_id'] = $data['operator_call_id'];
|
|
} else {
|
|
$data['uuid_call_id'] = (int)str_replace(['callhalf-',':'],'', $data['operator_call_id']);
|
|
}
|
|
$eventCalls = $this->insert($data);
|
|
unset($eventCalls['status']);
|
|
|
|
if(isset($data['user_id'])){
|
|
$user = $this->getUserByField('id', [$data['user_id']]);
|
|
} else {
|
|
$user = $this->getUserByField('vpbx_id', [$data['internal_number'], $data['vpbx_id'], str_replace('@ip.beeline.ru', '', $data['vpbx_id'])]);
|
|
}
|
|
|
|
if(!empty($user)){
|
|
if($user['vpbx_id'] != $data['vpbx_id'] && $user['vpbx_id'] == $data['internal_number'] && $data['operator'] != 'MANGO'){
|
|
$sql = "UPDATE users SET vpbx_id='".$data['vpbx_id']."' WHERE id = " . $user['id'];
|
|
mysql_query($sql);
|
|
}
|
|
$eventCalls['user_id'] = $addData['user_id'] = $user['id'];
|
|
if($user['agency'] == 1){
|
|
$eventCalls['agency_id'] = $addData['agency_id'] = $user['id'];
|
|
} else {
|
|
$eventCalls['agency_id'] = $addData['agency_id'] = $this->getAgency($user);
|
|
}
|
|
|
|
if($eventCalls['agency_id'] == 0){
|
|
$eventCalls['agency_id'] = $eventCalls['user_id'];
|
|
}
|
|
|
|
|
|
|
|
if('+'.str_replace('+','',$data['internal_number']) != '+79258880560' && '+'.str_replace('+','',$data['external_number']) != '+79258880560'){
|
|
$eventCalls['client_id'] = $this->searchClientByPhone($eventCalls);
|
|
}
|
|
|
|
|
|
$this->save($eventCalls,$eventCalls['id'],$eventCalls['idpg']);
|
|
|
|
|
|
}
|
|
|
|
}
|
|
if($eventCalls && $eventCalls['operator'] == 'TELPHIN'){
|
|
$eventCalls['uuid'] = $data['uuid'];
|
|
$this->saveCall($eventCalls);
|
|
} else if ($eventCalls && $eventCalls['operator'] == 'MEGAFON'){
|
|
$eventCalls['uuid'] = $data['uuid'];
|
|
$this->saveCallMegafon($eventCalls);
|
|
}
|
|
|
|
}
|
|
|
|
private function getAbonent($data){
|
|
$res = $data;
|
|
if(isset($data['abonentId'])){
|
|
$abonentId = $data['abonentId'];
|
|
$sqlAbonent = "SELECT * FROM mts_users WHERE mts_id = {$abonentId}";
|
|
$qAbonent = mysql_query($sqlAbonent);
|
|
$rAbonent = mysql_fetch_assoc($qAbonent);
|
|
$res['internal_number'] = $rAbonent['phone'];
|
|
$res['agency_id'] = $rAbonent['agency_id'];
|
|
$res['user_id'] = $rAbonent['user_id'];
|
|
$res['vpbx_id'] = $rAbonent['phone'];
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
private function checkID($data){
|
|
$res = 0;
|
|
$sql = "SELECT id FROM telphin_operator_id WHERE uuid_call_id = '$data[operator_call_id]'";
|
|
|
|
$q = mysql_query($sql);
|
|
if(mysql_num_rows($q) > 0){
|
|
$r = mysql_fetch_assoc($q);
|
|
$res = (int)$r['id'];
|
|
} else {
|
|
$sql_i = "INSERT INTO telphin_operator_id SET uuid_call_id = '$data[operator_call_id]', call_api_id='$data[call_api_id]', uuid='$data[uuid]'";
|
|
|
|
if(mysql_query($sql_i)){
|
|
$res = mysql_insert_id();
|
|
}
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
private function checkUUId($uuid){
|
|
//var_dump($uuid);
|
|
$res = 0;
|
|
$sql = "SELECT id FROM telphin_uuid WHERE uuid = '$uuid'";
|
|
$q = mysql_query($sql);
|
|
if(mysql_num_rows($q) == 0){
|
|
$sql_i = "INSERT INTO telphin_uuid SET uuid = '$uuid'";
|
|
if(mysql_query($sql_i)){
|
|
$res = mysql_insert_id();
|
|
}
|
|
} else {
|
|
$r = mysql_fetch_assoc($q);
|
|
$res = (int)$r['id'];
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
private function saveCall($eventCalls)
|
|
{
|
|
$id_uuid = $this->checkUUId($eventCalls['uuid']);
|
|
//var_dump($id_uuid);
|
|
if($eventCalls['agency_id'] != 12254){
|
|
$query = "SELECT * FROM mango_calls WHERE operator_id = 3 and operator_call_id='".$id_uuid."'";
|
|
|
|
$call = mysql_fetch_assoc(mysql_query($query));
|
|
$call_pg = $this->dbpg->fetch_assoc($this->dbpg->query($query));
|
|
|
|
if(!empty($eventCalls['answer_time'])){
|
|
$disconnect_reason = 'answered';
|
|
} else {
|
|
$disconnect_reason = 'not answered';
|
|
}
|
|
|
|
if($call){
|
|
if($eventCalls['start_time'] == 0){
|
|
$eventCalls['start_time'] = $call['start'];
|
|
}
|
|
if($eventCalls['release_time'] == 0){
|
|
$eventCalls['release_time'] = $call['finish'];
|
|
}
|
|
if($eventCalls['answer_time'] == 0){
|
|
$eventCalls['answer_time'] = $call['answer'];
|
|
}
|
|
if(!isset($eventCalls['agency_id']) || empty($eventCalls['agency_id'])){
|
|
$eventCalls['agency_id'] = $call['agency_id'];
|
|
}
|
|
if(!isset($eventCalls['client_id']) || empty($eventCalls['client_id'])){
|
|
$eventCalls['client_id'] = $call['client_id'];
|
|
}
|
|
if($eventCalls['agency_id'] != 12254){
|
|
$query = "UPDATE `mango_calls` SET
|
|
`start`= " . $eventCalls['start_time'] . ",
|
|
`finish`= " . $eventCalls['release_time'] . ",
|
|
`answer`= " . $eventCalls['answer_time'] . ",
|
|
`from_number`= '" . $eventCalls['external_number'] . "',
|
|
`to_extension`='" . $this->_inputData['vpbx_id'] . "',
|
|
`to_number`='" . $eventCalls['internal_number'] . "',
|
|
`line_number`='" . strtolower($eventCalls['operator']) . "',
|
|
`client_id`='".$eventCalls['client_id']."',
|
|
`agency_id`='" . $eventCalls['agency_id'] . "',
|
|
`disconnect_reason`='" . $disconnect_reason . "' WHERE
|
|
`operator_id` = 3 AND
|
|
`operator_call_id`='".$id_uuid."'
|
|
";
|
|
} else {
|
|
$query = "INSERT INTO `mango_calls` SET
|
|
`start`= " . $eventCalls['start_time'] . ",
|
|
`finish`= " . $eventCalls['release_time'] . ",
|
|
`answer`= " . $eventCalls['answer_time'] . ",
|
|
`from_number`= '" . $eventCalls['external_number'] . "',
|
|
`to_extension`='" . $this->_inputData['vpbx_id'] . "',
|
|
`to_number`='" . $eventCalls['internal_number'] . "',
|
|
`operator_id` = 3,
|
|
`line_number`='" . strtolower($eventCalls['operator']) . "',
|
|
`disconnect_reason`='" . $disconnect_reason . "',
|
|
`agency_id`='" . $eventCalls['agency_id'] . "',
|
|
`uuid`='" . $eventCalls['uuid'] . "',
|
|
`vpbx_id`='".$this->_inputData['vpbx_id']."',
|
|
`client_id`='".$eventCalls['client_id']."',
|
|
`operator_call_id`='".$id_uuid."'
|
|
";
|
|
}
|
|
}
|
|
|
|
if($call_pg){
|
|
if($eventCalls['start_time'] == 0){
|
|
$eventCalls['start_time'] = $call_pg['start'];
|
|
}
|
|
if($eventCalls['release_time'] == 0){
|
|
$eventCalls['release_time'] = $call_pg['finish'];
|
|
}
|
|
if($eventCalls['answer_time'] == 0){
|
|
$eventCalls['answer_time'] = $call_pg['answer'];
|
|
}
|
|
if(!isset($eventCalls['agency_id']) || empty($eventCalls['agency_id'])){
|
|
$eventCalls['agency_id'] = $call_pg['agency_id'];
|
|
}
|
|
if(!isset($eventCalls['client_id']) || empty($eventCalls['client_id'])){
|
|
$eventCalls['client_id'] = $call_pg['client_id'];
|
|
}
|
|
if($eventCalls['agency_id'] != 12254){
|
|
$querypg = "UPDATE mango_calls SET
|
|
start= " . $eventCalls['start_time'] . ",
|
|
finish= " . $eventCalls['release_time'] . ",
|
|
answer= " . $eventCalls['answer_time'] . ",
|
|
from_number= '" . $eventCalls['external_number'] . "',
|
|
to_extension='" . $this->_inputData['vpbx_id'] . "',
|
|
to_number='" . $eventCalls['internal_number'] . "',
|
|
line_number='" . strtolower($eventCalls['operator']) . "',
|
|
client_id='".$eventCalls['client_id']."',
|
|
agency_id='" . $eventCalls['agency_id'] . "',
|
|
disconnect_reason='" . $disconnect_reason . "' WHERE
|
|
operator_id = 3 AND
|
|
operator_call_id='".$id_uuid."'
|
|
";
|
|
} else {
|
|
$querypg = "INSERT INTO mango_calls (
|
|
start,
|
|
finish,
|
|
answer,
|
|
from_number,
|
|
to_extension,
|
|
to_number,
|
|
operator_id,
|
|
line_number,
|
|
disconnect_reason,
|
|
agency_id,
|
|
uuid,
|
|
vpbx_id,
|
|
client_id,
|
|
operator_call_id
|
|
) VALUES (
|
|
" . $eventCalls['start_time'] . ",
|
|
" . $eventCalls['release_time'] . ",
|
|
" . $eventCalls['answer_time'] . ",
|
|
'" . $eventCalls['external_number'] . "',
|
|
'" . $this->_inputData['vpbx_id'] . "',
|
|
'" . $eventCalls['internal_number'] . "',
|
|
3,
|
|
'" . strtolower($eventCalls['operator']) . "',
|
|
'" . $disconnect_reason . "',
|
|
'" . $eventCalls['agency_id'] . "',
|
|
'" . $eventCalls['uuid'] . "',
|
|
'".$this->_inputData['vpbx_id']."',
|
|
'".$eventCalls['client_id']."',
|
|
'{$id_uuid}'
|
|
)
|
|
";
|
|
}
|
|
}
|
|
}
|
|
//echo $query;
|
|
mysql_query($query);
|
|
$this->dbpg->query($querypg);
|
|
}
|
|
|
|
|
|
//Сохранение звонка Мегафон
|
|
private function saveCallMegafon($eventCalls){
|
|
|
|
//var_dump($this->_inputData['user']);
|
|
$query = "SELECT * FROM mango_calls WHERE operator_id = 4 and operator_call_id='".$eventCalls['operator_call_id']."'";
|
|
|
|
//$call = mysql_fetch_assoc(mysql_query($query));
|
|
|
|
$call_pg = $this->dbpg->fetch_assoc($this->dbpg->query($query));
|
|
|
|
if(!empty($eventCalls['answer_time'])){
|
|
$disconnect_reason = 'answered';
|
|
} else {
|
|
$disconnect_reason = 'not answered';
|
|
}
|
|
|
|
$to_number = $eventCalls['internal_number'];
|
|
//var_dump($eventCalls['agency_id']);
|
|
if($eventCalls['agency_id'] > 0){
|
|
$sql_u = "SELECT * FROM agency_megafon WHERE agency_id=$eventCalls[agency_id]";
|
|
// echo $sql_u;
|
|
$q_u = mysql_query($sql_u);
|
|
$r_u = mysql_fetch_assoc($q_u);
|
|
if($r_u){
|
|
$url_main = explode("/", str_replace("https://", "", $r_u['url']))[0];
|
|
$to_number = $this->_inputData['user'].'@'.$url_main;
|
|
echo $to_number;
|
|
}
|
|
}
|
|
|
|
/* if($call){
|
|
if($eventCalls['start_time'] == 0){
|
|
$eventCalls['start_time'] = $call['start'];
|
|
}
|
|
if($eventCalls['release_time'] == 0){
|
|
$eventCalls['release_time'] = $call['finish'];
|
|
}
|
|
if($eventCalls['answer_time'] == 0){
|
|
$eventCalls['answer_time'] = $call['answer'];
|
|
}
|
|
if(!isset($eventCalls['agency_id']) || empty($eventCalls['agency_id'])){
|
|
$eventCalls['agency_id'] = $call['agency_id'];
|
|
}
|
|
if(!isset($eventCalls['client_id']) || empty($eventCalls['client_id'])){
|
|
$eventCalls['client_id'] = $call['client_id'];
|
|
}
|
|
$query = "UPDATE `mango_calls` SET
|
|
`start`= " . $eventCalls['start_time'] . ",
|
|
`finish`= " . $eventCalls['release_time'] . ",
|
|
`answer`= " . $eventCalls['answer_time'] . ",
|
|
`from_number`= '" . $eventCalls['external_number'] . "',
|
|
`line_number`='" . strtolower($eventCalls['operator']) . "',
|
|
`client_id`='".$eventCalls['client_id']."',
|
|
`agency_id`='" . $eventCalls['agency_id'] . "',
|
|
`disconnect_reason`='" . $disconnect_reason . "' WHERE
|
|
`operator_id` = 4 AND
|
|
`operator_call_id`='".$eventCalls['operator_call_id']."'
|
|
";
|
|
} else {
|
|
$query = "INSERT INTO `mango_calls` SET
|
|
`start`= " . $eventCalls['start_time'] . ",
|
|
`finish`= " . $eventCalls['release_time'] . ",
|
|
`answer`= " . $eventCalls['answer_time'] . ",
|
|
`from_number`= '" . $eventCalls['external_number'] . "',
|
|
`to_extension`='" . $to_number . "',
|
|
`to_number`='" . $to_number . "',
|
|
`operator_id` = 4,
|
|
`line_number`='" . strtolower($eventCalls['operator']) . "',
|
|
`disconnect_reason`='" . $disconnect_reason . "',
|
|
`agency_id`='" . $eventCalls['agency_id'] . "',
|
|
`uuid`='" . $eventCalls['uuid'] . "',
|
|
`vpbx_id`='".$this->_inputData['vpbx_id']."',
|
|
`client_id`='".$eventCalls['client_id']."',
|
|
`operator_call_id`=".$eventCalls['operator_call_id']."
|
|
";
|
|
}
|
|
// echo $query;
|
|
mysql_query($query);*/
|
|
|
|
if($call_pg){
|
|
if($eventCalls['start_time'] == 0){
|
|
$eventCalls['start_time'] = $call_pg['start'];
|
|
}
|
|
if($eventCalls['release_time'] == 0){
|
|
$eventCalls['release_time'] = $call_pg['finish'];
|
|
}
|
|
if($eventCalls['answer_time'] == 0){
|
|
$eventCalls['answer_time'] = $call_pg['answer'];
|
|
}
|
|
if(!isset($eventCalls['agency_id']) || empty($eventCalls['agency_id'])){
|
|
$eventCalls['agency_id'] = $call_pg['agency_id'];
|
|
}
|
|
if(!isset($eventCalls['client_id']) || empty($eventCalls['client_id'])){
|
|
$eventCalls['client_id'] = $call_pg['client_id'];
|
|
}
|
|
$query_pg = "UPDATE mango_calls SET
|
|
start= " . $eventCalls['start_time'] . ",
|
|
finish= " . $eventCalls['release_time'] . ",
|
|
answer= " . $eventCalls['answer_time'] . ",
|
|
from_number= '" . $eventCalls['external_number'] . "',
|
|
line_number='" . strtolower($eventCalls['operator']) . "',
|
|
client_id='".$eventCalls['client_id']."',
|
|
agency_id='" . $eventCalls['agency_id'] . "',
|
|
disconnect_reason='" . $disconnect_reason . "' WHERE
|
|
operator_id = 4 AND
|
|
operator_call_id='".$eventCalls['operator_call_id']."'
|
|
";
|
|
} else {
|
|
$query_pg = "INSERT INTO mango_calls SET
|
|
start= " . $eventCalls['start_time'] . ",
|
|
finish= " . $eventCalls['release_time'] . ",
|
|
answer= " . $eventCalls['answer_time'] . ",
|
|
from_number= '" . $eventCalls['external_number'] . "',
|
|
to_extension='" . $to_number . "',
|
|
to_number='" . $to_number . "',
|
|
operator_id = 4,
|
|
line_number='" . strtolower($eventCalls['operator']) . "',
|
|
disconnect_reason='" . $disconnect_reason . "',
|
|
agency_id='" . $eventCalls['agency_id'] . "',
|
|
uuid='" . $eventCalls['uuid'] . "',
|
|
vpbx_id='".$this->_inputData['vpbx_id']."',
|
|
client_id='".$eventCalls['client_id']."',
|
|
operator_call_id='".$eventCalls['operator_call_id']."'
|
|
";
|
|
}
|
|
$this->dbpg->query($query_pg);
|
|
}
|
|
|
|
private function getAgency($user){
|
|
if($user['agency'] == 1) return $user['id'];
|
|
if($user['id_manager'] > 0){
|
|
$query = "SELECT * FROM users WHERE id = ".$user['id_manager'];
|
|
$result = mysql_fetch_assoc(mysql_query($query));
|
|
if($result['agency'] == 1) return $result['id'];
|
|
return $this->getAgency($result);
|
|
}
|
|
}
|
|
private function checkCalls($value, $operator, $internal_number)
|
|
{
|
|
if($operator == 'TELPHIN'){
|
|
|
|
$table = 'telphin_operator_id';
|
|
$valArr = $this->selectByField('uuid_call_id', $value, $table);
|
|
if($valArr){
|
|
return $this->selectByField('uuid_call_id', $valArr['id']);
|
|
} else {
|
|
return $valArr;
|
|
}
|
|
} else if($operator == 'MEGAFON'){
|
|
$result = false;
|
|
$table_table = $this->table;
|
|
$query = "SELECT * FROM ".$table_table." WHERE uuid_call_id = '$value' AND internal_number = '{$internal_number}'";
|
|
// echo $query."\n";
|
|
$arr = mysql_fetch_assoc(mysql_query($query));
|
|
$arrpg = $this->dbpg->fetch_assoc($this->dbpg->query($query));
|
|
if(!empty($arr)){
|
|
$arr['idpg'] = (int)$arrpg['id'];
|
|
$result = $arr;
|
|
} else if (!empty($arrpg)){
|
|
$arrpg['idpg'] = $arrpg['id'];
|
|
$arrpg['id'] = 0;
|
|
$result = $arrpg;
|
|
}
|
|
return $result;
|
|
//return $this->selectByField('uuid_call_id', $value);
|
|
} else if ($operator == 'MANGO'){
|
|
return $this->selectByField('operator_call_id', $value);
|
|
|
|
} else {
|
|
$val = (int)str_replace(['callhalf-',':'],'', $value);
|
|
return $this->selectByField('uuid_call_id', $val);
|
|
}
|
|
}
|
|
|
|
protected function getUserByField($field, $value)
|
|
{
|
|
if(is_array($value)){
|
|
foreach($value as $_v){
|
|
$_ar[] = "'$_v'";
|
|
}
|
|
$query = "SELECT * FROM users WHERE $field in (".implode(',', $_ar).") LIMIT 1";
|
|
}
|
|
else $query = "SELECT * FROM users WHERE $field = '$value' LIMIT 1";
|
|
// echo $query;
|
|
return mysql_fetch_assoc(mysql_query($query));
|
|
}
|
|
protected function getUsersByField($field, $value)
|
|
{
|
|
|
|
if(is_array($value)){
|
|
foreach($value as $_v){
|
|
$_ar[] = "'$_v'";
|
|
}
|
|
$query = "SELECT * FROM users WHERE $field in (".implode(',', $_ar).")";
|
|
} else {
|
|
$query = "SELECT * FROM users WHERE $field = '$value'";
|
|
}
|
|
|
|
$result = mysql_query($query);
|
|
$list = [];
|
|
|
|
if(mysql_num_rows($result) >0){
|
|
while($row = mysql_fetch_assoc($result)){
|
|
|
|
$list[] = $row;
|
|
}
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
public function funnel_tel($agency_id){
|
|
$funnel_telephony = array();
|
|
$sql_f_t = "SELECT * FROM funnel_telephony WHERE agency_id = {$agency_id}";
|
|
$q_f_t = mysql_query($sql_f_t);
|
|
while($r_f_t = mysql_fetch_assoc($q_f_t)){
|
|
$funnel_telephony[] = $r_f_t;
|
|
}
|
|
if(!empty($funnel_telephony)){
|
|
$this->funnel_telephony = $funnel_telephony;
|
|
}
|
|
}
|
|
|
|
protected function searchClientByPhone($data)
|
|
{
|
|
|
|
if(($data['agency_id']== 15401 && '+'.str_replace('+','',$this->_inputData['internal_number']) != '+79215648879')
|
|
|| $data['agency_id'] == 18205){
|
|
die();
|
|
}
|
|
$phone = $data['external_number'];
|
|
$user_id = $data['agency_id'];
|
|
$users = [];
|
|
|
|
$w = new WebHookIn();
|
|
$users = $w->getAllUserAgency($user_id);
|
|
|
|
//$users = array_keys($this->getAllUserAgency($user_id, $users));
|
|
|
|
$sql = "SELECT `id`, `stage`, `deleted`, `source` FROM `clients` WHERE cancel = 0 AND (phone = '$phone' OR id in (SELECT client_id FROM clients_phone2 WHERE phone = '$phone')) and (id_agent in (".implode(',', $users)."))";
|
|
//file_put_contents($_SERVER['DOCUMENT_ROOT']."/callevents/log_query.txt", $sql."\n", FILE_APPEND);
|
|
if($data['agency_id'] == 8826){
|
|
if($data['user_id'] == 11868) usleep(1000000);
|
|
if($data['user_id'] == 11864) usleep(1500000);
|
|
} else if ($data['agency_id'] == 12061){
|
|
if($data['user_id'] == 17835) usleep(1000000);
|
|
if($data['user_id'] == 12660) usleep(1500000);
|
|
if($data['user_id'] == 12064) usleep(2000000);
|
|
if($data['user_id'] == 12710) usleep(500000);
|
|
if($data['user_id'] == 14648) usleep(1700000);
|
|
if($data['user_id'] == 12659) usleep(2100000);
|
|
}
|
|
$client = mysql_fetch_assoc(mysql_query($sql));
|
|
if(empty($client) && $data['agency_id'] != 11456){
|
|
$client['phone'] = $phone;
|
|
$client['fio'] = "Новый клиент " . $this->_inputData['operator_rus'];
|
|
if($data['agency_id'] == 9588){
|
|
$client['fio'] = "Новый клиент ".$phone;
|
|
} else if ($data['agency_id'] == 12061) {
|
|
$client['fio'] = "Новый клиент " . $this->_inputData['operator_rus'] . " " . $phone;
|
|
}
|
|
$client['opis'] = "Клиент записан по входящему звонку в ".$this->_inputData['operator'];
|
|
$client['date_add'] = date('Y-m-d H:i:s');
|
|
$client['id_agent'] = $data['user_id'];
|
|
$client['who_work'] = $data['user_id'];
|
|
$client['stage'] = 1;
|
|
$client['confirm'] = 1;
|
|
if ($data['agency_id'] == 13154 && '+'.str_replace('+','',$this->_inputData['internal_number']) == '+79111111702'){
|
|
$client['who_work'] = 0;
|
|
$client['confirm'] = 0;
|
|
$client['source'] = 1678;
|
|
|
|
}
|
|
|
|
if ($data['agency_id'] == 12461){
|
|
$client['who_work'] = 0;
|
|
$client['confirm'] = 0;
|
|
}
|
|
|
|
if($data['agency_id'] == 9588){
|
|
$client['funnel_id'] = 340;
|
|
} else if($data['agency_id'] == 10732){
|
|
$client['funnel_id'] = 441;
|
|
} else if ($data['agency_id'] == 12461){
|
|
$funnel_id = 1854;
|
|
}
|
|
$funnel_id = 0;
|
|
$this->funnel_tel($data['agency_id']);
|
|
if(!empty($this->funnel_telephony)){
|
|
foreach($this->funnel_telephony as $funnel_t){
|
|
if($funnel_t['phone'] == '+'.str_replace('+','',$this->_inputData['internal_number'])){
|
|
if((int)$funnel_t['funnel_id'] > 0){
|
|
$funnel_id = (int)$funnel_t['funnel_id'];
|
|
|
|
}
|
|
}
|
|
}
|
|
} else if ($data['agency_id'] == 10387) {
|
|
$funnel_id = 484;
|
|
} else if($data['agency_id'] == 8435) {
|
|
$funnel_id = 503;
|
|
} else if ($data['agency_id'] == 7384){
|
|
$funnel_id = 75;
|
|
} else if ($data['agency_id'] == 8826 || $data['agency_id'] == 12061) {
|
|
$sourse_number = '+'.str_replace('+','',$this->_inputData['internal_number']);
|
|
if($this->_inputData['operator'] == 'MEGAFON' && !empty($this->_inputData['transit_number'])){
|
|
$sourse_number = '+'.str_replace('+','',$this->_inputData['transit_number']);
|
|
}
|
|
$resS = $this->findSource($sourse_number, $data['agency_id']);
|
|
if(isset($resS['id'])){
|
|
$responce['source'] = $resS['id'];
|
|
} else {
|
|
$responce['source'] = 0;
|
|
}
|
|
$client['source'] = $responce['source'];
|
|
} else if($data['agency_id'] == 14191){
|
|
$funnel_id = 844;
|
|
} else if($data['agency_id'] == 16001){
|
|
$funnel_id = 994;
|
|
} else if($data['agency_id'] == 12028){
|
|
$funnel_id = 996;
|
|
} else if($data['agency_id'] == 17460){
|
|
$funnel_id = 1159;
|
|
} else if($data['agency_id'] == 17664){
|
|
$funnel_id = 1180;
|
|
} else if($data['agency_id'] == 17328){
|
|
$funnel_id = 1183;
|
|
} else if($data['agency_id'] == 19322){
|
|
$funnel_id = 1447;
|
|
} else if ($data['agency_id'] == 21213) {
|
|
$funnel_id = 1793;
|
|
} else if ($data['agency_id'] == 20624){
|
|
$funnel_id = 1832;
|
|
} else if ($data['agency_id'] == 18631){
|
|
$funnel_id = 1350;
|
|
} else if ($data['agency_id'] == 22432){
|
|
$funnel_id = 2118;
|
|
} else if($data['agency_id'] == 22311){
|
|
$funnel_id = 2106;
|
|
} else if($data['agency_id'] == 22604){
|
|
$funnel_id = 2184;
|
|
} else if($data['agency_id'] == 18846){
|
|
$funnel_id = 2197;
|
|
}
|
|
|
|
if($funnel_id > 0){
|
|
$client['funnel_id'] = $funnel_id;
|
|
}
|
|
if($data['agency_id'] == 12343 && '+'.str_replace('+','',$this->_inputData['internal_number']) == '+79284369993'){
|
|
$client['id'] = $this->insertClient($client);
|
|
} else {
|
|
$client['id'] = $this->insertClient($client);
|
|
}
|
|
if($data['agency_id'] == 9588){
|
|
$clientClass = new Clients;
|
|
$clientClass->get_class_etap($client['id'], 340);
|
|
} else
|
|
if($data['agency_id'] == 10732){
|
|
$clientClass = new Clients;
|
|
$clientClass->get_class_etap($client['id'], 441);
|
|
}
|
|
if($funnel_id > 0){
|
|
$clientClass = new Clients;
|
|
$clientClass->get_class_etap($client['id'], $funnel_id);
|
|
}
|
|
if($this->_inputData['operator'] == 'MTS' && $data['agency_id'] == 12061){
|
|
/*** */
|
|
} else
|
|
if($data['agency_id'] != 7864 && $data['agency_id'] != 8826 && $data['agency_id'] != 11082 && $data['agency_id'] != 12254 && $data['agency_id'] != 12343 && $data['agency_id'] != 12482 && $data['agency_id'] != 12762
|
|
&& $data['agency_id'] != 13285 && $data['agency_id'] != 12556 && $data['agency_id'] != 13394 && $data['agency_id'] != 13430 && $data['agency_id'] != 13934 && $data['agency_id'] != 14191
|
|
&& $data['agency_id'] != 14310 && $data['agency_id'] != 12061 && $data['agency_id'] != 14700 && $data['agency_id'] !=11417 && $data['agency_id'] != 17328 && $data['agency_id'] !=16001 && $data['agency_id'] !=16444
|
|
&& $data['agency_id'] != 16490 && $data['agency_id'] != 14548 && $data['agency_id'] != 17157 && $data['agency_id'] != 17832 && $data['agency_id'] != 18455 && $data['agency_id'] != 18785
|
|
&& $data['agency_id'] != 16553 && $data['agency_id'] != 19111 && $data['agency_id'] != 19322 && $data['agency_id'] != 18708 && $data['agency_id'] != 20406 && $data['agency_id'] != 20382 && $data['agency_id'] != 20624
|
|
&& $data['agency_id'] != 12461 && $data['agency_id'] != 18631 && $data['agency_id'] != 21951 && $data['agency_id'] != 4 && $data['agency_id'] != 22432 && $data['agency_id'] != 18846 && $data['agency_id'] != 23103
|
|
&& $data['agency_id'] != 23065){
|
|
$req['client_id'] = $client['id'];
|
|
$req['user_id'] = $data['user_id'];
|
|
$req['who_work'] = $data['user_id'];
|
|
$req['stage'] = 1;
|
|
$req['confirm'] = 1;
|
|
$req['integration'] = 2;
|
|
$req['call_id'] = $this->_inputData['operator_call_id'];
|
|
|
|
$req['type_id'] = 1; // @todo: возможно будет изменяться в будующем
|
|
if($data['agency_id'] == 20669){
|
|
$req['type_id'] = 2;
|
|
}
|
|
$nameDef = "Новая заявка ".$this->_inputData['operator_rus'];
|
|
if($data['agency_id'] == 9588){
|
|
|
|
$nameDef = "Новая заявка ".$phone;
|
|
} else if($data['agency_id'] == 10732){
|
|
if('+'.str_replace('+','',$this->_inputData['internal_number']) == '+79673597249' || '+'.str_replace('+','',$this->_inputData['internal_number']) == '+79673400161'){
|
|
$nameDef = "Новый кандидат";
|
|
}
|
|
}
|
|
|
|
switch ($req['type_id']) {
|
|
case 1:
|
|
{
|
|
$req['name'] = "Подбор объекта для " . $client['fio'];
|
|
if($data['agency_id'] == 10732){
|
|
if('+'.str_replace('+','',$this->_inputData['internal_number']) == '+79673597249' || '+'.str_replace('+','',$this->_inputData['internal_number']) == '+79673400161'){
|
|
$req['name'] = "Новый кандидат";
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
case 2:
|
|
$req['name'] = "Реализация объекта для " . $client['fio'];
|
|
break;
|
|
case 3:
|
|
$req['name'] = "Другие услуги для " . $client['fio'];
|
|
break;
|
|
case 4:
|
|
$req['name'] = "Спрос по нашему объекту для " . $client['fio'];
|
|
break;
|
|
default :
|
|
$req['name'] = $nameDef;
|
|
}
|
|
|
|
if($data['agency_id'] == 9588){
|
|
$req['funnel_id'] = 340;
|
|
}
|
|
if($data['agency_id'] == 10732){
|
|
$req['funnel_id'] = 441;
|
|
}
|
|
if($data['agency_id'] == 10387){
|
|
$funnel_id = 482;
|
|
}
|
|
if($data['agency_id'] == 11082){
|
|
$funnel_id = 479;
|
|
}
|
|
if($data['agency_id'] == 8435){
|
|
$funnel_id = 503;
|
|
}
|
|
else if($data['agency_id'] == 10429) {
|
|
$funnel_id = 551;
|
|
} else if($data['agency_id'] == 12061) {
|
|
$funnel_id = 573;
|
|
} else if($data['agency_id'] == 13339){
|
|
$funnel_id = 665;
|
|
} else if($data['agency_id'] == 12569){
|
|
$funnel_id = 742;
|
|
} else if($data['agency_id'] == 13386){
|
|
$funnel_id = 796;
|
|
} else if($data['agency_id'] == 12028){
|
|
$funnel_id = 997;
|
|
} else if($data['agency_id'] == 17460){
|
|
$funnel_id = 1159;
|
|
} else if($data['agency_id'] == 17664){
|
|
$funnel_id = 1180;
|
|
} else if($data['agency_id'] == 19322){
|
|
$funnel_id = 1447;
|
|
} else if($data['agency_id'] == 19843){
|
|
$funnel_id = 1614;
|
|
} else if($data['agency_id'] == 20669){
|
|
$funnel_id = 1675;
|
|
} else if($data['agency_id'] == 19909){
|
|
$funnel_id = 1613;
|
|
} else if($data['agency_id'] == 19570) {
|
|
$funnel_id = 1484;
|
|
} else if ($data['agency_id'] == 21213) {
|
|
$funnel_id = 1793;
|
|
} else if ($data['agency_id'] == 20624){
|
|
$funnel_id = 1832;
|
|
} else if ($data['agency_id'] == 20947){
|
|
$funnel_id = 1819;
|
|
} else if ($data['agency_id'] == 21422){
|
|
$funnel_id = 1858;
|
|
} else if ($data['agency_id'] == 21790) {
|
|
$funnel_id = 1980;
|
|
} else if ($data['agency_id'] == 19750) {
|
|
$funnel_id = 1985;
|
|
} else if($data['agency_id'] == 22311){
|
|
$funnel_id = 2106;
|
|
} else if($data['agency_id'] == 18479){
|
|
$funnel_id = 2156;
|
|
} else if($data['agency_id'] == 22604){
|
|
$funnel_id = 2184;
|
|
} else if ($data['agency_id'] == 22666) {
|
|
$funnel_id = 2096;
|
|
} else if($data['agency_id'] == 22983){
|
|
$funnel_id = 2175;
|
|
} else if($data['agency_id'] == 23156){
|
|
|
|
$funnel_id = 2299;
|
|
}else if ($data['agency_id'] == 23360){
|
|
$funnel_id = 2424;
|
|
}
|
|
|
|
if($funnel_id > 0){
|
|
$req['funnel_id'] = $funnel_id;
|
|
}
|
|
$source = 0;
|
|
|
|
if(str_replace(['+7', '@ip.beeline.ru'], '', $data['internal_number']) != 'SIP00SKIU0133O' && str_replace(['+7', '@ip.beeline.ru'], '', $data['internal_number']) != 'SIP00SKIU00QVJ') {
|
|
$req['description'] = "Заявка создана по входящему звонку в ".$this->_inputData['operator'];
|
|
|
|
$sourse_number = '+'.str_replace('+','',$this->_inputData['internal_number']);
|
|
if($this->_inputData['operator'] == 'MEGAFON' && !empty($this->_inputData['transit_number'])){
|
|
$sourse_number = '+'.str_replace('+','',$this->_inputData['transit_number']);
|
|
}
|
|
$resS = $this->findSource($sourse_number, $data['agency_id']);
|
|
if(isset($resS['id'])){
|
|
$responce['source'] = $resS['id'];
|
|
} else {
|
|
$responce['source'] = 0;
|
|
}
|
|
|
|
if(isset($responce['source']) && $responce['source'] > 0){
|
|
$source = $responce['source'];
|
|
}
|
|
|
|
$req['source'] = $source;
|
|
if ($data['agency_id'] == 13154 && '+'.str_replace('+','',$this->_inputData['internal_number']) == '+79111111702'){
|
|
$req['who_work'] = 0;
|
|
$req['confirm'] = 0;
|
|
$req['source'] = 1678;
|
|
|
|
}
|
|
|
|
|
|
$req['id'] = $this->insertReq($req);
|
|
if($data['agency_id'] == 13339){
|
|
// file_put_contents(__DIR__.'/log_test_megafon.txt', $req['id']. "\n", FILE_APPEND);
|
|
|
|
$sql_in_ev = "INSERT INTO user_client_events (user_id, req_id, create_date, schedule_date, `type`, comment, calendar_view, from_id) ".
|
|
"VALUES ({$data['user_id']}, {$req['id']}, '".date('Y-m-d H:i:s')."', '".date('Y-m-d H:i:s', strtotime('+30 minutes'))."', 'call', '".$req['description']."', 0, {$data['user_id']})";
|
|
//file_put_contents(__DIR__.'/log_test_megafon.txt', $sql_in_ev. "\n", FILE_APPEND);
|
|
mysql_query($sql_in_ev);
|
|
}
|
|
if($data['agency_id'] == 9588){
|
|
$pdo = new MysqlPdo(hst, ndb, user, pass);
|
|
$sql = "SET NAMES 'utf8'";
|
|
$pdo->query($sql);
|
|
$reqClass = new Requisitions($pdo);
|
|
|
|
$reqClass->get_class_etap($req['id'], 340);
|
|
} else
|
|
if($data['agency_id'] == 10732){
|
|
$reqClass = new Requisitions();
|
|
$reqClass->get_class_etap($req['id'], 441);
|
|
}
|
|
if($funnel_id > 0){
|
|
$reqClass = new Requisitions();
|
|
$reqClass->get_class_etap($req['id'], $funnel_id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
if($this->_inputData['operator'] == 'MTS' && $data['agency_id'] == 12061){
|
|
/*** */
|
|
} else
|
|
if($data['agency_id'] != 7864 && $data['agency_id'] != 11456 && $data['agency_id'] != 8826 && $data['agency_id'] != 11082 && $data['agency_id'] != 12254 && $data['agency_id'] != 12343 && $data['agency_id'] != 12482 && $data['agency_id'] != 12762 && $data['agency_id'] != 13285
|
|
&& $data['agency_id'] != 12556 && $data['agency_id'] != 13394 && $data['agency_id'] != 13430 && $data['agency_id'] != 13934 && $data['agency_id'] != 14191 && $data['agency_id'] != 14310 && $data['agency_id'] != 12061
|
|
&& $data['agency_id'] != 14700 && $data['agency_id'] !=11417 && $data['agency_id'] != 17328 && $data['agency_id'] !=16001 && $data['agency_id'] !=16444 && $data['agency_id'] != 16490
|
|
&& $data['agency_id'] != 14548 && $data['agency_id'] != 17157 && $data['agency_id'] != 17832 && $data['agency_id'] != 18455 && $data['agency_id'] != 18785 && $data['agency_id'] != 16553 && $data['agency_id'] != 19111 &&
|
|
$data['agency_id'] != 19322 && $data['agency_id'] != 18708 && $data['agency_id'] != 20406 && $data['agency_id'] != 20382 && $data['agency_id'] != 20624 && $data['agency_id'] != 12461 && $data['agency_id'] != 18631 &&
|
|
$data['agency_id'] != 21951 && $data['agency_id'] != 4 && $data['agency_id'] != 22432 && $data['agency_id'] != 18846 && $data['agency_id'] != 23103 && $data['agency_id'] != 23065) {
|
|
$this->checkReq($data, $client['id']);
|
|
}
|
|
if ($client['stage'] == 8 or $client['deleted'] == 1) {
|
|
$sql = "UPDATE `clients` SET stage = 1, deleted = 0, who_work = ".$data['user_id']." WHERE id = ".$client['id'];
|
|
mysql_query($sql);
|
|
}
|
|
|
|
if($data['agency_id'] == 8826 || $data['agency_id'] == 12061){
|
|
if((int)$client['source'] == 0 ){
|
|
$sourse_number = '+'.str_replace('+','',$this->_inputData['internal_number']);
|
|
if($this->_inputData['operator'] == 'MEGAFON' && !empty($this->_inputData['transit_number'])){
|
|
$sourse_number = '+'.str_replace('+','',$this->_inputData['transit_number']);
|
|
}
|
|
$resS = $this->findSource($sourse_number, $data['agency_id']);
|
|
if(isset($resS['id'])){
|
|
$source = $resS['id'];
|
|
} else {
|
|
$source = 0;
|
|
}
|
|
//$source = $this->findSource('+'.str_replace('+','',$this->_inputData['internal_number']), $data['agency_id']);
|
|
if($source > 0){
|
|
$sql = "UPDATE `clients` SET source='{$source}' WHERE id = ".$client['id'];
|
|
mysql_query($sql);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
return $client['id'];
|
|
}
|
|
|
|
protected function insertClient($client)
|
|
{
|
|
$query = "INSERT INTO clients SET ";
|
|
$query_set = array();
|
|
|
|
foreach($client as $field => $value){
|
|
$query_set[] = $field." = '". $value."'";
|
|
}
|
|
$query .= implode(', ', $query_set);
|
|
|
|
mysql_query($query);
|
|
return mysql_insert_id();
|
|
}
|
|
|
|
protected function insertReq($req)
|
|
{
|
|
$query = "INSERT INTO requisitions SET ";
|
|
$query_set = array();
|
|
|
|
foreach($req as $field => $value){
|
|
$query_set[] = $field." = '". $value."'";
|
|
}
|
|
$query .= implode(', ', $query_set);
|
|
|
|
mysql_query($query);
|
|
return mysql_insert_id();
|
|
}
|
|
|
|
protected function checkReq($data, $client_id)
|
|
{
|
|
$user_id = $data['user_id'];
|
|
$sql = "SELECT id, name FROM requisitions WHERE client_id = $client_id AND who_work = $user_id AND deleted = 0 AND cancel = 0";
|
|
if($data['agency_id'] == 13154){
|
|
$sql = "SELECT id, name FROM requisitions WHERE client_id = $client_id AND who_work = $user_id AND cancel = 0";
|
|
}
|
|
$q = mysql_query($sql);
|
|
if(mysql_num_rows($q) == 0){
|
|
|
|
$req['type_id'] = 1; // @todo: возможно будет изменяться в будующем
|
|
if($data['agency_id'] == 20669){
|
|
$req['type_id'] = 2;
|
|
}
|
|
$nameDef = "Новая заявка ".$this->_inputData['operator_rus'];
|
|
$sql = "SELECT * FROM `clients` WHERE `id` = $client_id";
|
|
if ($client = mysql_fetch_assoc(mysql_query($sql))) {
|
|
|
|
if($data['agency_id'] == 9588){
|
|
$client['fio'] = $client['fio']." ".$client['phone'];
|
|
$nameDef = "Новая заявка ".$client['phone'];
|
|
}
|
|
switch ($req['type_id']) {
|
|
case 1:
|
|
{
|
|
$req['name'] = "Подбор объекта для " . $client['fio'];
|
|
if($data['agency_id'] == 10732){
|
|
if('+'.str_replace('+','',$this->_inputData['internal_number']) == '+79673597249' || '+'.str_replace('+','',$this->_inputData['internal_number']) == '+79673400161'){
|
|
$req['name'] = "Новый кандидат";
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
case 2:
|
|
$req['name'] = "Реализация объекта для " . $client['fio'];
|
|
break;
|
|
case 3:
|
|
$req['name'] = "Другие услуги для " . $client['fio'];
|
|
break;
|
|
case 4:
|
|
$req['name'] = "Спрос по нашему объекту для " . $client['fio'];
|
|
break;
|
|
default :
|
|
$req['name'] = $nameDef;
|
|
}
|
|
} else {
|
|
$req['name'] = $nameDef;
|
|
}
|
|
|
|
$req['client_id'] = $client_id;
|
|
$req['user_id'] = $user_id;
|
|
$req['who_work'] = $user_id;
|
|
$req['stage'] = 1;
|
|
$req['confirm'] = 1;
|
|
$req['integration'] = 2;
|
|
|
|
$req['call_id'] = $this->_inputData['operator_call_id'];
|
|
|
|
$req['description'] = "Заявка создана по входящему звонку в ".$this->_inputData['operator'];
|
|
|
|
if($data['agency_id'] == 9588){
|
|
$req['funnel_id'] = 340;
|
|
} else if($data['agency_id'] == 10732){
|
|
|
|
$req['funnel_id'] = 441;
|
|
} else if($data['agency_id'] == 8435){
|
|
$req['funnel_id'] = 503;
|
|
} else if($data['agency_id'] == 17460){
|
|
$funnel_id = 1159;
|
|
} else if($data['agency_id'] == 19322){
|
|
$funnel_id = 1447;
|
|
}else if($data['agency_id'] == 19843){
|
|
$funnel_id = 1614;
|
|
} else if($data['agency_id'] == 20669){
|
|
$funnel_id = 1675;
|
|
} else if ($data['agency_id'] == 21213) {
|
|
$funnel_id = 1793;
|
|
} else if ($data['agency_id'] == 20624){
|
|
$funnel_id = 1832;
|
|
} else if ($data['agency_id'] == 20553){
|
|
$funnel_id = 1897;
|
|
}
|
|
|
|
$this->funnel_tel($data['agency_id']);
|
|
$funnel_id = 0;
|
|
if(!empty($this->funnel_telephony)){
|
|
foreach($this->funnel_telephony as $funnel_t){
|
|
if($funnel_t['phone'] == '+'.str_replace('+','',$this->_inputData['internal_number'])){
|
|
if((int)$funnel_t['funnel_id'] > 0){
|
|
$funnel_id = (int)$funnel_t['funnel_id'];
|
|
|
|
}
|
|
}
|
|
}
|
|
} else if($data['agency_id'] == 10387){
|
|
$funnel_id = 482;
|
|
} else if($data['agency_id'] == 11082){
|
|
$funnel_id = 479;
|
|
} else if($data['agency_id'] == 10429){
|
|
$funnel_id = 551;
|
|
} else if ($data['agency_id'] == 7384){
|
|
$funnel_id = 75;
|
|
} else if($data['agency_id'] == 12061) {
|
|
$funnel_id = 573;
|
|
} else if($data['agency_id'] == 13339){
|
|
$funnel_id = 665;
|
|
} else if($data['agency_id'] == 12569){
|
|
$funnel_id = 742;
|
|
} else if($data['agency_id'] == 13386){
|
|
$funnel_id = 796;
|
|
} else if($data['agency_id'] == 12028){
|
|
$funnel_id = 997;
|
|
} else if($data['agency_id'] == 17664){
|
|
$funnel_id = 1180;
|
|
} else if($data['agency_id'] == 19909){
|
|
$funnel_id = 1613;
|
|
} else if($data['agency_id'] == 19570) {
|
|
$funnel_id = 1484;
|
|
} else if ($data['agency_id'] == 21213) {
|
|
$funnel_id = 1793;
|
|
} else if ($data['agency_id'] == 20947) {
|
|
$funnel_id = 1819;
|
|
} else if ($data['agency_id'] == 21422){
|
|
$funnel_id = 1858;
|
|
} else if ($data['agency_id'] == 20553){
|
|
$funnel_id = 1897;
|
|
} else if ($data['agency_id'] == 21790) {
|
|
$funnel_id = 1980;
|
|
} else if ($data['agency_id'] == 19750) {
|
|
$funnel_id = 1985;
|
|
} else if ($data['agency_id'] == 22746){
|
|
$funnel_id = 2155;
|
|
} else if($data['agency_id'] == 22604){
|
|
$funnel_id = 2184;
|
|
}
|
|
else if ($data['agency_id'] == 22666) {
|
|
$funnel_id = 2096;
|
|
} else if($data['agency_id'] == 23156){
|
|
$funnel_id = 2299;
|
|
} else if ($data['agency_id'] == 23360){
|
|
$funnel_id = 2424;
|
|
}
|
|
|
|
if($funnel_id > 0){
|
|
$req['funnel_id'] = $funnel_id;
|
|
}
|
|
$source = 0;
|
|
$sourse_number = '+'.str_replace('+','',$this->_inputData['internal_number']);
|
|
if($this->_inputData['operator'] == 'MEGAFON' && !empty($this->_inputData['transit_number'])){
|
|
$sourse_number = '+'.str_replace('+','',$this->_inputData['transit_number']);
|
|
}
|
|
$resS = $this->findSource($sourse_number, $data['agency_id']);
|
|
if(isset($resS['id'])){
|
|
$responce['source'] = $resS['id'];
|
|
} else {
|
|
$responce['source'] = 0;
|
|
}
|
|
|
|
if(isset($responce['source']) && $responce['source'] > 0){
|
|
$source = $responce['source'];
|
|
}
|
|
|
|
$req['source'] = $source;
|
|
|
|
if ($req['type_id'] == 4) {
|
|
|
|
$pdo = new MysqlPdo(hst, ndb, user, pass);
|
|
$autosearch_instce = new AutoSearch($pdo);
|
|
if ($req['autosearch_enabled'] == 1 && intval($req['object_id']) > 0) {
|
|
|
|
$objToFilter = $autosearch_instce->syncObjectToFilter(
|
|
intval($req['asfilter_id']),
|
|
intval($req['object_id']),
|
|
intval($req['req_id']),
|
|
(intval($req['asfilter_id']) > 0)
|
|
);
|
|
|
|
if ($objToFilter['success'])
|
|
$req['asfilter_id'] = $objToFilter['filter_id'];
|
|
|
|
}
|
|
}
|
|
|
|
if(str_replace(['+7', '@ip.beeline.ru'], '', $data['internal_number']) != 'SIP00SKIU0133O' && str_replace(['+7', '@ip.beeline.ru'], '', $data['internal_number']) != 'SIP00SKIU00QVJ') {
|
|
|
|
$req['id'] = $this->insertReq($req);
|
|
if($data['agency_id'] == 13339){
|
|
//file_put_contents(__DIR__.'/log_test_megafon.txt', $req['id']. "\n", FILE_APPEND);
|
|
|
|
$sql_in_ev = "INSERT INTO user_client_events (user_id, req_id, create_date, schedule_date, `type`, comment, calendar_view, from_id) ".
|
|
"VALUES ($user_id, {$req['id']}, '".date('Y-m-d H:i:s')."', '".date('Y-m-d H:i:s', strtotime('+30 minutes'))."', 'call', '".$req['description']."', 0, {$user_id})";
|
|
//file_put_contents(__DIR__.'/log_test_megafon.txt', $sql_in_ev. "\n", FILE_APPEND);
|
|
mysql_query($sql_in_ev);
|
|
}
|
|
if($data['agency_id'] == 9588){
|
|
$pdo = new MysqlPdo(hst, ndb, user, pass);
|
|
$sql = "SET NAMES 'utf8'";
|
|
$pdo->query($sql);
|
|
$reqClass = new Requisitions($pdo);
|
|
$reqClass->get_class_etap($req['id'], 340);
|
|
} else if($data['agency_id'] == 10732){
|
|
$reqClass = new Requisitions();
|
|
$reqClass->get_class_etap($req['id'], 441);
|
|
}
|
|
if($funnel_id > 0){
|
|
$reqClass = new Requisitions();
|
|
$reqClass->get_class_etap($req['id'], $funnel_id);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public function findSource($phone, $agency_id){
|
|
$users = [];
|
|
$users = array_keys($this->getAllUserAgency($agency_id, $users));
|
|
$source = '';
|
|
$sql = "SELECT * FROM advertising_phone WHERE phone = '".$phone."'";
|
|
$q = mysql_query($sql);
|
|
if(mysql_num_rows($q) > 0){
|
|
$r = mysql_fetch_assoc($q);
|
|
$sql_v = "SELECT id, source FROM advertising_sources WHERE id = ".$r['adv_id']." AND user_id in (".implode(',',$users).")";
|
|
|
|
$q_v = mysql_query($sql_v);
|
|
|
|
if(mysql_num_rows($q_v) > 0){
|
|
$r_v = mysql_fetch_assoc($q_v);
|
|
$source = $r_v;
|
|
}
|
|
}
|
|
|
|
return $source;
|
|
}
|
|
|
|
public function getCallEvents()
|
|
{
|
|
$userId = $_SESSION['id'];
|
|
if($userId) {
|
|
$query = "SELECT * FROM " . $this->table . " WHERE user_id = $userId and status = 1";
|
|
|
|
$event = $this->dbpg->fetch_assoc($this->dbpg->query($query));
|
|
|
|
$cannot_create = '0';
|
|
$perm_query = "SELECT cannot_create_req_manually FROM user_permissions WHERE user_id = " . $userId . " LIMIT 1";
|
|
$perm_result = mysql_query($perm_query);
|
|
if ($perm_row = mysql_fetch_assoc($perm_result)) {
|
|
$cannot_create = (isset($perm_row['cannot_create_req_manually']) ? $perm_row['cannot_create_req_manually'] : 0);
|
|
}
|
|
|
|
if ($event && (int)$event['client_id'] > 0) {
|
|
|
|
$responce = [
|
|
'call_id' => $event['id'],
|
|
'call_uuid' => $event['operator_call_id'],
|
|
'operator' => $event['operator'],
|
|
|
|
'call_type' => $event['external_number'],
|
|
'line1' => true,
|
|
'line2' => false,
|
|
'header_hide' => false,
|
|
'client_id' => $event['client_id'],
|
|
'user_id' => $event['user_id'],
|
|
'cannot_create_req_manually' => $cannot_create,
|
|
];
|
|
|
|
$responce['source'] = '';
|
|
if(!empty($event['transit_number'])){
|
|
$resS = $this->findSource($event['transit_number'], $event['agency_id']);
|
|
if(isset($resS['source'])){
|
|
$responce['source'] = $resS['source'];
|
|
$responce['source_id'] = (int)$resS['id'];
|
|
} else {
|
|
$responce['source'] = '';
|
|
$responce['source_id'] = 0;
|
|
}
|
|
}
|
|
return $responce;
|
|
}
|
|
}
|
|
$responce = [
|
|
'call_id' => false,
|
|
'header' => '',
|
|
'call_uuid' => '',
|
|
'operator' => '',
|
|
'call_type'=>"",
|
|
'line1'=> false,
|
|
'line2' => false,
|
|
'header_hide' => true,
|
|
'client_id' => '',
|
|
'source' => '',
|
|
'source_id' => 0
|
|
];
|
|
|
|
return $responce;
|
|
}
|
|
public function getClient($data){
|
|
$userId = $_SESSION['id'];
|
|
if($userId) {
|
|
$client_id = $data->client_id;
|
|
$query = "SELECT fio FROM clients WHERE id = $client_id";
|
|
$result = mysql_fetch_assoc(mysql_query($query));
|
|
if($result){
|
|
return ['header'=>$result['fio']];
|
|
}
|
|
|
|
}
|
|
}
|
|
public function getRequisition($data){
|
|
$userId = $_SESSION['id'];
|
|
if($userId) {
|
|
$pdo = new MysqlPdo(hst, ndb, user, pass);
|
|
$sql = "SET NAMES 'utf8'";
|
|
$pdo->query($sql);
|
|
$req = new Requisitions($pdo, true);
|
|
$client_id = $data->client_id;
|
|
$query = "SELECT * FROM requisitions WHERE deleted = 0 and client_id=$client_id";
|
|
|
|
$result = mysql_query($query);
|
|
$requisitions = false;
|
|
|
|
while ($row = mysql_fetch_assoc($result)) {
|
|
if ($row['funnel_id'] == 0) {
|
|
$row['step_id'] = $row['stage'];
|
|
}
|
|
$step = $req->get_class_etap($row['id'], $row['funnel_id'], $row['step_id']);
|
|
$requisitions[] = ['id' => $row['id'], 'name' => $row['name'], 'nazv' => $step['stage']];
|
|
}
|
|
|
|
$responce = [
|
|
'requisitions' => $requisitions,
|
|
];
|
|
$query = "SELECT fio FROM clients WHERE id = $client_id";
|
|
$result = mysql_fetch_assoc(mysql_query($query));
|
|
if($result){
|
|
$responce['header'] = $result['fio'];
|
|
}
|
|
|
|
return $responce;
|
|
}
|
|
|
|
}
|
|
public function getListTransfer(){
|
|
$userId = $_SESSION['id'];
|
|
if ($userId) {
|
|
$user = $this->getUserByField('id', $userId);
|
|
|
|
$agency_id = $this->getAgency($user);
|
|
//echo $agency_id;
|
|
$agensy = $this->getUserByField('id', $agency_id);
|
|
$user_list = [];
|
|
$user_list[$agency_id] = $agensy;
|
|
|
|
$user_list = $this->getAllUserAgency($agency_id, $user_list);
|
|
$uniquUser = [];
|
|
|
|
foreach ($user_list as $user) {
|
|
$uniquUser[] = [
|
|
'name' => trim($user['last_name'] . ' ' . $user['first_name'] . ' ' . $user['middle_name']),
|
|
'phone' => $user['phone']
|
|
];
|
|
}
|
|
|
|
$responce = [
|
|
'userlist' => $uniquUser,
|
|
];
|
|
|
|
|
|
return $responce;
|
|
}
|
|
|
|
}
|
|
|
|
public function getAllUserAgency($user_id,$list){
|
|
|
|
$agency_id = $user_id;
|
|
|
|
$usersIds = array();
|
|
$usersIds[] = $agency_id;
|
|
$sql_users = "SELECT id FROM users WHERE id in (select id from users where id=$agency_id or id_manager=$agency_id or id_manager in
|
|
(select id from users where id_manager=$agency_id or id_manager in
|
|
(select id from users where id_manager=$agency_id)))";
|
|
$q_users = mysql_query($sql_users);
|
|
while($r_users = mysql_fetch_assoc($q_users)){
|
|
$usersIds[] = (int)$r_users['id'];
|
|
}
|
|
|
|
|
|
$result = $this->getUsersByField('id', $usersIds);
|
|
foreach($result as $row){
|
|
$list[$row['id']] = $row;
|
|
//$list = $this->getAllUserAgency($row['id'],$list);
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
public function callTransfer($data){
|
|
$userId = $_SESSION['id'];
|
|
|
|
if ($userId) {
|
|
$user = $this->getUserByField('id', $userId);
|
|
$agency_id = $this->getAgency($user);
|
|
$operator = $data->operator;
|
|
$api_key = $this->getApiKey($agency_id,$operator);
|
|
$phone = str_replace('+7', '',$data->phone);
|
|
$callId = $data->callid;
|
|
$res = array();
|
|
$user_phone = str_replace('+7', '',$user['vpbx_id']);//str_replace('+7', '',$user['phone']);
|
|
$beeline = new BeelineApiClass($api_key);
|
|
switch ($data->method){
|
|
case 'transfer': {
|
|
$res = $beeline->callTransfer($user_phone,$callId,$phone);
|
|
|
|
break;
|
|
}
|
|
case 'consult': $beeline->callTransferConsult($user_phone,$callId,$phone);break;
|
|
}
|
|
return [$api_key,$phone,$callId,$user_phone];
|
|
|
|
}
|
|
}
|
|
|
|
private function getApiKey($agency_id,$operator){
|
|
$operator = strtolower($operator);
|
|
$sql = "SELECT * FROM agency_mango WHERE agency_id = '$agency_id' and `type`= '$operator'";
|
|
$result = mysql_fetch_assoc(mysql_query($sql));
|
|
return $result['api_key'];
|
|
}
|
|
|
|
|
|
public function getCallHistory(){
|
|
$userId = $_SESSION['id'];
|
|
if ($userId) {
|
|
$user = $this->getUserByField('id', $userId);
|
|
|
|
$today = strtotime(date('d.m.Y 00:00:00'));
|
|
$phone = str_replace(['@ip.beeline.ru','+7'],'',$user['vpbx_id']).'@ip.beeline.ru';
|
|
$query = "SELECT m.* FROM mango_calls as m WHERE m.start>'$today' AND (m.vpbx_id = '$phone' or m.vpbx_id = '$user[vpbx_id]') ORDER BY id DESC";
|
|
//echo $query;
|
|
|
|
$result = $this->dbpg->query($query);
|
|
$incoming = [];
|
|
$total = 0;
|
|
while($row = $this->dbpg->fetch_assoc($result)){
|
|
$start = date('H:i', $row['start']);
|
|
$date = date('Y-m-d',$row['start']);
|
|
$client = (int)$row['client_id'];
|
|
if (strpos($row['to_number'],$phone)!==false) $type = 'incoming';
|
|
else $type = 'outgoing';
|
|
if ($row['disconnect_reason'] == 'answered') $type .= '_answer';
|
|
$sql_u = "SELECT fio FROM clients WHERE id = $row[client_id]";
|
|
$q_u = mysql_query($sql_u);
|
|
$r_u = mysql_fetch_assoc($q_u);
|
|
$incoming[$date][] = ['name'=>$r_u['fio'], 'time'=>$start, 'type'=>$type, 'client_id'=>(int)$row['client_id']];
|
|
$total++;
|
|
}
|
|
|
|
|
|
|
|
|
|
$today = date('Y-m-d');
|
|
$yesterday = date('Y-m-d', time() - 86400);
|
|
$responce = [];
|
|
foreach($incoming as $key => $value){
|
|
|
|
if($key == $today){
|
|
$responce[] = ['day'=>'Сегодня', 'calls' => $value];
|
|
} elseif ($key == $yesterday){
|
|
$responce[] = ['day'=>'Вчера', 'calls' => $value];
|
|
} else {
|
|
$responce[] = ['day'=>date('d.m', strtotime($key)), 'calls' => $value];
|
|
}
|
|
}
|
|
|
|
return [
|
|
'calls' => $responce,
|
|
'totalCalls' => $total,
|
|
];
|
|
|
|
}
|
|
}
|
|
|
|
public function endCall($data){
|
|
$call_id = $data->call_id;
|
|
$sql = "UPDATE call_events SET status = 0 WHERE id = {$call_id}";
|
|
//mysql_query($sql);
|
|
$this->dbpg->query($sql, true);
|
|
return['call_id' => $call_id];
|
|
}
|
|
|
|
}
|