'decimal:2', 'montant_arbitre' => 'decimal:2', ]; public function budget(): BelongsTo { return $this->belongsTo(Budget::class); } public function commune(): BelongsTo { return $this->belongsTo(Commune::class); } public function commandes(): HasMany { return $this->hasMany(Commande::class, 'ligne_budget_id'); } public function historique(): HasMany { return $this->hasMany(HistoriqueBudget::class)->latest(); } /** * Calcule le total des commandes réelles liées à cette ligne */ public function getMontantConsommeAttribute(): float { // On somme le montant total des commandes validées ou plus loin return $this->commandes() ->whereIn('statut', ['validee', 'commandee', 'partiellement_recue', 'recue_complete', 'cloturee']) ->sum('montant_ttc') ?? 0; } /** * Calcule le total des commandes en brouillon ou attente */ public function getMontantEngageAttribute(): float { return $this->commandes() ->whereIn('statut', ['brouillon', 'en_attente_validation']) ->sum('montant_ttc') ?? 0; } /** * Calcule le montant restant (Arbitré - Consommé - Engagé). * Retourne 0.0 si aucun montant n'a encore été arbitré. */ public function getMontantDisponibleAttribute(): float { return (float) ($this->montant_arbitre ?? 0) - ($this->montant_consomme + $this->montant_engage); } }