private function getCrmEntityCommunications($entityTypeId, $entityId, $communicationType): array
{
$communications = array();
$result = static function (&$data)
{
$communications = array();
foreach ($data as $item)
{
$id = 'CRM'.$item['ENTITY_TYPE'].$item['ENTITY_ID'].':'.hash('crc32b', $item['TYPE'].':'.$item['VALUE']);
if (!array_key_exists($id, $communications))
{
$communications[$id] = $item;
}
}
return array_values($communications);
};
if (in_array($entityTypeId, array(\CCrmOwnerType::Lead, \CCrmOwnerType::Contact, \CCrmOwnerType::Company)))
{
$communications = array_merge(
$communications,
$this->getCommunicationsFromFM($entityTypeId, $entityId, $communicationType)
);
if (\CCrmOwnerType::Lead == $entityTypeId)
{
$entity = \CCrmLead::getById($entityId);
if (empty($entity))
{
return $result($communications);
}
$entityCompanyId = isset($entity['COMPANY_ID']) ? (int)$entity['COMPANY_ID'] : 0;
if ($entityCompanyId > 0)
{
$communications = array_merge(
$communications,
$this->getCrmEntityCommunications(\CCrmOwnerType::Company, $entityCompanyId, $communicationType)
);
}
$entityContactsIds = \Bitrix\Crm\Binding\LeadContactTable::getLeadContactIds($entityId);
if (!empty($entityContactsIds))
{
$communications = array_merge(
$communications,
$this->getCrmEntityCommunications(\CCrmOwnerType::Contact, $entityContactsIds, $communicationType)
);
}
}
else if (\CCrmOwnerType::Company == $entityTypeId)
{
$communications = array_merge(
$communications,
\CCrmActivity::getCompanyCommunications($entityId, $communicationType)
);
}
}
else if (\CCrmOwnerType::Deal == $entityTypeId || \CCrmOwnerType::DealRecurring == $entityTypeId)
{
$entity = \CCrmDeal::getById($entityId);
if (empty($entity))
{
return $result($communications);
}
$entityCompanyId = isset($entity['COMPANY_ID']) ? intval($entity['COMPANY_ID']) : 0;
if ($entityCompanyId > 0)
{
$communications = array_merge(
$communications,
$this->getCrmEntityCommunications(\CCrmOwnerType::Company, $entityCompanyId, $communicationType)
);
}
$entityContactsIds = \Bitrix\Crm\Binding\DealContactTable::getDealContactIds($entityId);
if (!empty($entityContactsIds))
{
$communications = array_merge(
$communications,
$this->getCrmEntityCommunications(\CCrmOwnerType::Contact, $entityContactsIds, $communicationType)
);
}
$communications = array_merge(
$communications,
\CCrmActivity::getCommunicationsByOwner(\CCrmOwnerType::DealName, $entityId, $communicationType)
);
}
else if (\CCrmOwnerType::Invoice == $entityTypeId)
{
$entity = \CCrmInvoice::getById($entityId);
if (empty($entity))
return $result($communications);
$entityContactId = isset($entity['UF_CONTACT_ID']) ? (int) $entity['UF_CONTACT_ID'] : 0;
if ($entityContactId > 0)
{
$communications = array_merge(
$communications,
$this->getCrmEntityCommunications(\CCrmOwnerType::Contact, $entityContactId, $communicationType)
);
}
$entityCompanyId = isset($entity['UF_COMPANY_ID']) ? (int) $entity['UF_COMPANY_ID'] : 0;
if ($entityCompanyId > 0)
{
$communications = array_merge(
$communications,
$this->getCrmEntityCommunications(\CCrmOwnerType::Company, $entityCompanyId, $communicationType)
);
}
$entityDealId = isset($entity['UF_DEAL_ID']) ? (int) $entity['UF_DEAL_ID'] : 0;
if ($entityDealId > 0)
{
$communications = array_merge(
$communications,
$this->getCrmEntityCommunications(\CCrmOwnerType::Deal, $entityDealId, $communicationType)
);
}
}
else if (\CCrmOwnerType::Order == $entityTypeId)
{
$entity = \Bitrix\Crm\Order\Order::load((int)$entityId);
if (empty($entity))
{
return $result($communications);
}
$ccCollection = $entity->getContactCompanyCollection();
if ($primaryCompany = $ccCollection->getPrimaryCompany())
{
$communications = array_merge(
$communications,
$this->getCrmEntityCommunications(\CCrmOwnerType::Company, $primaryCompany->getField('ENTITY_ID'), $communicationType)
);
}
if ($primaryContact = $ccCollection->getPrimaryContact())
{
$communications = array_merge(
$communications,
$this->getCrmEntityCommunications(\CCrmOwnerType::Contact, $primaryContact->getField('ENTITY_ID'), $communicationType)
);
}
}
else if (\CCrmOwnerType::Quote == $entityTypeId)
{
$entity = \CCrmQuote::getById($entityId);
if (empty($entity))
{
return $result($communications);
}
$entityContactId = isset($entity['CONTACT_ID']) ? (int) $entity['CONTACT_ID'] : 0;
if ($entityContactId > 0)
{
$communications = array_merge(
$communications,
$this->getCrmEntityCommunications(\CCrmOwnerType::Contact, $entityContactId, $communicationType)
);
}
$entityCompanyId = isset($entity['COMPANY_ID']) ? (int) $entity['COMPANY_ID'] : 0;
if ($entityCompanyId > 0)
{
$communications = array_merge(
$communications,
$this->getCrmEntityCommunications(\CCrmOwnerType::Company, $entityCompanyId, $communicationType)
);
}
$entityDealId = isset($entity['DEAL_ID']) ? (int) $entity['DEAL_ID'] : 0;
if ($entityDealId > 0)
{
$communications = array_merge(
$communications,
$this->getCrmEntityCommunications(\CCrmOwnerType::Deal, $entityDealId, $communicationType)
);
}
$entityLeadId = isset($entity['LEAD_ID']) ? (int) $entity['LEAD_ID'] : 0;
if ($entityLeadId > 0)
{
$communications = array_merge(
$communications,
$this->getCrmEntityCommunications(\CCrmOwnerType::Lead, $entityLeadId, $communicationType)
);
}
}
return $result($communications);
}