• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/data/datagenerator.php
  • Класс: Bitrix\Crm\Data\DataGenerator
  • Вызов: DataGenerator::createActivities
public function createActivities(array $params)
{
	$count = isset($params['COUNT']) ? (int)$params['COUNT'] : 0;
	if($count <= 0)
	{
		return;
	}

	$typeID = isset($params['TYPE_ID']) ? (int)$params['TYPE_ID'] : \CCrmActivityType::Undefined;
	if(!\CCrmActivityType::IsDefined($typeID))
	{
		return;
	}

	$ownerTypeID = isset($params['OWNER_TYPE_ID']) ? (int)$params['OWNER_TYPE_ID'] : \CCrmOwnerType::Undefined;
	if(!\CCrmOwnerType::IsDefined($ownerTypeID))
	{
		return;
	}

	$ownerID = isset($params['OWNER_ID']) ? (int)$params['OWNER_ID'] : 0;
	if($ownerID <= 0)
	{
		return;
	}

	$clientTypeID = isset($params['CLIENT_TYPE_ID']) ? (int)$params['CLIENT_TYPE_ID'] : \CCrmOwnerType::Undefined;
	$clientID = isset($params['CLIENT_ID']) ? (int)$params['CLIENT_ID'] : 0;
	if(!\CCrmOwnerType::IsDefined($clientTypeID) || $clientID <= 0)
	{
		$clientTypeID = $ownerTypeID;
		$clientID = $ownerID;
	}

	$commType = $typeID === \CCrmActivityType::Call ? 'PHONE' : '';
	$values = array();
	if($commType === 'PHONE' || $commType === 'EMAIL')
	{
		$values = self::getEntityMultifieldValues($clientTypeID, $clientID, $commType);
		if(empty($values))
		{
			return;
		}
	}

	$userIDs = isset($params['USER_IDS']) && is_array($params['USER_IDS']) ? $params['USER_IDS'] : array();
	if(empty($userIDs))
	{
		$userIDs[] = \CCrmSecurityHelper::GetCurrentUserID();
	}

	$date = isset($params['DATE']) ? $params['DATE'] : null;
	if(!$date)
	{
		$date = $date = new Date();
	}
	$maxDateOffset = isset($params['MAX_DATE_OFFSET']) ? (int)$params['MAX_DATE_OFFSET'] : 0;
	$dateTimeFormat = Date::convertFormatToPhp(FORMAT_DATETIME);

	$subjectPrefix = isset($params['SUBJECT_PREFIX']) ? $params['SUBJECT_PREFIX'] : '';

	for($i = 0; $i < $count; $i++)
	{
		$time = DateTime::createFromTimestamp($date->getTimestamp());
		if($maxDateOffset > 0)
		{
			$time->add(mt_rand(0, $maxDateOffset) . ' days');
		}
		$time->setTime(mt_rand(8, 20), mt_rand(0, 59), 0);
		$siteTime = $time->format($dateTimeFormat);

		$fields = array(
			'TYPE_ID' =>  $typeID,
			'START_TIME' => $siteTime,
			'END_TIME' => $siteTime,
			'SUBJECT' => "{$subjectPrefix} ({$siteTime})",
			'COMPLETED' => (mt_rand(1, 10) % 2) !== 0 ? 'Y' : 'N',
			'PRIORITY' => \CCrmActivityPriority::Medium,
			'DESCRIPTION' => '',
			'DESCRIPTION_TYPE' => \CCrmContentType::PlainText,
			'LOCATION' => '',
			'DIRECTION' =>  \CCrmActivityDirection::Outgoing,
			'NOTIFY_TYPE' => \CCrmActivityNotifyType::None,
			'RESPONSIBLE_ID' => self::getRandomItem($userIDs),
			'OWNER_ID' => $ownerID,
			'OWNER_TYPE_ID' => $ownerTypeID,
			'BINDINGS' => array(
				array('OWNER_TYPE_ID' => $ownerTypeID, 'OWNER_ID' => $ownerID)
			)
		);

		$ID = \CCrmActivity::Add($fields, false, true, array('REGISTER_SONET_EVENT' => true));
		$comms = array(
			array(
				'TYPE' => $commType,
				'VALUE' => self::getRandomItem($values, ''),
				'ENTITY_ID' => $clientID,
				'ENTITY_TYPE_ID' => $clientTypeID
			)
		);
		\CCrmActivity::SaveCommunications($ID, $comms, $fields, false, false);
	}
}