CAllCrmCompany::Update

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CAllCrmCompany
  4. Update
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_company.php
  • Класс: \CAllCrmCompany
  • Вызов: CAllCrmCompany::Update
public function Update($ID, array &$arFields, $bCompare = true, $bUpdateSearch = true, $arOptions = array())
{
	global $DB;

	$this->LAST_ERROR = '';
	$this->checkExceptions = [];

	$ID = (int) $ID;
	if(!is_array($arOptions))
	{
		$arOptions = [];
	}

	$arOptions['IS_COMPARE_ENABLED'] = $bCompare;

	$isSystemAction = isset($arOptions['IS_SYSTEM_ACTION']) && $arOptions['IS_SYSTEM_ACTION'];

	$arFilterTmp = Array('ID' => $ID);
	if (!$this->bCheckPermission)
	{
		$arFilterTmp['CHECK_PERMISSIONS'] = 'N';
	}

	$obRes = self::GetListEx([], $arFilterTmp);
	if (!($arRow = $obRes->Fetch()))
	{
		return false;
	}

	if(isset($arFields['IS_MY_COMPANY']))
	{
		$arFields['IS_MY_COMPANY'] = $arFields['IS_MY_COMPANY'] === 'Y' ? 'Y' : 'N';
	}

	if (isset($arFields['IS_MY_COMPANY']) && $arFields['IS_MY_COMPANY'] === 'Y')
	{
		$isMyCompany = true;
	}
	elseif ($arRow['IS_MY_COMPANY'] === 'Y')
	{
		$isMyCompany = true;
	}
	else
	{
		$isMyCompany = false;
	}

	if ($this->isUseOperation() && !$isMyCompany)
	{
		return $this->getCompatibilityAdapter()->performUpdate($ID, $arFields, $arOptions);
	}

	if(isset($arOptions['CURRENT_USER']))
	{
		$iUserId = intval($arOptions['CURRENT_USER']);
	}
	else
	{
		$iUserId = CCrmSecurityHelper::GetCurrentUserID();
	}

	unset(
		$arFields['DATE_CREATE'],
		$arFields['DATE_MODIFY'],
		$arFields['CATEGORY_ID']
	);

	if(isset($arFields['TITLE']) && trim($arFields['TITLE']) === '')
	{
		unset($arFields['TITLE']);
	}

	if(!$isSystemAction)
	{
		$arFields['~DATE_MODIFY'] = $DB->CurrentTimeFunction();
		if(!isset($arFields['MODIFY_BY_ID']) || $arFields['MODIFY_BY_ID'] <= 0)
		{
			$arFields['MODIFY_BY_ID'] = $iUserId;
		}
	}

	if (isset($arFields['ASSIGNED_BY_ID']) && $arFields['ASSIGNED_BY_ID'] <= 0)
	{
		unset($arFields['ASSIGNED_BY_ID']);
	}

	if (isset($arFields['REVENUE']))
	{
		$arFields['REVENUE'] = floatval($arFields['REVENUE']);
	}

	$assignedByID = (int)(isset($arFields['ASSIGNED_BY_ID']) ? $arFields['ASSIGNED_BY_ID'] : $arRow['ASSIGNED_BY_ID']);
	$categoryId = (int)($arRow['CATEGORY_ID'] ?? 0);

	$bResult = false;

	$arOptions['CURRENT_FIELDS'] = $arRow;
	$arOptions['FIELD_CHECK_OPTIONS']['CATEGORY_ID'] = $categoryId;
	if (!$this->CheckFields($arFields, $ID, $arOptions))
	{
		$arFields['RESULT_MESSAGE'] = &$this->LAST_ERROR;
	}
	else
	{
		$permissionEntityType = (new PermissionEntityTypeHelper(CCrmOwnerType::Company))
			->getPermissionEntityTypeForCategory($categoryId)
		;

		if($this->bCheckPermission && !CCrmAuthorizationHelper::CheckUpdatePermission($permissionEntityType, $ID, $this->cPerms))
		{
			$this->LAST_ERROR = GetMessage('CRM_PERMISSION_DENIED');
			$arFields['RESULT_MESSAGE'] = &$this->LAST_ERROR;
			return false;
		}

		if(!isset($arFields['ID']))
		{
			$arFields['ID'] = $ID;
		}

		$beforeEvents = GetModuleEvents('crm', 'OnBeforeCrmCompanyUpdate');
		while ($arEvent = $beforeEvents->Fetch())
		{
			if(ExecuteModuleEventEx($arEvent, array(&$arFields)) === false)
			{
				if(isset($arFields['RESULT_MESSAGE']))
				{
					$this->LAST_ERROR = $arFields['RESULT_MESSAGE'];
				}
				else
				{
					$this->LAST_ERROR = GetMessage('CRM_COMPANY_UPDATE_CANCELED', array('#NAME#' => $arEvent['TO_NAME']));
					$arFields['RESULT_MESSAGE'] = &$this->LAST_ERROR;
				}
				return false;
			}
		}

		$arAttr = [];
		$arAttr['OPENED'] = !empty($arFields['OPENED']) ? $arFields['OPENED'] : $arRow['OPENED'];
		$arAttr['IS_MY_COMPANY'] = !empty($arFields['IS_MY_COMPANY']) ? $arFields['IS_MY_COMPANY'] : $arRow['IS_MY_COMPANY'];
		$arEntityAttr = self::BuildEntityAttr($assignedByID, $arAttr);
		if($this->bCheckPermission)
		{
			$sEntityPerm = $this->cPerms->GetPermType($permissionEntityType, 'WRITE', $arEntityAttr);
			//HACK: Ensure that entity accessible for user restricted by BX_CRM_PERM_OPEN
			$this->PrepareEntityAttrs($arEntityAttr, $sEntityPerm);
			//HACK: Prevent 'OPENED' field change by user restricted by BX_CRM_PERM_OPEN permission
			if($sEntityPerm === BX_CRM_PERM_OPEN && isset($arFields['OPENED']) && $arFields['OPENED'] !== 'Y' && $assignedByID !== $iUserId)
			{
				$arFields['OPENED'] = 'Y';
			}
		}

		if(isset($arFields['LOGO']))
		{
			if(is_numeric($arFields['LOGO']) && $arFields['LOGO'] > 0)
			{
				//New file editor (file is already saved)
				if(isset($arFields['LOGO_del']) && $arFields['LOGO_del'] > 0)
				{
					CFile::Delete($arFields['LOGO_del']);
					if($arFields['LOGO'] == $arFields['LOGO_del'])
					{
						$arFields['LOGO'] = '';
					}
				}
			}
			elseif(is_array($arFields['LOGO']) && CFile::CheckImageFile($arFields['LOGO']) == '')
			{
				//todo some strange file replacement logic
				//Old file editor (file id is not saved yet)
				$arFields['LOGO']['MODULE_ID'] = 'crm';
				if($arFields['LOGO_del'] == 'Y' && !empty($arRow['LOGO']))
					CFile::Delete($arRow['LOGO']);
				CFile::SaveForDB($arFields, 'LOGO', 'crm');
				if($arFields['LOGO_del'] == 'Y' && !isset($arFields['LOGO']))
					$arFields['LOGO'] = '';
			}
		}

		self::getLastActivityAdapter()->performUpdate((int)$ID, $arFields, $arOptions);
		self::getCommentsAdapter()
			->setPreviousFields((int)$ID, $arRow)
			->normalizeFields((int)$ID, $arFields)
		;

		$sonetEventData = [];
		if ($bCompare)
		{
			$res = CCrmFieldMulti::GetList(
				array('ID' => 'asc'),
				array('ENTITY_ID' => 'COMPANY', 'ELEMENT_ID' => $ID)
			);
			$arRow['FM'] = [];
			while($ar = $res->Fetch())
				$arRow['FM'][$ar['TYPE_ID']][$ar['ID']] = array('VALUE' => $ar['VALUE'], 'VALUE_TYPE' => $ar['VALUE_TYPE']);

			$arEvents = self::CompareFields($arRow, $arFields, $arOptions);
			foreach($arEvents as $arEvent)
			{
				$arEvent['ENTITY_TYPE'] = 'COMPANY';
				$arEvent['ENTITY_ID'] = $ID;
				$arEvent['EVENT_TYPE'] = 1;

				if(!isset($arEvent['USER_ID']))
				{
					if($iUserId > 0)
					{
						$arEvent['USER_ID'] = $iUserId;
					}
					else if(isset($arFields['MODIFY_BY_ID']) && $arFields['MODIFY_BY_ID'] > 0)
					{
						$arEvent['USER_ID'] = $arFields['MODIFY_BY_ID'];
					}
					else if(isset($arOptions['CURRENT_USER']))
					{
						$arEvent['USER_ID'] = (int)$arOptions['CURRENT_USER'];
					}
				}

				$CCrmEvent = new CCrmEvent();
				$eventID = $CCrmEvent->Add($arEvent, $this->bCheckPermission);
				if(is_int($eventID) && $eventID > 0)
				{
					$fieldID = isset($arEvent['ENTITY_FIELD']) ? $arEvent['ENTITY_FIELD'] : '';
					if($fieldID === '')
					{
						continue;
					}

					switch($fieldID)
					{
						case 'ASSIGNED_BY_ID':
						{
							$sonetEventData[] = array(
								'TYPE' => CCrmLiveFeedEvent::Responsible,
								'FIELDS' => array(
									//'EVENT_ID' => $eventID,
									'TITLE' => GetMessage('CRM_COMPANY_EVENT_UPDATE_ASSIGNED_BY'),
									'MESSAGE' => '',
									'PARAMS' => array(
										'START_RESPONSIBLE_ID' => $arRow['ASSIGNED_BY_ID'],
										'FINAL_RESPONSIBLE_ID' => $arFields['ASSIGNED_BY_ID']
									)
								)
							);
						}
						break;
						case 'TITLE':
						{
							$sonetEventData[] = array(
								'TYPE' => CCrmLiveFeedEvent::Denomination,
								'FIELDS' => array(
									//'EVENT_ID' => $eventID,
									'TITLE' => GetMessage('CRM_COMPANY_EVENT_UPDATE_TITLE'),
									'MESSAGE' => '',
									'PARAMS' => array(
										'START_TITLE' => $arRow['TITLE'],
										'FINAL_TITLE' => $arFields['TITLE']
									)
								)
							);
						}
						break;
					}
				}
			}
		}

		if(isset($arFields['TITLE']) && $arFields['TITLE'] !== $arRow['TITLE'])
		{
			CCrmActivity::ResetEntityCommunicationSettings(CCrmOwnerType::Company, $ID);
		}

		if(isset($arFields['HAS_EMAIL']))
		{
			unset($arFields['HAS_EMAIL']);
		}

		if(isset($arFields['HAS_PHONE']))
		{
			unset($arFields['HAS_PHONE']);
		}

		if(isset($arFields['HAS_IMOL']))
		{
			unset($arFields['HAS_IMOL']);
		}

		unset($arFields["ID"]);

		$this->normalizeEntityFields($arFields);
		$sUpdate = $DB->PrepareUpdate(self::TABLE_NAME, $arFields);

		if ($sUpdate <> '')
		{
			$DB->Query("UPDATE b_crm_company SET {$sUpdate} WHERE ID = {$ID}", false, 'FILE: '.__FILE__.'
LINE: '.__LINE__); $bResult = true; } if(defined("BX_COMP_MANAGED_CACHE")) { static $arNameFields = array("TITLE"); $bClear = false; foreach($arNameFields as $val) { if(isset($arFields[$val])) { $bClear = true; break; } } if ($bClear) { $GLOBALS["CACHE_MANAGER"]->ClearByTag("crm_entity_name_".CCrmOwnerType::Company."_".$ID); } } CCrmEntityHelper::NormalizeUserFields($arFields, self::$sUFEntityID, $GLOBALS['USER_FIELD_MANAGER'], array('IS_NEW' => false)); $GLOBALS['USER_FIELD_MANAGER']->Update(self::$sUFEntityID, $ID, $arFields); //region Permissions $securityRegisterOptions = (new \Bitrix\Crm\Security\Controller\RegisterOptions()) ->setEntityAttributes($arEntityAttr) ; Crm\Security\Manager::getEntityController(CCrmOwnerType::Company) ->register($permissionEntityType, $ID, $securityRegisterOptions) ; //endregion if(isset($arFields['ADDRESS']) || isset($arFields['ADDRESS_2']) || isset($arFields['ADDRESS_CITY']) || isset($arFields['ADDRESS_POSTAL_CODE']) || isset($arFields['ADDRESS_REGION']) || isset($arFields['ADDRESS_PROVINCE']) || isset($arFields['ADDRESS_COUNTRY']) || isset($arFields['ADDRESS_LOC_ADDR_ID'])) { EntityAddress::register( CCrmOwnerType::Company, $ID, EntityAddressType::Primary, array( 'ADDRESS_1' => isset($arFields['ADDRESS']) ? $arFields['ADDRESS'] : (isset($arRow['ADDRESS']) ? $arRow['ADDRESS'] : null), 'ADDRESS_2' => isset($arFields['ADDRESS_2']) ? $arFields['ADDRESS_2'] : (isset($arRow['ADDRESS_2']) ? $arRow['ADDRESS_2'] : null), 'CITY' => isset($arFields['ADDRESS_CITY']) ? $arFields['ADDRESS_CITY'] : (isset($arRow['ADDRESS_CITY']) ? $arRow['ADDRESS_CITY'] : null), 'POSTAL_CODE' => isset($arFields['ADDRESS_POSTAL_CODE']) ? $arFields['ADDRESS_POSTAL_CODE'] : (isset($arRow['ADDRESS_POSTAL_CODE']) ? $arRow['ADDRESS_POSTAL_CODE'] : null), 'REGION' => isset($arFields['ADDRESS_REGION']) ? $arFields['ADDRESS_REGION'] : (isset($arRow['ADDRESS_REGION']) ? $arRow['ADDRESS_REGION'] : null), 'PROVINCE' => isset($arFields['ADDRESS_PROVINCE']) ? $arFields['ADDRESS_PROVINCE'] : (isset($arRow['ADDRESS_PROVINCE']) ? $arRow['ADDRESS_PROVINCE'] : null), 'COUNTRY' => isset($arFields['ADDRESS_COUNTRY']) ? $arFields['ADDRESS_COUNTRY'] : (isset($arRow['ADDRESS_COUNTRY']) ? $arRow['ADDRESS_COUNTRY'] : null), 'COUNTRY_CODE' => isset($arFields['ADDRESS_COUNTRY_CODE']) ? $arFields['ADDRESS_COUNTRY_CODE'] : (isset($arRow['ADDRESS_COUNTRY_CODE']) ? $arRow['ADDRESS_COUNTRY_CODE'] : null), 'LOC_ADDR_ID' => isset($arFields['ADDRESS_LOC_ADDR_ID']) ? (int)$arFields['ADDRESS_LOC_ADDR_ID'] : (isset($arRow['ADDRESS_LOC_ADDR_ID']) ? (int)$arRow['ADDRESS_LOC_ADDR_ID'] : 0), 'LOC_ADDR' => isset($arFields['ADDRESS_LOC_ADDR']) ? $arFields['ADDRESS_LOC_ADDR'] : null ), ); } if(isset($arFields['REG_ADDRESS']) || isset($arFields['REG_ADDRESS_2']) || isset($arFields['REG_ADDRESS_CITY']) || isset($arFields['REG_ADDRESS_POSTAL_CODE']) || isset($arFields['REG_ADDRESS_REGION']) || isset($arFields['REG_ADDRESS_PROVINCE']) || isset($arFields['REG_ADDRESS_COUNTRY']) || isset($arFields['REG_ADDRESS_LOC_ADDR_ID'])) { EntityAddress::register( CCrmOwnerType::Company, $ID, EntityAddressType::Registered, array( 'ADDRESS_1' => isset($arFields['REG_ADDRESS']) ? $arFields['REG_ADDRESS'] : (isset($arRow['REG_ADDRESS']) ? $arRow['REG_ADDRESS'] : null), 'ADDRESS_2' => isset($arFields['REG_ADDRESS_2']) ? $arFields['REG_ADDRESS_2'] : (isset($arRow['REG_ADDRESS_2']) ? $arRow['REG_ADDRESS_2'] : null), 'CITY' => isset($arFields['REG_ADDRESS_CITY']) ? $arFields['REG_ADDRESS_CITY'] : (isset($arRow['REG_ADDRESS_CITY']) ? $arRow['REG_ADDRESS_CITY'] : null), 'POSTAL_CODE' => isset($arFields['REG_ADDRESS_POSTAL_CODE']) ? $arFields['REG_ADDRESS_POSTAL_CODE'] : (isset($arRow['REG_ADDRESS_POSTAL_CODE']) ? $arRow['REG_ADDRESS_POSTAL_CODE'] : null), 'REGION' => isset($arFields['REG_ADDRESS_REGION']) ? $arFields['REG_ADDRESS_REGION'] : (isset($arRow['REG_ADDRESS_REGION']) ? $arRow['REG_ADDRESS_REGION'] : null), 'PROVINCE' => isset($arFields['REG_ADDRESS_PROVINCE']) ? $arFields['REG_ADDRESS_PROVINCE'] : (isset($arRow['REG_ADDRESS_PROVINCE']) ? $arRow['REG_ADDRESS_PROVINCE'] : null), 'COUNTRY' => isset($arFields['REG_ADDRESS_COUNTRY']) ? $arFields['REG_ADDRESS_COUNTRY'] : (isset($arRow['REG_ADDRESS_COUNTRY']) ? $arRow['REG_ADDRESS_COUNTRY'] : null), 'COUNTRY_CODE' => isset($arFields['REG_ADDRESS_COUNTRY_CODE']) ? $arFields['REG_ADDRESS_COUNTRY_CODE'] : (isset($arRow['REG_ADDRESS_COUNTRY_CODE']) ? $arRow['REG_ADDRESS_COUNTRY_CODE'] : null), 'LOC_ADDR_ID' => isset($arFields['REG_ADDRESS_LOC_ADDR_ID']) ? (int)$arFields['REG_ADDRESS_LOC_ADDR_ID'] : (isset($arRow['REG_ADDRESS_LOC_ADDR_ID']) ? (int)$arRow['REG_ADDRESS_LOC_ADDR_ID'] : 0), 'LOC_ADDR' => isset($arFields['REG_ADDRESS_LOC_ADDR']) ? $arFields['REG_ADDRESS_LOC_ADDR'] : null ), ); } //Statistics & History --> $oldLeadID = isset($arRow['LEAD_ID']) ? (int)$arRow['LEAD_ID'] : 0; $curLeadID = isset($arFields['LEAD_ID']) ? (int)$arFields['LEAD_ID'] : $oldLeadID; if($oldLeadID != $curLeadID) { if($oldLeadID > 0) { Bitrix\Crm\Statistics\LeadConversionStatisticsEntry::processBindingsChange($oldLeadID); } if($curLeadID > 0) { Bitrix\Crm\Statistics\LeadConversionStatisticsEntry::processBindingsChange($curLeadID); } } $enableDupIndexInvalidation = isset($arOptions['ENABLE_DUP_INDEX_INVALIDATION']) ? (bool)$arOptions['ENABLE_DUP_INDEX_INVALIDATION'] : true; if(!$isSystemAction && $enableDupIndexInvalidation) { DuplicateManager::markDuplicateIndexAsDirty(CCrmOwnerType::Company, $ID); } Bitrix\Crm\Statistics\CompanyGrowthStatisticEntry::synchronize($ID, array( 'ASSIGNED_BY_ID' => $assignedByID )); Crm\Activity\CommunicationStatistics::synchronizeByOwner(CCrmOwnerType::Company, $ID, array( 'ASSIGNED_BY_ID' => $assignedByID )); //<-- Statistics & History if($bResult) { $previousAssignedByID = isset($arRow['ASSIGNED_BY_ID']) ? (int)$arRow['ASSIGNED_BY_ID'] : 0; if ($assignedByID !== $previousAssignedByID && $enableDupIndexInvalidation) { DuplicateManager::onChangeEntityAssignedBy(CCrmOwnerType::Company, $ID); } \Bitrix\Crm\Counter\Monitor::getInstance()->onEntityUpdate( CCrmOwnerType::Company, $arRow, [ 'ASSIGNED_BY_ID' => $arFields['ASSIGNED_BY_ID'] ?? $arRow['ASSIGNED_BY_ID'], 'CATEGORY_ID' => $arFields['CATEGORY_ID'] ?? $arRow['CATEGORY_ID'], ] ); } self::getCommentsAdapter() ->setPreviousFields((int)$ID, $arRow) ->performUpdate((int)$ID, $arFields, $arOptions) ; if (isset($arFields['FM']) && is_array($arFields['FM'])) { $CCrmFieldMulti = new CCrmFieldMulti(); $CCrmFieldMulti->SetFields(CCrmOwnerType::CompanyName, $ID, $arFields['FM']); $multifields = DuplicateCommunicationCriterion::prepareEntityMultifieldValues( CCrmOwnerType::Company, $ID ); $hasEmail = CCrmFieldMulti::HasValues($multifields, CCrmFieldMulti::EMAIL) ? 'Y' : 'N'; $hasPhone = CCrmFieldMulti::HasValues($multifields, CCrmFieldMulti::PHONE) ? 'Y' : 'N'; $hasImol = CCrmFieldMulti::HasImolValues($multifields) ? 'Y' : 'N'; if( $hasEmail !== (isset($arRow['HAS_EMAIL']) ? $arRow['HAS_EMAIL'] : 'N') || $hasPhone !== (isset($arRow['HAS_PHONE']) ? $arRow['HAS_PHONE'] : 'N') || $hasImol !== (isset($arRow['HAS_IMOL']) ? $arRow['HAS_IMOL'] : 'N') ) { $DB->Query("UPDATE b_crm_company SET HAS_EMAIL = '{$hasEmail}', HAS_PHONE = '{$hasPhone}', HAS_IMOL = '{$hasImol}' WHERE ID = {$ID}", false, 'FILE: '.__FILE__.'
LINE: '.__LINE__); $arFields['HAS_EMAIL'] = $hasEmail; $arFields['HAS_PHONE'] = $hasPhone; $arFields['HAS_IMOL'] = $hasImol; } } $duplicateCriterionRegistrar = DuplicateManager::getCriterionRegistrar(\CCrmOwnerType::Company); $data = (new Crm\Integrity\CriterionRegistrar\Data()) ->setEntityTypeId(\CCrmOwnerType::Company) ->setEntityId($ID) ->setCurrentFields($arFields) ->setPreviousFields($arRow) ; $duplicateCriterionRegistrar->update($data); // update utm fields UtmTable::updateEntityUtmFromFields(CCrmOwnerType::Company, $ID, $arFields); //region save parent relations Container::getInstance()->getParentFieldManager()->saveParentRelationsForIdentifier( new Crm\ItemIdentifier(CCrmOwnerType::Company, $ID), $arFields ); //endregion if($bUpdateSearch) { CCrmSearch::UpdateSearch(array('ID' => $ID, 'CHECK_PERMISSIONS' => 'N'), 'COMPANY', true); } $arFields['ID'] = $ID; $originalContactBindings = ContactCompanyTable::getCompanyBindings($ID); $contactBindings = null; if (isset($arFields['CONTACT_ID']) && is_array($arFields['CONTACT_ID'])) { $arFields['CONTACT_ID'] = array_filter($arFields['CONTACT_ID']); $contactBindings = Crm\Binding\EntityBinding::prepareEntityBindings(CCrmOwnerType::Contact, $arFields['CONTACT_ID']); if (empty($arFields['CONTACT_ID'])) { \Bitrix\Crm\Binding\ContactCompanyTable::unbindAllContacts($arFields['ID']); } else { $arCurrentContact = Crm\Binding\EntityBinding::prepareEntityIDs(CCrmOwnerType::Contact, $originalContactBindings); $arAdd = array_diff($arFields['CONTACT_ID'], $arCurrentContact); $arDelete = array_diff($arCurrentContact, $arFields['CONTACT_ID']); \Bitrix\Crm\Binding\ContactCompanyTable::bindContactIDs($arFields['ID'], $arAdd); \Bitrix\Crm\Binding\ContactCompanyTable::unbindContactIDs($arFields['ID'], $arDelete); if (isset($GLOBALS["USER"])) { CUserOptions::SetOption("crm", "crm_contact_search", array('last_selected' => implode(',', $arAdd))); } } } CCrmEntityHelper::registerAdditionalTimelineEvents([ 'entityTypeId' => CCrmOwnerType::Company, 'entityId' => $ID, 'fieldsInfo' => static::GetFieldsInfo(), 'previousFields' => $arRow, 'currentFields' => $arFields, 'options' => $arOptions, 'bindings' => [ 'entityTypeId' => CCrmOwnerType::Contact, 'previous' => $originalContactBindings, 'current' => $contactBindings, ] ]); //region Search content index Bitrix\Crm\Search\SearchContentBuilderFactory::create(CCrmOwnerType::Company) ->build($ID, ['checkExist' => true]); //endregion Bitrix\Crm\Timeline\CompanyController::getInstance()->onModify( $ID, array( 'CURRENT_FIELDS' => $arFields, 'PREVIOUS_FIELDS' => $arRow, 'OPTIONS' => $arOptions ) ); $registerSonetEvent = isset($arOptions['REGISTER_SONET_EVENT']) && $arOptions['REGISTER_SONET_EVENT'] === true; $isUntypedCategory = $categoryId === 0; if ( $bResult && isset($arFields['ASSIGNED_BY_ID']) && $isUntypedCategory && Crm\Settings\Crm::isLiveFeedRecordsGenerationEnabled() ) { CCrmSonetSubscription::ReplaceSubscriptionByEntity( CCrmOwnerType::Company, $ID, CCrmSonetSubscriptionType::Responsibility, $arFields['ASSIGNED_BY_ID'], $arRow['ASSIGNED_BY_ID'], $registerSonetEvent ); } if($bResult && $bCompare && $registerSonetEvent && !empty($sonetEventData)) { $modifiedByID = intval($arFields['MODIFY_BY_ID']); foreach ($sonetEventData as &$sonetEvent) { $sonetEventFields = &$sonetEvent['FIELDS']; $sonetEventFields['ENTITY_TYPE_ID'] = CCrmOwnerType::Company; $sonetEventFields['ENTITY_ID'] = $ID; $sonetEventFields['USER_ID'] = $modifiedByID; $logEventID = $isUntypedCategory ? CCrmLiveFeed::CreateLogEvent($sonetEventFields, $sonetEvent['TYPE'], ['CURRENT_USER' => $iUserId]) : false; if ( $logEventID !== false && $sonetEvent['TYPE'] == CCrmLiveFeedEvent::Responsible && $categoryId === 0 && CModule::IncludeModule("im") ) { $title = CCrmOwnerType::GetCaption(CCrmOwnerType::Company, $ID, false); $url = CCrmOwnerType::GetEntityShowPath(CCrmOwnerType::Company, $ID); $serverName = (CMain::IsHTTPS() ? "https" : "http")."://".((defined("SITE_SERVER_NAME") && SITE_SERVER_NAME <> '') ? SITE_SERVER_NAME : COption::GetOptionString("main", "server_name", "")); if ($sonetEventFields['PARAMS']['FINAL_RESPONSIBLE_ID'] != $modifiedByID) { $arMessageFields = array( "MESSAGE_TYPE" => IM_MESSAGE_SYSTEM, "TO_USER_ID" => $sonetEventFields['PARAMS']['FINAL_RESPONSIBLE_ID'], "FROM_USER_ID" => $modifiedByID, "NOTIFY_TYPE" => IM_NOTIFY_FROM, "NOTIFY_MODULE" => "crm", "LOG_ID" => $logEventID, //"NOTIFY_EVENT" => "company_update", "NOTIFY_EVENT" => "changeAssignedBy", "NOTIFY_TAG" => "CRM|COMPANY_RESPONSIBLE|".$ID, "NOTIFY_MESSAGE" => GetMessage("CRM_COMPANY_RESPONSIBLE_IM_NOTIFY", Array("#title#" => "".htmlspecialcharsbx($title)."")), "NOTIFY_MESSAGE_OUT" => GetMessage("CRM_COMPANY_RESPONSIBLE_IM_NOTIFY", Array("#title#" => htmlspecialcharsbx($title)))." (".$serverName.$url.")" ); CIMNotify::Add($arMessageFields); } if ($sonetEventFields['PARAMS']['START_RESPONSIBLE_ID'] != $modifiedByID) { $arMessageFields = array( "MESSAGE_TYPE" => IM_MESSAGE_SYSTEM, "TO_USER_ID" => $sonetEventFields['PARAMS']['START_RESPONSIBLE_ID'], "FROM_USER_ID" => $modifiedByID, "NOTIFY_TYPE" => IM_NOTIFY_FROM, "NOTIFY_MODULE" => "crm", "LOG_ID" => $logEventID, //"NOTIFY_EVENT" => "company_update", "NOTIFY_EVENT" => "changeAssignedBy", "NOTIFY_TAG" => "CRM|COMPANY_RESPONSIBLE|".$ID, "NOTIFY_MESSAGE" => GetMessage("CRM_COMPANY_NOT_RESPONSIBLE_IM_NOTIFY", Array("#title#" => "".htmlspecialcharsbx($title)."")), "NOTIFY_MESSAGE_OUT" => GetMessage("CRM_COMPANY_NOT_RESPONSIBLE_IM_NOTIFY", Array("#title#" => htmlspecialcharsbx($title)))." (".$serverName.$url.")" ); CIMNotify::Add($arMessageFields); } } unset($sonetEventFields); } unset($sonetEvent); } if($bResult) { $afterEvents = GetModuleEvents('crm', 'OnAfterCrmCompanyUpdate'); while ($arEvent = $afterEvents->Fetch()) ExecuteModuleEventEx($arEvent, array(&$arFields)); $item = $this->createPullItem(array_merge($arRow, $arFields)); Crm\Integration\PullManager::getInstance()->sendItemUpdatedEvent( $item, [ 'TYPE' => self::$TYPE_NAME, 'SKIP_CURRENT_USER' => ($iUserId !== 0), 'CATEGORY_ID' => ($arFields['CATEGORY_ID'] ?? 0), ] ); } } return $bResult; }

Добавить комментарий