Ticket.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App;
  3. use App\Enums\TicketPriorities;
  4. use App\Enums\TicketStatuses;
  5. use App\Traits\DateTimeTrait;
  6. use App\Traits\SoftDeleteTrait;
  7. use Carbon\CarbonImmutable;
  8. use Exception;
  9. /**
  10. * Class Ticket
  11. * @property int $id
  12. * @property int $location_id
  13. * @property int $sale_id
  14. * @property int $aru_document_id
  15. * @property int $creator_user_id
  16. * @property int $assigned_user_id
  17. * @property string $subject
  18. * @property string $contact_name
  19. * @property int $status
  20. * @property int $priority
  21. * @property int $rating
  22. * @property string $dts_due
  23. */
  24. class Ticket extends Model {
  25. protected static $table = "ticket";
  26. protected static $idField = "id";
  27. protected static $fillableFields = [
  28. "location_id", "sale_id", "aru_document_id", "creator_user_id", "assigned_user_id",
  29. "subject", "contact_name", "status", "priority", "rating",
  30. "dts_due",
  31. "dts_created", "dts_updated", "dts_deleted",
  32. ];
  33. use DateTimeTrait;
  34. use SoftDeleteTrait;
  35. /**
  36. * @return Location
  37. * @throws Exception
  38. */
  39. public function getLocation(): Location {
  40. return Location::get($this->location_id);
  41. }
  42. /**
  43. * @return User
  44. * @throws Exception
  45. */
  46. public function getCreator(): User {
  47. return User::get($this->creator_user_id);
  48. }
  49. /**
  50. * @return User|null
  51. * @throws Exception
  52. */
  53. public function getAssignedTo(): ?User {
  54. return User::get($this->assigned_user_id);
  55. }
  56. public function getDateTimeDue(): ?CarbonImmutable {
  57. return $this->dts_due ? new CarbonImmutable($this->dts_due, "UTC") : null;
  58. }
  59. /**
  60. * @return TicketComment[]
  61. */
  62. public function getComments(): array {
  63. return TicketComment::query(["ticket_id" => $this->id], ["sort" => "id"]);
  64. }
  65. /**
  66. * @return string
  67. * @throws Exception
  68. */
  69. public function getDisplayCreator(): string {
  70. $creator = $this->getCreator();
  71. return $creator->getDisplayName() . " ($creator->email)";
  72. }
  73. /**
  74. * @return string
  75. * @throws Exception
  76. */
  77. public function getDisplayAssignedTo(): string {
  78. $assigned_to = $this->getAssignedTo();
  79. return $assigned_to ? $assigned_to->getDisplayName() . " ($assigned_to->email)" : __("Unassigned");
  80. }
  81. public function getDisplayStatus(): string {
  82. return TicketStatuses::enumToText($this->status);
  83. }
  84. public function getPriorityBadgeHtml(): string {
  85. return TicketPriorities::getBadgeHtml($this->priority);
  86. }
  87. public function getUrl(): string {
  88. return sprintf("/tickets/%d", $this->id);
  89. }
  90. /**
  91. * @param User $user
  92. * @return bool
  93. * @throws Exception
  94. */
  95. public function assign(User $user): bool {
  96. $this->assigned_user_id = $user->id;
  97. return $this->commit();
  98. }
  99. /**
  100. * @return bool
  101. * @throws Exception
  102. */
  103. public function unassign(): bool {
  104. $this->assigned_user_id = null;
  105. return $this->commit();
  106. }
  107. /**
  108. * @return Sale|null
  109. * @throws Exception
  110. */
  111. public function getSale(): ?Sale {
  112. return Sale::get($this->sale_id);
  113. }
  114. /**
  115. * @return Document|null
  116. * @throws Exception
  117. */
  118. public function getAruDocument(): ?Document {
  119. return Document::get($this->aru_document_id);
  120. }
  121. }