format('Y-m-d'); $servicio_det = ServicioDet::where('id', $servicio_id)->firstOrFail(); list($fecha) = explode(" ", $servicio_det->fecha_solicitud); //Validar fecha de solicitud if($now != $fecha){ //return response()->unprocessable('Parametros inválidos',['No se puede realizar la encuesta ya que corresponde a otra fecha.']); } $data = $request->all(); if($servicio_det->tipo_servicio_id == DOMESTICO) { $validator = Validator::make($data, [ '*.pregunta_id' => 'numeric|exists:preguntas_domestico,id', '*.respuesta_id' => 'numeric|exists:respuestas_domestico,id', '*.respuesta' => 'string|max:255' ]); if(ServicioEncuestaDomestico::where('servicio_det_id', $servicio_id)->first()){ //return response()->unprocessable('Parametros inválidos',['La encuesta para este servicio ya se realizó.']); return response()->success(['result' => 'ok']); } foreach ($data as $d){ if(isset($d['respuesta_id'])){ $respuesta = RespuestaDomestico::where('pregunta_id', $d['pregunta_id']) ->where('id', $d['respuesta_id']) ->first(); if(!$respuesta){ return response()->unprocessable('Parametros inválidos',['La respuesta no corresponde a la pregunta.']); } } } }else{ $validator = Validator::make($data, [ '*.pregunta_id' => 'numeric|exists:preguntas_empresarial,id', '*.respuesta_id' => 'numeric|exists:respuestas_empresarial,id', '*.respuesta' => 'string|max:255' ]); if(ServicioEncuestaEmpresarial::where('servicio_det_id', $servicio_id)->first()){ //return response()->unprocessable('Parametros inválidos',['La encuesta para este servicio ya se realizó.']); return response()->success(['result' => 'ok']); } foreach ($data as $d){ if(isset($d['respuesta_id'])){ $respuesta = RespuestaEmpresarial::where('pregunta_id', $d['pregunta_id']) ->where('id', $d['respuesta_id']) ->first(); if(!$respuesta){ return response()->unprocessable('Parametros inválidos',['La respuesta no corresponde a la pregunta.']); } } } } if($validator->fails()){ return response()->unprocessable('Parametros inválidos',$validator->errors()->all()); } DB::beginTransaction(); try { if($servicio_det->tipo_servicio_id == DOMESTICO) { foreach ($data as $d){ $data_domestico = [ 'servicio_det_id' => $servicio_id, 'pregunta_id' => $d['pregunta_id'], 'respuesta_id' => (isset($d['respuesta_id']))? $d['respuesta_id'] : null, 'respuesta' => (isset($d['respuesta']))? $d['respuesta'] : null ]; ServicioEncuestaDomestico::create($data_domestico); } }else{ foreach ($data as $d){ $data_empresarial = [ 'servicio_det_id' => $servicio_id, 'pregunta_id' => $d['pregunta_id'], 'respuesta_id' => (isset($d['respuesta_id']))? $d['respuesta_id'] : null, 'respuesta' => (isset($d['respuesta']))? $d['respuesta'] : null ]; ServicioEncuestaEmpresarial::create($data_empresarial); } } $servicio_progreso = ServicioProgreso::where('servicio_enc_id', $servicio_det->servicio_enc_id) ->where('servicio_det_id', $servicio_det->id) ->first(); if($servicio_progreso){ $servicio_progreso->update(['encuesta_contestada' => 1]); } DB::commit(); return response()->success(['result' => 'ok']); } catch (\Exception $e) { DB::rollBack(); return response()->unprocessable('Error', ['Error al realizar la encuesta.']); } } }