SupportTicketController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Http\Controllers\Seller;
  3. use App\Mail\SupportMailManager;
  4. use Illuminate\Http\Request;
  5. use App\Models\Ticket;
  6. use App\Models\TicketReply;
  7. use App\Models\User;
  8. use Auth;
  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('seller.support_ticket.index', compact('tickets'));
  21. }
  22. /**
  23. * Store a newly created resource in storage.
  24. *
  25. * @param \Illuminate\Http\Request $request
  26. * @return \Illuminate\Http\Response
  27. */
  28. public function store(Request $request)
  29. {
  30. $ticket = new Ticket;
  31. $ticket->code = max(100000, (Ticket::latest()->first() != null ? Ticket::latest()->first()->code + 1 : 0)).date('s');
  32. $ticket->user_id = Auth::user()->id;
  33. $ticket->subject = $request->subject;
  34. $ticket->details = $request->details;
  35. $ticket->files = $request->attachments;
  36. if($ticket->save()){
  37. $this->send_support_mail_to_admin($ticket);
  38. flash(translate('Ticket has been sent successfully'))->success();
  39. return redirect()->route('seller.support_ticket.index');
  40. }
  41. else{
  42. flash(translate('Something went wrong'))->error();
  43. }
  44. }
  45. public function send_support_mail_to_admin($ticket){
  46. $array['view'] = 'emails.support';
  47. $array['subject'] = 'Support ticket Code is:- '.$ticket->code;
  48. $array['from'] = env('MAIL_FROM_ADDRESS');
  49. $array['content'] = 'Hi. A ticket has been created. Please check the ticket.';
  50. $array['link'] = route('support_ticket.admin_show', encrypt($ticket->id));
  51. $array['sender'] = $ticket->user->name;
  52. $array['details'] = $ticket->details;
  53. try {
  54. Mail::to(User::where('user_type', 'admin')->first()->email)->queue(new SupportMailManager($array));
  55. } catch (\Exception $e) {
  56. // dd($e->getMessage());
  57. }
  58. }
  59. /**
  60. * Display the specified resource.
  61. *
  62. * @param int $id
  63. * @return \Illuminate\Http\Response
  64. */
  65. public function show($id)
  66. {
  67. $ticket = Ticket::findOrFail(decrypt($id));
  68. $ticket->client_viewed = 1;
  69. $ticket->save();
  70. $ticket_replies = $ticket->ticketreplies;
  71. return view('seller.support_ticket.show', compact('ticket','ticket_replies'));
  72. }
  73. public function ticket_reply_store(Request $request)
  74. {
  75. $ticket_reply = new TicketReply;
  76. $ticket_reply->ticket_id = $request->ticket_id;
  77. $ticket_reply->user_id = $request->user_id;
  78. $ticket_reply->reply = $request->reply;
  79. $ticket_reply->files = $request->attachments;
  80. $ticket_reply->ticket->viewed = 0;
  81. $ticket_reply->ticket->status = 'pending';
  82. $ticket_reply->ticket->save();
  83. if($ticket_reply->save()){
  84. flash(translate('Reply has been sent successfully'))->success();
  85. return back();
  86. }
  87. else{
  88. flash(translate('Something went wrong'))->error();
  89. }
  90. }
  91. }