69 lines
2.2 KiB
PHP
69 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Policies;
|
|
|
|
use App\Models\Commande;
|
|
use App\Models\User;
|
|
|
|
class CommandePolicy
|
|
{
|
|
public function viewAny(User $user): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function view(User $user, Commande $commande): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function create(User $user): bool
|
|
{
|
|
return $user->hasAnyRole(['admin', 'responsable', 'acheteur']);
|
|
}
|
|
|
|
public function update(User $user, Commande $commande): bool
|
|
{
|
|
if ($user->hasRole('admin')) {
|
|
return true;
|
|
}
|
|
|
|
$statutsEditables = ['brouillon', 'en_attente_validation'];
|
|
if (!in_array($commande->statut, $statutsEditables)) {
|
|
return false;
|
|
}
|
|
|
|
return $user->hasAnyRole(['responsable', 'acheteur'])
|
|
&& ($commande->user_id === $user->id || $commande->service_id === $user->service_id);
|
|
}
|
|
|
|
public function delete(User $user, Commande $commande): bool
|
|
{
|
|
return $user->hasRole('admin');
|
|
}
|
|
|
|
public function transition(User $user, Commande $commande, string $targetStatut): bool
|
|
{
|
|
if (!$commande->peutTransitionnerVers($targetStatut)) {
|
|
return false;
|
|
}
|
|
|
|
if ($user->hasRole('admin')) {
|
|
return true;
|
|
}
|
|
|
|
return match ($targetStatut) {
|
|
'en_attente_validation' => $user->hasAnyRole(['responsable', 'acheteur']) && $commande->user_id === $user->id,
|
|
'brouillon' => $user->hasAnyRole(['responsable', 'acheteur']) && $commande->user_id === $user->id,
|
|
'validee' => $user->hasRole('responsable') && $commande->service_id === $user->service_id,
|
|
'commandee' => $user->hasAnyRole(['responsable', 'acheteur']),
|
|
'partiellement_recue',
|
|
'recue_complete' => $user->hasAnyRole(['responsable', 'acheteur']),
|
|
'cloturee' => $user->hasAnyRole(['responsable', 'acheteur']),
|
|
'annulee' => $user->hasAnyRole(['responsable', 'acheteur'])
|
|
&& !in_array($commande->statut, ['commandee', 'partiellement_recue', 'recue_complete', 'cloturee']),
|
|
default => false,
|
|
};
|
|
}
|
|
}
|