LogErrorsController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Carbon\Carbon;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Storage;
  7. use Illuminate\Support\Facades\Validator;
  8. use Illuminate\Database\Query\Builder;
  9. use Illuminate\Http\File;
  10. class LogErrorsController extends Controller{
  11. private $responseController;
  12. private $encryptionController;
  13. private $documentManagementController;
  14. private $functionsController;
  15. private $resourcesController;
  16. public function __construct(){
  17. $this->responseController = new ResponseController();
  18. $this->encryptionController = new EncryptionController();
  19. $this->documentManagementController = new DocumentManagementController();
  20. $this->functionsController = new FunctionsController();
  21. $this->resourcesController = new ResourcesController();
  22. }
  23. public function getLogYears($idUser, $line) {
  24. DB::enableQueryLog();
  25. $idUser = $this->encryptionController->decrypt($idUser);
  26. if(!$idUser){
  27. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  28. }
  29. $usr = DB::table('S002V01TUSUA')->where([
  30. ['USUA_NULI', '=', $line],
  31. ['USUA_IDUS', '=', $idUser],
  32. ])->first();
  33. if(is_null($usr)){
  34. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  35. }
  36. $logStoragePath = "C:\\inetpub\\wwwroot\\errors_storage";
  37. if(!file_exists($logStoragePath)){
  38. return $this->responseController->makeResponse(true, 'No se encontró el directorio de almacenamiento de los errores.', [], 404);
  39. }
  40. $availableYears = scandir($logStoragePath, SCANDIR_SORT_DESCENDING);
  41. $availableYearsFn = [];
  42. foreach($availableYears as $year){
  43. if($year == '.' || $year == '..') continue;
  44. $availableYearsFn[] = $this->encryptionController->encrypt($year);
  45. }
  46. return $this->responseController->makeResponse(false, 'EXITO.', $availableYearsFn);
  47. }
  48. public function getLogMonths($year, $idUser, $line){
  49. DB::enableQueryLog();
  50. $idUser = $this->encryptionController->decrypt($idUser);
  51. if(!$idUser){
  52. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  53. }
  54. $usr = DB::table('S002V01TUSUA')->where([
  55. ['USUA_NULI', '=', $line],
  56. ['USUA_IDUS', '=', $idUser],
  57. ])->first();
  58. if(is_null($usr)){
  59. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  60. }
  61. $year = $this->encryptionController->decrypt($year);
  62. if(!$year){
  63. return $this->responseController->makeResponse(true, 'El año enviado está encriptado correctamente.', [], 400);
  64. }else if(intval($year) <= 0){
  65. return $this->responseController->makeResponse(true, 'El año enviado es inválido.', [], 400);
  66. }
  67. $pathYear = "C:\\inetpub\\wwwroot\\errors_storage\\$year";
  68. if(!file_exists($pathYear)){
  69. return $this->responseController->makeResponse(true, 'El año enviado no está registrado en el log de errores.', [], 400);
  70. }
  71. $availableMonths = scandir($pathYear);
  72. $availableMonthsFn = [];
  73. foreach($availableMonths as $month){
  74. if($month == '.' || $month == '..') continue;
  75. $availableMonthsFn[] = $this->encryptionController->encrypt($month);
  76. }
  77. return $this->responseController->makeResponse(false, 'EXITO.', $availableMonthsFn);
  78. }
  79. public function getLogDays($month, $year, $idUser, $line){
  80. DB::enableQueryLog();
  81. $idUser = $this->encryptionController->decrypt($idUser);
  82. if(!$idUser){
  83. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  84. }
  85. $usr = DB::table('S002V01TUSUA')->where([
  86. ['USUA_NULI', '=', $line],
  87. ['USUA_IDUS', '=', $idUser],
  88. ])->first();
  89. if(is_null($usr)){
  90. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  91. }
  92. $year = $this->encryptionController->decrypt($year);
  93. if(!$year){
  94. return $this->responseController->makeResponse(true, 'El año enviado está encriptado correctamente.', [], 400);
  95. }else if(intval($year) <= 0){
  96. return $this->responseController->makeResponse(true, 'El año enviado es inválido.', [], 400);
  97. }
  98. $pathYear = "C:\\inetpub\\wwwroot\\errors_storage\\$year";
  99. if(!file_exists($pathYear)){
  100. return $this->responseController->makeResponse(true, 'El año enviado no está registrado en el log de errores.', [], 400);
  101. }
  102. $month = $this->encryptionController->decrypt($month);
  103. if(!$month){
  104. return $this->responseController->makeResponse(true, 'El mes enviado está encriptado correctamente.', [], 400);
  105. }else if(intval($month) <= 0){
  106. return $this->responseController->makeResponse(true, 'El mes enviado es inválido.', [], 400);
  107. }
  108. $pathMonth = "$pathYear\\$month";
  109. if(!file_exists($pathMonth)){
  110. return $this->responseController->makeResponse(true, 'El mes enviado no está registrado en el log de errores.', [], 400);
  111. }
  112. $availableDays = scandir($pathMonth);
  113. $availableDaysFn = [];
  114. foreach($availableDays as $day){
  115. if($day == '.' || $day == '..') continue;
  116. $availableDaysFn[] = $this->encryptionController->encrypt($day);
  117. }
  118. return $this->responseController->makeResponse(false, 'EXITO.', $availableDaysFn);
  119. }
  120. public function getDayErrors($day, $month, $year, $idUser, $line){
  121. DB::enableQueryLog();
  122. $idUser = $this->encryptionController->decrypt($idUser);
  123. if(!$idUser){
  124. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  125. }
  126. $usr = DB::table('S002V01TUSUA')->where([
  127. ['USUA_NULI', '=', $line],
  128. ['USUA_IDUS', '=', $idUser],
  129. ])->first();
  130. if(is_null($usr)){
  131. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  132. }
  133. $year = $this->encryptionController->decrypt($year);
  134. if(!$year){
  135. return $this->responseController->makeResponse(true, 'El año enviado está encriptado correctamente.', [], 400);
  136. }else if(intval($year) <= 0){
  137. return $this->responseController->makeResponse(true, 'El año enviado es inválido.', [], 400);
  138. }
  139. $pathYear = "C:\\inetpub\\wwwroot\\errors_storage\\$year";
  140. if(!file_exists($pathYear)){
  141. return $this->responseController->makeResponse(true, 'El año enviado no está registrado en el log de errores.', [], 400);
  142. }
  143. $month = $this->encryptionController->decrypt($month);
  144. if(!$month){
  145. return $this->responseController->makeResponse(true, 'El mes enviado está encriptado correctamente.', [], 400);
  146. }else if(intval($month) <= 0){
  147. return $this->responseController->makeResponse(true, 'El mes enviado es inválido.', [], 400);
  148. }
  149. $pathMonth = "$pathYear\\$month";
  150. if(!file_exists($pathMonth)){
  151. return $this->responseController->makeResponse(true, 'El mes enviado no está registrado en el log de errores.', [], 400);
  152. }
  153. $day = $this->encryptionController->decrypt($day);
  154. if(!$day){
  155. return $this->responseController->makeResponse(true, 'El día enviado no está encriptado correctamente.', [], 400);
  156. }else if(intval($day) <= 0){
  157. return $this->responseController->makeResponse(true, 'El día enviado es inválido.', [], 400);
  158. }
  159. $pathDay = "$pathMonth\\$day";
  160. if(!file_exists($pathDay)){
  161. return $this->responseController->makeResponse(true, 'El día enviado no está registrado en el log de errores.', [], 400);
  162. }
  163. $pathErrors = "$pathDay\\errors_log.json";
  164. if(!file_exists($pathErrors)){
  165. return $this->responseController->makeResponse(true, 'No se encontró el log de errores del día solicitado.', [], 400);
  166. }
  167. $errorsStr = file_get_contents($pathErrors);
  168. $errorsArr = json_decode($errorsStr, true);
  169. $errorsArrFn = [];
  170. foreach($errorsArr as $k=>$v){
  171. $errorDateTime = Carbon::createFromTimestamp($k, 'America/Mexico_city');
  172. $errorDateTimeStr = $errorDateTime->toDateTimeString();
  173. $errorsByDate = [];
  174. if(array_key_exists($errorDateTimeStr, $errorsArrFn)){
  175. $errorsByDate = $errorsArrFn[$errorDateTimeStr];
  176. }
  177. foreach($v as $k1=>$v1){
  178. $idError = $k1 + 1;
  179. $idErrorEnc = $this->encryptionController->encrypt($idError);
  180. $module = DB::table('S002V01TMODU')->where([
  181. ['MODU_IDMO', '=', $v1['MODULO']],
  182. ['MODU_NULI', '=', $line]
  183. ])->first();
  184. if(!is_null($module)){
  185. $v1['MODULO'] = "{$module->MODU_NOMO} ($v1[MODULO])";
  186. }
  187. $user = DB::table('S002V01TUSUA')->where([
  188. ['USUA_IDUS', '=', $v1['ID_USUARIO']],
  189. ['USUA_NULI', '=', $line]
  190. ])->first();
  191. if(!is_null($user)){
  192. $v1['ID_USUARIO'] = $this->functionsController->joinName($user->USUA_NOMB, $user->USUA_APPA, $user->USUA_APMA) . " ($v1[ID_USUARIO])";
  193. }
  194. $v1['NUM_ERROR'] = $idErrorEnc;
  195. $v1['MODULO'] = $this->encryptionController->encrypt($v1['MODULO']);
  196. $v1['ID_USUARIO'] = $this->encryptionController->encrypt($v1['ID_USUARIO']);
  197. $errorsByDate[] = $v1;
  198. }
  199. $errorsArrFn[$errorDateTimeStr] = $errorsByDate;
  200. }
  201. $errorsFn = [];
  202. foreach($errorsArrFn as $k=>$v){
  203. $errorsFn[] = [
  204. 'FECHA' => $k,
  205. 'ERRORES' => $v,
  206. ];
  207. }
  208. return $this->responseController->makeResponse(false, 'EXITO.', $errorsFn);
  209. }
  210. }