Joywork/ajax/getFunnels.php
2026-05-22 21:21:54 +03:00

323 lines
17 KiB
PHP

<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
$get = clearInputData($_GET);
$post = clearInputData($_POST);
$isShowFunnels = true;
$arrayFunnel = array();
$arrayOrder = array();
$arrayId = array();
$arrNoSeeUsers = array();
if ($_SESSION['id'] && isset($post['section'])) {
//$agency_id = $_SESSION['agency_id'];
//if($agency_id == 0){
$user_id = intval($_SESSION['id']);
$user = new User;
$user->get($user_id);
$agency_id = $user->agencyId;
//}
/* $nameDefault = "Обычные";
$sqlCastom = "SELECT * FROM funnel_default WHERE agency_id = {$agency_id}";
$qCastom = mysql_query($sqlCastom);
if(mysql_num_rows($qCastom) > 0){
$rCastom = mysql_fetch_assoc($qCastom);
if(!empty($rCastom['name'])) $nameDefault = $rCastom['name'];
if($rCastom['is_show_funnels'] == 0) $isShowFunnels = false;
$rCastom['id'] = 0;
$arrayFunnel[] = $rCastom;
} else {
$arrayFunnel[] = array('id' => 0, 'name' => 'Обычные', 'is_client' => 1, 'is_req' => 1, 'is_stage' => 1, 'is_show_funnels' => 1, 'order_number' => 0);
}*/
$funnelId = $post['funnel_id'];
$section = $post['section'];
$useCustomFunnel = boolval($post['use_custom']);
if ($useCustomFunnel || $agency_id == 8353){
$nameDefault = "Обычные";
$sqlCastom = "SELECT * FROM funnel_default WHERE agency_id = {$agency_id}";
$qCastom = mysql_query($sqlCastom);
if(mysql_num_rows($qCastom) > 0){
$rCastom = mysql_fetch_assoc($qCastom);
if(!empty($rCastom['name'])) $nameDefault = $rCastom['name'];
if($rCastom['is_show_funnels'] == 0) $isShowFunnels = false;
$rCastom['id'] = 0;
$arrayFunnel[] = $rCastom;
} else {
$arrayFunnel[] = array('id' => 0, 'name' => 'Обычные', 'is_client' => 1, 'is_req' => 1, 'is_stage' => 1, 'is_show_funnels' => 1, 'order_number' => 0);
}
}
?>
<ul class="funnels_list" style="display: <?=(!$isShowFunnels) ? 'none' : 'inline-block'?>; margin: 0px 0 5px 16px;">
<?php
$_SESSION['new_transfers']['clients'] = null;
$_SESSION['new_transfers']['requisitions'] = null;
$funnelCounts = [];
if ($section == 'clients' && ($post['filter_stage_work'] || $post['filter_stage_hot'])) {
$bd_stage = "";
if ($post['filter_stage_work']) {
if ($post['filter_confirm'] || $post['filter_stage_send']) {
$bd_stage = " AND `clients`.`deleted` <> '1'";
} else {
$bd_stage = " AND `clients`.`deleted` <> '1' AND `clients`.`confirm` > 0";
}
}
if ($post['filter_stage_hot']) {
$bd_stage .= " AND `clients`.`hot` = 1";
}
if (empty($bd_stage))
$bd_stage = " AND `clients`.`deleted` = '0'";
$sql1 = "SELECT `clients`.`funnel_id`, COUNT(DISTINCT `clients`.`id`) AS `count`
FROM `events_clients` AS `events`
LEFT JOIN `clients` AS `clients` ON `clients`.`id` = `events`.`client_id`
WHERE (`events`.`user_id` = '$_SESSION[id]' AND `events`.`from_id` = '$_SESSION[id]') AND
(`events`.`event` = 'accepted' OR `events`.`event` = 'accepted_doer') AND
`events`.`viewed` = 0 AND `events`.`read` = 1 AND
(`clients`.`confirm` = 1 OR `clients`.`doers_confirm` = 1) AND
`events`.`req_id` = 0 $bd_stage AND
(`clients`.`who_work` = '$_SESSION[id]' OR `clients`.`doers` LIKE '%&quot;".$_SESSION['id']."&quot;:1%') AND
`clients`.`funnel_id` = 0
GROUP BY `clients`.`funnel_id`";
$sql1_list = "SELECT `clients`.`funnel_id`, `clients`.`id`
FROM `events_clients` AS `events`
LEFT JOIN `clients` AS `clients` ON `clients`.`id` = `events`.`client_id`
WHERE (`events`.`user_id` = '$_SESSION[id]' AND `events`.`from_id` = '$_SESSION[id]') AND
(`events`.`event` = 'accepted' OR `events`.`event` = 'accepted_doer') AND
`events`.`viewed` = 0 AND `events`.`read` = 1 AND
(`clients`.`confirm` = 1 OR `clients`.`doers_confirm` = 1) AND
`events`.`req_id` = 0 $bd_stage AND
(`clients`.`who_work` = '$_SESSION[id]' OR `clients`.`doers` LIKE '%&quot;".$_SESSION['id']."&quot;:1%') AND
`clients`.`funnel_id` = 0";
$sql2 = "SELECT `clients`.`funnel_id`, COUNT(DISTINCT `clients`.`id`) AS `count`
FROM `events_clients` AS `events`
LEFT JOIN `clients` AS `clients` ON `clients`.`id` = `events`.`client_id`
JOIN `funnel` AS `funnel`
WHERE (`events`.`user_id` = '$_SESSION[id]' AND `events`.`from_id` = '$_SESSION[id]') AND
(`events`.`event` = 'accepted' OR `events`.`event` = 'accepted_doer') AND
`events`.`viewed` = 0 AND `events`.`read` = 1 AND
(`clients`.`confirm` = 1 OR `clients`.`doers_confirm` = 1) AND
`events`.`req_id` = 0 $bd_stage AND
(`clients`.`who_work` = '$_SESSION[id]' OR `clients`.`doers` LIKE '%&quot;".$_SESSION['id']."&quot;:1%') AND
`clients`.`funnel_id` = `funnel`.`id` AND
`funnel`.`agency_id` = '$agency_id' AND `funnel`.`deleted` = 0
GROUP BY `clients`.`funnel_id`";
$sql2_list = "SELECT `clients`.`funnel_id`, `clients`.`id`
FROM `events_clients` AS `events`
LEFT JOIN `clients` AS `clients` ON `clients`.`id` = `events`.`client_id`
JOIN `funnel` AS `funnel`
WHERE (`events`.`user_id` = '$_SESSION[id]' AND `events`.`from_id` = '$_SESSION[id]') AND
(`events`.`event` = 'accepted' OR `events`.`event` = 'accepted_doer') AND
`events`.`viewed` = 0 AND `events`.`read` = 1 AND
(`clients`.`confirm` = 1 OR `clients`.`doers_confirm` = 1) AND
`events`.`req_id` = 0 $bd_stage AND
(`clients`.`who_work` = '$_SESSION[id]' OR `clients`.`doers` LIKE '%&quot;".$_SESSION['id']."&quot;:1%') AND
`clients`.`funnel_id` = `funnel`.`id` AND
`funnel`.`agency_id` = '$agency_id' AND `funnel`.`deleted` = 0";
} else if ($section == 'requisitions') {
$sql1 = "SELECT `req`.`funnel_id`, COUNT(DISTINCT `req`.`id`) AS `count`
FROM `events_clients` AS `events`
LEFT JOIN `requisitions` AS `req` ON `req`.`id` = `events`.`req_id`
WHERE (`events`.`user_id` = '$_SESSION[id]' AND `events`.`from_id` = '$_SESSION[id]') AND
(`events`.`event` = 'accepted' OR `events`.`event` = 'accepted_doer') AND
`events`.`viewed` = 0 AND `events`.`read` = 1 AND
(`req`.`confirm` = 1 OR `req`.`doers_confirm` = 1) AND
`events`.`client_id` = 0 AND `req`.`deleted` = 0 AND `req`.`cancel` = 0 AND
(`req`.`who_work` = '$_SESSION[id]' OR `req`.`doers` LIKE '%&quot;".$_SESSION['id']."&quot;:1%') AND
`req`.`funnel_id` = 0
GROUP BY `req`.`funnel_id`";
$sql1_list = "SELECT `req`.`funnel_id`, `req`.`id`
FROM `events_clients` AS `events`
LEFT JOIN `requisitions` AS `req` ON `req`.`id` = `events`.`req_id`
WHERE (`events`.`user_id` = '$_SESSION[id]' AND `events`.`from_id` = '$_SESSION[id]') AND
(`events`.`event` = 'accepted' OR `events`.`event` = 'accepted_doer') AND
`events`.`viewed` = 0 AND `events`.`read` = 1 AND
(`req`.`confirm` = 1 OR `req`.`doers_confirm` = 1) AND
`events`.`client_id` = 0 AND `req`.`deleted` = 0 AND `req`.`cancel` = 0 AND
(`req`.`who_work` = '$_SESSION[id]' OR `req`.`doers` LIKE '%&quot;".$_SESSION['id']."&quot;:1%') AND
`req`.`funnel_id` = 0";
$sql2 = "SELECT `req`.`funnel_id`, COUNT(DISTINCT `req`.`id`) AS `count`
FROM `events_clients` AS `events`
LEFT JOIN `requisitions` AS `req` ON `req`.`id` = `events`.`req_id`
JOIN `funnel` AS `funnel`
WHERE (`events`.`user_id` = '$_SESSION[id]' AND `events`.`from_id` = '$_SESSION[id]') AND
(`events`.`event` = 'accepted' OR `events`.`event` = 'accepted_doer') AND
`events`.`viewed` = 0 AND `events`.`read` = 1 AND
(`req`.`confirm` = 1 OR `req`.`doers_confirm` = 1) AND
`events`.`client_id` = 0 AND `req`.`deleted` = 0 AND
(`req`.`who_work` = '$_SESSION[id]' OR `req`.`doers` LIKE '%&quot;".$_SESSION['id']."&quot;:1%') AND
`req`.`funnel_id` = `funnel`.`id` AND `req`.`cancel` = 0 AND
`funnel`.`agency_id` = '$agency_id' AND `funnel`.`deleted` = 0
GROUP BY `req`.`funnel_id`";
$sql2_list = "SELECT `req`.`funnel_id`, `req`.`id`
FROM `events_clients` AS `events`
LEFT JOIN `requisitions` AS `req` ON `req`.`id` = `events`.`req_id`
JOIN `funnel` AS `funnel`
WHERE (`events`.`user_id` = '$_SESSION[id]' AND `events`.`from_id` = '$_SESSION[id]') AND
(`events`.`event` = 'accepted' OR `events`.`event` = 'accepted_doer') AND
`events`.`viewed` = 0 AND `events`.`read` = 1 AND
(`req`.`confirm` = 1 OR `req`.`doers_confirm` = 1) AND
`events`.`client_id` = 0 AND `req`.`deleted` = 0 AND `req`.`cancel` = 0 AND
(`req`.`who_work` = '$_SESSION[id]' OR `req`.`doers` LIKE '%&quot;".$_SESSION['id']."&quot;:1%') AND
`req`.`funnel_id` = `funnel`.`id` AND
`funnel`.`agency_id` = '$agency_id' AND `funnel`.`deleted` = 0";
}
$new_ids = [];
$res1 = mysql_query($sql1);
if (mysql_num_rows($res1)) {
while($counter = mysql_fetch_assoc($res1)) {
$funnelCounts[$counter['funnel_id']] = $counter['count'];
}
}
$res1_list = mysql_query($sql1_list);
if (mysql_num_rows($res1_list)) {
while($row = mysql_fetch_assoc($res1_list)) {
$new_ids[] = $row['id'];
}
}
$res2 = mysql_query($sql2);
if (mysql_num_rows($res2)) {
while($counter = mysql_fetch_assoc($res2)) {
$funnelCounts[$counter['funnel_id']] = $counter['count'];
}
}
$res2_list = mysql_query($sql2_list);
if (mysql_num_rows($res2_list)) {
while($row = mysql_fetch_assoc($res2_list)) {
$new_ids[] = $row['id'];
}
}
if (count($new_ids) > 0) {
if (($section == 'clients'))
$_SESSION['new_transfers']['clients'] = $new_ids;
else if (($section == 'requisitions'))
$_SESSION['new_transfers']['requisitions'] = $new_ids;
}
/* if ($useCustomFunnel || $agency_id == 8353 || $agency_id == 5238) {
?>
<li data-id="0" id="liFunnel" class="<?php if ($funnelId == null || $funnelId == 0) {echo 'active';}?>">
<?=$nameDefault?>
<?= ($funnelCounts[0] > 0) ? '<span class="counter">'.$funnelCounts[0].'</span>' : "" ?>
</li>
<?php
}*/
$funnelsSql = "SELECT * FROM `funnel`
WHERE `agency_id` = '$agency_id' AND `deleted`= 0 and is_client = 1
ORDER BY order_number desc, `id`";
if($section == 'requisitions') {
$funnelsSql = "SELECT * FROM `funnel`
WHERE `agency_id` = '$agency_id' AND `deleted`= 0 and is_req = 1
ORDER BY id";
if ($agency_id == 9588) {
$funnelsSql = "SELECT * FROM `funnel`
WHERE `agency_id` = '$agency_id' AND `deleted`= 0 and is_req = 1
ORDER BY order_number desc, id";
} else if ($agency_id == 11082) {
$funnelsSql = "SELECT * FROM `funnel`
WHERE `agency_id` = '$agency_id' AND `deleted`= 0 and is_req = 1
ORDER BY order_number ASC, id";
}
}
$funnelsData = mysql_query($funnelsSql);
$i = 0;
while($funnel = mysql_fetch_assoc($funnelsData)) {
$arrNoSeeUsers = array();
if($funnel['is_no_see']==1 && !empty($funnel['users_no_see'])){
$tempArrNoSee = json_decode($funnel['users_no_see'], true);
foreach($tempArrNoSee as $u){
if(strpos($u, 'all_') !== false){
$manId = str_replace('all_', '', $u);
$sql_u = "SELECT id FROM `users`
WHERE id = $manId OR
id_manager = $manId OR
id_manager IN (SELECT id FROM users WHERE id_manager = $manId)";
$q_u = mysql_query($sql_u);
while($r_u = mysql_fetch_row($q_u)){
$arrNoSeeUsers[] = (int)$r_u[0];
}
} else {
$arrNoSeeUsers[] = (int)$u;
}
}
}
$funnel['arr_no_see'] = $arrNoSeeUsers;
$arrayFunnel[] = $funnel;
/*
?>
<li data-id="<?= $funnel['id'] ?>" id="liFunnel<?= $funnel['id'] ?>"
class="<?= ((!$useCustomFunnel && $i == 0 && $funnelId == null) || ($funnelId != null && $funnel['id'] == $funnelId)) ? 'active' : '' ?>">
<?= $funnel['name'] ?>
<?= ($funnelCounts[intval($funnel['id'])] > 0) ? '<span class="counter">'.$funnelCounts[$funnel['id']].'</span>' : "" ?>
</li>
<?php
$i++;*/
}
// if($_SESSION['id'] == 5238){
foreach($arrayFunnel as $key=>$funnel){
$arrayOrder[$key]=$funnel['order_number'];
$arrayId[$key]=$funnel['id'];
}
array_multisort($arrayOrder, SORT_ASC, $arrayId, SORT_ASC, $arrayFunnel);
//var_dump($arrayFunnel);
foreach($arrayFunnel as $key => $funnel){
$is_see = true;
if($funnel['is_no_see'] == 1 && !empty($funnel['users_no_see'])){
$is_see = false;
if(in_array($_SESSION['id'], $funnel['arr_no_see'])){
$is_see = true;
}
}
if($is_see){
?>
<li data-id="<?= $funnel['id'] ?>" id="liFunnel<?= $funnel['id'] ?>" data-stage="<?= ($funnel['id'] == 0 && isset($funnel['is_stage']) ? $funnel['is_stage'] : '1') ?>"
class="<?= ((!$useCustomFunnel && $key == 0 && $funnelId == null) || ($funnelId != null && $funnel['id'] == $funnelId)) ? 'active' : '' ?>">
<?= $funnel['name'] ?>
<?= ($funnelCounts[intval($funnel['id'])] > 0) ? '<span class="counter">'.$funnelCounts[$funnel['id']].'</span>' : "" ?>
</li>
<?php
}
}
// }
?>
<li class="quest_funnel" title="Как настроить воронки продаж?" style="font-size:18px;">
<a style="border-bottom: none;" onclick="trialperiod.openVideo();" href="javascript:{}"><i class="ti-help-alt"></i></a>
</li>
</ul>
<?php
}
?>