Files

106 lines
3.4 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Livewire\Reports;
use App\Models\Inspection;
2026-08-28 13:04:28 +02:00
use App\Models\Phase;
use App\Models\Project;
use Carbon\Carbon;
2026-08-28 13:04:28 +02:00
use Livewire\Attributes\Layout;
use Livewire\Component;
#[Layout('layouts.app')]
class ReportsDashboard extends Component
{
public $dateRange = 'month'; // week, month, quarter, year
2026-08-28 13:04:28 +02:00
public $chartData = [];
2026-08-28 13:04:28 +02:00
public function mount()
{
$this->loadChartData();
}
2026-08-28 13:04:28 +02:00
public function loadChartData()
{
// Project progress over time (last 6 months)
2026-08-28 13:04:28 +02:00
$projects = Project::with(['phases' => function ($query) {
$query->select('project_id', 'progress_percent', 'updated_at');
}])->get();
2026-08-28 13:04:28 +02:00
// Simulate monthly progress data (since we don't have historical stored)
// In a real app, we'd have a progress_history table or similar
$months = [];
$current = Carbon::now();
for ($i = 5; $i >= 0; $i--) {
$month = $current->copy()->subMonths($i);
$months[] = $month->format('M Y');
}
2026-08-28 13:04:28 +02:00
$projectProgress = [];
foreach ($projects as $project) {
$progressData = [];
foreach ($months as $month) {
// For demo, we'll use current progress with some variation
$avgProgress = $project->phases->avg('progress_percent') ?? 0;
// Add some random variation for demo purposes
$variation = rand(-10, 10);
$progress = max(0, min(100, $avgProgress + $variation));
$progressData[] = round($progress);
}
$projectProgress[] = [
'name' => $project->name,
2026-08-28 13:04:28 +02:00
'data' => $progressData,
];
}
2026-08-28 13:04:28 +02:00
// Inspections by type (last 6 months)
$inspections = Inspection::with(['template', 'feature'])
->whereDate('created_at', '>=', Carbon::now()->subMonths(6))
->get();
2026-08-28 13:04:28 +02:00
$inspectionTypes = $inspections->groupBy(function ($inspection) {
return $inspection->template ? $inspection->template->name : 'Sin plantilla';
2026-08-28 13:04:28 +02:00
})->map(function ($group) {
return $group->count();
});
2026-08-28 13:04:28 +02:00
// Projects by status
$projectsByStatus = Project::selectRaw('status, count(*) as count')
->groupBy('status')
->pluck('count', 'status')
->toArray();
2026-08-28 13:04:28 +02:00
// Average phase progress by project
$projectPhaseProgress = Project::with(['phases'])
->get()
2026-08-28 13:04:28 +02:00
->map(function ($project) {
return [
'name' => $project->name,
2026-08-28 13:04:28 +02:00
'progress' => $project->phases->avg('progress_percent') ?? 0,
];
});
2026-08-28 13:04:28 +02:00
$this->chartData = [
'months' => $months,
'projectProgress' => $projectProgress,
'inspectionTypes' => [
'labels' => $inspectionTypes->keys()->toArray(),
2026-08-28 13:04:28 +02:00
'data' => $inspectionTypes->values()->toArray(),
],
'projectsByStatus' => [
2026-08-28 13:04:28 +02:00
'labels' => array_map(function ($status) {
return ucfirst(str_replace('_', ' ', $status));
}, array_keys($projectsByStatus)),
2026-08-28 13:04:28 +02:00
'data' => array_values($projectsByStatus),
],
2026-08-28 13:04:28 +02:00
'projectPhaseProgress' => $projectPhaseProgress,
];
}
2026-08-28 13:04:28 +02:00
public function render()
{
return view('livewire.reports.reports-dashboard');
}
}