CatalogMeasuresController.php 10 KB

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