- Модуль: crm
- Путь к файлу: ~/bitrix/modules/crm/lib/activity/communicationstatistics.php
- Класс: Bitrix\Crm\Activity\CommunicationStatistics
- Вызов: CommunicationStatistics::countRelatedDealActivities
static function countRelatedDealActivities($relatedOwnerTypeId, $relatedOwnerId = null)
{
if ($relatedOwnerTypeId !== \CCrmOwnerType::Contact && $relatedOwnerTypeId !== \CCrmOwnerType::Company)
{
$entityTypeName = \CCrmOwnerType::ResolveName($relatedOwnerTypeId);
throw new Main\NotSupportedException("The '{$entityTypeName}' is not supported in current context");
}
$query = new Query(Crm\ActivityBindingTable::getEntity());
$query->registerRuntimeField('', new ExpressionField('CNT', 'COUNT(*)'));
$query->registerRuntimeField('',
new ReferenceField('DT',
Crm\DealTable::getEntity(),
array('=this.OWNER_ID' => 'ref.ID'),
array('join_type' => 'INNER')
)
);
$query->addFilter('=OWNER_TYPE_ID', \CCrmOwnerType::Deal);
$query->addSelect('CNT');
$query->addSelect('DT.CATEGORY_ID', 'CATEGORY_ID');
$query->addGroup('DT.CATEGORY_ID');
$query->registerRuntimeField('',
new ReferenceField('A',
Crm\ActivityTable::getEntity(),
array('=this.ACTIVITY_ID' => 'ref.ID'),
array('join_type' => 'INNER')
)
);
$query->addFilter('=A.COMPLETED', 'Y');
$query->addFilter('!=A.TYPE_ID', \CCrmActivityType::Task);
$subQuery = new Query(Crm\ActivityBindingTable::getEntity());
$subQuery->addFilter('=OWNER_TYPE_ID', $relatedOwnerTypeId);
if ($relatedOwnerId)
$subQuery->addFilter('=OWNER_ID', $relatedOwnerId);
$subQuery->addSelect('ACTIVITY_ID');
$query->registerRuntimeField('',
new ReferenceField('M',
Base::getInstanceByQuery($subQuery),
array('=this.ACTIVITY_ID' => 'ref.ACTIVITY_ID'),
array('join_type' => 'INNER')
)
);
$result = array();
$iterator = $query->exec();
while ($row = $iterator->fetch())
$result[$row['CATEGORY_ID']] = (int)$row['CNT'];
return $result;
}