2026-05-07 23:31:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2026-08-28 13:04:28 +02:00
|
|
|
use App\Traits\LogsActivity;
|
|
|
|
|
use Carbon\Carbon;
|
2026-05-07 23:31:33 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2026-08-24 12:32:08 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
2026-06-16 18:05:53 +02:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2026-05-07 23:31:33 +02:00
|
|
|
|
|
|
|
|
class Feature extends Model
|
|
|
|
|
{
|
2026-08-28 13:04:28 +02:00
|
|
|
use LogsActivity, SoftDeletes;
|
2026-06-16 18:05:53 +02:00
|
|
|
|
|
|
|
|
const STATUSES = ['planned', 'started', 'in_progress', 'completed', 'verified'];
|
|
|
|
|
|
2026-05-07 23:31:33 +02:00
|
|
|
protected $fillable = [
|
2026-06-25 16:12:47 +02:00
|
|
|
'layer_id', 'name', 'geometry', 'properties', 'template_id', 'feature_type_id',
|
|
|
|
|
'progress', 'status', 'is_active', 'responsible', 'responsible_user_id',
|
2026-06-18 10:09:34 +02:00
|
|
|
'uuid', 'client_updated_at',
|
2026-08-24 12:32:08 +02:00
|
|
|
'planned_start', 'planned_end', 'actual_start', 'actual_end',
|
|
|
|
|
'baseline_start', 'baseline_end',
|
2026-05-07 23:31:33 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
2026-08-28 13:04:28 +02:00
|
|
|
'geometry' => 'array',
|
|
|
|
|
'properties' => 'array',
|
|
|
|
|
'is_active' => 'boolean',
|
|
|
|
|
'planned_start' => 'date',
|
|
|
|
|
'planned_end' => 'date',
|
|
|
|
|
'actual_start' => 'date',
|
|
|
|
|
'actual_end' => 'date',
|
|
|
|
|
'baseline_start' => 'date',
|
|
|
|
|
'baseline_end' => 'date',
|
2026-05-07 23:31:33 +02:00
|
|
|
];
|
|
|
|
|
|
2026-06-25 16:12:47 +02:00
|
|
|
public function featureType()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(FeatureType::class);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-07 23:31:33 +02:00
|
|
|
public function layer()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Layer::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function template()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(InspectionTemplate::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function inspections()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Inspection::class, 'feature_id');
|
|
|
|
|
}
|
2026-05-09 22:28:20 +02:00
|
|
|
|
2026-06-16 18:05:53 +02:00
|
|
|
public function issues()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Issue::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function responsibleUser()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'responsible_user_id');
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-09 22:28:20 +02:00
|
|
|
public function media()
|
|
|
|
|
{
|
|
|
|
|
return $this->morphMany(Media::class, 'mediable');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function images()
|
|
|
|
|
{
|
|
|
|
|
return $this->morphMany(Media::class, 'mediable')->where('category', 'image');
|
|
|
|
|
}
|
2026-06-16 18:05:53 +02:00
|
|
|
|
2026-08-24 12:32:08 +02:00
|
|
|
public function progressSnapshots(): MorphMany
|
|
|
|
|
{
|
|
|
|
|
return $this->morphMany(ProgressSnapshot::class, 'trackable');
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-16 18:05:53 +02:00
|
|
|
public function getStatusColorAttribute(): string
|
|
|
|
|
{
|
2026-08-28 13:04:28 +02:00
|
|
|
return match ($this->status) {
|
|
|
|
|
'planned' => '#6b7280',
|
|
|
|
|
'started' => '#3b82f6',
|
2026-06-16 18:05:53 +02:00
|
|
|
'in_progress' => '#f59e0b',
|
2026-08-28 13:04:28 +02:00
|
|
|
'completed' => '#10b981',
|
|
|
|
|
'verified' => '#8b5cf6',
|
|
|
|
|
default => '#6b7280',
|
2026-06-16 18:05:53 +02:00
|
|
|
};
|
|
|
|
|
}
|
2026-08-24 12:32:08 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Deviation in days: positive = delayed, negative = early
|
|
|
|
|
*/
|
|
|
|
|
public function getDeviationDaysAttribute(): ?int
|
|
|
|
|
{
|
|
|
|
|
if (! $this->planned_end) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$end = $this->actual_end ?? now()->toDateString();
|
2026-08-28 13:04:28 +02:00
|
|
|
|
2026-08-24 12:32:08 +02:00
|
|
|
return $this->planned_end->diffInDays($end, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getStartDeviationDaysAttribute(): ?int
|
|
|
|
|
{
|
|
|
|
|
if (! $this->planned_start) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$start = $this->actual_start ?? now()->toDateString();
|
2026-08-28 13:04:28 +02:00
|
|
|
|
2026-08-24 12:32:08 +02:00
|
|
|
return $this->planned_start->diffInDays($start, false);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-28 13:04:28 +02:00
|
|
|
public function getPlannedProgressAtAttribute(?Carbon $date = null): float
|
2026-08-24 12:32:08 +02:00
|
|
|
{
|
|
|
|
|
$date = $date ?? now();
|
2026-08-28 13:04:28 +02:00
|
|
|
|
2026-08-24 12:32:08 +02:00
|
|
|
if (! $this->planned_start || ! $this->planned_end) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2026-08-28 13:04:28 +02:00
|
|
|
|
2026-08-24 12:32:08 +02:00
|
|
|
if ($date <= $this->planned_start) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if ($date >= $this->planned_end) {
|
|
|
|
|
return 100;
|
|
|
|
|
}
|
2026-08-28 13:04:28 +02:00
|
|
|
|
2026-08-24 12:32:08 +02:00
|
|
|
$totalDays = $this->planned_start->diffInDays($this->planned_end);
|
|
|
|
|
$elapsedDays = $this->planned_start->diffInDays($date);
|
2026-08-28 13:04:28 +02:00
|
|
|
|
2026-08-24 12:32:08 +02:00
|
|
|
return round(($elapsedDays / $totalDays) * 100, 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getSpiAttribute(): ?float
|
|
|
|
|
{
|
|
|
|
|
$pv = $this->planned_progress_at;
|
|
|
|
|
$ev = $this->progress;
|
2026-08-28 13:04:28 +02:00
|
|
|
|
2026-08-24 12:32:08 +02:00
|
|
|
if ($pv <= 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2026-08-28 13:04:28 +02:00
|
|
|
|
2026-08-24 12:32:08 +02:00
|
|
|
return round($ev / $pv, 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getIsOnTrackAttribute(): ?bool
|
|
|
|
|
{
|
|
|
|
|
$spi = $this->spi;
|
|
|
|
|
if ($spi === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2026-08-28 13:04:28 +02:00
|
|
|
|
2026-08-24 12:32:08 +02:00
|
|
|
return $spi >= 0.95;
|
|
|
|
|
}
|
2026-06-16 18:05:53 +02:00
|
|
|
}
|