134 lines
2.9 KiB
PHP
134 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Tag extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
// Constantes de status
|
|
const STATUS_AVAILABLE = 'available';
|
|
const STATUS_ASSIGNED = 'assigned';
|
|
const STATUS_CANCELLED = 'cancelled';
|
|
const STATUS_DAMAGED = 'damaged';
|
|
|
|
protected $fillable = [
|
|
'folio',
|
|
'tag_number',
|
|
'vehicle_id',
|
|
'package_id',
|
|
'module_id',
|
|
'status_id',
|
|
];
|
|
|
|
public function vehicle()
|
|
{
|
|
return $this->belongsTo(Vehicle::class);
|
|
}
|
|
|
|
public function package()
|
|
{
|
|
return $this->belongsTo(Package::class);
|
|
}
|
|
|
|
public function status()
|
|
{
|
|
return $this->belongsTo(CatalogTagStatus::class, 'status_id');
|
|
}
|
|
|
|
public function module(){
|
|
return $this->belongsTo(Module::class, 'module_id');
|
|
}
|
|
|
|
public function vehicleTagLogs()
|
|
{
|
|
return $this->hasMany(VehicleTagLog::class);
|
|
}
|
|
|
|
public function scanHistories()
|
|
{
|
|
return $this->hasMany(ScanHistory::class);
|
|
}
|
|
|
|
public function cancellationLogs()
|
|
{
|
|
return $this->hasMany(TagCancellationLog::class);
|
|
}
|
|
|
|
/**
|
|
* Marcar tag como asignado a un vehículo
|
|
*/
|
|
public function markAsAssigned(int $vehicleId, string $folio): void
|
|
{
|
|
$statusAssigned = CatalogTagStatus::where('code', self::STATUS_ASSIGNED)->first();
|
|
|
|
$this->update([
|
|
'vehicle_id' => $vehicleId,
|
|
'folio' => $folio,
|
|
'status_id' => $statusAssigned->id,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Marcar tag como cancelado
|
|
*/
|
|
public function markAsCancelled(): void
|
|
{
|
|
$statusCancelled = CatalogTagStatus::where('code', self::STATUS_CANCELLED)->first();
|
|
|
|
$this->update([
|
|
'status_id' => $statusCancelled->id,
|
|
'vehicle_id' => null,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Marcar tag como dañado
|
|
*/
|
|
public function markAsDamaged(): void
|
|
{
|
|
$statusDamaged = CatalogTagStatus::where('code', self::STATUS_DAMAGED)->first();
|
|
|
|
$this->update([
|
|
'status_id' => $statusDamaged->id,
|
|
'vehicle_id' => null,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Verificar si el tag está disponible
|
|
*/
|
|
public function isAvailable(): bool
|
|
{
|
|
return $this->status->code === self::STATUS_AVAILABLE;
|
|
}
|
|
|
|
/**
|
|
* Verificar si el tag está asignado
|
|
*/
|
|
public function isAssigned(): bool
|
|
{
|
|
return $this->status->code === self::STATUS_ASSIGNED;
|
|
}
|
|
|
|
/**
|
|
* Verificar si el tag está cancelado
|
|
*/
|
|
public function isCancelled(): bool
|
|
{
|
|
return $this->status->code === self::STATUS_CANCELLED;
|
|
}
|
|
|
|
/**
|
|
* Verificar si el tag está dañado
|
|
*/
|
|
public function isDamaged(): bool
|
|
{
|
|
return $this->status->code === self::STATUS_DAMAGED;
|
|
}
|
|
|
|
}
|