10 private const COMMA =
',';
39 \Bitrix\Main\Localization\Loc::loadMessages(
40 $_SERVER[
'DOCUMENT_ROOT'].BX_ROOT.
'/components/bitrix/crm.'.strtolower($entityType).
'.edit/component.php'
45 $arResult += static::getAssignedByFields();
47 $ar = \CCrmFieldMulti::GetEntityTypeList();
48 foreach ($ar as $typeId => $arFields)
50 foreach ($arFields as $valueType => $valueName)
52 $arResult[$typeId.
'_'.$valueType] = [
67 \Bitrix\Main\Localization\Loc::loadMessages(
68 $_SERVER[
'DOCUMENT_ROOT'].BX_ROOT.
'/modules/crm/classes/general/crm_document.php'
72 'ASSIGNED_BY.EMAIL' => [
73 'Name' => GetMessage(
'CRM_DOCUMENT_FIELD_ASSIGNED_BY_EMAIL'),
76 'ASSIGNED_BY.WORK_PHONE' => [
77 'Name' => GetMessage(
'CRM_DOCUMENT_FIELD_ASSIGNED_BY_WORK_PHONE'),
80 'ASSIGNED_BY.PERSONAL_MOBILE' => [
81 'Name' => GetMessage(
'CRM_DOCUMENT_FIELD_ASSIGNED_BY_PERSONAL_MOBILE'),
84 'ASSIGNED_BY.UF_PHONE_INNER' => [
85 'Name' => GetMessage(
'CRM_DOCUMENT_FIELD_ASSIGNED_BY_UF_PHONE_INNER'),
89 'ASSIGNED_BY.LOGIN' => [
90 'Name' => GetMessage(
'CRM_DOCUMENT_FIELD_ASSIGNED_BY_LOGIN'),
93 'ASSIGNED_BY.ACTIVE' => [
94 'Name' => GetMessage(
'CRM_DOCUMENT_FIELD_ASSIGNED_BY_ACTIVE'),
97 'ASSIGNED_BY.LAST_NAME' => [
98 'Name' => GetMessage(
'CRM_DOCUMENT_FIELD_ASSIGNED_BY_LAST_NAME'),
101 'ASSIGNED_BY.NAME' => [
102 'Name' => GetMessage(
'CRM_DOCUMENT_FIELD_ASSIGNED_BY_NAME'),
105 'ASSIGNED_BY.SECOND_NAME' => [
106 'Name' => GetMessage(
'CRM_DOCUMENT_FIELD_ASSIGNED_BY_SECOND_NAME'),
109 'ASSIGNED_BY.WORK_POSITION' => [
110 'Name' => GetMessage(
'CRM_DOCUMENT_FIELD_ASSIGNED_BY_WORK_POSITION'),
113 'ASSIGNED_BY.PERSONAL_WWW' => [
114 'Name' => GetMessage(
'CRM_DOCUMENT_FIELD_ASSIGNED_BY_PERSONAL_WWW'),
117 'ASSIGNED_BY.PERSONAL_CITY' => [
118 'Name' => GetMessage(
'CRM_DOCUMENT_FIELD_ASSIGNED_BY_PERSONAL_CITY'),
121 'ASSIGNED_BY.UF_SKYPE' => [
122 'Name' => GetMessage(
'CRM_DOCUMENT_FIELD_ASSIGNED_BY_UF_SKYPE'),
125 'ASSIGNED_BY.UF_TWITTER' => [
126 'Name' => GetMessage(
'CRM_DOCUMENT_FIELD_ASSIGNED_BY_UF_TWITTER'),
129 'ASSIGNED_BY.UF_FACEBOOK' => [
130 'Name' => GetMessage(
'CRM_DOCUMENT_FIELD_ASSIGNED_BY_UF_FACEBOOK'),
133 'ASSIGNED_BY.UF_LINKEDIN' => [
134 'Name' => GetMessage(
'CRM_DOCUMENT_FIELD_ASSIGNED_BY_UF_LINKEDIN'),
137 'ASSIGNED_BY.UF_XING' => [
138 'Name' => GetMessage(
'CRM_DOCUMENT_FIELD_ASSIGNED_BY_UF_XING'),
141 'ASSIGNED_BY.UF_WEB_SITES' => [
142 'Name' => GetMessage(
'CRM_DOCUMENT_FIELD_ASSIGNED_BY_UF_WEB_SITES'),
150 return \CCrmOwnerType::isUseFactoryBasedApproach(\CCrmOwnerType::ResolveID($entityType));
168 array $usedFields = [
'*'],
169 string $sortBy =
'id',
170 string $sortOrder =
'asc'
173 if(empty($usedFields))
178 \Bitrix\Main\Localization\Loc::loadMessages(
179 $_SERVER[
'DOCUMENT_ROOT'].BX_ROOT.
'/modules/crm/classes/general/crm_fields.php'
182 $entityType = ucfirst(strtolower($entityType));
183 $className =
'CCrm'.$entityType;
184 $dbDocumentList = $className::GetListEx(
189 "SQL" =>
"L.ID IN (".implode(
",", $entityIds).
")"
192 "CHECK_PERMISSIONS" =>
"N"
198 [
'UF_*',
'ASSIGNED_BY_ID']
202 while (($objDocument = $dbDocumentList->Fetch()) !==
false)
204 $assignedByID = isset($objDocument[
'ASSIGNED_BY_ID'])? intval($objDocument[
'ASSIGNED_BY_ID']) : 0;
206 if ($assignedByID > 0)
211 self::addAddressFieldsValue($entityType, $usedFields, $objDocument);
212 self::addMultiFieldsValue($usedFields, $entityType, $objDocument);
213 self::addUserFieldsValue($className, $objDocument);
216 $typeId = \CCrmOwnerType::ResolveID($entityType);
217 $objDocument += static::getCommunicationFieldsValues($typeId, $objDocument[
'ID']);
219 $data[$objDocument[
'ID']] = $objDocument;
229 private static function addAddressFieldsValue($entityType, $usedFields, &$objDocument)
231 $rqAddr =
new Bitrix\Crm\RequisiteAddress();
233 $res = $rqAddr->getList(
236 'ENTITY_TYPE_ID' => \CCrmOwnerType::Requisite,
237 'ANCHOR_TYPE_ID' => \CCrmOwnerType::ResolveID($entityType),
238 'ANCHOR_ID' => $objDocument[
'ID']
241 'ADDRESS_TYPE_ID' =>
'TYPE_ID',
242 'ADDRESS' =>
'ADDRESS_1',
244 'ADDRESS_CITY' =>
'CITY',
245 'ADDRESS_POSTAL_CODE' =>
'POSTAL_CODE',
246 'ADDRESS_REGION' =>
'REGION',
247 'ADDRESS_PROVINCE' =>
'PROVINCE',
248 'ADDRESS_COUNTRY' =>
'COUNTRY',
249 'ADDRESS_COUNTRY_CODE' =>
'COUNTRY_CODE'
259 foreach ($res as $addresses)
262 (
int)$addresses[
'ADDRESS_TYPE_ID'] === (
int)\
Bitrix\Crm\EntityAddressType::Primary
265 foreach ($addresses as $key => $address)
267 $objDocument[$key] = $address;
270 if(isset($usedFields[strtoupper($entityType).
'.ADDRESS_LEGAL']) &&
271 (
int)$addresses[
'ADDRESS_TYPE_ID'] === \
Bitrix\Crm\EntityAddressType::Registered)
273 $objDocument[
'ADDRESS_LEGAL'] = self::buildAddress($entityType, $addresses);
278 private static function buildAddress($entityType,$address)
280 $entityAddressClassName =
'\\Bitrix\\Crm\\'.ucfirst(strtolower($entityType)).
'Address';
281 return Bitrix\Crm\Format\AddressFormatter::getSingleInstance()->formatTextComma(
282 $entityAddressClassName::mapEntityFields(
284 'ADDRESS' => $address[
'ADDRESS'],
285 'ADDRESS_2' => $address[
'ADDRESS_2'],
286 'ADDRESS_CITY' => $address[
'ADDRESS_CITY'],
287 'ADDRESS_REGION' => $address[
'ADDRESS_REGION'],
288 'ADDRESS_PROVINCE' => $address[
'ADDRESS_PROVINCE'],
289 'ADDRESS_POSTAL_CODE' => $address[
'ADDRESS_POSTAL_CODE'],
290 'ADDRESS_COUNTRY' => $address[
'ADDRESS_COUNTRY'],
291 'ADDRESS_LOC_ADDR_ID' => $address[
'ADDRESS_COUNTRY']
302 private static function addMultiFieldsValue($usedFields, $entityType, &$objDocument)
304 $userMultiFields = \CCrmFieldMulti::GetAllEntityFields($entityType, $objDocument[
'ID']);
306 foreach ($usedFields as $usedField)
308 self::checkUsedField($usedField, $entityType, $objDocument);
309 $splitedField = explode(
'_', $usedField);
312 in_array($splitedField[0], [
'PHONE',
'IM',
'EMAIL',
'WEB'])
313 && isset($userMultiFields[$splitedField[0]])
316 foreach ($userMultiFields[$splitedField[0]] as $field)
318 if(!isset($splitedField[1]))
320 $objDocument[$usedField] = $field[
'VALUE'];
324 if($field[
'VALUE_TYPE'] === $splitedField[1])
326 $objDocument[$usedField] = $field[
'VALUE'];
337 private static function addUserFieldsValue($className, &$objDocument)
339 $userFieldsList = $className::GetUserFields();
341 if (is_array($userFieldsList))
343 static::processUserFieldValues($userFieldsList, $objDocument);
349 foreach ($userFieldsSettings as $userFieldName => $userFieldParams)
351 $fieldTypeID = isset($userFieldParams[
'USER_TYPE'])? $userFieldParams[
'USER_TYPE'][
'USER_TYPE_ID']
353 $isFieldMultiple = isset($userFieldParams[
'MULTIPLE']) && $userFieldParams[
'MULTIPLE'] ===
'Y';
354 $fieldSettings = isset($userFieldParams[
'SETTINGS'])? $userFieldParams[
'SETTINGS'] : [];
356 if (isset($userFieldValues[$userFieldName]))
358 $fieldValue = $userFieldValues[$userFieldName];
360 elseif (isset($fieldSettings[
'DEFAULT_VALUE']))
362 $fieldValue = $fieldSettings[
'DEFAULT_VALUE'];
365 if ($fieldTypeID ===
'employee')
367 if (!$isFieldMultiple)
369 $userFieldValues[$userFieldName] = $fieldValue;
371 elseif (is_array($fieldValue))
373 $userFieldValues[$userFieldName] = [];
374 foreach ($fieldValue as $value)
376 $userFieldValues[$userFieldName][] = $value;
380 elseif ($fieldTypeID ===
'boolean')
382 $userFieldValues[$userFieldName] = self::getBool($fieldValue)?
'Y' :
'N';
387 private static function getBool($value)
389 if (empty($value) || $value ===
'false' || is_int($value) && ($value == 0) || (mb_strtoupper($value) ==
'N'))
402 if ($assignedByID < 1)
407 $dbUsers = \CUser::GetList(
410 [
'ID' => $assignedByID],
437 $arUser = is_object($dbUsers)? $dbUsers->Fetch() :
null;
438 $objDocument[
'ASSIGNED_BY.EMAIL'] = is_array($arUser)? $arUser[
'EMAIL'] :
'';
439 $objDocument[
'ASSIGNED_BY.WORK_PHONE'] = is_array($arUser)? $arUser[
'WORK_PHONE'] :
'';
440 $objDocument[
'ASSIGNED_BY.PERSONAL_MOBILE'] = is_array($arUser)? $arUser[
'PERSONAL_MOBILE'] :
'';
442 $objDocument[
'ASSIGNED_BY.LOGIN'] = is_array($arUser)? $arUser[
'LOGIN'] :
'';
443 $objDocument[
'ASSIGNED_BY.ACTIVE'] = is_array($arUser)? $arUser[
'ACTIVE'] :
'';
444 $objDocument[
'ASSIGNED_BY.NAME'] = is_array($arUser)? $arUser[
'NAME'] :
'';
445 $objDocument[
'ASSIGNED_BY.LAST_NAME'] = is_array($arUser)? $arUser[
'LAST_NAME'] :
'';
446 $objDocument[
'ASSIGNED_BY.SECOND_NAME'] = is_array($arUser)? $arUser[
'SECOND_NAME'] :
'';
447 $objDocument[
'ASSIGNED_BY.WORK_POSITION'] = is_array($arUser)? $arUser[
'WORK_POSITION'] :
'';
448 $objDocument[
'ASSIGNED_BY.PERSONAL_WWW'] = is_array($arUser)? $arUser[
'PERSONAL_WWW'] :
'';
449 $objDocument[
'ASSIGNED_BY.PERSONAL_CITY'] = is_array($arUser)? $arUser[
'PERSONAL_CITY'] :
'';
450 $objDocument[
'ASSIGNED_BY.UF_SKYPE'] = is_array($arUser)? $arUser[
'UF_SKYPE'] :
'';
451 $objDocument[
'ASSIGNED_BY.UF_TWITTER'] = is_array($arUser)? $arUser[
'UF_TWITTER'] :
'';
452 $objDocument[
'ASSIGNED_BY.UF_FACEBOOK'] = is_array($arUser)? $arUser[
'UF_FACEBOOK'] :
'';
453 $objDocument[
'ASSIGNED_BY.UF_LINKEDIN'] = is_array($arUser)? $arUser[
'UF_LINKEDIN'] :
'';
454 $objDocument[
'ASSIGNED_BY.UF_XING'] = is_array($arUser)? $arUser[
'UF_XING'] :
'';
455 $objDocument[
'ASSIGNED_BY.UF_WEB_SITES'] = is_array($arUser)? $arUser[
'UF_WEB_SITES'] :
'';
456 $objDocument[
'ASSIGNED_BY.UF_PHONE_INNER'] = is_array($arUser)? $arUser[
'UF_PHONE_INNER'] :
'';
464 private static function checkUsedField($usedField, $entityType, &$objDocument)
469 $objDocument[
'FULL_ADDRESS'] = self::buildAddress($entityType, $objDocument);
471 case 'BANKING_DETAILS':
472 $requisites = Bitrix\Crm\EntityRequisite::getSingleInstance()
476 '=ENTITY_TYPE_ID' => \CCrmOwnerType::ResolveID($entityType),
477 '=ENTITY_ID' => $objDocument[
'ID']
483 $titleMap = Bitrix\Crm\EntityBankDetail::getSingleInstance()->getRqFieldTitleMap();
485 $details = Bitrix\Crm\EntityBankDetail::getByOwners(
486 \CCrmOwnerType::Requisite, [$requisites[
'ID']]
488 $objDocument[
'BANKING_DETAILS'] =
'';
490 if($details[$requisites[
'ID']])
492 foreach ($details[$requisites[
'ID']] as $detail)
494 foreach ($titleMap as $key => $title)
496 if(isset($title[$detail[
'COUNTRY_ID']]))
499 $title[$detail[
'COUNTRY_ID']] .
': ' . $detail[$key];
505 $objDocument[
'BANKING_DETAILS'] = implode(self::COMMA, $tmpDetails);
508 case 'CREATED_BY_ID':
509 if ($objDocument[$usedField] > 1)
511 $dbUsers = \CUser::GetList(
514 [
'ID' => $objDocument[$usedField]],
523 $arUser = is_object($dbUsers)? $dbUsers->Fetch() :
null;
524 $objDocument[$usedField] =
525 is_array($arUser)? implode(
" ", [
535 $contactID = \Bitrix\Crm\Binding\ContactCompanyTable::getCompanyContactIDs($objDocument[
'ID']);
536 $contact = \CCrmContact::GetByID(
541 $objDocument[
'CONTACT_ID'] = implode(
" ", [
543 $contact[
'LAST_NAME']
549 $objDocument[
'COMPANY_ID'] = \CCrmCompany::GetByID(
550 $objDocument[
'COMPANY_ID']
554 $contactCompany =
"\Bitrix\Crm\Binding\ContactCompanyTable";
555 $companies = $contactCompany::getContactCompanyIDs(
559 $companiesTitle = [];
560 foreach ($companies as $company)
562 $companiesTitle[] = \CCrmCompany::GetByID(
566 $objDocument[
'COMPANY_IDS'] = implode(
', ', $companiesTitle);
568 case 'IS_RETURN_CUSTOMER':
569 case 'ASSIGNED_BY.ACTIVE':
572 $objDocument[$usedField] = $objDocument[$usedField] ===
'Y'
573 ? GetMessage(
'CRM_FIELDS_TYPE_B_VALUE_YES') : GetMessage(
'CRM_FIELDS_TYPE_B_VALUE_NO');
576 $honorifics = \CCrmStatus::GetStatus(
'HONORIFIC');
577 $objDocument[
'HONORIFIC'] = $honorifics[$objDocument[
'HONORIFIC']][
'NAME'];
579 case 'ORIGINATOR_ID':
583 $originator = \CCrmExternalSale::GetList([],[
584 [
'=ID' => $objDocument[
'ORIGINATOR_ID']]
587 if(isset($originator[0]))
589 $objDocument[
'ORIGINATOR_ID'] = $originator[0][
'NAME'];
593 $sources = \CCrmStatus::GetStatus(
'INDUSTRY');
594 $objDocument[
'INDUSTRY'] = $sources[$objDocument[
'INDUSTRY']][
'NAME'];
597 $sources = \CCrmStatus::GetStatus(
'SOURCE');
598 $objDocument[
'SOURCE_ID'] = $sources[$objDocument[
'SOURCE_ID']][
'NAME'];
601 $types = \CCrmStatus::GetStatus(
'COMPANY_TYPE');
602 $objDocument[
'COMPANY_TYPE'] = $types[$objDocument[
'COMPANY_TYPE']][
'NAME'];
605 $types = \CCrmStatus::GetStatus(
'CONTACT_TYPE');
606 $objDocument[
'TYPE_ID'] = $types[$objDocument[
'TYPE_ID']][
'NAME'];
609 $statuses = \CCrmStatus::GetStatus(
'STATUS');
610 $objDocument[
'STATUS_ID'] = $statuses[$objDocument[
'STATUS_ID']][
'NAME'];
613 $employees = \CCrmStatus::GetStatus(
'EMPLOYEES');
614 $objDocument[
'EMPLOYEES'] = $employees[$objDocument[
'EMPLOYEES']][
'NAME'];
630 $callId = Bitrix\Crm\Activity\Provider\Call::getId();
631 $emailId = Bitrix\Crm\Activity\Provider\Email::getId();
632 $olId = Bitrix\Crm\Activity\Provider\OpenLine::getId();
633 $webFormId = Bitrix\Crm\Activity\Provider\WebForm::getId();
635 $callDate = $emailDate = $olDate = $webFormDate =
null;
637 $ormRes = \Bitrix\Crm\ActivityTable::getList(
639 'select' => [
'END_TIME',
'PROVIDER_ID'],
642 '@PROVIDER_ID' => [$callId, $emailId, $olId, $webFormId],
643 '=BINDINGS.OWNER_TYPE_ID' => $typeId,
644 '@BINDINGS.OWNER_ID' => $ids,
646 'order' => [
'END_TIME' =>
'DESC']
650 while ($row = $ormRes->fetch())
652 if ($callDate ===
null)
654 if ($row[
'PROVIDER_ID'] === $callId)
656 $callDate = $row[
'END_TIME'];
659 if ($emailDate ===
null)
661 if ($row[
'PROVIDER_ID'] === $emailId)
663 $emailDate = $row[
'END_TIME'];
666 if ($olDate ===
null)
668 if ($row[
'PROVIDER_ID'] === $olId)
670 $olDate = $row[
'END_TIME'];
673 if ($webFormDate ===
null)
675 if ($row[
'PROVIDER_ID'] === $webFormId)
677 $webFormDate = $row[
'END_TIME'];
681 if ($callDate !==
null && $emailDate !==
null && $olDate !==
null && $webFormDate !==
null)
688 'COMMUNICATIONS.LAST_CALL_DATE' => (string)$callDate,
689 'COMMUNICATIONS.LAST_EMAIL_DATE' => (
string)$emailDate,
690 'COMMUNICATIONS.LAST_OL_DATE' => (string)$olDate,
691 'COMMUNICATIONS.LAST_FORM_DATE' => (
string)$webFormDate,