7609d35287
- Laravel 12 sur PHP 8.5, Breeze (Blade/Tailwind/Alpine.js) - Docker Compose dev (PostgreSQL 18 + Redis) et prod (stack complète + nginx) - Migrations et models : lieux, sections, dépôts, source_types/fields, sources, relevés - Colonne JSONB data sur releves avec colonnes générées indexées (nom, prenom, date_evenement) - Index GIN pour la recherche fulltext - Enums : UserRole, SourceStatus (avec transitions), CalendarType, FieldType - RoleMiddleware (alias `role`) + helpers isAdmin/isSectionManager sur User - CRUD Lieux (arbre hiérarchique, calcul nom_long en cascade) - CRUD admin : Sections (+ gestion membres), Dépôts, Types de sources (+ champs dynamiques, drag & drop) - CRUD Sources : visibilité filtrée par rôle, assignation membres, workflow de statut Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
64 lines
1.7 KiB
PHP
64 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\SourceStatus;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Source extends Model
|
|
{
|
|
protected $fillable = ['nom', 'description', 'source_type_id', 'depot_id', 'cote', 'auteur', 'status'];
|
|
|
|
protected $casts = [
|
|
'status' => SourceStatus::class,
|
|
];
|
|
|
|
public function sourceType(): BelongsTo
|
|
{
|
|
return $this->belongsTo(SourceType::class);
|
|
}
|
|
|
|
public function depot(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Depot::class);
|
|
}
|
|
|
|
public function membres(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(User::class, 'source_user');
|
|
}
|
|
|
|
public function releves(): HasMany
|
|
{
|
|
return $this->hasMany(Releve::class);
|
|
}
|
|
|
|
public function isVisibleBy(User $user): bool
|
|
{
|
|
if ($user->isAdmin() || $user->isSectionManager()) {
|
|
return true;
|
|
}
|
|
if ($this->status === SourceStatus::Termine) {
|
|
return true;
|
|
}
|
|
return $this->membres()->where('user_id', $user->id)->exists();
|
|
}
|
|
|
|
public function canTransitionTo(SourceStatus $new, User $user): bool
|
|
{
|
|
if (! in_array($new, $this->status->transitions())) {
|
|
return false;
|
|
}
|
|
|
|
return match ($new) {
|
|
SourceStatus::AValider => $user->isAdmin() || $user->isSectionManager()
|
|
|| $this->membres()->where('user_id', $user->id)->exists(),
|
|
SourceStatus::Termine, SourceStatus::EnCours => $user->isAdmin() || $user->isSectionManager(),
|
|
default => false,
|
|
};
|
|
}
|
|
}
|