|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Agent\helpdesk\Filter; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\Agent\helpdesk\TicketController; |
|
6
|
|
|
use App\Http\Controllers\Controller; |
|
7
|
|
|
use App\Model\helpdesk\Filters\Filter; |
|
8
|
|
|
use App\Model\helpdesk\Filters\Label; |
|
9
|
|
|
use App\Model\helpdesk\Ticket\Tickets; |
|
10
|
|
|
use Auth; |
|
11
|
|
|
use DB; |
|
12
|
|
|
use Illuminate\Http\Request; |
|
13
|
|
|
|
|
14
|
|
|
class FilterController extends Controller |
|
15
|
|
|
{ |
|
16
|
|
|
protected $request; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(Request $req) |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
$this->middleware(['auth', 'role.agent']); |
|
21
|
|
|
$this->request = $req; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function getFilter(Request $request) |
|
25
|
|
|
{ |
|
26
|
|
|
$labels = $this->request->input('labels'); |
|
27
|
|
|
$tags = $this->request->input('tags'); |
|
28
|
|
|
if ($request->has('department')) { |
|
29
|
|
|
$table = $this->departmentTickets($request->input('department'), $request->input('status')); |
|
30
|
|
|
} else { |
|
31
|
|
|
$segment = $this->request->input('segment'); |
|
32
|
|
|
$table = $this->segments($segment); |
|
33
|
|
|
} |
|
34
|
|
|
$tickets = []; |
|
|
|
|
|
|
35
|
|
|
$render = false; |
|
|
|
|
|
|
36
|
|
|
if (is_array($labels) && count($labels) > 0) { |
|
37
|
|
|
$table = $table |
|
38
|
|
|
->leftJoin('filters as label', function ($join) { |
|
39
|
|
|
$join->on('tickets.id', '=', 'label.ticket_id') |
|
40
|
|
|
->where('label.key', '=', 'label'); |
|
41
|
|
|
}) |
|
42
|
|
|
->whereIn('label.value', $labels); |
|
43
|
|
|
} |
|
44
|
|
|
if (is_array($tags) && count($tags) > 0) { |
|
45
|
|
|
$table = $table |
|
46
|
|
|
->leftJoin('filters as tag', function ($join) { |
|
47
|
|
|
$join->on('tickets.id', '=', 'tag.ticket_id') |
|
48
|
|
|
->where('tag.key', '=', 'tag'); |
|
49
|
|
|
}) |
|
50
|
|
|
->whereIn('tag.value', $tags); |
|
51
|
|
|
} |
|
52
|
|
|
if ((is_array($tags) && count($tags) > 0) || (is_array($labels) && count($labels) > 0)) { |
|
53
|
|
|
$render = true; |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
// return \Datatables::of($table)->make(); |
|
|
|
|
|
|
56
|
|
|
return \Ttable::getTable($table); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function filterByKey($key, $labels = []) |
|
60
|
|
|
{ |
|
61
|
|
|
$filter = new Filter(); |
|
62
|
|
|
$query = $filter->where('key', $key) |
|
|
|
|
|
|
63
|
|
|
->where(function ($query) use ($labels) { |
|
64
|
|
|
if (is_array($labels) && count($labels) > 0) { |
|
65
|
|
|
for ($i = 0; $i < count($labels); $i++) { |
|
|
|
|
|
|
66
|
|
|
$query->orWhere('value', 'LIKE', '%'.$labels[$i].'%'); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
}) |
|
70
|
|
|
->lists('ticket_id') |
|
71
|
|
|
->toArray(); |
|
72
|
|
|
|
|
73
|
|
|
return $query; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function segments($segment) |
|
77
|
|
|
{ |
|
78
|
|
|
if (strpos($segment, 'user') !== false) { |
|
79
|
|
|
return $this->formatUserTickets($segment); |
|
80
|
|
|
} |
|
81
|
|
|
$table = $this->table(); |
|
82
|
|
|
switch ($segment) { |
|
83
|
|
View Code Duplication |
case '/ticket/inbox': |
|
|
|
|
|
|
84
|
|
|
if (Auth::user()->role == 'agent') { |
|
85
|
|
|
$id = Auth::user()->primary_dpt; |
|
86
|
|
|
$table = $table->where('tickets.dept_id', '=', $id)->orWhere('assigned_to', '=', Auth::user()->id); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $table |
|
90
|
|
|
->Join('ticket_status', function ($join) { |
|
91
|
|
|
$join->on('ticket_status.id', '=', 'tickets.status') |
|
92
|
|
|
->whereIn('ticket_status.id', [1, 7]); |
|
93
|
|
|
}); |
|
94
|
|
View Code Duplication |
case '/ticket/closed': |
|
|
|
|
|
|
95
|
|
|
if (Auth::user()->role == 'agent') { |
|
96
|
|
|
$id = Auth::user()->primary_dpt; |
|
97
|
|
|
$table = $table->where('tickets.dept_id', '=', $id); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $table |
|
101
|
|
|
->Join('ticket_status', function ($join) { |
|
102
|
|
|
$join->on('ticket_status.id', '=', 'tickets.status') |
|
103
|
|
|
->whereIn('ticket_status.state', ['closed']); |
|
104
|
|
|
}); |
|
105
|
|
|
case '/ticket/myticket': |
|
106
|
|
|
return $table |
|
107
|
|
|
->leftJoin('ticket_status', function ($join) { |
|
108
|
|
|
$join->on('ticket_status.id', '=', 'tickets.status'); |
|
109
|
|
|
}) |
|
110
|
|
|
->orWhere('tickets.assigned_to', '=', Auth::user()->id) |
|
111
|
|
|
->where('tickets.status', '=', 1); |
|
112
|
|
View Code Duplication |
case '/unassigned': |
|
|
|
|
|
|
113
|
|
|
if (Auth::user()->role == 'agent') { |
|
114
|
|
|
$id = Auth::user()->primary_dpt; |
|
115
|
|
|
$table = $table->where('tickets.dept_id', '=', $id); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $table |
|
119
|
|
|
->leftJoin('ticket_status', function ($join) { |
|
120
|
|
|
$join->on('ticket_status.id', '=', 'tickets.status'); |
|
121
|
|
|
}) |
|
122
|
|
|
->where('tickets.assigned_to', '=', null) |
|
123
|
|
|
->where('tickets.status', '=', 1); |
|
124
|
|
|
case '/ticket/overdue': |
|
125
|
|
|
if (Auth::user()->role == 'agent') { |
|
126
|
|
|
$id = Auth::user()->primary_dpt; |
|
127
|
|
|
$table = $table->where('tickets.dept_id', '=', $id); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $table |
|
131
|
|
|
->leftJoin('ticket_status', function ($join) { |
|
132
|
|
|
$join->on('ticket_status.id', '=', 'tickets.status'); |
|
133
|
|
|
}) |
|
134
|
|
|
->where('tickets.status', '=', 1) |
|
135
|
|
|
->where('tickets.isanswered', '=', 0) |
|
136
|
|
|
->whereNotNull('tickets.duedate') |
|
137
|
|
|
->where('tickets.duedate', '!=', '00-00-00 00:00:00') |
|
138
|
|
|
|
|
139
|
|
|
// ->where('duedate','>',\Carbon\Carbon::now()); |
|
|
|
|
|
|
140
|
|
|
->where('tickets.duedate', '<', \Carbon\Carbon::now()); |
|
141
|
|
View Code Duplication |
case '/ticket/approval/closed': |
|
|
|
|
|
|
142
|
|
|
if (Auth::user()->role == 'agent') { |
|
143
|
|
|
$id = Auth::user()->primary_dpt; |
|
144
|
|
|
$table = $table->where('tickets.dept_id', '=', $id); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return $table |
|
148
|
|
|
->Join('ticket_status', function ($join) { |
|
149
|
|
|
$join->on('ticket_status.id', '=', 'tickets.status') |
|
150
|
|
|
->where('tickets.status', '=', 7); |
|
151
|
|
|
}); |
|
152
|
|
|
|
|
153
|
|
View Code Duplication |
case '/trash': |
|
|
|
|
|
|
154
|
|
|
if (Auth::user()->role == 'agent') { |
|
155
|
|
|
$id = Auth::user()->primary_dpt; |
|
156
|
|
|
$table = $table->where('tickets.dept_id', '=', $id); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $table |
|
160
|
|
|
->Join('ticket_status', function ($join) { |
|
161
|
|
|
$join->on('ticket_status.id', '=', 'tickets.status') |
|
162
|
|
|
->where('tickets.status', '=', 5); |
|
163
|
|
|
}); |
|
164
|
|
|
|
|
165
|
|
View Code Duplication |
case '/ticket/answered': |
|
|
|
|
|
|
166
|
|
|
if (Auth::user()->role == 'agent') { |
|
167
|
|
|
$id = Auth::user()->primary_dpt; |
|
168
|
|
|
$table = $table->where('tickets.dept_id', '=', $id); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return $table |
|
172
|
|
|
->Join('ticket_status', function ($join) { |
|
173
|
|
|
$join->on('ticket_status.id', '=', 'tickets.status') |
|
174
|
|
|
->where('tickets.status', '=', 1) |
|
175
|
|
|
->where('tickets.isanswered', '=', 1); |
|
176
|
|
|
}); |
|
177
|
|
View Code Duplication |
case '/ticket/assigned': |
|
|
|
|
|
|
178
|
|
|
if (Auth::user()->role == 'agent') { |
|
179
|
|
|
$id = Auth::user()->primary_dpt; |
|
180
|
|
|
$table = $table->where('tickets.dept_id', '=', $id); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $table |
|
184
|
|
|
->leftJoin('ticket_status', function ($join) { |
|
185
|
|
|
$join->on('ticket_status.id', '=', 'tickets.status'); |
|
186
|
|
|
}) |
|
187
|
|
|
->where('tickets.assigned_to', '>', 0) |
|
188
|
|
|
->where('tickets.status', '=', 1); |
|
189
|
|
View Code Duplication |
case '/ticket/open': |
|
|
|
|
|
|
190
|
|
|
if (Auth::user()->role == 'agent') { |
|
191
|
|
|
$id = Auth::user()->primary_dpt; |
|
192
|
|
|
$table = $table->where('tickets.dept_id', '=', $id); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
return $table |
|
196
|
|
|
->leftJoin('ticket_status', function ($join) { |
|
197
|
|
|
$join->on('ticket_status.id', '=', 'tickets.status'); |
|
198
|
|
|
}) |
|
199
|
|
|
->where('isanswered', '=', 0) |
|
200
|
|
|
->where('tickets.status', '=', 1); |
|
201
|
|
|
case '/duetoday': |
|
202
|
|
|
if (Auth::user()->role == 'agent') { |
|
203
|
|
|
$id = Auth::user()->primary_dpt; |
|
204
|
|
|
$table = $table->where('tickets.dept_id', '=', $id); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
return $table |
|
208
|
|
|
->leftJoin('ticket_status', function ($join) { |
|
209
|
|
|
$join->on('ticket_status.id', '=', 'tickets.status'); |
|
210
|
|
|
}) |
|
211
|
|
|
->where('tickets.status', '=', 1) |
|
212
|
|
|
|
|
213
|
|
|
->whereNotNull('tickets.duedate') |
|
214
|
|
|
->whereDate('tickets.duedate', '=', \Carbon\Carbon::now()->format('Y-m-d')); |
|
215
|
|
|
|
|
216
|
|
View Code Duplication |
case '/ticket/followup': |
|
|
|
|
|
|
217
|
|
|
if (Auth::user()->role == 'agent') { |
|
218
|
|
|
$id = Auth::user()->primary_dpt; |
|
219
|
|
|
$table = $table->where('tickets.dept_id', '=', $id); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
return $table |
|
223
|
|
|
->leftJoin('ticket_status', function ($join) { |
|
224
|
|
|
$join->on('ticket_status.id', '=', 'tickets.status'); |
|
225
|
|
|
}) |
|
226
|
|
|
->where('tickets.status', '=', 1) |
|
227
|
|
|
// ->where('tickets.isanswered', '=', 0) |
|
|
|
|
|
|
228
|
|
|
->where('tickets.follow_up', '=', 1); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
public function table() |
|
233
|
|
|
{ |
|
234
|
|
|
// if (Auth::user()->role == 'admin') { |
|
|
|
|
|
|
235
|
|
|
$ticket = new Tickets(); |
|
236
|
|
|
$tickets = $ticket |
|
|
|
|
|
|
237
|
|
|
->leftJoin('ticket_thread', function ($join) { |
|
238
|
|
|
$join->on('tickets.id', '=', 'ticket_thread.ticket_id') |
|
239
|
|
|
->whereNotNull('title') |
|
240
|
|
|
->where('ticket_thread.is_internal', '<>', 1); |
|
241
|
|
|
}) |
|
242
|
|
|
->leftJoin('ticket_thread as ticket_thread2', 'ticket_thread2.ticket_id', '=', 'tickets.id') |
|
243
|
|
|
->Join('ticket_source', 'ticket_source.id', '=', 'tickets.source') |
|
244
|
|
|
->leftJoin('ticket_priority', 'ticket_priority.priority_id', '=', 'tickets.priority_id') |
|
245
|
|
|
->leftJoin('users as u', 'u.id', '=', 'tickets.user_id') |
|
246
|
|
|
->leftJoin('users as u1', 'u1.id', '=', 'tickets.assigned_to') |
|
247
|
|
|
->leftJoin('ticket_attachment', 'ticket_attachment.thread_id', '=', 'ticket_thread.id') |
|
248
|
|
|
|
|
249
|
|
|
->leftJoin('ticket_collaborator', 'ticket_collaborator.ticket_id', '=', 'tickets.id') |
|
250
|
|
|
->select( |
|
251
|
|
|
'tickets.id', |
|
252
|
|
|
'ticket_thread.title', |
|
253
|
|
|
'tickets.ticket_number', |
|
254
|
|
|
'ticket_priority.priority', |
|
255
|
|
|
'u.user_name as user_name', |
|
256
|
|
|
'u1.user_name as assign_user_name', |
|
257
|
|
|
\DB::raw('max(ticket_thread.updated_at) as updated_at'), |
|
258
|
|
|
\DB::raw('min(ticket_thread.updated_at) as created_at'), |
|
259
|
|
|
'u.first_name as first_name', |
|
260
|
|
|
'u.last_name as last_name', |
|
261
|
|
|
'u1.first_name as assign_first_name', |
|
262
|
|
|
'u1.last_name as assign_last_name', |
|
263
|
|
|
'ticket_priority.priority_color', |
|
264
|
|
|
DB::raw('COUNT(DISTINCT ticket_thread2.id) as countthread'), |
|
265
|
|
|
DB::raw('COUNT(ticket_attachment.thread_id) as countattachment'), |
|
266
|
|
|
DB::raw('COUNT(ticket_collaborator.ticket_id) as countcollaborator'), |
|
267
|
|
|
'tickets.status', |
|
268
|
|
|
'tickets.user_id', |
|
269
|
|
|
'tickets.priority_id', 'tickets.assigned_to', |
|
270
|
|
|
'ticket_status.name as tickets_status', |
|
271
|
|
|
'ticket_source.css_class as css', |
|
272
|
|
|
DB::raw('substring_index(group_concat(ticket_thread.poster order by ticket_thread.id desc) , ",", 1) as last_replier'), |
|
273
|
|
|
DB::raw('substring_index(group_concat(ticket_thread.title order by ticket_thread.id asc) , ",", 1) as ticket_title'), |
|
274
|
|
|
'u.active as verified') |
|
275
|
|
|
->groupby('tickets.id'); |
|
276
|
|
|
|
|
277
|
|
|
return $tickets; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
public function filter($render, $ticket_id = []) |
|
281
|
|
|
{ |
|
282
|
|
|
if (Auth::user()->role == 'admin') { |
|
283
|
|
|
$tickets = Tickets::whereIn('status', [1, 7]); |
|
284
|
|
|
} else { |
|
285
|
|
|
$dept = DB::table('department')->where('id', '=', Auth::user()->primary_dpt)->first(); |
|
286
|
|
|
$tickets = Tickets::whereIn('status', [1, 7])->where('dept_id', '=', $dept->id); |
|
287
|
|
|
} |
|
288
|
|
|
if ($render == true) { |
|
289
|
|
|
$tickets = $tickets->whereIn('id', $ticket_id); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
return $tickets; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
View Code Duplication |
public function ticketController() |
|
|
|
|
|
|
296
|
|
|
{ |
|
297
|
|
|
$PhpMailController = new \App\Http\Controllers\Common\PhpMailController(); |
|
298
|
|
|
$NotificationController = new \App\Http\Controllers\Common\NotificationController(); |
|
299
|
|
|
$ticket_controller = new TicketController($PhpMailController, $NotificationController); |
|
300
|
|
|
|
|
301
|
|
|
return $ticket_controller; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
public function departmentTickets($dept, $status) |
|
305
|
|
|
{ |
|
306
|
|
|
$table = $this->table(); |
|
307
|
|
|
|
|
308
|
|
|
return $table->leftJoin('department as dep', 'tickets.dept_id', '=', 'dep.id') |
|
309
|
|
|
->leftJoin('ticket_status', 'tickets.status', '=', 'ticket_status.id') |
|
310
|
|
|
->where('dep.name', $dept) |
|
311
|
|
|
->where('ticket_status.name', $status); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
*@category function to format and return user tickets |
|
316
|
|
|
* |
|
317
|
|
|
*@param string $segment |
|
318
|
|
|
* |
|
319
|
|
|
*@return builder |
|
320
|
|
|
*/ |
|
321
|
|
|
public function formatUserTickets($segment) |
|
322
|
|
|
{ |
|
323
|
|
|
$convert_to_array = explode('/', $segment); |
|
324
|
|
|
$user_id = $convert_to_array[2]; |
|
325
|
|
|
$user = \DB::table('users')->select('role', 'id')->where('id', '=', $user_id)->first(); |
|
326
|
|
|
$table = $this->table(); |
|
327
|
|
|
if ($user->role == 'user') { |
|
328
|
|
|
$table = $table->leftJoin('ticket_status', 'tickets.status', '=', 'ticket_status.id') |
|
329
|
|
|
->where('tickets.user_id', '=', $user->id) |
|
330
|
|
|
->where('ticket_status.name', $convert_to_array[3]); |
|
331
|
|
|
} else { |
|
332
|
|
|
$table = $table->leftJoin('ticket_status', 'tickets.status', '=', 'ticket_status.id') |
|
333
|
|
|
->where('tickets.assigned_to', '=', $user->id) |
|
334
|
|
|
->where('ticket_status.name', $convert_to_array[3]); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
return $table; |
|
338
|
|
|
} |
|
339
|
|
|
} |
|
340
|
|
|
|