| @@ -1,10 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentSupport\App\Modules; |
| 4 | 4 | |
| 5 | +use FluentSupport\App\Models\Agent; | |
| 5 | 6 | use FluentSupport\App\Models\Conversation; |
| 6 | 7 | use FluentSupport\App\Models\Ticket; |
| 8 | +use FluentSupport\App\Models\Product; | |
| 9 | + | |
| 7 | 10 | /** |
| 8 | 11 | * StatModule class is responsible for getting data related to report |
| 9 | 12 | * @package FluentSupport\App\Modules |
| 10 | 13 | * |
| @@ -27,10 +30,10 @@ | ||
| 27 | 30 | $startDate = $currentDate; |
| 28 | 31 | $endDate = $currentDate; |
| 29 | 32 | } |
| 30 | 33 | |
| 31 | - $startDate = date('Y-m-d 00:00.01', strtotime($startDate)); | |
| 32 | - $endDate = date('Y-m-d 23:59.59', strtotime($endDate)); | |
| 34 | + $startDate = gmdate('Y-m-d 00:00.01', strtotime($startDate)); | |
| 35 | + $endDate = gmdate('Y-m-d 23:59.59', strtotime($endDate)); | |
| 33 | 36 | |
| 34 | 37 | //Get list of new ticket by agent |
| 35 | 38 | $newTickets = Ticket::where('agent_id', $agentId) |
| 36 | 39 | ->where('status', 'new') |
| @@ -77,9 +80,9 @@ | ||
| 77 | 80 | 'responses' => [ |
| 78 | 81 | 'title' => __('Responses', 'fluent-support'), |
| 79 | 82 | 'count' => $responses |
| 80 | 83 | ], |
| 81 | - 'interactions' =>[ | |
| 84 | + 'interactions' => [ | |
| 82 | 85 | 'title' => __('Interactions', 'fluent-support'), |
| 83 | 86 | 'count' => $interactions |
| 84 | 87 | ] |
| 85 | 88 | ]; |
| @@ -126,24 +129,28 @@ | ||
| 126 | 129 | |
| 127 | 130 | /** |
| 128 | 131 | * getTodayStats method will return a stats of today's tickets |
| 129 | 132 | * This method will count the ticket number by ticket status and return the array |
| 130 | - * @param bool $agentId By default value set to false however when it gets an agent id it will fetch | |
| 133 | + * @param bool|int $agentId By default value set to false however when it gets an agent id it will fetch | |
| 131 | 134 | * the result by this id |
| 132 | 135 | * @return array result in array format |
| 133 | 136 | */ |
| 134 | - public static function getTodayStats($agentId=false) | |
| 137 | + public static function getTodayStats($agentId = false) | |
| 135 | 138 | { |
| 136 | - $start = date('Y-m-d 00:00.01'); | |
| 137 | - $end = date('Y-m-d 23:59.59'); | |
| 139 | + $start = gmdate('Y-m-d 00:00.01'); | |
| 140 | + $end = gmdate('Y-m-d 23:59.59'); | |
| 138 | 141 | |
| 139 | 142 | $newTickets = Ticket::whereBetween('created_at', [$start, $end]); |
| 140 | 143 | |
| 141 | 144 | $closedTickets = Ticket::where('status', 'closed')->whereBetween('resolved_at', [$start, $end]); |
| 142 | 145 | |
| 143 | - $responses = Conversation::where('conversation_type', 'response')->whereBetween('created_at', [$start, $end]); | |
| 146 | + $responses = Conversation::where('conversation_type', 'response') | |
| 147 | + ->whereHas('person', function ($q) { | |
| 148 | + $q->where('person_type', 'agent'); | |
| 149 | + }) | |
| 150 | + ->whereBetween('created_at', [$start, $end]); | |
| 144 | 151 | |
| 145 | - if($agentId) { | |
| 152 | + if ($agentId) { | |
| 146 | 153 | $newTickets->where('agent_id', $agentId); |
| 147 | 154 | $closedTickets->where('agent_id', $agentId); |
| 148 | 155 | $responses->where('person_id', $agentId); |
| 149 | 156 | } |
| @@ -175,29 +182,105 @@ | ||
| 175 | 182 | $replies_count = Conversation::where('person_id', $agentId)->count(); |
| 176 | 183 | |
| 177 | 184 | //Get the number of interactions/responses by agent with tickets |
| 178 | 185 | $interactions_count = Conversation::where('person_id', $agentId) |
| 179 | - ->where('conversation_type', 'response') | |
| 180 | - ->groupBy('ticket_id') | |
| 181 | - ->get() | |
| 182 | - ->count(); | |
| 186 | + ->where('conversation_type', 'response') | |
| 187 | + ->groupBy('ticket_id') | |
| 188 | + ->get() | |
| 189 | + ->count(); | |
| 183 | 190 | |
| 184 | 191 | //Get the number of tickets that are closed by this agent |
| 185 | 192 | $total_closed = Ticket::where('agent_id', $agentId)->where('status', 'closed')->count(); |
| 186 | 193 | |
| 187 | 194 | return [ |
| 188 | - 'replies_count' => [ | |
| 195 | + 'replies_count' => [ | |
| 189 | 196 | 'title' => __('Total Replies', 'fluent-support'), |
| 190 | 197 | 'count' => $replies_count |
| 191 | 198 | ], |
| 192 | - 'interactions_count' => [ | |
| 193 | - 'title' => __('Total Interactions', 'fluent-support'), | |
| 194 | - 'count' => $interactions_count | |
| 195 | - ], | |
| 196 | - 'total_closed' => [ | |
| 199 | + // 'interactions_count' => [ | |
| 200 | + // 'title' => __('Total Interactions', 'fluent-support'), | |
| 201 | + // 'count' => $interactions_count | |
| 202 | + // ], | |
| 203 | + 'total_closed' => [ | |
| 197 | 204 | 'title' => __('Total Closed', 'fluent-support'), |
| 198 | 205 | 'count' => $total_closed |
| 199 | 206 | ] |
| 200 | 207 | ]; |
| 208 | + } | |
| 209 | + | |
| 210 | + /** | |
| 211 | + * This `getActiveTicketsByProductStats` method will count today's tickets by product that are waiting for reply | |
| 212 | + * @return array $result | |
| 213 | + */ | |
| 214 | + public static function getActiveTicketsByProductStats() | |
| 215 | + { | |
| 216 | + $products = Product::all(); | |
| 217 | + $result = []; | |
| 218 | + | |
| 219 | + foreach ($products as $product) { | |
| 220 | + $result[$product->id] = [ | |
| 221 | + 'title' => $product->title, | |
| 222 | + 'count' => static::countAwaitingTickets('product_id', $product->id) | |
| 223 | + ]; | |
| 224 | + } | |
| 225 | + | |
| 226 | + return $result; | |
| 227 | + } | |
| 228 | + | |
| 229 | + | |
| 230 | + /** | |
| 231 | + * This will count the number of tickets that are waiting for response also it can receive parameters | |
| 232 | + * @param string $whereClause // This is the where clause that will be used in the query | |
| 233 | + * @param string $whereClauseValue // This is the value of the where clause | |
| 234 | + * @return int $awatingTicketCount | |
| 235 | + */ | |
| 236 | + public static function countAwaitingTickets($whereClause = null, $whereClauseValue = null) | |
| 237 | + { | |
| 238 | + $ticket = new Ticket; | |
| 239 | + | |
| 240 | + if ($whereClause && $whereClauseValue) { | |
| 241 | + $ticket = $ticket->where(sanitize_text_field($whereClause), sanitize_text_field($whereClauseValue)); | |
| 242 | + } | |
| 243 | + | |
| 244 | + $awatingTicketCount = $ticket->where('status', '!=', 'closed') | |
| 245 | + ->where(function ($query) { | |
| 246 | + $query->whereColumn('last_agent_response', '<', 'last_customer_response') | |
| 247 | + ->orWhereNull('last_agent_response') | |
| 248 | + ->orWhere('status', 'new'); | |
| 249 | + })->count(); | |
| 250 | + | |
| 251 | + return $awatingTicketCount; | |
| 252 | + } | |
| 253 | + | |
| 254 | + /** | |
| 255 | + * This method will return a summary of today's stats of all agent | |
| 256 | + * @return array | |
| 257 | + */ | |
| 258 | + public static function getAgentTodayStats() | |
| 259 | + { | |
| 260 | + $stats = []; | |
| 261 | + Agent::get()->each(function ($agent) use (&$stats) { | |
| 262 | + $agentStat = static::getTodayStats($agent->id); | |
| 263 | + $waiting = static::countAwaitingTickets('agent_id', $agent->id); | |
| 264 | + if(!empty($agentStat['responses']['count']) || $waiting) { | |
| 265 | + $stats[] = [ | |
| 266 | + 'agent_id' => $agent->id, | |
| 267 | + 'agent_name' => $agent->full_name, | |
| 268 | + 'agent_email' => $agent->email, | |
| 269 | + 'avatar' => $agent->photo, | |
| 270 | + 'stats' => array_merge( | |
| 271 | + [ | |
| 272 | + 'waiting_today' => [ | |
| 273 | + 'title' => __('Waiting Today', 'fluent-support'), | |
| 274 | + 'count' => $waiting | |
| 275 | + ] | |
| 276 | + ], | |
| 277 | + $agentStat | |
| 278 | + ) | |
| 279 | + ]; | |
| 280 | + } | |
| 281 | + }); | |
| 282 | + | |
| 283 | + return $stats; | |
| 201 | 284 | } |
| 202 | 285 | |
| 203 | 286 | } |