| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace App;
- use App\Enums\TicketPriorities;
- use App\Enums\TicketStatuses;
- use App\Traits\DateTimeTrait;
- use App\Traits\SoftDeleteTrait;
- use Carbon\CarbonImmutable;
- use Exception;
- /**
- * Class Ticket
- * @property int $id
- * @property int $location_id
- * @property int $sale_id
- * @property int $aru_document_id
- * @property int $creator_user_id
- * @property int $assigned_user_id
- * @property string $subject
- * @property string $contact_name
- * @property int $status
- * @property int $priority
- * @property int $rating
- * @property string $dts_due
- */
- class Ticket extends Model {
- protected static $table = "ticket";
- protected static $idField = "id";
- protected static $fillableFields = [
- "location_id", "sale_id", "aru_document_id", "creator_user_id", "assigned_user_id",
- "subject", "contact_name", "status", "priority", "rating",
- "dts_due",
- "dts_created", "dts_updated", "dts_deleted",
- ];
- use DateTimeTrait;
- use SoftDeleteTrait;
- /**
- * @return Location
- * @throws Exception
- */
- public function getLocation(): Location {
- return Location::get($this->location_id);
- }
- /**
- * @return User
- * @throws Exception
- */
- public function getCreator(): User {
- return User::get($this->creator_user_id);
- }
- /**
- * @return User|null
- * @throws Exception
- */
- public function getAssignedTo(): ?User {
- return User::get($this->assigned_user_id);
- }
- public function getDateTimeDue(): ?CarbonImmutable {
- return $this->dts_due ? new CarbonImmutable($this->dts_due, "UTC") : null;
- }
- /**
- * @return TicketComment[]
- */
- public function getComments(): array {
- return TicketComment::query(["ticket_id" => $this->id], ["sort" => "id"]);
- }
- /**
- * @return string
- * @throws Exception
- */
- public function getDisplayCreator(): string {
- $creator = $this->getCreator();
- return $creator->getDisplayName() . " ($creator->email)";
- }
- /**
- * @return string
- * @throws Exception
- */
- public function getDisplayAssignedTo(): string {
- $assigned_to = $this->getAssignedTo();
- return $assigned_to ? $assigned_to->getDisplayName() : __("Unassigned");
- }
- public function getDisplayStatus(): string {
- return TicketStatuses::enumToText($this->status);
- }
- public function getPriorityBadgeHtml(): string {
- return TicketPriorities::getBadgeHtml($this->priority);
- }
- public function getUrl(): string {
- return sprintf("/tickets/%d", $this->id);
- }
- /**
- * @param User $user
- * @return bool
- * @throws Exception
- */
- public function assign(User $user): bool {
- $this->assigned_user_id = $user->id;
- return $this->commit();
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function unassign(): bool {
- $this->assigned_user_id = null;
- return $this->commit();
- }
- /**
- * @return Sale|null
- * @throws Exception
- */
- public function getSale(): ?Sale {
- return Sale::get($this->sale_id);
- }
- /**
- * @return Document|null
- * @throws Exception
- */
- public function getAruDocument(): ?Document {
- return Document::get($this->aru_document_id);
- }
- public function getTitle(): string {
- return __("Ticket %d - %s", $this->id, $this->subject);
- }
- }
|