79 lines
2.5 KiB
PHP
79 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Reports;
|
|
|
|
use App\Enums\ReportPeriodStatus;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Reports\StoreReportPeriodRequest;
|
|
use App\Models\ReportPeriod;
|
|
use App\Models\Team;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class ReportPeriodController extends Controller
|
|
{
|
|
/**
|
|
* Display the report periods page.
|
|
*/
|
|
public function index(Request $request, Team $current_team): Response
|
|
{
|
|
$periods = ReportPeriod::query()
|
|
->whereBelongsTo($current_team)
|
|
->orderByDesc('year')
|
|
->orderByDesc('month')
|
|
->get();
|
|
|
|
return Inertia::render('reports/periods/Index', [
|
|
'periods' => $periods->map(fn (ReportPeriod $period) => [
|
|
'id' => $period->id,
|
|
'year' => $period->year,
|
|
'month' => $period->month,
|
|
'label' => $period->label(),
|
|
'status' => $period->status->value,
|
|
'statusLabel' => $period->status->label(),
|
|
'isEditable' => $period->isEditable(),
|
|
'updatedAt' => $period->updated_at?->toIso8601String(),
|
|
])->values(),
|
|
'months' => collect(range(1, 12))->map(fn (int $month) => [
|
|
'value' => $month,
|
|
'label' => (new ReportPeriod([
|
|
'month' => $month,
|
|
'year' => now()->year,
|
|
'status' => ReportPeriodStatus::Draft,
|
|
]))->label(),
|
|
])->all(),
|
|
'years' => collect(range((int) now()->year - 1, (int) now()->year + 2))->values()->all(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created report period.
|
|
*/
|
|
public function store(StoreReportPeriodRequest $request, Team $current_team): RedirectResponse
|
|
{
|
|
ReportPeriod::create([
|
|
...$request->validated(),
|
|
'team_id' => $current_team->id,
|
|
'status' => ReportPeriodStatus::Draft,
|
|
]);
|
|
|
|
return to_route('reports.periods.index', $current_team);
|
|
}
|
|
|
|
/**
|
|
* Approve the given report period.
|
|
*/
|
|
public function approve(Team $current_team, ReportPeriod $report_period): RedirectResponse
|
|
{
|
|
abort_unless($report_period->team?->is($current_team), 404);
|
|
|
|
$report_period->update([
|
|
'status' => ReportPeriodStatus::Approved,
|
|
]);
|
|
|
|
return to_route('reports.periods.index', $current_team);
|
|
}
|
|
}
|