CatalogFailureController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 CatalogFailureController 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() {
  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('LDEQ_NULI', '=', $line)
  104. ->where('LEFQ_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($request['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. 'LDEQ_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('LEFQ_COEQ', '=', $equipment)
  292. ->where('LFEQ_NULI', '=', $requestData['NUMERO_LINEA'])
  293. ->exists();
  294. } catch (\Throwable $th) {
  295. DB::rollBack();
  296. return $this->responseController->makeResponse(
  297. true,
  298. "ERR_FAILURES_UPD004: No se pudo modificar el formulario en la lista de falla.",
  299. [],
  300. 500
  301. );
  302. }
  303. if ( $validateExists ) {
  304. try {
  305. $validate = DB::table('S002V01TLFEQ')
  306. ->where('LDEQ_NULI', '=', $requestData['NUMERO_LINEA'])
  307. ->where('LFEQ_IDFA', '=', $requestData['ID_FALLA'])
  308. ->update([
  309. 'LFEQ_COEQ' => $equipment,
  310. 'LFEQ_USMO' => $user,
  311. 'LFEQ_FEMO' => $currentDate,
  312. 'LFEQ_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  313. ]);
  314. } catch (\Throwable $th) {
  315. DB::rollBack();
  316. return $this->responseController->makeResponse(
  317. true,
  318. "ERR_FAILURES_UPD005: Ocurrió un error al modificar el formulario en la lista de falla el equipamiento $equipment.",
  319. $th->getMessage(),
  320. 500
  321. );
  322. }
  323. } else {
  324. try {
  325. $validate = DB::table('S002V01TLFEQ')->insert([
  326. 'LDEQ_NULI' => $requestData['NUMERO_LINEA'],
  327. 'LFEQ_IDFA' => $requestData['ID_FALLA'],
  328. 'LFEQ_COEQ' => $equipment,
  329. 'LFEQ_USRE' => $user,
  330. 'LFEQ_FERE' => $currentDate,
  331. 'LFEQ_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  332. ]);
  333. } catch (\Throwable $th) {
  334. DB::rollBack();
  335. return $this->responseController->makeResponse(
  336. true,
  337. "ERR_FAILURES_UPD006: Ocurrió un error al registrar el formulario en la lista de falla el equipamiento $equipment.",
  338. $th->getMessage(),
  339. 500
  340. );
  341. }
  342. }
  343. if ( !$validate ) {
  344. DB::rollBack();
  345. return $this->responseController->makeResponse(
  346. true,
  347. "ERR_FAILURES_UPD007: No se pudo modificar el equipamiento $equipment en la lista de fallas.",
  348. [],
  349. 500
  350. );
  351. }
  352. }
  353. DB::commit();
  354. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  355. }
  356. public function deleteFailures(Request $request) {
  357. $validator = Validator::make($request->all(), [
  358. 'ID_FALLA' => 'required|integer',
  359. 'NUMERO_LINEA' => 'required|integer',
  360. 'USUARIO' => 'required|string',
  361. ]);
  362. if ($validator->fails()) {
  363. return $this->responseController->makeResponse(
  364. true,
  365. "ERR_FAILURES_DEL000: Se encontraron uno o más errores.",
  366. $this->responseController->makeErrors($validator->errors()->messages()),
  367. 401
  368. );
  369. }
  370. DB::beginTransaction();
  371. $request = $request->all();
  372. $idFailure = trim($request['ID_FALLA']);
  373. try {
  374. $user = $this->encController->decrypt($request['USUARIO']);
  375. } catch (\Throwable $th) {
  376. DB::rollBack();
  377. return $this->responseController->makeResponse(true, "ERR_FAILURES_REG001: No se pudo obtener el usuario.", [], 500);
  378. }
  379. $line = $request['NUMERO_LINEA'];
  380. $now = $this->functionsController->now();
  381. $currentDate = $now->toDateTimeString();
  382. try {
  383. $exist = DB::table('S002V01TLIFA')
  384. ->where([
  385. ['LIFA_IDFA', '=', $idFailure],
  386. ['LIFA_NULI', '=', $line]
  387. ])
  388. ->get();
  389. } catch (\Throwable $th) {
  390. DB::rollBack();
  391. return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL001: Ocurrió un error al consultar en la base de datos.", $th, 500);
  392. }
  393. if (!$exist) {
  394. DB::rollBack();
  395. return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL002: No se pudo encontrar el registro dentro de la base de datos.", [], 500);
  396. }
  397. try {
  398. $validateUpdate = DB::table('S002V01TLIFA')
  399. ->where([
  400. ['LIFA_IDFA', '=', $idFailure],
  401. ['LIFA_NULI', '=', $line]
  402. ])
  403. ->update([
  404. 'LIFA_ESTA' => 'test',
  405. 'LIFA_USMO' => $user,
  406. 'LIFA_FEMO' => $currentDate,
  407. 'LIFA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  408. ]);
  409. } catch (\Throwable $th) {
  410. DB::rollBack();
  411. return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL003: Ocurrió un error al modificar el formulario en la base de datos.", $th, 500);
  412. }
  413. if (!$validateUpdate) {
  414. DB::rollBack();
  415. return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL004: No se pudo modificar el formulario en la base de datos.", [], 500);
  416. }
  417. DB::commit();
  418. return $this->responseController->makeResponse(false, "ÉXITO: Eliminación Exitosa");
  419. }
  420. }