| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Modules; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Agent; |
| 6 |
use FluentSupport\App\Models\Conversation; |
| 7 |
use FluentSupport\App\Models\Ticket; |
| 8 |
use FluentSupport\App\Models\Product; |
| 9 |
|
| 10 |
/** |
| 11 |
* StatModule class is responsible for getting data related to report |
| 12 |
* @package FluentSupport\App\Modules |
| 13 |
* |
| 14 |
* @version 1.0.0 |
| 15 |
*/ |
| 16 |
class StatModule |
| 17 |
{ |
| 18 |
/** |
| 19 |
* getAgentStat method will return ticket statistics by an agent id |
| 20 |
* This method will get agent id, start date and end date as parameter and fetch the ticket information and return |
| 21 |
* @param $agentId |
| 22 |
* @param false $startDate |
| 23 |
* @param false $endDate |
| 24 |
* @return array[] |
| 25 |
*/ |
| 26 |
public static function getAgentStat($agentId, $startDate = false, $endDate = false) |
| 27 |
{ |
| 28 |
if (!$startDate) { |
| 29 |
$currentDate = current_time('mysql'); |
| 30 |
$startDate = $currentDate; |
| 31 |
$endDate = $currentDate; |
| 32 |
} |
| 33 |
|
| 34 |
$startDate = gmdate('Y-m-d 00:00.01', strtotime($startDate)); |
| 35 |
$endDate = gmdate('Y-m-d 23:59.59', strtotime($endDate)); |
| 36 |
|
| 37 |
//Get list of new ticket by agent |
| 38 |
$newTickets = Ticket::where('agent_id', $agentId) |
| 39 |
->where('status', 'new') |
| 40 |
->count(); |
| 41 |
|
| 42 |
//Get list of active ticket by agent |
| 43 |
$activeTickets = Ticket::where('agent_id', $agentId) |
| 44 |
->where('status', 'active') |
| 45 |
->count(); |
| 46 |
|
| 47 |
//Get list of closed ticket by agent within a date range(default today) |
| 48 |
$closedTickets = Ticket::where('agent_id', $agentId) |
| 49 |
->where('status', 'closed') |
| 50 |
->whereBetween('resolved_at', [$startDate, $endDate]) |
| 51 |
->count(); |
| 52 |
|
| 53 |
//Get list of response by agent id within a date range(default today) |
| 54 |
$responses = Conversation::where('conversation_type', 'response') |
| 55 |
->where('person_id', $agentId) |
| 56 |
->whereBetween('created_at', [$startDate, $endDate]) |
| 57 |
->count(); |
| 58 |
|
| 59 |
//Count the response in tickets within a date range(default today) for agent |
| 60 |
$interactions = Conversation::where('person_id', $agentId) |
| 61 |
->where('conversation_type', 'response') |
| 62 |
->whereBetween('created_at', [$startDate, $endDate]) |
| 63 |
->groupBy('ticket_id') |
| 64 |
->get() |
| 65 |
->count(); |
| 66 |
|
| 67 |
return [ |
| 68 |
'new_tickets' => [ |
| 69 |
'title' => __('New Tickets', 'fluent-support'), |
| 70 |
'count' => $newTickets |
| 71 |
], |
| 72 |
'active_tickets' => [ |
| 73 |
'title' => __('Active Tickets', 'fluent-support'), |
| 74 |
'count' => $activeTickets |
| 75 |
], |
| 76 |
'closed_tickets' => [ |
| 77 |
'title' => __('Closed Tickets', 'fluent-support'), |
| 78 |
'count' => $closedTickets |
| 79 |
], |
| 80 |
'responses' => [ |
| 81 |
'title' => __('Responses', 'fluent-support'), |
| 82 |
'count' => $responses |
| 83 |
], |
| 84 |
'interactions' => [ |
| 85 |
'title' => __('Interactions', 'fluent-support'), |
| 86 |
'count' => $interactions |
| 87 |
] |
| 88 |
]; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* getOverAllStats method will return the overall statistics for all tickets |
| 93 |
* This method will count the ticket number by ticket status and return the array |
| 94 |
* @return array[] |
| 95 |
*/ |
| 96 |
public static function getOverAllStats() |
| 97 |
{ |
| 98 |
$newTickets = Ticket::where('status', 'new') |
| 99 |
->count(); |
| 100 |
|
| 101 |
$activeTickets = Ticket::where('status', 'active') |
| 102 |
->count(); |
| 103 |
|
| 104 |
$closedTickets = Ticket::where('status', 'closed') |
| 105 |
->count(); |
| 106 |
|
| 107 |
$responses = Conversation::where('conversation_type', 'response') |
| 108 |
->count(); |
| 109 |
|
| 110 |
return [ |
| 111 |
'new_tickets' => [ |
| 112 |
'title' => __('New Tickets', 'fluent-support'), |
| 113 |
'count' => $newTickets |
| 114 |
], |
| 115 |
'active_tickets' => [ |
| 116 |
'title' => __('Active Tickets', 'fluent-support'), |
| 117 |
'count' => $activeTickets |
| 118 |
], |
| 119 |
'closed_tickets' => [ |
| 120 |
'title' => __('Closed Tickets', 'fluent-support'), |
| 121 |
'count' => $closedTickets |
| 122 |
], |
| 123 |
'responses' => [ |
| 124 |
'title' => __('Responses', 'fluent-support'), |
| 125 |
'count' => $responses |
| 126 |
] |
| 127 |
]; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* getTodayStats method will return a stats of today's tickets |
| 132 |
* This method will count the ticket number by ticket status and return the array |
| 133 |
* @param bool|int $agentId By default value set to false however when it gets an agent id it will fetch |
| 134 |
* the result by this id |
| 135 |
* @return array result in array format |
| 136 |
*/ |
| 137 |
public static function getTodayStats($agentId = false) |
| 138 |
{ |
| 139 |
$start = gmdate('Y-m-d 00:00.01'); |
| 140 |
$end = gmdate('Y-m-d 23:59.59'); |
| 141 |
|
| 142 |
$newTickets = Ticket::whereBetween('created_at', [$start, $end]); |
| 143 |
|
| 144 |
$closedTickets = Ticket::where('status', 'closed')->whereBetween('resolved_at', [$start, $end]); |
| 145 |
|
| 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]); |
| 151 |
|
| 152 |
if ($agentId) { |
| 153 |
$newTickets->where('agent_id', $agentId); |
| 154 |
$closedTickets->where('agent_id', $agentId); |
| 155 |
$responses->where('person_id', $agentId); |
| 156 |
} |
| 157 |
|
| 158 |
return [ |
| 159 |
'new_tickets' => [ |
| 160 |
'title' => __('New Tickets', 'fluent-support'), |
| 161 |
'count' => $newTickets->count() |
| 162 |
], |
| 163 |
'closed_tickets' => [ |
| 164 |
'title' => __('Closed Tickets', 'fluent-support'), |
| 165 |
'count' => $closedTickets->count() |
| 166 |
], |
| 167 |
'responses' => [ |
| 168 |
'title' => __('Responses', 'fluent-support'), |
| 169 |
'count' => $responses->count() |
| 170 |
] |
| 171 |
]; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* getAgentOverallStats method will produce overall statistics by agent |
| 176 |
* @param $agentId |
| 177 |
* @return array[] |
| 178 |
*/ |
| 179 |
public static function getAgentOverallStats($agentId) |
| 180 |
{ |
| 181 |
//Get count of response by the agent |
| 182 |
$replies_count = Conversation::where('person_id', $agentId)->count(); |
| 183 |
|
| 184 |
//Get the number of interactions/responses by agent with tickets |
| 185 |
$interactions_count = Conversation::where('person_id', $agentId) |
| 186 |
->where('conversation_type', 'response') |
| 187 |
->groupBy('ticket_id') |
| 188 |
->get() |
| 189 |
->count(); |
| 190 |
|
| 191 |
//Get the number of tickets that are closed by this agent |
| 192 |
$total_closed = Ticket::where('agent_id', $agentId)->where('status', 'closed')->count(); |
| 193 |
|
| 194 |
return [ |
| 195 |
'replies_count' => [ |
| 196 |
'title' => __('Total Replies', 'fluent-support'), |
| 197 |
'count' => $replies_count |
| 198 |
], |
| 199 |
// 'interactions_count' => [ |
| 200 |
// 'title' => __('Total Interactions', 'fluent-support'), |
| 201 |
// 'count' => $interactions_count |
| 202 |
// ], |
| 203 |
'total_closed' => [ |
| 204 |
'title' => __('Total Closed', 'fluent-support'), |
| 205 |
'count' => $total_closed |
| 206 |
] |
| 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; |
| 284 |
} |
| 285 |
|
| 286 |
} |
| 287 |
|