425 lines
21 KiB
PHP
425 lines
21 KiB
PHP
<?php
|
|
|
|
require_once($_SERVER['DOCUMENT_ROOT']."/config.php");
|
|
header('Content-Type: application/json');
|
|
ini_set('error_reporting', E_ALL);
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
|
|
|
|
//Функция для записи звонков в базу
|
|
function read_phone($telphin, $p, $agency_id, $type, $main_user_id, $dataApi){
|
|
// var_dump($p);
|
|
//echo $agency_id.' -- '. $only_widget."<br>";
|
|
//поиск пользователя
|
|
//echo $p->user_find_number."++<br>";
|
|
$user_id = $telphin -> find_user($p->user_find_number);
|
|
|
|
//echo $user_id.'<br>';
|
|
$new_client = false;
|
|
if($user_id == 0) $user_id = $main_user_id;
|
|
//echo $user_id.'<br>';
|
|
if($user_id != 0){
|
|
$funnel_id = 0;
|
|
if($agency_id == 12569){
|
|
$funnel_id = 742;
|
|
} else if($agency_id == 17460){
|
|
$funnel_id = 1159;
|
|
} else if($agency_id == 19322){
|
|
$funnel_id = 1447;
|
|
} else if($agency_id == 22432){
|
|
$funnel_id = 2118;
|
|
}
|
|
|
|
if($user_id != 0){
|
|
if($agency_id == 18205 && ($p->user_find_number != '+79837004984' && $p->user_find_number !='79837004984')){
|
|
/** */
|
|
} else {
|
|
//Поиск клиента
|
|
$client_id = $telphin -> find_client($p->client_find_number);
|
|
$source = 0;
|
|
$source = $telphin->findSource($telphin->ver_phone($p->source_find));
|
|
|
|
if($client_id == 0 && $dataApi['only_widget'] == 0){
|
|
$new_client = true;
|
|
if($agency_id == 7384){
|
|
$sql_cl_mango = "INSERT INTO `clients` (fio, phone, opis, id_agent, date_add, who_work, confirm, stage, funnel_id, source) " .
|
|
" VALUES ('Клиент telphin', '".$telphin->ver_phone($p->client_find_number)."', '".$p->comment."', {$user_id}, '".date('Y-m-d H:i:s', (strtotime($p->init_time_gmt)+3*60*60))."', {$user_id}, '1', '1', 75, '{$source}')";
|
|
} else if($agency_id == 7478){
|
|
$sql_cl_mango = "INSERT INTO `clients` (fio, phone, opis, id_agent, date_add, who_work, confirm, stage, funnel_id, source) " .
|
|
" VALUES ('Клиент telphin', '".$telphin->ver_phone($p->client_find_number)."', '".$p->comment."', {$user_id}, '".date('Y-m-d H:i:s', (strtotime($p->init_time_gmt)+3*60*60))."', {$user_id}, '1', '1', 80, '{$source}')";
|
|
} else if($funnel_id > 0){
|
|
$sql_cl_mango = "INSERT INTO `clients` (fio, phone, opis, id_agent, date_add, who_work, confirm, stage, funnel_id, source) " .
|
|
" VALUES ('Клиент telphin', '".$telphin->ver_phone($p->client_find_number)."', '".$p->comment."', {$user_id}, '".date('Y-m-d H:i:s', (strtotime($p->init_time_gmt)+3*60*60))."', {$user_id}, '1', '1', {$funnel_id}, '{$source}')";
|
|
|
|
} else {
|
|
$sql_cl_mango = "INSERT INTO `clients` (fio, phone, opis, id_agent, date_add, who_work, confirm, stage, source) " .
|
|
" VALUES ('Клиент telphin', '".$telphin->ver_phone($p->client_find_number)."', '".$p->comment."', {$user_id}, '".date('Y-m-d H:i:s', (strtotime($p->init_time_gmt)+3*60*60))."', {$user_id}, '1', '1', '{$source}')";
|
|
|
|
}
|
|
if($agency_id == 8826 || $agency_id == 12569 || $agency_id == 14349 || $agency_id == 19843 || $agency_id == 20947 || $agency_id == 21422 || $agency_id == 22432 || $agency_id == 21951){
|
|
if($type == 'abonentTelphinAnswer' || $type == 'abonentTelphinNoAnswer'){
|
|
mysql_query($sql_cl_mango);
|
|
$client_id = mysql_insert_id();
|
|
}
|
|
} else {
|
|
mysql_query($sql_cl_mango);
|
|
$client_id = mysql_insert_id();
|
|
if($agency_id == 7384){
|
|
$client = new Clients;
|
|
$client->get_class_etap($client_id, 75);
|
|
} else if($agency_id == 7478){
|
|
$client = new Clients;
|
|
$client->get_class_etap($client_id, 80);
|
|
} else if($funnel_id > 0){
|
|
$client = new Clients;
|
|
$client->get_class_etap($client_id, $funnel_id);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
if($client_id != 0){
|
|
|
|
if($agency_id == 7384 && ($source == 677)){
|
|
$new_client = true;
|
|
$sql_s = "SELECT id FROM clients WHERE phone = '".$telphin->ver_phone($p->client_find_number)."' and date_add = '".date('Y-m-d H:i:s', (strtotime($p->init_time_gmt)+3*60*60))."' and funnel_id = 75";
|
|
//echo $sql_s.'<br>';
|
|
$q_s = mysql_query($sql_s);
|
|
if(mysql_num_rows($q_s) == 0){
|
|
$sql_cl_mango = "INSERT INTO `clients` (fio, phone, opis, id_agent, date_add, who_work, confirm, stage, funnel_id, source) " .
|
|
" VALUES ('Клиент telphin', '".$telphin->ver_phone($p->client_find_number)."', '".$p->comment."', {$user_id}, '".date('Y-m-d H:i:s', (strtotime($p->init_time_gmt)+3*60*60))."', {$user_id}, '1', '1', 75, '{$source}')";
|
|
mysql_query($sql_cl_mango);
|
|
$client_id = mysql_insert_id();
|
|
$client = new Clients;
|
|
$client->get_class_etap($client_id, 75);
|
|
} else {
|
|
$r_s = mysql_fetch_assoc($q_s);
|
|
$client_id = (int)$r_s['id'];
|
|
}
|
|
} else if($type == 'abonentTelphinAnswer'){
|
|
$cl_stage = $telphin -> get_client_stage();
|
|
//Перевод клиента в стадию "Новый", если он был закрыт
|
|
if($cl_stage == 8 || $cl_stage == 10){
|
|
$up_client = "UPDATE `clients` SET `stage`='1', `who_work`='{$user_id}', `deleted` = 0 WHERE `id`={$client_id}";
|
|
mysql_query($up_client);
|
|
$dop_text = "c «Закрыт» на «Новый» (По входящему звонку telphin)";
|
|
$in_ev = "INSERT INTO `events_clients` (`user_id`, `client_id`, `event`, `dop_text`, `read`) VALUES ('{$user_id}', '{$client_id}', 'stageedit', '{$dop_text}', 1)";
|
|
mysql_query($in_ev);
|
|
}
|
|
}
|
|
|
|
$record = '';
|
|
$isRecord = false;
|
|
|
|
$sql_m = "SELECT id FROM user_client_events WHERE client_id = {$client_id} AND create_date = '".date('Y-m-d H:i:s', (strtotime($p->init_time_gmt)+3*60*60))."'".
|
|
" AND `type` = 'call' AND telphin = 1 AND tracking = 0";
|
|
|
|
$q_m = mysql_query($sql_m);
|
|
|
|
if(mysql_num_rows($q_m) == 0){
|
|
|
|
if(!empty($p->cdr)){
|
|
|
|
$record=$telphin->recordSave($p->cdr);
|
|
$isRecord = true;
|
|
} else {
|
|
$record = json_encode([]);
|
|
}
|
|
|
|
//Поиск чей клиент
|
|
$sql_who = "SELECT who_work FROM clients WHERE id = {$client_id}";
|
|
$q_who = mysql_query($sql_who);
|
|
$r_who = mysql_fetch_assoc($q_who);
|
|
$who_work = $user_id;
|
|
if($r_who['who_work'] != 0){
|
|
$who_work = $r_who['who_work'];
|
|
}
|
|
|
|
if($type == 'abonentTelphinAnswer'){
|
|
if(!$new_client && $who_work != $user_id){
|
|
$tg_notif = "INSERT INTO telephony_notifications (user_id, who_work, from_mes, to_mes, client_id, type_phone) ".
|
|
"VALUES ({$user_id},{$who_work},0,1,{$client_id}, 1),".
|
|
"({$who_work},{$user_id},1,0,{$client_id}, 1)";
|
|
mysql_query($tg_notif);
|
|
}
|
|
}
|
|
|
|
//Поиск отслеживаемого звонка
|
|
$sql_t = "SELECT * FROM user_client_events WHERE client_id = {$client_id} AND user_id={$who_work} AND `type` = 'call' AND tracking = 1 AND calendar_view = 0";
|
|
$q_t = mysql_query($sql_t);
|
|
if(mysql_num_rows($q_t) > 0){
|
|
$r_t = mysql_fetch_assoc($q_t);
|
|
mysql_query("UPDATE user_client_events SET calendar_view = 1, record='".$record."' WHERE id = ".$r_t['id']);
|
|
if($type == 'telphinAbonentNoAnswer'){
|
|
$sql_in_ev = "INSERT INTO user_client_events (user_id, client_id, create_date, schedule_date_to, schedule_date, `type`, comment, calendar_view, from_id, telphin) ".
|
|
"VALUES (".$r_t['user_id'].", {$client_id}, '".date('Y-m-d H:i:s', (strtotime($p->init_time_gmt)+3*60*60))."', '".date('Y-m-d H:i:s')."', '".date('Y-m-d H:i:s', time()+3600)."', 'call', '".$r_t['comment'].' '.$p->comment_in."', 0, ".$r_t['from_id'].", 1)";
|
|
mysql_query($sql_in_ev);
|
|
}
|
|
} else {
|
|
|
|
$sql_in_ev = "INSERT INTO user_client_events (user_id, client_id, create_date, schedule_date, `type`, comment, calendar_view, from_id, telphin, record) ".
|
|
"VALUES ({$who_work}, {$client_id}, '".date('Y-m-d H:i:s', (strtotime($p->init_time_gmt)+3*60*60))."', '".date('Y-m-d H:i:s', (strtotime($p->init_time_gmt)+3*60*60))."', 'call', '".$p->comment_in."', 1, {$user_id}, 1, '".$record."')";
|
|
//echo $sql_in_ev.'</br>';
|
|
mysql_query($sql_in_ev);
|
|
}
|
|
}
|
|
|
|
//Поиск заявки по клиенту
|
|
if($dataApi['is_req'] > 0){
|
|
if(!$isRecord){
|
|
if(!empty($p->cdr)){
|
|
$record = $telphin->recordSave($p->cdr);
|
|
$isRecord = true;
|
|
} else {
|
|
$record = json_encode([]);
|
|
}
|
|
}
|
|
|
|
$reqClass = new Requisitions();
|
|
|
|
$req_s = "SELECT id, user_id, who_work FROM requisitions WHERE client_id=$client_id and who_work={$user_id} and cancel=0 and deleted=0";
|
|
|
|
$req_q = mysql_query($req_s);
|
|
$req_rows = mysql_num_rows($req_q);
|
|
$req_comm = str_replace(["Клиент","записан"], ["Заявка","записана"], $p->comment);
|
|
if($req_rows > 0){
|
|
|
|
$req = mysql_fetch_assoc($req_q);
|
|
$sql_m = "SELECT id FROM user_client_events WHERE req_id = $req[id] AND create_date = '".date('Y-m-d H:i:s', (strtotime($p->init_time_gmt)+3*60*60))."'".
|
|
" AND user_id={$user_id} AND `type` = 'call' AND telphin = 1 AND tracking = 0";
|
|
$q_m = mysql_query($sql_m);
|
|
if(mysql_num_rows($q_m) == 0){
|
|
$sql_in_ev = "INSERT INTO user_client_events (user_id, req_id, create_date, schedule_date, `type`, comment, calendar_view, from_id, telphin, record) ".
|
|
"VALUES ($req[who_work], $req[id], '".date('Y-m-d H:i:s', (strtotime($p->init_time_gmt)+3*60*60))."', '".date('Y-m-d H:i:s', (strtotime($p->init_time_gmt)+3*60*60))."', 'call', '".$p->comment_in."', 1, {$user_id}, 1, '".$record."')";
|
|
mysql_query($sql_in_ev);
|
|
}
|
|
} else {
|
|
$nameReq = 'Заявка Телфин';
|
|
$sql_cl = "SELECT fio FROM clients WHERE id={$client_id}";
|
|
$q_cl = mysql_query($sql_cl);
|
|
$r_cl = mysql_fetch_assoc($q_cl);
|
|
if(isset($r_cl['fio'])){
|
|
$nameReq = "Подбор объекта для ".$r_cl['fio'];
|
|
}
|
|
$type_id= 1;
|
|
if($agency_id == 20669){
|
|
if(isset($r_cl['fio'])){
|
|
$nameReq = "Реализация объекта для ".$r_cl['fio'];
|
|
}
|
|
$type_id= 2;
|
|
}
|
|
$funnel_id = 0;
|
|
if($agency_id == 12569){
|
|
$funnel_id = 742;
|
|
} else if($agency_id == 17460){
|
|
$funnel_id = 1159;
|
|
} else if($agency_id == 19322){
|
|
$funnel_id = 1447;
|
|
}
|
|
else if($agency_id == 19843){
|
|
$funnel_id = 1614;
|
|
} else if($agency_id == 20669){
|
|
$funnel_id = 1675;
|
|
} else if($agency_id == 20947){
|
|
$funnel_id = 1819;
|
|
}else if($agency_id == 21422){
|
|
$funnel_id = 1858;
|
|
}
|
|
if( $agency_id == 12569 || $agency_id == 14349 || $agency_id == 19843 || $agency_id == 20947 || $agency_id == 21422 || $agency_id == 20197){
|
|
if($type == 'abonentTelphinAnswer' || $type == 'abonentTelphinNoAnswer'){
|
|
$sql_req = "INSERT INTO `requisitions` SET name='{$nameReq}', user_id={$user_id}, integration=1, client_id={$client_id}, stage=1, type_id={$type_id}, who_work={$user_id}, description='{$req_comm}', source='{$source}', funnel_id='{$funnel_id}'";
|
|
mysql_query($sql_req);
|
|
}
|
|
} else if ($agency_id != 18205 && $agency_id != 19322 && $agency_id != 4 && $agency_id != 22432 && $agency_id != 21951) {
|
|
$sql_req = "INSERT INTO `requisitions` SET name='{$nameReq}', user_id={$user_id}, integration=1, client_id={$client_id}, stage=1, type_id={$type_id}, who_work={$user_id}, description='{$req_comm}', source='{$source}', funnel_id='{$funnel_id}'";
|
|
mysql_query($sql_req);
|
|
}
|
|
if($agency_id != 18205 && $agency_id != 19322 && $agency_id != 4 && $agency_id != 21951){
|
|
$req_id = mysql_insert_id();
|
|
}
|
|
if(isset($req_id) && $req_id > 0){
|
|
|
|
$sql_in_ev = "INSERT INTO user_client_events (user_id, req_id, create_date, schedule_date, `type`, comment, calendar_view, from_id, telphin, record) ".
|
|
"VALUES ($user_id, $req_id, '".date('Y-m-d H:i:s', (strtotime($p->init_time_gmt)+3*60*60))."', '".date('Y-m-d H:i:s', (strtotime($p->init_time_gmt)+3*60*60))."', 'call', '".$p->comment_in."', 1, {$user_id}, 1, '".$record."')";
|
|
mysql_query($sql_in_ev);
|
|
if($funnel_id > 0){
|
|
$reqClass->get_class_etap($req_id, $funnel_id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
$sql_tel = "SELECT * FROM agency_mango WHERE `type` = 'telphin'";
|
|
|
|
if(isset($_SESSION['id'])){
|
|
$sql_tel = "SELECT * FROM agency_mango WHERE `type` = 'telphin' AND agency_id = 20947";
|
|
//echo $sql_tel;
|
|
}
|
|
|
|
$q_tel = mysql_query($sql_tel);
|
|
|
|
while($r_tel = mysql_fetch_assoc($q_tel)){
|
|
$api_key = $r_tel['api_key'];
|
|
$api_salt = $r_tel['api_salt'];
|
|
$last_date = $r_tel['last_date'];
|
|
$agency_id =$r_tel['agency_id'];
|
|
$main_user_id = $r_tel['main_user'];
|
|
$lost_update = $r_tel['lost_update'];
|
|
|
|
$telphin = new TelphinApiClass($api_key, $api_salt);
|
|
if($telphin->auth()){
|
|
|
|
$usersTelfin = array();
|
|
// if((time() - $lost_update) >= 86400){
|
|
|
|
$usersCall=json_decode($telphin->getUsers());
|
|
foreach($usersCall as $userT){
|
|
$extra_params = json_decode($userT->extra_params);
|
|
if(isset($extra_params->phone_work)){
|
|
$usersTelfin[$userT->name] = $extra_params->phone_work;
|
|
}
|
|
// if((time() - $lost_update) >= 86400){
|
|
$telphin->saveUser($userT, $agency_id);
|
|
// }
|
|
}
|
|
if((time() - $lost_update) >= 86400){
|
|
$sql_up_tel = "UPDATE agency_mango SET lost_update = '".time()."' WHERE id = ".$r_tel['id'];
|
|
mysql_query($sql_up_tel);
|
|
}
|
|
|
|
|
|
|
|
$start_date = date('Y-m-d H:i:s', (strtotime($last_date) - 6*60*60));
|
|
/* if($_SESSION['id'] == 20947)
|
|
$start_date = date('Y-m-d H:i:s', (strtotime('2026-04-20 08:40:00') - 2*60*60));*/
|
|
$end_date= date('Y-m-d H:i:s');
|
|
//var_dump(rawurlencode($start_date));
|
|
|
|
$usersIds = array();
|
|
$w = new WebHookIn();
|
|
$usersIds = $w->getAllUserAgency($agency_id);
|
|
$usersIds = array_unique($usersIds);
|
|
//var_dump($usersIds);
|
|
$telphin->set_users($usersIds);
|
|
// var_dump($telphin->users);
|
|
$phones_type=array();
|
|
$calls = json_decode($telphin->calls(rawurlencode($start_date), rawurlencode($end_date)));
|
|
|
|
foreach ($calls->call_history as $call) {
|
|
|
|
$calls_ids = $telphin->stat($call, $agency_id, $r_tel['only_widget']);
|
|
$id_call = $calls_ids['id'];
|
|
$call_id_pg = $calls_ids['idpg'];
|
|
// echo $call->to_username.' -- '.$call->from_username.'<br>';
|
|
$typearr = json_decode($telphin->get_phone_type($call));
|
|
$type = $typearr->type;
|
|
|
|
$user_find_number = $typearr->find_user;
|
|
if(isset($usersTelfin[$typearr->find_user]) && !empty($usersTelfin[$typearr->find_user])){
|
|
$user_find_number = $usersTelfin[$typearr->find_user];
|
|
}
|
|
|
|
//var_dump($calls);
|
|
|
|
|
|
// $telphin->save_user_call($id_call, $user_find_number, $main_user_id);
|
|
$telphin->save_user_call_pg($call_id_pg, $user_find_number, $main_user_id);
|
|
|
|
switch ($type){
|
|
case 'telphinAbonentAnswer': {
|
|
$call->comment = 'Клиент записан по исходящему звонку в telphin';
|
|
$call->comment_in = 'Исходящий звонок в Telphin';
|
|
$call->user_find_number = $call->from_username;
|
|
if(isset($usersTelfin[$typearr->find_user]) && !empty($usersTelfin[$typearr->find_user])){
|
|
$call->user_find_number = $usersTelfin[$typearr->find_user];
|
|
}
|
|
$call->source_find = $call->from_username;
|
|
$call->client_find_number = $call->to_username;
|
|
$call->comment_in .= ' '.$telphin->find_user_comment($call, 'from');
|
|
|
|
$phones_type['telphinAbonentAnswer'][] = $call;
|
|
break;
|
|
}
|
|
case 'abonentTelphinAnswer': {
|
|
|
|
$call->comment = 'Клиент записан по входящему звонку в telphin';
|
|
$call->comment_in = 'Входящий звонок в Telphin';
|
|
$call->user_find_number = $call->to_username;
|
|
if(isset($usersTelfin[$typearr->find_user]) && !empty($usersTelfin[$typearr->find_user])){
|
|
$call->user_find_number = $usersTelfin[$typearr->find_user];
|
|
}
|
|
$call->source_find = $call->to_username;
|
|
$call->client_find_number = $call->from_username;
|
|
$call->comment_in .= ' '.$telphin->find_user_comment($call, 'to');
|
|
|
|
$phones_type['abonentTelphinAnswer'][] = $call;
|
|
break;
|
|
}
|
|
case 'telphinAbonentNoAnswer': {
|
|
$call->comment = 'Клиент записан по исходящему звонку в telphin';
|
|
$call->comment_in = 'Исходящий звонок в Telphin.<br>Абонент не ответил';
|
|
$call->user_find_number = $call->from_username;
|
|
if(isset($usersTelfin[$typearr->find_user]) && !empty($usersTelfin[$typearr->find_user])){
|
|
$call->user_find_number = $usersTelfin[$typearr->find_user];
|
|
}
|
|
$call->source_find = $call->from_username;
|
|
$call->client_find_number = $call->to_username;
|
|
$call->comment_in .= ' '.$telphin->find_user_comment($call, 'from');
|
|
|
|
$phones_type['telphinAbonentNoAnswer'][] = $call;
|
|
break;
|
|
}
|
|
case 'abonentTelphinNoAnswer':{
|
|
$call->comment = 'Клиент записан по входящему звонку в telphin';
|
|
$call->comment_in = 'Пропущеный входящий звонок в Telphin';
|
|
$call->user_find_number = $call->to_username;
|
|
if(isset($usersTelfin[$typearr->find_user]) && !empty($usersTelfin[$typearr->find_user])){
|
|
$call->user_find_number = $usersTelfin[$typearr->find_user];
|
|
}
|
|
$call->source_find = $call->to_username;
|
|
$call->client_find_number = $call->from_username;
|
|
$call->comment_in .= ' '.$telphin->find_user_comment($call, 'to');
|
|
|
|
$phones_type['abonentTelphinNoAnswer'][] = $call;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
//print_r($phones_type);
|
|
|
|
if(isset($phones_type['telphinAbonentAnswer'])){
|
|
foreach ($phones_type['telphinAbonentAnswer'] as $p) {
|
|
read_phone($telphin, $p, $agency_id, 'telphinAbonentAnswer', $main_user_id, $r_tel);
|
|
}
|
|
}
|
|
if(isset($phones_type['abonentTelphinAnswer'])){
|
|
foreach ($phones_type['abonentTelphinAnswer'] as $p) {
|
|
read_phone($telphin, $p, $agency_id, 'abonentTelphinAnswer', $main_user_id, $r_tel);
|
|
}
|
|
}
|
|
if(isset($phones_type['telphinAbonentNoAnswer'])){
|
|
foreach ($phones_type['telphinAbonentNoAnswer'] as $p) {
|
|
read_phone($telphin, $p, $agency_id, 'telphinAbonentNoAnswer', $main_user_id, $r_tel);
|
|
}
|
|
}
|
|
if(isset($phones_type['abonentTelphinNoAnswer'])){
|
|
foreach ($phones_type['abonentTelphinNoAnswer'] as $p) {
|
|
read_phone($telphin, $p, $agency_id, 'abonentTelphinNoAnswer', $main_user_id, $r_tel);
|
|
}
|
|
}
|
|
}
|
|
|
|
$sql_up_tel = "UPDATE agency_mango SET last_date = '".date('Y-m-d H:i:s')."' WHERE id = ".$r_tel['id'];
|
|
mysql_query($sql_up_tel);
|
|
|
|
}
|