Guest User

Untitled

a guest
Jan 23rd, 2019
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. id|fecha_inicial |fecha_final |estado_horario_id
  2. 1|2019-01-01 08:00:00|2019-01-01 09:00:00| 1
  3. 2|2019-01-01 09:00:00|2019-01-01 10:00:00| 1
  4. ...
  5. 330|2019-22-01 22:00:00|2019-22-01 23:00:00| 1
  6.  
  7. id|hora_inicio |hora_fin |estado_horario_id
  8. 331|2019-23-01 08:00:00|2019-23-01 09:00:00| 1
  9. 332|2019-23-01 09:00:00|2019-23-01 10:00:00| 1
  10. 333|2019-23-01 10:00:00|2019-23-01 11:00:00| 1
  11. ...
  12. 336|2019-23-01 13:00:00|2019-23-01 14:00:00| 2
  13. 337|2019-23-01 14:00:00|2019-23-01 15:00:00| 2
  14. ...
  15. 345|2019-23-01 22:00:00|2019-23-01 23:00:00| 2
  16.  
  17. <input type="date" name="horario[{{$last_horario['id']}}][desde]" id="desde" onchange="ModificaEstadoHorario()" />
  18.  
  19. <script type="text/javascript">
  20. function ModificaEstadoHorario(){
  21.  
  22. var fechaElegida = new Date(desde.value);
  23. var actual = new Date();
  24.  
  25. if(fechaElegida.toISOString().substring(0, 10) < actual.toISOString().substring(0, 10)){
  26.  
  27. document.getElementById('estado_horario_id').value = 'Inactiva';
  28. }else{
  29. document.getElementById('estado_horario_id').value = 'Activa';
  30. }
  31. }
  32. <script>
  33.  
  34. <input type="text" value="Activa" name="horario[{{$last_horario['id']}}][estado_horario_id]" id="estado_horario_id">
  35.  
  36. public function store(Request $request){
  37.  
  38. try {
  39. /*CON EL SIGUIENTE CÓDIGO SE LOGRA GUARDAR MÚLTIPLES HORARIOS*/
  40.  
  41. foreach($request->horario as $id_horario=>$row){
  42. foreach($row['check'] as $check){
  43.  
  44.  
  45. $startdate = $row['desde'];
  46. $endate = $row['hasta'];
  47.  
  48. $period = CarbonPeriod::create($startdate, $endate); // Creamos un objeto "periodo" propio de Carbon
  49. $period_2 = CarbonPeriod::create($startdate, $endate);
  50.  
  51. $dates = $times = []; // Variables para almacenar las fechas y los horarios
  52. $dates_2 = $times_2 = [];
  53.  
  54. $tstart = Carbon::createFromFormat('H:i:s', $row['hora_inicio']); /*Creamos una instancia de carbon para la hora de inicio*/
  55. $tend = Carbon::createFromFormat('H:i:s', $row['hora_termino']); /*Y otra instancia para la hora de finalización*/
  56.  
  57. $tstart_2 = Carbon::createFromFormat('H:i:s', $row['hora_inicio']);
  58. $tstart_2->modify('+1 hour');
  59.  
  60. $tend_2 = Carbon::createFromFormat('H:i:s', $row['hora_termino']);
  61.  
  62. while ($tstart < $tend){ // Mientras la hora de inicio sea menor a la hora de finalización
  63.  
  64. $times[] = $tstart->format("H:i:s"); // Agregamos esa hora al array de los horarios
  65. $tstart->addHour(); // Luego aumentamos en 1 hora
  66. }
  67. while ($tstart_2 <= $tend_2){
  68.  
  69. $times_2[] = $tstart_2->format('H:i:s');
  70. $tstart_2->addHour();
  71. }
  72. foreach($period as $date){ // Recorremos todas las fechas generadas en el periodo
  73. foreach($times as $time){ // Así como los horarios generados en el while anterior
  74.  
  75. $dates[] = $date->format("Y-m-d") . " " . $time; // Y lo concatenamos y guardamos en el array de las fechas
  76. }
  77. }
  78. foreach ($period_2 as $date_2) {
  79. foreach ($times_2 as $time_2) {
  80.  
  81. $dates_2[] = $date_2->format('Y-m-d'). " ".$time_2;
  82. }
  83. }
  84.  
  85. $cuenta = count($dates);
  86.  
  87.  
  88.  
  89. //AQUI ES EN DONDE INTENTO ESTABLECER LO DICHO ANTERIORMENTE
  90.  
  91. /*QUE SI LA FECHA ESCOGIDA ES MENOR QUE LA ACTUAL EL ESTADO
  92. DEL HORARIO CAMBIE DE ACTIVO A INACTIVO*/
  93.  
  94. /*PERO DE ESTA MANERA ME DEJA TODO EN INACTIVO YA QUE
  95. COMO EL VALOR CAMBIA SE MANTIENE ASÍ Y NO VUELVE A
  96. CAMBIAR A - ACTIVO DESPUÉS QUE PASA LA FECHA Y HORA
  97. ACTUAL*/
  98. if ($row['estado_horario_id'] == 'Activa') {
  99.  
  100. $row['estado_horario_id'] = 2;
  101.  
  102. }else if ($row['estado_horario_id'] == 'Inactiva'){
  103.  
  104. $row['estado_horario_id'] = 1;
  105. }
  106.  
  107. for($i = 0; $i < $cuenta; $i++){
  108.  
  109. $horarios[]= [
  110. 'hora_incio' => $dates[$i],
  111. 'hora_fin' => $dates_2[$i],
  112. 'estado_horario_id' => $row['estado_horario_id'],
  113. 'cancha_id ' => $check
  114. ];
  115. HorariosNew::create(['hora_inicio' => $dates[$i],'hora_fin' => $dates_2[$i],'estado_horario_id' => $row['estado_horario_id'], 'cancha_id' => $check]);
  116. }
  117. }
  118. }
  119. }catch (IlluminateDatabaseQueryException $e) {
  120.  
  121. Session::flash('error', 'Hubo un error desconocido, no se pudo registrar el horario');
  122. return redirect()->route('horarios.store');
  123. }
  124.  
  125. Session::flash('message', 'Se agregó correctamente el horario');
  126. return redirect()->route('horarios.store');
  127. }
Add Comment
Please, Sign In to add comment