CatalogFailureController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 CatalogFailureController 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 getFailures($line) {
  24. try {
  25. $getFailures = DB::table('S002V01TLIFA')
  26. ->where('LIFA_NULI', '=', $line)
  27. ->get([
  28. 'LIFA_IDFA', // ID_FALLA
  29. 'LIFA_NOFA', // FALLA
  30. 'LIFA_ESTA', // ESTADO
  31. 'LIFA_USRE', // USUARIO_REGISTRA
  32. 'LIFA_FERE', // FECHA_REGISTRA
  33. 'LIFA_USMO', // USUARIO_MODIFICA
  34. 'LIFA_FEMO', // FECHA_MODIFICA
  35. ]);
  36. } catch (\Throwable $th) {
  37. return $this->responseController->makeResponse(true, "ERR_FAILURES_GET000: No se pudo realizar la consulta a la base.", [], 500);
  38. }
  39. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $getFailures);
  40. }
  41. public function getFailuresActives() {
  42. try {
  43. $getFailures = DB::table('S002V01TLIFA')
  44. ->where('LIFA_ESTA', '=', 'Activo')
  45. ->where('LIFA_NULI', '=', 1)
  46. ->get([
  47. 'LIFA_IDFA', // ID_FALLA
  48. 'LIFA_NOFA', // FALLA
  49. 'LIFA_ESTA', // ESTADO
  50. 'LIFA_USRE', // USUARIO_REGISTRA
  51. 'LIFA_FERE', // FECHA_REGISTRA
  52. 'LIFA_USMO', // USUARIO_MODIFICA
  53. 'LIFA_FEMO', // FECHA_MODIFICA
  54. ]);
  55. } catch (\Throwable $th) {
  56. return $this->responseController->makeResponse(true, "ERR_FAILURES_GET000: No se pudo realizar la consulta a la base.", [], 500);
  57. }
  58. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $getFailures);
  59. }
  60. public function registerFailures(Request $request) {
  61. $validator = Validator::make($request->all(), [
  62. 'FALLA' => 'required|string',
  63. 'NUMERO_LINEA' => 'required|string',
  64. 'USUARIO' => 'required|string',
  65. ]);
  66. if ($validator->fails()) {
  67. return $this->responseController->makeResponse(
  68. true,
  69. "ERR_FAILURES_REG000: Se encontraron uno o más errores.",
  70. $this->responseController->makeErrors($validator->errors()->messages()),
  71. 401
  72. );
  73. }
  74. DB::beginTransaction();
  75. $request = $request->all();
  76. try {
  77. $user = $this->encController->decrypt($request['USUARIO']);
  78. } catch (\Throwable $th) {
  79. DB::rollBack();
  80. return $this->responseController->makeResponse(true, "ERR_FAILURES_REG001: No se pudo obtener el usuario.", [], 500);
  81. }
  82. $nameFailures = trim($request['FALLA']);
  83. $line = $request['NUMERO_LINEA'];
  84. $currentDate = Carbon::now()->timezone('America/Mazatlan')->toDateTimeString();
  85. $arrInsert = [
  86. 'LIFA_NOFA' => $nameFailures,
  87. 'LIFA_NULI' => $line,
  88. 'LIFA_USRE' => $user,
  89. 'LIFA_FERE' => $currentDate,
  90. 'LIFA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  91. ];
  92. try {
  93. $validatorRegister = DB::table('S002V01TLIFA')->insert($arrInsert);
  94. } catch (\Throwable $th) {
  95. DB::rollBack();
  96. return $this->responseController->makeResponse(true, "ERR_FAILURES_REG001: Ocurrió un error al insertar el formulario en la base de datos.", $th, 500);
  97. }
  98. if (!$validatorRegister) {
  99. DB::rollBack();
  100. return $this->responseController->makeResponse(true, "ERR_FAILURES_REG002: No se pudo insertar el formulario en la base de datos.", [], 500);
  101. }
  102. DB::commit();
  103. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  104. }
  105. public function updateFailures(Request $request) {
  106. $validator = Validator::make($request->all(), [
  107. 'ID_FALLA' => 'required|string',
  108. 'FALLA' => 'required|string',
  109. 'NUMERO_LINEA' => 'required|string',
  110. 'USUARIO' => 'required|string',
  111. ]);
  112. if ($validator->fails()) {
  113. return $this->responseController->makeResponse(
  114. true,
  115. "ERR_FAILURES_UPD000: Se encontraron uno o más errores.",
  116. $this->responseController->makeErrors($validator->errors()->messages()),
  117. 401
  118. );
  119. }
  120. DB::beginTransaction();
  121. $request = $request->all();
  122. $idFailure = trim($request['ID_FALLA']);
  123. $nameFailures = trim($request['FALLA']);
  124. try {
  125. $user = $this->encController->decrypt($request['USUARIO']);
  126. } catch (\Throwable $th) {
  127. DB::rollBack();
  128. return $this->responseController->makeResponse(true, "ERR_FAILURES_REG001: No se pudo obtener el usuario.", [], 500);
  129. }
  130. $line = $request['NUMERO_LINEA'];
  131. $currentDate = Carbon::now()->timezone('America/Mazatlan')->toDateTimeString();
  132. try {
  133. $exist = DB::table('S002V01TLIFA')->where('LIFA_IDFA', '=', $idFailure)->exists();
  134. } catch (\Throwable $th) {
  135. DB::rollBack();
  136. return $this->responseController->makeResponse(true, "ERR_FAILURES_UPD001: Ocurrió un error al consultar en la base de datos.", $th, 500);
  137. }
  138. if (!$exist) {
  139. DB::rollBack();
  140. return $this->responseController->makeResponse(true, "ERR_FAILURES_UPD002: No se pudo encontrar el registro dentro de la base de datos.", [], 500);
  141. }
  142. $arrUpdate = [
  143. 'LIFA_NOFA' => $nameFailures,
  144. 'LIFA_USMO' => $user,
  145. 'LIFA_FEMO' => $currentDate,
  146. 'LIFA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  147. ];
  148. try {
  149. $response = DB::table('S002V01TLIFA')->where('LIFA_IDFA', '=', $idFailure)->where('LIFA_NULI', '=', $line)->update($arrUpdate);
  150. } catch (\Throwable $th) {
  151. DB::rollBack();
  152. return $this->responseController->makeResponse(true, "ERR_FAILURES_UPD003: Ocurrió un error al modificar el formulario en la base de datos.", $th, 500);
  153. }
  154. if (!$response) {
  155. DB::rollBack();
  156. return $this->responseController->makeResponse(true, "ERR_FAILURES_UPD004: No se pudo modificar el formulario en la base de datos.", [], 500);
  157. }
  158. DB::commit();
  159. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  160. }
  161. public function deleteFailures(Request $request) {
  162. $validator = Validator::make($request->all(), [
  163. 'ID_FALLA' => 'required|integer',
  164. 'NUMERO_LINEA' => 'required|integer',
  165. 'USUARIO' => 'required|string',
  166. ]);
  167. if ($validator->fails()) {
  168. return $this->responseController->makeResponse(
  169. true,
  170. "ERR_FAILURES_DEL000: Se encontraron uno o más errores.",
  171. $this->responseController->makeErrors($validator->errors()->messages()),
  172. 401
  173. );
  174. }
  175. DB::beginTransaction();
  176. $request = $request->all();
  177. $idFailure = trim($request['ID_FALLA']);
  178. try {
  179. $user = $this->encController->decrypt($request['USUARIO']);
  180. } catch (\Throwable $th) {
  181. DB::rollBack();
  182. return $this->responseController->makeResponse(true, "ERR_FAILURES_REG001: No se pudo obtener el usuario.", [], 500);
  183. }
  184. $line = $request['NUMERO_LINEA'];
  185. $currentDate = Carbon::now()->timezone('America/Mazatlan')->toDateTimeString();
  186. try {
  187. $exist = DB::table('S002V01TLIFA')->where('LIFA_IDFA', '=', $idFailure)->exists();
  188. } catch (\Throwable $th) {
  189. DB::rollBack();
  190. return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL001: Ocurrió un error al consultar en la base de datos.", $th, 500);
  191. }
  192. if (!$exist) {
  193. DB::rollBack();
  194. return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL002: No se pudo encontrar el registro dentro de la base de datos.", [], 500);
  195. }
  196. $arrUpdate = [
  197. 'LIFA_ESTA' => 'Eliminado',
  198. 'LIFA_USMO' => $user,
  199. 'LIFA_FEMO' => $currentDate,
  200. 'LIFA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  201. ];
  202. try {
  203. $response = DB::table('S002V01TLIFA')->where('LIFA_IDFA', '=', $idFailure)->where('LIFA_NULI', '=', $line)->update($arrUpdate);
  204. } catch (\Throwable $th) {
  205. DB::rollBack();
  206. return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL003: Ocurrió un error al modificar el formulario en la base de datos.", $th, 500);
  207. }
  208. if (!$response) {
  209. DB::rollBack();
  210. return $this->responseController->makeResponse(true, "ERR_FAILURES_DEL004: No se pudo modificar el formulario en la base de datos.", [], 500);
  211. }
  212. return $this->responseController->makeResponse(false, "ÉXITO: Eliminación Exitosa");
  213. }
  214. }