• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/volume/event.php
  • Класс: Bitrix\Crm\Volume\Event
  • Вызов: Event::prepareRelationQuery
public function prepareRelationQuery($indicatorType)
{
	$query = Crm\EventRelationsTable::query();

	$query->registerRuntimeField(new ORM\Fields\ExpressionField(
		'FILES',
		'%s',
		'EVENT_BY.FILES'
	));

	$query->registerRuntimeField(
		(new ORM\Fields\ExpressionField(
			'DATE_CREATE',
			'%s',
			'EVENT_BY.DATE_CREATE'
		))
		->configureValueType(ORM\Fields\DatetimeField::class)
	);

	$query->registerRuntimeField(new ORM\Fields\ExpressionField(
		'DATE_CREATED_SHORT',
		'DATE(%s)',
		'DATE_CREATE'
	));

	if (
		$indicatorType == Crm\Volume\Company::class
	)
	{
		$categoryId = Crm\Volume\Company::getCategoryId();
		$companyRelation = new ORM\Fields\Relations\Reference(
			'COMPANY',
			Crm\CompanyTable::class,
			ORM\Query\Join::on('this.ENTITY_ID', 'ref.ID')
				->where('this.ENTITY_TYPE', \CCrmOwnerType::CompanyName)
				->where('ref.CATEGORY_ID', $categoryId)
				->where('ref.IS_MY_COMPANY', 'N'),
			['join_type' => ($indicatorType == Crm\Volume\Company::class ? 'INNER' : 'LEFT')]
		);
		$query->registerRuntimeField($companyRelation);
	}
	elseif (
		$indicatorType == Crm\Volume\Contact::class
	)
	{
		$categoryId = Crm\Volume\Contact::getCategoryId();
		$leadRelation = new ORM\Fields\Relations\Reference(
			'CONTACT',
			Crm\ContactTable::class,
			ORM\Query\Join::on('this.ENTITY_ID', 'ref.ID')
				->where('this.ENTITY_TYPE', \CCrmOwnerType::ContactName)
				->where('ref.CATEGORY_ID', $categoryId),
			['join_type' => ($indicatorType == Crm\Volume\Contact::class ? 'INNER' : 'LEFT')]
		);
		$query->registerRuntimeField($leadRelation);
	}
	else
	{
		$dealRelation = new ORM\Fields\Relations\Reference(
			'DEAL',
			Crm\DealTable::class,
			ORM\Query\Join::on('this.ENTITY_ID', 'ref.ID')->where('this.ENTITY_TYPE', \CCrmOwnerType::DealName),
			array('join_type' => ($indicatorType == Crm\Volume\Deal::class ? 'INNER' : 'LEFT'))
		);
		$query->registerRuntimeField($dealRelation);

		$leadRelation = new ORM\Fields\Relations\Reference(
			'LEAD',
			Crm\LeadTable::class,
			ORM\Query\Join::on('this.ENTITY_ID', 'ref.ID')->where('this.ENTITY_TYPE', \CCrmOwnerType::LeadName),
			array('join_type' => ($indicatorType == Crm\Volume\Lead::class ? 'INNER' : 'LEFT'))
		);
		$query->registerRuntimeField($leadRelation);

		$leadRelation = new ORM\Fields\Relations\Reference(
			'QUOTE',
			Crm\QuoteTable::class,
			ORM\Query\Join::on('this.ENTITY_ID', 'ref.ID')->where('this.ENTITY_TYPE', \CCrmOwnerType::QuoteName),
			array('join_type' => ($indicatorType == Crm\Volume\Quote::class ? 'INNER' : 'LEFT'))
		);
		$query->registerRuntimeField($leadRelation);

		// STAGE_SEMANTIC_ID
		Crm\Volume\Quote::registerStageField($query, 'QUOTE', 'QUOTE_STAGE_SEMANTIC_ID');

		$leadRelation = new ORM\Fields\Relations\Reference(
			'INVOICE',
			Crm\InvoiceTable::class,
			ORM\Query\Join::on('this.ENTITY_ID', 'ref.ID')->where('this.ENTITY_TYPE', \CCrmOwnerType::InvoiceName),
			array('join_type' => ($indicatorType == Crm\Volume\Invoice::class ? 'INNER' : 'LEFT'))
		);
		$query->registerRuntimeField($leadRelation);

		$dayField = new ORM\Fields\ExpressionField(
			'INVOICE_DATE_CREATE_SHORT',
			'DATE(%s)',
			'INVOICE.DATE_INSERT'
		);
		$query->registerRuntimeField($dayField);


		// STAGE_SEMANTIC_ID
		Crm\Volume\Invoice::registerStageField($query, 'INVOICE', 'INVOICE_STAGE_SEMANTIC_ID');

		$stageField = new ORM\Fields\ExpressionField(
			'STAGE_SEMANTIC_ID',
			'case '.
			' when %s is not null then %s '.
			' when %s is not null then %s '.
			' when (%s) is not null then (%s) '.
			' when (%s) is not null then (%s) '.
			' else \'-\' '.
			'end',
			array(
				'DEAL.STAGE_SEMANTIC_ID', 'DEAL.STAGE_SEMANTIC_ID',
				'LEAD.STATUS_SEMANTIC_ID', 'LEAD.STATUS_SEMANTIC_ID',
				'QUOTE_STAGE_SEMANTIC_ID', 'QUOTE_STAGE_SEMANTIC_ID',
				'INVOICE_STAGE_SEMANTIC_ID', 'INVOICE_STAGE_SEMANTIC_ID'
			)
		);
		$query->registerRuntimeField($stageField);
	}

	return $query;
}