CatalogMeasuresController.php 10.0 KB

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