124 lines
2.9 KiB
PHP
124 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Carbon\Carbon;
|
|
|
|
class Hardware extends Model
|
|
{
|
|
protected $table = 'hardwares';
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'type',
|
|
'brand',
|
|
'model',
|
|
'serial_number',
|
|
'status',
|
|
'purchase_date',
|
|
'commissioning_date',
|
|
'warranty_expiration_date',
|
|
'location',
|
|
'ip_address',
|
|
'order_id',
|
|
'notes',
|
|
];
|
|
|
|
protected $casts = [
|
|
'purchase_date' => 'date',
|
|
'commissioning_date' => 'date',
|
|
'warranty_expiration_date' => 'date',
|
|
];
|
|
|
|
protected $appends = [
|
|
'is_under_warranty',
|
|
'warranty_status_label',
|
|
'warranty_remaining_days',
|
|
];
|
|
|
|
/**
|
|
* Relation avec la commande d'achat.
|
|
*/
|
|
public function order()
|
|
{
|
|
return $this->belongsTo(Order::class);
|
|
}
|
|
|
|
/**
|
|
* Accesseur : l'équipement est-il sous garantie ?
|
|
*/
|
|
public function getIsUnderWarrantyAttribute(): bool
|
|
{
|
|
if (!$this->warranty_expiration_date) {
|
|
return false;
|
|
}
|
|
|
|
return $this->warranty_expiration_date->isAfter(Carbon::today());
|
|
}
|
|
|
|
/**
|
|
* Accesseur : label en français du statut de garantie.
|
|
*/
|
|
public function getWarrantyStatusLabelAttribute(): string
|
|
{
|
|
if (!$this->warranty_expiration_date) {
|
|
return 'Non spécifiée';
|
|
}
|
|
|
|
if ($this->is_under_warranty) {
|
|
$days = $this->warranty_remaining_days;
|
|
return "Sous garantie ($days j. restants)";
|
|
}
|
|
|
|
return 'Garantie expirée';
|
|
}
|
|
|
|
/**
|
|
* Accesseur : nombre de jours de garantie restants.
|
|
*/
|
|
public function getWarrantyRemainingDaysAttribute(): ?int
|
|
{
|
|
if (!$this->warranty_expiration_date) {
|
|
return null;
|
|
}
|
|
|
|
if ($this->warranty_expiration_date->isBefore(Carbon::today())) {
|
|
return 0;
|
|
}
|
|
|
|
return (int) Carbon::today()->diffInDays($this->warranty_expiration_date);
|
|
}
|
|
|
|
/**
|
|
* Scope : Moteur de recherche multicritère
|
|
*/
|
|
public function scopeSearch($query, $search)
|
|
{
|
|
return $query->where(function ($q) use ($search) {
|
|
$q->where('name', 'like', "%{$search}%")
|
|
->orWhere('brand', 'like', "%{$search}%")
|
|
->orWhere('model', 'like', "%{$search}%")
|
|
->orWhere('serial_number', 'like', "%{$search}%")
|
|
->orWhere('location', 'like', "%{$search}%")
|
|
->orWhere('ip_address', 'like', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Scope : Filtrer par type
|
|
*/
|
|
public function scopeByType($query, $type)
|
|
{
|
|
return $query->where('type', $type);
|
|
}
|
|
|
|
/**
|
|
* Scope : Filtrer par statut
|
|
*/
|
|
public function scopeByStatus($query, $status)
|
|
{
|
|
return $query->where('status', $status);
|
|
}
|
|
}
|