Files
construprogress/app/Models/Inspection.php
T

113 lines
2.6 KiB
PHP
Raw Normal View History

2026-05-07 23:31:33 +02:00
<?php
namespace App\Models;
2026-08-28 13:04:28 +02:00
use App\Traits\LogsActivity;
2026-05-07 23:31:33 +02:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Scout\Searchable;
2026-05-07 23:31:33 +02:00
class Inspection extends Model
{
use LogsActivity, SoftDeletes, Searchable;
2026-05-07 23:31:33 +02:00
protected static function booted(): void
{
static::deleting(function ($inspection) {
// Cascada: borrar media asociado cuando se elimina la inspección
$inspection->media()->get()->each->delete();
});
}
const STATUSES = ['pending', 'in_progress', 'completed', 'approved', 'rejected'];
2026-08-28 13:04:28 +02:00
const RESULTS = ['pass', 'fail', 'conditional'];
protected $fillable = [
'project_id', 'layer_id', 'feature_id', 'template_id', 'user_id',
'data', 'status', 'inspector_user_id', 'completed_at', 'result', 'notes',
'uuid', 'client_updated_at',
];
protected $casts = [
2026-08-28 13:04:28 +02:00
'data' => 'array',
'completed_at' => 'datetime',
];
2026-05-07 23:31:33 +02:00
public function project()
{
return $this->belongsTo(Project::class);
}
public function layer()
{
return $this->belongsTo(Layer::class);
}
public function template()
{
return $this->belongsTo(InspectionTemplate::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
public function inspector()
{
return $this->belongsTo(User::class, 'inspector_user_id');
}
public function media()
{
return $this->morphMany(Media::class, 'mediable');
}
public function feature()
{
return $this->belongsTo(Feature::class, 'feature_id');
}
public function issues()
{
return $this->hasMany(Issue::class);
}
2026-08-28 13:04:28 +02:00
public function scopePending($q)
{
return $q->where('status', 'pending');
}
public function scopeCompleted($q)
{
return $q->where('status', 'completed');
}
public function scopeRejected($q)
{
return $q->where('status', 'rejected');
}
public function toSearchableArray(): array
{
return [
'id' => $this->id,
'project_id' => $this->project_id,
'layer_id' => $this->layer_id,
'feature_id' => $this->feature_id,
'template_id' => $this->template_id,
'user_id' => $this->user_id,
'inspector_user_id' => $this->inspector_user_id,
'status' => $this->status,
'result' => $this->result,
'notes' => $this->notes,
];
}
public function getScoutKey(): string
{
return (string) $this->id;
}
2026-05-07 23:31:33 +02:00
}