321 lines
12 KiB
PHP
321 lines
12 KiB
PHP
<?php
|
||
require_once($_SERVER['DOCUMENT_ROOT']."/config.php");
|
||
function clearInputElement($el2) // очистка входящего элимента
|
||
{
|
||
|
||
$el = $el2;
|
||
|
||
$el = strip_tags($el); // от тегов
|
||
|
||
$el = htmlspecialchars($el); // от кавычек и прочего
|
||
|
||
$el = str_replace("\n", "<br>", $el);
|
||
|
||
$el = str_replace("\r", "", $el);
|
||
|
||
$el = mysql_real_escape_string($el); // от MySQL-инъекций
|
||
|
||
$el = trim($el); // от лишних пробелов
|
||
|
||
$el = stripslashes($el); // от слэшей
|
||
|
||
return $el;
|
||
|
||
}
|
||
|
||
header('Content-Type: application/json');
|
||
|
||
ini_set('display_errors', 1);
|
||
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE);
|
||
|
||
$pdo = new MysqlPdo(hst, ndb, user, pass);
|
||
$sql = "SET NAMES 'utf8'";
|
||
$pdo->query($sql);
|
||
|
||
$content = file_get_contents('php://input');
|
||
|
||
|
||
/*if($_SESSION['id'] == 19895){
|
||
|
||
$content = '{"vid":941016816,"site":"gk.livingstone-hotel.ru","comment":"Comments","time":1741681952,"page":"http:\/\/villa-vsochi.ru\/subscribe\/","ref":"http:\/\/google.am\/search","browser":"Chrome","device":"Desktop","platform":"Win10","country":"Россия","region":"Москва","city":"Москва","ip":"99.101.202.77","phones":["11111111111","77777777777"],"mails":["hovasapyan777@mail.ru","hovasapyan888@mail.ru"],"utm":{"utm_term":"Купить BMW Mini у официалов в Москве","utm_source":"google","utm_campaign":"MyCampaign","utm_medium":"cpc","utm_content":"textlink","gclid":"CjwKCAiAkrTjBRAoEiwAXpf9CeZ9Dn"},"uuid":"8a1090c2-f10b-11eb-9a03-0242ac130003","token":"61d64a35558b92a5dc79dc4f5d6bbc5f"}';
|
||
|
||
}*/
|
||
|
||
|
||
$message = json_decode($content, true);
|
||
|
||
//var_dump($message);
|
||
|
||
if(!empty($message)){
|
||
$agency_id = 19895;
|
||
}
|
||
|
||
if(!isset($agency_id)){
|
||
die();
|
||
|
||
} else {
|
||
$user_id = $agency_id;
|
||
$user = new User();
|
||
$user->get($user_id);
|
||
$domain = $message['site'];
|
||
|
||
|
||
$CallEvents = new CallEvents();
|
||
|
||
$funnel_id = 0;
|
||
$user_id = 0;
|
||
$funnel_id_req = 0;
|
||
$activities_id = 0;
|
||
$source_id = 0;
|
||
$users = array();
|
||
$client_id = 0;
|
||
$email = '';
|
||
$partner = 0;
|
||
$user_id_req = 0;
|
||
$agency_id = 0;
|
||
$confirm = 1;
|
||
|
||
if($user->agencyId == 19895){
|
||
$user_id = 19895;
|
||
$agency_id = 19895;
|
||
$funnel_id_req = 1513;
|
||
$user_id_req = 19895;
|
||
}
|
||
|
||
|
||
$users = array_keys($CallEvents->getAllUserAgency($user->agencyId, $users));
|
||
//var_dump($users);
|
||
|
||
$fio_client = "Без имени";
|
||
$opis = 'Домен: '.$domain;
|
||
|
||
$phones = [];
|
||
//var_dump($message['phones']);
|
||
if(isset($message['phones'])){
|
||
foreach ($message['phones'] as $phone){
|
||
//if($fio_client != '') $fio_client .= ' | ';
|
||
//$fio_client .= $phone;
|
||
$phones[] = '+'.$phone;
|
||
}
|
||
}
|
||
|
||
if(empty($phones)) die();
|
||
|
||
//var_dump($phones);
|
||
//echo $fio_client."\n";
|
||
|
||
if($source_id == 0){
|
||
//Поиск источника по домену
|
||
$sql_d = "SELECT *, (SELECT group_id FROM advertising_sources WHERE id = advertising_sources_domain.adv_id) AS group_id FROM `advertising_sources_domain` WHERE domain = '{$domain}' AND `type` = 1";
|
||
$q_d = $pdo->query($sql_d, true);
|
||
$r_d = $pdo->fetch_assoc($q_d);
|
||
if($r_d){
|
||
$source_id = (int)$r_d['adv_id'];
|
||
$group_id = (int)$r_d['group_id'];
|
||
}
|
||
}
|
||
|
||
if($source_id == 0){
|
||
//Поиск источника
|
||
$sql_source = "SELECT id FROM `advertising_sources` WHERE source
|
||
= '".$nameSourse."' AND (user_id in (".implode(',', $users).") or manager_id in (".implode(',', $users)."))";
|
||
$q_source = $pdo->query($sql_source);
|
||
$r_source = $pdo->fetch_assoc($q_source);
|
||
if($r_source['id'] > 0){
|
||
$source_id = $r_source['id'];
|
||
//echo $source_id."\n";
|
||
} else {
|
||
$sql_up = "INSERT INTO `advertising_sources` SET source='".$nameSourse."', user_id={$user_id}, manager_id={$user->agencyId}";
|
||
$pdo->query($sql_up);
|
||
$source_id = $pdo->insert_id();
|
||
}
|
||
}
|
||
if($agency_id == 19895){
|
||
if($source_id > 0){
|
||
$sql_aa = "SELECT * FROM advertising_sources_activities WHERE source_id = {$source_id}";
|
||
$q_aa = $pdo->query($sql_aa, true);
|
||
|
||
while($r_aa = $pdo->fetch_assoc($q_aa)) {
|
||
$activities_ids[] = (int)$r_aa['activities_id'];
|
||
}
|
||
}
|
||
|
||
}
|
||
$hot = 0;
|
||
if($agency_id == 19895 && $group_id == 290){
|
||
$hot = 1;
|
||
}
|
||
$confirm = 1;
|
||
$who_work = $user_id;
|
||
if($agency_id == 19895){
|
||
//Поиск ответственного по источнику
|
||
$sql_w = "SELECT * FROM advertising_sources_who_work WHERE adv_id = {$source_id}";
|
||
|
||
$q_w = $pdo->query($sql_w);
|
||
if($pdo->num_rows($q_w) > 0){
|
||
$r_w = $pdo->fetch_assoc($q_w);
|
||
$user_id = $who_work = (int)$r_w['who_work'];
|
||
} else {
|
||
$confirm = 0;
|
||
$who_work = 0;
|
||
}
|
||
}
|
||
|
||
if(isset($message['mails'])){
|
||
$email = $message['mails'][0];
|
||
}
|
||
|
||
if(isset($message['utm'])){
|
||
foreach($message['utm'] as $key => $field){
|
||
if($opis != '') $opis .= "<br>";
|
||
$opis .= $key . ": " . $field;
|
||
|
||
}
|
||
}
|
||
|
||
if($agency_id == 19895){
|
||
$opis = '';
|
||
}
|
||
|
||
|
||
//Поиск клиента
|
||
$sql = "SELECT `id`, `stage`, `deleted` FROM `clients` WHERE cancel=0 AND (phone in ('".implode('\',\'',$phones)."') OR id in (SELECT client_id FROM clients_phone2 WHERE phone in ('".implode('\',\'',$phones)."'))) and id_agent in (".implode(',', $users).")";
|
||
//echo $sql."\n";
|
||
|
||
$client = $pdo->fetch_assoc($pdo->query($sql));
|
||
if(!empty($client) && $client){
|
||
$client_id = $client['id'];
|
||
} else {
|
||
$phone=$phones[0];
|
||
$sql = "INSERT INTO clients SET phone='{$phone}', email='{$email}', fio='{$fio_client}', hot={$hot}, opis='{$opis}', id_agent={$user_id}, who_work={$who_work}, date_add=NOW(), confirm={$confirm}, funnel_id={$funnel_id}, stage=1, source= '{$source_id}', integration=8";
|
||
//echo $sql;
|
||
$pdo->query($sql);
|
||
$client_id = $pdo->insert_id();
|
||
|
||
if($funnel_id > 0){
|
||
$Clients = new Clients;
|
||
$Clients->get_class_etap($client_id, $funnel_id);
|
||
}
|
||
if($activities_id){
|
||
$sql_a = "INSERT INTO `clients_activities` (`activity_id`, `client_id`) VALUES ({$activities_id}, {$client_id})";
|
||
$pdo->query($sql_a);
|
||
} else if($activities_ids && !empty($activities_ids)){
|
||
foreach($activities_ids as $activities_id){
|
||
$sql_a = "INSERT INTO `clients_activities` (`activity_id`, `client_id`) VALUES ({$activities_id}, {$client_id})";
|
||
$pdo->query($sql_a);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
if($client_id > 0){
|
||
$web_hook_in = new WebHookIn($pdo);
|
||
$reqid=(int)$web_hook_in->check_is_req($who_work, $source_id, $client_id);
|
||
if($reqid > 0 && $agency_id == 19895) {
|
||
$comment = "Обращался повторно<br>";
|
||
if(!empty($activities_ids)){
|
||
$sql_activites = "SELECT * FROM activities WHERE id in (".implode(',', $activities_ids).")";
|
||
$q_aclivites = $pdo->query($sql_activites);
|
||
$comment .= "Теги: ";
|
||
while($r_activites = $pdo->fetch_assoc($q_aclivites)){
|
||
$comment .= $r_activites['name']." ";
|
||
}
|
||
$sql_ev = "INSERT INTO user_client_events (req_id, `type`, comment, user_id, from_id, create_date, calendar_view)
|
||
VALUES ($reqid, 'comment', '{$comment}', {$user_id}, {$user_id}, NOW(), 1)";
|
||
$pdo->query($sql_ev);
|
||
|
||
}
|
||
die();
|
||
}
|
||
|
||
//Поиск воронки по источнику
|
||
if($source_id > 0){
|
||
$sql_req_funnel="SELECT * FROM advertising_sources_funnel WHERE source_id = {$source_id}";
|
||
$q_req_funnel = $pdo->query($sql_req_funnel, true);
|
||
$r_req_funnel = $pdo->fetch_assoc($q_req_funnel);
|
||
if($r_req_funnel && $r_req_funnel['funnel_id_req'] > 0){
|
||
$funnel_id_req = (int)$r_req_funnel['funnel_id_req'];
|
||
}
|
||
}
|
||
$sql = "INSERT INTO requisitions SET client_id={$client_id}, hot={$hot}, name='{$fio_client}', description='{$opis}', user_id={$user_id}, integration=8, who_work={$who_work}, type_id=1, confirm={$confirm}, funnel_id={$funnel_id_req}, stage=1, source= '{$source_id}'";
|
||
|
||
|
||
$pdo->query($sql);
|
||
$req_id = $pdo->insert_id();
|
||
if($funnel_id_req > 0){
|
||
$Requisitions = new Requisitions();
|
||
$Requisitions->get_class_etap($req_id, $funnel_id_req);
|
||
}
|
||
|
||
if($activities_ids && !empty($activities_ids)){
|
||
foreach($activities_ids as $activities_id){
|
||
$sql_a = "INSERT INTO `requisitions_activities` (`activity_id`, `req_id`) VALUES ({$activities_id}, {$req_id})";
|
||
$pdo->query($sql_a);
|
||
}
|
||
} else
|
||
if($activities_id){
|
||
$sql_a = "INSERT INTO `requisitions_activities` (`activity_id`, `req_id`) VALUES ({$activities_id}, {$req_id})";
|
||
$pdo->query($sql_a);
|
||
}
|
||
|
||
if($who_work > 0){
|
||
$comment = "Домен ".$domain;
|
||
if($agency_id == 19895) {
|
||
$comment = '';
|
||
}
|
||
|
||
$sql_add = "INSERT INTO `events_clients` (`user_id`, `req_id`, `from_id`, `event`, `dop_text`, `read`,`is_not_show`) ".
|
||
"VALUES ({$user_id}, {$req_id}, {$who_work}, 'add', '{$comment}', 2, 1)";
|
||
$pdo->query($sql_add);
|
||
|
||
/*$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, $req_id, '".date('Y-m-d H:i:s')."', '".date('Y-m-d H:i:s', strtotime('+15 minutes'))."', 'call', '{$comment}', 0, {$user_id_req})";
|
||
$pdo->query($sql_in_ev);*/
|
||
}
|
||
|
||
if($agency_id == 19895 && $req_id > 0){
|
||
$arr_data = array();
|
||
if(isset($message['utm'])){
|
||
foreach($message['utm'] as $key => $field){
|
||
$field_name = str_replace('utm_', 'UTM ',trim($key));
|
||
$field_val = trim($field);
|
||
$sql_fields = "SELECT * FROM `client_req_fields` WHERE title = '{$field_name}' AND agency_id = {$agency_id}";
|
||
// echo $sql_fields."\n";
|
||
$q_fields = $pdo->query($sql_fields);
|
||
$r_fields = $pdo->fetch_assoc($q_fields);
|
||
if($r_fields){
|
||
|
||
if($r_fields['type'] == 1 || $r_fields['type'] == 0){
|
||
|
||
|
||
$arr_data[] = "({$req_id}, {$r_fields['id']}, 1, '\"{$field_val}\"')";
|
||
|
||
} elseif ($r_fields['type'] == 2){
|
||
$params = json_decode($r_fields['params'], true);
|
||
//var_dump($params['items']);
|
||
foreach($params['items'] as $item){
|
||
if(trim($item['name']) == $field_val){
|
||
$id_item = $item['value'];
|
||
$arr_data[] = "({$req_id}, {$r_fields['id']}, 1, {$id_item})";
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if(!empty($arr_data)){
|
||
$sql_data = "INSERT INTO requisition_data (`req_id`, `field_id`, `type`, `value`) VALUES ".implode(',' , $arr_data);
|
||
//echo $sql_data;
|
||
$pdo->query($sql_data);
|
||
}
|
||
|
||
$rqClass = new Requisitions(null, true);
|
||
$rqClass->set_agencyId($agency_id);
|
||
$rqClass->set_userId($user_id);
|
||
$rqClass->autoUnitReq(1, $req_id);
|
||
|
||
}
|
||
|
||
}
|
||
|
||
} |