Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
lead.php
1<?php
10
11use Bitrix\Crm\LeadTable;
12use Bitrix\Crm\PhaseSemantics;
13use Bitrix\Crm\Service\Container;
18use Bitrix\Main\Page\Asset;
21use Bitrix\Sender\Connector\BaseFilter as ConnectorBaseFilter;
23
24Loc::loadMessages(__FILE__);
25
30class Lead extends ConnectorBaseFilter implements Connector\IncrementallyConnector
31{
37 public function getName()
38 {
39 return Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_NAME');
40 }
41
47 public function getCode()
48 {
49 return "crm_lead";
50 }
51
57 public function getQuery($selectList = [])
58 {
60 self::getUiFilterFields(),
61 $this->getFieldValues()
62 );
63 $runtime = Helper::getRuntimeByEntity(null);
64
65 $sqlHelper = \Bitrix\Main\Application::getConnection()->getSqlHelper();
66
67 $nameExprField = new Entity\ExpressionField(
68 $sqlHelper->quote('NAME'),
69 'CASE
70 WHEN %2$s=\'Y\' AND %3$s>0 THEN %4$s
71 WHEN %2$s=\'Y\' AND %5$s>0 THEN %6$s
72 ELSE %1$s
73 END',
74 [
75 'NAME', 'IS_RETURN_CUSTOMER',
76 'CONTACT_ID', 'CONTACT.NAME',
77 'COMPANY_ID', 'COMPANY.TITLE'
78 ]
79 );
80
81 $query = new Entity\Query(LeadTable::getEntity());
82 $query->setFilter($filter);
83 $query->setSelect(array_merge($selectList, [
84 $nameExprField,
85 'CRM_ENTITY_ID' => 'ID',
86 'CRM_ENTITY_TYPE_ID',
87 'CRM_ENTITY_TYPE',
88 'CRM_CONTACT_ID' => 'CONTACT_ID',
89 'CRM_COMPANY_ID' => 'COMPANY_ID',
90 ]));
91
92 $query->registerRuntimeField(new Entity\ExpressionField('CRM_ENTITY_TYPE', '\''.\CCrmOwnerType::LeadName.'\''));
93
94 $query->registerRuntimeField(Helper::createExpressionMultiField(
95 \CCrmOwnerType::LeadName,
96 'EMAIL'
97 ));
98 $query->registerRuntimeField(Helper::createExpressionMultiField(
99 \CCrmOwnerType::LeadName,
100 'PHONE'
101 ));
102 $query->registerRuntimeField(new Entity\ExpressionField('CRM_ENTITY_TYPE_ID', \CCrmOwnerType::Lead));
103 $query->registerRuntimeField(null, new Entity\ReferenceField(
104 'CONTACT',
105 "\\Bitrix\\Crm\\ContactTable",
106 ['=this.CONTACT_ID' => 'ref.ID'],
107 ['join_type' => 'LEFT']
108 ));
109 $query->registerRuntimeField(null, new Entity\ReferenceField(
110 'COMPANY',
111 "\\Bitrix\\Crm\\CompanyTable",
112 ['=this.COMPANY_ID' => 'ref.ID'],
113 ['join_type' => 'LEFT']
114 ));
115 foreach ($runtime as $item)
116 {
117 $item = new Entity\ExpressionField(
118 $item['name'],
119 $item['expression'],
120 $item['buildFrom']
121 );
122 $query->registerRuntimeField($item);
123 }
124
125 //echo "<pre>" . $query->getQuery(); die();
126
127 return $query;
128 }
129
135 protected function getDataCountByType()
136 {
137 if (!$this->hasFieldValues())
138 {
139 return array();
140 }
141
142 return QueryCount::getCount($this->getQuery(), $this->getDataTypeId());
143 }
144
150 public function getData()
151 {
152 if (!$this->hasFieldValues())
153 {
154 return array();
155 }
156
157 $query = $this->getQuery();
158 if ($this->getResultView()->hasNav())
159 {
160 $query->setOffset($this->getResultView()->getNav()->getOffset());
161 $query->setLimit($this->getResultView()->getNav()->getLimit());
162 }
163
164 return QueryData::getData($query, $this->getDataTypeId());
165 }
166
172 public static function getPersonalizeList()
173 {
174 return Loader::includeModule('crm') ? array_merge(
176 Helper::buildPersonalizeList(\CCrmOwnerType::LeadName)
178 }
179
188 public static function getUiFilterFields(bool $checkAccessRights = true): array
189 {
190 $list = [
191 [
192 'id' => 'EMAIL',
193 'type' => 'string',
194 'sender_segment_filter' => '%EMAIL',
195 'sender_internal' => true
196 ],
197 [
198 'id' => 'PHONE',
199 'type' => 'string',
200 'sender_segment_filter' => '%PHONE',
201 'sender_internal' => true
202 ],
203 [
204 'id' => 'NAME',
205 'type' => 'string',
206 'sender_segment_filter' => '%NAME',
207 'sender_internal' => true
208 ],
209 ];
210
211 $list[] = array(
212 "id" => "DATE_CREATE",
213 "name" => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_FIELD_DATE_CREATE'),
214 "type" => "date",
215 "include" => [
216 AdditionalDateType::CUSTOM_DATE,
217 AdditionalDateType::PREV_DAY,
218 AdditionalDateType::NEXT_DAY,
219 AdditionalDateType::MORE_THAN_DAYS_AGO,
220 AdditionalDateType::AFTER_DAYS,
221 ],
222 "allow_years_switcher" => true,
223 "default" => true
224 );
225
226 $list[] = array(
227 "id" => "STATUS_ID",
228 "name" => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_FIELD_STATUS_ID'),
229 "type" => "list",
230 "items" => \CCrmStatus::GetStatusList('STATUS'),
231 "default" => true,
232 'params' => array('multiple' => 'Y'),
233 );
234
235 $list[] = array(
236 "id" => "SOURCE_ID",
237 "name" => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_FIELD_SOURCE_ID'),
238 "type" => "list",
239 "items" => \CCrmStatus::GetStatusList('SOURCE'),
240 "default" => true,
241 'params' => array('multiple' => 'Y'),
242 );
243
244 $list[] = array(
245 'id' => 'COMMUNICATION_TYPE',
246 "name" => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_FIELD_COMMUNICATION_TYPE'),
247 'params' => array('multiple' => 'Y'),
248 'default' => true,
249 'type' => 'list',
250 'items' => \CCrmFieldMulti::PrepareListItems(array(
251 \CCrmFieldMulti::PHONE,
252 \CCrmFieldMulti::EMAIL,
253 \CCrmFieldMulti::IM
254 )),
255 'filter_callback' => ['\Bitrix\Sender\Integration\Crm\Connectors\Helper', 'getCommunicationTypeFilter']
256 );
257
258 $list[] = PhaseSemantics::getListFilterInfo(
259 \CCrmOwnerType::Lead,
260 array(
261 'id' => 'STATUS_SEMANTIC_ID',
262 "name" => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_FIELD_STATUS_SEMANTIC_ID'),
263 'default' => true,
264 'params' => array('multiple' => 'Y'),
265 ),
266 true
267 );
268
269 $list[] = array(
270 'id' => 'PRODUCT_ROW.PRODUCT_ID',
271 "name" => Loc::getMessage("SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_FIELD_PRODUCT_ID"),
272 'default' => true,
273 'type' => 'dest_selector',
274 'partial' => true,
275 'params' => array(
276 'multiple' => 'Y',
277 'apiVersion' => 3,
278 'context' => 'CRM_LEAD_FILTER_PRODUCT_ID',
279 'contextCode' => 'CRM',
280 'useClientDatabase' => 'N',
281 'enableAll' => 'N',
282 'enableDepartments' => 'N',
283 'enableUsers' => 'N',
284 'enableSonetgroups' => 'N',
285 'allowEmailInvitation' => 'N',
286 'allowSearchEmailUsers' => 'N',
287 'departmentSelectDisable' => 'Y',
288 'addTabCrmProducts' => 'Y',
289 'enableCrm' => 'Y',
290 'enableCrmProducts' => 'Y',
291 'convertJson' => 'Y'
292 )
293 );
294
295 $list[] = array(
296 "id" => "STATUS_CONVERTED",
297 "name" => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_FIELD_STATUS_CONVERTED'),
298 'type' => 'checkbox',
299 'sender_segment_filter' => array(
300 'N' => array('=STATUS_SEMANTIC_ID', PhaseSemantics::PROCESS),
301 'Y' => array('!=STATUS_SEMANTIC_ID', PhaseSemantics::PROCESS),
302 ),
303 "default" => false,
304 );
305
306 $list[] = array(
307 "id" => "ASSIGNED_BY_ID",
308 "name" => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_FIELD_ASSIGNED_BY_ID'),
309 'type' => 'dest_selector',
310 'params' => array(
311 'context' => 'SENDER_LEAD_FILTER_ASSIGNED_BY_ID',
312 'multiple' => 'Y',
313 'contextCode' => 'U',
314 'enableAll' => 'N',
315 'enableSonetgroups' => 'N',
316 'allowEmailInvitation' => 'N',
317 'allowSearchEmailUsers' => 'N',
318 'departmentSelectDisable' => 'Y',
319 'isNumeric' => 'Y',
320 'prefix' => 'U'
321 ),
322 "default" => false,
323 );
324
325 $list[] = array(
326 "id" => "POST",
327 'type' => 'string',
328 "name" => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_FIELD_POST'),
329 'params' => array('multiple' => 'Y'),
330 "default" => false
331 );
332
333 $list[] = array(
334 "id" => "BIRTHDATE",
335 "name" => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_FIELD_BIRTHDATE'),
336 'type' => 'date',
337 "include" => [
338 AdditionalDateType::CUSTOM_DATE,
339 AdditionalDateType::PREV_DAY,
340 AdditionalDateType::NEXT_DAY,
341 AdditionalDateType::MORE_THAN_DAYS_AGO,
342 AdditionalDateType::AFTER_DAYS,
343 ],
344 "allow_years_switcher" => true,
345 "default" => false,
346 );
347
348 $list[] = array(
349 'id' => 'HONORIFIC',
350 'name' => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_FIELD_HONORIFIC'),
351 'params' => array('multiple' => 'Y'),
352 'default' => false,
353 'type' => 'list',
354 'items' => \CCrmStatus::GetStatusList('HONORIFIC'),
355 );
356
357 return array_merge($list, Helper::getFilterUserFields(\CCrmOwnerType::Lead, $checkAccessRights));
358 }
359
365 public static function getUiFilterPresets()
366 {
367 return array(
368 'crm_lead_all' => array(
369 'name' => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_PRESET_ALL'),
370 'sender_segment_name' => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_PRESET_SEGMENT_ALL'),
371 'fields' => array(
372 self::FIELD_FOR_PRESET_ALL => 'Y',
373 )
374 ),
375 'crm_lead_converted' => array(
376 'name' => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_PRESET_CONV'),
377 'sender_segment_name' => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_PRESET_SEGMENT_CONV'),
378 'fields' => array(
379 'STATUS_SEMANTIC_ID' => array(PhaseSemantics::SUCCESS),
380 )
381 ),
382 'crm_lead_in_work' => array(
383 'name' => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_PRESET_INW'),
384 'sender_segment_name' => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_PRESET_SEGMENT_INW'),
385 'fields' => array(
386 'STATUS_SEMANTIC_ID' => array(PhaseSemantics::PROCESS),
387 )
388 ),
389 'crm_lead_birthday' => array(
390 'name' => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_PRESET_BIRTH'),
391 'sender_segment_name' => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_PRESET_SEGMENT_BIRTH'),
392 'sender_segment_business_case' => true,
393 'fields' => array(
394 'BIRTHDATE_datesel' => 'NEXT_DAY',
395 'BIRTHDATE_days' => '5',
396 'BIRTHDATE_allow_year' => '0',
397 )
398 ),
399 );
400 }
401
402
403
409 public function isResultViewable()
410 {
411 return true;
412 }
413
414 protected function onInitResultView()
415 {
416 $this->getResultView()
417 ->setCallback(
418 ResultView::ColumnModifier,
419 function (array &$columns)
420 {
421 Asset::getInstance()->addJs('/bitrix/js/crm/common.js');
422 $columns = array_merge(
423 [[
424 'id' => 'CRM_LEAD',
425 'name' => Loc::getMessage('SENDER_INTEGRATION_CRM_CONNECTOR_LEAD_FIELD_LEAD'),
426 'default' => true
427 ]],
428 $columns
429 );
430
431 return $columns;
432 }
433 )
434 ->setCallback(
435 ResultView::Draw,
436 function(array &$row)
437 {
438 (new Helper())->onResultViewDraw($row);
439 }
440 );
441 }
442
443 public function getUiFilterId()
444 {
445 $code = str_replace('_', '', $this->getCode());
446 return $this->getId() . '_--filter--'.$code.'--';
447 }
448
453 public function getStatFields()
454 {
455 return ['PRODUCT_ROW.PRODUCT_ID'];
456 }
457
465 public function getLimitedQueries(int $offset, int $limit, string $excludeType = null): array
466 {
467 $query = $this->getQuery();
468
469 $query->whereBetween('ID', $offset, $limit);
470 return [
471 $query
472 ];
473 }
474
478 public function getEntityLimitInfo(): array
479 {
480 $lastLead = \CCrmLead::GetListEx(
481 ['ID' => 'DESC'],
482 ['CHECK_PERMISSIONS' => 'N'],
483 false,
484 ['nTopCount' => '1'],
485 ['ID']
486 )->Fetch();
487 $lastLeadId = $lastLead['ID'] ?? 0;
488
489 return [
490 'lastContactId' => 0,
491 'lastCompanyId' => 0,
492 'lastId' => $lastLeadId,
493 ];
494 }
495
502 public function getLimitedData(int $offset, int $limit): \Bitrix\Main\DB\Result
503 {
504 $query = $this->getLimitedQueries($offset, $limit)[0];
505 return QueryData::getData($query, $this->getDataTypeId());
506 }
507
508 public function getContactDataLead(int $leadId): ?array
509 {
510 $query = LeadTable::query()
511 ->setSelect(['NAME', 'LAST_NAME', 'POST', 'HONORIFIC', 'BIRTHDATE', 'HAS_EMAIL', 'HAS_IMOL', 'HAS_PHONE'])
512 ->where('ID', $leadId);
513
514 // $leadDb = LeadTable::getById($leadId);
515 if ($lead = $query->fetch())
516 {
517 $contactsFields = [];
518
519 if (
520 ($lead['HAS_EMAIL'] === 'Y')
521 || ($lead['HAS_IMOL'] === 'Y')
522 || ($lead['HAS_PHONE'] === 'Y')
523 )
524 {
525 $leadMultiFields = Container::getInstance()
526 ->getMultifieldStorage()
527 ->get(new \Bitrix\Crm\ItemIdentifier(\CCrmOwnerType::Lead, $leadId));
528 $contactsFields = $leadMultiFields->toArray();
529 }
530
531 return [
532 'NAME' => $lead['NAME'],
533 'LAST_NAME' => $lead['LAST_NAME'],
534 'POST' => $lead['POST'],
535 'HONORIFIC' => $lead['HONORIFIC'],
536 'BIRTHDATE' => $lead['BIRTHDATE'],
537 'FM' => $contactsFields,
538 ];
539 }
540
541 return null;
542 }
543}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getFilterByFields(array $fields=array(), array $values=array(), $entityTypeName='')
Definition helper.php:548
static createExpressionMultiField($entityName, $multiFieldTypeId)
Definition helper.php:45
static getRuntimeByEntity($entityTypeName='')
Definition helper.php:399
static getFilterUserFields(int $entityTypeId, bool $checkAccessRights=true)
Definition helper.php:200
getLimitedQueries(int $offset, int $limit, string $excludeType=null)
Definition lead.php:465
static getUiFilterFields(bool $checkAccessRights=true)
Definition lead.php:188
getLimitedData(int $offset, int $limit)
Definition lead.php:502
static getCount(Entity\Query $query, $dataTypeId=null)
static getData(Entity\Query $query, $dataTypeId=null)
setCallback($name, $callable)