2026-06-16 18:05:53 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
|
use App\Traits\LogsActivity;
|
|
|
|
|
|
|
|
|
|
class Issue extends Model
|
|
|
|
|
{
|
|
|
|
|
use SoftDeletes, LogsActivity;
|
|
|
|
|
|
|
|
|
|
const STATUSES = ['open', 'in_review', 'resolved', 'closed'];
|
|
|
|
|
const PRIORITIES = ['low', 'medium', 'high', 'critical'];
|
2026-06-18 13:30:54 +02:00
|
|
|
const TYPES = ['defect', 'safety', 'quality', 'documentation', 'other'];
|
2026-06-16 18:05:53 +02:00
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'project_id', 'feature_id', 'inspection_id',
|
2026-06-18 13:30:54 +02:00
|
|
|
'title', 'description', 'status', 'priority', 'type',
|
2026-06-18 10:09:34 +02:00
|
|
|
'reported_by', 'assigned_to', 'resolved_at', 'resolution_notes',
|
|
|
|
|
'uuid', 'client_updated_at',
|
2026-06-16 18:05:53 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $casts = ['resolved_at' => 'datetime'];
|
|
|
|
|
|
|
|
|
|
public function project() { return $this->belongsTo(Project::class); }
|
|
|
|
|
public function feature() { return $this->belongsTo(Feature::class); }
|
|
|
|
|
public function inspection() { return $this->belongsTo(Inspection::class); }
|
|
|
|
|
public function reporter() { return $this->belongsTo(User::class, 'reported_by'); }
|
|
|
|
|
public function assignee() { return $this->belongsTo(User::class, 'assigned_to'); }
|
|
|
|
|
public function media() { return $this->morphMany(Media::class, 'mediable'); }
|
2026-06-18 12:12:39 +02:00
|
|
|
public function tasks() { return $this->hasMany(IssueTask::class)->orderBy('order')->orderBy('id'); }
|
|
|
|
|
public function comments() { return $this->hasMany(IssueComment::class)->orderBy('created_at'); }
|
2026-06-16 18:05:53 +02:00
|
|
|
|
|
|
|
|
public function scopeOpen($q) { return $q->where('status', 'open'); }
|
|
|
|
|
public function scopeCritical($q) { return $q->where('priority', 'critical'); }
|
|
|
|
|
|
2026-06-18 12:12:39 +02:00
|
|
|
/** Resolution progress derived from the checklist: done tasks / total. */
|
|
|
|
|
public function getProgressAttribute(): int
|
|
|
|
|
{
|
|
|
|
|
$total = $this->tasks->count();
|
|
|
|
|
if ($total === 0) {
|
|
|
|
|
return in_array($this->status, ['resolved', 'closed'], true) ? 100 : 0;
|
|
|
|
|
}
|
|
|
|
|
return (int) round($this->tasks->where('is_done', true)->count() / $total * 100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** True when there is at least one task and all of them are done. */
|
|
|
|
|
public function getTasksCompleteAttribute(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->tasks->count() > 0 && $this->tasks->every(fn ($t) => $t->is_done);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-16 18:05:53 +02:00
|
|
|
public function getPriorityColorAttribute(): string
|
|
|
|
|
{
|
|
|
|
|
return match($this->priority) {
|
|
|
|
|
'low' => '#6b7280',
|
|
|
|
|
'medium' => '#f59e0b',
|
|
|
|
|
'high' => '#ef4444',
|
|
|
|
|
'critical' => '#7c3aed',
|
|
|
|
|
default => '#6b7280',
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getStatusColorAttribute(): string
|
|
|
|
|
{
|
|
|
|
|
return match($this->status) {
|
|
|
|
|
'open' => '#ef4444',
|
|
|
|
|
'in_review' => '#f59e0b',
|
|
|
|
|
'resolved' => '#10b981',
|
|
|
|
|
'closed' => '#6b7280',
|
|
|
|
|
default => '#6b7280',
|
|
|
|
|
};
|
|
|
|
|
}
|
2026-06-18 13:30:54 +02:00
|
|
|
|
|
|
|
|
/** Human label (Spanish) for each issue type. */
|
|
|
|
|
public static function typeLabels(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'defect' => 'Defecto',
|
|
|
|
|
'safety' => 'Seguridad',
|
|
|
|
|
'quality' => 'Calidad',
|
|
|
|
|
'documentation' => 'Documentación',
|
|
|
|
|
'other' => 'Otro',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getTypeLabelAttribute(): string
|
|
|
|
|
{
|
|
|
|
|
return self::typeLabels()[$this->type] ?? ucfirst((string) $this->type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getTypeColorAttribute(): string
|
|
|
|
|
{
|
|
|
|
|
return match($this->type) {
|
|
|
|
|
'defect' => '#ef4444',
|
|
|
|
|
'safety' => '#f97316',
|
|
|
|
|
'quality' => '#0ea5e9',
|
|
|
|
|
'documentation' => '#8b5cf6',
|
|
|
|
|
default => '#6b7280',
|
|
|
|
|
};
|
|
|
|
|
}
|
2026-06-16 18:05:53 +02:00
|
|
|
}
|