Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
activitymanager.php
1<?php
2
4
7use Bitrix\Crm\Integration\Calendar\ActivityHandler;
10
12{
13 public const STATUS_MEETING_NOT_HELD = 'meeting_not_held';
14 public const STATUS_CANCELED_BY_MANAGER = 'canceled_by_manager';
15 public const STATUS_CANCELED_BY_CLIENT = 'canceled_by_client';
16
17 private int $eventId;
18 private ?CrmDealLink $link;
19 private ?string $guestName;
20
26 public function __construct(int $eventId, ?CrmDealLink $link = null, ?string $guestName = null)
27 {
28 $this->eventId = $eventId;
29 $this->link = $link;
30 $this->guestName = $guestName;
31 }
32
41 string $activityName,
42 ?string $description,
43 DateTime $eventStart
44 ): ?\Bitrix\Main\Result
45 {
46 if (!$this->isAvailable())
47 {
48 return null;
49 }
50
51 $fields = [
52 'SUBJECT' => $activityName,
53 'DESCRIPTION' => $description,
54 'RESPONSIBLE_ID' => $this->link->getOwnerId(),
55 'CALENDAR_EVENT_ID' => $this->eventId,
56 'BINDINGS' => $this->getBindings(),
57 'SETTINGS' => $this->getSettings(),
58 'END_TIME' => $eventStart,
59 ];
60
61 return (new \Bitrix\Crm\Activity\Provider\CalendarSharing())
62 ->createActivity(\Bitrix\Crm\Activity\Provider\CalendarSharing::PROVIDER_TYPE_ID, $fields)
63 ;
64 }
65
71 public function completeSharedCrmActivity(?string $status): bool
72 {
73 if (!$this->isAvailable())
74 {
75 return false;
76 }
77
78 $activity = \CCrmActivity::GetByCalendarEventId($this->eventId, false);
79
80 if (!$activity)
81 {
82 return false;
83 }
84
85 $crmStatus = null;
86 switch ($status)
87 {
89 $crmStatus = ActivityHandler::SHARING_STATUS_MEETING_NOT_HELD;
90 break;
92 $crmStatus = ActivityHandler::SHARING_STATUS_CANCELED_BY_MANAGER;
93 break;
95 $crmStatus = ActivityHandler::SHARING_STATUS_CANCELED_BY_CLIENT;
96 break;
97 }
98
99 (new ActivityHandler($activity, $activity['OWNER_TYPE_ID'], $activity['OWNER_ID']))
100 ->completeWithStatus($crmStatus);
101
102 return true;
103 }
104
111 public function editActivityDeadline(DateTime $deadline): bool
112 {
113 if (!$this->isAvailable())
114 {
115 return false;
116 }
117
118 $activity = \CCrmActivity::GetByCalendarEventId($this->eventId, false);
119
120 if (!$activity)
121 {
122 return false;
123 }
124
125 (new ActivityHandler($activity, $activity['OWNER_TYPE_ID'], $activity['OWNER_ID']))
126 ->updateDeadline($deadline)
127 ;
128
129 return true;
130 }
131
135 private function isAvailable(): bool
136 {
137 return Loader::includeModule('crm') === true
138 && \Bitrix\Crm\Integration\Calendar\Helper::isSharingCrmAvaible()
139 ;
140 }
141
145 private function getBindings(): array
146 {
147 $result = [];
148 $entityTypeId = null;
149
150 switch ($this->link->getObjectType())
151 {
153 $entityTypeId = \CCrmOwnerType::Deal;
154 break;
155 }
156
157 $result[] = [
158 'OWNER_TYPE_ID' => $entityTypeId,
159 'OWNER_ID' => $this->link->getObjectId(),
160 ];
161
162 if ($this->link->getContactType() && $this->link->getContactId())
163 {
164 $result[] = [
165 'OWNER_TYPE_ID' => $this->link->getContactType(),
166 'OWNER_ID' => $this->link->getContactId(),
167 ];
168 }
169
170 return $result;
171 }
172
176 private function getSettings(): array
177 {
178 $result = [];
179
180 if ($this->link->getContactType() && $this->link->getContactId())
181 {
182 $result = [
183 'CONTACT_TYPE_ID' => $this->link->getContactType(),
184 'CONTACT_ID' => $this->link->getContactId(),
185 ];
186 }
187 elseif ($this->guestName)
188 {
189 $result = [
190 'GUEST_NAME' => $this->guestName,
191 ];
192 }
193
194 $result['LINK_ID'] = $this->link->getId();
195
196 return $result;
197 }
198}
createCalendarSharingActivity(string $activityName, ?string $description, DateTime $eventStart)
__construct(int $eventId, ?CrmDealLink $link=null, ?string $guestName=null)