установка категорий компаний bitrix24 crm companies

← разместить еще код

Расшаренный код:

$rootActivity = $this->GetRootActivity();


define('NO_KEEP_STATISTIC', true);
define('NOT_CHECK_PERMISSIONS', true);
define('CHK_EVENT', true);
define('NO_AGENT_CHECK', true);
define('PUBLIC_AJAX_MODE', true);
define('STATISTIC_SKIP_ACTIVITY_CHECK', true);

require($_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/prolog_before.php");
if(!CModule::IncludeModule('crm')) die('error');

$dealID = '{=Document:ID}';

//потенциальный
$dbRes = CCrmCompany::GetList(array('TITLE' => 'ASC'), array(), ['TITLE', 'UF_CRM_1545220215027']);
while ($arRes = $dbRes->Fetch()) {
    $arCompanies[$arRes['ID']] = $arRes;
}

$dbRes = CCrmDeal::GetList(array('TITLE' => 'ASC'), array("ID" => $dealID), ['ID', 'COMPANY_ID', 'STAGE_ID', 'DATE_CREATE']);
while ($arRes = $dbRes->Fetch()) {
    $arCompaniesInDeals[$arRes['ID']] = $arRes['COMPANY_ID'];
    $arDeals[$arRes['ID']] = $arRes;
}
// print_r($arCompaniesInDeals);
// die();

//потенциальный
/*foreach( $arCompanies as $k=>$company ){
	if( !in_array($k, $arCompaniesInDeals) ){
		$POT[] = $k;
		if( $company['UF_CRM_1545220215027'] == 263 ) continue;
		$arFields = [
		   'UF_CRM_1545220215027' => 263 //потенциальный
		];
		$CCrmCompany = new CCrmCompany( $bNeedCheckPermission );
		$bUpdateResult = $CCrmCompany->Update($k, $arFields);
	}
}*/
// print_r($POT);




//текущий или постоянный
$deal_success_status = [
	'Успешно реализован',
	'Сделка успешна',
	'WON',
	'C1:WON',
	'C2:WON',
	'C3:WON',
];
$deal_fail_status = [
	'Договор не заключен',
	'Нет Потребности',
	'Не проходит по бюджету и срокам',
	'Не актуально сейчас',
	'Клиент не выходит на связь',
	'Другое',
	'Сделка провалена',
	'Поддержка прекращена',
	'Анализ причины провала',
	'C3:APOLOGY',
	'C3:LOSE',
	'C2:LOSE',
	'C1:LOSE',
	'C1:1',
	'C1:2',
	'C1:3',
	'C1:4',
	'C1:5',
	'LOSE',
];


$res = CCrmEvent::GetList(array(), array(
    "ENTITY_TYPE"=>"DEAL",
    "ENTITY_FIELD"=>"STAGE_ID",
    "EVENT_TYPE"=>"1",
    "ENTITY_ID"=>$dealID,
), []);
while($arEvent = $res->Fetch()){
    $arChanges[$arEvent['ENTITY_ID']] = $arEvent;
}
foreach( $arChanges as $arEvent ){
    if( in_array($arEvent['EVENT_TEXT_2'], $deal_success_status) ) $arSuccess[$arEvent['ENTITY_ID']] = $arEvent['DATE_CREATE'];
    if( in_array($arEvent['EVENT_TEXT_2'], $deal_fail_status) ) $arFail[$arEvent['ENTITY_ID']] = $arEvent['DATE_CREATE'];
}

foreach( $arDeals as $id=>$deal ){
	if( in_array($deal['STAGE_ID'], $deal_success_status) && !array_key_exists($id, $arSuccess) ) $arSuccess[$id] = $deal['DATE_CREATE'];
	if( in_array($deal['STAGE_ID'], $deal_fail_status) && !array_key_exists($id, $arFail) ) $arFail[$id] = $deal['DATE_CREATE'];
}


foreach( $arSuccess as $deal=>$date ){
	if( strtotime($date) < strtotime( date('d.m.').(intval(date('Y')-1)) ) ){
		$arSuccess_before[$deal] = $date;
	} else {
		$arSuccess_after[$deal] = $date;
	}
}
// print_r($arSuccess_before);
// echo "333";
// print_r($arFail);

// print_r($arCompaniesInDeals);
// die();
foreach( $arCompaniesInDeals as $deal=>$company ){
	if( array_key_exists($deal, $arSuccess_after) ){
		$arCompanies_deals[$company]['after_success'][] = $deal;
	} elseif( array_key_exists($deal, $arSuccess_before) ) {
		$arCompanies_deals[$company]['before_success'][] = $deal;
	} else {
		if( array_key_exists($deal, $arFail) ){
			$arCompanies_deals[$company]['fail'][] = $deal;
		} else {
			$arCompanies_deals[$company]['inwork'][] = $deal;
		}
	}
}
// print_r($arCompanies_deals);

// print_r($arFail);
// die();


$CCrmCompany = new CCrmCompany( $bNeedCheckPermission );
foreach( $arCompanies_deals as $company=>$data ){
	if( !$company ) continue;
	if( count($data) == 1 && count($data['fail']) > 0 ){
		$FAIL[] = $company;

		$arFields = [
		   'UF_CRM_1545220215027' => 268 //отказ
		];
		$bUpdateResult = $CCrmCompany->Update($company, $arFields);

		continue;
	}
	if( count($data['after_success']) >= 2 ){
		$POSTOYANNII[] = $company;

		$arFields = [
		   'UF_CRM_1545220215027' => 266 //потенциальный
		];
		$bUpdateResult = $CCrmCompany->Update($company, $arFields);

		continue;
	}
	if( count($data['before_success']) > 0 && count($data['after_success']) == 0 && count($data['inwork']) == 0 ){
		$BIVSHII[] = $company;

		$arFields = [
		   'UF_CRM_1545220215027' => 267 //потенциальный
		];
		$bUpdateResult = $CCrmCompany->Update($company, $arFields);

		continue;
	}
	if( count($data['after_success']) == 0 && count($data['inwork']) > 0 ){
		$PRODAZHA[] = $company;

		$arFields = [
		   'UF_CRM_1545220215027' => 264 //потенциальный
		];
		$bUpdateResult = $CCrmCompany->Update($company, $arFields);

		continue;
	}
	if( count($data['after_success']) == 1 ){
		$ACCOUNTING[] = $company;

		$arFields = [
		   'UF_CRM_1545220215027' => 265 //потенциальный
		];
		$bUpdateResult = $CCrmCompany->Update($company, $arFields);

		continue;
	}
}