FailureController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. /*
  3. Desarrollador: Ing. Jean Jairo Benitez Meza
  4. Ultima Modificación: 11/04/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\EncryptionController;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Carbon;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Validator;
  15. use App\Http\Controllers\FunctionsController;
  16. class FailureController 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 getFault($line) {
  27. try {
  28. $getFault = DB::table('S002V01TFALL')
  29. ->join('S002V01TLIFA', 'FALL_IDFA', '=', 'LIFA_IDFA')
  30. ->join('S002V01TLIME', 'FALL_IDME', '=', 'LIME_IDME')
  31. ->orderBy('FALL_NUFA', 'ASC')
  32. ->where('FALL_NULI', '=', $line)
  33. ->get([
  34. 'FALL_NUFA',
  35. 'FALL_COEQ',
  36. 'FALL_IDFA',
  37. 'LIFA_NOFA',
  38. 'LIME_MEDI',
  39. 'LIME_ACRO',
  40. 'FALL_CAUS',
  41. 'FALL_FEFA',
  42. 'FALL_CLAS',
  43. 'FALL_REPA',
  44. 'FALL_DESO',
  45. 'FALL_COME',
  46. 'FALL_LIVA',
  47. 'FALL_VAOB',
  48. 'FALL_IDME',
  49. 'FALL_NUSI',
  50. 'FALL_ESTA',
  51. 'FALL_USRE',
  52. 'FALL_FERE',
  53. 'FALL_USMO',
  54. 'FALL_FEMO',
  55. ]);
  56. } catch (\Throwable $th) {
  57. return $this->responseController->makeResponse(true, "ERR_FAULT_GET000: No se pudo realizar la consulta a la base.", [], 500);
  58. }
  59. return $this->responseController->makeResponse(false, "ÉXITO", $getFault);
  60. }
  61. public function getFaultByEquipment(Request $request) {
  62. $validator = Validator::make($request->all(), [
  63. 'CODIGO_EQUIPAMIENTO' => 'required|string',
  64. 'NUMERO_LINEA' => 'required|string',
  65. ]);
  66. if ($validator->fails()) {
  67. return $this->responseController->makeResponse(
  68. true,
  69. "ERR_FAULT_GET000: Se encontraron uno o más errores.",
  70. $this->responseController->makeErrors($validator->errors()->messages()),
  71. 401
  72. );
  73. }
  74. try {
  75. $getListFault = DB::table('S002V01TLIFA')
  76. ->where('FALL_COEQ', '=', $request['CODIGO_EQUIPAMIENTO'])
  77. ->where('FALL_NULI', '=', $request['NUMERO_LINEA'])
  78. ->join('S002V01TFALL', 'FALL_IDFA', '=', 'LIFA_IDFA')
  79. ->distinct()
  80. ->get('FALL_IDFA AS ID_FALLA');
  81. } catch (\Throwable $th) {
  82. return $this->responseController->makeResponse(true, "ERR_FAULT_GET001: No se pudo realizar la consulta a la base.", [], 500);
  83. }
  84. $request = $request->all();
  85. foreach ($getListFault as $key => $fault) {
  86. try {
  87. $getFault = DB::table('S002V01TFALL')
  88. ->where('FALL_COEQ', '=', $request['CODIGO_EQUIPAMIENTO'])
  89. ->where('FALL_NULI', '=', $request['NUMERO_LINEA'])
  90. ->where('FALL_IDFA', '=', $fault->ID_FALLA)
  91. ->join('S002V01TLIFA', 'FALL_IDFA', '=', 'LIFA_IDFA')
  92. ->join('S002V01TLIME', 'FALL_IDME', '=', 'LIME_IDME')
  93. ->get([
  94. 'FALL_NUFA AS NUMERO DE FALLA',
  95. 'FALL_COEQ AS CODIGO_EQUIPAMIENTO',
  96. 'FALL_IDFA AS ID_FALLA',
  97. 'LIFA_NOFA AS FALLA',
  98. 'FALL_CAUS AS CAUSA',
  99. 'FALL_FEFA AS FECHA_FALLA',
  100. 'FALL_CLAS AS CLASIFICACION',
  101. 'FALL_REPA AS REPARABLE',
  102. 'FALL_DESO AS DESCRIPCION DE SOLUCION',
  103. 'FALL_COME AS COMENARIOS',
  104. 'FALL_LIVA AS VALOR_LIMITE',
  105. 'FALL_VAOB AS VALOR_OBTENIDO',
  106. 'FALL_IDME AS ID_MEDIDA',
  107. 'LIME_MEDI AS MEDIDA',
  108. 'LIME_ACRO AS ACRONIMO',
  109. 'FALL_NUSI AS NUMERO_SINTOMA',
  110. 'FALL_ESTA AS ESTADO',
  111. 'FALL_USRE AS USUARIO_REGISTRA',
  112. 'FALL_FERE AS FECHA_REGISTRA',
  113. 'FALL_USMO AS USUARIO_MOFIFICA',
  114. 'FALL_FEMO AS FECHA_MOFIFICA',
  115. ]);
  116. } catch (\Throwable $th) {
  117. return $this->responseController->makeResponse(true, "ERR_FAULT_GET002: No se pudo realizar la consulta a la base.", [], 500);
  118. }
  119. $fault->FALLA = $getFault[0]->FALLA;
  120. $fault->LISTA = $getFault;
  121. }
  122. return $this->responseController->makeResponse(false, "ÉXITO", $getListFault);
  123. }
  124. public function registerFault(Request $request) {
  125. $validator = Validator::make($request->all(), [
  126. 'CODIGO_EQUIPAMIENTO' => 'required|string',
  127. 'ID_FALLA' => 'required|string',
  128. 'CAUSA' => 'required|string',
  129. 'FECHA' => 'required|string',
  130. 'CLASIFICACION' => 'required|string',
  131. 'REPARABLE' => 'required|boolean',
  132. 'COMENTARIOS' => 'required|string',
  133. 'LIMITE_VALOR' => 'required|string',
  134. 'VALOR_OBTENIDO' => 'required|string',
  135. 'ID_MEDIDA' => 'required|string',
  136. 'NUMERO_SINTOMA' => 'string',
  137. 'NUMERO_LINEA' => 'required|string',
  138. 'USUARIO' => 'required|string',
  139. ]);
  140. if ($validator->fails()) {
  141. return $this->responseController->makeResponse(
  142. true,
  143. "ERR_FAULT_REG000: Se encontraron uno o más errores.",
  144. $this->responseController->makeErrors($validator->errors()->messages()),
  145. 401
  146. );
  147. }
  148. DB::beginTransaction();
  149. $request = $request->all();
  150. $line = $request['NUMERO_LINEA'];
  151. $now = $this->functionsController->now();
  152. $currentDate = $now->toDateTimeString();
  153. try {
  154. $user = $this->encController->decrypt($request['USUARIO']);
  155. } catch (\Throwable $th) {
  156. DB::rollBack();
  157. return $this->responseController->makeResponse(true, "ERR_FAILURES_REG001: No se pudo obtener el usuario.", [], 500);
  158. }
  159. // Se valida que exista el ID del síntoma
  160. if ( $request['NUMERO_SINTOMA'] != null ) {
  161. try {
  162. $exist = DB::table('S002V01TSINT')
  163. ->where('SINT_NUSI', '=', $request['NUMERO_SINTOMA'])
  164. ->where('SINT_NULI', '=', $line)
  165. ->where('SINT_ESTA', '=', 'Activo')
  166. ->exists();
  167. } catch (\Throwable $th) {
  168. DB::rollBack();
  169. return $this->responseController->makeResponse(true, "ERR_FAULT_REG001: Ocurrió al consultar la lista de síntomas.", $th, 500);
  170. }
  171. if (!$exist) {
  172. DB::rollBack();
  173. return $this->responseController->makeResponse(true, "ERR_FAULT_REG002: No existe el síntoma seleccionado.", [], 500);
  174. }
  175. }
  176. // Se valida que exista el ID de la lista de medidas
  177. try {
  178. $existMeasure = DB::table('S002V01TLIME')
  179. ->where('LIME_IDME', '=', $request['ID_MEDIDA'])
  180. ->where('LIME_NULI', '=', $line)
  181. ->where('LIME_ESTA', '=', 'Activo')
  182. ->exists();
  183. } catch (\Throwable $th) {
  184. DB::rollBack();
  185. return $this->responseController->makeResponse(true, "ERR_FAULT_REG003: Ocurrió al consultar la lista de medidas.", $th, 500);
  186. }
  187. if ( !$existMeasure ) {
  188. DB::rollBack();
  189. return $this->responseController->makeResponse(true, "ERR_FAULT_REG004: No existe la medida seleccionada.", [], 500);
  190. }
  191. // Se valida que exista el ID de la lista de fallas
  192. try {
  193. $existFailure = DB::table('S002V01TLIFA')
  194. ->where('LIFA_IDFA', '=', $request['ID_FALLA'])
  195. ->where('LIFA_NULI', '=', $line)
  196. ->where('LIFA_ESTA', '=', 'Activo')
  197. ->exists();
  198. } catch (\Throwable $th) {
  199. DB::rollBack();
  200. return $this->responseController->makeResponse(true, "ERR_FAULT_REG005: Ocurrió un error al consultar la lista de fallas.", $th, 500);
  201. }
  202. if (!$existFailure) {
  203. DB::rollBack();
  204. return $this->responseController->makeResponse(true, "ERR_FAULT_REG006: No existe la falla seleccionada.", [], 500);
  205. }
  206. $arrInsert = [
  207. 'FALL_COEQ' => trim($request['CODIGO_EQUIPAMIENTO']),
  208. 'FALL_IDFA' => trim($request['ID_FALLA']),
  209. 'FALL_CAUS' => trim($request['CAUSA']),
  210. 'FALL_FEFA' => trim($request['FECHA']),
  211. 'FALL_CLAS' => trim($request['CLASIFICACION']),
  212. 'FALL_REPA' => trim($request['REPARABLE']),
  213. 'FALL_DESO' => $request['DESCRIPCION'] === null ? $request['DESCRIPCION'] : trim($request['DESCRIPCION']),
  214. 'FALL_COME' => trim($request['COMENTARIOS']),
  215. 'FALL_LIVA' => trim($request['LIMITE_VALOR']),
  216. 'FALL_VAOB' => trim($request['VALOR_OBTENIDO']),
  217. 'FALL_IDME' => trim($request['ID_MEDIDA']),
  218. 'FALL_NUSI' => $request['NUMERO_SINTOMA'] === null ? $request['NUMERO_SINTOMA'] : trim($request['NUMERO_SINTOMA']),
  219. 'FALL_NULI' => $line,
  220. 'FALL_USRE' => $user,
  221. 'FALL_FERE' => $currentDate,
  222. 'FALL_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  223. ];
  224. try {
  225. $response = DB::table('S002V01TFALL')->insert($arrInsert);
  226. } catch (\Throwable $th) {
  227. DB::rollBack();
  228. return $this->responseController->makeResponse(true, "ERR_FAULT_REG007: Ocurrió un error al insertar el formulario en la base de datos.", $th, 500);
  229. }
  230. if (!$response) {
  231. DB::rollBack();
  232. return $this->responseController->makeResponse(true, "ERR_FAULT_REG008: No se pudo insertar el formulario en la base de datos.", [], 500);
  233. }
  234. DB::commit();
  235. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  236. }
  237. public function updateFault(Request $request) {
  238. $validator = Validator::make($request->all(), [
  239. 'NUMERO_FALLA' => 'required|string',
  240. 'CODIGO_EQUIPAMIENTO' => 'required|string',
  241. 'ID_FALLA' => 'required|string',
  242. 'CAUSA' => 'required|string',
  243. 'FECHA' => 'required|string',
  244. 'CLASIFICACION' => 'required|string',
  245. 'REPARABLE' => 'required|boolean',
  246. 'COMENTARIOS' => 'required|string',
  247. 'LIMITE_VALOR' => 'required|string',
  248. 'VALOR_OBTENIDO' => 'required|string',
  249. 'ID_MEDIDA' => 'required|string',
  250. 'NUMERO_SINTOMA' => 'string',
  251. 'NUMERO_LINEA' => 'required|string',
  252. 'USUARIO' => 'required|string',
  253. ]);
  254. if ($validator->fails()) {
  255. return $this->responseController->makeResponse(
  256. true,
  257. "ERR_FAULT_UPD000: Se encontraron uno o más errores.",
  258. $this->responseController->makeErrors($validator->errors()->messages()),
  259. 401
  260. );
  261. }
  262. DB::beginTransaction();
  263. $request = $request->all();
  264. $idFault = trim($request['NUMERO_FALLA']);
  265. $line = $request['NUMERO_LINEA'];
  266. $now = $this->functionsController->now();
  267. $currentDate = $now->toDateTimeString();
  268. try {
  269. $user = $this->encController->decrypt($request['USUARIO']);
  270. } catch (\Throwable $th) {
  271. DB::rollBack();
  272. return $this->responseController->makeResponse(true, "ERR_FAILURES_REG001: No se pudo obtener el usuario.", [], 500);
  273. }
  274. try {
  275. $exist = DB::table('S002V01TFALL')->where('FALL_NUFA', '=', $idFault)->where('FALL_NULI', '=', $line)->exists();
  276. } catch (\Throwable $th) {
  277. DB::rollBack();
  278. return $this->responseController->makeResponse(true, "ERR_FAULT_UPD001: Ocurrió un error al consultar en la base de datos.", $th, 500);
  279. }
  280. if (!$exist) {
  281. DB::rollBack();
  282. return $this->responseController->makeResponse(true, "ERR_FAULT_UPD002: No se pudo encontrar el registro dentro de la base de datos.", [], 500);
  283. }
  284. // Se valida que exista el ID del síntoma
  285. if ( $request['NUMERO_SINTOMA'] != null ) {
  286. try {
  287. $exist = DB::table('S002V01TSINT')
  288. ->where('SINT_NUSI', '=', $request['NUMERO_SINTOMA'])
  289. ->where('SINT_NULI', '=', $line)
  290. ->where('SINT_ESTA', '=', 'Activo')
  291. ->exists();
  292. } catch (\Throwable $th) {
  293. DB::rollBack();
  294. return $this->responseController->makeResponse(true, "ERR_FAULT_UPD003: Ocurrió al consultar la lista de síntomas.", $th, 500);
  295. }
  296. if (!$exist) {
  297. DB::rollBack();
  298. return $this->responseController->makeResponse(true, "ERR_FAULT_UPD004: No existe el síntoma seleccionado.", [], 500);
  299. }
  300. }
  301. // Se valida que exista el ID de la lista de medidas
  302. try {
  303. $existMeasure = DB::table('S002V01TLIME')
  304. ->where('LIME_IDME', '=', $request['ID_MEDIDA'])
  305. ->where('LIME_NULI', '=', $line)
  306. ->where('LIME_ESTA', '=', 'Activo')
  307. ->exists();
  308. } catch (\Throwable $th) {
  309. DB::rollBack();
  310. return $this->responseController->makeResponse(true, "ERR_FAULT_UPD005: Ocurrió al consultar la lista de medidas.", $th, 500);
  311. }
  312. if ( !$existMeasure ) {
  313. DB::rollBack();
  314. return $this->responseController->makeResponse(true, "ERR_FAULT_UPD006: No existe la medida seleccionada.", [], 500);
  315. }
  316. // Se valida que exista el ID de la lista de fallas
  317. try {
  318. $existFailure = DB::table('S002V01TLIFA')
  319. ->where('LIFA_IDFA', '=', $request['ID_FALLA'])
  320. ->where('LIFA_NULI', '=', $line)
  321. ->where('LIFA_ESTA', '=', 'Activo')
  322. ->exists();
  323. } catch (\Throwable $th) {
  324. DB::rollBack();
  325. return $this->responseController->makeResponse(true, "ERR_FAULT_UPD007: Ocurrió un error al consultar la lista de fallas.", $th, 500);
  326. }
  327. if (!$existFailure) {
  328. DB::rollBack();
  329. return $this->responseController->makeResponse(true, "ERR_FAULT_UPD008: No existe la falla seleccionada.", [], 500);
  330. }
  331. $arrUpdate = [
  332. 'FALL_COEQ' => trim($request['CODIGO_EQUIPAMIENTO']),
  333. 'FALL_IDFA' => trim($request['ID_FALLA']),
  334. 'FALL_CAUS' => trim($request['CAUSA']),
  335. 'FALL_FEFA' => trim($request['FECHA']),
  336. 'FALL_CLAS' => trim($request['CLASIFICACION']),
  337. 'FALL_REPA' => trim($request['REPARABLE']),
  338. 'FALL_DESO' => $request['DESCRIPCION'] === null ? $request['DESCRIPCION'] : trim($request['DESCRIPCION']),
  339. 'FALL_COME' => trim($request['COMENTARIOS']),
  340. 'FALL_LIVA' => trim($request['LIMITE_VALOR']),
  341. 'FALL_VAOB' => trim($request['VALOR_OBTENIDO']),
  342. 'FALL_IDME' => trim($request['ID_MEDIDA']),
  343. 'FALL_NUSI' => $request['NUMERO_SINTOMA'] === null ? $request['NUMERO_SINTOMA'] : trim($request['NUMERO_SINTOMA']),
  344. 'FALL_USMO' => $user,
  345. 'FALL_FEMO' => $currentDate,
  346. 'FALL_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  347. ];
  348. try {
  349. $response = DB::table('S002V01TFALL')->where('FALL_NUFA', '=', $idFault)->where('FALL_NULI', '=', $line)->update($arrUpdate);
  350. } catch (\Throwable $th) {
  351. DB::rollBack();
  352. return $this->responseController->makeResponse(true, "ERR_FAULT_UPD009: Ocurrió un error al modificar el formulario en la base de datos.", $th, 500);
  353. }
  354. if (!$response) {
  355. DB::rollBack();
  356. return $this->responseController->makeResponse(true, "ERR_FAULT_UPD010: No se pudo modificar el formulario en la base de datos.", [], 500);
  357. }
  358. DB::commit();
  359. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  360. }
  361. public function deleteFault(Request $request) {
  362. $validator = Validator::make($request->all(), [
  363. 'NUMERO_FALLA' => 'required|string',
  364. 'NUMERO_LINEA' => 'required|string',
  365. 'USUARIO' => 'required|string',
  366. ]);
  367. if ($validator->fails()) {
  368. return $this->responseController->makeResponse(
  369. true,
  370. "ERR_FAULT_DEL000: Se encontraron uno o más errores.",
  371. $this->responseController->makeErrors($validator->errors()->messages()),
  372. 401
  373. );
  374. }
  375. DB::beginTransaction();
  376. $request = $request->all();
  377. $idFault = trim($request['NUMERO_FALLA']);
  378. $line = $request['NUMERO_LINEA'];
  379. $now = $this->functionsController->now();
  380. $currentDate = $now->toDateTimeString();
  381. try {
  382. $user = $this->encController->decrypt($request['USUARIO']);
  383. } catch (\Throwable $th) {
  384. DB::rollBack();
  385. return $this->responseController->makeResponse(true, "ERR_FAILURES_REG001: No se pudo obtener el usuario.", $th, 500);
  386. }
  387. try {
  388. $exist = DB::table('S002V01TFALL')->where('FALL_NUFA', '=', $idFault)->where('FALL_NULI', '=', $line)->exists();
  389. } catch (\Throwable $th) {
  390. DB::rollBack();
  391. return $this->responseController->makeResponse(true, "ERR_FAULT_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_FAULT_DEL002: No se pudo encontrar el registro dentro de la base de datos.", [], 500);
  396. }
  397. $arrUpdate = [
  398. 'FALL_ESTA' => 'Eliminado',
  399. 'FALL_USMO' => $user,
  400. 'FALL_FEMO' => $currentDate,
  401. 'FALL_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  402. ];
  403. try {
  404. $response = DB::table('S002V01TFALL')->where('FALL_NUFA', '=', $idFault)->where('FALL_NULI', '=', $line)->update($arrUpdate);
  405. } catch (\Throwable $th) {
  406. DB::rollBack();
  407. return $this->responseController->makeResponse(true, "ERR_FAULT_DEL003: Ocurrió un error al modificar el formulario en la base de datos.", $th, 500);
  408. }
  409. if (!$response) {
  410. DB::rollBack();
  411. return $this->responseController->makeResponse(true, "ERR_FAULT_DEL004: No se pudo modificar el formulario en la base de datos.", [], 500);
  412. }
  413. DB::commit();
  414. return $this->responseController->makeResponse(false, "ÉXITO: Eliminación Exitosa");
  415. }
  416. }