ProcessManagementController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Validator;
  6. class ProcessManagementController extends Controller
  7. {
  8. private $responseController;
  9. private $encController;
  10. private $resourcesController;
  11. private $documentManagementController;
  12. private $functionsController;
  13. public function __construct(){
  14. $this->responseController = new ResponseController();
  15. $this->encController = new EncryptionController();
  16. $this->resourcesController = new ResourcesController();
  17. $this->documentManagementController = new DocumentManagementController();
  18. $this->functionsController = new FunctionsController();
  19. }
  20. public function getWorkflows($user, $line) {
  21. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  22. if ($arrResponseCheckUser['error']) {
  23. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  24. }
  25. try {
  26. $arrWorkflow = DB::table('S002V01TFLTR')
  27. ->where('FLTR_NULI', '=', $line)
  28. ->join('S002V01TMODU', 'MODU_IDMO', '=', 'FLTR_IDMO')
  29. ->get([
  30. 'FLTR_IDFT AS ID_FLUJO',
  31. 'FLTR_NOFT AS NOMBRE_FLUJO',
  32. 'FLTR_DEFT AS DESCRIPCION_FLUJO',
  33. 'FLTR_ESAU AS AUTOMATICO',
  34. 'FLTR_TIFT AS TIPO_FLUJO',
  35. 'MODU_IDMO AS ID_MODULO',
  36. 'MODU_NOMO AS NOMBRE_MODULO',
  37. 'FLTR_ESPR AS PREDEFINIDO',
  38. 'FLTR_ESTA AS ESTADO',
  39. 'FLTR_USRE AS USUARIO_REGISTRA',
  40. 'FLTR_FERE AS FECHA_REGISTRA',
  41. 'FLTR_USMO AS USUARIO_MODIFICA',
  42. 'FLTR_FEMO AS FECHA_MODIFICA',
  43. ]);
  44. $arrWorkflow = json_decode(json_encode($arrWorkflow), true);
  45. } catch (\Throwable $th) {
  46. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener los flujos de trabajo.', $th->getMessage(), 500);
  47. }
  48. $responseCheckLatestUpdate = $this->resourcesController->checkLatestUpdate($arrWorkflow, $line);
  49. if ($responseCheckLatestUpdate['error']) {
  50. return $this->responseController->makeResponse(true, $responseCheckLatestUpdate['msg'], [], 500);
  51. }
  52. $arrWorkflow = $responseCheckLatestUpdate['response'];
  53. foreach ($arrWorkflow as $key => $workflow) {
  54. $workflow['ID_FLUJO'] = '#'.$workflow['ID_FLUJO'];
  55. if ($workflow['TIPO_FLUJO'] === 'S') {
  56. $workflow['TIPO_FLUJO'] = 'Solicitud';
  57. } else if ($workflow['TIPO_FLUJO'] === 'F') {
  58. $workflow['TIPO_FLUJO'] = 'Formulario';
  59. }
  60. $arrWorkflow[$key] = $workflow;
  61. }
  62. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrWorkflow);
  63. }
  64. public function getWorkflow($idWorkflow, $user, $line) {
  65. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  66. if ($arrResponseCheckUser['error']) {
  67. DB::rollBack();
  68. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  69. }
  70. $user = $arrResponseCheckUser['response'];
  71. $idWorkflow = $this->encController->decrypt($idWorkflow);
  72. if (is_null($idWorkflow)) {
  73. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el ID del workflow.', [], 401);
  74. }
  75. try {
  76. $validateExists = DB::table('S002V01TFLTR')
  77. ->where('FLTR_IDFT', '=', $idWorkflow)
  78. ->where('FLTR_NULI', '=', $line)
  79. ->where('FLTR_ESTA', '=', 'Activo')
  80. ->exists();
  81. } catch (\Throwable $th) {
  82. return $this->responseController->makeResponse(true, "Ocurrió un error al verificar el workflow.", $th->getMessage(), 500);
  83. }
  84. if (!$validateExists) {
  85. return $this->responseController->makeResponse(true, "El workflow no existe.", [], 500);
  86. }
  87. try {
  88. $arrWorkflow = (array) DB::table('S002V01TFLTR')
  89. ->where('FLTR_IDFT', '=', $idWorkflow)
  90. ->where('FLTR_NULI', '=', $line)
  91. ->where('FLTR_ESTA', '=', 'Activo')
  92. ->first([
  93. 'FLTR_NOFT AS NOMBRE_FLUJO',
  94. 'FLTR_DEFT AS DESCRIPCION_FLUJO',
  95. 'FLTR_ESAU AS AUTOMATICO',
  96. 'FLTR_TIFT AS TIPO_FLUJO',
  97. 'FLTR_IDMO AS MODULO',
  98. ]);
  99. } catch (\Throwable $th) {
  100. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener los flujos de trabajo.', $th->getMessage(), 500);
  101. }
  102. $arrWorkflow['AUTOMATICO'] = $arrWorkflow['AUTOMATICO'] === 'Si' ? true : false;
  103. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrWorkflow);
  104. }
  105. public function registerWorkflow(Request $request) {
  106. $validator = Validator::make($request->all(), [
  107. 'NOMBRE_FLUJO' => 'required|string',
  108. 'DESCRIPCION_FLUJO' => 'required|string',
  109. 'AUTOMATICO' => 'required|boolean',
  110. 'TIPO_FLUJO' => 'required|string|in:S,F',
  111. 'MODULO' => 'required|string',
  112. 'USUARIO' => 'required|string',
  113. 'NUMERO_LINEA' => 'required|string',
  114. ]);
  115. if ($validator->fails()) {
  116. return $this->responseController->makeResponse(
  117. true,
  118. "Se encontraron uno o más errores.",
  119. $this->responseController->makeErrors($validator->errors()->messages()),
  120. 401
  121. );
  122. }
  123. DB::beginTransaction();
  124. $requestData = $request->all();
  125. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  126. if ($arrResponseCheckUser['error']) {
  127. DB::rollBack();
  128. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_REG001:'.$arrResponseCheckUser['msg'], [], 401);
  129. }
  130. $user = $arrResponseCheckUser['response'];
  131. $now = $this->functionsController->now();
  132. $currentDate = $now->toDateTimeString();
  133. try {
  134. $validateInsert = DB::table('S002V01TFLTR')->insert([
  135. 'FLTR_NULI' => $requestData['NUMERO_LINEA'],
  136. 'FLTR_NOFT' => $requestData['NOMBRE_FLUJO'],
  137. 'FLTR_DEFT' => $requestData['DESCRIPCION_FLUJO'],
  138. 'FLTR_ESAU' => $requestData['AUTOMATICO'] ? 'Si' : 'No',
  139. 'FLTR_TIFT' => $requestData['TIPO_FLUJO'],
  140. 'FLTR_IDMO' => $requestData['MODULO'],
  141. 'FLTR_USRE' => $user,
  142. 'FLTR_FERE' => $currentDate,
  143. 'FLTR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  144. ]);
  145. } catch (\Throwable $th) {
  146. DB::rollBack();
  147. return $this->responseController->makeResponse(true, 'Ocurrió un error al insertar el flujo de trabajo.', $th->getMessage(), 500);
  148. }
  149. if (!$validateInsert) {
  150. DB::rollBack();
  151. return $this->responseController->makeResponse(true, 'No se pudo insertar el flujo de trabajo.', [], 500);
  152. }
  153. DB::commit();
  154. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  155. }
  156. public function updateWorkflow(Request $request, $idWorkflow) {
  157. $validator = Validator::make($request->all(), [
  158. 'NOMBRE_FLUJO' => 'required|string',
  159. 'DESCRIPCION_FLUJO' => 'required|string',
  160. 'AUTOMATICO' => 'required|boolean',
  161. 'TIPO_FLUJO' => 'required|string|in:S,F',
  162. 'MODULO' => 'required|string',
  163. 'USUARIO' => 'required|string',
  164. 'NUMERO_LINEA' => 'required|string',
  165. ]);
  166. if ($validator->fails()) {
  167. return $this->responseController->makeResponse(
  168. true,
  169. "Se encontraron uno o más errores.",
  170. $this->responseController->makeErrors($validator->errors()->messages()),
  171. 401
  172. );
  173. }
  174. DB::beginTransaction();
  175. $requestData = $request->all();
  176. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  177. if ($arrResponseCheckUser['error']) {
  178. DB::rollBack();
  179. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  180. }
  181. $user = $arrResponseCheckUser['response'];
  182. $idWorkflow = $this->encController->decrypt($idWorkflow);
  183. if (is_null($idWorkflow)) {
  184. DB::rollBack();
  185. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el ID del workflow.', [], 401);
  186. }
  187. try {
  188. $lastWorkflow = (array) DB::table('S002V01TFLTR')
  189. ->where('FLTR_IDFT', '=', $idWorkflow)
  190. ->where('FLTR_NULI', '=', $requestData['NUMERO_LINEA'])
  191. ->where('FLTR_ESTA', '=', 'Activo')
  192. ->first();
  193. } catch (\Throwable $th) {
  194. DB::rollBack();
  195. return $this->responseController->makeResponse(true, "Ocurrió un error al verificar el workflow.", $th->getMessage(), 500);
  196. }
  197. if (empty($lastWorkflow)) {
  198. DB::rollBack();
  199. return $this->responseController->makeResponse(true, "El workflow no existe.", [], 500);
  200. }
  201. $strLast = $lastWorkflow['FLTR_HICA'];
  202. $arrLast = array();
  203. if (is_null($strLast)) {
  204. unset($lastWorkflow['FLTR_HICA']);
  205. $arrLast[] = $lastWorkflow;
  206. } else {
  207. $arrLast = json_decode($strLast);
  208. unset($lastWorkflow['FLTR_HICA']);
  209. $arrLast[] = $lastWorkflow;
  210. }
  211. $last = json_encode($arrLast);
  212. $now = $this->functionsController->now();
  213. $currentDate = $now->toDateTimeString();
  214. try {
  215. $validateUpdate = DB::table('S002V01TFLTR')
  216. ->where('FLTR_IDFT', '=', $idWorkflow)
  217. ->where('FLTR_NULI', '=', $requestData['NUMERO_LINEA'])
  218. ->where('FLTR_ESTA', '=', 'Activo')
  219. ->update([
  220. 'FLTR_NOFT' => $requestData['NOMBRE_FLUJO'],
  221. 'FLTR_DEFT' => $requestData['DESCRIPCION_FLUJO'],
  222. 'FLTR_ESAU' => $requestData['AUTOMATICO'] ? 'Si' : 'No',
  223. 'FLTR_TIFT' => $requestData['TIPO_FLUJO'],
  224. 'FLTR_IDMO' => $requestData['MODULO'],
  225. 'FLTR_HICA' => $last,
  226. 'FLTR_USMO' => $user,
  227. 'FLTR_FEMO' => $currentDate,
  228. 'FLTR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  229. ]);
  230. } catch (\Throwable $th) {
  231. DB::rollBack();
  232. return $this->responseController->makeResponse(true, 'Ocurrió un error al modificar el flujo de trabajo.', $th->getMessage(), 500);
  233. }
  234. if (!$validateUpdate) {
  235. DB::rollBack();
  236. return $this->responseController->makeResponse(true, 'No se pudo modificar el flujo de trabajo.', [], 500);
  237. }
  238. DB::commit();
  239. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  240. }
  241. public function deleteWorkflow(Request $request, $idWorkflow) {
  242. $validator = Validator::make($request->all(), [
  243. 'USUARIO' => 'required|string',
  244. 'NUMERO_LINEA' => 'required|string',
  245. ]);
  246. if ($validator->fails()) {
  247. return $this->responseController->makeResponse(
  248. true,
  249. "Se encontraron uno o más errores.",
  250. $this->responseController->makeErrors($validator->errors()->messages()),
  251. 401
  252. );
  253. }
  254. DB::beginTransaction();
  255. $requestData = $request->all();
  256. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  257. if ($arrResponseCheckUser['error']) {
  258. DB::rollBack();
  259. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  260. }
  261. $user = $arrResponseCheckUser['response'];
  262. $idWorkflow = $this->encController->decrypt($idWorkflow);
  263. if (is_null($idWorkflow)) {
  264. DB::rollBack();
  265. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el ID del workflow.', [], 401);
  266. }
  267. try {
  268. $validateExists = DB::table('S002V01TFLTR')
  269. ->where('FLTR_IDFT', '=', $idWorkflow)
  270. ->where('FLTR_NULI', '=', $requestData['NUMERO_LINEA'])
  271. ->where('FLTR_ESTA', '=', 'Activo')
  272. ->exists();
  273. } catch (\Throwable $th) {
  274. DB::rollBack();
  275. return $this->responseController->makeResponse(true, "Ocurrió un error al verificar el workflow.", $th->getMessage(), 500);
  276. }
  277. if (!$validateExists) {
  278. DB::rollBack();
  279. return $this->responseController->makeResponse(true, "El workflow no existe.", [], 500);
  280. }
  281. $now = $this->functionsController->now();
  282. $currentDate = $now->toDateTimeString();
  283. try {
  284. $validateDelete = DB::table('S002V01TFLTR')
  285. ->where('FLTR_IDFT', '=', $idWorkflow)
  286. ->where('FLTR_NULI', '=', $requestData['NUMERO_LINEA'])
  287. ->where('FLTR_ESTA', '=', 'Activo')
  288. ->update([
  289. 'FLTR_ESTA' => 'Eliminado',
  290. 'FLTR_USMO' => $user,
  291. 'FLTR_FEMO' => $currentDate,
  292. 'FLTR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  293. ]);
  294. } catch (\Throwable $th) {
  295. DB::rollBack();
  296. return $this->responseController->makeResponse(true, 'Ocurrió un error al eliminar el flujo de trabajo.', $th->getMessage(), 500);
  297. }
  298. if (!$validateDelete) {
  299. DB::rollBack();
  300. return $this->responseController->makeResponse(true, 'No se pudo eliminar el flujo de trabajo.', [], 500);
  301. }
  302. DB::commit();
  303. return $this->responseController->makeResponse(false, "ÉXITO: Eliminación Exitosa");
  304. }
  305. public function getHistoryWorkflow($idWorkflow, $user, $line) {
  306. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  307. if ($arrResponseCheckUser['error']) {
  308. DB::rollBack();
  309. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  310. }
  311. $user = $arrResponseCheckUser['response'];
  312. $idWorkflow = $this->encController->decrypt($idWorkflow);
  313. if (is_null($idWorkflow)) {
  314. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el ID del workflow.', [], 401);
  315. }
  316. try {
  317. $validateExists = DB::table('S002V01TFLTR')
  318. ->where('FLTR_IDFT', '=', $idWorkflow)
  319. ->where('FLTR_NULI', '=', $line)
  320. ->exists();
  321. } catch (\Throwable $th) {
  322. return $this->responseController->makeResponse(true, "Ocurrió un error al verificar el workflow.", $th->getMessage(), 500);
  323. }
  324. if (!$validateExists) {
  325. return $this->responseController->makeResponse(true, "El workflow no existe.", [], 500);
  326. }
  327. try {
  328. $workflow = (array) DB::table('S002V01TFLTR')
  329. ->where('FLTR_IDFT', '=', $idWorkflow)
  330. ->where('FLTR_NULI', '=', $line)
  331. ->first([
  332. 'FLTR_HICA AS HISTORIAL',
  333. ]);
  334. } catch (\Throwable $th) {
  335. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener los flujos de trabajo.', $th->getMessage(), 500);
  336. }
  337. if (empty($workflow)) {
  338. return $this->responseController->makeResponse(false, "No se encontró información en el historial.", [], 500);
  339. }
  340. if (is_null($workflow['HISTORIAL'])) {
  341. return $this->responseController->makeResponse(false, "No se encontró información en el historial.", [], 500);
  342. }
  343. $arrClaves = [
  344. 'FLTR_NOFT' => 'NOMBRE_FLUJO',
  345. 'FLTR_DEFT' => 'DESCRIPCION_FLUJO',
  346. 'FLTR_ESAU' => 'AUTOMATICO',
  347. 'FLTR_IDMO' => 'MODULO',
  348. 'FLTR_TIFT' => 'TIPO_FLUJO',
  349. 'FLTR_ESTA' => 'ESTADO',
  350. 'FLTR_ESPR' => 'PREDEFINIDO',
  351. 'FLTR_USRE' => 'USUARIO_REGISTRA',
  352. 'FLTR_FERE' => 'FECHA_REGISTRA',
  353. 'FLTR_USMO' => 'USUARIO_MODIFICA',
  354. 'FLTR_FEMO' => 'FECHA_MODIFICA',
  355. ];
  356. $arrHistory = array();
  357. $arrHistoryTemp = json_decode($workflow['HISTORIAL'], true);
  358. foreach ($arrHistoryTemp as $keyHistory => $historyTemp) {
  359. foreach ($historyTemp as $key => $value) {
  360. if (array_key_exists($key, $arrClaves)) {
  361. if ($key === 'FLTR_IDMO') {
  362. try {
  363. $module = (array) DB::table('S002V01TMODU')
  364. ->where('MODU_IDMO', '=', $value)
  365. ->where('MODU_NULI', '=', $line)
  366. ->first();
  367. } catch (\Throwable $th) {
  368. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener los módulos del flujo de trabajo.', $th->getMessage(), 500);
  369. }
  370. if (empty($module)) {
  371. return $this->responseController->makeResponse(false, "No se encontró información del módulo del flujo de trabajo.", [], 500);
  372. }
  373. $value = $module['MODU_NOMO'];
  374. }
  375. if ($key === 'FLTR_TIFT' && $value === 'S') {
  376. $value = 'Solicitud';
  377. }
  378. if ($key === 'FLTR_TIFT' && $value === 'F') {
  379. $value = 'Formulario';
  380. }
  381. $arrHistory[$keyHistory][$arrClaves[$key]] = $value;
  382. }
  383. }
  384. }
  385. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrHistory);
  386. }
  387. public function getProcessWorkflow($idWorkflow, $user, $line) {
  388. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  389. if ($arrResponseCheckUser['error']) {
  390. DB::rollBack();
  391. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  392. }
  393. $user = $arrResponseCheckUser['response'];
  394. $idWorkflow = $this->encController->decrypt($idWorkflow);
  395. if (is_null($idWorkflow)) {
  396. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el ID del workflow.', [], 401);
  397. }
  398. try {
  399. $validateExists = DB::table('S002V01TFLTR')
  400. ->where('FLTR_IDFT', '=', $idWorkflow)
  401. ->where('FLTR_NULI', '=', $line)
  402. ->exists();
  403. } catch (\Throwable $th) {
  404. return $this->responseController->makeResponse(true, "Ocurrió un error al verificar el workflow.", $th->getMessage(), 500);
  405. }
  406. if (!$validateExists) {
  407. return $this->responseController->makeResponse(true, "El workflow no existe.", [], 500);
  408. }
  409. try {
  410. $arrWorks = DB::table('S002V01TTRAB')
  411. ->where('TRAB_IDFT', '=', $idWorkflow)
  412. ->where('TRAB_ESTA', '=', 'Activo')
  413. ->where('TRAB_NULI', '=', $line)
  414. ->get([
  415. 'TRAB_IDTR AS ID_TRABAJO',
  416. 'TRAB_NOTR AS NOMBRE_TRABAJO',
  417. 'TRAB_DETR AS DESCRIPCION_TRABAJO',
  418. 'TRAB_SECU AS SECUENCIA',
  419. 'TRAB_SISE AS SIGUIENTE_SECUENCIA',
  420. 'TRAB_CASE AS CAMBIO_SECUENCIA',
  421. 'TRAB_DISP AS DISPONIBLE',
  422. 'TRAB_COMP AS COMPLETO',
  423. 'TRAB_FUVA AS VALIDADO',
  424. 'TRAB_ESCO AS CONDICIONAL',
  425. 'TRAB_USRE AS USUARIO_REGISTRA',
  426. 'TRAB_FERE AS FECHA_REGISTRA',
  427. 'TRAB_USMO AS USUARIO_MODIFICA',
  428. 'TRAB_FEMO AS FECHA_MODIFICA',
  429. ]);
  430. } catch (\Throwable $th) {
  431. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener los trabajos.', $th->getMessage(), 500);
  432. }
  433. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrWorks);
  434. }
  435. public function getTastks($user, $line) {
  436. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  437. if ($arrResponseCheckUser['error']) {
  438. DB::rollBack();
  439. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  440. }
  441. }
  442. }