-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.php
More file actions
414 lines (360 loc) · 16.7 KB
/
Copy pathclass.php
File metadata and controls
414 lines (360 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<?php
class ProjectCheck
{
private Usual $call;
private Logger $logger;
public function __construct($call, Logger $logger)
{
$this->call = $call;
$this->logger = $logger;
}
/**
* @throws Exception
*/
private function getProjectTotalHours($projectId): int | float
{
$totalHours = 0;
$tasks = $this->getProjectTasks($projectId);
if (empty($tasks)) {
$this->logger->log("Не найдено ни одного таска по фильтру в проекте $projectId");
return 0;
}
$totalHours = $this->getTaskTimeForMultipleTasks($tasks);
return round($totalHours, 2);
}
/**
* @throws Exception
*/
private function getProjectTasks($projectId): array
{
$method = 'tasks.task.list';
$allTasks = [];
$start = 0;
$lastMonthFirstDay = (new DateTime('first day of last month'))
->setTime(0, 0, 0)
->format('Y-m-d\TH:i:sP');
do {
$paramsUnfinished = [
'filter' => [
'GROUP_ID' => $projectId,
'CLOSED_DATE' => null,
'!REAL_STATUS' => 5
],
'select' => ['ID', 'TITLE', 'TIME_ESTIMATE', 'TIME_SPENT_IN_LOGS', 'CLOSED_DATE'],
'start' => $start
];
$paramsFinished = [
'filter' => [
'GROUP_ID' => $projectId,
'>CLOSED_DATE' => $lastMonthFirstDay,
],
'select' => ['ID', 'TITLE', 'TIME_ESTIMATE', 'TIME_SPENT_IN_LOGS', 'CLOSED_DATE'],
'start' => $start
];
$responseUnfinished = $this->call->callBitrix24API($method, $paramsUnfinished);
$tasksUnfinished = $responseUnfinished['result']['tasks'] ?? [];
$responseFinished = $this->call->callBitrix24API($method, $paramsFinished);
$tasksFinished = $responseFinished['result']['tasks'] ?? [];
$tasks = array_merge($tasksUnfinished, $tasksFinished);
$allTasks = array_merge($allTasks, $tasks);
$start += 50;
} while ((!empty($tasksUnfinished) || !empty($tasksFinished)) && count($tasks) >= 50);
return $allTasks;
}
private function getTaskTimeForMultipleTasks(array $tasks, $lastMonth = false): float|int
{
$totalHours = 0;
foreach ($tasks as $task) {
try {
$taskId = $task['id'] ?? 0;
$method = 'task.elapseditem.getlist';
$params = [
'TASKID' => $taskId,
];
$response = $this->call->callBitrix24API($method, $params);
if (isset($response['result'])) {
$elapsedItems = $response['result'];
} else {
$this->logger->log("Response for task $taskId: " . json_encode($response));
$elapsedItems = [];
}
$totalHours += $this->calculateTimeForTask($elapsedItems, $lastMonth);
} catch (Exception $e) {
$this->logger->log("Error processing task " . $taskId . ": " . $e->getMessage());
return 0;
}
}
return $totalHours;
}
/**
* Использвуется в getTaskTimeForMultipleTasks, собирает затрач. время в задаче
* @param array $elapsedItems
* @param bool $lastMonth
* @return float|int
*/
private function calculateTimeForTask(array $elapsedItems, $lastMonth): float | int
{
$totalTime = 0;
$interval = $lastMonth ? 'last month' : 'this month';
if ($elapsedItems === [] || $elapsedItems === 0) {
$this->logger->log("No elapsed items found for interval: $interval");
return 0;
}
$firstDay = (new DateTime("first day of $interval"))->setTime(0, 0, 0);
$lastDay = (new DateTime("last day of $interval"))->setTime(23, 59, second: 59);
foreach ($elapsedItems as $item) {
try {
if ($item == null || $item == 0) {
$this->logger->log("Empty elapsed item");
continue;
}
if (empty($item['CREATED_DATE']) || !is_string($item['CREATED_DATE'])) {
$this->logger->log("Invalid CREATED_DATE: " . json_encode($item));
continue;
}
$createdDate = new DateTime($item['CREATED_DATE']);
if ($createdDate > $lastDay) {
break;
}
if ($createdDate >= $firstDay) {
$totalTime += (int)$item['SECONDS'];
}
} catch (Exception $e) {
$this->logger->log("Error processing elapsed item: " . $e->getMessage());
continue;
}
}
if ($totalTime === 0) {
$this->logger->log("No time spent in the task");
return 0;
}
return $totalTime / 3600;
}
/**
* @throws Exception
*/
private function sendNotification($userId, $message)
{
if (!$userId) {
return '';
}
$method = 'im.notify';
$params = [
'to' => $userId,
'message' => $message,
'type' => 'SYSTEM'
];
return $this->call->callBitrix24API($method, $params);
}
/**
* Получает компании с привязанными проектами
* @throws Exception
*/
public function getCompaniesWithProjectLink(): array
{
$method = 'crm.company.list';
$allCompanies = [];
$start = 0;
do {
$params = [
'filter' => [
'!UF_CRM_PROJECT_LINK' => [null, '', false],
],
'select' => ['ID', 'TITLE', 'UF_CRM_PROJECT_LINK', 'UF_CRM_DEFAULT_RATE', 'ASSIGNED_BY_ID', 'UF_CRM_HOURS_LIMIT', 'UF_CRM_NOTIFY_DATE', 'UF_CRM_EXTRANET_USER', 'UF_CRM_FRONTEND_RATE', 'UF_CRM_BACKEND_RATE', 'UF_CRM_DESIGNER_RATE', 'UF_CRM_PM_RATE', 'UF_CRM_CONTENT_MANAGER_RATE'],
'start' => $start
];
$response = $this->call->callBitrix24API($method, $params);
$companies = $response['result'] ?? [];
$allCompanies = array_merge($allCompanies, $companies);
$start += 50;
} while (!empty($companies) && count($companies) >= 50);
return $allCompanies;
}
public function checkProjectHours($company, $dealCreator): array
{
$companyId = $company['ID'];
$projectLink = $company['UF_CRM_PROJECT_LINK'] ?? '';
$projectId = $dealCreator->extractProjectId($projectLink);
$hoursLimit = $company['UF_CRM_HOURS_LIMIT'] ?? 0;
$notifyUser = $company['UF_CRM_EXTRANET_USER'] ?? 0;
$responsible = $company['ASSIGNED_BY_ID'] ?? 0;
if ($projectId <= 0) {
throw new InvalidArgumentException('Project ID must be greater than 0');
}
if ($hoursLimit <= 0) {
return [
'status' => 'no_limit',
'message' => "У компании $companyId не установлен лимит часов"
];
}
try {
$totalHours = $this->getProjectTotalHours($projectId);
if ($totalHours > $hoursLimit) {
// Проверяем, было ли уже уведомление в этом месяце
$notifyDate = $company['UF_CRM_NOTIFY_DATE'] ?? '';
$currentDate = new DateTime();
$wasNotifiedThisMonth = false;
if (!empty($notifyDate)) {
try {
$lastNotifyDate = new DateTime($notifyDate);
$wasNotifiedThisMonth = $lastNotifyDate->format('Y-m') === $currentDate->format('Y-m');
} catch (Exception $e) {
$this->logger->log("Ошибка при парсинге даты уведомления: " . $e->getMessage());
}
}
if ($wasNotifiedThisMonth) {
return [
'status' => 'already_notified',
'total_hours' => $totalHours,
'hours_limit' => $hoursLimit,
'excess_hours' => round($totalHours - $hoursLimit, 2),
'message' => "Лимит превышен, но уведомление в этом месяце уже отправлялось. Проект $projectId"
];
}
$projResult = $this->call->callBitrix24API('sonet_group.get', [
'FILTER' => [
'ID' => $projectId
]
]);
$projectName = $projResult['result'][0]['NAME'] ?? '';
$companyName = $company['TITLE'] ?? '';
$message = "🚨 ПРЕВЫШЕН ЛИМИТ ЧАСОВ! 🚨\n\n"
. "📊 Проект: #$projectId $projectName\n"
. "🏢 Компания: $companyName\n"
. "⏰ Текущее время: $totalHours часов\n"
. "⚠️ Лимит: $hoursLimit часов\n"
. "📈 Превышение: " . round($totalHours - $hoursLimit, 2) . " часов\n\n"
. "🔗 Ссылка на компанию: https://akvilon-marketing.bitrix24.ru/crm/company/details/$companyId/\n"
. "🔗 Ссылка на проект: https://akvilon-marketing.bitrix24.ru/workgroups/group/$projectId/";
// Отправляем уведомления
$notifyResult_1 = $this->sendNotification($notifyUser, $message); // Экстранет пользователь
$notifyResult_2 = $this->sendNotification(1, $message); // Администратор
$notifyResult_3 = $this->sendNotification($responsible, $message); // Ответственный
// Обновляем дату уведомления в компании
$updateResult = [];
if (!empty($notifyResult_1['result']) && !empty($notifyResult_2['result']) && !empty($notifyResult_3['result'])) {
$currentDate = new DateTime();
$updateResult = $this->call->callBitrix24API('crm.company.update', [
'id' => $companyId,
'fields' => [
'UF_CRM_NOTIFY_DATE' => $currentDate->format('d.m.Y')
],
]);
}
return [
'status' => 'limit_exceeded',
'total_hours' => $totalHours,
'hours_limit' => $hoursLimit,
'excess_hours' => round($totalHours - $hoursLimit, 2),
'project_id' => $projectId,
'project_name' => $projectName,
'company_id' => $companyId,
'company_name' => $companyName,
'notifications' => [
'extranet_user' => !empty($notifyResult_1['result']),
'admin' => !empty($notifyResult_2['result']),
'responsible' => !empty($notifyResult_3['result']),
'update' => !empty($updateResult['result']),
],
'message' => $message
];
} elseif ($totalHours === 0) {
return [
'status' => 'empty',
'message' => "Не найдено задач по текущему месяцу. Проект $projectId"
];
} else {
return [
'status' => 'ok',
'total_hours' => $totalHours,
'hours_limit' => $hoursLimit,
'remaining_hours' => round($hoursLimit - $totalHours, 2),
'message' => "Лимит не превышен. Текущее количество часов: $totalHours, лимит: $hoursLimit, осталось: " . round($hoursLimit - $totalHours, 2) . " часов. Проект $projectId"
];
}
} catch (Exception $e) {
return [
'status' => 'error',
'message' => $e->getMessage()
];
}
}
/**
* Создает задачу "Счет и акт" в конце месяца для указанной компании
*
* @param array $company Данные компании
* @param DealCreator $dealCreator Экземпляр DealCreator для извлечения ID проекта
* @return array Результат создания задачи
*/
public function checkFirstDateForTask($company, $dealCreator): array
{
try {
$companyId = $company['ID'] ?? 0;
$responsibleId = $company['ASSIGNED_BY_ID'] ?? 0;
$projectLink = $company['UF_CRM_PROJECT_LINK'] ?? '';
$projectId = $dealCreator->extractProjectId($projectLink);
$today = new DateTime();
$firstDayOfMonth = new DateTime('first day of this month');
if ($today->format('Y-m-d') !== $firstDayOfMonth->format('Y-m-d')) {
return [
'type' => 'taskCreate',
'status' => 'skip',
'message' => 'Задача создается только в первый день месяца (' .
$today->format('d.m.Y') . ')'
];
}
$tasks = $this->getProjectTasks($projectId);
$totalTime = $this->getTaskTimeForMultipleTasks($tasks,true);
if (empty($totalTime) || $totalTime === 0) {
return [
'type' => 'taskCreate',
'status' => 'empty tasks',
'message' => 'Задачи в прошлом месяце не имеют в себе затраченного времени',
];
}
if ($projectId <= 0) {
throw new Exception("У компании $companyId не указана ссылка на проект или не удалось извлечь ID проекта");
}
$deadline = clone $today;
$addedDays = 0;
while ($addedDays < 3) {
$deadline->modify('+1 day');
$weekday = $deadline->format('N');
if ($weekday < 6) {
$addedDays++;
}
}
$taskTitle = "Создать задачу Счета и акта для компании #$companyId " . $company['TITLE'] . " И проекта #$projectId";
$taskDescription = "Необходимо подготовить счет и акт для компании.\n"
. "Ссылка на компанию: https://akvilon-marketing.bitrix24.ru/crm/company/details/$companyId/\n".
"Ссылка на создание: https://akvilon-marketing.bitrix24.ru/workgroups/group/$projectId/tasks/task/edit/0/?SCOPE=tasks_grid&GROUP_ID=$projectId\n";
$createTaskResult = $this->call->callBitrix24API('tasks.task.add', [
'fields' => [
'TITLE' => $taskTitle,
'DESCRIPTION' => $taskDescription,
'RESPONSIBLE_ID' => $responsibleId,
'CREATED_BY' => 1,
'GROUP_ID' => $projectId,
'DEADLINE' => $deadline->format('Y-m-d'),
'UF_CRM_TASK' => ['C_' . $companyId],
],
]);
if (empty($createTaskResult['result'])) {
throw new Exception("Ошибка при создании задачи: " . json_encode($createTaskResult));
}
return [
'type' => 'task',
'status' => 'success',
'task' => $createTaskResult['result']['id'],
'message' => 'Задача "Счет и акт" успешно создана',
'deadline' => $deadline->format('Y-m-d'),
];
} catch (Exception $e) {
$this->logger->log("Ошибка в checkFirstDateForTask для компании $companyId: " . $e->getMessage());
return [
'status' => 'error',
'message' => $e->getMessage()
];
}
}
}