PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.2.1
Fluent Support – Helpdesk & Customer Support Ticket System v2.2.1
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / app / Http / Controllers / ReportingController.php

ReportingController.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.2.1, at app/Http/Controllers/ReportingController.php

461 lines 15.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\App\Http\Controllers;
4
5 use FluentSupport\App\Modules\Reporting\Reporting;
6 use FluentSupport\App\Modules\StatModule;
7 use FluentSupport\App\Services\Helper;
8 use FluentSupport\Framework\Http\Request\Request;
9 use FluentSupport\App\Models\Ticket;
10 use FluentSupport\App\Models\Conversation;
11 use FluentSupport\App\Models\TagPivot;
12
13 /**
14 * ReportingController class for REST API
15 * This class is responsible for getting data for all request related to report
16 * @package FluentSupport\App\Http\Controllers
17 *
18 * @version 1.0.0
19 */
20 class ReportingController extends Controller
21 {
22 private static function getSanitizedDateRange(Request $request)
23 {
24 $dateRange = $request->get('date_range', []);
25
26 if (is_array($dateRange) && count($dateRange) >= 2) {
27 return [
28 sanitize_text_field($dateRange[0] ?? ''),
29 sanitize_text_field($dateRange[1] ?? '')
30 ];
31 }
32
33 if (is_string($dateRange)) {
34 $parts = array_map('trim', explode(',', $dateRange));
35 return [
36 sanitize_text_field($parts[0] ?? ''),
37 sanitize_text_field($parts[1] ?? '')
38 ];
39 }
40
41 return ['', ''];
42 }
43
44 private static function getAgentIdsForGroup(Request $request)
45 {
46 $groupId = $request->getSafe('agent_group_id', 'intval');
47 if (!$groupId) {
48 return null;
49 }
50
51 return TagPivot::where('source_type', 'agent_group')
52 ->where('tag_id', $groupId)
53 ->pluck('source_id')
54 ->toArray();
55 }
56
57 /**
58 * getOverallReports method will return the overall statistics of all ticket by ticket statuses
59 * The response will have an array with ticket number by ticket status
60 * @param Request $request
61 * @return array
62 */
63 public function getOverallReports(Request $request)
64 {
65 return [
66 'overall_reports' => StatModule::getOverAllStats()
67 ];
68 }
69
70 public function getActiveTicketsByProduct()
71 {
72 return [
73 'stats' => StatModule::getActiveTicketsByProductStats()
74 ];
75 }
76
77 /**
78 * getTicketsChart method will generate statistics for all tickets within a date range and return ticket number by date
79 * @param Request $request
80 * @param Reporting $reporting
81 * @return array
82 */
83 public function getTicketsChart(Request $request, Reporting $reporting)
84 {
85 list($from, $to) = self::getSanitizedDateRange($request);
86
87 $filter = [
88 'agent_id' => $request->getSafe('agent_id', 'intval') ?: null,
89 'product_id' => $request->getSafe('product_id', 'intval') ?: null,
90 'mailbox_id' => $request->getSafe('mailbox_id', 'intval') ?: null,
91 ];
92
93 $agentIds = self::getAgentIdsForGroup($request);
94 if ($agentIds !== null) {
95 $filter['agent_ids'] = $agentIds;
96 }
97
98 $stats = $reporting->getTicketsGrowth($from, $to, $filter);
99
100 return [
101 'stats' => $stats
102 ];
103 }
104
105 /**
106 * getResolveChart method will generate statistics for closed tickets within a date range and return ticket number by date
107 * @param Request $request
108 * @param Reporting $reporting
109 * @return array
110 */
111 public function getResolveChart(Request $request, Reporting $reporting): array
112 {
113 $type = $request->getSafe('type', 'sanitize_text_field');
114 list($from, $to) = self::getSanitizedDateRange($request);
115
116 $filter = [
117 'agent_id' => $request->getSafe('agent_id', 'intval') ?: null,
118 'product_id' => $request->getSafe('product_id', 'intval') ?: null,
119 'mailbox_id' => $request->getSafe('mailbox_id', 'intval') ?: null,
120 ];
121
122 $agentIds = self::getAgentIdsForGroup($request);
123 if ($agentIds !== null) {
124 $filter['agent_ids'] = $agentIds;
125 }
126
127 $stats = $reporting->getTicketResolveGrowth($from, $to, $filter,$type);
128
129 return [
130 'stats' => $stats
131 ];
132 }
133
134 /**
135 * getResponseChart method will generate response statistics for ticket by date range
136 * @param Request $request
137 * @param Reporting $reporting
138 * @return array
139 */
140 public function getResponseChart(Request $request, Reporting $reporting)
141 {
142 list($from, $to) = self::getSanitizedDateRange($request);
143 $filter = [];
144
145 if ($person_id = $request->getSafe('agent_id', 'intval')) {
146 $filter['person_id'] = $person_id;
147 }
148
149 $agentIds = self::getAgentIdsForGroup($request);
150 if ($agentIds !== null) {
151 $filter['person_ids'] = $agentIds;
152 }
153
154 $stats = $reporting->getResponseGrowth($from, $to, $filter);
155
156 return [
157 'stats' => $stats
158 ];
159 }
160
161 /**
162 * getAgentsSummary method will generate summary for agent
163 * This method will count closed tickets, open tickets, responses/interactions with ticket by agent within a date range
164 * @param Request $request
165 * @param Reporting $reporting
166 * @return array
167 */
168 public function getAgentsSummary(Request $request, Reporting $reporting)
169 {
170 return [
171 'summary' => $reporting->agentSummary($request->getSafe('from', 'sanitize_text_field'), $request->getSafe('to', 'sanitize_text_field'))
172 ];
173 }
174
175 /**
176 * getAgentOverallReports method will return the overall statistics report for logged-in agent
177 * @param Request $request
178 * @return array
179 */
180 /**
181 * getAgentGroupsSummary method will generate summary aggregated by agent group
182 * @param Request $request
183 * @param Reporting $reporting
184 * @return array
185 */
186 public function getAgentGroupsSummary(Request $request, Reporting $reporting)
187 {
188 return [
189 'summary' => $reporting->agentGroupSummary(
190 $request->getSafe('from', 'sanitize_text_field'),
191 $request->getSafe('to', 'sanitize_text_field')
192 )
193 ];
194 }
195
196 public function getAgentOverallReports(Request $request): array
197 {
198 $agent = Helper::getAgentByUserId(get_current_user_id());
199
200 return [
201 'overall_reports' => StatModule::getAgentOverallStats($agent->id),
202 'today_reports' => StatModule::getTodayStats($agent->id)
203 ];
204 }
205
206 /**
207 * getResponseGrowthChart method will generate response statistics for ticket by date range for product or mailbox
208 * @param Request $request
209 * @param Reporting $reporting
210 * @return array
211 */
212 public function getResponseGrowthChart(Request $request, Reporting $reporting): array
213 {
214 $type = $request->getSafe('type', 'sanitize_text_field');
215 list($from, $to) = self::getSanitizedDateRange($request);
216
217 $filter = [
218 'product_id' => $request->getSafe('product_id', 'intval') ?: null,
219 'mailbox_id' => $request->getSafe('mailbox_id', 'intval') ?: null,
220 ];
221
222 $stats = $reporting->getResponseGrowthChart($from, $to, $filter,$type);
223
224 return [
225 'stats' => $stats
226 ];
227 }
228
229 /**
230 * getProductsSummary method will generate summary for product
231 * This method will count closed tickets, open tickets, responses, interactions with ticket by agent within a date range
232 * @param Request $request
233 * @param Reporting $reporting
234 * @return array
235 */
236 public function getProductsSummary(Request $request, Reporting $reporting): array
237 {
238 return [
239 'summary' => $reporting->getSummary('product',$request->getSafe('from', 'sanitize_text_field'), $request->getSafe('to', 'sanitize_text_field'))
240 ];
241
242 }
243
244 /**
245 * getMailBoxesSummary method will generate summary for mailbox
246 * This method will count closed tickets, open tickets, responses, interactions with ticket by agent within a date range
247 * @param Request $request
248 * @param Reporting $reporting
249 * @return array
250 */
251 public function getMailBoxesSummary(Request $request, Reporting $reporting): array
252 {
253 return [
254 'summary' => $reporting->getSummary('mailbox',$request->getSafe('from', 'sanitize_text_field'), $request->getSafe('to', 'sanitize_text_field'))
255 ];
256 }
257
258 /**
259 * getAgentResolveChart method will generate ticket data for resolved ticket
260 * @param Request $request
261 * @param Reporting $reporting
262 * @return array
263 */
264 public function getAgentResolveChart(Request $request, Reporting $reporting)
265 {
266 //Get logged in agent information
267 $agent = Helper::getAgentByUserId(get_current_user_id());
268 list($from, $to) = self::getSanitizedDateRange($request);
269
270 return [
271 'stats' => $reporting->getTicketResolveGrowth($from, $to, ['agent_id' => $agent->id])
272 ];
273 }
274
275 /**
276 * getAgentResponseChart method will generate the statistics of response by agent in tickets within date range
277 * @param Request $request
278 * @param Reporting $reporting
279 * @return array
280 */
281 public function getAgentResponseChart(Request $request, Reporting $reporting)
282 {
283 $agent = Helper::getAgentByUserId(get_current_user_id());
284 list($from, $to) = self::getSanitizedDateRange($request);
285
286 return [
287 'stats' => $reporting->getResponseGrowth($from, $to, ['person_id' => $agent->id])
288 ];
289 }
290
291 /**
292 * getPersonalSummary method will generate summary for specific agent
293 * This method will count closed tickets, open tickets, responses/interactions with ticket by agent within a date range
294 * @param Reporting $reporting
295 * @param Request $request
296 * @return array
297 */
298 public function getPersonalSummary(Reporting $reporting, Request $request)
299 {
300 $agent = Helper::getAgentByUserId(get_current_user_id());
301
302 return [
303 'summary' => $reporting->agentSummary($request->getSafe('from', 'sanitize_text_field'), $request->getSafe('to', 'sanitize_text_field'), $agent->id)
304 ];
305 }
306
307 public function dayTimeStats(Reporting $reporting, Request $request)
308 {
309 list($from, $to) = self::getSanitizedDateRange($request);
310
311 $filter = [
312 'report_type' => $request->getSafe('report_type', 'sanitize_text_field') ?: null,
313 'agent_id' => $request->getSafe('agent_id', 'intval') ?: null,
314 ];
315
316 $results = $reporting->getQueryResults($from, $to, $filter);
317
318 return $this->send([
319 'stats' => $results
320 ]);
321 }
322
323 public function ticketResponseStats(Reporting $reporting, Request $request)
324 {
325
326 list($from, $to) = self::getSanitizedDateRange($request);
327
328 $filter = [
329 'person_type' => $request->getSafe('person_type', 'sanitize_text_field') ?: null,
330 'person_id' => $request->getSafe('person_id', 'intval') ?: null,
331 ];
332
333 return $reporting->getTicketResponseStats($from, $to, $filter);
334 }
335
336 /**
337 * getStats method will return statistics similar to getOverallReports but with filters
338 * Returns: New Tickets, Active Tickets, Closed Tickets, and Responses
339 * Filters: date_range, mailbox_id (business_box), product_id, agent_id, customer_id
340 * @param Request $request
341 * @return array
342 */
343 public function getStats(Request $request)
344 {
345 list($from, $to) = self::getSanitizedDateRange($request);
346
347 $filters = [
348 'mailbox_id' => $request->getSafe('mailbox_id', 'intval') ?: $request->getSafe('business_box', 'intval'),
349 'product_id' => $request->getSafe('product_id', 'intval'),
350 'agent_id' => $request->getSafe('agent_id', 'intval'),
351 'customer_id' => $request->getSafe('customer_id', 'intval'),
352 ];
353
354 $agentIds = self::getAgentIdsForGroup($request);
355
356 $baseQuery = Ticket::query();
357 foreach ($filters as $field => $value) {
358 if ($value) {
359 $baseQuery->where($field, $value);
360 }
361 }
362
363 if ($agentIds !== null) {
364 $baseQuery->whereIn('agent_id', $agentIds);
365 }
366
367 $applyDateRange = function($query, $dateField = 'created_at') use ($from, $to) {
368 if ($from && $to) {
369 $query->whereBetween($dateField, ["$from 00:00:00", "$to 23:59:59"]);
370 } elseif ($from) {
371 $query->where($dateField, '>=', "$from 00:00:00");
372 } elseif ($to) {
373 $query->where($dateField, '<=', "$to 23:59:59");
374 }
375 };
376
377 $countTickets = function($status, $dateField = 'created_at') use ($baseQuery, $applyDateRange) {
378 $query = clone $baseQuery;
379 $query->where('status', $status);
380 $applyDateRange($query, $dateField);
381 return $query->count();
382 };
383
384 $newTickets = $countTickets('new');
385 $closedTickets = $countTickets('closed', 'resolved_at');
386
387 $openQuery = clone $baseQuery;
388 $openQuery->where('status', '!=', 'closed');
389 $applyDateRange($openQuery);
390 $openTickets = $openQuery->count();
391
392 $responsesQuery = Conversation::query()->where('conversation_type', 'response');
393
394 if (array_filter($filters) || $agentIds !== null) {
395 $responsesQuery->whereHas('ticket', function ($q) use ($filters, $agentIds) {
396 foreach ($filters as $field => $value) {
397 if ($value) {
398 $q->where($field, $value);
399 }
400 }
401 if ($agentIds !== null) {
402 $q->whereIn('agent_id', $agentIds);
403 }
404 });
405 }
406
407 $applyDateRange($responsesQuery, 'created_at');
408 $responses = $responsesQuery->count();
409
410 $agentId = $filters['agent_id'];
411
412 if ($agentId) {
413 $repliesQuery = Conversation::query()
414 ->where('person_id', $agentId)
415 ->where('conversation_type', 'response');
416
417 $applyDateRange($repliesQuery, 'created_at');
418 $totalReplies = $repliesQuery->count();
419
420 $stats = [
421 'total_replies' => $totalReplies,
422 'new_tickets' => $newTickets,
423 'closed_tickets' => $closedTickets,
424 'responses' => $responses,
425 'open_tickets' => $openTickets,
426 ];
427 } else {
428 $activeTickets = $countTickets('active');
429
430 $stats = [
431 'new_tickets' => $newTickets,
432 'active_tickets' => $activeTickets,
433 'closed_tickets' => $closedTickets,
434 'responses' => $responses,
435 'open_tickets' => $openTickets,
436 ];
437 }
438
439 $labels = [
440 'total_replies' => __('Total Replies', 'fluent-support'),
441 'new_tickets' => __('New Tickets', 'fluent-support'),
442 'active_tickets' => __('Active Tickets', 'fluent-support'),
443 'closed_tickets' => __('Closed Tickets', 'fluent-support'),
444 'responses' => __('Responses', 'fluent-support'),
445 'open_tickets' => __('Open Tickets', 'fluent-support'),
446 ];
447
448 $overallReports = [];
449 foreach ($stats as $key => $count) {
450 $overallReports[$key] = [
451 'title' => $labels[$key] ?? ucwords(str_replace('_', ' ', (string) $key)),
452 'key' => $key,
453 'count' => $count,
454 ];
455 }
456
457 return ['overall_reports' => $overallReports];
458 }
459
460 }
461