• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/statistics/contactactivitymarkstatisticentry.php
  • Класс: Bitrix\Crm\Statistics\ContactActivityMarkStatisticEntry
  • Вызов: ContactActivityMarkStatisticEntry::register
static function register($ownerID, array $entityFields = null, array $options = null)
{
	if(!is_int($ownerID))
		$ownerID = (int)$ownerID;

	if($ownerID <= 0)
		throw new Main\ArgumentException('Owner ID must be greater than zero.', 'ownerID');

	if(!is_array($options))
		$options = array();

	$date = isset($options['DATE']) ? $options['DATE'] : null;
	if(!$date)
		throw new Main\ArgumentException('Options DATE must be specified.', 'DATE');

	$providerId = isset($options['PROVIDER_ID']) ? $options['PROVIDER_ID'] : null;
	if(!$providerId)
		throw new Main\ArgumentException('Options PROVIDER_ID must be specified.', 'PROVIDER_ID');
	
	$providerTypeId = isset($options['PROVIDER_TYPE_ID']) ? $options['PROVIDER_TYPE_ID'] : null;
	if(!$providerTypeId)
		throw new Main\ArgumentException('Options PROVIDER_TYPE_ID must be specified.', 'PROVIDER_TYPE_ID');

	$sourceId = isset($options['SOURCE_ID']) ? $options['SOURCE_ID'] : null;
	if(!$sourceId)
		throw new Main\ArgumentException('Options SOURCE_ID must be specified.', 'SOURCE_ID');

	$value = isset($options['VALUE']) && is_array($options['VALUE']) ? $options['VALUE'] : null;
	if(!$value)
		throw new Main\ArgumentException('Options VALUE must be specified.', 'VALUE');

	if(!is_array($entityFields))
	{
		$dbResult = \CCrmContact::GetListEx(
			array(),
			array('=ID' => $ownerID, 'CHECK_PERMISSIONS' => 'N'),
			false,
			false,
			array('ASSIGNED_BY_ID')
		);
		$entityFields = is_object($dbResult) ? $dbResult->Fetch() : null;
		if(!is_array($entityFields))
		{
			return false;
		}
	}

	$responsibleID = isset($entityFields['ASSIGNED_BY_ID']) ? (int)$entityFields['ASSIGNED_BY_ID'] : 0;
	$none = isset($value[StatisticsMark::None]) ?  (int)$value[StatisticsMark::None] : 0;
	$positive = isset($value[StatisticsMark::Positive]) ?  (int)$value[StatisticsMark::Positive] : 0;
	$negative = isset($value[StatisticsMark::Negative]) ?  (int)$value[StatisticsMark::Negative] : 0;

	if ($none === 0 && $positive === 0 && $negative === 0)
	{
		ContactActivityMarkStatisticsTable::delete(array(
			'OWNER_ID' => $ownerID,
			'DEADLINE_DATE' => $date,
			'PROVIDER_ID' => $providerId,
			'PROVIDER_TYPE_ID' => $providerTypeId,
			'SOURCE_ID' => $sourceId
		));
		return true;
	}

	$present = self::get($ownerID, $date, $providerId, $providerTypeId, $sourceId);
	if(is_array($present))
	{
		if($responsibleID === (int)$present['RESPONSIBLE_ID']
			&& $none === (int)$present['NONE_QTY']
			&& $positive === (int)$present['POSITIVE_QTY']
			&& $negative === (int)$present['NEGATIVE_QTY']
		)
			return true;

		if($responsibleID !== (int)$present['RESPONSIBLE_ID'])
		{
			ContactActivityMarkStatisticsTable::synchronize(
				$ownerID,
				array('RESPONSIBLE_ID' => $responsibleID)
			);
		}
	}

	$data = array(
		'OWNER_ID' => $ownerID,
		'DEADLINE_DATE' => $date,
		'PROVIDER_ID' => $providerId,
		'PROVIDER_TYPE_ID' => $providerTypeId,
		'SOURCE_ID' => $sourceId,
		'RESPONSIBLE_ID' => $responsibleID,
		'NONE_QTY' => $none,
		'POSITIVE_QTY' => $positive,
		'NEGATIVE_QTY' => $negative
	);

	ContactActivityMarkStatisticsTable::upsert($data);

	return true;
}