1
0

ticket.php 2.9 KB

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