Initial commit with contrats and domaines modules
This commit is contained in:
75
app/Models/Contrat.php
Normal file
75
app/Models/Contrat.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Contrat extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'titre',
|
||||
'description',
|
||||
'fournisseur_id',
|
||||
'service_id',
|
||||
'date_debut',
|
||||
'date_echeance',
|
||||
'statut',
|
||||
'montant',
|
||||
'preavis_jours',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'date_debut' => 'date',
|
||||
'date_echeance' => 'date',
|
||||
'montant' => 'decimal:2',
|
||||
];
|
||||
|
||||
const STATUTS_LABELS = [
|
||||
'actif' => 'Actif',
|
||||
'a_renouveler' => 'À renouveler',
|
||||
'expire' => 'Expiré',
|
||||
'resilie' => 'Résilié',
|
||||
];
|
||||
|
||||
public function fournisseur(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Fournisseur::class);
|
||||
}
|
||||
|
||||
public function service(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Service::class);
|
||||
}
|
||||
|
||||
public function piecesJointes(): HasMany
|
||||
{
|
||||
return $this->hasMany(PieceJointe::class)->orderByDesc('created_at');
|
||||
}
|
||||
|
||||
// Un contrat est considéré "proche d'expiration" si l'échéance est dans moins de 30 jours (ou selon son préavis)
|
||||
public function getEstProcheEcheanceAttribute(): bool
|
||||
{
|
||||
if (!$this->date_echeance || in_array($this->statut, ['resilie', 'expire'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$joursAvantAlerte = $this->preavis_jours > 0 ? $this->preavis_jours + 15 : 30;
|
||||
|
||||
return Carbon::now()->diffInDays($this->date_echeance, false) <= $joursAvantAlerte;
|
||||
}
|
||||
|
||||
public function getEstEnRetardAttribute(): bool
|
||||
{
|
||||
if (!$this->date_echeance || $this->statut === 'resilie') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Carbon::now()->isAfter($this->date_echeance);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user