214 lines
6.3 KiB
PHP
214 lines
6.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Modules\Helpdesk\Models;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Modules\Helpdesk\Database\Factories\TicketFactory;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property string $reference
|
|
* @property string $subject
|
|
* @property string $body
|
|
*/
|
|
final class Ticket extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
use SoftDeletes;
|
|
|
|
protected $table = 'helpdesk_tickets';
|
|
|
|
/** @var list<string> */
|
|
protected $fillable = [
|
|
'reference', 'company_id', 'user_id', 'agent_id', 'status_id', 'board_id', 'priority_id', 'category_id', 'project_id', 'team_id',
|
|
'subject', 'body', 'due_at', 'completed_at', 'last_activity_at', 'archived_at',
|
|
'sla_policy_id', 'product_id', 'first_response_at', 'sla_first_response_due_at', 'sla_resolution_due_at',
|
|
'sla_first_response_breached_at', 'sla_resolution_breached_at', 'public_replies_count', 'time_spent_minutes',
|
|
];
|
|
|
|
/** @var array<string, string> */
|
|
protected $casts = [
|
|
'company_id' => 'integer',
|
|
'team_id' => 'integer',
|
|
'board_id' => 'integer',
|
|
'due_at' => 'datetime',
|
|
'completed_at' => 'datetime',
|
|
'last_activity_at' => 'datetime',
|
|
'archived_at' => 'datetime',
|
|
'sla_policy_id' => 'integer',
|
|
'product_id' => 'integer',
|
|
'first_response_at' => 'datetime',
|
|
'sla_first_response_due_at' => 'datetime',
|
|
'sla_resolution_due_at' => 'datetime',
|
|
'sla_first_response_breached_at' => 'datetime',
|
|
'sla_resolution_breached_at' => 'datetime',
|
|
'public_replies_count' => 'integer',
|
|
'time_spent_minutes' => 'integer',
|
|
];
|
|
|
|
/** @return BelongsTo<User, Ticket> */
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
/** @return BelongsTo<User, Ticket> */
|
|
public function owner(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
/** @return BelongsTo<User, Ticket> */
|
|
public function agent(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'agent_id');
|
|
}
|
|
|
|
/** @return BelongsTo<Status, Ticket> */
|
|
public function status(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Status::class, 'status_id');
|
|
}
|
|
|
|
/** @return BelongsTo<KanbanBoard, Ticket> */
|
|
public function board(): BelongsTo
|
|
{
|
|
return $this->belongsTo(KanbanBoard::class, 'board_id');
|
|
}
|
|
|
|
/** @return BelongsTo<Priority, Ticket> */
|
|
public function priority(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Priority::class, 'priority_id');
|
|
}
|
|
|
|
/** @return BelongsTo<Category, Ticket> */
|
|
public function category(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Category::class, 'category_id');
|
|
}
|
|
|
|
/** @return BelongsTo<Project, Ticket> */
|
|
public function project(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Project::class, 'project_id');
|
|
}
|
|
|
|
/** @return BelongsTo<Team, Ticket> */
|
|
public function team(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Team::class, 'team_id');
|
|
}
|
|
|
|
/** @return HasMany<Comment> */
|
|
public function comments(): HasMany
|
|
{
|
|
return $this->hasMany(Comment::class, 'ticket_id')
|
|
->whereNull('parent_id')
|
|
->oldest();
|
|
}
|
|
|
|
/** @return BelongsTo<SlaPolicy, Ticket> */
|
|
public function slaPolicy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(SlaPolicy::class, 'sla_policy_id');
|
|
}
|
|
|
|
/** @return BelongsTo<Product, Ticket> */
|
|
public function product(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Product::class, 'product_id');
|
|
}
|
|
|
|
/** @return HasMany<TicketSlaEvent> */
|
|
public function slaEvents(): HasMany
|
|
{
|
|
return $this->hasMany(TicketSlaEvent::class, 'ticket_id')->latest();
|
|
}
|
|
|
|
/** @return HasMany<TicketRating> */
|
|
public function ratings(): HasMany
|
|
{
|
|
return $this->hasMany(TicketRating::class, 'ticket_id')->latest();
|
|
}
|
|
|
|
/** @return HasMany<ChecklistItem> */
|
|
public function checklistItems(): HasMany
|
|
{
|
|
return $this->hasMany(ChecklistItem::class, 'ticket_id')->orderBy('sort_order')->orderBy('id');
|
|
}
|
|
|
|
/** @return BelongsToMany<Tag> */
|
|
public function tags(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Tag::class, 'helpdesk_ticket_tag', 'ticket_id', 'tag_id');
|
|
}
|
|
|
|
public function getSlaFirstResponseBreachedAttribute(): bool
|
|
{
|
|
return $this->first_response_at === null
|
|
&& $this->sla_first_response_due_at !== null
|
|
&& now()->greaterThan($this->sla_first_response_due_at);
|
|
}
|
|
|
|
public function getSlaResolutionBreachedAttribute(): bool
|
|
{
|
|
return $this->completed_at === null
|
|
&& $this->sla_resolution_due_at !== null
|
|
&& now()->greaterThan($this->sla_resolution_due_at);
|
|
}
|
|
|
|
/** @return HasMany<GitCommit> */
|
|
public function gitCommits(): HasMany
|
|
{
|
|
return $this->hasMany(GitCommit::class, 'ticket_id')->latest('committed_at');
|
|
}
|
|
|
|
/** @return HasMany<Attachment> */
|
|
public function attachments(): HasMany
|
|
{
|
|
return $this->hasMany(Attachment::class, 'ticket_id')->latest();
|
|
}
|
|
|
|
/** @param Builder<Ticket> $query @return Builder<Ticket> */
|
|
public function scopeOpen(Builder $query): Builder
|
|
{
|
|
return $query->whereNull('completed_at');
|
|
}
|
|
|
|
/** @param Builder<Ticket> $query @return Builder<Ticket> */
|
|
public function scopeVisibleTo(Builder $query, User $user): Builder
|
|
{
|
|
if ((bool) ($user->helpdesk_admin ?? false)) {
|
|
return $query;
|
|
}
|
|
|
|
if ((bool) ($user->helpdesk_agent ?? false)) {
|
|
return $query->where(static function (Builder $subQuery) use ($user): void {
|
|
$subQuery->where('agent_id', $user->id)->orWhere('user_id', $user->id);
|
|
});
|
|
}
|
|
|
|
return $query->where('user_id', $user->id);
|
|
}
|
|
|
|
/**
|
|
* Create a new factory instance for the model.
|
|
*/
|
|
protected static function newFactory(): TicketFactory
|
|
{
|
|
return TicketFactory::new();
|
|
}
|
|
|
|
}
|