ticket.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. namespace App;
  3. use App\Enums\DocumentTypes;
  4. use App\Enums\TicketPriorities;
  5. use Exception;
  6. use RuntimeException;
  7. /**
  8. * @var int $id
  9. */
  10. $core = Core::getInstance();
  11. $core->addBreadcrumb("/tickets", __("Tickets"));
  12. $user = $core->getUser();
  13. $ticket = Ticket::get($id);
  14. if(!$ticket) {
  15. throw new RuntimeException(__("Ticket not found."));
  16. }
  17. $core->addBreadcrumb("/tickets/$id", $ticket->subject);
  18. $request = Request::fromRequest();
  19. $ticket_submit = $request->int("ticket_submit");
  20. if($ticket_submit) {
  21. $assign_to_user_id = $request->int("assignee");
  22. $assign_to_user = null;
  23. if($assign_to_user_id) {
  24. $assign_to_user = User::requireStaff($assign_to_user_id);
  25. }
  26. $priority = $request->int("priority");
  27. if(!TicketPriorities::validate($priority)) {
  28. throw new RuntimeException(__("Invalid priority."));
  29. }
  30. if($assign_to_user) {
  31. if((int) $ticket->assigned_user_id !== (int) $assign_to_user->id) {
  32. $ticket->assign($assign_to_user);
  33. }
  34. }
  35. else {
  36. $ticket->unassign();
  37. }
  38. try {
  39. $sale_number = $request->int("sale_number");
  40. $aru_document_number = $request->int("aru_document_number");
  41. if($sale_number && $aru_document_number) {
  42. throw new RuntimeException(__("Lead No. and ARU No. cannot be simultaneously provided."));
  43. }
  44. if($sale_number) {
  45. $sale = Sale::getByNumber($sale_number);
  46. if(!$sale) {
  47. throw new RuntimeException(__("Lead No. %d not found.", $sale_number));
  48. }
  49. $ticket->sale_id = $sale->id;
  50. }
  51. else {
  52. $ticket->sale_id = null;
  53. }
  54. if($aru_document_number) {
  55. $aru = Document::getByNumber($aru_document_number, DocumentTypes::ADVANCE_REPLACEMENT_UNIT);
  56. if(!$aru) {
  57. throw new RuntimeException(__("ARU No. %d not found.", $aru_document_number));
  58. }
  59. $ticket->aru_document_id = $aru->id;
  60. }
  61. else {
  62. $ticket->aru_document_id = null;
  63. }
  64. }
  65. catch(Exception $e) {
  66. Session::addAlert("danger", $e->getMessage());
  67. Http::redirect($ticket->getUrl());
  68. }
  69. $ticket->priority = $priority;
  70. $ticket->commit();
  71. Session::addAlert("success", __("Ticket updated successfully."));
  72. Http::redirect($ticket->getUrl());
  73. }
  74. $comment_submit = $request->int("comment_submit");
  75. if($comment_submit) {
  76. $content = $request->string("content");
  77. $internal = (int) $request->int("internal");
  78. $status = $request->int("status");
  79. $attachments = Request::getFileArray("attachments", true);
  80. $comment = new TicketComment();
  81. $comment->ticket_id = $ticket->id;
  82. $comment->user_id = $user->id;
  83. $comment->content = $content;
  84. $comment->internal = $internal;
  85. $comment->dts_created = Database::dts();
  86. $comment->commit();
  87. $ticket->status = $status;
  88. $ticket->commit();
  89. foreach($attachments as $attachment) {
  90. $comment->addAttachment($attachment);
  91. }
  92. Session::addAlert("success", __("Comment added successfully."));
  93. Http::redirect($ticket->getUrl());
  94. }
  95. $core->addScript("/js/new-document-modal.js");
  96. View::include("header");
  97. View::include("ticket", compact("ticket"));
  98. View::include("modals/new_document");
  99. View::include("footer");