ModuleController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /*
  3. Desarrollador: Ing. Jean Jairo Benitez Meza
  4. Ultima Modificación: 11/04/2023
  5. Módulo: Formularios Dinámicos
  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 ModuleController 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. // Obtener Todos los Módulos (FUNCIONAL)
  27. public function getModule($user, $line){
  28. try {
  29. $resp = DB::table('S002V01TMODU')
  30. ->where('MODU_NULI', '=', $line)
  31. ->orderBy('MODU_IDMO', 'ASC')
  32. ->get([
  33. 'MODU_IDMO',
  34. 'MODU_NOMO AS MODU_NOMB',
  35. 'MODU_USMO',
  36. 'MODU_FEMO',
  37. // 'MODU_ESTA'
  38. ]);
  39. } catch (\Throwable $th) {
  40. return $this->responseController->makeResponse(true, "ERR_MODULE_REG000: No se pudo realizar la consulta a la base.", [], 500);
  41. }
  42. return $this->responseController->makeResponse(false, "ÉXITO", $resp);
  43. }
  44. // Obtener Todos los Módulos ACTIVOS
  45. public function getModuleAviable($user, $line){
  46. try {
  47. $resp = DB::table('S002V01TMODU')
  48. // ->where('MODU_ESTA', '=', 'Activo')
  49. ->where('MODU_NULI', '=', $line)
  50. ->orderBy('MODU_IDMO', 'ASC')
  51. ->get([ 'MODU_IDMO AS CODIGO_MODULO', 'MODU_NOMO AS MODULO']);
  52. } catch (\Throwable $th) {
  53. return $this->responseController->makeResponse(true, "ERR_MODULE_REG000: No se pudo realizar la consulta a la base.", [], 500);
  54. }
  55. return $this->responseController->makeResponse(false, "ÉXITO", $resp);
  56. }
  57. // Registro de Módulos (FUNCIONAL)
  58. public function registerModule(Request $request){
  59. $validator = Validator::make($request->all(), [
  60. 'linea' => 'required|integer',
  61. 'codigo' => 'required|string|max:10',
  62. 'modulo' => 'required|string|max:150',
  63. 'user' => 'required|string'
  64. ]);
  65. if ($validator->fails()) {
  66. return $this->responseController->makeResponse(
  67. true,
  68. "ERR_MODULE_REG000: Se encontraron uno o más errores.",
  69. $this->responseController->makeErrors($validator->errors()->messages()),
  70. 401
  71. );
  72. }
  73. DB::beginTransaction();
  74. $form = $request->all();
  75. try {
  76. $valid = DB::table('S002V01TMODU')->where('MODU_IDMO', $form['codigo'])->first();
  77. } catch (\Throwable $th) {
  78. DB::rollBack();
  79. return $this->responseController->makeResponse(true, "ERR_MODULE_REG001: No se pudo realizar la consulta a la base.", [], 500);
  80. }
  81. if (!empty($valid)) {
  82. DB::rollBack();
  83. return $this->responseController->makeResponse(true, "ERR_MODULE_REG002: El código ".$form['codigo']." ya existe.", [], 500);
  84. }
  85. $now = $this->functionsController->now();
  86. $currentDate = $now->toDateTimeString();
  87. $user = $this->encController->decrypt($form['user']);
  88. $ins = [
  89. 'MODU_NULI' => $form['linea'],
  90. 'MODU_IDMO' => $form['codigo'],
  91. 'MODU_NOMO' => $form['modulo'],
  92. 'MODU_USRE' => $user,
  93. 'MODU_FERE' => $currentDate,
  94. 'MODU_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  95. ];
  96. try {
  97. $validateInsert = DB::table('S002V01TMODU')->insert($ins);
  98. } catch (\Throwable $th) {
  99. DB::rollBack();
  100. return $this->responseController->makeResponse(true, "ERR_MODULE_REG003: Ocurrió un error al hacer la inserción en la base.", [], 500);
  101. }
  102. if (!$validateInsert) {
  103. DB::rollBack();
  104. return $this->responseController->makeResponse(true, "ERR_MODULE_REG004: No se pudo realizar la inserción en la base.", [], 500);
  105. }
  106. DB::commit();
  107. return $this->responseController->makeResponse(false, "EXITO: Registro correcto");
  108. }
  109. // Modificación de Módulos (FUNCIONAL)
  110. public function updateModule(Request $request){
  111. $validator = Validator::make($request->all(), [
  112. 'codigo' => 'required|string|max:10',
  113. 'modulo' => 'required|string|max:150',
  114. 'user' => 'required|string'
  115. ]);
  116. if ($validator->fails()) {
  117. return $this->responseController->makeResponse(
  118. true,
  119. "ERR_MODULE_REG000: Se encontraron uno o más errores.",
  120. $this->responseController->makeErrors($validator->errors()->messages()),
  121. 401
  122. );
  123. }
  124. DB::beginTransaction();
  125. $form = $request->all();
  126. try {
  127. $valid = DB::table('S002V01TMODU')
  128. ->where('MODU_IDMO', $form['codigo'])
  129. ->where('MODU_ESTA', '=', 'Activo')
  130. ->first();
  131. } catch (\Throwable $th) {
  132. DB::rollBack();
  133. return $this->responseController->makeResponse(true, "ERR_MODULE_REG001: No se pudo realizar la consulta a la base.", [], 500);
  134. }
  135. if (empty($valid)) {
  136. DB::rollBack();
  137. return $this->responseController->makeResponse(true, "ERR_MODULE_REG002: El código ".$form['codigo']." no existe.", [], 500);
  138. }
  139. try {
  140. $user = $this->encController->decrypt($request['user']);
  141. } catch (\Throwable $th) {
  142. DB::rollBack();
  143. return $this->responseController->makeResponse(true, "ERR_MODULE_REG003: No se pudo obtener el usuario.", [], 500);
  144. }
  145. $now = $this->functionsController->now();
  146. $currentDate = $now->toDateTimeString();
  147. $mdf = [
  148. 'MODU_NOMO' => $form['modulo'],
  149. 'MODU_USMO' => $user,
  150. 'MODU_FEMO' => $currentDate,
  151. 'MODU_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  152. ];
  153. try {
  154. $validateUdpdate = DB::table('S002V01TMODU')->where('MODU_IDMO', $form['codigo'])->update($mdf);
  155. } catch (\Throwable $th) {
  156. DB::rollBack();
  157. return $this->responseController->makeResponse(true, "ERR_MODULE_REG004: No se pudo realizar la modificación a la base.", [], 500);
  158. }
  159. if (!$validateUdpdate) {
  160. DB::rollBack();
  161. return $this->responseController->makeResponse(true, "ERR_MODULE_REG005: No se pudo realizar la inserción en la base.", [], 500);
  162. }
  163. DB::commit();
  164. return $this->responseController->makeResponse(false, "EXITO: Modificación Exitosa");
  165. }
  166. // Eliminación de Módulos (FUNCIONAL)
  167. public function deleteModule(Request $request){
  168. $validator = Validator::make($request->all(), [
  169. 'codigo' => 'required|string|max:10',
  170. 'user' => 'required|string'
  171. ]);
  172. if ($validator->fails()) {
  173. return $this->responseController->makeResponse(
  174. true,
  175. "ERR_MODULE_DEL000: Se encontraron uno o más errores.",
  176. $this->responseController->makeErrors($validator->errors()->messages()),
  177. 401
  178. );
  179. }
  180. DB::beginTransaction();
  181. $form = $request->all();
  182. try {
  183. $valid = DB::table('S002V01TMODU')->where('MODU_IDMO', $form['codigo'])->first();
  184. } catch (\Throwable $th) {
  185. DB::rollBack();
  186. return $this->responseController->makeResponse(true, "ERR_MODULE_DEL001: No se pudo realizar la consulta a la base.", [], 500);
  187. }
  188. if (empty($valid)) {
  189. DB::rollBack();
  190. return $this->responseController->makeResponse(true, "ERR_MODULE_DEL002: El código ".$form['codigo']." no existe.", [], 500);
  191. }
  192. if ( $valid->MODU_ESTA == 'Eliminado' ) {
  193. DB::rollBack();
  194. return $this->responseController->makeResponse(true, "ERR_MODULE_DEL003: El código ".$form['codigo']." ya está eliminado.", [], 500);
  195. }
  196. try {
  197. $user = $this->encController->decrypt($request['user']);
  198. } catch (\Throwable $th) {
  199. DB::rollBack();
  200. return $this->responseController->makeResponse(true, "ERR_MODULE_DEL004: No se pudo obtener el usuario.", [], 500);
  201. }
  202. $now = $this->functionsController->now();
  203. $currentDate = $now->toDateTimeString();
  204. $mdf = [
  205. 'MODU_ESTA' => 'Eliminado',
  206. 'MODU_USMO' => $user,
  207. 'MODU_FEMO' => $currentDate,
  208. 'MODU_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  209. ];
  210. try {
  211. $validateUpdate = DB::table('S002V01TMODU')->where('MODU_IDMO', $form['codigo'])->update($mdf);
  212. } catch (\Throwable $th) {
  213. DB::rollBack();
  214. return $this->responseController->makeResponse(true, "ERR_MODULE_DEL005: No se pudo realizar la consulta a la base.", $th, 500);
  215. }
  216. if (!$validateUpdate) {
  217. DB::rollBack();
  218. return $this->responseController->makeResponse(true, "ERR_MODULE_DEL006: No se pudo realizar la consulta a la base.", [], 500);
  219. }
  220. DB::commit();
  221. return $this->responseController->makeResponse(false, "EXITO: Modificación Exitosa");
  222. }
  223. }