121 $config = $message->getConfiguration();
122 $authorId = $config->get(
'LETTER_CREATED_BY_ID');
124 $crmEntityId = $message->getRecipientCode();
125 $text = $message->replaceFields($config->get(
'COMMENT'),
'#', $crmEntityTypeId);
127 if (!$this->responsibleQueue || $this->responsibleQueue->getId() <> $message->getId())
129 $responsibleList = $config->get(
'ASSIGNED_BY');
130 if ($responsibleList)
132 $responsibleList = explode(
',', $responsibleList);
134 if (empty($responsibleList))
136 $responsibleList = [$authorId];
138 $this->responsibleQueue = (
new Queue(
'sender/rc', $message->getId()))
141 ->setValues($responsibleList);
143 if ($config->get(
'CHECK_WORK_TIME') ===
'Y')
145 $this->responsibleQueue->enableWorkTimeCheck();
149 $entityFields[
'TITLE'] = $message->replaceFields($config->get(
'TITLE'),
'#', $crmEntityTypeId);
150 $assignedId = $this->getAssignedWithCrmData((
int)$crmEntityTypeId, (
int)$crmEntityId);
151 $isAssignedById = ($config->get(
'LINK_WITH_RESPONSIBLE') ===
'Y') && $assignedId;
152 $entityFields[
'ASSIGNED_BY_ID'] = $isAssignedById ? $assignedId : $this->responsibleQueue->next();
154 $selector = (
new ActualEntitySelector())
155 ->setEntity($crmEntityTypeId, $crmEntityId)
157 $facility =
new EntityManageFacility($selector);
158 $facility->enableAutoGenRc();
160 if ($config->get(
'ALWAYS_ADD') ===
'Y' || $config->get(
'FROM_PREVIOUS') ===
'Y')
162 $facility->setRegisterMode(EntityManageFacility::REGISTER_MODE_ALWAYS_ADD);
165 switch ($message->getCode())
168 if (empty($entityFields[
'SOURCE_ID']))
170 $entityFields[
'SOURCE_ID'] =
'RC_GENERATOR';
172 $facility->registerLead($entityFields);
176 if (empty($entityFields[
'SOURCE_ID']))
178 $entityFields[
'SOURCE_ID'] =
'RC_GENERATOR';
181 $config->get(
'CATEGORY_ID'),
186 if ($config->get(
'FROM_PREVIOUS') ===
'Y')
188 $lastDeal = $this->
getLastDeal($facility, $config->get(
'DEAL_DAYS_AGO'));
190 if(!$lastDeal || !$lastOrder)
192 $this->responsibleQueue->previous();
196 $entityFields += $lastDeal;
200 $registeredId = $facility->registerDeal($entityFields);
202 if($registeredId && $config->get(
'FROM_PREVIOUS') ===
'Y')
205 $this->copyOrder($lastDeal[
'ID'], $registeredId);
213 if (!$facility->getRegisteredId())
215 $this->responsibleQueue->previous();
224 $commentId = Timeline\CommentEntry::create([
225 'TYPE_ID' => Timeline\TimelineType::COMMENT,
226 'AUTHOR_ID' => $authorId,
231 'ENTITY_TYPE_ID' => $facility->getRegisteredTypeId(),
232 'ENTITY_ID' => $facility->getRegisteredId(),
236 'ASSOCIATED_ENTITY_TYPE_ID' => $facility->getRegisteredTypeId(),
237 'ASSOCIATED_ENTITY_ID' => $facility->getRegisteredId()
240 return $commentId > 0;
258 if ($facility->canAddDeal() && !is_numeric($categoryId))
264 $categoryId = (int) $categoryId;
265 $categories = array_map(
268 return (
int) $category[
'ID'];
270 DealCategory::getAll(
true)
273 if (!in_array($categoryId, $categories))
275 $categoryId = $categories[0];
284 $categoryFilters = [];
285 if ($facility->getSelector()->getCompanyId())
287 $categoryFilters[] = [
288 '=COMPANY_ID' => $facility->getSelector()->getCompanyId()
291 if ($facility->getSelector()->getContactId())
293 $categoryFilters[] = [
294 '=CONTACT_ID' => $facility->getSelector()->getContactId()
297 foreach ($categoryFilters as $categoryFilter)
299 $categoryFilter[
'=STAGE_SEMANTIC_ID'] = [
300 PhaseSemantics::PROCESS,
301 PhaseSemantics::SUCCESS
303 $dealRow = DealTable::getRow([
304 'select' => [
'CATEGORY_ID'],
305 'filter' => $categoryFilter,
307 'order' => [
'DATE_CREATE' =>
'DESC']
314 $categoryId = $dealRow[
'CATEGORY_ID'];
320 protected function getLastDeal(EntityManageFacility $facility, $days)
324 if ($facility->getSelector()->getCompanyId())
327 '=COMPANY_ID' => $facility->getSelector()->getCompanyId()
330 if ($facility->getSelector()->getContactId())
333 '=CONTACT_ID' => $facility->getSelector()->getContactId()
336 foreach ($dealFilters as $dealFilter)
338 $dealFilter[
'=STAGE_SEMANTIC_ID'] = [
339 PhaseSemantics::SUCCESS
343 $dateCreate = (new \DateTime())->modify(
"-$days days");
348 $dealFilter[
'<DATE_CREATE'] = $endOfTheDay;
349 $dealFilter[
'>DATE_CREATE'] = $beginningOfTheDay;
351 $dealFilter[
'=HAS_PRODUCTS'] = 1;
353 $dealRow = DealTable::getRow([
354 'select' => [
'ID',
'CURRENCY_ID',
'RECEIVED_AMOUNT'],
355 'filter' => $dealFilter,
357 'order' => [
'DATE_CREATE' =>
'DESC']
412 $newOrder = $orderClassName::create($order->getSiteId(), $order->getUserId(), $order->getCurrency());
421 $basketItem = $newBasket->createItem($item->getField(
'MODULE'), $item->getProductId());