117 lines
3.9 KiB
PHP
117 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Actions\Integration\CreateIntegrationAction;
|
|
use App\Models\IntegrationRequest;
|
|
use App\Models\IntegrationTemplate;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
|
|
class IntegrationController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
return Inertia::render('Integration/Index', [
|
|
'integrations' => IntegrationRequest::with(['agent', 'template'])->latest()->get(),
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return Inertia::render('Integration/Create', [
|
|
'templates' => IntegrationTemplate::where('is_active', true)->get(),
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request, CreateIntegrationAction $action)
|
|
{
|
|
$validated = $request->validate([
|
|
'first_name' => 'required|string|max:255',
|
|
'last_name' => 'required|string|max:255',
|
|
'email' => 'required|email|unique:agents,email',
|
|
'position' => 'required|string|max:255',
|
|
'department' => 'required|string|max:255',
|
|
'arrival_date' => 'required|date',
|
|
'template_id' => 'nullable|exists:integration_templates,id',
|
|
]);
|
|
|
|
$integration = $action->execute($validated);
|
|
|
|
return redirect()->route('integrations.show', $integration);
|
|
}
|
|
|
|
public function show(IntegrationRequest $integration)
|
|
{
|
|
$user = auth()->user();
|
|
|
|
$serviceTaskConstraint = function ($query) use ($user) {
|
|
// Admin, RH, and Prescripteur can see all tasks (global progress)
|
|
if (!$user->hasRole(['Admin', 'RH', 'Prescripteur'])) {
|
|
$query->whereHas('service', function ($q) use ($user) {
|
|
$q->whereIn('name', $user->getRoleNames());
|
|
});
|
|
}
|
|
};
|
|
|
|
$integration->load([
|
|
'agent',
|
|
'template',
|
|
'serviceTasks' => $serviceTaskConstraint,
|
|
'serviceTasks.service',
|
|
'serviceTasks.integrationRequest',
|
|
'serviceTasks.taskItems',
|
|
'serviceTasks.comments.user',
|
|
'serviceTasks.attachments',
|
|
'activities.causer',
|
|
'comments.user'
|
|
]);
|
|
|
|
$taskActivities = \Spatie\Activitylog\Models\Activity::where('subject_type', \App\Models\ServiceTask::class)
|
|
->whereIn('subject_id', $integration->serviceTasks->pluck('id'))
|
|
->with('causer')
|
|
->latest()
|
|
->get();
|
|
|
|
return Inertia::render('Integration/Show', [
|
|
'integration' => $integration,
|
|
'activities' => $integration->activities->merge($taskActivities)->sortByDesc('created_at')->values(),
|
|
]);
|
|
}
|
|
|
|
public function validateRH(IntegrationRequest $integration, \App\Services\IntegrationService $service)
|
|
{
|
|
$this->authorize('update', $integration);
|
|
|
|
$service->validateRH($integration);
|
|
|
|
return back()->with('success', 'Intégration validée par les RH. Les services ont été notifiés.');
|
|
}
|
|
|
|
public function downloadPdf(IntegrationRequest $integration)
|
|
{
|
|
if ($integration->status !== \App\Enums\IntegrationStatus::Completed) {
|
|
abort(403, 'Le téléchargement de la fiche agent n\'est possible que lorsque le processus est terminé.');
|
|
}
|
|
|
|
// Load relationships needed for the PDF
|
|
$integration->load([
|
|
'agent',
|
|
'template',
|
|
'serviceTasks.service',
|
|
'serviceTasks.taskItems.completedBy'
|
|
]);
|
|
|
|
$pdf = \Barryvdh\DomPDF\Facade\Pdf::loadView('pdf.integration', [
|
|
'integration' => $integration,
|
|
'agent' => $integration->agent,
|
|
'date' => now()->format('d/m/Y'),
|
|
]);
|
|
|
|
// Define paper size and orientation if needed, e.g., A4 portrait
|
|
$pdf->setPaper('a4', 'portrait');
|
|
|
|
return $pdf->download("Fiche_Agent_{$integration->agent->last_name}_{$integration->agent->first_name}.pdf");
|
|
}
|
|
}
|