CAllCrmActivity::GetNearest

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CAllCrmActivity
  4. GetNearest
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_activity.php
  • Класс: \CAllCrmActivity
  • Вызов: CAllCrmActivity::GetNearest
static function GetNearest($ownerTypeID, $ownerID, $userID)
{
	global $DB;
	$userID = (int)$userID;

	if (\Bitrix\Main\Config\Option::get('crm', 'enable_any_incoming_act', 'Y') === 'Y')
	{
		$query = Crm\Activity\Entity\EntityUncompletedActivityTable::query()
			->where('ENTITY_TYPE_ID', (int)$ownerTypeID)
			->where('ENTITY_ID', (int)$ownerID)
			->setOrder(['MIN_DEADLINE' => 'ASC'])
			->setLimit(1)
			->setSelect(['ACTIVITY_ID', 'MIN_DEADLINE'])
		;
		if($userID > 0)
		{
			$query->where('RESPONSIBLE_ID', $userID);
		}
		if ($result = $query->fetch())
		{
			return [
				'ID' => $result['ACTIVITY_ID'],
				'DEADLINE' => $result['MIN_DEADLINE'] instanceof DateTime ? $result['MIN_DEADLINE']->toString() : false,
			];
		}

		return false;
	}

	$tableName = CCrmActivity::TABLE_NAME;
	$bindingTableName = CCrmActivity::BINDING_TABLE_NAME;
	$deadline = $DB->DateToCharFunction('a.DEADLINE', 'FULL');

	if($userID > 0)
	{
		$sql = "SELECT a.ID, {$deadline} AS DEADLINE_FORMATTED, a.DEADLINE FROM {$tableName} a INNER JOIN {$bindingTableName} b ON a.ID = b.ACTIVITY_ID AND a.COMPLETED = 'N' AND a.RESPONSIBLE_ID = {$userID} AND a.DEADLINE IS NOT NULL AND b.OWNER_TYPE_ID = {$ownerTypeID} AND b.OWNER_ID = {$ownerID} ORDER BY a.DEADLINE ASC";
	}
	else
	{
		$sql = "SELECT a.ID, {$deadline} AS DEADLINE_FORMATTED, a.DEADLINE FROM {$tableName} a INNER JOIN {$bindingTableName} b ON a.ID = b.ACTIVITY_ID AND a.COMPLETED = 'N' AND a.DEADLINE IS NOT NULL AND b.OWNER_TYPE_ID = {$ownerTypeID} AND b.OWNER_ID = {$ownerID} ORDER BY a.DEADLINE ASC";
	}

	$dbResult = $DB->Query(
		$DB->TopSql($sql, 1),
		false,
		'File: '.__FILE__.'
Line: '.__LINE__ ); $arResult = $dbResult ? $dbResult->Fetch() : null; if($arResult) { $arResult['DEADLINE'] = $arResult['DEADLINE_FORMATTED']; unset($arResult['DEADLINE_FORMATTED']); } return $arResult; }

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