- Модуль: crm
- Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_report_helper.php
- Класс: \CCrmReportHelper
- Вызов: CCrmReportHelper::appendUTMFields
static function appendUTMFields(\Bitrix\Main\Entity\Base $entity)
{
// Lead, Deal, Invoice->Deal, Product->Deal
$connection = Main\Application::getConnection();
$sqlHelper = $connection->getSqlHelper();
$entityTypeId = CCrmOwnerType::ResolveID(mb_strtoupper($entity->getName()));
$utmRefMap = static::getUTMFieldMap();
$isNeedAppendFields = ($entityTypeId === CCrmOwnerType::Lead || $entityTypeId === CCrmOwnerType::Deal)
&& is_array($utmRefMap) && !empty($utmRefMap);
$utmFields = [];
foreach($entity->getFields() as $field)
{
if (in_array($field->getName(), array('INVOICE_UTS', 'DEAL_BY', 'DEAL_OWNER'), true)
&& $field instanceof Bitrix\Main\Entity\ReferenceField)
{
self::appendUTMFields($field->getRefEntity());
}
else
{
$fieldName = $field->getName();
if ($isNeedAppendFields && $field instanceof Bitrix\Main\Entity\ReferenceField
&& isset($utmRefMap[$fieldName]))
{
$utmFields[] = [
'def' => [
'data_type' => 'string',
'expression' => [
"(SELECT UTM.VALUE ".
"FROM b_crm_utm UTM ".
"WHERE UTM.ENTITY_TYPE_ID = $entityTypeId ".
"AND UTM.ENTITY_ID = %s ".
"AND UTM.CODE = '".$sqlHelper->forSql($fieldName, 45)."')",
'ID'
]
],
'name' => $fieldName.self::UTM_FIELD_POSTFIX
];
}
else if ($fieldName === 'IS_RETURN_CUSTOMER')
{
$utmFields[] = [
'def' => [
'data_type' => 'boolean',
'expression' => ['%s', 'IS_RETURN_CUSTOMER'],
'values' => array('N', 'Y')
],
'name' => 'IS_RETURN_CUSTOMER_BL'
];
}
}
}
foreach ($utmFields as $fieldInfo)
{
if (!$entity->hasField($fieldInfo['name']))
{
$entity->addField($fieldInfo['def'], $fieldInfo['name']);
}
}
}