| @@ -11,13 +11,9 @@ | ||
| 11 | 11 | use FluentSupport\App\Models\Conversation; |
| 12 | 12 | use FluentSupport\App\Models\TagPivot; |
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | - * ReportingController class for REST API | |
| 16 | - * This class is responsible for getting data for all request related to report | |
| 17 | - * @package FluentSupport\App\Http\Controllers | |
| 18 | - * | |
| 19 | - * @version 1.0.0 | |
| 15 | + * Report endpoints. | |
| 20 | 16 | */ |
| 21 | 17 | class ReportingController extends Controller |
| 22 | 18 | { |
| 23 | 19 | public static function getSanitizedDateRange(Request $request) |
| @@ -54,17 +50,11 @@ | ||
| 54 | 50 | ->pluck('source_id') |
| 55 | 51 | ->toArray(); |
| 56 | 52 | } |
| 57 | 53 | |
| 58 | - public function getActiveTicketsByProduct() | |
| 59 | - { | |
| 60 | - return [ | |
| 61 | - 'stats' => StatModule::getActiveTicketsByProductStats() | |
| 62 | - ]; | |
| 63 | - } | |
| 64 | - | |
| 65 | 54 | /** |
| 66 | - * getAgentOverallReports method will return the overall statistics report for logged-in agent | |
| 55 | + * Stats for the logged-in agent. | |
| 56 | + * | |
| 67 | 57 | * @param Request $request |
| 68 | 58 | * @return array |
| 69 | 59 | */ |
| 70 | 60 | public function getAgentOverallReports(Request $request): array |
| @@ -77,9 +67,8 @@ | ||
| 77 | 67 | ]; |
| 78 | 68 | } |
| 79 | 69 | |
| 80 | 70 | /** |
| 81 | - * getAgentResolveChart method will generate ticket data for resolved ticket | |
| 82 | 71 | * @param Request $request |
| 83 | 72 | * @param Reporting $reporting |
| 84 | 73 | * @return array |
| 85 | 74 | */ |
| @@ -84,9 +73,8 @@ | ||
| 84 | 73 | * @return array |
| 85 | 74 | */ |
| 86 | 75 | public function getAgentResolveChart(Request $request, Reporting $reporting) |
| 87 | 76 | { |
| 88 | - //Get logged in agent information | |
| 89 | 77 | $agent = Helper::getAgentByUserId(get_current_user_id()); |
| 90 | 78 | list($from, $to) = self::getSanitizedDateRange($request); |
| 91 | 79 | |
| 92 | 80 | return [ |
| @@ -94,9 +82,8 @@ | ||
| 94 | 82 | ]; |
| 95 | 83 | } |
| 96 | 84 | |
| 97 | 85 | /** |
| 98 | - * getAgentResponseChart method will generate the statistics of response by agent in tickets within date range | |
| 99 | 86 | * @param Request $request |
| 100 | 87 | * @param Reporting $reporting |
| 101 | 88 | * @return array |
| 102 | 89 | */ |
| @@ -110,10 +97,10 @@ | ||
| 110 | 97 | ]; |
| 111 | 98 | } |
| 112 | 99 | |
| 113 | 100 | /** |
| 114 | - * getPersonalSummary method will generate summary for specific agent | |
| 115 | - * This method will count closed tickets, open tickets, responses/interactions with ticket by agent within a date range | |
| 101 | + * Closed/open ticket and response counts for the logged-in agent in a date range. | |
| 102 | + * | |
| 116 | 103 | * @param Reporting $reporting |
| 117 | 104 | * @param Request $request |
| 118 | 105 | * @return array |
| 119 | 106 | */ |
| @@ -126,11 +113,12 @@ | ||
| 126 | 113 | ]; |
| 127 | 114 | } |
| 128 | 115 | |
| 129 | 116 | /** |
| 130 | - * getStats method will return statistics similar to getOverallReports but with filters | |
| 131 | - * Returns: New Tickets, Active Tickets, Closed Tickets, and Responses | |
| 132 | - * Filters: date_range, mailbox_id (business_box), product_id, agent_id, customer_id | |
| 117 | + * Overview counts filtered by date_range, mailbox_id (or business_box), product_id, | |
| 118 | + * agent_id, agent_group_id and customer_id. With agent_id, active_tickets is | |
| 119 | + * replaced by the agent's total_replies. | |
| 120 | + * | |
| 133 | 121 | * @param Request $request |
| 134 | 122 | * @return array |
| 135 | 123 | */ |
| 136 | 124 | public function getStats(Request $request) |
| @@ -185,15 +173,24 @@ | ||
| 185 | 173 | $openQuery->where('status', '!=', 'closed'); |
| 186 | 174 | $applyDateRange($openQuery); |
| 187 | 175 | $openTickets = $openQuery->count(); |
| 188 | 176 | |
| 177 | + $agentId = $filters['agent_id']; | |
| 178 | + | |
| 189 | 179 | $responsesQuery = $access->applyMailboxRestrictionScopeViaTicket( |
| 190 | 180 | Conversation::query()->where('conversation_type', 'response') |
| 191 | 181 | ); |
| 192 | 182 | |
| 193 | - if (array_filter($filters) || $agentIds !== null) { | |
| 194 | - $responsesQuery->whereHas('ticket', function ($q) use ($filters, $agentIds) { | |
| 195 | - foreach ($filters as $field => $value) { | |
| 183 | + // For a single agent, count the replies they wrote, not every reply on tickets assigned to them. | |
| 184 | + $ticketFilters = $filters; | |
| 185 | + if ($agentId) { | |
| 186 | + $responsesQuery->where('person_id', $agentId); | |
| 187 | + unset($ticketFilters['agent_id']); | |
| 188 | + } | |
| 189 | + | |
| 190 | + if (array_filter($ticketFilters) || $agentIds !== null) { | |
| 191 | + $responsesQuery->whereHas('ticket', function ($q) use ($ticketFilters, $agentIds) { | |
| 192 | + foreach ($ticketFilters as $field => $value) { | |
| 196 | 193 | if ($value) { |
| 197 | 194 | $q->where($field, $value); |
| 198 | 195 | } |
| 199 | 196 | } |
| @@ -205,22 +202,10 @@ | ||
| 205 | 202 | |
| 206 | 203 | $applyDateRange($responsesQuery, 'created_at'); |
| 207 | 204 | $responses = $responsesQuery->count(); |
| 208 | 205 | |
| 209 | - $agentId = $filters['agent_id']; | |
| 210 | - | |
| 211 | 206 | if ($agentId) { |
| 212 | - $repliesQuery = $access->applyMailboxRestrictionScopeViaTicket( | |
| 213 | - Conversation::query() | |
| 214 | - ->where('person_id', $agentId) | |
| 215 | - ->where('conversation_type', 'response') | |
| 216 | - ); | |
| 217 | - | |
| 218 | - $applyDateRange($repliesQuery, 'created_at'); | |
| 219 | - $totalReplies = $repliesQuery->count(); | |
| 220 | - | |
| 221 | 207 | $stats = [ |
| 222 | - 'total_replies' => $totalReplies, | |
| 223 | 208 | 'new_tickets' => $newTickets, |
| 224 | 209 | 'closed_tickets' => $closedTickets, |
| 225 | 210 | 'responses' => $responses, |
| 226 | 211 | 'open_tickets' => $openTickets, |
| @@ -237,9 +222,8 @@ | ||
| 237 | 222 | ]; |
| 238 | 223 | } |
| 239 | 224 | |
| 240 | 225 | $labels = [ |
| 241 | - 'total_replies' => __('Total Replies', 'fluent-support'), | |
| 242 | 226 | 'new_tickets' => __('New Tickets', 'fluent-support'), |
| 243 | 227 | 'active_tickets' => __('Active Tickets', 'fluent-support'), |
| 244 | 228 | 'closed_tickets' => __('Closed Tickets', 'fluent-support'), |
| 245 | 229 | 'responses' => __('Responses', 'fluent-support'), |
| @@ -244,8 +228,12 @@ | ||
| 244 | 228 | 'closed_tickets' => __('Closed Tickets', 'fluent-support'), |
| 245 | 229 | 'responses' => __('Responses', 'fluent-support'), |
| 246 | 230 | 'open_tickets' => __('Open Tickets', 'fluent-support'), |
| 247 | 231 | ]; |
| 232 | + | |
| 233 | + if ($agentId) { | |
| 234 | + $labels['responses'] = __('Total Responses', 'fluent-support'); | |
| 235 | + } | |
| 248 | 236 | |
| 249 | 237 | $overallReports = []; |
| 250 | 238 | foreach ($stats as $key => $count) { |
| 251 | 239 | $overallReports[$key] = [ |