• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/volume/contact.php
  • Класс: Bitrix\Crm\Volume\Contact
  • Вызов: Contact::measureEntity
public function measureEntity()
{
	self::loadTablesInformation();

	$query = $this->prepareQuery();

	if ($this->prepareFilter($query))
	{
		$avgContactTableRowLength = (double)self::$tablesInformation[Crm\ContactTable::getTableName()]['AVG_SIZE'];

		$connection = \Bitrix\Main\Application::getConnection();

		$this->checkTemporally();

		$data = array(
			'INDICATOR_TYPE' => '',
			'OWNER_ID' => '',
			'STAGE_SEMANTIC_ID' => '',
			'DATE_CREATE' => new \Bitrix\Main\Type\Date(),
			'ENTITY_COUNT' => '',
			'ENTITY_SIZE' => '',
		);

		$insert = $connection->getSqlHelper()->prepareInsert(Crm\VolumeTmpTable::getTableName(), $data);

		$sqlIns = 'INSERT INTO '.$connection->getSqlHelper()->quote(Crm\VolumeTmpTable::getTableName()). '('. $insert[0]. ') ';

		$query
			->registerRuntimeField(new ORM\Fields\ExpressionField('INDICATOR_TYPE', '\''.static::getIndicatorId().'\''))
			->addSelect('INDICATOR_TYPE')

			->registerRuntimeField(new ORM\Fields\ExpressionField('OWNER_ID', '\''.$this->getOwner().'\''))
			->addSelect('OWNER_ID')

			->registerRuntimeField(new ORM\Fields\ExpressionField('STAGE_SEMANTIC_ID', '\'-\''))
			->addSelect('STAGE_SEMANTIC_ID')

			//date
			->addSelect('DATE_CREATE_SHORT', 'DATE_CREATE_SHORT')
			->addGroup('DATE_CREATE_SHORT')

			->registerRuntimeField(new ORM\Fields\ExpressionField('ENTITY_COUNT', 'COUNT(%s)', 'ID'))
			->addSelect('ENTITY_COUNT')

			->registerRuntimeField(new ORM\Fields\ExpressionField('ENTITY_SIZE', 'COUNT(%s) * '.$avgContactTableRowLength, 'ID'))
			->addSelect('ENTITY_SIZE');

		$querySql = $sqlIns. $query->getQuery();

		$connection->queryExecute($querySql);

		if ($this->collectEntityRowSize)
		{
			$entityList = self::getEntityList();
			foreach ($entityList as $entityClass)
			{
				if ($entityClass == Crm\ContactTable::class)
				{
					continue;
				}
				/**
				 * @var \Bitrix\Main\ORM\Data\DataManager $entityClass
				 */
				$entityEntity = $entityClass::getEntity();

				if ($entityEntity->hasField('CONTACT_ID'))
				{
					$fieldName = 'CONTACT_ID';
				}
				elseif ($entityEntity->hasField('OWNER_ID'))
				{
					$fieldName = 'OWNER_ID';
				}
				else
				{
					continue;
				}

				$query = $this->prepareQuery();

				if ($this->prepareFilter($query))
				{
					$reference = new ORM\Fields\Relations\Reference(
						'RefEntity',
						$entityClass,
						array('this.ID' => 'ref.'.$fieldName),
						array('join_type' => 'INNER')
					);
					$query->registerRuntimeField($reference);

					$primary = $entityEntity->getPrimary();
					if (is_array($primary) && !empty($primary))
					{
						array_walk($primary, function (&$item) {
							$item = 'RefEntity.'.$item;
						});
					}
					elseif (!empty($primary))
					{
						$primary = array('RefEntity.'.$primary);
					}

					$query
						//primary
						->registerRuntimeField(new ORM\Fields\ExpressionField('COUNT_REF', 'COUNT(*)'))
						->addSelect('COUNT_REF')
						->setGroup($primary)

						//date
						->addSelect('DATE_CREATE_SHORT', 'DATE_CREATE_SHORT')
						->addGroup('DATE_CREATE_SHORT');

					$avgTableRowLength = (double)self::$tablesInformation[$entityClass::getTableName()]['AVG_SIZE'];

					$query1 = new ORM\Query\Query($query);
					$query1
						->registerRuntimeField(new ORM\Fields\ExpressionField('INDICATOR_TYPE', '\''.static::getIndicatorId().'\''))
						->addSelect('INDICATOR_TYPE')
						->registerRuntimeField(new ORM\Fields\ExpressionField('OWNER_ID', '\''.$this->getOwner().'\''))
						->addSelect('OWNER_ID')

						//date
						->addSelect('DATE_CREATE_SHORT')
						->addGroup('DATE_CREATE_SHORT')
						->registerRuntimeField(new ORM\Fields\ExpressionField('REF_SIZE', 'SUM(COUNT_REF) * '.$avgTableRowLength))
						->addSelect('REF_SIZE');


					Crm\VolumeTmpTable::updateFromSelect(
						$query1,
						array('ENTITY_SIZE' => 'destination.ENTITY_SIZE + source.REF_SIZE'),
						array(
							'INDICATOR_TYPE' => 'INDICATOR_TYPE',
							'OWNER_ID' => 'OWNER_ID',
							'DATE_CREATE' => 'DATE_CREATE_SHORT',
						)
					);
				}
			}
		}

		$this->copyTemporallyData();
	}

	return $this;
}