CatalogMeasuresController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 CatalogMeasuresController 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 getMeasures($line) {
  27. try {
  28. $getMeasures = DB::table('S002V01TLIME')
  29. ->where('LIME_NULI', '=', $line)
  30. ->get([
  31. 'LIME_IDME', // ID_MEDIDA
  32. 'LIME_MEDI', // MEDIDA
  33. 'LIME_ACRO', // ACRONIMO
  34. 'LIME_ESTA', // ESTADO
  35. 'LIME_USRE', // USUARIO_REGISTRA
  36. 'LIME_FERE', // FECHA_REGISTRA
  37. 'LIME_USMO', // USUARIO_MODIFICA
  38. 'LIME_FEMO', // FECHA_MODIFICA
  39. ]);
  40. } catch (\Throwable $th) {
  41. return $this->responseController->makeResponse(true, "ERR_MEASUREMENT_GET000: No se pudo realizar la consulta a la base.", [], 500);
  42. }
  43. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $getMeasures);
  44. }
  45. public function getMeasuresActives() {
  46. try {
  47. $getMeasures = DB::table('S002V01TLIME')
  48. ->where('LIME_ESTA', '=', 'Activo')
  49. ->where('LIME_NULI', '=', 1)
  50. ->get([
  51. 'LIME_IDME AS ID_MEDIDA',
  52. 'LIME_MEDI AS MEDIDA',
  53. 'LIME_ACRO AS ACRONIMO',
  54. 'LIME_ESTA AS ESTADO',
  55. 'LIME_USRE AS USUARIO_REGISTRA',
  56. 'LIME_FERE AS FECHA_REGISTRA',
  57. 'LIME_USMO AS USUARIO_MODIFICA',
  58. 'LIME_FEMO AS FECHA_MODIFICA',
  59. ]);
  60. } catch (\Throwable $th) {
  61. return $this->responseController->makeResponse(true, "ERR_MEASUREMENT_GET000: No se pudo realizar la consulta a la base.", [], 500);
  62. }
  63. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $getMeasures);
  64. }
  65. public function registerMeasures(Request $request) {
  66. $validator = Validator::make($request->all(), [
  67. 'MEDIDA' => 'required|string',
  68. 'ACRONIMO' => 'required|string',
  69. 'NUMERO_LINEA' => 'required|string',
  70. 'USUARIO' => 'required|string',
  71. ]);
  72. if ($validator->fails()) {
  73. return $this->responseController->makeResponse(
  74. true,
  75. "ERR_MEASUREMENT_REG000: Se encontraron uno o más errores.",
  76. $this->responseController->makeErrors($validator->errors()->messages()),
  77. 401
  78. );
  79. }
  80. DB::beginTransaction();
  81. $request = $request->all();
  82. $measurement = trim($request['MEDIDA']);
  83. $acronym = trim($request['ACRONIMO']);
  84. try {
  85. $user = $this->encController->decrypt($request['USUARIO']);
  86. } catch (\Throwable $th) {
  87. DB::rollBack();
  88. return $this->responseController->makeResponse(true, "ERR_FAILURES_REG001: No se pudo obtener el usuario.", $th, 500);
  89. }
  90. $line = $request['NUMERO_LINEA'];
  91. $now = $this->functionsController->now();
  92. $currentDate = $now->toDateTimeString();
  93. $arrInsert = [
  94. 'LIME_MEDI' => $measurement,
  95. 'LIME_ACRO' => $acronym,
  96. 'LIME_NULI' => $line,
  97. 'LIME_USRE' => $user,
  98. 'LIME_FERE' => $currentDate,
  99. 'LIME_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  100. ];
  101. try {
  102. $response = DB::table('S002V01TLIME')->insert($arrInsert);
  103. } catch (\Throwable $th) {
  104. DB::rollBack();
  105. return $this->responseController->makeResponse(true, "ERR_MEASUREMENT_REG001: Ocurrió un error al insertar el formulario en la base de datos.", $th, 500);
  106. }
  107. if (!$response) {
  108. DB::rollBack();
  109. return $this->responseController->makeResponse(true, "ERR_MEASUREMENT_REG002: No se pudo insertar el formulario en la base de datos.", [], 500);
  110. }
  111. DB::commit();
  112. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  113. }
  114. public function updateMeasures(Request $request) {
  115. $validator = Validator::make($request->all(), [
  116. 'ID_MEDIDA' => 'required|string',
  117. 'MEDIDA' => 'required|string',
  118. 'ACRONIMO' => 'required|string',
  119. 'NUMERO_LINEA' => 'required|string',
  120. 'USUARIO' => 'required|string',
  121. ]);
  122. if ($validator->fails()) {
  123. return $this->responseController->makeResponse(
  124. true,
  125. "ERR_MEASUREMENT_UPD000: Se encontraron uno o más errores.",
  126. $this->responseController->makeErrors($validator->errors()->messages()),
  127. 401
  128. );
  129. }
  130. DB::beginTransaction();
  131. $request = $request->all();
  132. $idMeasures = trim($request['ID_MEDIDA']);
  133. $measurement = trim($request['MEDIDA']);
  134. $acronym = trim($request['ACRONIMO']);
  135. try {
  136. $user = $this->encController->decrypt($request['USUARIO']);
  137. } catch (\Throwable $th) {
  138. DB::rollBack();
  139. return $this->responseController->makeResponse(true, "ERR_FAILURES_REG001: No se pudo obtener el usuario.", [], 500);
  140. }
  141. $line = $request['NUMERO_LINEA'];
  142. $now = $this->functionsController->now();
  143. $currentDate = $now->toDateTimeString();
  144. try {
  145. $exist = DB::table('S002V01TLIME')->where('LIME_IDME', '=', $idMeasures)->exists();
  146. } catch (\Throwable $th) {
  147. DB::rollBack();
  148. return $this->responseController->makeResponse(true, "ERR_MEASUREMENT_UPD001: Ocurrió un error al consultar en la base de datos.", $th, 500);
  149. }
  150. if (!$exist) {
  151. DB::rollBack();
  152. return $this->responseController->makeResponse(true, "ERR_MEASUREMENT_UPD002: No se pudo encontrar el registro dentro de la base de datos.", [], 500);
  153. }
  154. $arrUpdate = [
  155. 'LIME_MEDI' => $measurement,
  156. 'LIME_ACRO' => $acronym,
  157. 'LIME_USMO' => $user,
  158. 'LIME_FEMO' => $currentDate,
  159. 'LIME_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  160. ];
  161. try {
  162. $response = DB::table('S002V01TLIME')->where('LIME_IDME', '=', $idMeasures)->where('LIME_NULI', '=', $line)->update($arrUpdate);
  163. } catch (\Throwable $th) {
  164. DB::rollBack();
  165. return $this->responseController->makeResponse(true, "ERR_MEASUREMENT_UPD003: Ocurrió un error al modificar el formulario en la base de datos.", $th, 500);
  166. }
  167. if (!$response) {
  168. DB::rollBack();
  169. return $this->responseController->makeResponse(true, "ERR_MEASUREMENT_UPD004: No se pudo modificar el formulario en la base de datos.", [], 500);
  170. }
  171. DB::commit();
  172. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  173. }
  174. public function deleteMeasures(Request $request) {
  175. $validator = Validator::make($request->all(), [
  176. 'ID_MEDIDA' => 'required|integer',
  177. 'NUMERO_LINEA' => 'required|integer',
  178. 'USUARIO' => 'required|string',
  179. ]);
  180. if ($validator->fails()) {
  181. return $this->responseController->makeResponse(
  182. true,
  183. "ERR_MEASUREMENT_DEL000: Se encontraron uno o más errores.",
  184. $this->responseController->makeErrors($validator->errors()->messages()),
  185. 401
  186. );
  187. }
  188. DB::beginTransaction();
  189. $request = $request->all();
  190. $idMeasures = trim($request['ID_MEDIDA']);
  191. $line = $request['NUMERO_LINEA'];
  192. $now = $this->functionsController->now();
  193. $currentDate = $now->toDateTimeString();
  194. try {
  195. $user = $this->encController->decrypt($request['USUARIO']);
  196. } catch (\Throwable $th) {
  197. DB::rollBack();
  198. return $this->responseController->makeResponse(true, "ERR_FAILURES_REG001: No se pudo obtener el usuario.", [], 500);
  199. }
  200. try {
  201. $exist = DB::table('S002V01TLIME')->where('LIME_IDME', '=', $idMeasures)->exists();
  202. } catch (\Throwable $th) {
  203. DB::rollBack();
  204. return $this->responseController->makeResponse(true, "ERR_MEASUREMENT_DEL001: Ocurrió un error al consultar en la base de datos.", $th, 500);
  205. }
  206. if (!$exist) {
  207. DB::rollBack();
  208. return $this->responseController->makeResponse(true, "ERR_MEASUREMENT_DEL002: No se pudo encontrar el registro dentro de la base de datos.", [], 500);
  209. }
  210. $arrUpdate = [
  211. 'LIME_ESTA' => 'Eliminado',
  212. 'LIME_USMO' => $user,
  213. 'LIME_FEMO' => $currentDate,
  214. 'LIME_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  215. ];
  216. try {
  217. $response = DB::table('S002V01TLIME')->where('LIME_IDME', '=', $idMeasures)->where('LIME_NULI', '=', $line)->update($arrUpdate);
  218. } catch (\Throwable $th) {
  219. DB::rollBack();
  220. return $this->responseController->makeResponse(true, "ERR_MEASUREMENT_DEL003: Ocurrió un error al modificar el formulario en la base de datos.", $th, 500);
  221. }
  222. if (!$response) {
  223. DB::rollBack();
  224. return $this->responseController->makeResponse(true, "ERR_MEASUREMENT_DEL004: No se pudo modificar el formulario en la base de datos.", [], 500);
  225. }
  226. DB::commit();
  227. return $this->responseController->makeResponse(false, "ÉXITO: Eliminación Exitosa");
  228. }
  229. }