120 lines
2.7 KiB
PHP
120 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class HorarioBloqueado extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $table = 'horarios_bloqueados';
|
|
|
|
protected $fillable = [
|
|
'fecha',
|
|
'hora_inicio',
|
|
'hora_fin',
|
|
'motivo',
|
|
'activo',
|
|
];
|
|
|
|
protected $casts = [
|
|
'fecha' => 'date',
|
|
'activo' => 'boolean',
|
|
];
|
|
|
|
/**
|
|
* Scope para filtrar horarios activos
|
|
*/
|
|
public function scopeActivo(Builder $query): Builder
|
|
{
|
|
return $query->where('activo', true);
|
|
}
|
|
|
|
/**
|
|
* Scope para filtrar horarios por fecha
|
|
*/
|
|
public function scopePorFecha(Builder $query, $fecha): Builder
|
|
{
|
|
return $query->whereDate('fecha', $fecha);
|
|
}
|
|
|
|
/**
|
|
* Verificar si una fecha y hora está bloqueada
|
|
*/
|
|
public static function estaBloqueado($fecha, $hora): bool
|
|
{
|
|
$horaCarbon = Carbon::parse($hora)->format('H:i:s');
|
|
|
|
return self::whereDate('fecha', $fecha)
|
|
->where('activo', true)
|
|
->whereTime('hora_inicio', '<=', $horaCarbon)
|
|
->whereTime('hora_fin', '>', $horaCarbon)
|
|
->exists();
|
|
}
|
|
|
|
/**
|
|
* Verificar si una fecha tiene bloques activos
|
|
*/
|
|
public static function tieneBloques($fecha): bool
|
|
{
|
|
return self::whereDate('fecha', $fecha)
|
|
->where('activo', true)
|
|
->exists();
|
|
}
|
|
|
|
/**
|
|
* Obtener los bloques activos para una fecha
|
|
*/
|
|
public static function getBloquesPorFecha($fecha)
|
|
{
|
|
return self::whereDate('fecha', $fecha)
|
|
->where('activo', true)
|
|
->orderBy('hora_inicio')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Activar el horario bloqueado
|
|
*/
|
|
public function activar(): bool
|
|
{
|
|
return $this->update(['activo' => true]);
|
|
}
|
|
|
|
/**
|
|
* Desactivar el horario bloqueado
|
|
*/
|
|
public function desactivar(): bool
|
|
{
|
|
return $this->update(['activo' => false]);
|
|
}
|
|
|
|
/**
|
|
* Obtener hora de inicio formateada
|
|
*/
|
|
public function getHoraInicioFormatAttribute(): string
|
|
{
|
|
return Carbon::parse($this->hora_inicio)->format('H:i');
|
|
}
|
|
|
|
/**
|
|
* Obtener hora de fin formateada
|
|
*/
|
|
public function getHoraFinFormatAttribute(): string
|
|
{
|
|
return Carbon::parse($this->hora_fin)->format('H:i');
|
|
}
|
|
|
|
/**
|
|
* Obtener rango de horario formateado
|
|
*/
|
|
public function getRangoHorarioAttribute(): string
|
|
{
|
|
return "{$this->hora_inicio_format} - {$this->hora_fin_format}";
|
|
}
|
|
}
|