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