feat(reports): complete reporting system with deviations, progress curves, multi-format export

- Added planned/actual/baseline dates to Phase and Feature models
- Created ProgressSnapshot model + migration for historical tracking
- Implemented CaptureDailyProgressSnapshot job (scheduled daily at 06:30)
- Added DeviationCalculator logic (SPI, planned progress, deviation days)
- ReportGenerator service with complete data aggregation
- ReportController with builder, preview, generate (HTML/Excel)
- ReportBuilder Livewire component with date range, entity selection, format
- ProjectReportExport with 10 sheets (Summary, Phases, Features, Inspections, Issues, Tasks, Deviations, Media, Progress Curve, Parameters)
- Blade partials for all sections (header, summary, phases, features, inspections, issues, tasks, media, deviations, progress curve)
- New routes: /projects/{project}/reports/*
- Added 'generate reports' permission
- All 101 tests passing
This commit is contained in:
Javier Braña
2026-08-24 12:32:08 +02:00
parent c58a49f22d
commit ee32544525
30 changed files with 3170 additions and 7 deletions
+82 -3
View File
@@ -3,6 +3,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Traits\LogsActivity;
@@ -16,12 +17,20 @@ class Feature extends Model
'layer_id', 'name', 'geometry', 'properties', 'template_id', 'feature_type_id',
'progress', 'status', 'is_active', 'responsible', 'responsible_user_id',
'uuid', 'client_updated_at',
'planned_start', 'planned_end', 'actual_start', 'actual_end',
'baseline_start', 'baseline_end',
];
protected $casts = [
'geometry' => 'array',
'properties' => 'array',
'is_active' => 'boolean',
'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',
];
public function featureType()
@@ -64,6 +73,11 @@ class Feature extends Model
return $this->morphMany(Media::class, 'mediable')->where('category', 'image');
}
public function progressSnapshots(): MorphMany
{
return $this->morphMany(ProgressSnapshot::class, 'trackable');
}
public function getStatusColorAttribute(): string
{
return match($this->status) {
@@ -75,4 +89,69 @@ class Feature extends Model
default => '#6b7280',
};
}
/**
* Deviation in days: positive = delayed, negative = early
*/
public function getDeviationDaysAttribute(): ?int
{
if (! $this->planned_end) {
return null;
}
$end = $this->actual_end ?? now()->toDateString();
return $this->planned_end->diffInDays($end, false);
}
public function getStartDeviationDaysAttribute(): ?int
{
if (! $this->planned_start) {
return null;
}
$start = $this->actual_start ?? now()->toDateString();
return $this->planned_start->diffInDays($start, false);
}
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);
}
public function getSpiAttribute(): ?float
{
$pv = $this->planned_progress_at;
$ev = $this->progress;
if ($pv <= 0) {
return null;
}
return round($ev / $pv, 2);
}
public function getIsOnTrackAttribute(): ?bool
{
$spi = $this->spi;
if ($spi === null) {
return null;
}
return $spi >= 0.95;
}
}
+78 -2
View File
@@ -3,6 +3,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class Phase extends Model
@@ -12,6 +13,7 @@ class Phase extends Model
protected $fillable = [
'project_id', 'name', 'description', 'order', 'color', 'progress_percent',
'planned_start', 'planned_end', 'actual_start', 'actual_end',
'baseline_start', 'baseline_end',
];
protected $casts = [
@@ -19,6 +21,8 @@ class Phase extends Model
'planned_end' => 'date',
'actual_start' => 'date',
'actual_end' => 'date',
'baseline_start' => 'date',
'baseline_end' => 'date',
];
public function project()
@@ -61,13 +65,85 @@ class Phase extends Model
return $this->hasMany(Task::class);
}
public function progressSnapshots(): MorphMany
{
return $this->morphMany(ProgressSnapshot::class, 'trackable');
}
/**
* Deviation in days: positive = delayed, negative = early
*/
public function getDeviationDaysAttribute(): ?int
{
if (! $this->planned_end) {
return null;
}
$end = $this->actual_end ?? now();
$end = $this->actual_end ?? now()->toDateString();
return $this->planned_end->diffInDays($end, false);
}
/**
* 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;
}
}
+79
View File
@@ -0,0 +1,79 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
class ProgressSnapshot extends Model
{
protected $fillable = [
'trackable_type',
'trackable_id',
'snapshot_date',
'progress',
'planned_start',
'planned_end',
'actual_start',
'actual_end',
'method',
'metadata',
];
protected $casts = [
'snapshot_date' => 'date',
'planned_start' => 'date',
'planned_end' => 'date',
'actual_start' => 'date',
'actual_end' => 'date',
'progress' => 'decimal:2',
'metadata' => 'array',
];
public function trackable(): MorphTo
{
return $this->morphTo();
}
/**
* Scope for a specific date range
*/
public function scopeDateRange($query, $from, $to)
{
return $query->whereBetween('snapshot_date', [$from, $to]);
}
/**
* Scope for specific trackable
*/
public function scopeForTrackable($query, $model, $id = null)
{
$query->where('trackable_type', $model);
if ($id) {
$query->where('trackable_id', $id);
}
return $query;
}
/**
* Get latest snapshot for a trackable
*/
public static function latestFor($model, $id): ?self
{
return static::forTrackable($model, $id)
->latest('snapshot_date')
->first();
}
/**
* Get progress trend (last N snapshots)
*/
public static function trendFor($model, $id, int $limit = 10): array
{
return static::forTrackable($model, $id)
->orderBy('snapshot_date')
->take($limit)
->get(['snapshot_date', 'progress', 'method'])
->toArray();
}
}
+5
View File
@@ -138,6 +138,11 @@ class Task extends Model
return $this->morphMany(ActivityLog::class, 'model');
}
public function progressSnapshots(): MorphMany
{
return $this->morphMany(ProgressSnapshot::class, 'trackable');
}
// ============================================================
// Scopes
// ============================================================