FailureListController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. <?php
  2. /*
  3. Desarrollador: Ing. Jean Jairo Benitez Meza
  4. Ultima Modificación: 15/09/2023
  5. Módulo: Analisis de Fallas
  6. */
  7. namespace App\Http\Controllers;
  8. use App\Http\Controllers\Controller;
  9. use App\Http\Controllers\ResponseController;
  10. use App\Http\Controllers\FunctionsController;
  11. use App\Http\Controllers\EncryptionController;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Carbon;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Validator;
  16. class FailureListController extends Controller
  17. {
  18. private $responseController;
  19. private $encController;
  20. private $functionsController;
  21. public function __construct() {
  22. $this->responseController = new ResponseController();
  23. $this->encController = new EncryptionController();
  24. $this->functionsController = new FunctionsController();
  25. }
  26. public function getFailures($user, $line) {
  27. try {
  28. $getFailures = DB::table('S002V01TLIFA')
  29. ->where('LIFA_NULI', '=', $line)
  30. ->get([
  31. 'LIFA_IDFA AS ID_FALLA',
  32. 'LIFA_NOFA AS NOMBRE_FALLA',
  33. 'LIFA_NIVE AS NIVEL_CRITICIDAD',
  34. 'LIFA_CAUS AS CAUSA_FALLA',
  35. 'LIFA_SOLU AS SOLICION',
  36. 'LIFA_DESC AS DESCRIPCION',
  37. 'LIFA_ESTA AS ESTADO',
  38. 'LIFA_USRE AS USUARIO_REGISTRA',
  39. 'LIFA_FERE AS FECHA_REGISTRA',
  40. 'LIFA_USMO AS USUARIO_MODIFICA',
  41. 'LIFA_FEMO AS FECHA_MODIFICA',
  42. ]);
  43. } catch (\Throwable $th) {
  44. return $this->responseController->makeResponse(true, "ERR_FAILURES_GET000: No se pudo realizar la consulta a la base.", [], 500);
  45. }
  46. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $getFailures);
  47. }
  48. public function getFailuresActives($user, $line) {
  49. try {
  50. $getFailures = DB::table('S002V01TLIFA')
  51. ->where('LIFA_ESTA', '=', 'Activo')
  52. ->where('LIFA_NULI', '=', 1)
  53. ->get([
  54. 'LIFA_IDFA AS ID_FALLA',
  55. 'LIFA_NOFA AS NOMBRE_FALLA',
  56. 'LIFA_NIVE AS NIVEL_CRITICIDAD',
  57. 'LIFA_CAUS AS CAUSA_FALLA',
  58. 'LIFA_SOLU AS SOLICION',
  59. 'LIFA_DESC AS DESCRIPCION',
  60. 'LIFA_ESTA AS ESTADO',
  61. 'LIFA_USRE AS USUARIO_REGISTRA',
  62. 'LIFA_FERE AS FECHA_REGISTRA',
  63. 'LIFA_USMO AS USUARIO_MODIFICA',
  64. 'LIFA_FEMO AS FECHA_MODIFICA',
  65. ]);
  66. } catch (\Throwable $th) {
  67. return $this->responseController->makeResponse(true, "ERR_FAILURES_GET000: No se pudo realizar la consulta a la base.", [], 500);
  68. }
  69. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $getFailures);
  70. }
  71. public function getFailureById($idFailure, $user, $line) {
  72. $idFailure = $this->encController->decrypt($idFailure);
  73. try {
  74. $arrFailures = (array) DB::table('S002V01TLIFA')
  75. ->where('LIFA_IDFA', '=', $idFailure)
  76. ->where('LIFA_NULI', '=', $line)
  77. ->where('LIFA_ESTA', '=', 'Activo')
  78. ->first([
  79. 'LIFA_IDFA AS ID_FALLA',
  80. 'LIFA_NOFA AS NOMBRE_FALLA',
  81. 'LIFA_NIVE AS NIVEL_CRITICIDAD',
  82. 'LIFA_CAUS AS CAUSA_FALLA',
  83. 'LIFA_SOLU AS SOLICION',
  84. 'LIFA_DESC AS DESCRIPCION',
  85. 'LIFA_ESTA AS ESTADO',
  86. 'LIFA_USRE AS USUARIO_REGISTRA',
  87. 'LIFA_FERE AS FECHA_REGISTRA',
  88. 'LIFA_USMO AS USUARIO_MODIFICA',
  89. 'LIFA_FEMO AS FECHA_MODIFICA',
  90. ]);
  91. } catch (\Throwable $th) {
  92. return $this->responseController->makeResponse(
  93. true,
  94. "ERR_FAILURES_GETBYID000: No se pudo realizar la consulta a la base.",
  95. $th->getMessage(),
  96. 500
  97. );
  98. }
  99. if ( !empty($arrFailures) ) {
  100. try {
  101. $arrEquipment = DB::table('S002V01TLFEQ')
  102. ->where('LFEQ_IDFA', '=', $arrFailures['ID_FALLA'])
  103. ->where('LFEQ_NULI', '=', $line)
  104. ->where('LFEQ_ESTA', '=', 'Activo')
  105. ->where('EQUI_NULI', '=', $line)
  106. ->join('S002V01TEQUI', 'EQUI_COEQ', '=', 'LFEQ_COEQ')
  107. ->get([
  108. 'EQUI_COEQ AS CODIGO',
  109. 'EQUI_TIPO AS TIPO',
  110. 'EQUI_MODE AS MODELO',
  111. 'EQUI_IDEQ AS ID_EQUIPO',
  112. 'EQUI_ESFU AS ESTADO_FUNCIONAMIENTO',
  113. // 'EQUI_GAIM AS GALERIA_IMAGENES',
  114. 'EQUI_ELOR AS ELEMENTO_ORIGEN',
  115. 'EQUI_TICO AS TIPO_CODIGO'
  116. ]);
  117. } catch (\Throwable $th) {
  118. return $this->responseController->makeResponse(
  119. true,
  120. "ERR_FAILURES_GETBYID001: No se pudo realizar la consulta a la base.",
  121. $th->getMessage(),
  122. 500
  123. );
  124. }
  125. $arrEquipment = json_decode( json_encode($arrEquipment), true );
  126. foreach ($arrEquipment as $keyEquipment => $equipment) {
  127. $equipment['ID_EQUIPO'] = strval($equipment['ID_EQUIPO']);
  128. $arrEquipment[$keyEquipment] = $equipment;
  129. }
  130. $arrFailures['EQUIPAMIENTOS'] = $arrEquipment;
  131. }
  132. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrFailures);
  133. }
  134. public function registerFailures(Request $request) {
  135. $validator = Validator::make($request->all(), [
  136. 'NOMBRE_FALLA' => 'required|string',
  137. 'NIVEL' => 'required|string',
  138. 'CAUSA' => 'required|string',
  139. 'SOLUCION' => 'required|string',
  140. 'DESCRIPCION' => 'required|string',
  141. 'EQUIPAMIENTOS' => 'required',
  142. 'NUMERO_LINEA' => 'required|string',
  143. 'USUARIO' => 'required|string',
  144. ]);
  145. if ($validator->fails()) {
  146. return $this->responseController->makeResponse(
  147. true,
  148. "ERR_FAILURES_REG000: Se encontraron uno o más errores.",
  149. $this->responseController->makeErrors($validator->errors()->messages()),
  150. 401
  151. );
  152. }
  153. DB::beginTransaction();
  154. $requestData = $request->all();
  155. try {
  156. $user = $this->encController->decrypt($requestData['USUARIO']);
  157. } catch (\Throwable $th) {
  158. DB::rollBack();
  159. return $this->responseController->makeResponse(true, "ERR_FAILURES_REG001: No se pudo obtener el usuario.", [], 500);
  160. }
  161. $now = $this->functionsController->now();
  162. $currentDate = $now->toDateTimeString();
  163. try {
  164. $idFailure = DB::table('S002V01TLIFA')->insertGetId([
  165. 'LIFA_NULI' => $requestData['NUMERO_LINEA'],
  166. 'LIFA_NOFA' => $requestData['NOMBRE_FALLA'],
  167. 'LIFA_NIVE' => $requestData['NIVEL'],
  168. 'LIFA_CAUS' => $requestData['CAUSA'],
  169. 'LIFA_SOLU' => $requestData['SOLUCION'],
  170. 'LIFA_DESC' => $requestData['DESCRIPCION'],
  171. 'LIFA_USRE' => $user,
  172. 'LIFA_FERE' => $currentDate,
  173. 'LIFA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  174. ]);
  175. } catch (\Throwable $th) {
  176. DB::rollBack();
  177. return $this->responseController->makeResponse(
  178. true,
  179. "ERR_FAILURES_REG002: Ocurrió un error al registrar el formulario en la lista de falla.",
  180. $th->getMessage(),
  181. 500
  182. );
  183. }
  184. if ( !$idFailure ) {
  185. DB::rollBack();
  186. return $this->responseController->makeResponse(
  187. true,
  188. "ERR_FAILURES_REG003: No se pudo registrar el formulario en la lista de falla.",
  189. [],
  190. 500
  191. );
  192. }
  193. foreach ($requestData['EQUIPAMIENTOS'] as $keyEquipment => $equipment) {
  194. try {
  195. $validateRegisterEquipment = DB::table('S002V01TLFEQ')->insert([
  196. 'LFEQ_NULI' => $requestData['NUMERO_LINEA'],
  197. 'LFEQ_IDFA' => $idFailure,
  198. 'LFEQ_COEQ' => $equipment,
  199. 'LFEQ_USRE' => $user,
  200. 'LFEQ_FERE' => $currentDate,
  201. 'LFEQ_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  202. ]);
  203. } catch (\Throwable $th) {
  204. DB::rollBack();
  205. return $this->responseController->makeResponse(
  206. true,
  207. "ERR_FAILURES_REG004: Ocurrió un error al registrar el formulario en la lista de falla.",
  208. $th->getMessage(),
  209. 500
  210. );
  211. }
  212. if ( !$validateRegisterEquipment ) {
  213. DB::rollBack();
  214. return $this->responseController->makeResponse(
  215. true,
  216. "ERR_FAILURES_REG005: No se pudo registrar el equipamiento $equipment en la lista de fallas.",
  217. [],
  218. 500
  219. );
  220. }
  221. }
  222. DB::commit();
  223. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  224. }
  225. public function updateFailures(Request $request) {
  226. $validator = Validator::make($request->all(), [
  227. 'ID_FALLA' => 'required|integer',
  228. 'NOMBRE_FALLA' => 'required|string',
  229. 'NIVEL' => 'required|string',
  230. 'CAUSA' => 'required|string',
  231. 'SOLUCION' => 'required|string',
  232. 'DESCRIPCION' => 'required|string',
  233. 'EQUIPAMIENTOS' => 'required',
  234. 'NUMERO_LINEA' => 'required|string',
  235. 'USUARIO' => 'required|string',
  236. ]);
  237. if ($validator->fails()) {
  238. return $this->responseController->makeResponse(
  239. true,
  240. "ERR_FAILURES_UPD000: Se encontraron uno o más errores.",
  241. $this->responseController->makeErrors($validator->errors()->messages()),
  242. 401
  243. );
  244. }
  245. DB::beginTransaction();
  246. $requestData = $request->all();
  247. try {
  248. $user = $this->encController->decrypt($request['USUARIO']);
  249. } catch (\Throwable $th) {
  250. DB::rollBack();
  251. return $this->responseController->makeResponse(true, "ERR_FAILURES_UPD001: No se pudo obtener el usuario.", [], 500);
  252. }
  253. $now = $this->functionsController->now();
  254. $currentDate = $now->toDateTimeString();
  255. try {
  256. $idFailure = DB::table('S002V01TLIFA')
  257. ->where('LIFA_NULI', '=', $requestData['NUMERO_LINEA'])
  258. ->where('LIFA_IDFA', '=', $requestData['ID_FALLA'])
  259. ->update([
  260. 'LIFA_NOFA' => $requestData['NOMBRE_FALLA'],
  261. 'LIFA_NIVE' => $requestData['NIVEL'],
  262. 'LIFA_CAUS' => $requestData['CAUSA'],
  263. 'LIFA_SOLU' => $requestData['SOLUCION'],
  264. 'LIFA_DESC' => $requestData['DESCRIPCION'],
  265. 'LIFA_USMO' => $user,
  266. 'LIFA_FEMO' => $currentDate,
  267. 'LIFA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  268. ]);
  269. } catch (\Throwable $th) {
  270. DB::rollBack();
  271. return $this->responseController->makeResponse(
  272. true,
  273. "ERR_FAILURES_UPD002: Ocurrió un error al modificar el formulario en la lista de falla.",
  274. $th->getMessage(),
  275. 500
  276. );
  277. }
  278. if ( !$idFailure ) {
  279. DB::rollBack();
  280. return $this->responseController->makeResponse(
  281. true,
  282. "ERR_FAILURES_UPD003: No se pudo modificar el formulario en la lista de falla.",
  283. [],
  284. 500
  285. );
  286. }
  287. foreach ($requestData['EQUIPAMIENTOS'] as $keyEquipment => $equipment) {
  288. try {
  289. $validateExists = DB::table('S002V01TLFEQ')
  290. ->where('LFEQ_IDFA', '=', $requestData['ID_FALLA'])
  291. ->where('LFEQ_NULI', '=', $requestData['NUMERO_LINEA'])
  292. ->where('LFEQ_COEQ', '=', $equipment)
  293. ->exists();
  294. } catch (\Throwable $th) {
  295. DB::rollBack();
  296. return $this->responseController->makeResponse(
  297. true,
  298. "ERR_FAILURES_UPD004: Ocurrió un error al consultar el equipamiento.",
  299. $th->getMessage(),
  300. 500
  301. );
  302. }
  303. if ( $validateExists ) {
  304. try {
  305. $validate = DB::table('S002V01TLFEQ')
  306. ->where('LFEQ_NULI', '=', $requestData['NUMERO_LINEA'])
  307. ->where('LFEQ_IDFA', '=', $requestData['ID_FALLA'])
  308. ->where('LFEQ_COEQ', '=', $equipment)
  309. ->update([
  310. 'LFEQ_ESTA' => 'Activo',
  311. 'LFEQ_USMO' => $user,
  312. 'LFEQ_FEMO' => $currentDate,
  313. 'LFEQ_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  314. ]);
  315. } catch (\Throwable $th) {
  316. DB::rollBack();
  317. return $this->responseController->makeResponse(
  318. true,
  319. "ERR_FAILURES_UPD005: Ocurrió un error al modificar el formulario en la lista de falla el equipamiento $equipment.",
  320. $th->getMessage(),
  321. 500
  322. );
  323. }
  324. } else {
  325. try {
  326. $validate = DB::table('S002V01TLFEQ')->insert([
  327. 'LFEQ_NULI' => $requestData['NUMERO_LINEA'],
  328. 'LFEQ_IDFA' => $requestData['ID_FALLA'],
  329. 'LFEQ_COEQ' => $equipment,
  330. 'LFEQ_USRE' => $user,
  331. 'LFEQ_FERE' => $currentDate,
  332. 'LFEQ_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  333. ]);
  334. } catch (\Throwable $th) {
  335. DB::rollBack();
  336. return $this->responseController->makeResponse(
  337. true,
  338. "ERR_FAILURES_UPD006: Ocurrió un error al registrar el formulario en la lista de falla el equipamiento $equipment.",
  339. $th->getMessage(),
  340. 500
  341. );
  342. }
  343. }
  344. if ( !$validate ) {
  345. DB::rollBack();
  346. return $this->responseController->makeResponse(
  347. true,
  348. "ERR_FAILURES_UPD007: No se pudo modificar el equipamiento $equipment en la lista de fallas.",
  349. [],
  350. 500
  351. );
  352. }
  353. }
  354. try {
  355. $getListEquipment = DB::table('S002V01TLFEQ')
  356. ->where('LFEQ_IDFA', '=', $requestData['ID_FALLA'])
  357. ->where('LFEQ_NULI', '=', $requestData['NUMERO_LINEA'])
  358. ->get([
  359. 'LFEQ_IDLF',
  360. 'LFEQ_IDFA',
  361. 'LFEQ_COEQ',
  362. 'LFEQ_ESTA',
  363. ]);
  364. } catch (\Throwable $th) {
  365. DB::rollBack();
  366. return $this->responseController->makeResponse(
  367. true,
  368. "ERR_FAILURES_UPD008: Ocurrió un error al consultar la lista de equipamientos.",
  369. $th->getMessage(),
  370. 500
  371. );
  372. }
  373. $arrListEquipment = json_decode( json_encode($getListEquipment), true );
  374. foreach ($arrListEquipment as $keyListEquipment => $listEquipment) {
  375. if( !in_array($listEquipment['LFEQ_COEQ'], $requestData['EQUIPAMIENTOS']) ) {
  376. try {
  377. $validateDelete = DB::table('S002V01TLFEQ')
  378. ->where('LFEQ_NULI', '=', $requestData['NUMERO_LINEA'])
  379. ->where('LFEQ_IDFA', '=', $requestData['ID_FALLA'])
  380. ->where('LFEQ_COEQ', '=', $listEquipment['LFEQ_COEQ'])
  381. ->update([
  382. 'LFEQ_ESTA' => 'Eliminado',
  383. 'LFEQ_USMO' => $user,
  384. 'LFEQ_FEMO' => $currentDate,
  385. 'LFEQ_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  386. ]);
  387. } catch (\Throwable $th) {
  388. DB::rollBack();
  389. return $this->responseController->makeResponse(
  390. true,
  391. "ERR_FAILURES_UPD009: Ocurrió un error al eliminar de la lista de falla el equipamiento ". $listEquipment['LFEQ_COEQ'].".",
  392. $th->getMessage(),
  393. 500
  394. );
  395. }
  396. if ( !$validateDelete ) {
  397. DB::rollBack();
  398. return $this->responseController->makeResponse(
  399. true,
  400. "ERR_FAILURES_UPD010: No se pudo eliminar de la lista de falla el equipamiento ". $listEquipment['LFEQ_COEQ'].".",
  401. [],
  402. 500
  403. );
  404. }
  405. }
  406. }
  407. DB::commit();
  408. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  409. }
  410. public function deleteFailures(Request $request) {
  411. $validator = Validator::make($request->all(), [
  412. 'ID_FALLA' => 'required|integer',
  413. 'NUMERO_LINEA' => 'required|integer',
  414. 'USUARIO' => 'required|string',
  415. ]);
  416. if ($validator->fails()) {
  417. return $this->responseController->makeResponse(
  418. true,
  419. "ERR_FAILURES_DEL000: Se encontraron uno o más errores.",
  420. $this->responseController->makeErrors($validator->errors()->messages()),
  421. 401
  422. );
  423. }
  424. DB::beginTransaction();
  425. $requestData = $request->all();
  426. try {
  427. $user = $this->encController->decrypt($request['USUARIO']);
  428. } catch (\Throwable $th) {
  429. DB::rollBack();
  430. return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL001: No se pudo obtener el usuario.", $th->getMessage(), 500);
  431. }
  432. $now = $this->functionsController->now();
  433. $currentDate = $now->toDateTimeString();
  434. try {
  435. $exist = DB::table('S002V01TLIFA')
  436. ->where('LIFA_IDFA', '=', $requestData['ID_FALLA'])
  437. ->where('LIFA_NULI', '=', $requestData['NUMERO_LINEA'])
  438. ->exists();
  439. } catch (\Throwable $th) {
  440. DB::rollBack();
  441. return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL002: Ocurrió un error al consultar en la base de datos.", $th->getMessage(), 500);
  442. }
  443. if (!$exist) {
  444. DB::rollBack();
  445. return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL003: No se pudo encontrar el registro dentro de la base de datos.", [], 500);
  446. }
  447. try {
  448. $validateUpdate = DB::table('S002V01TLIFA')
  449. ->where('LIFA_IDFA', '=', $requestData['ID_FALLA'])
  450. ->where('LIFA_NULI', '=', $requestData['NUMERO_LINEA'])
  451. ->update([
  452. 'LIFA_ESTA' => 'Eliminado',
  453. 'LIFA_USMO' => $user,
  454. 'LIFA_FEMO' => $currentDate,
  455. 'LIFA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  456. ]);
  457. } catch (\Throwable $th) {
  458. DB::rollBack();
  459. return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL004: Ocurrió un error al modificar el formulario en la base de datos.", $th->getMessage(), 500);
  460. }
  461. if (!$validateUpdate) {
  462. DB::rollBack();
  463. return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL005: No se pudo modificar el formulario en la base de datos.", [], 500);
  464. }
  465. try {
  466. $validateDelete = DB::table('S002V01TLFEQ')
  467. ->where('LFEQ_IDFA', '=', $requestData['ID_FALLA'])
  468. ->where('LFEQ_NULI', '=', $requestData['NUMERO_LINEA'])
  469. ->update([
  470. 'LFEQ_ESTA' => 'Eliminado',
  471. 'LFEQ_USMO' => $user,
  472. 'LFEQ_FEMO' => $currentDate,
  473. 'LFEQ_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  474. ]);
  475. } catch (\Throwable $th) {
  476. DB::rollBack();
  477. return $this->responseController->makeResponse(
  478. true,
  479. "ERR_FAILURES_DEL006: Ocurrió un error al eliminar la lista de equipamientos.",
  480. $th->getMessage(),
  481. 500
  482. );
  483. }
  484. if ( !$validateDelete ) {
  485. DB::rollBack();
  486. return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL007: No se pudo eliminar la lista de equipamientos.", [], 500);
  487. }
  488. DB::commit();
  489. return $this->responseController->makeResponse(false, "ÉXITO: Eliminación Exitosa");
  490. }
  491. }