responseController = new ResponseController(); $this->encController = new EncryptionController(); $this->functionsController = new FunctionsController(); $this->arrAlphabet = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'); $this->arrClasificateDocument = array( 'AV' => 'Avisos', 'AU' => 'Audios', 'CA' => 'Catálogos', 'CE' => 'Certificaciones', 'CO' => 'Contratos', 'DP' => 'Diagramas o Planos', 'FA' => 'Facturas', 'FI' => 'Ficheros', 'FO' => 'Fotografías', 'IN' => 'Informes', 'LA' => 'Layouts', 'OR' => 'Órdenes', 'PL' => 'Plantillas', 'RE' => 'Referencias', 'VI' => 'Videos', ); $this->arrStatesEquipment = array( 'A' => 'Adquisición', 'S' => 'Stock', 'T' => 'Traslado', 'I' => 'Instalación', 'R' => 'Reparación', 'D' => 'Disposición', ); // $this->pathService = 'C:\ITTEC\SAM\Dev\SistemaMantenimiento\sistema-mantenimiento-back'; $this->pathService = 'C:\ITTEC\SAM\Dev\SistemaMantenimiento\sistema-mantenimiento-back'; } // Se utiliza para rellenar un número con ceros a la izquierda public function formatSecuence($cont, $length){ $longigud = strlen($cont); $aumentar = $length - $longigud; $contador = ''; for ($i = 0; $i < $aumentar; $i++) { $contador .= '0'; } $contador .= $cont === 0 ? 1 : $cont; return $contador; } // Se obtiene el usuario encriptado // Se desencripta y se verfica que exista // Regresa el usuario desencriptado public function checkUserEnc(string $encUser, string $line): Array { $arrResponse = array( 'error' => false, 'msg' => '', 'response' => [] ); try { $user = $this->encController->decrypt($encUser); } catch (\Throwable $th) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió un error al desencriptar el ID del usuario.'; $arrResponse['response'] = $th->getMessage(); return $arrResponse; } try { $validateUser = DB::table('S002V01TUSUA') ->where('USUA_NULI', '=', $line) ->where('USUA_IDUS', '=', $user) ->exists(); } catch(\Throwable $th) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió al verificar la existencia del usuario.'; $arrResponse['response'] = $th->getMessage(); return $arrResponse; } if (!$validateUser) { $arrResponse['error'] = true; $arrResponse['msg'] = 'El usuario no existe.'; $arrResponse['response'] = []; return $arrResponse; } $arrResponse['error'] = false; $arrResponse['msg'] = 'Usuario validado'; $arrResponse['response'] = $user; return $arrResponse; } public function checkUserDec($decUser, $line) { $arrResponse = array( 'error' => false, 'msg' => '', 'response' => [] ); try { $validateUser = DB::table('S002V01TUSUA') ->where('USUA_NULI', '=', $line) ->where('USUA_IDUS', '=', $decUser) ->exists(); } catch(\Throwable $th) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió al verificar la existencia del usuario.'; $arrResponse['response'] = $th->getMessage(); return $arrResponse; } if (!$validateUser) { $arrResponse['error'] = true; $arrResponse['msg'] = 'El usuario no existe.'; $arrResponse['response'] = []; return $arrResponse; } $arrResponse['error'] = false; $arrResponse['msg'] = 'Usuario validado'; $arrResponse['response'] = $decUser; return $arrResponse; } public function validateAddress($codigoPostal, $idColonia, $idMunicipio, $idLocalidad = null, $idEstado, $idPais, $line): Array { $arrResponse = array( 'error' => false, 'msg' => '', 'response' => [] ); // Se obtiene el resultado si existe el código del país try { $validatePais = DB::table('S002V01TPAIS') ->where('PAIS_IDPA', '=', $idPais) ->where('PAIS_NULI', '=', $line) ->exists(); } catch (\Throwable $th) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió al validar el país.'; $arrResponse['response'] = $th->getMessage(); return $arrResponse; } // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente if (!$validatePais) { $arrResponse['error'] = true; $arrResponse['msg'] = 'El país ingresado no se encuentra en la lista de países.'; return $arrResponse; } // Se valida que los paises sean MEX, USA y CAN para verificar el estado/entidad federativa del país // Por otro lado, el método se terminará y mandará un estado correcto. if($idPais !== 'MEX' && $idPais !== 'USA' && $idPais !== 'CAN') { $arrResponse['error'] = false; $arrResponse['msg'] = 'Correcto'; return $arrResponse; } // Se obtiene el resultado si existe el código del estado relacionado al páis try { $validateEstado = DB::table('S002V01TESTA') ->where('ESTA_COPA', '=', $idPais) ->where('ESTA_COES', '=', $idEstado) ->where('ESTA_NULI', '=', $line) ->exists(); } catch (\Throwable $th) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió al validar el estado.'; $arrResponse['response'] = $th->getMessage(); return $arrResponse; } // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente if (!$validateEstado) { $arrResponse['error'] = true; $arrResponse['msg'] = 'El estado ingresado no se encuentra en la lista de países y estados.'; return $arrResponse; } // Se valida que el país ingresado sea MEX para verificar el municipio, localidad, colonia y código postal // Por otro lado, el método de terminará y mandará un estado correcto. if($idPais !== 'MEX') { $arrResponse['error'] = false; $arrResponse['msg'] = 'Correcto'; return $arrResponse; } // Se obtiene el resultado si existe el código del municipio relacionado al estado try { $validateMunicipio = DB::table('S002V01TMUNI') ->where('MUNI_COES', '=', $idEstado) ->where('MUNI_COMU', '=', $idMunicipio) ->where('MUNI_NULI', '=', $line) ->exists(); } catch (\Throwable $th) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió al validar el municipio.'; $arrResponse['response'] = $th->getMessage(); return $arrResponse; } // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente if (!$validateMunicipio) { $arrResponse['error'] = true; $arrResponse['msg'] = 'El municipio ingresado ('.$idMunicipio.') no se encuentra en la lista de países, estados y municipios.'; return $arrResponse; } // Se verifica si el campo localidad exista para poder validarlo if (!is_null($idLocalidad)) { // Se obtiene el resultado si existe el código de la localidad relacionado al estado try { $validateLocalidad = DB::table('S002V01TLOCA') ->where('LOCA_COES', '=', $idEstado) ->where('LOCA_COLO', '=', $idLocalidad) ->where('LOCA_NULI', '=', $line) ->exists(); } catch (\Throwable $th) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió al validar la localidad.'; $arrResponse['response'] = $th->getMessage(); return $arrResponse; } // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente if (!$validateLocalidad) { $arrResponse['error'] = true; $arrResponse['msg'] = 'La localidad ingresada no se encuentra en la lista de países, estados, municipios y localidades.'; return $arrResponse; } } // Se obtiene el resultado si existe el código de la colonia relacionada al código postal try { $validateColonia = DB::table('S002V01TCOLO') ->where('COLO_COPO', '=', $codigoPostal) ->where('COLO_COCO', '=', $idColonia) ->where('COLO_NULI', '=', $line) ->exists(); } catch (\Throwable $th) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió al validar la colonia.'; $arrResponse['response'] = $th->getMessage(); return $arrResponse; } // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente if (!$validateColonia) { $arrResponse['error'] = true; $arrResponse['msg'] = 'La colonia ingresada no se encuentra en la lista de países, estados, municipios y códigos postales.'; return $arrResponse; } // Se obtiene el resultado si existe el código postal relacionado al estado try { $validateCodigoPostal = DB::table('S002V01TCOPO') ->where('COPO_COES', '=', $idEstado) ->where('COPO_COPO', '=', $codigoPostal) ->where('COPO_NULI', '=', $line) ->exists(); } catch (\Throwable $th) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió al validar el código postal.'; $arrResponse['response'] = $th->getMessage(); return $arrResponse; } // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente if (!$validateCodigoPostal) { $arrResponse['error'] = true; $arrResponse['msg'] = 'El código postal ingresado no se encuentra en la lista de países, estados, municipios y colonias.'; return $arrResponse; } return $arrResponse; } public function getAddress($codigoPostal, $idColonia, $idMunicipio, $idLocalidad = null, $idEstado, $idPais, $line): Array { $arrResponse = array( 'error' => false, 'msg' => '', 'response' => [] ); // Se obtiene el resultado si existe el código del país try { $arrPais = (array) DB::table('S002V01TPAIS') ->where('PAIS_IDPA', '=', $idPais) ->where('PAIS_NULI', '=', $line) ->first([ 'PAIS_IDPA', 'PAIS_NOMB' ]); } catch (\Throwable $th) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió al validar el país.'; $arrResponse['response'] = $th->getMessage(); return $arrResponse; } // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente if (empty($arrPais)) { $arrResponse['error'] = true; $arrResponse['msg'] = 'El país ingresado no se encuentra en la lista de países.'; return $arrResponse; } $arrResponse['response']['PAIS'] = $arrPais['PAIS_NOMB'].' ('.$arrPais['PAIS_IDPA'].')'; // Se valida que los paises sean MEX, USA y CAN para verificar el estado/entidad federativa del país // Por otro lado, el método se terminará y mandará un estado correcto. if($idPais !== 'MEX' && $idPais !== 'USA' && $idPais !== 'CAN') { $arrResponse['error'] = false; $arrResponse['msg'] = 'Correcto'; return $arrResponse; } // Se obtiene el resultado si existe el código del estado relacionado al páis try { $arrEstado = (array) DB::table('S002V01TESTA') ->where('ESTA_COPA', '=', $idPais) ->where('ESTA_COES', '=', $idEstado) ->where('ESTA_NULI', '=', $line) ->first(); } catch (\Throwable $th) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió al validar el estado.'; $arrResponse['response'] = $th->getMessage(); return $arrResponse; } // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente if (empty($arrEstado)) { $arrResponse['error'] = true; $arrResponse['msg'] = 'El estado ingresado no se encuentra en la lista de países y estados.'; return $arrResponse; } $arrResponse['response']['ENTIDAD_FEDERATIVA'] = $arrEstado['ESTA_NOES'].' ('.$arrEstado['ESTA_COES'].')'; // Se valida que el país ingresado sea MEX para verificar el municipio, localidad, colonia y código postal // Por otro lado, el método de terminará y mandará un estado correcto. if($idPais !== 'MEX') { $arrResponse['error'] = false; $arrResponse['msg'] = 'Correcto'; return $arrResponse; } // Se obtiene el resultado si existe el código del municipio relacionado al estado try { $arrMunicipio = (array) DB::table('S002V01TMUNI') ->where('MUNI_COES', '=', $idEstado) ->where('MUNI_COMU', '=', $idMunicipio) ->where('MUNI_NULI', '=', $line) ->first(['MUNI_COMU','MUNI_NOMU']); } catch (\Throwable $th) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió al validar el municipio.'; $arrResponse['response'] = $th->getMessage(); return $arrResponse; } // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente if (!$arrMunicipio) { $arrResponse['error'] = true; $arrResponse['msg'] = 'El municipio ingresado ('.$idMunicipio.') no se encuentra en la lista de países, estados y municipios.'; return $arrResponse; } $arrResponse['response']['MUNICIPIO'] = $arrMunicipio['MUNI_NOMU'].' ('.$arrMunicipio['MUNI_COMU'].')'; // Se verifica si el campo localidad exista para poder validarlo if (!is_null($idLocalidad)) { // Se obtiene el resultado si existe el código de la localidad relacionado al estado try { $arrLocalidad = (array) DB::table('S002V01TLOCA') ->where('LOCA_COES', '=', $idEstado) ->where('LOCA_COLO', '=', $idLocalidad) ->where('LOCA_NULI', '=', $line) ->first(['LOCA_COLO','LOCA_NOLO']); } catch (\Throwable $th) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió al validar la localidad.'; $arrResponse['response'] = $th->getMessage(); return $arrResponse; } // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente if (!$arrLocalidad) { $arrResponse['error'] = true; $arrResponse['msg'] = 'La localidad ingresada no se encuentra en la lista de países, estados, municipios y localidades.'; return $arrResponse; } $arrResponse['response']['LOCALIDAD'] = $arrLocalidad['LOCA_NOLO'].' ('.$arrLocalidad['LOCA_COLO'].')'; } else { $arrResponse['response']['LOCALIDAD'] = null; } // Se obtiene el resultado si existe el código de la colonia relacionada al código postal try { $arrColonia = (array) DB::table('S002V01TCOLO') ->where('COLO_COPO', '=', $codigoPostal) ->where('COLO_COCO', '=', $idColonia) ->where('COLO_NULI', '=', $line) ->first(['COLO_COCO', 'COLO_NOCO']); } catch (\Throwable $th) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió al validar la colonia.'; $arrResponse['response'] = $th->getMessage(); return $arrResponse; } // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente if (!$arrColonia) { $arrResponse['error'] = true; $arrResponse['msg'] = 'La colonia ingresada no se encuentra en la lista de países, estados, municipios y códigos postales.'; return $arrResponse; } $arrResponse['response']['COLONIA'] = $arrColonia['COLO_NOCO'].' ('.$arrColonia['COLO_COCO'].')'; // Se obtiene el resultado si existe el código postal relacionado al estado try { $arrCodigoPostal = (array) DB::table('S002V01TCOPO') ->where('COPO_COES', '=', $idEstado) ->where('COPO_COPO', '=', $codigoPostal) ->where('COPO_NULI', '=', $line) ->first(['COPO_COPO']); } catch (\Throwable $th) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió al validar el código postal.'; $arrResponse['response'] = $th->getMessage(); return $arrResponse; } // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente if (!$arrCodigoPostal) { $arrResponse['error'] = true; $arrResponse['msg'] = 'El código postal ingresado no se encuentra en la lista de países, estados, municipios y colonias.'; return $arrResponse; } $arrResponse['response']['CODIGO_POSTAL'] = $arrCodigoPostal['COPO_COPO']; return $arrResponse; } public function checkLatestUpdate(array $arr, $line) { $arrResponse = array('error' => false, 'msg' => '', 'response' => []); $arrTemp = array(); foreach ($arr as $keyValue => $value) { $value = (array) $value; if (!array_key_exists('USUARIO_REGISTRA', $value)) { $arrResponse['error'] = true; $arrResponse['msg'] = 'La clave USUARIO_REGISTRA no existe en el arreglo.'; return $arrResponse; } if (!array_key_exists('FECHA_REGISTRA', $value)) { $arrResponse['error'] = true; $arrResponse['msg'] = 'La clave FECHA_REGISTRA no existe en el arreglo.'; return $arrResponse; } if (!array_key_exists('USUARIO_MODIFICA', $value)) { $arrResponse['error'] = true; $arrResponse['msg'] = 'La clave USUARIO_MODIFICA no existe en el arreglo.'; return $arrResponse; } if (!array_key_exists('FECHA_MODIFICA', $value)) { $arrResponse['error'] = true; $arrResponse['msg'] = 'La clave FECHA_MODIFICA no existe en el arreglo.'; return $arrResponse; } foreach ($value as $keyItem => $item) { if ( $keyItem !== 'USUARIO_REGISTRA' && $keyItem !== 'FECHA_REGISTRA' && $keyItem !== 'USUARIO_MODIFICA' && $keyItem !== 'FECHA_MODIFICA' ) { $arrTemp[$keyValue][$keyItem] = $item; } } $userNumber = ''; if (is_null($value['USUARIO_MODIFICA'])) { $userNumber = $value['USUARIO_REGISTRA']; } else { $userNumber = $value['USUARIO_MODIFICA']; } $lastUpdate = ''; if (is_null($value['FECHA_MODIFICA'])) { $lastUpdate = $value['FECHA_REGISTRA']; } else { $lastUpdate = $value['FECHA_MODIFICA']; } try { $user = (array) DB::table('S002V01TUSUA') ->where('USUA_NULI', '=', $line) ->where('USUA_IDUS', '=', $userNumber) ->first(); } catch (\Throwable $th) { $arrResponse['msg'] = 'Ocurrió un error al obtener el usuario "'.$userNumber.'": '.$th->getMessage(); } $nameUser = $user['USUA_NOMB'].' '.$user['USUA_APPA']; if ( !is_null($user['USUA_APMA']) ) { $nameUser .= ' '.$user['USUA_APMA']; } $arrTemp[$keyValue]['USUARIO_MODIFICA'] = $nameUser.' ('.$userNumber.')'; $responseDatetime = $this->reformatDatetime($lastUpdate); if ($responseDatetime['error']) { $arrResponse['error'] = true; $arrResponse['msg'] = $responseDatetime['msg']; return $arrResponse; } $arrTemp[$keyValue]['FECHA_MODIFICA'] = $responseDatetime['response']; } $arrResponse['response'] = $arrTemp; return $arrResponse; } public function reformatDatetime($datetime) { $arrResponse = array('error' => false, 'msg' => '', 'response' => []); $arrDatetime = explode(' ', $datetime); if ($arrDatetime === false || count($arrDatetime) !== 2) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió un error al obtener el formato de la Datetime.'; return $arrResponse; } $date = $arrDatetime[0]; $time = $arrDatetime[1]; $responseDate = $this->formatOnlyDate($date); if ($responseDate['error']) { $arrResponse['error'] = true; $arrResponse['msg'] = $responseDate['msg']; return $arrResponse; } $formatDate = $responseDate['response']; $responseTime = $this->formatOnlyTime($time); if ($responseTime['error']) { $arrResponse['error'] = true; $arrResponse['msg'] = $responseTime['msg']; return $arrResponse; } $formatTime = $responseTime['response']; $arrResponse['response'] = $formatDate.' '.$formatTime; return $arrResponse; } public function formatOnlyDate(string $date) { $arrResponse = array('error' => false, 'msg' => '', 'response' => []); $arrDate = explode('-', $date); if ($arrDate === false || count($arrDate) !== 3) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió un error al obtener el formato de la fecha.'; return $arrResponse; } $arrResponse['response'] = $arrDate[2].'-'.$arrDate[1].'-'.$arrDate[0]; return $arrResponse; } public function formatOnlyTime(string $time) { $arrResponse = array('error' => false, 'msg' => '', 'response' => []); $arrTime = explode(':', $time); if ($arrTime === false || count($arrTime) != 3) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió un error al obtener el formato de la hora.'; return $arrResponse; } $arrSetTime = array( 13 => 1, 14 => 2, 15 => 3, 16 => 4, 17 => 5, 18 => 6, 19 => 7, 20 => 8, 21 => 9, 22 => 10, 23 => 11, 24 => 00); $hour = intval($arrTime[0]); $minute = intval($arrTime[1]); $second = intval($arrTime[2]); $format = 'AM'; if (array_key_exists($hour, $arrSetTime)) { $format = 'PM'; $hour = $arrSetTime[$hour]; } $hour = $this->formatSecuence($hour, 2); $minute = $this->formatSecuence($minute, 2); $second = $this->formatSecuence($second, 2); $arrResponse['response'] = $hour.':'.$minute.':'.$second.' '.$format; return $arrResponse; } public function getUser($idUser, $line) { $arrResponse = array('error' => false, 'msg' => '', 'response' => []); try { $user = (array) DB::table('S002V01TUSUA') ->where('USUA_IDUS', '=', $idUser) ->where('USUA_NULI', '=', $line) ->first([ 'USUA_NOMB AS NOMBRE', 'USUA_APPA AS APELLIDO_PATERNO', 'USUA_APMA AS APELLIDO_MATERNO', ]); } catch (\Throwable $th) { $arrResponse['error'] = true; $arrResponse['msg'] = 'Ocurrió un error al obtener el nombre del usuario.'; return $arrResponse; } if (empty($user)) { $arrResponse['error'] = true; $arrResponse['msg'] = 'El usuario no existe.'; return $arrResponse; } $nameUser = $user['NOMBRE'] . ' ' . $user['APELLIDO_PATERNO']; if (array_key_exists('APELLIDO_MATERNO', $user)) { $nameUser .= ' ' . $user['APELLIDO_MATERNO']; } $arrResponse['response'] = $nameUser; return $arrResponse; } public function formatBytes ($bytes, $decimal = 2) { if ($bytes === 0) { return '0 Bytes'; } $k = 1024; $dm = $decimal < 0 ? 0 : $decimal; $sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; $i = floor(log($bytes) / log($k)); return sprintf("%.{$dm}f", ($bytes / pow($k, $i))) . ' ' . $sizes[$i]; } }