Задача: Прослеживать количество исходящих звонков в течение рабочего дня (с 10 до 22) по лидам в статусе "В работе", со значением "Входящий" в поле "Поток" и ответственными из департаментов 1242, 1292, 181, 183, 1609, 1610, 1611, 1612. Если в лиде менее 3-х звонков в день, то на 22:00 текущего дня нужно создать задачу на старшего менеджера: "Внимание! В лиде менее 3х звонков." |
function MinThreeCallsForLead(){ if(CModule::IncludeModule('crm') && CModule::IncludeModule('intranet') && CModule::IncludeModule('voximplant') && CModule::IncludeModule('tasks')){ $arParks = [ 282 => [1609, 1242],//парк 1 284 => [1611, 181],//парк 2 286 => [1612, 183],//парк 3 288 => [1610, 1292],//парк 4 ]; $arDepartments = []; foreach($arParks as $arDeps){ $arDepartments = array_merge($arDepartments,$arDeps); } //$arDepartments = [1242, 1292, 181, 183, 1609, 1610, 1611, 1612]; $arUsers = [ "BY_DEPARTMENT" => [], "MANAGERS" => [], "ALL" => [] ]; foreach($arDepartments as $departmentId){ $rsUsers = CIntranetUtils::GetDepartmentEmployees($departmentId, true); while($arUser = $rsUsers->fetch()){ $arUsers["BY_DEPARTMENT"][$arUser["ID"]] = $departmentId; $arUsers["ALL"][$arUser["ID"]] = $arUser["ID"]; } $arUsers["MANAGERS"][$departmentId] = false; $arManagers = CIntranetUtils::GetDepartmentManager([$departmentId]); foreach($arManagers as $arManager){ $arUsers["MANAGERS"][$departmentId] = $arManager["ID"]; break; } } $date = new DateTime(); if($date->format("H")*1 < 22){ //берем предыдущий день, если агент вдруг запустился позже нужного $date->modify("-1 day"); } $arFilter = array( "STATUS_ID" => ["IN_PROCESS"], "ASSIGNED_BY_ID"=> $arUsers["ALL"], "UF_CRM_1543416257" => "696", //входящий поток //$propPark => array_keys($arParks), "CHECK_PERMISSIONS"=>"N", //не проверять права доступа текущего пользователя "<DATE_CREATE" => $date->format("d.m.Y")." 10:00:00" ); $arSelect = array( "ID", "ASSIGNED_BY_ID", ); $res = CCrmLead::GetList(Array(), $arFilter, $arSelect); $arLeadIds = []; $arContactIds = []; $arLeads = []; while($arLead = $res->Fetch()){ $arLeadIds[] = $arLead["ID"]; $arLeads[$arLead["ID"]] = [ "MANAGER"=>$arLead["ASSIGNED_BY_ID"], "CONTACTS"=> [], "CALLS" =>0 ]; $contactIds = \Bitrix\Crm\Binding\LeadContactTable::getLeadBindings($arLead["ID"]); if(!empty($contactIds)){ foreach($contactIds as $arCont){ $arContactsIds[] = $arCont["CONTACT_ID"]; $arLeads[$arLead["ID"]]["CONTACTS"][] = $arCont["CONTACT_ID"]; } } } if(!empty($arLeadIds)){ if(empty($arContactsIds)){ $filterTemp =[ "CRM_ENTITY_TYPE"=>"LEAD", "CRM_ENTITY_ID" => $arLeadIds, ]; } else{ $filterTemp = [ "LOGIC"=>"OR", [ "CRM_ENTITY_TYPE"=>"LEAD", "CRM_ENTITY_ID" => $arLeadIds, ], [ "CRM_ENTITY_TYPE"=>"CONTACT", "CRM_ENTITY_ID" => $arContactsIds, ], ]; } $arCalls = Bitrix\Voximplant\StatisticTable::getList([ "select" => ["ID","CRM_ENTITY_ID","CRM_ENTITY_TYPE"], "filter" => [ ">CALL_START_DATE"=>$date->format("d.m.Y")." 00:00:00", "INCOMING"=>1,//исходящий $filterTemp ], "order" => ['ID' => 'DESC'], "limit" => 1000000 ])->fetchAll(); foreach($arCalls as $arCall){ if($arCall["CRM_ENTITY_TYPE"]=="LEAD"){ if(!empty($arLeads[$arCall["CRM_ENTITY_ID"]])){ $arLeads[$arCall["CRM_ENTITY_ID"]]["CALLS"]++; } } else{//контакт foreach($arLeads as &$arLead1){ if(!empty($arLead1["CONTACTS"]) && in_array($arCall["CRM_ENTITY_ID"], $arLead1["CONTACTS"])){ $arLead1["CALLS"]++; } } } } foreach($arLeads as $leadId =>$arLead){ if($arLead["CALLS"]<3){ if(!empty($arUsers["BY_DEPARTMENT"][$arLead["MANAGER"]]) && !empty($arUsers["MANAGERS"][$arUsers["BY_DEPARTMENT"][$arLead["MANAGER"]]])){ $bigManager = $arUsers["MANAGERS"][$arUsers["BY_DEPARTMENT"][$arLead["MANAGER"]]]; } else{ $bigManager = $arLead["MANAGER"]; } $newTask = array( "TITLE" => "В лиде ".$leadId." менее 3х звонков", "DESCRIPTION" => "В лиде ".$leadId." было ".$arLead["CALLS"]." звонков", "DEADLINE" => date("d.m.Y")." 22:00:00", "CREATED_BY" => $bigManager ,//кем создано "RESPONSIBLE_ID" => $bigManager ,//ответственный "UF_CRM_TASK" =>["L_".$leadId]//ID сделки ); //echo "лид№".$leadId." звонков:".$arLead["CALLS"]." начальник:".$bigManager."<br>"; $taskItem = \CTaskItem::add($newTask, 1); } } } } return "MinThreeCallsForLead();"; } |
MinThreeCallsForLead();