Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- id|fecha_inicial |fecha_final |estado_horario_id
- 1|2019-01-01 08:00:00|2019-01-01 09:00:00| 1
- 2|2019-01-01 09:00:00|2019-01-01 10:00:00| 1
- ...
- 330|2019-22-01 22:00:00|2019-22-01 23:00:00| 1
- id|hora_inicio |hora_fin |estado_horario_id
- 331|2019-23-01 08:00:00|2019-23-01 09:00:00| 1
- 332|2019-23-01 09:00:00|2019-23-01 10:00:00| 1
- 333|2019-23-01 10:00:00|2019-23-01 11:00:00| 1
- ...
- 336|2019-23-01 13:00:00|2019-23-01 14:00:00| 2
- 337|2019-23-01 14:00:00|2019-23-01 15:00:00| 2
- ...
- 345|2019-23-01 22:00:00|2019-23-01 23:00:00| 2
- <input type="date" name="horario[{{$last_horario['id']}}][desde]" id="desde" onchange="ModificaEstadoHorario()" />
- <script type="text/javascript">
- function ModificaEstadoHorario(){
- var fechaElegida = new Date(desde.value);
- var actual = new Date();
- if(fechaElegida.toISOString().substring(0, 10) < actual.toISOString().substring(0, 10)){
- document.getElementById('estado_horario_id').value = 'Inactiva';
- }else{
- document.getElementById('estado_horario_id').value = 'Activa';
- }
- }
- <script>
- <input type="text" value="Activa" name="horario[{{$last_horario['id']}}][estado_horario_id]" id="estado_horario_id">
- public function store(Request $request){
- try {
- /*CON EL SIGUIENTE CÓDIGO SE LOGRA GUARDAR MÚLTIPLES HORARIOS*/
- foreach($request->horario as $id_horario=>$row){
- foreach($row['check'] as $check){
- $startdate = $row['desde'];
- $endate = $row['hasta'];
- $period = CarbonPeriod::create($startdate, $endate); // Creamos un objeto "periodo" propio de Carbon
- $period_2 = CarbonPeriod::create($startdate, $endate);
- $dates = $times = []; // Variables para almacenar las fechas y los horarios
- $dates_2 = $times_2 = [];
- $tstart = Carbon::createFromFormat('H:i:s', $row['hora_inicio']); /*Creamos una instancia de carbon para la hora de inicio*/
- $tend = Carbon::createFromFormat('H:i:s', $row['hora_termino']); /*Y otra instancia para la hora de finalización*/
- $tstart_2 = Carbon::createFromFormat('H:i:s', $row['hora_inicio']);
- $tstart_2->modify('+1 hour');
- $tend_2 = Carbon::createFromFormat('H:i:s', $row['hora_termino']);
- while ($tstart < $tend){ // Mientras la hora de inicio sea menor a la hora de finalización
- $times[] = $tstart->format("H:i:s"); // Agregamos esa hora al array de los horarios
- $tstart->addHour(); // Luego aumentamos en 1 hora
- }
- while ($tstart_2 <= $tend_2){
- $times_2[] = $tstart_2->format('H:i:s');
- $tstart_2->addHour();
- }
- foreach($period as $date){ // Recorremos todas las fechas generadas en el periodo
- foreach($times as $time){ // Así como los horarios generados en el while anterior
- $dates[] = $date->format("Y-m-d") . " " . $time; // Y lo concatenamos y guardamos en el array de las fechas
- }
- }
- foreach ($period_2 as $date_2) {
- foreach ($times_2 as $time_2) {
- $dates_2[] = $date_2->format('Y-m-d'). " ".$time_2;
- }
- }
- $cuenta = count($dates);
- //AQUI ES EN DONDE INTENTO ESTABLECER LO DICHO ANTERIORMENTE
- /*QUE SI LA FECHA ESCOGIDA ES MENOR QUE LA ACTUAL EL ESTADO
- DEL HORARIO CAMBIE DE ACTIVO A INACTIVO*/
- /*PERO DE ESTA MANERA ME DEJA TODO EN INACTIVO YA QUE
- COMO EL VALOR CAMBIA SE MANTIENE ASÍ Y NO VUELVE A
- CAMBIAR A - ACTIVO DESPUÉS QUE PASA LA FECHA Y HORA
- ACTUAL*/
- if ($row['estado_horario_id'] == 'Activa') {
- $row['estado_horario_id'] = 2;
- }else if ($row['estado_horario_id'] == 'Inactiva'){
- $row['estado_horario_id'] = 1;
- }
- for($i = 0; $i < $cuenta; $i++){
- $horarios[]= [
- 'hora_incio' => $dates[$i],
- 'hora_fin' => $dates_2[$i],
- 'estado_horario_id' => $row['estado_horario_id'],
- 'cancha_id ' => $check
- ];
- HorariosNew::create(['hora_inicio' => $dates[$i],'hora_fin' => $dates_2[$i],'estado_horario_id' => $row['estado_horario_id'], 'cancha_id' => $check]);
- }
- }
- }
- }catch (IlluminateDatabaseQueryException $e) {
- Session::flash('error', 'Hubo un error desconocido, no se pudo registrar el horario');
- return redirect()->route('horarios.store');
- }
- Session::flash('message', 'Se agregó correctamente el horario');
- return redirect()->route('horarios.store');
- }
Add Comment
Please, Sign In to add comment