SupportTicketController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Ticket;
  5. use App\Models\User;
  6. use Auth;
  7. use App\Models\TicketReply;
  8. use App\Mail\SupportMailManager;
  9. use Mail;
  10. class SupportTicketController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return \Illuminate\Http\Response
  16. */
  17. public function index()
  18. {
  19. $tickets = Ticket::where('user_id', Auth::user()->id)->orderBy('created_at', 'desc')->paginate(9);
  20. return view('frontend.user.support_ticket.index', compact('tickets'));
  21. }
  22. public function admin_index(Request $request)
  23. {
  24. $sort_search =null;
  25. $tickets = Ticket::orderBy('created_at', 'desc');
  26. if ($request->has('search')){
  27. $sort_search = $request->search;
  28. $tickets = $tickets->where('code', 'like', '%'.$sort_search.'%');
  29. }
  30. $tickets = $tickets->paginate(15);
  31. return view('backend.support.support_tickets.index', compact('tickets', 'sort_search'));
  32. }
  33. /**
  34. * Show the form for creating a new resource.
  35. *
  36. * @return \Illuminate\Http\Response
  37. */
  38. public function create()
  39. {
  40. //
  41. }
  42. /**
  43. * Store a newly created resource in storage.
  44. *
  45. * @param \Illuminate\Http\Request $request
  46. * @return \Illuminate\Http\Response
  47. */
  48. public function store(Request $request)
  49. {
  50. //dd();
  51. $ticket = new Ticket;
  52. $ticket->code = max(100000, (Ticket::latest()->first() != null ? Ticket::latest()->first()->code + 1 : 0)).date('s');
  53. $ticket->user_id = Auth::user()->id;
  54. $ticket->subject = $request->subject;
  55. $ticket->details = $request->details;
  56. $ticket->files = $request->attachments;
  57. if($ticket->save()){
  58. $this->send_support_mail_to_admin($ticket);
  59. flash(translate('Ticket has been sent successfully'))->success();
  60. return redirect()->route('support_ticket.index');
  61. }
  62. else{
  63. flash(translate('Something went wrong'))->error();
  64. }
  65. }
  66. public function send_support_mail_to_admin($ticket){
  67. $array['view'] = 'emails.support';
  68. $array['subject'] = 'Support ticket Code is:- '.$ticket->code;
  69. $array['from'] = env('MAIL_FROM_ADDRESS');
  70. $array['content'] = 'Hi. A ticket has been created. Please check the ticket.';
  71. $array['link'] = route('support_ticket.admin_show', encrypt($ticket->id));
  72. $array['sender'] = $ticket->user->name;
  73. $array['details'] = $ticket->details;
  74. // dd($array);
  75. // dd(User::where('user_type', 'admin')->first()->email);
  76. try {
  77. Mail::to(User::where('user_type', 'admin')->first()->email)->queue(new SupportMailManager($array));
  78. } catch (\Exception $e) {
  79. // dd($e->getMessage());
  80. }
  81. }
  82. public function send_support_reply_email_to_user($ticket, $tkt_reply){
  83. $array['view'] = 'emails.support';
  84. $array['subject'] = 'Support ticket Code is:- '.$ticket->code;
  85. $array['from'] = env('MAIL_FROM_ADDRESS');
  86. $array['content'] = 'Hi. A ticket has been created. Please check the ticket.';
  87. $array['link'] = route('support_ticket.show', encrypt($ticket->id));
  88. $array['sender'] = $tkt_reply->user->name;
  89. $array['details'] = $tkt_reply->reply;
  90. try {
  91. Mail::to($ticket->user->email)->queue(new SupportMailManager($array));
  92. } catch (\Exception $e) {
  93. //dd($e->getMessage());
  94. }
  95. }
  96. public function admin_store(Request $request)
  97. {
  98. $ticket_reply = new TicketReply;
  99. $ticket_reply->ticket_id = $request->ticket_id;
  100. $ticket_reply->user_id = Auth::user()->id;
  101. $ticket_reply->reply = $request->reply;
  102. $ticket_reply->files = $request->attachments;
  103. $ticket_reply->ticket->client_viewed = 0;
  104. $ticket_reply->ticket->status = $request->status;
  105. $ticket_reply->ticket->save();
  106. if($ticket_reply->save()){
  107. flash(translate('Reply has been sent successfully'))->success();
  108. $this->send_support_reply_email_to_user($ticket_reply->ticket, $ticket_reply);
  109. return back();
  110. }
  111. else{
  112. flash(translate('Something went wrong'))->error();
  113. }
  114. }
  115. public function seller_store(Request $request)
  116. {
  117. $ticket_reply = new TicketReply;
  118. $ticket_reply->ticket_id = $request->ticket_id;
  119. $ticket_reply->user_id = $request->user_id;
  120. $ticket_reply->reply = $request->reply;
  121. $ticket_reply->files = $request->attachments;
  122. $ticket_reply->ticket->viewed = 0;
  123. $ticket_reply->ticket->status = 'pending';
  124. $ticket_reply->ticket->save();
  125. if($ticket_reply->save()){
  126. flash(translate('Reply has been sent successfully'))->success();
  127. return back();
  128. }
  129. else{
  130. flash(translate('Something went wrong'))->error();
  131. }
  132. }
  133. /**
  134. * Display the specified resource.
  135. *
  136. * @param int $id
  137. * @return \Illuminate\Http\Response
  138. */
  139. public function show($id)
  140. {
  141. $ticket = Ticket::findOrFail(decrypt($id));
  142. $ticket->client_viewed = 1;
  143. $ticket->save();
  144. $ticket_replies = $ticket->ticketreplies;
  145. return view('frontend.user.support_ticket.show', compact('ticket','ticket_replies'));
  146. }
  147. public function admin_show($id)
  148. {
  149. $ticket = Ticket::findOrFail(decrypt($id));
  150. $ticket->viewed = 1;
  151. $ticket->save();
  152. return view('backend.support.support_tickets.show', compact('ticket'));
  153. }
  154. /**
  155. * Show the form for editing the specified resource.
  156. *
  157. * @param int $id
  158. * @return \Illuminate\Http\Response
  159. */
  160. public function edit($id)
  161. {
  162. //
  163. }
  164. /**
  165. * Update the specified resource in storage.
  166. *
  167. * @param \Illuminate\Http\Request $request
  168. * @param int $id
  169. * @return \Illuminate\Http\Response
  170. */
  171. public function update(Request $request, $id)
  172. {
  173. //
  174. }
  175. /**
  176. * Remove the specified resource from storage.
  177. *
  178. * @param int $id
  179. * @return \Illuminate\Http\Response
  180. */
  181. public function destroy($id)
  182. {
  183. //
  184. }
  185. }