query("SET NAMES 'utf8'"); header('Content-Type: application/json'); function read_phone($alloClass, $p, $agency_id, $type, $main_user_id, $funnel_telephony){ //поиск пользователя $pdo = new MysqlPdo(hst, ndb, user, pass); $pdo->query("SET NAMES 'utf8'"); $user_id = $alloClass->find_user($p['user_find_number']); $client_id = 0; if($user_id == 0) $user_id = $main_user_id; if($user_id > 0){ $who_work_i = $user_id; $confirm = 1; $funnel_id = 0; if(!empty($funnel_telephony)){ foreach($funnel_telephony as $funnel_t){ if($funnel_t['phone'] == $alloClass->ver_phone($p['user_find_number'])){ if((int)$funnel_t['funnel_id'] > 0){ $funnel_id = (int)$funnel_t['funnel_id']; } } } } //Поиск клиента $client_id = $alloClass->find_client($p['client_find_number']); $source = $alloClass->findSource($alloClass->ver_phone($p['user_find_number'])); if($client_id == 0){ $sql_cl_mango = "INSERT INTO `clients` (fio, phone, opis, id_agent, date_add, who_work, confirm, stage, funnel_id, source) " . " VALUES ('Клиент Алло Инкогнито', '".$alloClass -> ver_phone($p['client_find_number'])."', '".$p['comment']."', {$user_id}, '".date('Y-m-d H:i:s', $p['start'])."', {$who_work_i}, '{$confirm}', '1',$funnel_id,$source)"; if($agency_id == 9973 || $agency_id == 7864 || $agency_id == 10982 || $agency_id == 12254){ if($type == 'abonentAlloAnswer' || $type == 'abonentAlloNoAnswer'){ $pdo->query($sql_cl_mango); $client_id = $pdo->insert_id(); } } else { $pdo->query($sql_cl_mango); $client_id = $pdo->insert_id(); } if($client_id > 0 && $funnel_id > 0){ $client = new Clients; $client->get_class_etap($client_id, $funnel_id); } } if($client_id > 0){ if($type == 'abonentAlloAnswer' || $type == 'abonentAlloNoAnswer'){ $cl_stage = $alloClass -> 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}"; $pdo->query($up_client); $dop_text = "c «Закрыт» на «Новый» (По входящему звонку Алло Инкогнито)"; $in_ev = "INSERT INTO `events_clients` (`user_id`, `client_id`, `event`, `dop_text`, `read`) VALUES ('{$user_id}', '{$client_id}', 'stageedit', '{$dop_text}', 1)"; $pdo->query($in_ev); } } $sql_m = "SELECT id FROM user_client_events WHERE client_id = {$client_id} AND create_date = '".date('Y-m-d H:i:s', $p['start'])."'". " AND `type` = 'call' AND allo = 1"; $isRecord = false; $q_m = $pdo->query($sql_m); if($pdo->num_rows($q_m) == 0){ $record = ''; $record=$alloClass->findRecord((int)$p['call_id']); if(!empty($record) && $record != '[]'){ $isRecord=true; } //Поиск чей клиент $sql_who = "SELECT who_work FROM clients WHERE id = {$client_id}"; $q_who = $pdo->query($sql_who); $r_who = $pdo->fetch_assoc($q_who); $who_work = $user_id; if($r_who['who_work'] != 0){ $who_work = $r_who['who_work']; } $sql_in_ev = "INSERT INTO user_client_events (user_id, client_id, create_date, schedule_date, `type`, comment, calendar_view, from_id, allo, record) ". "VALUES ({$who_work}, {$client_id}, '".date('Y-m-d H:i:s', $p['start'])."', '".date('Y-m-d H:i:s',$p['start'])."', 'call', '".$p['comment_in']."', 1, {$user_id}, 1, '".$record."')"; //echo $sql_in_ev.'
'; $pdo->query($sql_in_ev); } //Поиск заявки по клиенту if($agency_id != 7864){ if(!$isRecord){ $record=$alloClass->findRecord((int)$p['call_id']); if(!empty($record) && $record != '[]'){ $isRecord=true; } } $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 = $pdo->query($req_s); $req_rows = $pdo->num_rows($req_q); $req_comm = str_replace(["Клиент","записан"], ["Заявка","записана"], $p['comment']); if($req_rows > 0){ if($req_rows > 1){ while($req = $pdo->fetch_assoc($req_q)){ if($req['who_work'] == $user_id) break; } } else { $req = $pdo->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', $p['start'])."'". " AND user_id={$req['who_work']} AND `type` = 'call' AND allo = 1 AND tracking = 0"; $q_m = $pdo->query($sql_m); if($pdo->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, allo, record) ". "VALUES ($req[who_work], $req[id], '".date('Y-m-d H:i:s', $p['start'])."', '".date('Y-m-d H:i:s', $p['start'])."', 'call', '".$p['comment_in']."', 1, {$user_id}, 1, '".$record."')"; $pdo->query($sql_in_ev); } } else { $nameReq = 'Заявка Алло Инкогнито'; $sql_cl = "SELECT fio FROM clients WHERE id={$client_id}"; $q_cl = $pdo->query($sql_cl); $r_cl = $pdo->fetch_assoc($q_cl); if(isset($r_cl['fio'])){ $nameReq = "Подбор объекта для ".$r_cl['fio']; } $req_id = 0; if($agency_id == 11082){ $funnel_id = 479; } else if($agency_id == 10429){ $funnel_id = 551; } else if($agency_id == 12061){ $funnel_id = 573; } $sql_req = "INSERT INTO `requisitions` SET name='{$nameReq}', integration=1, user_id={$user_id}, client_id={$client_id}, stage=1, type_id=1, who_work={$user_id}, description='{$req_comm}'"; if($funnel_id > 0) { $sql_req .= ", funnel_id={$funnel_id}"; $reqClass = new Requisitions(); } if($agency_id == 9973 || $agency_id == 7864 || $agency_id == 10982){ if($type == 'abonentAlloAnswer' || $type == 'abonentAlloNoAnswer'){ $pdo->query($sql_req); $req_id = $pdo->insert_id(); $sql_in_ev = "INSERT INTO user_client_events (user_id, req_id, create_date, schedule_date, `type`, comment, calendar_view, from_id, allo, record) ". "VALUES ($user_id, $req_id, '".date('Y-m-d H:i:s', $p['start'])."', '".date('Y-m-d H:i:s', $p['start'])."', 'call', '".$p['comment_in']."', 1, {$user_id}, 1, '".$record."')"; $pdo->query($sql_in_ev); } } else if($agency_id != 11082 && $agency_id != 12254 && $agency_id != 12343 && $agency_id != 12482 && $agency_id != 12762 && $agency_id != 12556) { $pdo->query($sql_req); $req_id = $pdo->insert_id(); $sql_in_ev = "INSERT INTO user_client_events (user_id, req_id, create_date, schedule_date, `type`, comment, calendar_view, from_id, allo, record) ". "VALUES ($user_id, $req_id, '".date('Y-m-d H:i:s', $p['start'])."', '".date('Y-m-d H:i:s', $p['start'])."', 'call', '".$p['comment_in']."', 1, {$user_id}, 1, '".$record."')"; $pdo->query($sql_in_ev); } if($req_id > 0 && $funnel_id > 0){ $reqClass->get_class_etap($req_id, $funnel_id); } } } } } } $sql_allo = "SELECT * FROM agency_alloincognito"; //echo $sql_allo; $q_allo = $pdo->query($sql_allo); while($r_allo = $pdo->fetch_assoc($q_allo)){ $authorization_token = $r_allo['authorization_token']; $created_authorization_token = $r_allo['created_authorization_token']; $lost_date = $r_allo['lost_date']; $agency_id =$r_allo['agency_id']; $main_user_id = $r_allo['main_user']; $phones_type = array(); $nums = array(); if(!empty($r_allo['main_phone_ats'])){ $nums[] = $r_allo['main_phone_ats']; } $funnel_telephony = array(); $sql_f_t = "SELECT * FROM funnel_telephony WHERE agency_id = {$agency_id}"; $q_f_t = $pdo->query($sql_f_t); while($r_f_t = $pdo->fetch_assoc($q_f_t)){ $funnel_telephony[] = $r_f_t; } $alloClass = new AlloincognitoApiClass($agency_id, $pdo); if($alloClass->validate_agency($agency_id)){ $alloClass->set_authorization_token($authorization_token, $created_authorization_token); $arrTokens = array('access_token'=>$r_allo['access_token'], 'refresh_token'=>$r_allo['refresh_token'], 'access_token_expired_in'=>$r_allo['access_token_expired_in'], 'refresh_token_expired_in'=>$r_allo['refresh_token_expired_in'] ); $alloClass->set_access_token($arrTokens); $emps = $alloClass->get_emp(); if(isset($emps['result']) && isset($emps['result']['emps'])){ foreach($emps['result']['emps'] as $emp){ foreach($emp['nums'] as $num){ $nums[] = $num['value']; } } } // var_dump($nums); $start = date('Y-m-d',(strtotime($lost_date) - 4*60*60)).'T'.date('H:i:s', (strtotime($lost_date) - 4*60*60)).'.000Z'; $end = date('Y-m-d').'T'.date('H:i:s').'.000Z'; //echo $start.' '.$end."\n"; //2022-10-18T08:53:41.929Z $calls = $alloClass->get_calls($start, $end, $nums); // var_dump($calls); if(!empty($calls['result']) && !empty($calls['result']['rowset'])){ foreach($calls['result']['rowset'] as $call){ //Юзеры $user_find = $call['abonentNum']; if(isset($call['redirectNum'])){ $alloClass->checkUser($call['redirectNum'], $agency_id); $user_find = $call['redirectNum']; } else { $alloClass->checkUser($call['abonentNum'], $agency_id); } $call_id = $alloClass->stat($call, $agency_id); $alloClass->save_user_call($call_id, $user_find, $main_user_id); $type = $alloClass->get_phone_type($call); //echo $type."\n"; $call['start'] = strtotime($call['callDate']); $call['call_id'] = $call_id; switch ($type){ case 'alloAbonentAnswer': { $call['comment'] = 'Клиент записан по исходящему звонку в Алло Инкогнито'; $call['comment_in'] = 'Исходящий звонок в Алло Инкогнито'; $call['user_find_number'] = $user_find; $call['client_find_number'] = $call['extNum']; $phones_type['alloAbonentAnswer'][] = $call; break; } case 'abonentAlloAnswer': { $call['comment'] = 'Клиент записан по входящему звонку в Алло Инкогнито'; $call['comment_in'] = 'Входящий звонок в Алло Инкогнито'; $call['user_find_number'] = $user_find; $call['client_find_number'] = $call['extNum']; $call->comment_in .= ' '.'(Сотрудник '.$call->abonentName.')'; $phones_type['abonentAlloAnswer'][] = $call; break; } case 'alloAbonentNoAnswer': { $call['comment'] = 'Клиент записан по исходящему звонку в Алло Инкогнито'; $call['comment_in'] = 'Исходящий звонок в Алло Инкогнито.
Абонент не ответил'; $call['user_find_number'] = $user_find; $call['client_find_number'] = $call['extNum']; $phones_type['alloAbonentNoAnswer'][] = $call; break; } case 'abonentAlloNoAnswer':{ $call['comment'] = 'Клиент записан по входящему звонку в Алло Инкогнито'; $call['comment_in'] = 'Пропущеный входящий звонок в Алло Инкогнито'; $call['user_find_number'] = $user_find; $call['client_find_number'] = $call['extNum']; $phones_type['abonentAlloNoAnswer'][] = $call; break; } } } } if(isset($phones_type['alloAbonentAnswer'])){ foreach ($phones_type['alloAbonentAnswer'] as $p) { read_phone($alloClass, $p, $agency_id, 'alloAbonentAnswer', $main_user_id, $funnel_telephony); } } if(isset($phones_type['abonentAlloAnswer'])){ foreach ($phones_type['abonentAlloAnswer'] as $p) { read_phone($alloClass, $p, $agency_id, 'abonentAlloAnswer', $main_user_id, $funnel_telephony); } } if(isset($phones_type['alloAbonentNoAnswer'])){ foreach ($phones_type['alloAbonentNoAnswer'] as $p) { read_phone($alloClass, $p, $agency_id, 'alloAbonentNoAnswer', $main_user_id, $funnel_telephony); } } if(isset($phones_type['abonentAlloNoAnswer'])){ foreach ($phones_type['abonentAlloNoAnswer'] as $p) { read_phone($alloClass, $p, $agency_id, 'abonentAlloNoAnswer', $main_user_id, $funnel_telephony); } } } $sql_up_allo = "UPDATE agency_alloincognito SET lost_date = '".date('Y-m-d H:i:s')."' WHERE id = ".$r_allo['id']; $pdo->query($sql_up_allo); }