Files
construprogress/app/Models/Phase.php
T

150 lines
3.6 KiB
PHP
Raw Normal View History

2026-05-07 23:31:33 +02:00
<?php
2026-05-07 23:31:33 +02:00
namespace App\Models;
2026-05-07 23:31:33 +02:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
2026-05-07 23:31:33 +02:00
class Phase extends Model
{
use SoftDeletes;
2026-05-07 23:31:33 +02:00
protected $fillable = [
'project_id', 'name', 'description', 'order', 'color', 'progress_percent',
'planned_start', 'planned_end', 'actual_start', 'actual_end',
'baseline_start', 'baseline_end',
2026-05-07 23:31:33 +02:00
];
protected $casts = [
'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
public function project()
{
return $this->belongsTo(Project::class);
}
public function layers()
{
return $this->hasMany(Layer::class);
}
public function progressUpdates()
{
return $this->hasMany(ProgressUpdate::class);
}
public function currentLayer()
{
return $this->hasOne(Layer::class)->latestOfMany();
}
public function features()
{
return $this->hasManyThrough(Feature::class, Layer::class);
}
public function media()
{
return $this->morphMany(Media::class, 'mediable');
}
public function images()
{
return $this->morphMany(Media::class, 'mediable')->where('category', 'image');
}
public function tasks()
{
return $this->hasMany(Task::class);
}
2026-05-07 23:31:33 +02:00
public function progressSnapshots(): MorphMany
{
return $this->morphMany(ProgressSnapshot::class, 'trackable');
}
/**
* Deviation in days: positive = delayed, negative = early
*/
public function getDeviationDaysAttribute(): ?int
2026-05-07 23:31:33 +02:00
{
if (! $this->planned_end) {
return null;
}
$end = $this->actual_end ?? now()->toDateString();
return $this->planned_end->diffInDays($end, false);
2026-05-07 23:31:33 +02:00
}
/**
* Start deviation: positive = delayed start, negative = early start
*/
public function getStartDeviationDaysAttribute(): ?int
{
if (! $this->planned_start) {
return null;
}
$start = $this->actual_start ?? now()->toDateString();
return $this->planned_start->diffInDays($start, false);
}
/**
* Planned progress at a given date (linear interpolation)
*/
public function getPlannedProgressAtAttribute(?\Carbon\Carbon $date = null): float
{
$date = $date ?? now();
if (! $this->planned_start || ! $this->planned_end) {
return 0;
}
if ($date <= $this->planned_start) {
return 0;
}
if ($date >= $this->planned_end) {
return 100;
}
$totalDays = $this->planned_start->diffInDays($this->planned_end);
$elapsedDays = $this->planned_start->diffInDays($date);
return round(($elapsedDays / $totalDays) * 100, 2);
}
/**
* Schedule Performance Index (SPI) = EV / PV
*/
public function getSpiAttribute(): ?float
{
$pv = $this->planned_progress_at; // Planned Value
$ev = $this->progress_percent; // Earned Value (current progress)
if ($pv <= 0) {
return null;
}
return round($ev / $pv, 2);
}
/**
* Check if phase is on track (SPI >= 0.95)
*/
public function getIsOnTrackAttribute(): ?bool
{
$spi = $this->spi;
if ($spi === null) {
return null;
}
return $spi >= 0.95;
}
}