require $_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/prolog_before.php";CModule::IncludeModule("iblock");$error = 0;$access = 0;$firms = array();$filter_firms = array();$filter_firms["LOGIC"] = "OR";$company_db = CIBlockElement::GetList(array("SORT" => "ASC"), array("IBLOCK_ID" => IBLOCK_COMPANY, "ACTIVE" => "Y", "PROPERTY_ADMIN" => $_SESSION["CRM_ADMIN"]["ADMIN"]["ID"]), false, false, array());while ($company = $company_db->GetNext()) {
$firms[] = $company;
$filter_firms[] = array("PROPERTY_COMPANY" => $company["ID"]);
}$filter = array();$filter["IBLOCK_ID"] = IBLOCK_LEADS;$filter["ACTIVE"] = "Y";$filter[] = $filter_firms;if (isset($_REQUEST["LEAD_ID"])) {
$filter["ID"] = $_REQUEST["LEAD_ID"];
}$leads_db = CIBlockElement::GetList(array("SORT" => "PROPERTY_DATE"), $filter, false, false, array());$lead = $leads_db->GetNext();if ($lead["ID"] > 0) {
$access = 1;
}if ($access == 1) {
if ($_REQUEST["ACTION"] == "DELETE" and $_REQUEST["ID"]) {
if (CIBlockElement::Delete($_REQUEST["ID"])) {
echo "1";
}
}
if ($_REQUEST["ACTION"] == "LOAD") {
if (isset($_SESSION["CRM"]["FILTER_LEADS"])) {
$statuses = array();
$status_default = 0;
$status_db = CIBlockElement::GetList(array("SORT" => "ASC"), array("IBLOCK_ID" => IBLOCK_GUIDES_STATUSLEADS, "ACTIVE" => "Y", "CODE" => $_SESSION["CRM_ADMIN"]["ADMIN"]["ID"]), false, false, array("ID", "NAME", "PROPERTY_DEFAULT", "PROPERTY_COLOR"));
while ($status_item = $status_db->GetNext()) {
if ($status_item["PROPERTY_DEFAULT_VALUE"] == "да") {
$status_default = $status_item["NAME"];
}
$statuses["ID"][$status_item["ID"]] = $status_item;
$statuses["NAME"][$status_item["NAME"]] = $status_item;
}
$users = array();
foreach ($firms as $firm) {
$db_props = CIBlockElement::GetProperty(IBLOCK_COMPANY, $firm["ID"], array(), array("CODE" => "USERS"));
while ($prop = $db_props->GetNext()) {
$rsUser = CUser::GetByID($prop["VALUE"]);
$UserData = $rsUser->Fetch();
$users[$UserData["ID"]] = $UserData;
}
}
$summ_all = 0;
$status_leads = array();
$managers = array();
$leads_db = CIBlockElement::GetList(array("SORT" => "PROPERTY_DATE"), $_SESSION["CRM"]["FILTER_LEADS"], false, false, array());
while ($lead_tmp = $leads_db->GetNext()) {
$db_props = CIBlockElement::GetProperty(IBLOCK_LEADS, $lead_tmp["ID"], array(), array("CODE" => "SUMMA"));
$summa = $db_props->GetNext();
$summ_all = $summ_all + $summa["VALUE"];
$db_props = CIBlockElement::GetProperty(IBLOCK_LEADS, $lead_tmp["ID"], array(), array("CODE" => "STATUS"));
$status = $db_props->GetNext();
$managers_tmp = array();
$db_props = CIBlockElement::GetProperty(IBLOCK_LEADS, $lead_tmp["ID"], array(), array("CODE" => "GROUP_11"));
while ($manager = $db_props->GetNext()) {
if (strlen($manager["VALUE"]) > 0) {
$managers_tmp[$manager["VALUE"]] = $manager["VALUE"];
}
}
foreach ($managers_tmp as $manager) {
if (strlen($manager) > 0) {
if (count($managers_tmp) > 1) {
$managers[$manager] = $managers[$manager] + round(1 / count($managers_tmp), 2);
} else {
$managers[$manager] = $managers[$manager] + 1;
}
}
}
$status_leads[$statuses["ID"][$status["VALUE"]]["NAME"]] = $status_leads[$statuses["ID"][$status["VALUE"]]["NAME"]] + 1;
}
arsort($managers);
$leads_status = "<div class="card-body">
\x9 <p class="card-title text-md-center text-xl-left">Статусы</p>\xa \x9<div class="d-flex flex-wrap justify-content-between justify-content-md-center justify-content-xl-between align-items-center">\xa\x9\x9 \x9<h4 class="mbj mb-md-2 mb-xl-0 order-md-1 order-xl-0"><span style="color:" . $statuses["NAME"][$status_default]["PROPERTY_COLOR_VALUE"] . ";">" . $status_default . ":</span> " . ($status_leads[$status_default] > 0 ? $status_leads[$status_default] : 0) . "</h4>
\x9 \x9<i class="ti-briefcase icon-md text-muted mb-0 mb-mdk mb-xl-0"></i>\xa \x9 </div>";
foreach ($status_leads as $key => $value) {
if ($key != $status_default) {
$leads_status .= "<p class="mb-0"><span class="text-default" style="color:" . $statuses["NAME"][$key]["PROPERTY_COLOR_VALUE"] . ";">" . $key . ":</span> <span class="text-black ml-1">" . $value . "</span></p>";
}
}
if (define_access("leads.statistics")) {
if (strpos($_SERVER["HTTP_REFERER"], "/leads/") !== false) {
$leads_status .= "<button class="btn btn-light btn-icon-text btn-rounded btn-sm mlh mt-3" onclick="taskLeads('STAT');"><i class="ti-stats-up text-danger pr-2"></i> Статистика по выборке</button>";
}
}
$leads_status .= "\xa\x9\x9\x9</div>";
$leads_finance = "<div class="card-body">\xa\x9\x9\x9 <p class="card-title text-md-center text-xl-left">Финансы</p>
\x9 \x9 <div class="d-flex flex-wrap justify-content-between justify-content-md-center justify-content-xl-between align-items-center">
\x9\x9 <h4 class="mb-0 mb-md-2 mb-xl-0 order-md-1 order-xl-0">Общая: " . number_format($summ_all, 0, , " ") . "</h4>
\x9 \x9\x9 <i class="ti-wallet icon-md text-muted mb-0 mb-md-3 mb-xlh"></i>\xa \x9 \x9</div>\xa \x9\x9</div>";
$top_manager = array_key_first($managers);
$leads_managers = "<div class="card-body">
\x9 \x9<p class="card-title text-md-center text-xl-left">ТОП менеджеров</p>
\x9\x9 <div class="d-flex flex-wrap justify-content-between justify-content-md-center justify-content-xl-between align-items-center">\xa <h4 class="mb-0 mb-md-2 mb-xl-0 order-md-1 order-xlh">" . $users[$top_manager]["LAST_NAME"] . " " . $users[$top_manager]["NAME"] . ": " . $managers[$top_manager] . "</h4>\xa \x9 \x9<i class="ti-user icon-md text-muted mb-0 mb-md-3 mb-xl-0"></i>\xa \x9 \x9</div>";
$count_manager = 0;
foreach ($managers as $key => $value) {
if ($count_manager > 0 and $count_manager <= 3) {
$leads_managers .= "<p class="mb-0"><span class="text-black">" . $users[$key]["LAST_NAME"] . " " . $users[$key]["NAME"] . ": <span class="text-black ml-1">" . $value . "</span></span></p>";
}
$count_manager++;
}
$leads_managers .= "</div>";
successResponse(array("leads_status" => $leads_status, "leads_finance" => $leads_finance, "leads_managers" => $leads_managers));
}
}
© 2023 Quttera Ltd. All rights reserved.