Ticket.php 3.3 KB

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