Files
dsi-commander/app/Models/LigneCommande.php

61 lines
1.7 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class LigneCommande extends Model
{
protected $table = 'lignes_commande';
protected $fillable = [
'commande_id', 'article_id', 'categorie_id',
'designation', 'reference', 'quantite', 'quantite_recue',
'unite', 'prix_unitaire_ht', 'taux_tva',
'montant_ht', 'montant_ttc', 'notes', 'ordre',
];
protected $casts = [
'quantite' => 'decimal:3',
'quantite_recue' => 'decimal:3',
'prix_unitaire_ht' => 'decimal:2',
'taux_tva' => 'decimal:2',
'montant_ht' => 'decimal:2',
'montant_ttc' => 'decimal:2',
];
protected static function booted(): void
{
static::saving(function (LigneCommande $ligne) {
if ($ligne->prix_unitaire_ht !== null && $ligne->quantite !== null) {
$ligne->montant_ht = round((float) $ligne->quantite * (float) $ligne->prix_unitaire_ht, 2);
$ligne->montant_ttc = round((float) $ligne->montant_ht * (1 + (float) $ligne->taux_tva / 100), 2);
}
});
static::saved(function (LigneCommande $ligne) {
$ligne->commande?->recalculerMontants();
});
static::deleted(function (LigneCommande $ligne) {
$ligne->commande?->recalculerMontants();
});
}
public function commande(): BelongsTo
{
return $this->belongsTo(Commande::class);
}
public function article(): BelongsTo
{
return $this->belongsTo(Article::class);
}
public function categorie(): BelongsTo
{
return $this->belongsTo(Categorie::class);
}
}