ResourcesController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\DB;
  4. class ResourcesController extends Controller
  5. {
  6. private $responseController;
  7. public $arrAlphabet;
  8. public $encController;
  9. public $functionsController;
  10. public $arrClasificateDocument;
  11. public $arrStatesEquipment;
  12. public $pathService;
  13. public function __construct() {
  14. $this->responseController = new ResponseController();
  15. $this->encController = new EncryptionController();
  16. $this->functionsController = new FunctionsController();
  17. $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');
  18. $this->arrClasificateDocument = array(
  19. 'AV' => 'Avisos',
  20. 'AU' => 'Audios',
  21. 'CA' => 'Catálogos',
  22. 'CE' => 'Certificaciones',
  23. 'CO' => 'Contratos',
  24. 'DP' => 'Diagramas o Planos',
  25. 'FA' => 'Facturas',
  26. 'FI' => 'Ficheros',
  27. 'FO' => 'Fotografías',
  28. 'IN' => 'Informes',
  29. 'LA' => 'Layouts',
  30. 'OR' => 'Órdenes',
  31. 'PL' => 'Plantillas',
  32. 'RE' => 'Referencias',
  33. 'VI' => 'Videos',
  34. );
  35. $this->arrStatesEquipment = array(
  36. 'A' => 'Adquisición',
  37. 'S' => 'Stock',
  38. 'T' => 'Traslado',
  39. 'I' => 'Instalación',
  40. 'R' => 'Reparación',
  41. 'D' => 'Disposición',
  42. );
  43. // $this->pathService = 'C:\ITTEC\SAM\Dev\SistemaMantenimiento\sistema-mantenimiento-back';
  44. $this->pathService = 'C:\ITTEC\SAM\Dev\SistemaMantenimiento\sistema-mantenimiento-back';
  45. }
  46. // Se utiliza para rellenar un número con ceros a la izquierda
  47. public function formatSecuence($cont, $length){
  48. $longigud = strlen($cont);
  49. $aumentar = $length - $longigud;
  50. $contador = '';
  51. for ($i = 0; $i < $aumentar; $i++) {
  52. $contador .= '0';
  53. }
  54. $contador .= $cont === 0 ? 1 : $cont;
  55. return $contador;
  56. }
  57. // Se obtiene el usuario encriptado
  58. // Se desencripta y se verfica que exista
  59. // Regresa el usuario desencriptado
  60. public function checkUserEnc(string $encUser, string $line): Array {
  61. $arrResponse = array( 'error' => false, 'msg' => '', 'response' => [] );
  62. try {
  63. $user = $this->encController->decrypt($encUser);
  64. } catch (\Throwable $th) {
  65. $arrResponse['error'] = true;
  66. $arrResponse['msg'] = 'Ocurrió un error al desencriptar el ID del usuario.';
  67. $arrResponse['response'] = $th->getMessage();
  68. return $arrResponse;
  69. }
  70. try {
  71. $validateUser = DB::table('S002V01TUSUA')
  72. ->where('USUA_NULI', '=', $line)
  73. ->where('USUA_IDUS', '=', $user)
  74. ->exists();
  75. } catch(\Throwable $th) {
  76. $arrResponse['error'] = true;
  77. $arrResponse['msg'] = 'Ocurrió al verificar la existencia del usuario.';
  78. $arrResponse['response'] = $th->getMessage();
  79. return $arrResponse;
  80. }
  81. if (!$validateUser) {
  82. $arrResponse['error'] = true;
  83. $arrResponse['msg'] = 'El usuario no existe.';
  84. $arrResponse['response'] = [];
  85. return $arrResponse;
  86. }
  87. $arrResponse['error'] = false;
  88. $arrResponse['msg'] = 'Usuario validado';
  89. $arrResponse['response'] = $user;
  90. return $arrResponse;
  91. }
  92. public function checkUserDec($decUser, $line) {
  93. $arrResponse = array( 'error' => false, 'msg' => '', 'response' => [] );
  94. try {
  95. $validateUser = DB::table('S002V01TUSUA')
  96. ->where('USUA_NULI', '=', $line)
  97. ->where('USUA_IDUS', '=', $decUser)
  98. ->exists();
  99. } catch(\Throwable $th) {
  100. $arrResponse['error'] = true;
  101. $arrResponse['msg'] = 'Ocurrió al verificar la existencia del usuario.';
  102. $arrResponse['response'] = $th->getMessage();
  103. return $arrResponse;
  104. }
  105. if (!$validateUser) {
  106. $arrResponse['error'] = true;
  107. $arrResponse['msg'] = 'El usuario no existe.';
  108. $arrResponse['response'] = [];
  109. return $arrResponse;
  110. }
  111. $arrResponse['error'] = false;
  112. $arrResponse['msg'] = 'Usuario validado';
  113. $arrResponse['response'] = $decUser;
  114. return $arrResponse;
  115. }
  116. public function validateAddress($codigoPostal, $idColonia, $idMunicipio, $idLocalidad = null, $idEstado, $idPais, $line): Array {
  117. $arrResponse = array( 'error' => false, 'msg' => '', 'response' => [] );
  118. // Se obtiene el resultado si existe el código del país
  119. try {
  120. $validatePais = DB::table('S002V01TPAIS')
  121. ->where('PAIS_IDPA', '=', $idPais)
  122. ->where('PAIS_NULI', '=', $line)
  123. ->exists();
  124. } catch (\Throwable $th) {
  125. $arrResponse['error'] = true;
  126. $arrResponse['msg'] = 'Ocurrió al validar el país.';
  127. $arrResponse['response'] = $th->getMessage();
  128. return $arrResponse;
  129. }
  130. // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente
  131. if (!$validatePais) {
  132. $arrResponse['error'] = true;
  133. $arrResponse['msg'] = 'El país ingresado no se encuentra en la lista de países.';
  134. return $arrResponse;
  135. }
  136. // Se valida que los paises sean MEX, USA y CAN para verificar el estado/entidad federativa del país
  137. // Por otro lado, el método se terminará y mandará un estado correcto.
  138. if($idPais !== 'MEX' && $idPais !== 'USA' && $idPais !== 'CAN') {
  139. $arrResponse['error'] = false;
  140. $arrResponse['msg'] = 'Correcto';
  141. return $arrResponse;
  142. }
  143. // Se obtiene el resultado si existe el código del estado relacionado al páis
  144. try {
  145. $validateEstado = DB::table('S002V01TESTA')
  146. ->where('ESTA_COPA', '=', $idPais)
  147. ->where('ESTA_COES', '=', $idEstado)
  148. ->where('ESTA_NULI', '=', $line)
  149. ->exists();
  150. } catch (\Throwable $th) {
  151. $arrResponse['error'] = true;
  152. $arrResponse['msg'] = 'Ocurrió al validar el estado.';
  153. $arrResponse['response'] = $th->getMessage();
  154. return $arrResponse;
  155. }
  156. // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente
  157. if (!$validateEstado) {
  158. $arrResponse['error'] = true;
  159. $arrResponse['msg'] = 'El estado ingresado no se encuentra en la lista de países y estados.';
  160. return $arrResponse;
  161. }
  162. // Se valida que el país ingresado sea MEX para verificar el municipio, localidad, colonia y código postal
  163. // Por otro lado, el método de terminará y mandará un estado correcto.
  164. if($idPais !== 'MEX') {
  165. $arrResponse['error'] = false;
  166. $arrResponse['msg'] = 'Correcto';
  167. return $arrResponse;
  168. }
  169. // Se obtiene el resultado si existe el código del municipio relacionado al estado
  170. try {
  171. $validateMunicipio = DB::table('S002V01TMUNI')
  172. ->where('MUNI_COES', '=', $idEstado)
  173. ->where('MUNI_COMU', '=', $idMunicipio)
  174. ->where('MUNI_NULI', '=', $line)
  175. ->exists();
  176. } catch (\Throwable $th) {
  177. $arrResponse['error'] = true;
  178. $arrResponse['msg'] = 'Ocurrió al validar el municipio.';
  179. $arrResponse['response'] = $th->getMessage();
  180. return $arrResponse;
  181. }
  182. // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente
  183. if (!$validateMunicipio) {
  184. $arrResponse['error'] = true;
  185. $arrResponse['msg'] = 'El municipio ingresado ('.$idMunicipio.') no se encuentra en la lista de países, estados y municipios.';
  186. return $arrResponse;
  187. }
  188. // Se verifica si el campo localidad exista para poder validarlo
  189. if (!is_null($idLocalidad)) {
  190. // Se obtiene el resultado si existe el código de la localidad relacionado al estado
  191. try {
  192. $validateLocalidad = DB::table('S002V01TLOCA')
  193. ->where('LOCA_COES', '=', $idEstado)
  194. ->where('LOCA_COLO', '=', $idLocalidad)
  195. ->where('LOCA_NULI', '=', $line)
  196. ->exists();
  197. } catch (\Throwable $th) {
  198. $arrResponse['error'] = true;
  199. $arrResponse['msg'] = 'Ocurrió al validar la localidad.';
  200. $arrResponse['response'] = $th->getMessage();
  201. return $arrResponse;
  202. }
  203. // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente
  204. if (!$validateLocalidad) {
  205. $arrResponse['error'] = true;
  206. $arrResponse['msg'] = 'La localidad ingresada no se encuentra en la lista de países, estados, municipios y localidades.';
  207. return $arrResponse;
  208. }
  209. }
  210. // Se obtiene el resultado si existe el código de la colonia relacionada al código postal
  211. try {
  212. $validateColonia = DB::table('S002V01TCOLO')
  213. ->where('COLO_COPO', '=', $codigoPostal)
  214. ->where('COLO_COCO', '=', $idColonia)
  215. ->where('COLO_NULI', '=', $line)
  216. ->exists();
  217. } catch (\Throwable $th) {
  218. $arrResponse['error'] = true;
  219. $arrResponse['msg'] = 'Ocurrió al validar la colonia.';
  220. $arrResponse['response'] = $th->getMessage();
  221. return $arrResponse;
  222. }
  223. // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente
  224. if (!$validateColonia) {
  225. $arrResponse['error'] = true;
  226. $arrResponse['msg'] = 'La colonia ingresada no se encuentra en la lista de países, estados, municipios y códigos postales.';
  227. return $arrResponse;
  228. }
  229. // Se obtiene el resultado si existe el código postal relacionado al estado
  230. try {
  231. $validateCodigoPostal = DB::table('S002V01TCOPO')
  232. ->where('COPO_COES', '=', $idEstado)
  233. ->where('COPO_COPO', '=', $codigoPostal)
  234. ->where('COPO_NULI', '=', $line)
  235. ->exists();
  236. } catch (\Throwable $th) {
  237. $arrResponse['error'] = true;
  238. $arrResponse['msg'] = 'Ocurrió al validar el código postal.';
  239. $arrResponse['response'] = $th->getMessage();
  240. return $arrResponse;
  241. }
  242. // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente
  243. if (!$validateCodigoPostal) {
  244. $arrResponse['error'] = true;
  245. $arrResponse['msg'] = 'El código postal ingresado no se encuentra en la lista de países, estados, municipios y colonias.';
  246. return $arrResponse;
  247. }
  248. return $arrResponse;
  249. }
  250. public function getAddress($codigoPostal, $idColonia, $idMunicipio, $idLocalidad = null, $idEstado, $idPais, $line): Array {
  251. $arrResponse = array( 'error' => false, 'msg' => '', 'response' => [] );
  252. // Se obtiene el resultado si existe el código del país
  253. try {
  254. $arrPais = (array) DB::table('S002V01TPAIS')
  255. ->where('PAIS_IDPA', '=', $idPais)
  256. ->where('PAIS_NULI', '=', $line)
  257. ->first([
  258. 'PAIS_IDPA',
  259. 'PAIS_NOMB'
  260. ]);
  261. } catch (\Throwable $th) {
  262. $arrResponse['error'] = true;
  263. $arrResponse['msg'] = 'Ocurrió al validar el país.';
  264. $arrResponse['response'] = $th->getMessage();
  265. return $arrResponse;
  266. }
  267. // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente
  268. if (empty($arrPais)) {
  269. $arrResponse['error'] = true;
  270. $arrResponse['msg'] = 'El país ingresado no se encuentra en la lista de países.';
  271. return $arrResponse;
  272. }
  273. $arrResponse['response']['PAIS'] = $arrPais['PAIS_NOMB'].' ('.$arrPais['PAIS_IDPA'].')';
  274. // Se valida que los paises sean MEX, USA y CAN para verificar el estado/entidad federativa del país
  275. // Por otro lado, el método se terminará y mandará un estado correcto.
  276. if($idPais !== 'MEX' && $idPais !== 'USA' && $idPais !== 'CAN') {
  277. $arrResponse['error'] = false;
  278. $arrResponse['msg'] = 'Correcto';
  279. return $arrResponse;
  280. }
  281. // Se obtiene el resultado si existe el código del estado relacionado al páis
  282. try {
  283. $arrEstado = (array) DB::table('S002V01TESTA')
  284. ->where('ESTA_COPA', '=', $idPais)
  285. ->where('ESTA_COES', '=', $idEstado)
  286. ->where('ESTA_NULI', '=', $line)
  287. ->first();
  288. } catch (\Throwable $th) {
  289. $arrResponse['error'] = true;
  290. $arrResponse['msg'] = 'Ocurrió al validar el estado.';
  291. $arrResponse['response'] = $th->getMessage();
  292. return $arrResponse;
  293. }
  294. // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente
  295. if (empty($arrEstado)) {
  296. $arrResponse['error'] = true;
  297. $arrResponse['msg'] = 'El estado ingresado no se encuentra en la lista de países y estados.';
  298. return $arrResponse;
  299. }
  300. $arrResponse['response']['ENTIDAD_FEDERATIVA'] = $arrEstado['ESTA_NOES'].' ('.$arrEstado['ESTA_COES'].')';
  301. // Se valida que el país ingresado sea MEX para verificar el municipio, localidad, colonia y código postal
  302. // Por otro lado, el método de terminará y mandará un estado correcto.
  303. if($idPais !== 'MEX') {
  304. $arrResponse['error'] = false;
  305. $arrResponse['msg'] = 'Correcto';
  306. return $arrResponse;
  307. }
  308. // Se obtiene el resultado si existe el código del municipio relacionado al estado
  309. try {
  310. $arrMunicipio = (array) DB::table('S002V01TMUNI')
  311. ->where('MUNI_COES', '=', $idEstado)
  312. ->where('MUNI_COMU', '=', $idMunicipio)
  313. ->where('MUNI_NULI', '=', $line)
  314. ->first(['MUNI_COMU','MUNI_NOMU']);
  315. } catch (\Throwable $th) {
  316. $arrResponse['error'] = true;
  317. $arrResponse['msg'] = 'Ocurrió al validar el municipio.';
  318. $arrResponse['response'] = $th->getMessage();
  319. return $arrResponse;
  320. }
  321. // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente
  322. if (!$arrMunicipio) {
  323. $arrResponse['error'] = true;
  324. $arrResponse['msg'] = 'El municipio ingresado ('.$idMunicipio.') no se encuentra en la lista de países, estados y municipios.';
  325. return $arrResponse;
  326. }
  327. $arrResponse['response']['MUNICIPIO'] = $arrMunicipio['MUNI_NOMU'].' ('.$arrMunicipio['MUNI_COMU'].')';
  328. // Se verifica si el campo localidad exista para poder validarlo
  329. if (!is_null($idLocalidad)) {
  330. // Se obtiene el resultado si existe el código de la localidad relacionado al estado
  331. try {
  332. $arrLocalidad = (array) DB::table('S002V01TLOCA')
  333. ->where('LOCA_COES', '=', $idEstado)
  334. ->where('LOCA_COLO', '=', $idLocalidad)
  335. ->where('LOCA_NULI', '=', $line)
  336. ->first(['LOCA_COLO','LOCA_NOLO']);
  337. } catch (\Throwable $th) {
  338. $arrResponse['error'] = true;
  339. $arrResponse['msg'] = 'Ocurrió al validar la localidad.';
  340. $arrResponse['response'] = $th->getMessage();
  341. return $arrResponse;
  342. }
  343. // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente
  344. if (!$arrLocalidad) {
  345. $arrResponse['error'] = true;
  346. $arrResponse['msg'] = 'La localidad ingresada no se encuentra en la lista de países, estados, municipios y localidades.';
  347. return $arrResponse;
  348. }
  349. $arrResponse['response']['LOCALIDAD'] = $arrLocalidad['LOCA_NOLO'].' ('.$arrLocalidad['LOCA_COLO'].')';
  350. } else {
  351. $arrResponse['response']['LOCALIDAD'] = null;
  352. }
  353. // Se obtiene el resultado si existe el código de la colonia relacionada al código postal
  354. try {
  355. $arrColonia = (array) DB::table('S002V01TCOLO')
  356. ->where('COLO_COPO', '=', $codigoPostal)
  357. ->where('COLO_COCO', '=', $idColonia)
  358. ->where('COLO_NULI', '=', $line)
  359. ->first(['COLO_COCO', 'COLO_NOCO']);
  360. } catch (\Throwable $th) {
  361. $arrResponse['error'] = true;
  362. $arrResponse['msg'] = 'Ocurrió al validar la colonia.';
  363. $arrResponse['response'] = $th->getMessage();
  364. return $arrResponse;
  365. }
  366. // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente
  367. if (!$arrColonia) {
  368. $arrResponse['error'] = true;
  369. $arrResponse['msg'] = 'La colonia ingresada no se encuentra en la lista de países, estados, municipios y códigos postales.';
  370. return $arrResponse;
  371. }
  372. $arrResponse['response']['COLONIA'] = $arrColonia['COLO_NOCO'].' ('.$arrColonia['COLO_COCO'].')';
  373. // Se obtiene el resultado si existe el código postal relacionado al estado
  374. try {
  375. $arrCodigoPostal = (array) DB::table('S002V01TCOPO')
  376. ->where('COPO_COES', '=', $idEstado)
  377. ->where('COPO_COPO', '=', $codigoPostal)
  378. ->where('COPO_NULI', '=', $line)
  379. ->first(['COPO_COPO']);
  380. } catch (\Throwable $th) {
  381. $arrResponse['error'] = true;
  382. $arrResponse['msg'] = 'Ocurrió al validar el código postal.';
  383. $arrResponse['response'] = $th->getMessage();
  384. return $arrResponse;
  385. }
  386. // En caso de que no exista, entonces se termina el método y se manda un estado incorrecto y su mensaje correspondiente
  387. if (!$arrCodigoPostal) {
  388. $arrResponse['error'] = true;
  389. $arrResponse['msg'] = 'El código postal ingresado no se encuentra en la lista de países, estados, municipios y colonias.';
  390. return $arrResponse;
  391. }
  392. $arrResponse['response']['CODIGO_POSTAL'] = $arrCodigoPostal['COPO_COPO'];
  393. return $arrResponse;
  394. }
  395. public function checkLatestUpdate(array $arr, $line) {
  396. $arrResponse = array('error' => false, 'msg' => '', 'response' => []);
  397. $arrTemp = array();
  398. foreach ($arr as $keyValue => $value) {
  399. $value = (array) $value;
  400. if (!array_key_exists('USUARIO_REGISTRO', $value)) {
  401. $arrResponse['error'] = true;
  402. $arrResponse['msg'] = 'La clave USUARIO_REGISTRO no existe en el arreglo.';
  403. return $arrResponse;
  404. }
  405. if (!array_key_exists('FECHA_REGISTRO', $value)) {
  406. $arrResponse['error'] = true;
  407. $arrResponse['msg'] = 'La clave FECHA_REGISTRO no existe en el arreglo.';
  408. return $arrResponse;
  409. }
  410. if (!array_key_exists('USUARIO_MODIFICA', $value)) {
  411. $arrResponse['error'] = true;
  412. $arrResponse['msg'] = 'La clave USUARIO_MODIFICA no existe en el arreglo.';
  413. return $arrResponse;
  414. }
  415. if (!array_key_exists('FECHA_MODIFICA', $value)) {
  416. $arrResponse['error'] = true;
  417. $arrResponse['msg'] = 'La clave FECHA_MODIFICA no existe en el arreglo.';
  418. return $arrResponse;
  419. }
  420. foreach ($value as $keyItem => $item) {
  421. if (
  422. $keyItem !== 'USUARIO_REGISTRO' &&
  423. $keyItem !== 'FECHA_REGISTRO' &&
  424. $keyItem !== 'USUARIO_MODIFICA' &&
  425. $keyItem !== 'FECHA_MODIFICA'
  426. ) {
  427. $arrTemp[$keyValue][$keyItem] = $item;
  428. }
  429. }
  430. $userNumber = '';
  431. if (is_null($value['USUARIO_MODIFICA'])) {
  432. $userNumber = $value['USUARIO_REGISTRO'];
  433. } else {
  434. $userNumber = $value['USUARIO_MODIFICA'];
  435. }
  436. $lastUpdate = '';
  437. if (is_null($value['FECHA_MODIFICA'])) {
  438. $lastUpdate = $value['FECHA_REGISTRO'];
  439. } else {
  440. $lastUpdate = $value['FECHA_MODIFICA'];
  441. }
  442. try {
  443. $user = (array) DB::table('S002V01TUSUA')
  444. ->where('USUA_NULI', '=', $line)
  445. ->where('USUA_IDUS', '=', $userNumber)
  446. ->first();
  447. } catch (\Throwable $th) {
  448. $arrResponse['msg'] = 'Ocurrió un error al obtener el usuario "'.$userNumber.'": '.$th->getMessage();
  449. }
  450. $nameUser = $user['USUA_NOMB'].' '.$user['USUA_APPA'];
  451. if ( !is_null($user['USUA_APMA']) ) {
  452. $nameUser .= ' '.$user['USUA_APMA'];
  453. }
  454. $arrTemp[$keyValue]['USUARIO_MODIFICA'] = $nameUser.' ('.$userNumber.')';
  455. $responseDatetime = $this->reformatDatetime($lastUpdate);
  456. if ($responseDatetime['error']) {
  457. $arrResponse['error'] = true;
  458. $arrResponse['msg'] = $responseDatetime['msg'];
  459. return $arrResponse;
  460. }
  461. $arrTemp[$keyValue]['FECHA_MODIFICA'] = $responseDatetime['response'];
  462. }
  463. $arrResponse['response'] = $arrTemp;
  464. return $arrResponse;
  465. }
  466. public function reformatDatetime($datetime) {
  467. $arrResponse = array('error' => false, 'msg' => '', 'response' => []);
  468. $arrDatetime = explode(' ', $datetime);
  469. if ($arrDatetime === false || count($arrDatetime) !== 2) {
  470. $arrResponse['error'] = true;
  471. $arrResponse['msg'] = 'Ocurrió un error al obtener el formato de la Datetime.';
  472. return $arrResponse;
  473. }
  474. $date = $arrDatetime[0];
  475. $time = $arrDatetime[1];
  476. $responseDate = $this->formatOnlyDate($date);
  477. if ($responseDate['error']) {
  478. $arrResponse['error'] = true;
  479. $arrResponse['msg'] = $responseDate['msg'];
  480. return $arrResponse;
  481. }
  482. $formatDate = $responseDate['response'];
  483. $responseTime = $this->formatOnlyTime($time);
  484. if ($responseTime['error']) {
  485. $arrResponse['error'] = true;
  486. $arrResponse['msg'] = $responseTime['msg'];
  487. return $arrResponse;
  488. }
  489. $formatTime = $responseTime['response'];
  490. $arrResponse['response'] = $formatDate.' '.$formatTime;
  491. return $arrResponse;
  492. }
  493. public function formatOnlyDate(string $date) {
  494. $arrResponse = array('error' => false, 'msg' => '', 'response' => []);
  495. $arrDate = explode('-', $date);
  496. if ($arrDate === false || count($arrDate) !== 3) {
  497. $arrResponse['error'] = true;
  498. $arrResponse['msg'] = 'Ocurrió un error al obtener el formato de la fecha.';
  499. return $arrResponse;
  500. }
  501. $arrResponse['response'] = $arrDate[2].'-'.$arrDate[1].'-'.$arrDate[0];
  502. return $arrResponse;
  503. }
  504. public function formatOnlyTime(string $time) {
  505. $arrResponse = array('error' => false, 'msg' => '', 'response' => []);
  506. $arrTime = explode(':', $time);
  507. if ($arrTime === false || count($arrTime) != 3) {
  508. $arrResponse['error'] = true;
  509. $arrResponse['msg'] = 'Ocurrió un error al obtener el formato de la hora.';
  510. return $arrResponse;
  511. }
  512. $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);
  513. $hour = intval($arrTime[0]);
  514. $minute = intval($arrTime[1]);
  515. $second = intval($arrTime[2]);
  516. $format = 'AM';
  517. if (array_key_exists($hour, $arrSetTime)) {
  518. $format = 'PM';
  519. $hour = $arrSetTime[$hour];
  520. }
  521. $hour = $this->formatSecuence($hour, 2);
  522. $minute = $this->formatSecuence($minute, 2);
  523. $second = $this->formatSecuence($second, 2);
  524. $arrResponse['response'] = $hour.':'.$minute.':'.$second.' '.$format;
  525. return $arrResponse;
  526. }
  527. public function getUser($idUser, $line) {
  528. $arrResponse = array('error' => false, 'msg' => '', 'response' => []);
  529. try {
  530. $user = (array) DB::table('S002V01TUSUA')
  531. ->where('USUA_IDUS', '=', $idUser)
  532. ->where('USUA_NULI', '=', $line)
  533. ->first([
  534. 'USUA_NOMB AS NOMBRE',
  535. 'USUA_APPA AS APELLIDO_PATERNO',
  536. 'USUA_APMA AS APELLIDO_MATERNO',
  537. ]);
  538. } catch (\Throwable $th) {
  539. $arrResponse['error'] = true;
  540. $arrResponse['msg'] = 'Ocurrió un error al obtener el nombre del usuario.';
  541. return $arrResponse;
  542. }
  543. if (empty($user)) {
  544. $arrResponse['error'] = true;
  545. $arrResponse['msg'] = 'El usuario no existe.';
  546. return $arrResponse;
  547. }
  548. $nameUser = $user['NOMBRE'] . ' ' . $user['APELLIDO_PATERNO'];
  549. if (array_key_exists('APELLIDO_MATERNO', $user)) {
  550. $nameUser .= ' ' . $user['APELLIDO_MATERNO'];
  551. }
  552. $arrResponse['response'] = $nameUser;
  553. return $arrResponse;
  554. }
  555. public function formatBytes ($bytes, $decimal = 2) {
  556. if ($bytes === 0) {
  557. return '0 Bytes';
  558. }
  559. $k = 1024;
  560. $dm = $decimal < 0 ? 0 : $decimal;
  561. $sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  562. $i = floor(log($bytes) / log($k));
  563. return sprintf("%.{$dm}f", ($bytes / pow($k, $i))) . ' ' . $sizes[$i];
  564. }
  565. }