SymptomController.php 14 KB

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