public function generarTurnos($agenda_id = NULL) { $errores = true; if (empty($agenda_id)) { $agendas = Agenda::whereNotIn('id', function ($query) { $query->select('agenda_id')->from('turnos'); })->where('habilitado_turnos', '=', 1)->get(); } else { $agendas = Agenda::where('id', $agenda_id)->get(); } foreach ($agendas as $agenda) { $coe = $agenda->centroOdontologoEspecialidad; $horario_desde = date('H:i', strtotime(substr_replace($coe->horario_desde, ':', 2, 0))); $horario_hasta = date('H:i', strtotime(substr_replace($coe->horario_hasta, ':', 2, 0))); $duracion = $coe->duracion_turno; $hora = $horario_desde; $hora_hasta = date('H:i', strtotime("+" . $duracion . " minutes", strtotime($hora))); $turnos = array(); while (strtotime($hora) < strtotime($horario_hasta)) { $turno = array(); $turno["agenda_id"] = $agenda->id; $turno["hora_desde"] = str_replace(":", "", $hora); $turno["hora_hasta"] = str_replace(":", "", $hora_hasta); $turno["tipo_turno"] = 'T'; // turno $turno["estado"] = 'L'; // libre $turno["created_at"] = date("Y-m-d H:i:s"); $turno["updated_at"] = date("Y-m-d H:i:s"); $turnos[] = $turno; $hora = $hora_hasta; $hora_hasta = date('H:i', strtotime("+" . $duracion . " minutes", strtotime($hora))); } $e = Turno::insert($turnos); if (!$e) { $errores = false; } } return $errores; }