DealInvoiceStatistics::getList

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. DealInvoiceStatistics
  4. getList
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/widget/data/dealinvoicestatistics.php
  • Класс: Bitrix\Crm\Widget\Data\DealInvoiceStatistics
  • Вызов: DealInvoiceStatistics::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);

	$categoryID = (int)$filter->getExtraParam('dealCategoryID', -1);
	$semanticID = $filter->getExtraParam('semanticID', PhaseSemantics::UNDEFINED);
	$isFinalSemantics = PhaseSemantics::isFinal($semanticID);

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

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

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

	if($aggregate !== '' && !in_array($aggregate, array('SUM', 'COUNT', 'MAX', 'MIN')))
	{
		$aggregate = '';
	}

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

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

	$query = new Query(DealInvoiceStatisticsTable::getEntity());
	$query->addSelect($name);

	if($aggregate !== '')
	{
		if($aggregate === 'COUNT')
		{
			$query->registerRuntimeField('', new ExpressionField($name, "COUNT(*)"));
		}
		else
		{
			$query->registerRuntimeField('', new ExpressionField($name, "{$aggregate}({$name})"));
		}
	}

	$query->setTableAliasPostfix('_s2');

	$subQuery = new Query(DealInvoiceStatisticsTable::getEntity());
	$subQuery->setTableAliasPostfix('_s1');
	$subQuery->addSelect('OWNER_ID');

	$subQuery->addFilter('>=END_DATE', $periodStartDate);
	$subQuery->addFilter('<=START_DATE', $periodEndDate);
	//$subQuery->addFilter('<=CREATED_DATE', $periodEndDate);

	if($semanticID !== PhaseSemantics::UNDEFINED)
	{
		$subQuery->addFilter('=STAGE_SEMANTIC_ID', $semanticID);
	}

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

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

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

	$subQuery->addGroup('OWNER_ID');
	$subQuery->addSelect('MAX_CREATED_DATE');
	$subQuery->registerRuntimeField('', new ExpressionField('MAX_CREATED_DATE', 'MAX(CREATED_DATE)'));

	$query->registerRuntimeField('',
		new ReferenceField('M',
			Base::getInstanceByQuery($subQuery),
			array('=this.OWNER_ID' => 'ref.OWNER_ID', '=this.CREATED_DATE' => 'ref.MAX_CREATED_DATE'),
			array('join_type' => 'INNER')
		)
	);

	$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)
		{
			if($isFinalSemantics)
			{
				$query->addSelect('END_DATE', 'D');
				$query->addGroup('END_DATE');
				if(!$sort)
				{
					$query->addOrder('END_DATE', 'ASC');
				}
			}
			else
			{
				$query->addSelect('CREATED_DATE', 'D');
				$query->addGroup('CREATED_DATE');
				if(!$sort)
				{
					$query->addOrder('CREATED_DATE', 'ASC');
				}
			}
		}
	}

	$dbResult = $query->exec();
	//Trace('sql', Query::getLastQuery(), 1);
	$result = array();
	if($group === self::GROUP_BY_DATE)
	{
		while($ary = $dbResult->fetch())
		{
			$ary['DATE'] = $ary['D']->format('Y-m-d');
			unset($ary['D']);

			if($ary['DATE'] === '9999-12-31')
			{
				//Skip empty dates
				continue;
			}

			$result[] = $ary;
		}
	}
	elseif($group === self::GROUP_BY_USER)
	{
		$userIDs = array();
		while($ary = $dbResult->fetch())
		{
			$userID = $ary['RESPONSIBLE_ID'] = (int)$ary['RESPONSIBLE_ID'];
			if($userID > 0 && !isset($userNames[$userID]))
			{
				$userIDs[$userID] = true;
			}
			$result[] = $ary;
		}
		self::parseUserInfo($result, array('RESPONSIBLE_ID' => 'USER'));
	}
	else
	{
		while($ary = $dbResult->fetch())
		{
			$result[] = $ary;
		}
	}
	return $result;
}

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