DealIdle::getList

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. DealIdle
  4. getList
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/widget/data/dealidle.php
  • Класс: Bitrix\Crm\Widget\Data\DealIdle
  • Вызов: DealIdle::getList
public function getList(array $params)
{
	/** @var Filter $filter */
	$filter = isset($params['filter']) ? $params['filter'] : null;
	if(!($filter instanceof Filter))
	{
		throw new Main\ObjectNotFoundException("The 'filter' is not found in params.");
	}

	$this->applyFilterContext($filter);

	$permissionSql = '';
	if($this->enablePermissionCheck)
	{
		$permissionSql = $this->preparePermissionSql();
		if($permissionSql === false)
		{
			//Access denied;
			return array();
		}
	}


	/** @var array $select */
	$select = isset($params['select']) && is_array($params['select']) ? $params['select'] : array();
	$name = '';
	if(!empty($select))
	{
		$selectItem = $select[0];
		if(isset($selectItem['name']))
		{
			$name = $selectItem['name'];
		}
	}

	if($name === '')
	{
		$name = 'COUNT';
	}

	$group = isset($params['group'])? mb_strtoupper($params['group']) : '';
	if($group !== '' && $group !== self::GROUP_BY_USER && $group !== self::GROUP_BY_DATE)
	{
		$group = '';
	}

	$period = $filter->getPeriod();
	$periodStartDate = $period['START'];
	$periodEndDate = $period['END'];

	$query = new Query(DealStageHistoryTable::getEntity());
	$query->registerRuntimeField('', new ExpressionField($name, "COUNT(*)"));
	$query->addSelect($name);
	$query->addFilter('=TYPE_ID', HistoryEntryType::CREATION);
	$query->addFilter('>=START_DATE', $periodStartDate);
	$query->addFilter('<=START_DATE', $periodEndDate);

	if($this->enablePermissionCheck && is_string($permissionSql) && $permissionSql !== '')
	{
		$query->addFilter('@OWNER_ID', new SqlExpression($permissionSql));
	}

	$responsibleIDs = $filter->getResponsibleIDs();
	if(is_array($responsibleIDs) && !empty($responsibleIDs))
	{
		$query->addFilter('@RESPONSIBLE_ID', $responsibleIDs);
	}

	$categoryID = (int)$filter->getExtraParam('dealCategoryID', -1);
	if($categoryID >= 0)
	{
		//HACK: use SqlExpression to avoid filter sql like (CATEGORY_ID IS NULL OR CATEGORY_ID = 0), that cause the filesort.
		$query->addFilter('=CATEGORY_ID', new Main\DB\SqlExpression('?i', $categoryID));
	}

	$sort = isset($params['sort']) && is_array($params['sort']) && !empty($params['sort']) ? $params['sort'] : null;
	if($sort)
	{
		foreach($sort as $sortItem)
		{
			if(isset($sortItem['name']))
			{
				$query->addOrder($sortItem['name'], isset($sortItem['order']) ? $sortItem['order'] : 'ASC');
			}
		}
	}

	if($group !== '')
	{
		if($group === self::GROUP_BY_USER)
		{
			$query->addSelect('RESPONSIBLE_ID');
			$query->addGroup('RESPONSIBLE_ID');
		}
		else//if($group === self::GROUP_BY_DATE)
		{
			$query->addSelect('START_DATE', 'DATE');
			$query->addGroup('START_DATE');
			if(!$sort)
			{
				$query->addOrder('START_DATE', 'ASC');
			}
		}
	}

	$query->registerRuntimeField('',
		new ExpressionField(
			'E1',
			'(CASE WHEN NOT EXISTS('.self::prepareHistoryQuery($periodStartDate, $periodEndDate, HistoryEntryType::MODIFICATION, '%s', '_h')->getQuery().') THEN 1 ELSE 0 END)',
			'OWNER_ID'
		)
	);
	$query->addFilter('=E1', 1);

	$query->registerRuntimeField('',
		new ExpressionField(
			'E2',
			'(CASE WHEN NOT EXISTS('.self::prepareHistoryQuery($periodStartDate, $periodEndDate, HistoryEntryType::FINALIZATION, '%s', '_h')->getQuery().') THEN 1 ELSE 0 END)',
			'OWNER_ID'
		)
	);
	$query->addFilter('=E2', 1);

	$query->registerRuntimeField('',
		new ExpressionField(
			'E3',
			'(CASE WHEN NOT EXISTS('.self::prepareActivityQuery($periodStartDate, $periodEndDate, '%s', '_a')->getQuery().') THEN 1 ELSE 0 END)',
			'OWNER_ID'
		)
	);
	$query->addFilter('=E3', 1);

	$query->registerRuntimeField('',
		new ExpressionField(
			'E4',
			'(CASE WHEN NOT EXISTS('.self::prepareInvoiceQuery($periodStartDate, $periodEndDate, '%s', '_i')->getQuery().') THEN 1 ELSE 0 END)',
			'OWNER_ID'
		)
	);
	$query->addFilter('=E4', 1);

	$results = array();
	$dbResult = $query->exec();
	if($group === self::GROUP_BY_DATE)
	{
		while($ary = $dbResult->fetch())
		{
			$ary['DATE'] = $ary['DATE']->format('Y-m-d');
			$results[] = $ary;
		}
	}
	elseif($group === self::GROUP_BY_USER)
	{
		$rawResult = array();
		$userIDs = array();
		while($ary = $dbResult->fetch())
		{
			$userID = $ary['RESPONSIBLE_ID'] = (int)$ary['RESPONSIBLE_ID'];
			if($userID > 0 && !isset($userIDs[$userID]))
			{
				$userIDs[$userID] = true;
			}
			$rawResult[] = $ary;
		}
		$userNames = self::prepareUserNames(array_keys($userIDs));
		foreach($rawResult as $item)
		{
			$userID = $item['RESPONSIBLE_ID'];
			$item['USER_ID'] = $userID;
			$item['USER'] = isset($userNames[$userID]) ? $userNames[$userID] : "[{$userID}]";
			unset($item['RESPONSIBLE_ID']);

			$results[] = $item;
		}
	}
	else
	{
		while($ary = $dbResult->fetch())
		{
			$results[] = $ary;
		}
	}

	return $results;
}

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