SymptomController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 SymptomController 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 getSymptom($line) {
  24. try {
  25. $getSymptom = DB::table('S002V01TSINT')
  26. ->join('S002V01TLIFA', 'SINT_IDFA', '=', 'LIFA_IDFA')
  27. ->join('S002V01TLIME', 'SINT_IDME', '=', 'LIME_IDME')
  28. ->where('SINT_NULI', '=', $line)
  29. ->get([
  30. 'SINT_NUSI', // NUMERO_SINTOMA
  31. 'SINT_COEQ', // CODIGO_EQUIPAMIENTO
  32. 'SINT_IDFA', // ID_FALLA
  33. 'LIFA_NOFA', // NOMBRE_FALLA
  34. 'SINT_CAUS', // CAUSA
  35. 'SINT_CLAS', // CLASIFICACION
  36. 'SINT_COME', // COMENTARIOS
  37. 'SINT_LIVA', // LIMITE_VALOR
  38. 'SINT_IDME', // ID_MEDIDA
  39. 'LIME_MEDI', // NOMBRE_MEDIDA
  40. 'LIME_ACRO', // ACRONIMO_MEDIDA
  41. 'SINT_ESTA', // ESTADO
  42. 'SINT_USRE', // USUARIO_REGISTRA
  43. 'SINT_FERE', // FECHA_REGISTRA
  44. 'SINT_USMO', // USUARIO_MODIFICA
  45. 'SINT_FEMO', // FECHA_MODIFICA
  46. ]);
  47. } catch (\Throwable $th) {
  48. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_GET000: No se pudo realizar la consulta a la base.", [], 500);
  49. }
  50. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $getSymptom);
  51. }
  52. public function registerSymptom(Request $request) {
  53. $validator = Validator::make($request->all(), [
  54. 'CODIGO_EQUIPAMIENTO' => 'required|string',
  55. 'ID_FALLA' => 'required|string',
  56. 'CAUSA' => 'required|string',
  57. 'CLASIFICACION' => 'required|string',
  58. 'COMENTARIOS' => 'required|string',
  59. 'LIMITE_VALOR' => 'required|string',
  60. 'ID_MEDIDA' => 'required|string',
  61. 'NUMERO_LINEA' => 'required|string',
  62. 'USUARIO' => 'required|string',
  63. ]);
  64. if ($validator->fails()) {
  65. return $this->responseController->makeResponse(
  66. true,
  67. "ERR_SYMPTOM_REG000: Se encontraron uno o más errores.",
  68. $this->responseController->makeErrors($validator->errors()->messages()),
  69. 401
  70. );
  71. }
  72. DB::beginTransaction();
  73. $request = $request->all();
  74. try {
  75. $user = $this->encController->decrypt($request['USUARIO']);
  76. } catch (\Throwable $th) {
  77. DB::rollBack();
  78. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_REG001: No se pudo obtener el usuario.", [], 500);
  79. }
  80. $line = $request['NUMERO_LINEA'];
  81. $currentDate = Carbon::now()->timezone('America/Mazatlan')->toDateTimeString();
  82. // Se valida que exista el ID de la lista de medidas
  83. try {
  84. $existMeasure = DB::table('S002V01TLIME')
  85. ->where('LIME_IDME', '=', $request['ID_MEDIDA'])
  86. ->where('LIME_NULI', '=', $line)
  87. ->where('LIME_ESTA', '=', 'Activo')
  88. ->exists();
  89. } catch (\Throwable $th) {
  90. DB::rollBack();
  91. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_REG002: Ocurrió al consultar la lista de medidas.", [], 500);
  92. }
  93. if ( !$existMeasure ) {
  94. DB::rollBack();
  95. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_REG003: No existe la medida seleccionada.", [], 500);
  96. }
  97. // Se valida que exista el ID de la lista de fallas
  98. try {
  99. $existFailure = DB::table('S002V01TLIFA')
  100. ->where('LIFA_IDFA', '=', $request['ID_FALLA'])
  101. ->where('LIFA_NULI', '=', $line)
  102. ->where('LIFA_ESTA', '=', 'Activo')
  103. ->get();
  104. } catch (\Throwable $th) {
  105. DB::rollBack();
  106. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_REG004: Ocurrió un error al consultar en la base de datos.", [], 500);
  107. }
  108. if (!$existFailure) {
  109. DB::rollBack();
  110. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_REG005: No se pudo encontrar el registro dentro de la base de datos.", [], 500);
  111. }
  112. $arrInsert = [
  113. 'SINT_COEQ' => trim($request['CODIGO_EQUIPAMIENTO']),
  114. 'SINT_IDFA' => trim($request['ID_FALLA']),
  115. 'SINT_CAUS' => trim($request['CAUSA']),
  116. 'SINT_CLAS' => trim($request['CLASIFICACION']),
  117. 'SINT_COME' => trim($request['COMENTARIOS']),
  118. 'SINT_LIVA' => trim($request['LIMITE_VALOR']),
  119. 'SINT_IDME' => trim($request['ID_MEDIDA']),
  120. 'SINT_NULI' => $line,
  121. 'SINT_USRE' => $user,
  122. 'SINT_FERE' => $currentDate,
  123. 'SINT_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  124. ];
  125. try {
  126. $response = DB::table('S002V01TSINT')->insert($arrInsert);
  127. } catch (\Throwable $th) {
  128. DB::rollBack();
  129. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_REG006: Ocurrió un error al insertar el formulario en la base de datos.", [], 500);
  130. }
  131. if (!$response) {
  132. DB::rollBack();
  133. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_REG007: No se pudo insertar el formulario en la base de datos.", [], 500);
  134. }
  135. DB::commit();
  136. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  137. }
  138. public function updateSymptom(Request $request) {
  139. $validator = Validator::make($request->all(), [
  140. 'NUMERO_SINTOMA' => 'required|string',
  141. 'CODIGO_EQUIPAMIENTO' => 'required|string',
  142. 'ID_FALLA' => 'required|string',
  143. 'CAUSA' => 'required|string',
  144. 'CLASIFICACION' => 'required|string',
  145. 'COMENTARIOS' => 'required|string',
  146. 'LIMITE_VALOR' => 'required|string',
  147. 'ID_MEDIDA' => 'required|string',
  148. 'NUMERO_LINEA' => 'required|string',
  149. 'USUARIO' => 'required|string',
  150. ]);
  151. if ($validator->fails()) {
  152. return $this->responseController->makeResponse(
  153. true,
  154. "ERR_SYMPTOM_UPD000: Se encontraron uno o más errores.",
  155. $this->responseController->makeErrors($validator->errors()->messages()),
  156. 401
  157. );
  158. }
  159. DB::beginTransaction();
  160. $request = $request->all();
  161. try {
  162. $user = $this->encController->decrypt($request['USUARIO']);
  163. } catch (\Throwable $th) {
  164. DB::rollBack();
  165. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_UPD001: No se pudo obtener el usuario.", [], 500);
  166. }
  167. $line = $request['NUMERO_LINEA'];
  168. $currentDate = Carbon::now()->timezone('America/Mazatlan')->toDateTimeString();
  169. // Se valida que exista el ID del síntoma
  170. try {
  171. $exist = DB::table('S002V01TSINT')
  172. ->where('SINT_NUSI', '=', $request['NUMERO_SINTOMA'])
  173. ->where('SINT_NULI', '=', $line)
  174. ->exists();
  175. } catch (\Throwable $th) {
  176. DB::rollBack();
  177. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_UPD002: Ocurrió un error al consultar en la base de datos.", [], 500);
  178. }
  179. if (!$exist) {
  180. DB::rollBack();
  181. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_UPD003: No se pudo encontrar el registro dentro de la base de datos.", [], 500);
  182. }
  183. // Se valida que exista el ID de la lista de medidas
  184. try {
  185. $existMeasure = DB::table('S002V01TLIME')
  186. ->where('LIME_IDME', '=', $request['ID_MEDIDA'])
  187. ->where('LIME_NULI', '=', $line)
  188. ->where('LIME_ESTA', '=', 'Activo')
  189. ->exists();
  190. } catch (\Throwable $th) {
  191. DB::rollBack();
  192. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_UPD004: Ocurrió al consultar la lista de medidas.", [], 500);
  193. }
  194. if ( !$existMeasure ) {
  195. DB::rollBack();
  196. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_UPD005: No existe la medida seleccionada.", [], 500);
  197. }
  198. // Se valida que exista el ID de la lista de fallas
  199. try {
  200. $existFailure = DB::table('S002V01TLIFA')
  201. ->where('LIFA_IDFA', '=', $request['ID_FALLA'])
  202. ->where('LIFA_NULI', '=', $line)
  203. ->where('LIFA_ESTA', '=', 'Activo')
  204. ->exists();
  205. } catch (\Throwable $th) {
  206. DB::rollBack();
  207. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_UPD006: Ocurrió un error al consultar la lista de fallas.", [], 500);
  208. }
  209. if (!$existFailure) {
  210. DB::rollBack();
  211. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_UPD007: No existe la falla seleccionada.", [], 500);
  212. }
  213. $arrUpdate = [
  214. 'SINT_COEQ' => trim($request['CODIGO_EQUIPAMIENTO']),
  215. 'SINT_IDFA' => trim($request['ID_FALLA']),
  216. 'SINT_CAUS' => trim($request['CAUSA']),
  217. 'SINT_CLAS' => trim($request['CLASIFICACION']),
  218. 'SINT_COME' => trim($request['COMENTARIOS']),
  219. 'SINT_LIVA' => trim($request['LIMITE_VALOR']),
  220. 'SINT_IDME' => trim($request['ID_MEDIDA']),
  221. 'SINT_USMO' => $user,
  222. 'SINT_FEMO' => $currentDate,
  223. 'SINT_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  224. ];
  225. try {
  226. $response = DB::table('S002V01TSINT')->where('SINT_NUSI', '=', $request['NUMERO_SINTOMA'])->where('SINT_NULI', '=', $line)->update($arrUpdate);
  227. } catch (\Throwable $th) {
  228. DB::rollBack();
  229. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_UPD008: Ocurrió un error al modificar el formulario en la base de datos.", [], 500);
  230. }
  231. if (!$response) {
  232. DB::rollBack();
  233. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_UPD009: No se pudo modificar el formulario en la base de datos.", [], 500);
  234. }
  235. DB::commit();
  236. return $this->responseController->makeResponse(true, "EXITO: Modificación Exitosa");
  237. }
  238. public function deleteSymptom(Request $request) {
  239. $validator = Validator::make($request->all(), [
  240. 'NUMERO_SINTOMA' => 'required|string',
  241. 'NUMERO_LINEA' => 'required|string',
  242. 'USUARIO' => 'required|string',
  243. ]);
  244. if ($validator->fails()) {
  245. return $this->responseController->makeResponse(
  246. true,
  247. "ERR_SYMPTOM_DEL000: Se encontraron uno o más errores.",
  248. $this->responseController->makeErrors($validator->errors()->messages()),
  249. 401
  250. );
  251. }
  252. DB::beginTransaction();
  253. $request = $request->all();
  254. $idSymptom = trim($request['NUMERO_SINTOMA']);
  255. $line = $request['NUMERO_LINEA'];
  256. $currentDate = Carbon::now()->timezone('America/Mazatlan')->toDateTimeString();
  257. try {
  258. $user = $this->encController->decrypt($request['USUARIO']);
  259. } catch (\Throwable $th) {
  260. DB::rollBack();
  261. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_DEL001: No se pudo obtener el usuario.", [], 500);
  262. }
  263. try {
  264. $exist = DB::table('S002V01TSINT')
  265. ->where('SINT_NUSI', '=', $idSymptom)
  266. ->where('SINT_NULI', '=', $line)
  267. ->exists();
  268. } catch (\Throwable $th) {
  269. DB::rollBack();
  270. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_DEL002: Ocurrió un error al consultar en la base de datos.", [], 500);
  271. }
  272. if (!$exist) {
  273. DB::rollBack();
  274. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_DEL003: No se pudo encontrar el registro dentro de la base de datos.", [], 500);
  275. }
  276. $arrUpdate = [
  277. 'SINT_ESTA' => 'Eliminado',
  278. 'SINT_USMO' => $user,
  279. 'SINT_FEMO' => $currentDate,
  280. 'SINT_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  281. ];
  282. try {
  283. $response = DB::table('S002V01TSINT')->where('SINT_NUSI', '=', $idSymptom)->where('SINT_NULI', '=', $line)->update($arrUpdate);
  284. } catch (\Throwable $th) {
  285. DB::rollBack();
  286. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_DEL004: Ocurrió un error al modificar el formulario en la base de datos.", [], 500);
  287. }
  288. if (!$response) {
  289. DB::rollBack();
  290. return $this->responseController->makeResponse(true, "ERR_SYMPTOM_DEL005: No se pudo modificar el formulario en la base de datos.", [], 500);
  291. }
  292. DB::commit();
  293. return $this->responseController->makeResponse(false, "ÉXITO: Eliminación Exitosa");
  294. }
  295. }