11use Bitrix\Crm\Activity\BindingSelector;
12use Bitrix\Crm\Integrity\ActualEntitySelector;
31 private const TIME_LINE_COUNT_LIMIT = 500;
40 if (!$eventData[
'SEND_RESULT'])
45 static $isModuleIncluded =
null;
46 if ($isModuleIncluded ===
null)
48 $isModuleIncluded = Loader::includeModule(
'crm');
51 if (!$isModuleIncluded)
56 if ($letter->getMessage()->isReturnCustomer())
61 $recipient = $eventData[
'RECIPIENT'];
62 $fields = $eventData[
'RECIPIENT'][
'FIELDS'];
64 $entityId = $fields[
'CRM_ENTITY_ID'] ?? $recipient[
'CONTACT_ID'];
71 TimeLineQueueTable::add([
72 'RECIPIENT_ID' => $recipient[
'ID'],
73 'POSTING_ID' => $letter->getId(),
74 'FIELDS' => Json::encode($fields),
75 'ENTITY_ID' => $entityId,
76 'CONTACT_TYPE_ID' => $recipient[
'CONTACT_TYPE_ID'],
77 'CONTACT_CODE' => $recipient[
'CONTACT_CODE'],
88 static $isModuleIncluded =
null;
90 if ($isModuleIncluded ===
null)
92 $isModuleIncluded = Loader::includeModule(
'crm');
95 if (!$isModuleIncluded)
100 if ($letter->getMessage()->isReturnCustomer())
106 foreach($eventDataArray as $eventData)
108 $recipient = $eventData[
'RECIPIENT'];
109 $fields = $eventData[
'RECIPIENT'][
'FIELDS'];
111 $entityId = $fields[
'CRM_ENTITY_ID'] ?? $recipient[
'CONTACT_ID'];
119 'RECIPIENT_ID' => $recipient[
'ID'],
120 'POSTING_ID' => $letter->getId(),
121 'FIELDS' => Json::encode($fields),
122 'ENTITY_ID' => $entityId,
123 'CONTACT_TYPE_ID' => $recipient[
'CONTACT_TYPE_ID'],
124 'CONTACT_CODE' => $recipient[
'CONTACT_CODE'],
130 TimeLineQueueTable::addMulti($dataToInsert,
true);
142 static $isModuleIncluded =
null;
143 if ($isModuleIncluded ===
null)
145 $isModuleIncluded = Loader::includeModule(
'crm');
148 if (!$isModuleIncluded)
153 $entityTypeId = $entityId =
null;
157 $letter = Entity\Letter::createInstanceById($letterId);
161 $queuedRows = TimeLineQueueTable::getList([
164 '=POSTING_ID' => $letterId
166 'limit' => self::TIME_LINE_COUNT_LIMIT
169 if(empty($queuedRows))
174 foreach ($queuedRows as $row)
176 $idsToDelete[] = $row[
'ID'];
182 $fields = Json::decode($row[
'FIELDS']);
183 if (isset($fields[
'CRM_ENTITY_TYPE_ID']) && $fields[
'CRM_ENTITY_TYPE_ID'])
185 $entityTypeId = $fields[
'CRM_ENTITY_TYPE_ID'];
187 if (isset($fields[
'CRM_ENTITY_ID']) && $fields[
'CRM_ENTITY_ID'])
189 $entityId = $fields[
'CRM_ENTITY_ID'];
192 if (!$entityTypeId || !$entityId)
195 $row[
'CONTACT_TYPE_ID'],
210 if (!$selector->search()->hasEntities())
216 'ID' => $row[
'RECIPIENT_ID'],
217 'CONTACT_TYPE_ID' => $row[
'CONTACT_TYPE_ID'],
218 'CONTACT_CODE' => $row[
'CONTACT_CODE'],
221 $batchData[] = static::buildTimeLineEvent($selector, $letter, $recipient);
224 if (!empty($batchData))
226 Timeline\RecipientEntry::createMulti($batchData);
236 return TimeLineJob::getAgentName($letterId);
241 $parameters = static::buildTimeLineEvent($selector, $letter, $recipient);
243 if(!empty($parameters))
245 Timeline\RecipientEntry::create($parameters);
252 $createdBy = $letter->get(
'CREATED_BY');
260 $activityBindings = BindingSelector::findBindings($selector);
261 foreach ($activityBindings as $binding)
263 $binding[
'ENTITY_ID'] = $binding[
'OWNER_ID'];
264 $binding[
'ENTITY_TYPE_ID'] = $binding[
'OWNER_TYPE_ID'];
266 'ENTITY_TYPE_ID' => $binding[
'OWNER_TYPE_ID'],
267 'ENTITY_ID' => $binding[
'OWNER_ID'],
272 'ENTITY_TYPE_ID' => $selector->getPrimaryTypeId(),
273 'ENTITY_ID' => $selector->getPrimaryId(),
274 'TYPE_CATEGORY_ID' => $letter->getMessage()->getCode(),
275 'AUTHOR_ID' => $createdBy,
277 'letterId' => $letter->getId(),
280 'id' => $recipient[
'ID'],
281 'typeId' => $recipient[
'CONTACT_TYPE_ID'],
282 'code' => $recipient[
'CONTACT_ID'],
285 'BINDINGS' => $bindings
289 protected static function getEntitySelector()
292 static $selector =
null;
295 $selector =
new ActualEntitySelector();
307 return self::getEntitySelector()->setEntity($entityTypeId, $entityId);
312 $selector = self::getEntitySelector();
314 switch ($recipientTypeId)
316 case Recipient\Type::EMAIL:
317 $selector->appendEmailCriterion($recipientCode);
320 case Recipient\Type::PHONE:
321 $selector->appendPhoneCriterion($recipientCode);
341 $lockName = self::getTimelineQueueLock((
string)$letterId);
343 return $connection->lock($lockName);
356 $lockName = self::getTimelineQueueLock((
string)$letterId);
358 return $connection->unlock($lockName);
368 private static function getTimelineQueueLock(
string $letterId): string
370 return "time_line_queue_$letterId";
static deleteList(array $filter)
static loadMessages($file)
static buildTimeLineEvent(ActualEntitySelector $selector, Entity\Base $letter, $recipient)
static addTimeLineEvent(ActualEntitySelector $selector, Entity\Base $letter, $recipient)
static lockTimelineQueue($letterId)
static onAfterPostingSendRecipient(array $eventData, Entity\Letter $letter)
static handleTimeLineEvents($letterId)
static onAfterPostingSendRecipientMultiple(array $eventDataArray, Entity\Letter $letter)
static unlockTimelineQueue($letterId)
static getEntitySelectorById($entityTypeId, $entityId)
static getEntitySelectorByRecipient($recipientTypeId, $recipientCode)