CAllCrmContact::CreateRequisite

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CAllCrmContact
  4. CreateRequisite
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_contact.php
  • Класс: \CAllCrmContact
  • Вызов: CAllCrmContact::CreateRequisite
static function CreateRequisite($ID, $presetID)
{
	if(!is_integer($ID))
	{
		$ID = (int)$ID;
	}

	if($ID <= 0)
	{
		throw new Main\ArgumentException('Must be greater than zero', 'ID');
	}

	if(!is_integer($presetID))
	{
		$presetID = (int)$presetID;
	}

	if($presetID <= 0)
	{
		throw new Main\ArgumentException('Must be greater than zero', 'presetID');
	}

	$externalID = "CONTACT_{$ID}";

	if(Crm\EntityRequisite::getByExternalId($externalID, array('ID')) !== null)
	{
		//Already exists
		return false;
	}

	$dbResult = self::GetListEx(
		array(),
		array('=ID' => $ID, 'CHECK_PERMISSIONS' => 'N')
	);

	$entityFields = $dbResult->Fetch();
	if(!is_array($entityFields))
	{
		throw new Main\ObjectNotFoundException("The contact with ID '{$ID}' is not found");
	}

	$presetEntity = new Crm\EntityPreset();
	$presetFields = $presetEntity->getById($presetID);
	if(!is_array($presetFields))
	{
		throw new Main\ObjectNotFoundException("The preset with ID '{$presetID}' is not found");
	}

	$fieldInfos = $presetEntity->settingsGetFields(
		is_array($presetFields['SETTINGS']) ? $presetFields['SETTINGS'] : array()
	);

	$fullName = self::GetFullName($entityFields);

	$requisiteFields = array();
	foreach($fieldInfos as $fieldInfo)
	{
		$fieldName = isset($fieldInfo['FIELD_NAME']) ? $fieldInfo['FIELD_NAME'] : '';
		if($fieldName === Crm\EntityRequisite::PERSON_FIRST_NAME)
		{
			if(isset($entityFields['NAME']) && $entityFields['NAME'] !== '')
			{
				$requisiteFields[Crm\EntityRequisite::PERSON_FIRST_NAME] = $entityFields['NAME'];
			}
		}
		elseif($fieldName === Crm\EntityRequisite::PERSON_SECOND_NAME)
		{
			if(isset($entityFields['SECOND_NAME']) && $entityFields['SECOND_NAME'] !== '')
			{
				$requisiteFields[Crm\EntityRequisite::PERSON_SECOND_NAME] = $entityFields['SECOND_NAME'];
			}
		}
		elseif($fieldName === Crm\EntityRequisite::PERSON_LAST_NAME)
		{
			if(isset($entityFields['LAST_NAME']) && $entityFields['LAST_NAME'] !== '')
			{
				$requisiteFields[Crm\EntityRequisite::PERSON_LAST_NAME] = $entityFields['LAST_NAME'];
			}
		}
		elseif($fieldName === Crm\EntityRequisite::PERSON_FULL_NAME)
		{
			if($fullName !== '')
			{
				$requisiteFields[Crm\EntityRequisite::PERSON_FULL_NAME] = $fullName;
			}
		}
		elseif($fieldName === Crm\EntityRequisite::ADDRESS)
		{
			$requisiteFields[Crm\EntityRequisite::ADDRESS] = array(
				EntityAddressType::Primary =>
					ContactAddress::mapEntityFields(
						$entityFields,
						array('TYPE_ID' => EntityAddressType::Primary, 'SKIP_EMPTY' => true)
					)
			);
		}
	}

	if(empty($requisiteFields))
	{
		return false;
	}

	$requisiteFields['NAME'] = $fullName !== '' ? $fullName : $externalID;
	$requisiteFields['PRESET_ID'] = $presetID;
	$requisiteFields['ACTIVE'] = 'Y';
	$requisiteFields['ENTITY_TYPE_ID'] = CCrmOwnerType::Contact;
	$requisiteFields['ENTITY_ID'] = $ID;
	$requisiteFields['XML_ID'] = $externalID;

	$requisiteEntity = new Crm\EntityRequisite();
	return $requisiteEntity->add($requisiteFields)->isSuccess();
}

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