DocumentManagementController.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  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. use Illuminate\Support\Facades\Hash;
  7. use Illuminate\Support\Carbon;
  8. use Illuminate\Support\Facades\Storage;
  9. class DocumentManagementController extends Controller{
  10. private $responseController;
  11. private $encryptionController;
  12. private $functionsController;
  13. private $resourcesController;
  14. public function __construct(){
  15. $this->responseController = new ResponseController();
  16. $this->encryptionController = new EncryptionController();
  17. $this->functionsController = new FunctionsController();
  18. $this->resourcesController = new ResourcesController();
  19. }
  20. public function downloadFile($token, $idUser, $line){
  21. DB::enableQueryLog();
  22. $tokenInfo = DB::table('S002V01TTODE')->where([
  23. ['TODE_NULI', '=', $line],
  24. ['TODE_TOKE', '=', $token]
  25. ])->first();
  26. if(is_null($tokenInfo)){
  27. return $this->responseController->makeResponse(true, 'El token de descarga no existe.', [], 404);
  28. }else if($tokenInfo->TODE_ESTA != 'Activo'){
  29. return $this->responseController->makeResponse(true, 'El token de descarga ya fue utilizado.', [], 401);
  30. }
  31. $token = $this->encryptionController->decrypt($token);
  32. if(!$token){
  33. return $this->responseController->makeResponse(true, 'El token de descarga no está encriptado correctamente.', [], 400);
  34. }
  35. $tokenArr = explode("|", $token);
  36. if(count($tokenArr) != 3){
  37. return $this->responseController->makeResponse(true, 'Estructura de token inválida.', [], 401);
  38. }else if(intval($tokenArr[1]) != $tokenInfo->TODE_LLAL){
  39. return $this->responseController->makeResponse(true, 'Token inválido.', [], 401);
  40. }else if($tokenArr[2] != 'syp'){
  41. return $this->responseController->makeResponse(true, 'Token inválido.', [], 401);
  42. }
  43. $idUser = $this->encryptionController->decrypt($idUser);
  44. if(!$idUser){
  45. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  46. }
  47. $usr = DB::table('S002V01TUSUA')->where([
  48. ['USUA_IDUS', '=', $idUser],
  49. ['USUA_NULI', '=', $line]
  50. ])->first();
  51. if(is_null($usr)){
  52. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  53. }
  54. $id = $tokenArr[0];
  55. $codiArr = explode("=", $id);
  56. $prefArr = explode("-", $codiArr[0]);
  57. $suffArr = explode(".", $codiArr[2]);
  58. $nuli = intval($prefArr[0]);
  59. $como = $prefArr[1];
  60. $cldo = $prefArr[2];
  61. $fecr = $prefArr[3];
  62. $nuse = intval($prefArr[4]);
  63. $nuve = $codiArr[1];
  64. $noar = $suffArr[0];
  65. $exte = $suffArr[1];
  66. $nowStr = Carbon::now('America/Mexico_city')->toDateTimeString();
  67. DB::table('S002V01TTODE')->where([
  68. ['TODE_IDTO', '=', $tokenInfo->TODE_IDTO],
  69. ['TODE_NULI', '=', $line]
  70. ])->update([
  71. 'TODE_ESTA' => 'Usado',
  72. 'TODE_USDE' => $idUser,
  73. 'TODE_FEDE' => $nowStr
  74. ]);
  75. $file = DB::table('S002V01TAFAL')->where([
  76. ['AFAL_NULI', '=', $nuli],
  77. ['AFAL_COMO', '=', $como],
  78. ['AFAL_CLDO', '=', $cldo],
  79. ['AFAL_FECR', '=', $fecr],
  80. ['AFAL_NUSE', '=', $nuse],
  81. ['AFAL_NUVE', '=', $nuve],
  82. ['AFAL_NOAR', '=', $noar],
  83. ['AFAL_EXTE', '=', $exte],
  84. ])->first();
  85. if(is_null($file)){
  86. return $this->responseController->makeResponse(true, 'El archivo solicitado no existe.', [], 404);
  87. }
  88. return response()->download($file->AFAL_UBIC, $id);
  89. }
  90. public function getDownloadToken($idFile, $idUser, $line){
  91. DB::enableQueryLog();
  92. $idFile = $this->encryptionController->decrypt($idFile);
  93. if(!$idFile){
  94. return $this->responseController->makeResponse(true, 'El ID del archivo requerido no está encriptado correctamente.', [], 400);
  95. }
  96. $idUser = $this->encryptionController->decrypt($idUser);
  97. if(!$idUser){
  98. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  99. }
  100. $usr = DB::table('S002V01TUSUA')->where([
  101. ['USUA_IDUS', '=', $idUser],
  102. ['USUA_NULI', '=', $line]
  103. ])->first();
  104. if(is_null($usr)){
  105. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  106. }
  107. $codiArr = explode("=", $idFile);
  108. $prefArr = explode("-", $codiArr[0]);
  109. $suffArr = explode(".", $codiArr[2]);
  110. $nuli = intval($prefArr[0]);
  111. $como = $prefArr[1];
  112. $cldo = $prefArr[2];
  113. $fecr = $prefArr[3];
  114. $nuse = intval($prefArr[4]);
  115. $nuve = $codiArr[1];
  116. $noar = $suffArr[0];
  117. $exte = $suffArr[1];
  118. $file = DB::table('S002V01TAFAL')->where([
  119. ['AFAL_NULI', '=', $nuli],
  120. ['AFAL_COMO', '=', $como],
  121. ['AFAL_CLDO', '=', $cldo],
  122. ['AFAL_FECR', '=', $fecr],
  123. ['AFAL_NUSE', '=', $nuse],
  124. ['AFAL_NUVE', '=', $nuve],
  125. ['AFAL_NOAR', '=', $noar],
  126. ['AFAL_EXTE', '=', $exte],
  127. ])->first();
  128. if(is_null($file)){
  129. return $this->responseController->makeResponse(true, 'El archivo solicitado no existe.', [], 404);
  130. }
  131. $rand = rand(0, 99999);
  132. $tokenArr = [$idFile, $rand, "syp"];
  133. $tokenStr = join('|', $tokenArr);
  134. $token = $this->encryptionController->encrypt($tokenStr);
  135. $token = str_replace("+", "=P=", $token);
  136. $token = str_replace("/", "=S=", $token);
  137. $nowStr = Carbon::now('America/Mexico_city')->toDateTimeString();
  138. DB::table('S002V01TTODE')->insert([
  139. 'TODE_NULI' => $line,
  140. 'TODE_TOKE' => $token,
  141. 'TODE_CODO' => $idFile,
  142. 'TODE_LLAL' => $rand,
  143. 'TODE_USRE' => $idUser,
  144. 'TODE_FERE' => $nowStr
  145. ]);
  146. return $this->responseController->makeResponse(false, 'EXITO', ['TOKEN' => $token]);
  147. }
  148. public function getFileInfo($id, $idUser, $line){
  149. DB::enableQueryLog();
  150. $id = $this->encryptionController->decrypt($id);
  151. if(!$id){
  152. return $this->responseController->makeResponse(true, 'El ID del archivo requerido no está encriptado correctamente.', [], 400);
  153. }
  154. $idUser = $this->encryptionController->decrypt($idUser);
  155. if(!$idUser){
  156. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  157. }
  158. $usr = DB::table('S002V01TUSUA')->where([
  159. ['USUA_IDUS', '=', $idUser],
  160. ['USUA_NULI', '=', $line]
  161. ])->first();
  162. if(is_null($usr)){
  163. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  164. }
  165. $idArr = explode("=", $id);
  166. $codiArr = explode("-", $idArr[0]);
  167. $nuli = $line;
  168. $como = $codiArr[1];
  169. $cldo = $codiArr[2];
  170. $fecr = $codiArr[3];
  171. $nuse = intval($codiArr[4]);
  172. $nuve = intval($idArr[1]);
  173. $fileInfo = DB::table('S002V01TAFAL')->select([
  174. 'AFAL_NOAR AS NOMBREARCHIVO',
  175. 'AFAL_EXTE AS EXTENSION',
  176. 'AFAL_TAMA AS PESO',
  177. 'AFAL_USAC AS ACCESO',
  178. 'AFAL_ESTA AS ESTADO'
  179. ])->where([
  180. ['AFAL_NULI', '=', $nuli],
  181. ['AFAL_COMO', '=', $como],
  182. ['AFAL_CLDO', '=', $cldo],
  183. ['AFAL_FECR', '=', $fecr],
  184. ['AFAL_NUSE', '=', $nuse],
  185. ['AFAL_NUVE', '=', $nuve],
  186. ])->first();
  187. if(is_null($fileInfo)){
  188. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  189. }else if($fileInfo->ESTADO != 'Activo'){
  190. return $this->responseController->makeResponse(true, 'El archivo solicitado no está disponible.', [], 404);
  191. }
  192. return $this->responseController->makeResponse(false, 'EXITO', $fileInfo);
  193. }
  194. public function getFiles($mod, $cla, $sda, $eda, $dna, $ext, $mode, $idUser, $line){
  195. DB::enableQueryLog();
  196. $idUser = $this->encryptionController->decrypt($idUser);
  197. if(!$idUser){
  198. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  199. }
  200. $usr = DB::table('S002V01TUSUA')->where([
  201. ['USUA_NULI', '=', $line],
  202. ['USUA_IDUS', '=', $idUser]
  203. ])->first();
  204. if(is_null($usr)){
  205. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  206. }
  207. $availableModes = ['my-files', 'shared', 'deleted'];
  208. if(!in_array($mode, $availableModes)){
  209. return $this->responseController->makeResponse(true, 'El modo seleccionado es inválido.', [], 401);
  210. }
  211. $whereParameters = [
  212. ['AFAL_NULI', '=', $line]
  213. ];
  214. if($mod != '-'){
  215. $whereParameters[] = ['AFAL_COMO', '=', $mod];
  216. }
  217. if($cla != '-'){
  218. $whereParameters[] = ['AFAL_CLDO', '=', $cla];
  219. }
  220. $files = DB::table('S002V01TAFAL')->where($whereParameters)->get()->all();
  221. $filesDateFiltered = [];
  222. foreach($files as $file){
  223. $dateArr = str_split($file->AFAL_FECR, 2);
  224. $dateStr = "20$dateArr[0]-$dateArr[1]-$dateArr[2]";
  225. $dateObj = new Carbon($dateStr, 'America/Guatemala');
  226. if($sda != '-' && $eda == '-'){
  227. $dateIniArr = str_split($sda, 2);
  228. $dateIniStr = "20$dateIniArr[0]-$dateIniArr[1]-$dateIniArr[2]";
  229. $dateIniObj = new Carbon($dateIniStr, 'America/Guatemala');
  230. if($dateIniObj->lte($dateObj)){
  231. $filesDateFiltered[] = $file;
  232. }
  233. }else if($sda == '-' && $eda != '-'){
  234. $dateFinArr = str_split($eda, 2);
  235. $dateFinStr = "20$dateFinArr[0]-$dateFinArr[1]-$dateFinArr[2]";
  236. $dateFinObj = new Carbon($dateFinStr, 'America/Guatemala');
  237. if($dateFinObj->gte($dateObj)){
  238. $filesDateFiltered[] = $file;
  239. }
  240. }else if($sda != '-' && $eda != '-'){
  241. $dateIniArr = str_split($sda, 2);
  242. $dateIniStr = "20$dateIniArr[0]-$dateIniArr[1]-$dateIniArr[2]";
  243. $dateIniObj = new Carbon($dateIniStr, 'America/Guatemala');
  244. $dateFinArr = str_split($eda, 2);
  245. $dateFinStr = "20$dateFinArr[0]-$dateFinArr[1]-$dateFinArr[2]";
  246. $dateFinObj = new Carbon($dateFinStr, 'America/Guatemala');
  247. if($dateFinObj->gte($dateObj) && $dateIniObj->lte($dateObj)){
  248. $filesDateFiltered[] = $file;
  249. }
  250. }else{
  251. $filesDateFiltered[] = $file;
  252. }
  253. }
  254. $filesNameFiltered = [];
  255. foreach($filesDateFiltered as $file){
  256. if($dna != '-' && $ext == '-'){
  257. if(str_contains($file->AFAL_NOAR, $dna)){
  258. $filesNameFiltered[] = $file;
  259. }
  260. }else if($dna == '-' && $ext != '-'){
  261. //$extArr = explode(',', $ext);
  262. $extArr = json_decode($ext);
  263. if(in_array($file->AFAL_EXTE, $extArr)){
  264. $filesNameFiltered[] = $file;
  265. }
  266. }else if($dna != '-' && $ext != '-'){
  267. $extArr = json_decode($ext);
  268. if(str_contains($file->AFAL_NOAR, $dna) && in_array($file->AFAL_EXTE, $extArr)){
  269. $filesNameFiltered[] = $file;
  270. }
  271. }else{
  272. $filesNameFiltered[] = $file;
  273. }
  274. }
  275. $filesAccessFiltered = [];
  276. foreach($filesNameFiltered as $file){
  277. $access = json_decode($file->AFAL_USAC);
  278. if($mode == 'my-files' && $file->AFAL_USRE == $idUser && $file->AFAL_ESTA == 'Activo'){
  279. $filesAccessFiltered[] = $file;
  280. }else if($mode == 'shared' && in_array($idUser, $access) && $file->AFAL_USRE != $idUser && $file->AFAL_ESTA == 'Activo'){
  281. $filesAccessFiltered[] = $file;
  282. }else if($mode == 'deleted' && $file->AFAL_ESTA == 'Eliminado' && ($file->AFAL_USRE == $idUser || in_array($idUser, $access))){
  283. $filesAccessFiltered[] = $file;
  284. }
  285. }
  286. $filesF = [];
  287. foreach($filesAccessFiltered as $file){
  288. $linea = $file->AFAL_NULI < 10 ? "0" . $file->AFAL_NULI : "" . $file->AFAL_NULI . "";
  289. $secu = "";
  290. for($i = strlen($file->AFAL_NUSE); $i < 6; $i++){
  291. $secu .= "0";
  292. }
  293. $secu .= $file->AFAL_NUSE;
  294. $vers = $file->AFAL_NUVE < 10 ? "0" . $file->AFAL_NUVE : "" . $file->AFAL_NUVE . "";
  295. $fName = $file->AFAL_NOAR . '.' . $file->AFAL_EXTE;
  296. $usrReg = DB::table('S002V01TUSUA')->where([
  297. ['USUA_NULI', '=', $line],
  298. ['USUA_IDUS', '=', $file->AFAL_USRE],
  299. ])->first();
  300. $nameReg = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  301. $nameReg .= " (" . $file->AFAL_USRE . ")";
  302. $access = json_decode($file->AFAL_USAC, true);
  303. foreach($access as $k=>$v){
  304. $usrAcc = DB::table('S002V01TUSUA')->where([
  305. ['USUA_NULI', '=', $line],
  306. ['USUA_IDUS', '=', $v],
  307. ])->first();
  308. $nameAcc = $this->functionsController->joinName($usrAcc->USUA_NOMB, $usrAcc->USUA_APPA, $usrAcc->USUA_APMA);
  309. $nameAcc .= " (" . $v . ")";
  310. $access[$k] = $nameAcc;
  311. }
  312. $accessStr = json_encode($access);
  313. $filesF[] = [
  314. 'CODIGO' => $linea . '-' . $file->AFAL_COMO . '-' . $file->AFAL_CLDO . '-' . $file->AFAL_FECR . '-' . $secu,
  315. 'VERSION' => $vers,
  316. 'NOMBRE' => $fName,
  317. 'TAMANIO' => $file->AFAL_TAMA,
  318. 'USRREG' => $nameReg,
  319. 'ACCESO' => $accessStr
  320. ];
  321. }
  322. $now = $this->functionsController->now();
  323. $nowStr = $now->toDateTimeString();
  324. $actions = DB::getQueryLog();
  325. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  326. $idac = $this->functionsController->registerActivity(
  327. $line,
  328. 'S002V01M04GDEL',
  329. 'S002V01F01ADDO',
  330. 'S002V01P01GEDO',
  331. 'Consulta',
  332. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los documentos registrados.",
  333. $idUser,
  334. $nowStr
  335. );
  336. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  337. return $this->responseController->makeresponse(false, "EXITO", $filesF);
  338. }
  339. public function uploadTempFile(Request $request){
  340. DB::enableQueryLog();
  341. if(!$request->hasFile('file')){
  342. return $this->responseController->makeResponse(true, "No se envió ningún archivo.", [], 400);
  343. }
  344. $validator = Validator::make($request->all(), [
  345. 'id_user' => 'required|string',
  346. 'linea' => 'required|integer'
  347. ]);
  348. if($validator->fails()){
  349. return $this->responseController->makeResponse(
  350. true,
  351. "Se encontraron uno o más errores.",
  352. $this->responseController->makeErrors(
  353. $validator->errors()->messages()
  354. ),
  355. 401
  356. );
  357. }
  358. $form = $request->all();
  359. $idUser = $this->encryptionController->decrypt($form['id_user']);
  360. if(!$idUser){
  361. return $this->responseController->makeResponse(true, "El id del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  362. }
  363. $usr = DB::table('S002V01TUSUA')->where([
  364. ['USUA_IDUS', '=', $idUser],
  365. ['USUA_NULI', '=', $form['linea']]
  366. ])->first();
  367. if(is_null($usr)){
  368. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  369. }
  370. $originalFileName = $request->file('file')->getClientOriginalName();
  371. $extension = $request->file('file')->extension();
  372. $size = $request->file('file')->getSize();
  373. $extArr = explode(".", $originalFileName);
  374. $extArr = array_reverse($extArr);
  375. $extStr = $extArr[0];
  376. $isValid = $this->functionsController->checkFileSize($extStr, $size);
  377. if($isValid){
  378. $dir = str_replace("app\\Http\\Controllers", "storage\\app", __DIR__);
  379. $tmpPath = $request->file('file')->store('tempFiles');
  380. $tmpPath = str_replace("/", "\\", $tmpPath);
  381. $location = "$dir\\$tmpPath";
  382. $nowStr = Carbon::now('America/Mexico_city')->toDateTimeString();
  383. $fileID = DB::table('S002V01TARTE')->insertGetId([
  384. 'ARTE_NULI' => $form['linea'],
  385. 'ARTE_NOAR' => $originalFileName,
  386. 'ARTE_EXTE' => $extension,
  387. 'ARTE_TAMA' => $size,
  388. 'ARTE_UBTE' => $location,
  389. 'ARTE_USRE' => $idUser,
  390. 'ARTE_FERE' => $nowStr
  391. ]);
  392. $actions = DB::getQueryLog();
  393. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  394. $idac = $this->functionsController->registerActivity(
  395. $form['linea'],
  396. 'S002V01M04GDEL',
  397. 'S002V01F01ADDO',
  398. 'S002V01P05REDO',
  399. 'Registro',
  400. "El usuario $name (" . $usr->USUA_IDUS . ") subió de manera temporal el archivo $originalFileName.",
  401. $idUser,
  402. $nowStr,
  403. );
  404. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  405. return $this->responseController->makeresponse(false, "EXITO", [
  406. 'idArchivo' => $this->encryptionController->encrypt($fileID),
  407. ]);
  408. }else{
  409. return $this->responseController->makeResponse(true, "El archivo enviado tiene una extensión no soportada o sobrepasa el límite de peso de su categoría.", [], 400);
  410. }
  411. }
  412. public function deleteTempFile(Request $request){
  413. DB::enableQueryLog();
  414. $validator = Validator::make($request->all(), [
  415. 'id_user' => 'required|string',
  416. 'id_file' => 'required|string',
  417. 'linea' => 'required|integer'
  418. ]);
  419. if($validator->fails()){
  420. return $this->responseController->makeResponse(
  421. true,
  422. "Se encontraron uno o más errores.",
  423. $this->responseController->makeErrors(
  424. $validator->errors()->messages()
  425. ),
  426. 401
  427. );
  428. }
  429. $form = $request->all();
  430. $idUser = $this->encryptionController->decrypt($form['id_user']);
  431. if(!$idUser){
  432. return $this->responseController->makeResponse(true, "El id del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  433. }
  434. $usr = DB::table('S002V01TUSUA')->where([
  435. ['USUA_IDUS', '=', $idUser],
  436. ['USUA_NULI', '=', $form['linea']]
  437. ])->first();
  438. if(is_null($usr)){
  439. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  440. }
  441. $idFile = $this->encryptionController->decrypt($form['id_file']);
  442. if(!$idFile){
  443. return $this->responseController->makeResponse(true, "El id del archivo que desea eliminar no fue encriptado correctamente", [], 400);
  444. }
  445. $file = DB::table('S002V01TARTE')->where([
  446. ['ARTE_IDAR', '=', $idFile],
  447. ['ARTE_NULI', '=', $form['linea']]
  448. ])->first();
  449. if(is_null($file)){
  450. return $this->responseController->makeResponse(true, 'El archivo que desea eliminar no está registrado', [], 404);
  451. }
  452. if(file_exists($file->ARTE_UBTE)){
  453. unlink($file->ARTE_UBTE);
  454. }
  455. $nowStr = Carbon::now('America/Mexico_city')->toDateTimeString();
  456. DB::table('S002V01TARTE')->where([
  457. ['ARTE_IDAR', '=', $idFile],
  458. ['ARTE_NULI', '=', $form['linea']]
  459. ])->update([
  460. 'ARTE_ESTA' => 'Eliminado',
  461. 'ARTE_USMO' => $idUser,
  462. 'ARTE_FEMO' => $nowStr
  463. ]);
  464. $actions = DB::getQueryLog();
  465. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  466. $idac = $this->functionsController->registerActivity(
  467. $form['linea'],
  468. 'S002V01M04GDEL',
  469. 'S002V01F01ADDO',
  470. 'S002V01P05REDO',
  471. 'Eliminación',
  472. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó el archivo " . $file->ARTE_NOAR,
  473. $idUser,
  474. $nowStr,
  475. );
  476. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  477. return $this->responseController->makeresponse(false, "EXITO");
  478. }
  479. public function saveFinalFile(Request $request) {
  480. DB::enableQueryLog();
  481. $validator = Validator::make($request->all(), [
  482. 'id_user' => 'required|string',
  483. 'id_file' => 'required|string',
  484. 'linea' => 'required|integer',
  485. 'module' => 'required|string|max:4',
  486. 'clasification' => 'required|string|max:2',
  487. 'has_order' => 'required|string|in:S,N',
  488. 'id_order' => 'required_if:has_order,=,S|string',
  489. ]);
  490. if($validator->fails()){
  491. return $this->responseController->makeResponse(
  492. true,
  493. "Se encontraron uno o más errores.",
  494. $this->responseController->makeErrors(
  495. $validator->errors()->messages()
  496. ),
  497. 401
  498. );
  499. }
  500. $form = $request->all();
  501. $idUser = $this->encryptionController->decrypt($form['id_user']);
  502. if(!$idUser){
  503. return $this->responseController->makeResponse(true, "El id del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  504. }
  505. $usr = DB::table('S002V01TUSUA')->where([
  506. ['USUA_NULI', '=', $form['linea']],
  507. ['USUA_IDUS', '=', $idUser],
  508. ])->first();
  509. if(is_null($usr)){
  510. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  511. }
  512. $idFile = $this->encryptionController->decrypt($form['id_file']);
  513. if(!$idFile){
  514. return $this->responseController->makeResponse(true, "El id del archivo solicitado no fue encriptado correctamente", [], 400);
  515. }
  516. $tempFile = DB::table('S002V01TARTE')->where([
  517. ['ARTE_NULI', '=', $form['linea']],
  518. ['ARTE_IDAR', '=', $idFile],
  519. ])->first();
  520. if(is_null($tempFile)){
  521. return $this->responseController->makeResponse(true, 'El archivo consultado no está registrado', [], 404);
  522. }else if($tempFile->ARTE_ESTA == 'Eliminado'){
  523. return $this->responseController->makeResponse(true, 'El archivo consultado está eliminado', [], 404);
  524. }
  525. $fileResponse = $this->moveFinalFile(
  526. intval($form['linea']),
  527. $form['module'],
  528. $form['clasification'],
  529. $tempFile,
  530. $idUser,
  531. );
  532. if(!$fileResponse[0]){
  533. return $this->responseController->makeResponse(true, $fileResponse[1], [], 400);
  534. }
  535. $now = $this->functionsController->now();
  536. $nowStr = $now->toDateTimeString();
  537. if($form['has_order'] == 'S' && ($form['module'] == 'GMPR' || $form['module'] == 'GMCO')){
  538. $idOrder = $this->encryptionController->decrypt($form['id_order']);
  539. if(!$idOrder){
  540. return $this->responseController->makeResponse(true, "El id de la orden relacionada no fue encriptado correctamente", [], 400);
  541. }
  542. $order = DB::table('S002V01TOTPR')->where([
  543. ['OTPR_IDOT', '=', $idOrder],
  544. ['OTPR_NULI', '=', $form['linea']]
  545. ])->first();
  546. if(is_null($order)){
  547. return $this->responseController->makeResponse(true, 'La orden solicitada no está registrada', [], 404);
  548. }
  549. $documentsArr = json_decode($order->OTPR_DONE, true);
  550. $fileCodeEnc = $this->encryptionController->encrypt($fileResponse[1]);
  551. $documentsArr[] = $fileCodeEnc;
  552. $documentsStr = json_encode($documentsArr);
  553. DB::table('S002V01TOTPR')->where([
  554. ['OTPR_IDOT', '=', $idOrder],
  555. ['OTPR_NULI', '=', $form['linea']]
  556. ])->update([
  557. 'OTPR_DONE' => $documentsStr,
  558. 'OTPR_USMO' => $idUser,
  559. 'OTPR_FEMO' => $nowStr
  560. ]);
  561. }
  562. $actions = DB::getQueryLog();
  563. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  564. $idac = $this->functionsController->registerActivity(
  565. $form['linea'],
  566. 'S002V01M04GDEL',
  567. 'S002V01F01ADDO',
  568. 'S002V01P05REDO',
  569. 'Registro',
  570. "El usuario $name (" . $usr->USUA_IDUS . ") registró el archivo " . $tempFile->ARTE_NOAR,
  571. $idUser,
  572. $nowStr,
  573. );
  574. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  575. return $this->responseController->makeresponse(false, "EXITO");
  576. }
  577. public function moveFinalFile(int $line, string $como, string $cldo, object $tempFile, string $idUser) {
  578. $modulesCodes = [
  579. "GEAD","GIST","GMPR","GMCO","GEEQ","COAC","GEPR",
  580. "ANFA","PCSA","GPRS","GEPR","GDEL","ADSI","USPE"
  581. ];
  582. $clasifications = [
  583. "AV","AU","CA","CE","CO","DP","FA","FI",
  584. "FO","IN","LA","OR","PL","RE","VI"
  585. ];
  586. if(!in_array($como, $modulesCodes)){
  587. return [false, "El código $como es inválido."];
  588. }
  589. if(!in_array($cldo, $clasifications)){
  590. return [false, "La clasificación $cldo es inválida."];
  591. }
  592. $now = $this->functionsController->now();
  593. $nowStr = $now->toDateTimeString();
  594. $dateTimeArr = explode(' ', $nowStr);
  595. $dateArr = explode('-', $dateTimeArr[0]);
  596. $year = substr($dateArr[0], 2);
  597. $fecr = "$year$dateArr[1]$dateArr[2]";
  598. $sec = DB::table('S002V01TAFAL')->where([
  599. ['AFAL_COMO', '=', $como],
  600. ['AFAL_CLDO', '=', $cldo],
  601. ['AFAL_NULI', '=', $line],
  602. ])->orderBy('AFAL_NUSE', 'desc')->first();
  603. $nuse = 1;
  604. if(!is_null($sec)){
  605. $nuse = intval($sec->AFAL_NUSE) + 1;
  606. }
  607. $fileNameArr = explode('.', $tempFile->ARTE_NOAR);
  608. array_pop($fileNameArr);
  609. $noar = implode('.', $fileNameArr);
  610. $exte = $tempFile->ARTE_EXTE;
  611. $ver = DB::table('S002V01TAFAL')->where([
  612. ['AFAL_NULI', '=', $line],
  613. ['AFAL_COMO', '=', $como],
  614. ['AFAL_CLDO', '=', $cldo],
  615. ['AFAL_NOAR', '=', $noar],
  616. ['AFAL_EXTE', '=', $exte],
  617. ])->orderBy('AFAL_NUVE', 'desc')->first();
  618. $nuve = 1;
  619. if(!is_null($ver)){
  620. $nuve = intval($sec->AFAL_NUVE) + 1;
  621. }
  622. $tama = $tempFile->ARTE_TAMA;
  623. $ubiFileArr = explode('tempFiles', $tempFile->ARTE_UBTE);
  624. $ubic = $ubiFileArr[0] . 'files' . $ubiFileArr[1];
  625. if(file_exists($tempFile->ARTE_UBTE)){
  626. rename($tempFile->ARTE_UBTE, $ubic);
  627. DB::table('S002V01TARTE')->where([
  628. ['ARTE_IDAR', '=', $tempFile->ARTE_IDAR],
  629. ['ARTE_NULI', '=', $line],
  630. ])->update([
  631. 'ARTE_ESTA' => 'Eliminado',
  632. 'ARTE_USMO' => $idUser,
  633. 'ARTE_FEMO' => $nowStr,
  634. ]);
  635. }
  636. $code = $line < 10 ? "0$line" : "$line";
  637. $code .= "-$como-$cldo-$fecr-";
  638. for($i = strlen($nuse); $i < 6; $i++){
  639. $code .= "0";
  640. }
  641. $code .= "$nuse=";
  642. $code .= $nuve < 10 ? "0$nuve=" : "$nuve=";
  643. $code .= "$noar.$exte";
  644. $usac = json_encode([$idUser]);
  645. DB::table('S002V01TAFAL')->insert([
  646. 'AFAL_NULI' => $line,
  647. 'AFAL_COMO' => $como,
  648. 'AFAL_CLDO' => $cldo,
  649. 'AFAL_FECR' => $fecr,
  650. 'AFAL_NUSE' => $nuse,
  651. 'AFAL_NUVE' => $nuve,
  652. 'AFAL_NOAR' => $noar,
  653. 'AFAL_EXTE' => $exte,
  654. 'AFAL_TAMA' => $tama,
  655. 'AFAL_UBIC' => $ubic,
  656. 'AFAL_USAC' => $usac,
  657. 'AFAL_USRE' => $idUser,
  658. 'AFAL_FERE' => $nowStr
  659. ]);
  660. return [true, $code];
  661. }
  662. public function getPublicDocumentURL($id, $idUser, $line) {
  663. DB::enableQueryLog();
  664. $idUser = $this->encryptionController->decrypt($idUser);
  665. if(!$idUser){
  666. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  667. }
  668. $usr = DB::table('S002V01TUSUA')->where([
  669. ['USUA_NULI', '=', $line],
  670. ['USUA_IDUS', '=', $idUser],
  671. ])->first();
  672. if(is_null($usr)){
  673. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  674. }
  675. $id = $this->encryptionController->decrypt($id);
  676. if(!$id){
  677. return $this->responseController->makeResponse(true, 'El ID del archivo solicitado no está encriptado correctamente.', [], 400);
  678. }
  679. $idArr = explode('=', $id);
  680. $codeArr = explode('-', $idArr[0]);
  681. $file = DB::table('S002V01TAFAL')->where([
  682. ['AFAL_NULI', '=', $codeArr[0]],
  683. ['AFAL_COMO', '=', $codeArr[1]],
  684. ['AFAL_CLDO', '=', $codeArr[2]],
  685. ['AFAL_FECR', '=', $codeArr[3]],
  686. ['AFAL_NUSE', '=', $codeArr[4]],
  687. ['AFAL_NUVE', '=', $idArr[1]],
  688. ])->first();
  689. if(is_null($file)){
  690. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  691. }
  692. $ubicArr = explode('storage', $file->AFAL_UBIC);
  693. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  694. $publicUbi = "$ubicArr[0]public\\public_files\\$id";
  695. } else {
  696. $publicUbi = "$ubicArr[0]public_files\\$id";
  697. }
  698. if(!file_exists($publicUbi)){
  699. copy($file->AFAL_UBIC, $publicUbi);
  700. }
  701. $apiURI = $this->functionsController->getApiURI();
  702. $publicUbiStr = str_replace('C:\inetpub\wwwroot\\', $apiURI, $publicUbi);
  703. $publicUbiStr = str_replace('\\', '/', $publicUbiStr);
  704. $now = $this->functionsController->now();
  705. $nowStr = $now->toDateTimeString();
  706. $actions = DB::getQueryLog();
  707. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  708. $idac = $this->functionsController->registerActivity(
  709. $line,
  710. 'S002V01M04GDEL',
  711. 'S002V01F01ADDO',
  712. 'S002V01P01GEDO',
  713. 'Consulta',
  714. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la URL pública del archivo $id.",
  715. $idUser,
  716. $nowStr
  717. );
  718. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  719. return $this->responseController->makeresponse(false, "EXITO", ['public_uri' => $publicUbiStr]);
  720. }
  721. public function privateGetPublicDocumentURL($id, $idUser, $line) {
  722. $arrResponse = array('error' => false, 'msg' => '', 'response' => []);
  723. DB::enableQueryLog();
  724. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($idUser, $line);
  725. if ($arrResponseCheckUser['error']) {
  726. $arrResponse['error'] = true;
  727. $arrResponse['msg'] = $arrResponseCheckUser['msg'];
  728. return $arrResponse;
  729. }
  730. $idUser = $arrResponseCheckUser['response'];
  731. $id = $this->encryptionController->decrypt($id);
  732. if(!$id){
  733. $arrResponse['error'] = true;
  734. $arrResponse['msg'] = 'El ID del archivo solicitado no está encriptado correctamente.';
  735. return $arrResponse;
  736. }
  737. $idArr = explode('=', $id);
  738. $codeArr = explode('-', $idArr[0]);
  739. $file = DB::table('S002V01TAFAL')->where([
  740. ['AFAL_NULI', '=', $codeArr[0]],
  741. ['AFAL_COMO', '=', $codeArr[1]],
  742. ['AFAL_CLDO', '=', $codeArr[2]],
  743. ['AFAL_FECR', '=', $codeArr[3]],
  744. ['AFAL_NUSE', '=', $codeArr[4]],
  745. ['AFAL_NUVE', '=', $idArr[1]],
  746. ])->first();
  747. if(is_null($file)){
  748. $arrResponse['error'] = true;
  749. $arrResponse['msg'] = 'El archivo solicitado no está registrado.';
  750. return $arrResponse;
  751. }
  752. $ubicArr = explode('storage', $file->AFAL_UBIC);
  753. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  754. $publicUbi = "$ubicArr[0]public\public_files\\$id";
  755. } else {
  756. $publicUbi = "$ubicArr[0]public_files\\$id";
  757. }
  758. if(!file_exists($publicUbi)){
  759. copy($file->AFAL_UBIC, $publicUbi);
  760. }
  761. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  762. $apiURI = 'http://192.168.100.105:8000/';
  763. $publicUbiStr = str_replace('C:\ITTEC\SAM\Dev\SistemaMantenimiento\sistema-mantenimiento-back\public\\', $apiURI, $publicUbi);
  764. } else {
  765. $apiURI = $this->functionsController->getApiURI();
  766. $publicUbiStr = str_replace('C:\inetpub\wwwroot\\', $apiURI, $publicUbi);
  767. }
  768. $publicUbiStr = str_replace('\\', '/', $publicUbiStr);
  769. $arrResponse['response'] = ['public_uri' => $publicUbiStr];
  770. return $arrResponse;
  771. }
  772. public function getFileAccess($id, $idUser, $line) {
  773. DB::enableQueryLog();
  774. $idUser = $this->encryptionController->decrypt($idUser);
  775. if(!$idUser){
  776. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  777. }
  778. $usr = DB::table('S002V01TUSUA')->where([
  779. ['USUA_NULI', '=', $line],
  780. ['USUA_IDUS', '=', $idUser],
  781. ])->first();
  782. if(is_null($usr)){
  783. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  784. }
  785. $id = $this->encryptionController->decrypt($id);
  786. if(!$id){
  787. return $this->responseController->makeResponse(true, 'El ID del archivo solicitado no está encriptado correctamente.', [], 400);
  788. }
  789. $idArr = explode('=', $id);
  790. $codeArr = explode('-', $idArr[0]);
  791. $file = DB::table('S002V01TAFAL')->where([
  792. ['AFAL_NULI', '=', $codeArr[0]],
  793. ['AFAL_COMO', '=', $codeArr[1]],
  794. ['AFAL_CLDO', '=', $codeArr[2]],
  795. ['AFAL_FECR', '=', $codeArr[3]],
  796. ['AFAL_NUSE', '=', $codeArr[4]],
  797. ['AFAL_NUVE', '=', $idArr[1]],
  798. ])->first();
  799. if(is_null($file)){
  800. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  801. }
  802. $owner = $this->encryptionController->encrypt($file->AFAL_USRE);
  803. $permissionsArr = json_decode($file->AFAL_USAC, true);
  804. foreach($permissionsArr as $k=>$v){
  805. $idEnc = $this->encryptionController->encrypt($v);
  806. $permissionsArr[$k] = $idEnc;
  807. }
  808. $now = $this->functionsController->now();
  809. $nowStr = $now->toDateTimeString();
  810. $actions = DB::getQueryLog();
  811. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  812. $idac = $this->functionsController->registerActivity(
  813. $line,
  814. 'S002V01M04GDEL',
  815. 'S002V01F01ADDO',
  816. 'S002V01P01GEDO',
  817. 'Consulta',
  818. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los usuarios con acceso al archivo $id.",
  819. $idUser,
  820. $nowStr
  821. );
  822. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  823. return $this->responseController->makeresponse(false, "EXITO", [
  824. 'access' => $permissionsArr,
  825. 'owner' => $owner
  826. ]);
  827. }
  828. public function updateFilePermissions(Request $request) {
  829. DB::enableQueryLog();
  830. $validator = Validator::make($request->all(), [
  831. 'id_user' => 'required|string',
  832. 'id_file' => 'required|string',
  833. 'linea' => 'required|integer',
  834. 'permissions' => 'required|json',
  835. ]);
  836. if($validator->fails()){
  837. return $this->responseController->makeResponse(
  838. true,
  839. "Se encontraron uno o más errores.",
  840. $this->responseController->makeErrors(
  841. $validator->errors()->messages()
  842. ),
  843. 401
  844. );
  845. }
  846. $form = $request->all();
  847. $idUser = $this->encryptionController->decrypt($form['id_user']);
  848. if(!$idUser){
  849. return $this->responseController->makeResponse(true, "El ID del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  850. }
  851. $usr = DB::table('S002V01TUSUA')->where([
  852. ['USUA_NULI', '=', $form['linea']],
  853. ['USUA_IDUS', '=', $idUser],
  854. ])->first();
  855. if(is_null($usr)){
  856. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  857. }
  858. $idFile = $this->encryptionController->decrypt($form['id_file']);
  859. if(!$idFile){
  860. return $this->responseController->makeResponse(true, "El ID del archivo solicitado no fue encriptado correctamente.", [], 400);
  861. }
  862. $idFileArr = explode('=', $idFile);
  863. $codeArr = explode('-', $idFileArr[0]);
  864. $file = DB::table('S002V01TAFAL')->where([
  865. ['AFAL_NULI', '=', $codeArr[0]],
  866. ['AFAL_COMO', '=', $codeArr[1]],
  867. ['AFAL_CLDO', '=', $codeArr[2]],
  868. ['AFAL_FECR', '=', $codeArr[3]],
  869. ['AFAL_NUSE', '=', $codeArr[4]],
  870. ['AFAL_NUVE', '=', $idFileArr[1]],
  871. ])->first();
  872. if(is_null($file)){
  873. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  874. }
  875. $permissionsArr = json_decode($file->AFAL_USAC, true);
  876. if(!in_array($idUser, $permissionsArr)){
  877. $usrName = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA) . " ($idUser)";
  878. return $this->responseController->makeResponse(true, "El usuario $usrName no tiene permisos para modificar el archivo $idFile.", [], 401);
  879. }
  880. $newPermissionsArr = json_decode($form['permissions'], true);
  881. if(count($newPermissionsArr) <= 0){
  882. return $this->responseController->makeResponse(true, "El arreglo de permisos está vacío.", [], 400);
  883. }
  884. $newPermissionsArrDec = [];
  885. foreach($newPermissionsArr as $val){
  886. $idDec = $this->encryptionController->decrypt($val);
  887. if(!$idDec){
  888. return $this->responseController->makeResponse(true, "Alguno de los ID's enviados no fue encriptado correctamente.", [], 400);
  889. }
  890. $newPermissionsArrDec[] = $idDec;
  891. }
  892. if(!in_array($file->AFAL_USRE, $newPermissionsArrDec)){
  893. return $this->responseController->makeResponse(true, "El arreglo de permisos no incluye el ID del propietario.", [], 401);
  894. }
  895. $now = $this->functionsController->now();
  896. $nowStr = $now->toDateTimeString();
  897. $usac = json_encode($newPermissionsArrDec);
  898. DB::table('S002V01TAFAL')->where([
  899. ['AFAL_NULI', '=', $codeArr[0]],
  900. ['AFAL_COMO', '=', $codeArr[1]],
  901. ['AFAL_CLDO', '=', $codeArr[2]],
  902. ['AFAL_FECR', '=', $codeArr[3]],
  903. ['AFAL_NUSE', '=', $codeArr[4]],
  904. ['AFAL_NUVE', '=', $idFileArr[1]],
  905. ])->update([
  906. 'AFAL_USAC' => $usac,
  907. 'AFAL_USMO' => $idUser,
  908. 'AFAL_FEMO' => $nowStr,
  909. ]);
  910. $actions = DB::getQueryLog();
  911. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  912. $idac = $this->functionsController->registerActivity(
  913. $form['linea'],
  914. 'S002V01M04GDEL',
  915. 'S002V01F01ADDO',
  916. 'S002V01P01GEDO',
  917. 'Actualización',
  918. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó los usuarios con acceso del archivo $idFile.",
  919. $idUser,
  920. $nowStr
  921. );
  922. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  923. return $this->responseController->makeresponse(false, "EXITO");
  924. }
  925. public function deleteFile(Request $request) {
  926. DB::enableQueryLog();
  927. $validator = Validator::make($request->all(), [
  928. 'id_user' => 'required|string',
  929. 'id_file' => 'required|string',
  930. 'linea' => 'required|integer',
  931. ]);
  932. if($validator->fails()){
  933. return $this->responseController->makeResponse(
  934. true,
  935. "Se encontraron uno o más errores.",
  936. $this->responseController->makeErrors(
  937. $validator->errors()->messages()
  938. ),
  939. 401
  940. );
  941. }
  942. $form = $request->all();
  943. $idUser = $this->encryptionController->decrypt($form['id_user']);
  944. if(!$idUser){
  945. return $this->responseController->makeResponse(true, "El ID del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  946. }
  947. $usr = DB::table('S002V01TUSUA')->where([
  948. ['USUA_NULI', '=', $form['linea']],
  949. ['USUA_IDUS', '=', $idUser],
  950. ])->first();
  951. if(is_null($usr)){
  952. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  953. }
  954. $idFile = $this->encryptionController->decrypt($form['id_file']);
  955. if(!$idFile){
  956. return $this->responseController->makeResponse(true, "El ID del archivo solicitado no fue encriptado correctamente.", [], 400);
  957. }
  958. $idFileArr = explode('=', $idFile);
  959. $codeArr = explode('-', $idFileArr[0]);
  960. $file = DB::table('S002V01TAFAL')->where([
  961. ['AFAL_NULI', '=', $codeArr[0]],
  962. ['AFAL_COMO', '=', $codeArr[1]],
  963. ['AFAL_CLDO', '=', $codeArr[2]],
  964. ['AFAL_FECR', '=', $codeArr[3]],
  965. ['AFAL_NUSE', '=', $codeArr[4]],
  966. ['AFAL_NUVE', '=', $idFileArr[1]],
  967. ])->first();
  968. if(is_null($file)){
  969. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  970. }else if($file->AFAL_ESTA == 'Eliminado'){
  971. return $this->responseController->makeResponse(true, 'El archivo solicitado está eliminado.', [], 401);
  972. }
  973. $now = $this->functionsController->now();
  974. $nowStr = $now->toDateTimeString();
  975. DB::table('S002V01TAFAL')->where([
  976. ['AFAL_NULI', '=', $codeArr[0]],
  977. ['AFAL_COMO', '=', $codeArr[1]],
  978. ['AFAL_CLDO', '=', $codeArr[2]],
  979. ['AFAL_FECR', '=', $codeArr[3]],
  980. ['AFAL_NUSE', '=', $codeArr[4]],
  981. ['AFAL_NUVE', '=', $idFileArr[1]],
  982. ])->update([
  983. 'AFAL_ESTA' => 'Eliminado',
  984. 'AFAL_USMO' => $idUser,
  985. 'AFAL_FEMO' => $nowStr
  986. ]);
  987. $actions = DB::getQueryLog();
  988. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  989. $idac = $this->functionsController->registerActivity(
  990. $form['linea'],
  991. 'S002V01M04GDEL',
  992. 'S002V01F01ADDO',
  993. 'S002V01P01GEDO',
  994. 'Eliminación',
  995. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó el archivo $idFile.",
  996. $idUser,
  997. $nowStr
  998. );
  999. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1000. return $this->responseController->makeresponse(false, "EXITO");
  1001. }
  1002. public function getAssociatedWorkOrders($idFile, $idUser, $line) {
  1003. DB::enableQueryLog();
  1004. $idUser = $this->encryptionController->decrypt($idUser);
  1005. if(!$idUser){
  1006. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  1007. }
  1008. $usr = DB::table('S002V01TUSUA')->where([
  1009. ['USUA_NULI', '=', $line],
  1010. ['USUA_IDUS', '=', $idUser]
  1011. ])->first();
  1012. if(is_null($usr)){
  1013. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  1014. }
  1015. $idFile = $this->encryptionController->decrypt($idFile);
  1016. if(!$idFile){
  1017. return $this->responseController->makeResponse(true, 'El ID del archivo solicitado no está encriptado correctamente.', [], 400);
  1018. }
  1019. $idFileArr = explode('=', $idFile);
  1020. $codeArr = explode('-', $idFileArr[0]);
  1021. $file = DB::table('S002V01TAFAL')->where([
  1022. ['AFAL_NULI', '=', $line],
  1023. ['AFAL_COMO', '=', $codeArr[1]],
  1024. ['AFAL_CLDO', '=', $codeArr[2]],
  1025. ['AFAL_FECR', '=', $codeArr[3]],
  1026. ['AFAL_NUSE', '=', $codeArr[4]],
  1027. ['AFAL_NUVE', '=', $idFileArr[1]],
  1028. ])->first();
  1029. if(is_null($file)){
  1030. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  1031. }
  1032. $orders = DB::table('S002V01TOTPR')
  1033. ->select([
  1034. 'OTPR_IDOT AS IDORDEN',
  1035. 'OTPR_EQIN AS EQUIPAMIENTO',
  1036. 'OTPR_ACAS AS ACTIVADOR',
  1037. 'OTPR_CLAS AS CLASIFICACION',
  1038. 'ACTI_PRIO AS PRIORIDAD',
  1039. 'ACTI_TIAC AS TIPOACTIVADOR'
  1040. ])->join('S002V01TACTI', 'OTPR_ACAS', '=', 'ACTI_IDCO')
  1041. ->where('OTPR_NULI', '=', $line)
  1042. ->whereJsonContains('OTPR_DONE', $idFile)->get()->all();
  1043. $now = $this->functionsController->now();
  1044. $nowStr = $now->toDateTimeString();
  1045. $actions = DB::getQueryLog();
  1046. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1047. $idac = $this->functionsController->registerActivity(
  1048. $line,
  1049. 'S002V01M04GDEL',
  1050. 'S002V01F01ADDO',
  1051. 'S002V01P01GEDO',
  1052. 'Consulta',
  1053. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las órdenes de trabajo relacionadas al archivo $idFile.",
  1054. $idUser,
  1055. $nowStr
  1056. );
  1057. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1058. return $this->responseController->makeresponse(false, "EXITO", $orders);
  1059. }
  1060. public function changeAssociationStatus(Request $request) {
  1061. DB::enableQueryLog();
  1062. $validator = Validator::make($request->all(), [
  1063. 'id_user' => 'required|string',
  1064. 'linea' => 'required|integer',
  1065. 'id_file' => 'required|string',
  1066. 'action' => 'required|string|in:disassociate,associate',
  1067. 'id_order' => 'required|string',
  1068. ]);
  1069. if($validator->fails()){
  1070. return $this->responseController->makeResponse(
  1071. true,
  1072. "Se encontraron uno o más errores.",
  1073. $this->responseController->makeErrors(
  1074. $validator->errors()->messages()
  1075. ),
  1076. 401
  1077. );
  1078. }
  1079. $form = $request->all();
  1080. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1081. if(!$idUser){
  1082. return $this->responseController->makeResponse(true, "El ID del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  1083. }
  1084. $usr = DB::table('S002V01TUSUA')->where([
  1085. ['USUA_NULI', '=', $form['linea']],
  1086. ['USUA_IDUS', '=', $idUser],
  1087. ])->first();
  1088. if(is_null($usr)){
  1089. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1090. }
  1091. $idFile = $this->encryptionController->decrypt($form['id_file']);
  1092. if(!$idFile){
  1093. return $this->responseController->makeResponse(true, "El ID del archivo solicitado no fue encriptado correctamente.", [], 400);
  1094. }
  1095. $idFileArr = explode('=', $idFile);
  1096. $codeArr = explode('-', $idFileArr[0]);
  1097. $file = DB::table('S002V01TAFAL')->where([
  1098. ['AFAL_NULI', '=', $codeArr[0]],
  1099. ['AFAL_COMO', '=', $codeArr[1]],
  1100. ['AFAL_CLDO', '=', $codeArr[2]],
  1101. ['AFAL_FECR', '=', $codeArr[3]],
  1102. ['AFAL_NUSE', '=', $codeArr[4]],
  1103. ['AFAL_NUVE', '=', $idFileArr[1]],
  1104. ])->first();
  1105. if(is_null($file)){
  1106. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  1107. }else if($file->AFAL_ESTA == 'Eliminado'){
  1108. return $this->responseController->makeResponse(true, 'El archivo solicitado está eliminado.', [], 401);
  1109. }
  1110. $idOrder = $this->encryptionController->decrypt($form['id_order']);
  1111. if(!$idOrder){
  1112. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no fue encriptado correctamente.', [], 400);
  1113. }
  1114. $workOrder = DB::table('S002V01TOTPR')->where([
  1115. ['OTPR_NULI', '=', $form['linea']],
  1116. ['OTPR_IDOT', '=', $idOrder]
  1117. ])->first();
  1118. if(is_null($workOrder)){
  1119. return $this->responseController->makeResponse(true, 'La orden solicitada no existe', [], 404);
  1120. }else if($workOrder->OTPR_ESTA == 'E'){
  1121. return $this->responseController->makeResponse(true, 'La orden solicitada está eliminada', [], 401);
  1122. }
  1123. $filesArr = json_decode($workOrder->OTPR_DONE, true);
  1124. if($form['action'] == 'disassociate'){
  1125. if(!in_array($idFile, $filesArr)){
  1126. return $this->responseController->makeResponse(true, 'El archivo enviado no está asociado a la orden de trabajo.', [], 401);
  1127. }
  1128. $filesArrAux = [];
  1129. foreach($filesArr as $fileStr){
  1130. if($fileStr != $idFile){
  1131. $filesArrAux[] = $fileStr;
  1132. }
  1133. }
  1134. $filesArr = $filesArrAux;
  1135. }else{
  1136. if(in_array($idFile, $filesArr)){
  1137. return $this->responseController->makeResponse(true, 'El archivo enviado ya está asociado a la orden de trabajo.', [], 401);
  1138. }
  1139. $filesArr[] = $idFile;
  1140. }
  1141. $now = $this->functionsController->now();
  1142. $nowStr = $now->toDateTimeString();
  1143. $filesStr = json_encode($filesArr);
  1144. DB::table('S002V01TOTPR')->where([
  1145. ['OTPR_NULI', '=', $form['linea']],
  1146. ['OTPR_IDOT', '=', $idOrder]
  1147. ])->update([
  1148. 'OTPR_DONE' => $filesStr,
  1149. 'OTPR_USMO' => $idUser,
  1150. 'OTPR_FEMO' => $nowStr,
  1151. ]);
  1152. $actions = DB::getQueryLog();
  1153. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1154. $idac = $this->functionsController->registerActivity(
  1155. $form['linea'],
  1156. 'S002V01M04GDEL',
  1157. 'S002V01F01ADDO',
  1158. 'S002V01P01GEDO',
  1159. 'Actualización',
  1160. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó la relación del archivo $idFile y la orden de trabajo #$idOrder.",
  1161. $idUser,
  1162. $nowStr
  1163. );
  1164. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1165. return $this->responseController->makeresponse(false, "EXITO");
  1166. }
  1167. }