| @@ -6,9 +6,8 @@ | ||
| 6 | 6 | use FluentSupport\App\Modules\StatModule; |
| 7 | 7 | use FluentSupport\App\Services\Helper; |
| 8 | 8 | use FluentSupport\Framework\Http\Request\Request; |
| 9 | 9 | use FluentSupport\App\Models\Ticket; |
| 10 | -use FluentSupport\App\Services\Tickets\AgentTicketAccess; | |
| 11 | 10 | use FluentSupport\App\Models\Conversation; |
| 12 | 11 | use FluentSupport\App\Models\TagPivot; |
| 13 | 12 | |
| 14 | 13 | /** |
| @@ -19,9 +18,9 @@ | ||
| 19 | 18 | * @version 1.0.0 |
| 20 | 19 | */ |
| 21 | 20 | class ReportingController extends Controller |
| 22 | 21 | { |
| 23 | - public static function getSanitizedDateRange(Request $request) | |
| 22 | + private static function getSanitizedDateRange(Request $request) | |
| 24 | 23 | { |
| 25 | 24 | $dateRange = $request->get('date_range', []); |
| 26 | 25 | |
| 27 | 26 | if (is_array($dateRange) && count($dateRange) >= 2) { |
| @@ -41,9 +40,9 @@ | ||
| 41 | 40 | |
| 42 | 41 | return ['', '']; |
| 43 | 42 | } |
| 44 | 43 | |
| 45 | - public static function getAgentIdsForGroup(Request $request) | |
| 44 | + private static function getAgentIdsForGroup(Request $request) | |
| 46 | 45 | { |
| 47 | 46 | $groupId = $request->getSafe('agent_group_id', 'intval'); |
| 48 | 47 | if (!$groupId) { |
| 49 | 48 | return null; |
| @@ -54,8 +53,21 @@ | ||
| 54 | 53 | ->pluck('source_id') |
| 55 | 54 | ->toArray(); |
| 56 | 55 | } |
| 57 | 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 | + | |
| 58 | 70 | public function getActiveTicketsByProduct() |
| 59 | 71 | { |
| 60 | 72 | return [ |
| 61 | 73 | 'stats' => StatModule::getActiveTicketsByProductStats() |
| @@ -62,12 +74,126 @@ | ||
| 62 | 74 | ]; |
| 63 | 75 | } |
| 64 | 76 | |
| 65 | 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 | + /** | |
| 66 | 176 | * getAgentOverallReports method will return the overall statistics report for logged-in agent |
| 67 | 177 | * @param Request $request |
| 68 | 178 | * @return array |
| 69 | 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 | + | |
| 70 | 196 | public function getAgentOverallReports(Request $request): array |
| 71 | 197 | { |
| 72 | 198 | $agent = Helper::getAgentByUserId(get_current_user_id()); |
| 73 | 199 | |
| @@ -77,8 +203,60 @@ | ||
| 77 | 203 | ]; |
| 78 | 204 | } |
| 79 | 205 | |
| 80 | 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 | + /** | |
| 81 | 259 | * getAgentResolveChart method will generate ticket data for resolved ticket |
| 82 | 260 | * @param Request $request |
| 83 | 261 | * @param Reporting $reporting |
| 84 | 262 | * @return array |
| @@ -125,8 +303,37 @@ | ||
| 125 | 303 | 'summary' => $reporting->agentSummary($request->getSafe('from', 'sanitize_text_field'), $request->getSafe('to', 'sanitize_text_field'), $agent->id) |
| 126 | 304 | ]; |
| 127 | 305 | } |
| 128 | 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 | + | |
| 129 | 336 | /** |
| 130 | 337 | * getStats method will return statistics similar to getOverallReports but with filters |
| 131 | 338 | * Returns: New Tickets, Active Tickets, Closed Tickets, and Responses |
| 132 | 339 | * Filters: date_range, mailbox_id (business_box), product_id, agent_id, customer_id |
| @@ -145,13 +352,9 @@ | ||
| 145 | 352 | ]; |
| 146 | 353 | |
| 147 | 354 | $agentIds = self::getAgentIdsForGroup($request); |
| 148 | 355 | |
| 149 | - $access = new AgentTicketAccess(); | |
| 150 | - | |
| 151 | - // Bound first: a caller-supplied mailbox_id below is not proof of access. | |
| 152 | - $baseQuery = $access->applyMailboxRestrictionScope(Ticket::query()); | |
| 153 | - | |
| 356 | + $baseQuery = Ticket::query(); | |
| 154 | 357 | foreach ($filters as $field => $value) { |
| 155 | 358 | if ($value) { |
| 156 | 359 | $baseQuery->where($field, $value); |
| 157 | 360 | } |
| @@ -178,9 +381,9 @@ | ||
| 178 | 381 | return $query->count(); |
| 179 | 382 | }; |
| 180 | 383 | |
| 181 | 384 | $newTickets = $countTickets('new'); |
| 182 | - $closedTickets = $countTickets('closed', 'resolved_at'); | |
| 385 | + $closedTickets = $countTickets('closed'); | |
| 183 | 386 | |
| 184 | 387 | $openQuery = clone $baseQuery; |
| 185 | 388 | $openQuery->where('status', '!=', 'closed'); |
| 186 | 389 | $applyDateRange($openQuery); |
| @@ -185,11 +388,9 @@ | ||
| 185 | 388 | $openQuery->where('status', '!=', 'closed'); |
| 186 | 389 | $applyDateRange($openQuery); |
| 187 | 390 | $openTickets = $openQuery->count(); |
| 188 | 391 | |
| 189 | - $responsesQuery = $access->applyMailboxRestrictionScopeViaTicket( | |
| 190 | - Conversation::query()->where('conversation_type', 'response') | |
| 191 | - ); | |
| 392 | + $responsesQuery = Conversation::query()->where('conversation_type', 'response'); | |
| 192 | 393 | |
| 193 | 394 | if (array_filter($filters) || $agentIds !== null) { |
| 194 | 395 | $responsesQuery->whereHas('ticket', function ($q) use ($filters, $agentIds) { |
| 195 | 396 | foreach ($filters as $field => $value) { |
| @@ -208,13 +409,11 @@ | ||
| 208 | 409 | |
| 209 | 410 | $agentId = $filters['agent_id']; |
| 210 | 411 | |
| 211 | 412 | if ($agentId) { |
| 212 | - $repliesQuery = $access->applyMailboxRestrictionScopeViaTicket( | |
| 213 | - Conversation::query() | |
| 214 | - ->where('person_id', $agentId) | |
| 215 | - ->where('conversation_type', 'response') | |
| 216 | - ); | |
| 413 | + $repliesQuery = Conversation::query() | |
| 414 | + ->where('person_id', $agentId) | |
| 415 | + ->where('conversation_type', 'response'); | |
| 217 | 416 | |
| 218 | 417 | $applyDateRange($repliesQuery, 'created_at'); |
| 219 | 418 | $totalReplies = $repliesQuery->count(); |
| 220 | 419 | |