FailureController.php 19 KB

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