'date', 'completed_at' => 'datetime', ]; /** * Priority levels */ const PRIORITY_LOW = 'low'; const PRIORITY_MEDIUM = 'medium'; const PRIORITY_HIGH = 'high'; const PRIORITY_URGENT = 'urgent'; /** * Status values */ const STATUS_PENDING = 'pending'; const STATUS_IN_PROGRESS = 'in_progress'; const STATUS_COMPLETED = 'completed'; const STATUS_CANCELLED = 'cancelled'; /** * Get all priority options */ public static function getPriorities(): array { return [ self::PRIORITY_LOW => 'Low', self::PRIORITY_MEDIUM => 'Medium', self::PRIORITY_HIGH => 'High', self::PRIORITY_URGENT => 'Urgent', ]; } /** * Get all status options */ public static function getStatuses(): array { return [ self::STATUS_PENDING => 'Pending', self::STATUS_IN_PROGRESS => 'In Progress', self::STATUS_COMPLETED => 'Completed', self::STATUS_CANCELLED => 'Cancelled', ]; } /** * Get the user this task is assigned to */ public function assignee(): BelongsTo { return $this->belongsTo(User::class, 'assigned_to'); } /** * Get the user who created this task */ public function creator(): BelongsTo { return $this->belongsTo(User::class, 'created_by'); } /** * Scope for filtering by status */ public function scopeStatus($query, string $status) { return $query->where('status', $status); } /** * Scope for filtering by priority */ public function scopePriority($query, string $priority) { return $query->where('priority', $priority); } /** * Scope for filtering by assigned user */ public function scopeAssignedTo($query, int $userId) { return $query->where('assigned_to', $userId); } /** * Scope for filtering by creator */ public function scopeCreatedBy($query, int $userId) { return $query->where('created_by', $userId); } /** * Scope for overdue tasks */ public function scopeOverdue($query) { return $query->where('due_date', '<', now()) ->whereNotIn('status', [self::STATUS_COMPLETED, self::STATUS_CANCELLED]); } /** * Scope for tasks due today */ public function scopeDueToday($query) { return $query->whereDate('due_date', today()); } /** * Check if task is overdue */ public function isOverdue(): bool { return $this->due_date && $this->due_date->isPast() && !in_array($this->status, [self::STATUS_COMPLETED, self::STATUS_CANCELLED]); } /** * Mark task as completed */ public function markAsCompleted(): bool { return $this->update([ 'status' => self::STATUS_COMPLETED, 'completed_at' => now(), ]); } }