GenericController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. class GenericController extends Controller
  16. {
  17. private $responseController;
  18. private $encryptionController;
  19. public function __construct(){
  20. $this->responseController = new ResponseController();
  21. $this->encryptionController = new EncryptionController();
  22. }
  23. public function generateForm($numberForm, $line){
  24. try {
  25. $objForm = DB::table('S002V01TFODI')
  26. ->where('FODI_NUFO', '=', $numberForm)
  27. ->where('FODI_ESTA', '=', 'Activo')
  28. ->where('FODI_NULI', '=', $line)
  29. ->first([
  30. 'FODI_TIRE AS TITULO_REGISTRO',
  31. 'FODI_TIMO AS TITULO_MODIFICACION',
  32. 'FODI_DAFO AS DATA_FORM'
  33. ]);
  34. } catch (\Throwable $th) {
  35. return $this->responseController->makeResponse(true, "ERR_GENERIC_GEN000: No se pudo realizar la consulta a la base.", $th, 500);
  36. }
  37. if (empty($objForm)) {
  38. return $this->responseController->makeResponse(true, "ERR_GENERIC_GEN001: No se encontró el formulario", [], 500);
  39. }
  40. $dataFormDynamic = json_decode($objForm->DATA_FORM);
  41. try {
  42. for ($i = 0; $i < count($dataFormDynamic->fields); $i++){
  43. for ($j = 0; $j < count($dataFormDynamic->fields[$i]->form); $j++){
  44. if (
  45. $dataFormDynamic->fields[$i]->form[$j]->tag == 'select' &&
  46. array_key_exists('table_rel', (array) $dataFormDynamic->fields[$i]->form[$j]) &&
  47. $dataFormDynamic->fields[$i]->form[$j]->column_rel
  48. ){
  49. $nomTab = $dataFormDynamic->fields[$i]->form[$j]->table_rel->NOMBRE_TABLA;
  50. $column_rel = $dataFormDynamic->fields[$i]->form[$j]->column_rel;
  51. $data_select = $this->getDataSelect($nomTab,$column_rel);
  52. if($data_select['error']) return ["error" => true, "msg" => $data_select['msg']];
  53. $dataFormDynamic->fields[$i]->form[$j]->data_select = $data_select['response'];
  54. }
  55. }
  56. }
  57. } catch (\Throwable $th) {
  58. return $this->responseController->makeResponse(true, "ERR_GENERIC_GEN002: No se pudo realizar la consulta a la base.", [], 500);
  59. }
  60. $objResponse = [
  61. 'TITULO_REGISTRO' => $objForm->TITULO_REGISTRO,
  62. 'TITULO_MODIFICACION' => $objForm->TITULO_MODIFICACION,
  63. 'FIELDS' => $dataFormDynamic->fields
  64. ];
  65. return $this->responseController->makeResponse(false, "ÉXITO", $objResponse);
  66. }
  67. public function getDataInfo(Request $request, $numberForm){
  68. $request = $request->all();
  69. $idTableMain = $request['tables']['main_table'];
  70. $arr_id_secondary = $request['tables']['secondary_table'];
  71. if (empty($numberForm) || empty($numberForm)) {
  72. return $this->responseController->makeResponse(true, "ERR_DYNFORM_GET000: Los identificadores no pueden estar vacios.", [], 500);
  73. }
  74. try {
  75. $data_form = DB::table('S002V01TFODI')->where('FODI_NUFO', $numberForm)->first(['FODI_DAFO AS DATA_FORM', 'FODI_NOMB AS NOMBRE']);
  76. $data_json = json_decode($data_form->DATA_FORM);
  77. } catch (\Throwable $th) {
  78. return $this->responseController->makeResponse(true, "ERR_DYNFORM_GET001: No se pudo realizar la consulta a la base.", [], 500);
  79. }
  80. // Obtenemos los datos del formulario
  81. $arr_fields = $data_json->fields;
  82. $name_form = $data_form->NOMBRE;
  83. // Obtener el nombre de la tabla principal
  84. $table_main = $data_json->table_main;
  85. try {
  86. $str_table_main = $this->encryptionController->decrypt($table_main);
  87. } catch (\Throwable $th) {
  88. return $this->responseController->makeResponse(true, "ERR_DYNFORM_GET002: Ocurrió un error al obtener la tabla principal.", [], 500);
  89. }
  90. // Obtener el arreglo de las tablas relacionales
  91. $table_relationship = $data_json->table_relationship;
  92. try {
  93. $table_relationship = $this->encryptionController->decrypt($table_relationship);
  94. } catch (\Throwable $th) {
  95. return $this->responseController->makeResponse(true, "ERR_DYNFORM_GET003: Ocurrió un error al obtener las tablas relacionales", [], 500);
  96. }
  97. // Se obtienen los datos del formulario por tablas
  98. $arr_form_main = array();
  99. $arr_form_mm = array();
  100. $arr_form_secondary = array();
  101. foreach ($arr_fields as $field) {
  102. foreach ($field->form as $form) {
  103. $date_table = (array) $form->data_table;
  104. if ($date_table['table'] == $str_table_main) {
  105. $arr_form_main[] = $form;
  106. }else if(array_key_exists('columnMM', $date_table)){
  107. $arr_form_mm[] = $form;
  108. }else{
  109. $arr_form_secondary[] = $form;
  110. }
  111. }
  112. }
  113. // Se le asignan los valores al formulario principales
  114. $arr_resp = $this->getPrimaryKeyData($str_table_main);
  115. if ($arr_resp['error']) {
  116. return $this->responseController->makeResponse(true, "ERR_DYNFORM_GET004: No se pudo obtener la información de la llave principal.", [], 500);
  117. }else{
  118. $primary_key = $arr_resp['response']->COLUMN_NAME;
  119. try {
  120. $arr_data_main = (array) DB::table($str_table_main)->where($primary_key, '=', $idTableMain)->first();
  121. } catch (\Throwable $th) {
  122. return $this->responseController->makeResponse(true, "ERR_DYNFORM_GET005: No se pudo realizar la consulta a la base.", [], 500);
  123. }
  124. // Se asignan los valores obtenidos al formulario
  125. foreach ($arr_form_main as $data_form) {
  126. $column_form = $data_form->data_table->column;
  127. foreach ($arr_data_main as $key => $data_main) {
  128. if ($key == $column_form) {
  129. $data_form->value = $data_main;
  130. }
  131. }
  132. // Se obtienen los datos del select
  133. if ($data_form->tag == 'select' && $data_form->table_rel != null && $data_form->column_rel != null) {
  134. $nomTab = $data_form->table_rel->NOMBRE_TABLA;
  135. $column_rel = $data_form->column_rel;
  136. $data_select = $this->getDataSelect($nomTab,$column_rel);
  137. if($data_select['error']){
  138. return $this->responseController->makeResponse(true, "ERR_DYNFORM_GET006: No se pudo realizar la consulta a la base.", [], 500);
  139. }
  140. $data_form->data_select = $data_select['response'];
  141. }
  142. }
  143. }
  144. // Se le asignan los valores al formulario M:M
  145. foreach ($arr_form_mm as $form_mm) {
  146. $table = $form_mm->columnMM->table;
  147. $column = $form_mm->columnMM->column;
  148. $table_rel = $form_mm->table_rel->NOMBRE_TABLA;
  149. $column_rel = $form_mm->column_rel;
  150. $data_select = $this->getDataSelect($table_rel,$column_rel);
  151. if ($data_select['error']) {
  152. return $this->responseController->makeResponse(true, "ERR_DYNFORM_GET007: No se pudo realizar la consulta a la base.", [], 500);
  153. }else{
  154. $form_mm->data_select = [];
  155. foreach ($data_select['response'] as $value) {
  156. $form_mm->data_select[] = $value;
  157. }
  158. }
  159. try {
  160. $arr_data_mm = DB::table($table)->where($column, '=', $idTableMain)->get();
  161. } catch (\Throwable $th) {
  162. return $this->responseController->makeResponse(true, "ERR_DYNFORM_GET008: No se pudo realizar la consulta a la base.", [], 500);
  163. }
  164. $form_mm->value = [];
  165. foreach ($arr_data_mm as $value) {
  166. $form_mm->value[] = $value->PRAR_IDPR;
  167. }
  168. }
  169. // Se le asignan los valores al formulario secundarios
  170. foreach ($arr_id_secondary as $data_form) {
  171. $arr_resp = $this->getPrimaryKeyData($data_form['table']);
  172. if ($arr_resp['error']) {
  173. return $this->responseController->makeResponse(true, "ERR_DYNFORM_GET009: No se pudo obtener la información de la llave principal.", [], 500);
  174. }else{
  175. $primary_key = $arr_resp['response']->COLUMN_NAME;
  176. try {
  177. $arr_data_secondary = (array) DB::table($data_form['table'])->where($primary_key, '=', $data_form['id'])->first();
  178. } catch (\Throwable $th) {
  179. return $this->responseController->makeResponse(true, "ERR_DYNFORM_GET010: No se pudo realizar la consulta a la base.", [], 500);
  180. }
  181. }
  182. // Se asignan los valores obtenidos al formulario
  183. foreach ($arr_form_secondary as $form_secondary) {
  184. if ($form_secondary->data_table->table == $data_form['table']) {
  185. $column_form = $form_secondary->data_table->column;
  186. foreach ($arr_data_secondary as $key => $data_main) {
  187. if ($key == $column_form) {
  188. $form_secondary->value = $data_main;
  189. }
  190. }
  191. // Se obtienen los datos del select
  192. if ($form_secondary->tag == 'select' && $form_secondary->table_rel != null && $form_secondary->column_rel != null) {
  193. $nomTab = $form_secondary->table_rel->NOMBRE_TABLA;
  194. $column_rel = $form_secondary->column_rel;
  195. $data_select = $this->getDataSelect($nomTab,$column_rel);
  196. if($data_select['error']){
  197. return $this->responseController->makeResponse(true, "ERR_DYNFORM_GET011: No se pudo realizar la consulta a la base.", [], 500);
  198. }
  199. $form_secondary->data_select = $data_select['response'];
  200. }
  201. }
  202. }
  203. }
  204. foreach ($arr_fields as $key => $fields) {
  205. foreach ($fields->form as $key => $form) {
  206. $data_table = $form->data_table;
  207. foreach ($arr_form_main as $key => $form_main) {
  208. $data_table_main = $form_main->data_table;
  209. if ($data_table->table == $data_table_main->table && $data_table->column == $data_table_main->column) {
  210. $form->value = $form_main->value;
  211. }
  212. }
  213. foreach ($arr_form_mm as $key => $form_mm) {
  214. $data_table_main = $form_mm->data_table;
  215. if ($data_table->table == $data_table_main->table && $data_table->column == $data_table_main->column) {
  216. $form->value = $form_mm->value;
  217. }
  218. }
  219. foreach ($arr_form_secondary as $key => $form_secondary) {
  220. $data_table_main = $form_secondary->data_table;
  221. if ($data_table->table == $data_table_main->table && $data_table->column == $data_table_main->column) {
  222. $form->value = $form_secondary->value;
  223. }
  224. }
  225. }
  226. }
  227. $resp = [ 'NOMBRE_FORM' => $name_form, 'FIELDS' => $arr_fields];
  228. return $this->responseController->makeResponse(false, "ÉXITO", $resp);
  229. }
  230. public function registerForm(Request $request) {
  231. $validator = Validator::make($request->all(), [
  232. 'CODIGO_FORM' => 'required|string',
  233. 'DATA_VALUE' => 'required|string',
  234. 'USER' => 'required|string',
  235. 'LINE' => 'required|string',
  236. ]);
  237. if ($validator->fails()) {
  238. return $this->responseController->makeResponse(
  239. true,
  240. "ERR_GENERIC_REG000: Se encontraron uno o más errores.",
  241. $this->responseController->makeErrors($validator->errors()->messages()),
  242. 401
  243. );
  244. }
  245. $response = $request->all();
  246. // Se obtiene el número del formulario
  247. try {
  248. $numberForm = $this->encryptionController->decrypt($response['CODIGO_FORM']);
  249. } catch (\Throwable $th) {
  250. return $this->responseController->makeResponse(true,"ERR_GENERIC_REG001: Ocurrió al obtener el número de formulario.",[],500);
  251. }
  252. // Se obtiene la información a un objeto
  253. $arrDataValue = json_decode($response['DATA_VALUE']);
  254. // Se obtiene el usuario
  255. try {
  256. $user = $this->encryptionController->decrypt($response['USER']);
  257. } catch (\Throwable $th) {
  258. return $this->responseController->makeResponse(true,"ERR_GENERIC_REG002: Ocurrió un error al obtener el usuario.",[],500);
  259. }
  260. // Se obtiene el número de línea
  261. $line = $response['LINE'];
  262. try {
  263. // Se consulta la información del formulario
  264. $getForm = DB::table('S002V01TFODI')
  265. ->where('FODI_NUFO', '=', $numberForm)
  266. ->where('FODI_ESTA', '=', 'Activo')
  267. ->where('FODI_NULI', '=', $line)
  268. ->first([ 'FODI_DAFO' ]);
  269. // Si no encuentra ningún registro, entonces manda un mensaje de error
  270. if (empty($getForm)) {
  271. return $this->responseController->makeResponse(true,"ERR_GENERIC_REG003: No se encontró el formulario.",[],500);
  272. }
  273. } catch (\Throwable $th) {
  274. return $this->responseController->makeResponse(true,"ERR_GENERIC_REG004: Ocurrió un error en la consulta.",[],500);
  275. }
  276. // Se obtiene la información del formulario dinámico
  277. $arrFields = json_decode($getForm->FODI_DAFO);
  278. try {
  279. $mainTable = $this->encryptionController->decrypt($arrFields->table_main);
  280. } catch (\Throwable $th) {
  281. return $this->responseController->makeResponse(true,"ERR_GENERIC_REG005: Ocurrió un error al obtener la tabla principal",[],500);
  282. }
  283. // Se obtienen las columnas condicionales
  284. $arrInformationWhere = $arrFields->info_users_dates;
  285. // Se iteran según las condicionales
  286. foreach ($arrInformationWhere as $keyInfo => $informationWhere) {
  287. // Se obtiene el nombre de la tabla
  288. $tableNameInformation = $informationWhere->table;
  289. // Se inicializa el arreglo para la inserción
  290. $arrInsert = array();
  291. $codeTable = strtoupper(substr($mainTable, 8, strlen($mainTable)));
  292. // Se itera la información del formulario
  293. foreach ($arrDataValue as $key => $dataValue) {
  294. $tableNameForm = $dataValue->data_table->table;
  295. // Se verifica que la tabla de las condicionales coincida con la información del formulario
  296. if ($tableNameForm == $tableNameInformation) {
  297. // Se inserta la información de la columna con el valor perteneciente
  298. if (array_key_exists('value', (array) $dataValue)) {
  299. $arrInsert[$dataValue->column] = $dataValue->value;
  300. }else{
  301. $arrInsert[$dataValue->column] = false;
  302. }
  303. }
  304. if ($tableNameForm == 'NA') {
  305. $columnTable = $dataValue->data_table->column;
  306. $arrInsert[$codeTable.'_INEX'][$columnTable] = $dataValue->value;
  307. }
  308. }
  309. // Se ingresa en la tabla el usuario
  310. $arrInsert[$informationWhere->user_reg] = $user;
  311. $arrInsert[$informationWhere->date_reg] = Carbon::now()->timezone('America/Mazatlan')->toDateTimeString();
  312. $arrInsert[$informationWhere->timestamp] = DB::raw('CURRENT_TIMESTAMP');
  313. if (array_key_exists($codeTable.'_INEX', $arrInsert)) {
  314. $arrInsert[$codeTable.'_INEX'] = json_encode($arrInsert[$codeTable.'_INEX']);
  315. }
  316. // Se verifica que el arreglo contenga datos
  317. if (!empty($arrInsert)) {
  318. try {
  319. $response = DB::table($tableNameInformation)->insert($arrInsert);
  320. if (!$response) {
  321. return $this->responseController->makeResponse(true,"ERR_GENERIC_REG006: El registro no fue exitoso.",[],500);
  322. }
  323. } catch (\Exception $thx) {
  324. $message = $thx->getMessage();
  325. if (count(explode('Duplicate entry', $message)) == 2) {
  326. return $this->responseController->makeResponse(true,"ERR_GENERIC_REG007: El identificador se encuentra duplicado.",[],500);
  327. } else {
  328. return $this->responseController->makeResponse(true,"ERR_GENERIC_REG008: Ocurrió un error al momento de insertar los registros. $thx",[],500);
  329. }
  330. }
  331. }
  332. }
  333. return $this->responseController->makeResponse(false, "ÉXITO");
  334. }
  335. public function updateForm(Request $request) {
  336. $validator = Validator::make($request->all(), [
  337. 'CODIGO_FORM' => 'required|string',
  338. 'DATA_VALUE' => 'required|string',
  339. 'USER' => 'required|string',
  340. 'LINE' => 'required|string',
  341. ]);
  342. if ($validator->fails()) {
  343. return $this->responseController->makeResponse(
  344. true,
  345. "ERR_GENERIC_UPD000: Se encontraron uno o más errores.",
  346. $this->responseController->makeErrors($validator->errors()->messages()),
  347. 401
  348. );
  349. }
  350. $response = $request->all();
  351. // Se obtiene el número del formulario
  352. try {
  353. $numberForm = $this->encryptionController->decrypt($response['CODIGO_FORM']);
  354. } catch (\Throwable $th) {
  355. return $this->responseController->makeResponse(true,"ERR_GENERIC_UPD001: Ocurrió un error al obtener el número del formulario.",[],500);
  356. }
  357. // Se obtiene la información a un objeto
  358. $arrDataValue = json_decode($response['DATA_VALUE']);
  359. // Se obtiene el usuario
  360. try {
  361. $user = $this->encryptionController->decrypt($response['USER']);
  362. } catch (\Throwable $th) {
  363. return $this->responseController->makeResponse(true,"ERR_GENERIC_UPD002: Ocurrió un error al obtener el usuario.",[],500);
  364. }
  365. // Se obtiene el número de línea
  366. $line = $response['LINE'];
  367. try {
  368. // Se consulta la información del formulario
  369. $getForm = DB::table('S002V01TFODI')
  370. ->where('FODI_NUFO', '=', $numberForm)
  371. ->where('FODI_ESTA', '=', 'Activo')
  372. ->where('FODI_NULI', '=', $line)
  373. ->first([ 'FODI_DAFO' ]);
  374. // Si no encuentra ningún registro, entonces manda un mensaje de error
  375. if (empty($getForm)) {
  376. return $this->responseController->makeResponse(true,"ERR_GENERIC_UPD003: No se encontró el formulario.",[],500);
  377. }
  378. } catch (\Throwable $th) {
  379. return $this->responseController->makeResponse(true,"ERR_GENERIC_UPD004: Ocurrió un error en la consulta.",[],500);
  380. }
  381. // Se obtiene la información del formulario dinámico
  382. $arrFields = json_decode($getForm->FODI_DAFO);
  383. // Se obtienen las columnas condicionales
  384. $arrInformationWhere = $arrFields->info_users_dates;
  385. try {
  386. $mainTable = $this->encryptionController->decrypt($arrFields->table_main);
  387. } catch (\Throwable $th) {
  388. return $this->responseController->makeResponse(true,"ERR_GENERIC_UPD005: Ocurrió un error en la consulta.",[],500);
  389. }
  390. foreach ($arrInformationWhere as $keyInfo => $informationWhere) {
  391. // Se obtiene el nombre de la tabla
  392. $tableNameInformation = $informationWhere->table;
  393. // Se inicializa el arreglo para la inserción
  394. $arrUpdate = array();
  395. // Se inicializa el arreglo para la condicional
  396. $arrWhere = array();
  397. $codeTable = strtoupper(substr($mainTable, 8, strlen($mainTable)));
  398. // Se itera la información del formulario
  399. foreach ($arrDataValue as $key => $dataValue) {
  400. $tableNameForm = $dataValue->data_table->table;
  401. // Se verifica que la tabla de las condicionales coincida con la información del formulario
  402. if ($tableNameForm == $tableNameInformation) {
  403. // Se inserta la información de la columna con el valor perteneciente
  404. $arrUpdate[$dataValue->column] = $dataValue->value;
  405. }
  406. if ($tableNameForm == 'NA') {
  407. $columnTable = $dataValue->data_table->column;
  408. $arrUpdate[$codeTable.'_INEX'][$columnTable] = $dataValue->value;
  409. }
  410. }
  411. // Se ingresa en la tabla el usuario
  412. $arrUpdate[$informationWhere->user_mod] = $user;
  413. $arrUpdate[$informationWhere->date_mod] = Carbon::now()->timezone('America/Mazatlan')->toDateTimeString();
  414. $arrUpdate[$informationWhere->timestamp] = DB::raw('CURRENT_TIMESTAMP');
  415. if (array_key_exists($codeTable.'_INEX', $arrUpdate)) {
  416. $arrUpdate[$codeTable.'_INEX'] = json_encode($arrUpdate[$codeTable.'_INEX']);
  417. }
  418. foreach ($informationWhere->where as $keyInfoWhere => $infoWhere) {
  419. foreach ($arrDataValue as $key => $dataValue) {
  420. if ($infoWhere == $dataValue->column) {
  421. $arrWhere[$infoWhere] = $dataValue->value;
  422. }
  423. }
  424. }
  425. // Se verifica que el arreglo contenga datos
  426. if (!empty($arrUpdate)) {
  427. try {
  428. $responseUpdate = DB::table($tableNameInformation)->where($arrWhere)->update($arrUpdate);
  429. if (!$responseUpdate) {
  430. return $this->responseController->makeResponse(true,"ERR_GENERIC_UPD005: No se encontró el registros selecciondo.",[],500);
  431. }
  432. } catch (\Exception $th) {
  433. $message = $th->getMessage();
  434. if (count(explode('Duplicate entry', $message)) == 2) {
  435. return $this->responseController->makeResponse(true,"ERR_GENERIC_UPD006: El identificador se encuentra duplicado.",[],500);
  436. } else {
  437. return $this->responseController->makeResponse(true,"ERR_GENERIC_UPD007: Ocurrió un error al momento de insertar los registros. ",[],500);
  438. }
  439. }
  440. }
  441. }
  442. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  443. }
  444. /* ------------------------------------------------------ FUNCIONES ----------------------------------------------------------------------*/
  445. private function getConditions($conditions, $query, $flag = 0):string {
  446. foreach ($conditions as $condition){
  447. $queryAux = "";
  448. $cond1 = $condition->cond1;
  449. $cond2 = $condition->cond2;
  450. $queryAux .= "$cond1->table.$cond1->column $condition->ope ";
  451. if(!$condition->isCond2Column && !$condition->isCond2Request ){
  452. $queryAux .= $cond2." ";
  453. }else if($condition->isCond2Column && !$condition->isCond2Request){
  454. $queryAux .= "$cond2->table.$cond2->column ";
  455. }else if($condition->isCond2Request && !$condition->isCond2Column){
  456. $queryAux .= "";
  457. }else{
  458. return "";
  459. }
  460. if($condition->conditionAnidada){
  461. $flag ++;
  462. $query .= "$condition->operadorLogic ($queryAux";
  463. return $this->getConditions($condition->conditionAnidada, $query, $flag);
  464. }else{
  465. $query .= $condition->operadorLogic." ".$queryAux;
  466. if ($flag > 0){
  467. $query .= str_repeat(")", $flag);
  468. $flag = 0;
  469. }
  470. }
  471. }
  472. return $query;
  473. }
  474. private function getTablesName(){
  475. $databaseName = DB::connection()->getDatabaseName();
  476. try {
  477. $resp = DB::select('SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA="'.$databaseName.'"');
  478. } catch (\Throwable $th) {
  479. return ["error" => true, "msg" => "ERR_GENERIC_REG000: No se pudo realizar la consulta a la base."];
  480. }
  481. if ( count($resp) == 0) {
  482. return ["error" => true, "msg" => "ERR_GENERIC_REG001: Ocurrió un error con obtener los nombres de las tablas."];
  483. }
  484. return ["error" => false, "msg" => "ÉXITO", "response" => $resp];
  485. }
  486. private function getDataSelect($base_name, $column){
  487. $pk_data = $this->getPrimaryKeyData($base_name);
  488. if($pk_data['error']) return ["error" => true, "msg" => $pk_data['msg']];
  489. $pk = $pk_data['response']->COLUMN_NAME;
  490. try {
  491. $resp = DB::table($base_name)->get([ $pk.' AS valor', $column. ' AS opcion' ]);
  492. } catch (\Throwable $th) {
  493. return ["error" => true, "msg" => "ERR_GENERIC_REG000: No se pudo realizar la consulta a la base."];
  494. }
  495. return ["error" => false, "msg" => "ÉXITO", "response" => $resp];
  496. }
  497. private function getPrimaryKeyData($table_name_main){
  498. $databaseName = DB::connection()->getDatabaseName();
  499. try {
  500. $resp = DB::select(DB::raw("SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH AS LENGTH FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '".$databaseName."' AND TABLE_NAME = '".$table_name_main."' AND COLUMN_KEY = 'PRI'"));
  501. } catch (\Throwable $th) {
  502. return ["error" => true, "msg" => "ERR_GENERIC_REG000: No se pudo realizar la consulta a la base."];
  503. }
  504. if (count($resp) == 0) {
  505. return ["error" => true, "msg" => "ERR_GENERIC_REG001: No se pudo realizar la consulta a la base."];
  506. }
  507. return ["error" => false, "msg" => "ÉXITO", "response" => $resp[0]];
  508. }
  509. }