124 lines
3.7 KiB
PHP
124 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Profesional;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\LogSeguridadController;
|
|
|
|
class ProfesionalController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(): JsonResponse
|
|
{
|
|
$items = Profesional::all();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $items,
|
|
'message' => 'Registros obtenidos correctamente',
|
|
], 200);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
// 1. Validamos los datos de entrada
|
|
$validated = $request->validate([
|
|
'inicio' => 'required|date_format:Y-m-d H:i:s',
|
|
'correo' => 'nullable|email|max:255',
|
|
'nombrecompleto' => 'nullable|string|max:255',
|
|
'descripcion' => 'required|string',
|
|
'cliente_id' => 'nullable|exists:clientes,id',
|
|
'estadoturno_id' => 'required|exists:estadosturnos,id',
|
|
'agenda_id' => 'required|exists:agendas,id',
|
|
'profesional_id' => 'required|exists:profesionales,id',
|
|
'servicio_id' => 'required|exists:servicios,id',
|
|
'modalidad_id' => 'required|exists:modalidades,id',
|
|
]);
|
|
// 2. Verificamos si el profesional ya tiene un turno en ese "inicio"
|
|
$existeTurno = Turno::where('profesional_id', $validated['profesional_id'])
|
|
->where('inicio', $validated['inicio'])
|
|
->whereIn('estadoturno_id', [1, 2]) // 1: Pendiente, 2: Confirmado
|
|
->exists();
|
|
|
|
if ($existeTurno) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Horario no disponible.',
|
|
], 422); // Error de validación lógica
|
|
}
|
|
|
|
$turno = Turno::create($validated);
|
|
|
|
// 3. Registramos el evento en el log de seguridad
|
|
$personaId = auth()->check() ? auth()->user()->persona->id : null;
|
|
LogSeguridadController::registrarAccion('Creacion de turno ID: ' . $turno->id, 'Profesional', 17, $personaId);
|
|
|
|
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $turno,
|
|
'message' => 'Turno agendado correctamente para el ' . $turno->inicio->format('d/m/Y H:i'),
|
|
], 201);
|
|
|
|
}
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(Profesional $profesional): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $profesional,
|
|
'message' => 'Registro obtenido correctamente',
|
|
], 200);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, Profesional $profesional): JsonResponse
|
|
{
|
|
$payload = $request->only((new Profesional())->getFillable());
|
|
$profesional->update($payload);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $profesional,
|
|
'message' => 'Registro actualizado correctamente',
|
|
], 200);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Profesional $profesional): JsonResponse
|
|
{
|
|
$profesional->delete();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Registro eliminado correctamente',
|
|
], 200);
|
|
}
|
|
|
|
public function aceptarFormulario(Formulario $formulario): JsonResponse
|
|
{
|
|
$turno->update(['estadoturno_id' => 2]);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Turno aceptado correctamente',
|
|
], 200);
|
|
}
|
|
} |