114 lines
3.0 KiB
PHP
114 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class PieceJointe extends Model
|
|
{
|
|
public $timestamps = false;
|
|
|
|
protected $table = 'pieces_jointes';
|
|
|
|
protected $fillable = [
|
|
'commande_id', 'contrat_id', 'user_id', 'type',
|
|
'nom_original', 'chemin', 'mime_type', 'taille', 'description',
|
|
];
|
|
|
|
protected $casts = [
|
|
'taille' => 'integer',
|
|
'created_at' => 'datetime',
|
|
];
|
|
|
|
const TYPES_LABELS = [
|
|
'devis' => 'Devis',
|
|
'bon_commande' => 'Bon de commande',
|
|
'bon_livraison' => 'Bon de livraison',
|
|
'facture' => 'Facture',
|
|
'contrat' => 'Contrat',
|
|
'avenant' => 'Avenant',
|
|
'autre' => 'Autre',
|
|
];
|
|
|
|
const TYPES_ICONES = [
|
|
'devis' => '📋',
|
|
'bon_commande' => '🛒',
|
|
'bon_livraison' => '📦',
|
|
'facture' => '🧾',
|
|
'contrat' => '📄',
|
|
'avenant' => '📑',
|
|
'autre' => '📎',
|
|
];
|
|
|
|
const TYPES_COULEURS = [
|
|
'devis' => 'purple',
|
|
'bon_commande' => 'blue',
|
|
'bon_livraison' => 'orange',
|
|
'facture' => 'green',
|
|
'contrat' => 'indigo',
|
|
'avenant' => 'teal',
|
|
'autre' => 'gray',
|
|
];
|
|
|
|
// -----------------------------------------------------------------------
|
|
// Relations
|
|
// -----------------------------------------------------------------------
|
|
|
|
public function commande(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Commande::class);
|
|
}
|
|
|
|
public function contrat(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Contrat::class);
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
// -----------------------------------------------------------------------
|
|
// Accesseurs
|
|
// -----------------------------------------------------------------------
|
|
|
|
public function getTypeLabelAttribute(): string
|
|
{
|
|
return self::TYPES_LABELS[$this->type] ?? $this->type;
|
|
}
|
|
|
|
public function getTailleFormatteeAttribute(): string
|
|
{
|
|
$taille = $this->taille;
|
|
if ($taille < 1024) {
|
|
return $taille . ' o';
|
|
} elseif ($taille < 1024 * 1024) {
|
|
return round($taille / 1024, 1) . ' Ko';
|
|
}
|
|
return round($taille / (1024 * 1024), 1) . ' Mo';
|
|
}
|
|
|
|
public function getEstImageAttribute(): bool
|
|
{
|
|
return str_starts_with($this->mime_type, 'image/');
|
|
}
|
|
|
|
public function getEstPdfAttribute(): bool
|
|
{
|
|
return $this->mime_type === 'application/pdf';
|
|
}
|
|
|
|
// -----------------------------------------------------------------------
|
|
// Méthodes
|
|
// -----------------------------------------------------------------------
|
|
|
|
public function supprimer(): void
|
|
{
|
|
Storage::disk('private')->delete($this->chemin);
|
|
$this->delete();
|
|
}
|
|
}
|