287 lines
12 KiB
PHP
287 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 = '{"0":{"date":"2025-03-31 07:39:54.411637+00:00","phone":"79285225361","project":"A-RF","project_tag":"c3RyZWFtX2lkXzIwOTc="},"1":{"date":"2025-03-31 07:39:54.411637+00:00","phone":"79281936574","project":"A-RF","project_tag":"c3RyZWFtX2lkXzIwOTc="},"2":{"date":"2025-03-31 07:39:54.411637+00:00","phone":"79116081868","project":"A-RF","project_tag":"c3RyZWFtX2lkXzIwOTc="},"token":"61d64a35558b92a5dc79dc4f5d6bbc5f"}';
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
//$cont_arr = explode("\n", $content);
|
||
|
|
|
||
|
|
//var_dump($cont_arr);
|
||
|
|
|
||
|
|
//die();
|
||
|
|
//foreach($cont_arr as $content){
|
||
|
|
$messages = json_decode($content, true);
|
||
|
|
|
||
|
|
//var_dump($messages);
|
||
|
|
|
||
|
|
|
||
|
|
if(!empty($messages)){
|
||
|
|
$agency_id = 19895;
|
||
|
|
}
|
||
|
|
|
||
|
|
if(!isset($agency_id)){
|
||
|
|
die();
|
||
|
|
|
||
|
|
} else {
|
||
|
|
foreach($messages as $key => $message){
|
||
|
|
// var_dump($message);
|
||
|
|
|
||
|
|
$user_id = $agency_id;
|
||
|
|
$user = new User();
|
||
|
|
$user->get($user_id);
|
||
|
|
$tag = trim($message['project_tag']);
|
||
|
|
$phone = '+'.$message['phone'];
|
||
|
|
$project = $message['project'];
|
||
|
|
|
||
|
|
var_dump($phone);
|
||
|
|
|
||
|
|
if(!empty($phone)){
|
||
|
|
|
||
|
|
|
||
|
|
$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));
|
||
|
|
|
||
|
|
|
||
|
|
$fio_client = "Без имени";
|
||
|
|
$opis = 'Имя проекта: '.$project;
|
||
|
|
$opis .= "<br>";
|
||
|
|
$opis .= 'тег: '.$tag;
|
||
|
|
// $fio_client = $project;
|
||
|
|
|
||
|
|
if($agency_id == 19895){
|
||
|
|
$opis = '';
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
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 tag = '{$project}'";
|
||
|
|
$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
|
||
|
|
= '".$project."' 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='".$project."', user_id={$user_id}, manager_id={$user->agencyId}";
|
||
|
|
$pdo->query($sql_up);
|
||
|
|
$source_id = $pdo->insert_id();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$activities_ids = [];
|
||
|
|
//Поиск тега
|
||
|
|
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'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
} else {
|
||
|
|
$sql_activites = "SELECT id FROM `activities` WHERE name = '".$tag."' AND (user_id in (".implode(',', $users).") or manager_id in (".implode(',', $users)."))";
|
||
|
|
|
||
|
|
$q_aclivites = $pdo->query($sql_activites);
|
||
|
|
$r_activites = $pdo->fetch_assoc($q_aclivites);
|
||
|
|
if($r_activites['id'] > 0){
|
||
|
|
$activities_id = $r_activites['id'];
|
||
|
|
//echo $activities_id."\n";
|
||
|
|
} else {
|
||
|
|
$sql_up = "INSERT INTO `activities` SET name='".$tag."', user_id={$user_id}, manager_id={$user->agencyId}";
|
||
|
|
$pdo->query($sql_up);
|
||
|
|
$activities_id = $pdo->insert_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;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
//Поиск клиента
|
||
|
|
$sql = "SELECT `id`, `stage`, `deleted` 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).")";
|
||
|
|
//echo $sql."\n";
|
||
|
|
// die();
|
||
|
|
$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}', hot={$hot}, fio='{$fio_client}', 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=7";
|
||
|
|
echo $sql."\n";
|
||
|
|
$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);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
} else {
|
||
|
|
//Поиск воронки по источнику
|
||
|
|
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=7, who_work={$who_work}, type_id=1, confirm={$confirm}, funnel_id={$funnel_id_req}, stage=1, source= '{$source_id}'";
|
||
|
|
// echo $sql."\n";
|
||
|
|
|
||
|
|
$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 = 'Имя проекта: '.$project;
|
||
|
|
|
||
|
|
$sql_add = "INSERT INTO `events_clients` (`user_id`, `req_id`, `from_id`, `event`, `dop_text`, `read`) ".
|
||
|
|
"VALUES ({$user_id}, {$req_id}, {$who_work}, 'add', '{$comment}', 2)";
|
||
|
|
$pdo->query($sql_add);
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if($agency_id == 19895 && $req_id > 0){
|
||
|
|
$rqClass = new Requisitions(null, true);
|
||
|
|
$rqClass->set_agencyId($agency_id);
|
||
|
|
$rqClass->set_userId($user_id);
|
||
|
|
$rqClass->autoUnitReq(1, $req_id);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|