DocumentManagementController.php 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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. $idUser = $this->encryptionController->decrypt($idUser);
  151. if(!$idUser){
  152. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  153. }
  154. $usr = DB::table('S002V01TUSUA')->where([
  155. ['USUA_IDUS', '=', $idUser],
  156. ['USUA_NULI', '=', $line]
  157. ])->first();
  158. if(is_null($usr)){
  159. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  160. }
  161. $id = $this->encryptionController->decrypt($id);
  162. if(!$id){
  163. return $this->responseController->makeResponse(true, 'El ID del archivo requerido no está encriptado correctamente.', [], 400);
  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. $idUser = "0000000001";
  361. if(!$idUser){
  362. return $this->responseController->makeResponse(true, "El id del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  363. }
  364. $usr = DB::table('S002V01TUSUA')->where([
  365. ['USUA_IDUS', '=', $idUser],
  366. ['USUA_NULI', '=', $form['linea']]
  367. ])->first();
  368. if(is_null($usr)){
  369. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  370. }
  371. $originalFileName = $request->file('file')->getClientOriginalName();
  372. $extension = $request->file('file')->extension();
  373. $size = $request->file('file')->getSize();
  374. $extArr = explode(".", $originalFileName);
  375. $extArr = array_reverse($extArr);
  376. $extStr = strtolower($extArr[0]);
  377. $isValid = $this->functionsController->checkFileSize($extStr, $size);
  378. if($isValid){
  379. $dir = str_replace("app\\Http\\Controllers", "storage\\app", __DIR__);
  380. $tmpPath = $request->file('file')->store('tempFiles');
  381. $tmpPath = str_replace("/", "\\", $tmpPath);
  382. $location = "$dir\\$tmpPath";
  383. $nowStr = Carbon::now('America/Mexico_city')->toDateTimeString();
  384. $fileID = DB::table('S002V01TARTE')->insertGetId([
  385. 'ARTE_NULI' => $form['linea'],
  386. 'ARTE_NOAR' => $originalFileName,
  387. 'ARTE_EXTE' => $extension,
  388. 'ARTE_TAMA' => $size,
  389. 'ARTE_UBTE' => $location,
  390. 'ARTE_USRE' => $idUser,
  391. 'ARTE_FERE' => $nowStr
  392. ]);
  393. $actions = DB::getQueryLog();
  394. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  395. $idac = $this->functionsController->registerActivity(
  396. $form['linea'],
  397. 'S002V01M04GDEL',
  398. 'S002V01F01ADDO',
  399. 'S002V01P05REDO',
  400. 'Registro',
  401. "El usuario $name (" . $usr->USUA_IDUS . ") subió de manera temporal el archivo $originalFileName.",
  402. $idUser,
  403. $nowStr,
  404. );
  405. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  406. return $this->responseController->makeresponse(false, "EXITO", [
  407. 'idArchivo' => $this->encryptionController->encrypt($fileID),
  408. ]);
  409. }else{
  410. 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);
  411. }
  412. }
  413. public function deleteTempFile(Request $request){
  414. DB::enableQueryLog();
  415. $validator = Validator::make($request->all(), [
  416. 'id_user' => 'required|string',
  417. 'id_file' => 'required|string',
  418. 'linea' => 'required|integer'
  419. ]);
  420. if($validator->fails()){
  421. return $this->responseController->makeResponse(
  422. true,
  423. "Se encontraron uno o más errores.",
  424. $this->responseController->makeErrors(
  425. $validator->errors()->messages()
  426. ),
  427. 401
  428. );
  429. }
  430. $form = $request->all();
  431. $idUser = $this->encryptionController->decrypt($form['id_user']);
  432. if(!$idUser){
  433. return $this->responseController->makeResponse(true, "El id del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  434. }
  435. $usr = DB::table('S002V01TUSUA')->where([
  436. ['USUA_IDUS', '=', $idUser],
  437. ['USUA_NULI', '=', $form['linea']]
  438. ])->first();
  439. if(is_null($usr)){
  440. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  441. }
  442. $idFile = $this->encryptionController->decrypt($form['id_file']);
  443. if(!$idFile){
  444. return $this->responseController->makeResponse(true, "El id del archivo que desea eliminar no fue encriptado correctamente", [], 400);
  445. }
  446. $file = DB::table('S002V01TARTE')->where([
  447. ['ARTE_IDAR', '=', $idFile],
  448. ['ARTE_NULI', '=', $form['linea']]
  449. ])->first();
  450. if(is_null($file)){
  451. return $this->responseController->makeResponse(true, 'El archivo que desea eliminar no está registrado', [], 404);
  452. }
  453. if(file_exists($file->ARTE_UBTE)){
  454. unlink($file->ARTE_UBTE);
  455. }
  456. $nowStr = Carbon::now('America/Mexico_city')->toDateTimeString();
  457. DB::table('S002V01TARTE')->where([
  458. ['ARTE_IDAR', '=', $idFile],
  459. ['ARTE_NULI', '=', $form['linea']]
  460. ])->update([
  461. 'ARTE_ESTA' => 'Eliminado',
  462. 'ARTE_USMO' => $idUser,
  463. 'ARTE_FEMO' => $nowStr
  464. ]);
  465. $actions = DB::getQueryLog();
  466. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  467. $idac = $this->functionsController->registerActivity(
  468. $form['linea'],
  469. 'S002V01M04GDEL',
  470. 'S002V01F01ADDO',
  471. 'S002V01P05REDO',
  472. 'Eliminación',
  473. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó el archivo " . $file->ARTE_NOAR,
  474. $idUser,
  475. $nowStr,
  476. );
  477. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  478. return $this->responseController->makeresponse(false, "EXITO");
  479. }
  480. public function saveFinalFile(Request $request) {
  481. DB::enableQueryLog();
  482. $validator = Validator::make($request->all(), [
  483. 'id_user' => 'required|string',
  484. 'id_file' => 'required|string',
  485. 'linea' => 'required|integer',
  486. 'module' => 'required|string|max:4',
  487. 'clasification' => 'required|string|max:2',
  488. 'has_order' => 'required|string|in:S,N',
  489. 'id_order' => 'required_if:has_order,=,S|string',
  490. ]);
  491. if($validator->fails()){
  492. return $this->responseController->makeResponse(
  493. true,
  494. "Se encontraron uno o más errores.",
  495. $this->responseController->makeErrors(
  496. $validator->errors()->messages()
  497. ),
  498. 401
  499. );
  500. }
  501. $form = $request->all();
  502. $idUser = $this->encryptionController->decrypt($form['id_user']);
  503. if(!$idUser){
  504. return $this->responseController->makeResponse(true, "El id del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  505. }
  506. $usr = DB::table('S002V01TUSUA')->where([
  507. ['USUA_NULI', '=', $form['linea']],
  508. ['USUA_IDUS', '=', $idUser],
  509. ])->first();
  510. if(is_null($usr)){
  511. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  512. }
  513. $idFile = $this->encryptionController->decrypt($form['id_file']);
  514. if(!$idFile){
  515. return $this->responseController->makeResponse(true, "El id del archivo solicitado no fue encriptado correctamente", [], 400);
  516. }
  517. $tempFile = DB::table('S002V01TARTE')->where([
  518. ['ARTE_NULI', '=', $form['linea']],
  519. ['ARTE_IDAR', '=', $idFile],
  520. ])->first();
  521. if(is_null($tempFile)){
  522. return $this->responseController->makeResponse(true, 'El archivo consultado no está registrado', [], 404);
  523. }else if($tempFile->ARTE_ESTA == 'Eliminado'){
  524. return $this->responseController->makeResponse(true, 'El archivo consultado está eliminado', [], 404);
  525. }
  526. $fileResponse = $this->moveFinalFile(
  527. intval($form['linea']),
  528. $form['module'],
  529. $form['clasification'],
  530. $tempFile,
  531. $idUser,
  532. );
  533. if(!$fileResponse[0]){
  534. return $this->responseController->makeResponse(true, $fileResponse[1], [], 400);
  535. }
  536. $now = $this->functionsController->now();
  537. $nowStr = $now->toDateTimeString();
  538. if($form['has_order'] == 'S' && ($form['module'] == 'GMPR' || $form['module'] == 'GMCO')){
  539. $idOrder = $this->encryptionController->decrypt($form['id_order']);
  540. if(!$idOrder){
  541. return $this->responseController->makeResponse(true, "El id de la orden relacionada no fue encriptado correctamente", [], 400);
  542. }
  543. $order = DB::table('S002V01TOTPR')->where([
  544. ['OTPR_IDOT', '=', $idOrder],
  545. ['OTPR_NULI', '=', $form['linea']]
  546. ])->first();
  547. if(is_null($order)){
  548. return $this->responseController->makeResponse(true, 'La orden solicitada no está registrada', [], 404);
  549. }
  550. $documentsArr = json_decode($order->OTPR_DONE, true);
  551. $fileCodeEnc = $this->encryptionController->encrypt($fileResponse[1]);
  552. $documentsArr[] = $fileCodeEnc;
  553. $documentsStr = json_encode($documentsArr);
  554. DB::table('S002V01TOTPR')->where([
  555. ['OTPR_IDOT', '=', $idOrder],
  556. ['OTPR_NULI', '=', $form['linea']]
  557. ])->update([
  558. 'OTPR_DONE' => $documentsStr,
  559. 'OTPR_USMO' => $idUser,
  560. 'OTPR_FEMO' => $nowStr
  561. ]);
  562. }
  563. $actions = DB::getQueryLog();
  564. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  565. $idac = $this->functionsController->registerActivity(
  566. $form['linea'],
  567. 'S002V01M04GDEL',
  568. 'S002V01F01ADDO',
  569. 'S002V01P05REDO',
  570. 'Registro',
  571. "El usuario $name (" . $usr->USUA_IDUS . ") registró el archivo " . $tempFile->ARTE_NOAR,
  572. $idUser,
  573. $nowStr,
  574. );
  575. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  576. return $this->responseController->makeresponse(false, "EXITO");
  577. }
  578. public function moveFinalFile(int $line, string $como, string $cldo, object $tempFile, string $idUser) {
  579. $modulesCodes = [
  580. "GEAD","GIST","GMPR","GMCO","GEEQ","COAC","GEPR",
  581. "ANFA","PCSA","GPRS","GEPR","GDEL","ADSI","USPE"
  582. ];
  583. $clasifications = [
  584. "AV","AU","CA","CE","CO","DP","FA","FI",
  585. "FO","IN","LA","OR","PL","RE","VI"
  586. ];
  587. if(!in_array($como, $modulesCodes)){
  588. return [false, "El código $como es inválido."];
  589. }
  590. if(!in_array($cldo, $clasifications)){
  591. return [false, "La clasificación $cldo es inválida."];
  592. }
  593. $now = $this->functionsController->now();
  594. $nowStr = $now->toDateTimeString();
  595. $dateTimeArr = explode(' ', $nowStr);
  596. $dateArr = explode('-', $dateTimeArr[0]);
  597. $year = substr($dateArr[0], 2);
  598. $fecr = "$year$dateArr[1]$dateArr[2]";
  599. $sec = DB::table('S002V01TAFAL')->where([
  600. ['AFAL_COMO', '=', $como],
  601. ['AFAL_CLDO', '=', $cldo],
  602. ['AFAL_NULI', '=', $line],
  603. ])->orderBy('AFAL_NUSE', 'desc')->first();
  604. $nuse = 1;
  605. if(!is_null($sec)){
  606. $nuse = intval($sec->AFAL_NUSE) + 1;
  607. }
  608. $fileNameArr = explode('.', $tempFile->ARTE_NOAR);
  609. array_pop($fileNameArr);
  610. $noar = implode('.', $fileNameArr);
  611. $exte = $tempFile->ARTE_EXTE;
  612. $ver = DB::table('S002V01TAFAL')->where([
  613. ['AFAL_NULI', '=', $line],
  614. ['AFAL_COMO', '=', $como],
  615. ['AFAL_CLDO', '=', $cldo],
  616. ['AFAL_NOAR', '=', $noar],
  617. ['AFAL_EXTE', '=', $exte],
  618. ])->orderBy('AFAL_NUVE', 'desc')->first();
  619. $nuve = 1;
  620. if(!is_null($ver)){
  621. $nuve = intval($sec->AFAL_NUVE) + 1;
  622. }
  623. $tama = $tempFile->ARTE_TAMA;
  624. $ubiFileArr = explode('tempFiles', $tempFile->ARTE_UBTE);
  625. $ubic = $ubiFileArr[0] . 'files' . $ubiFileArr[1];
  626. if(file_exists($tempFile->ARTE_UBTE)){
  627. rename($tempFile->ARTE_UBTE, $ubic);
  628. DB::table('S002V01TARTE')->where([
  629. ['ARTE_IDAR', '=', $tempFile->ARTE_IDAR],
  630. ['ARTE_NULI', '=', $line],
  631. ])->update([
  632. 'ARTE_ESTA' => 'Eliminado',
  633. 'ARTE_USMO' => $idUser,
  634. 'ARTE_FEMO' => $nowStr,
  635. ]);
  636. }
  637. $code = $line < 10 ? "0$line" : "$line";
  638. $code .= "-$como-$cldo-$fecr-";
  639. for($i = strlen($nuse); $i < 6; $i++){
  640. $code .= "0";
  641. }
  642. $code .= "$nuse=";
  643. $code .= $nuve < 10 ? "0$nuve=" : "$nuve=";
  644. $code .= "$noar.$exte";
  645. $usac = json_encode([$idUser]);
  646. DB::table('S002V01TAFAL')->insert([
  647. 'AFAL_NULI' => $line,
  648. 'AFAL_COMO' => $como,
  649. 'AFAL_CLDO' => $cldo,
  650. 'AFAL_FECR' => $fecr,
  651. 'AFAL_NUSE' => $nuse,
  652. 'AFAL_NUVE' => $nuve,
  653. 'AFAL_NOAR' => $noar,
  654. 'AFAL_EXTE' => $exte,
  655. 'AFAL_TAMA' => $tama,
  656. 'AFAL_UBIC' => $ubic,
  657. 'AFAL_USAC' => $usac,
  658. 'AFAL_USRE' => $idUser,
  659. 'AFAL_FERE' => $nowStr
  660. ]);
  661. return [true, $code];
  662. }
  663. public function getPublicDocumentURL($id, $idUser, $line) {
  664. DB::enableQueryLog();
  665. $idUser = $this->encryptionController->decrypt($idUser);
  666. if(!$idUser){
  667. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  668. }
  669. $usr = DB::table('S002V01TUSUA')->where([
  670. ['USUA_NULI', '=', $line],
  671. ['USUA_IDUS', '=', $idUser],
  672. ])->first();
  673. if(is_null($usr)){
  674. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  675. }
  676. $id = $this->encryptionController->decrypt($id);
  677. if(!$id){
  678. return $this->responseController->makeResponse(true, 'El ID del archivo solicitado no está encriptado correctamente.', [], 400);
  679. }
  680. $idArr = explode('=', $id);
  681. $codeArr = explode('-', $idArr[0]);
  682. $file = DB::table('S002V01TAFAL')->where([
  683. ['AFAL_NULI', '=', $codeArr[0]],
  684. ['AFAL_COMO', '=', $codeArr[1]],
  685. ['AFAL_CLDO', '=', $codeArr[2]],
  686. ['AFAL_FECR', '=', $codeArr[3]],
  687. ['AFAL_NUSE', '=', $codeArr[4]],
  688. ['AFAL_NUVE', '=', $idArr[1]],
  689. ])->first();
  690. if(is_null($file)){
  691. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  692. }
  693. $ubicArr = explode('storage', $file->AFAL_UBIC);
  694. $publicUbi = "$ubicArr[0]public_files\\$id";
  695. if(!file_exists($publicUbi)){
  696. copy($file->AFAL_UBIC, $publicUbi);
  697. }
  698. $apiURI = $this->functionsController->getApiURI();
  699. $publicUbiStr = str_replace('C:\inetpub\wwwroot\\', $apiURI, $publicUbi);
  700. $publicUbiStr = str_replace('\\', '/', $publicUbiStr);
  701. $now = $this->functionsController->now();
  702. $nowStr = $now->toDateTimeString();
  703. $actions = DB::getQueryLog();
  704. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  705. $idac = $this->functionsController->registerActivity(
  706. $line,
  707. 'S002V01M04GDEL',
  708. 'S002V01F01ADDO',
  709. 'S002V01P01GEDO',
  710. 'Consulta',
  711. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la URL pública del archivo $id.",
  712. $idUser,
  713. $nowStr
  714. );
  715. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  716. return $this->responseController->makeresponse(false, "EXITO", ['public_uri' => $publicUbiStr]);
  717. }
  718. public function getPublicTempDocumentURL($id, $idUser, $line) {
  719. DB::enableQueryLog();
  720. $idUser = $this->encryptionController->decrypt($idUser);
  721. if(!$idUser){
  722. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  723. }
  724. $usr = DB::table('S002V01TUSUA')->where([
  725. ['USUA_NULI', '=', $line],
  726. ['USUA_IDUS', '=', $idUser],
  727. ])->first();
  728. if(is_null($usr)){
  729. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  730. }
  731. $id = $this->encryptionController->decrypt($id);
  732. if(!$id){
  733. return $this->responseController->makeResponse(true, 'El ID del archivo solicitado no está encriptado correctamente.', [], 400);
  734. }
  735. $file = DB::table('S002V01TARTE')->where([
  736. ['ARTE_NULI', '=', $line],
  737. ['ARTE_IDAR', '=', $id],
  738. ])->first();
  739. if(is_null($file)){
  740. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  741. }else if($file->ARTE_ESTA == 'Eliminado'){
  742. return $this->responseController->makeResponse(true, 'El archivo solicitado está eliminado.', [], 404);
  743. }
  744. $ubicArr = explode('storage', $file->ARTE_UBTE);
  745. $publicUbi = "$ubicArr[0]public_files\\" . $file->ARTE_NOAR;
  746. if(!file_exists($publicUbi)){
  747. copy($file->ARTE_UBTE, $publicUbi);
  748. }
  749. $apiURI = $this->functionsController->getApiURI();
  750. $publicUbiStr = str_replace('C:\inetpub\wwwroot\\', $apiURI, $publicUbi);
  751. $publicUbiStr = str_replace('\\', '/', $publicUbiStr);
  752. $now = $this->functionsController->now();
  753. $nowStr = $now->toDateTimeString();
  754. $actions = DB::getQueryLog();
  755. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  756. $idac = $this->functionsController->registerActivity(
  757. $line,
  758. 'S002V01M04GDEL',
  759. 'S002V01F01ADDO',
  760. 'S002V01P01GEDO',
  761. 'Consulta',
  762. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la URL pública del archivo temporal " . $file->ARTE_NOAR . ".",
  763. $idUser,
  764. $nowStr
  765. );
  766. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  767. return $this->responseController->makeresponse(false, "EXITO", ['public_uri' => $publicUbiStr]);
  768. }
  769. public function getFileAccess($id, $idUser, $line) {
  770. DB::enableQueryLog();
  771. $idUser = $this->encryptionController->decrypt($idUser);
  772. if(!$idUser){
  773. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  774. }
  775. $usr = DB::table('S002V01TUSUA')->where([
  776. ['USUA_NULI', '=', $line],
  777. ['USUA_IDUS', '=', $idUser],
  778. ])->first();
  779. if(is_null($usr)){
  780. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  781. }
  782. $id = $this->encryptionController->decrypt($id);
  783. if(!$id){
  784. return $this->responseController->makeResponse(true, 'El ID del archivo solicitado no está encriptado correctamente.', [], 400);
  785. }
  786. $idArr = explode('=', $id);
  787. $codeArr = explode('-', $idArr[0]);
  788. $file = DB::table('S002V01TAFAL')->where([
  789. ['AFAL_NULI', '=', $codeArr[0]],
  790. ['AFAL_COMO', '=', $codeArr[1]],
  791. ['AFAL_CLDO', '=', $codeArr[2]],
  792. ['AFAL_FECR', '=', $codeArr[3]],
  793. ['AFAL_NUSE', '=', $codeArr[4]],
  794. ['AFAL_NUVE', '=', $idArr[1]],
  795. ])->first();
  796. if(is_null($file)){
  797. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  798. }
  799. $owner = $this->encryptionController->encrypt($file->AFAL_USRE);
  800. $permissionsArr = json_decode($file->AFAL_USAC, true);
  801. foreach($permissionsArr as $k=>$v){
  802. $idEnc = $this->encryptionController->encrypt($v);
  803. $permissionsArr[$k] = $idEnc;
  804. }
  805. $now = $this->functionsController->now();
  806. $nowStr = $now->toDateTimeString();
  807. $actions = DB::getQueryLog();
  808. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  809. $idac = $this->functionsController->registerActivity(
  810. $line,
  811. 'S002V01M04GDEL',
  812. 'S002V01F01ADDO',
  813. 'S002V01P01GEDO',
  814. 'Consulta',
  815. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los usuarios con acceso al archivo $id.",
  816. $idUser,
  817. $nowStr
  818. );
  819. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  820. return $this->responseController->makeresponse(false, "EXITO", [
  821. 'access' => $permissionsArr,
  822. 'owner' => $owner
  823. ]);
  824. }
  825. public function updateFilePermissions(Request $request) {
  826. DB::enableQueryLog();
  827. $validator = Validator::make($request->all(), [
  828. 'id_user' => 'required|string',
  829. 'id_file' => 'required|string',
  830. 'linea' => 'required|integer',
  831. 'permissions' => 'required|json',
  832. ]);
  833. if($validator->fails()){
  834. return $this->responseController->makeResponse(
  835. true,
  836. "Se encontraron uno o más errores.",
  837. $this->responseController->makeErrors(
  838. $validator->errors()->messages()
  839. ),
  840. 401
  841. );
  842. }
  843. $form = $request->all();
  844. $idUser = $this->encryptionController->decrypt($form['id_user']);
  845. if(!$idUser){
  846. return $this->responseController->makeResponse(true, "El ID del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  847. }
  848. $usr = DB::table('S002V01TUSUA')->where([
  849. ['USUA_NULI', '=', $form['linea']],
  850. ['USUA_IDUS', '=', $idUser],
  851. ])->first();
  852. if(is_null($usr)){
  853. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  854. }
  855. $idFile = $this->encryptionController->decrypt($form['id_file']);
  856. if(!$idFile){
  857. return $this->responseController->makeResponse(true, "El ID del archivo solicitado no fue encriptado correctamente.", [], 400);
  858. }
  859. $idFileArr = explode('=', $idFile);
  860. $codeArr = explode('-', $idFileArr[0]);
  861. $file = DB::table('S002V01TAFAL')->where([
  862. ['AFAL_NULI', '=', $codeArr[0]],
  863. ['AFAL_COMO', '=', $codeArr[1]],
  864. ['AFAL_CLDO', '=', $codeArr[2]],
  865. ['AFAL_FECR', '=', $codeArr[3]],
  866. ['AFAL_NUSE', '=', $codeArr[4]],
  867. ['AFAL_NUVE', '=', $idFileArr[1]],
  868. ])->first();
  869. if(is_null($file)){
  870. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  871. }
  872. $permissionsArr = json_decode($file->AFAL_USAC, true);
  873. if(!in_array($idUser, $permissionsArr)){
  874. $usrName = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA) . " ($idUser)";
  875. return $this->responseController->makeResponse(true, "El usuario $usrName no tiene permisos para modificar el archivo $idFile.", [], 401);
  876. }
  877. $newPermissionsArr = json_decode($form['permissions'], true);
  878. if(count($newPermissionsArr) <= 0){
  879. return $this->responseController->makeResponse(true, "El arreglo de permisos está vacío.", [], 400);
  880. }
  881. $newPermissionsArrDec = [];
  882. foreach($newPermissionsArr as $val){
  883. $idDec = $this->encryptionController->decrypt($val);
  884. if(!$idDec){
  885. return $this->responseController->makeResponse(true, "Alguno de los ID's enviados no fue encriptado correctamente.", [], 400);
  886. }
  887. $newPermissionsArrDec[] = $idDec;
  888. }
  889. if(!in_array($file->AFAL_USRE, $newPermissionsArrDec)){
  890. return $this->responseController->makeResponse(true, "El arreglo de permisos no incluye el ID del propietario.", [], 401);
  891. }
  892. $now = $this->functionsController->now();
  893. $nowStr = $now->toDateTimeString();
  894. $usac = json_encode($newPermissionsArrDec);
  895. DB::table('S002V01TAFAL')->where([
  896. ['AFAL_NULI', '=', $codeArr[0]],
  897. ['AFAL_COMO', '=', $codeArr[1]],
  898. ['AFAL_CLDO', '=', $codeArr[2]],
  899. ['AFAL_FECR', '=', $codeArr[3]],
  900. ['AFAL_NUSE', '=', $codeArr[4]],
  901. ['AFAL_NUVE', '=', $idFileArr[1]],
  902. ])->update([
  903. 'AFAL_USAC' => $usac,
  904. 'AFAL_USMO' => $idUser,
  905. 'AFAL_FEMO' => $nowStr,
  906. ]);
  907. $actions = DB::getQueryLog();
  908. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  909. $idac = $this->functionsController->registerActivity(
  910. $form['linea'],
  911. 'S002V01M04GDEL',
  912. 'S002V01F01ADDO',
  913. 'S002V01P01GEDO',
  914. 'Actualización',
  915. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó los usuarios con acceso del archivo $idFile.",
  916. $idUser,
  917. $nowStr
  918. );
  919. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  920. return $this->responseController->makeresponse(false, "EXITO");
  921. }
  922. public function deleteFile(Request $request) {
  923. DB::enableQueryLog();
  924. $validator = Validator::make($request->all(), [
  925. 'id_user' => 'required|string',
  926. 'id_file' => 'required|string',
  927. 'linea' => 'required|integer',
  928. ]);
  929. if($validator->fails()){
  930. return $this->responseController->makeResponse(
  931. true,
  932. "Se encontraron uno o más errores.",
  933. $this->responseController->makeErrors(
  934. $validator->errors()->messages()
  935. ),
  936. 401
  937. );
  938. }
  939. $form = $request->all();
  940. $idUser = $this->encryptionController->decrypt($form['id_user']);
  941. if(!$idUser){
  942. return $this->responseController->makeResponse(true, "El ID del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  943. }
  944. $usr = DB::table('S002V01TUSUA')->where([
  945. ['USUA_NULI', '=', $form['linea']],
  946. ['USUA_IDUS', '=', $idUser],
  947. ])->first();
  948. if(is_null($usr)){
  949. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  950. }
  951. $idFile = $this->encryptionController->decrypt($form['id_file']);
  952. if(!$idFile){
  953. return $this->responseController->makeResponse(true, "El ID del archivo solicitado no fue encriptado correctamente.", [], 400);
  954. }
  955. $idFileArr = explode('=', $idFile);
  956. $codeArr = explode('-', $idFileArr[0]);
  957. $file = DB::table('S002V01TAFAL')->where([
  958. ['AFAL_NULI', '=', $codeArr[0]],
  959. ['AFAL_COMO', '=', $codeArr[1]],
  960. ['AFAL_CLDO', '=', $codeArr[2]],
  961. ['AFAL_FECR', '=', $codeArr[3]],
  962. ['AFAL_NUSE', '=', $codeArr[4]],
  963. ['AFAL_NUVE', '=', $idFileArr[1]],
  964. ])->first();
  965. if(is_null($file)){
  966. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  967. }else if($file->AFAL_ESTA == 'Eliminado'){
  968. return $this->responseController->makeResponse(true, 'El archivo solicitado está eliminado.', [], 401);
  969. }
  970. $now = $this->functionsController->now();
  971. $nowStr = $now->toDateTimeString();
  972. DB::table('S002V01TAFAL')->where([
  973. ['AFAL_NULI', '=', $codeArr[0]],
  974. ['AFAL_COMO', '=', $codeArr[1]],
  975. ['AFAL_CLDO', '=', $codeArr[2]],
  976. ['AFAL_FECR', '=', $codeArr[3]],
  977. ['AFAL_NUSE', '=', $codeArr[4]],
  978. ['AFAL_NUVE', '=', $idFileArr[1]],
  979. ])->update([
  980. 'AFAL_ESTA' => 'Eliminado',
  981. 'AFAL_USMO' => $idUser,
  982. 'AFAL_FEMO' => $nowStr
  983. ]);
  984. $actions = DB::getQueryLog();
  985. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  986. $idac = $this->functionsController->registerActivity(
  987. $form['linea'],
  988. 'S002V01M04GDEL',
  989. 'S002V01F01ADDO',
  990. 'S002V01P01GEDO',
  991. 'Eliminación',
  992. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó el archivo $idFile.",
  993. $idUser,
  994. $nowStr
  995. );
  996. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  997. return $this->responseController->makeresponse(false, "EXITO");
  998. }
  999. public function getAssociatedWorkOrders($idFile, $idUser, $line) {
  1000. DB::enableQueryLog();
  1001. $idUser = $this->encryptionController->decrypt($idUser);
  1002. if(!$idUser){
  1003. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  1004. }
  1005. $usr = DB::table('S002V01TUSUA')->where([
  1006. ['USUA_NULI', '=', $line],
  1007. ['USUA_IDUS', '=', $idUser]
  1008. ])->first();
  1009. if(is_null($usr)){
  1010. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  1011. }
  1012. $idFile = $this->encryptionController->decrypt($idFile);
  1013. if(!$idFile){
  1014. return $this->responseController->makeResponse(true, 'El ID del archivo solicitado no está encriptado correctamente.', [], 400);
  1015. }
  1016. $idFileArr = explode('=', $idFile);
  1017. $codeArr = explode('-', $idFileArr[0]);
  1018. $file = DB::table('S002V01TAFAL')->where([
  1019. ['AFAL_NULI', '=', $line],
  1020. ['AFAL_COMO', '=', $codeArr[1]],
  1021. ['AFAL_CLDO', '=', $codeArr[2]],
  1022. ['AFAL_FECR', '=', $codeArr[3]],
  1023. ['AFAL_NUSE', '=', $codeArr[4]],
  1024. ['AFAL_NUVE', '=', $idFileArr[1]],
  1025. ])->first();
  1026. if(is_null($file)){
  1027. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  1028. }
  1029. $orders = DB::table('S002V01TOTPR')
  1030. ->select([
  1031. 'OTPR_IDOT AS IDORDEN',
  1032. 'OTPR_EQIN AS EQUIPAMIENTO',
  1033. 'OTPR_ACAS AS ACTIVADOR',
  1034. 'OTPR_CLAS AS CLASIFICACION',
  1035. 'ACTI_PRIO AS PRIORIDAD',
  1036. 'ACTI_TIAC AS TIPOACTIVADOR'
  1037. ])->join('S002V01TACTI', 'OTPR_ACAS', '=', 'ACTI_IDAC')
  1038. ->where('OTPR_NULI', '=', $line)
  1039. ->whereJsonContains('OTPR_DONE', $idFile)->get()->all();
  1040. $now = $this->functionsController->now();
  1041. $nowStr = $now->toDateTimeString();
  1042. $actions = DB::getQueryLog();
  1043. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1044. $idac = $this->functionsController->registerActivity(
  1045. $line,
  1046. 'S002V01M04GDEL',
  1047. 'S002V01F01ADDO',
  1048. 'S002V01P01GEDO',
  1049. 'Consulta',
  1050. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las órdenes de trabajo relacionadas al archivo $idFile.",
  1051. $idUser,
  1052. $nowStr
  1053. );
  1054. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1055. return $this->responseController->makeresponse(false, "EXITO", $orders);
  1056. }
  1057. public function changeAssociationStatus(Request $request) {
  1058. DB::enableQueryLog();
  1059. $validator = Validator::make($request->all(), [
  1060. 'id_user' => 'required|string',
  1061. 'linea' => 'required|integer',
  1062. 'id_file' => 'required|string',
  1063. 'action' => 'required|string|in:disassociate,associate',
  1064. 'id_order' => 'required|string',
  1065. ]);
  1066. if($validator->fails()){
  1067. return $this->responseController->makeResponse(
  1068. true,
  1069. "Se encontraron uno o más errores.",
  1070. $this->responseController->makeErrors(
  1071. $validator->errors()->messages()
  1072. ),
  1073. 401
  1074. );
  1075. }
  1076. $form = $request->all();
  1077. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1078. if(!$idUser){
  1079. return $this->responseController->makeResponse(true, "El ID del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  1080. }
  1081. $usr = DB::table('S002V01TUSUA')->where([
  1082. ['USUA_NULI', '=', $form['linea']],
  1083. ['USUA_IDUS', '=', $idUser],
  1084. ])->first();
  1085. if(is_null($usr)){
  1086. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1087. }
  1088. $idFile = $this->encryptionController->decrypt($form['id_file']);
  1089. if(!$idFile){
  1090. return $this->responseController->makeResponse(true, "El ID del archivo solicitado no fue encriptado correctamente.", [], 400);
  1091. }
  1092. $idFileArr = explode('=', $idFile);
  1093. $codeArr = explode('-', $idFileArr[0]);
  1094. $file = DB::table('S002V01TAFAL')->where([
  1095. ['AFAL_NULI', '=', $codeArr[0]],
  1096. ['AFAL_COMO', '=', $codeArr[1]],
  1097. ['AFAL_CLDO', '=', $codeArr[2]],
  1098. ['AFAL_FECR', '=', $codeArr[3]],
  1099. ['AFAL_NUSE', '=', $codeArr[4]],
  1100. ['AFAL_NUVE', '=', $idFileArr[1]],
  1101. ])->first();
  1102. if(is_null($file)){
  1103. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  1104. }else if($file->AFAL_ESTA == 'Eliminado'){
  1105. return $this->responseController->makeResponse(true, 'El archivo solicitado está eliminado.', [], 401);
  1106. }
  1107. $idOrder = $this->encryptionController->decrypt($form['id_order']);
  1108. if(!$idOrder){
  1109. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no fue encriptado correctamente.', [], 400);
  1110. }
  1111. $workOrder = DB::table('S002V01TOTPR')->where([
  1112. ['OTPR_NULI', '=', $form['linea']],
  1113. ['OTPR_IDOT', '=', $idOrder]
  1114. ])->first();
  1115. if(is_null($workOrder)){
  1116. return $this->responseController->makeResponse(true, 'La orden solicitada no existe', [], 404);
  1117. }else if($workOrder->OTPR_ESTA == 'E'){
  1118. return $this->responseController->makeResponse(true, 'La orden solicitada está eliminada', [], 401);
  1119. }
  1120. $filesArr = json_decode($workOrder->OTPR_DONE, true);
  1121. if($form['action'] == 'disassociate'){
  1122. if(!in_array($idFile, $filesArr)){
  1123. return $this->responseController->makeResponse(true, 'El archivo enviado no está asociado a la orden de trabajo.', [], 401);
  1124. }
  1125. $filesArrAux = [];
  1126. foreach($filesArr as $fileStr){
  1127. if($fileStr != $idFile){
  1128. $filesArrAux[] = $fileStr;
  1129. }
  1130. }
  1131. $filesArr = $filesArrAux;
  1132. }else{
  1133. if(in_array($idFile, $filesArr)){
  1134. return $this->responseController->makeResponse(true, 'El archivo enviado ya está asociado a la orden de trabajo.', [], 401);
  1135. }
  1136. $filesArr[] = $idFile;
  1137. }
  1138. $now = $this->functionsController->now();
  1139. $nowStr = $now->toDateTimeString();
  1140. $filesStr = json_encode($filesArr);
  1141. DB::table('S002V01TOTPR')->where([
  1142. ['OTPR_NULI', '=', $form['linea']],
  1143. ['OTPR_IDOT', '=', $idOrder]
  1144. ])->update([
  1145. 'OTPR_DONE' => $filesStr,
  1146. 'OTPR_USMO' => $idUser,
  1147. 'OTPR_FEMO' => $nowStr,
  1148. ]);
  1149. $actions = DB::getQueryLog();
  1150. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1151. $idac = $this->functionsController->registerActivity(
  1152. $form['linea'],
  1153. 'S002V01M04GDEL',
  1154. 'S002V01F01ADDO',
  1155. 'S002V01P01GEDO',
  1156. 'Actualización',
  1157. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó la relación del archivo $idFile y la orden de trabajo #$idOrder.",
  1158. $idUser,
  1159. $nowStr
  1160. );
  1161. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1162. return $this->responseController->makeresponse(false, "EXITO");
  1163. }
  1164. public function privateGetPublicDocumentURL($id, $idUser, $line) {
  1165. $idUser = $this->encryptionController->decrypt($idUser);
  1166. if(!$idUser){
  1167. return ['error' => true, 'msg' => 'El ID del usuario que realizó la petición no está encriptado correctamente.', 'response' => []];
  1168. }
  1169. $usr = DB::table('S002V01TUSUA')->where([
  1170. ['USUA_NULI', '=', $line],
  1171. ['USUA_IDUS', '=', $idUser],
  1172. ])->first();
  1173. if(is_null($usr)){
  1174. return ['error' => true, 'msg' => 'El usuario que realizó la consulta no está registrado.', 'response' => []];
  1175. }
  1176. $id = $this->encryptionController->decrypt($id);
  1177. if(!$id){
  1178. return ['error' => true, 'msg' => 'El ID del archivo solicitado no está encriptado correctamente.', 'response' => []];
  1179. }
  1180. $idArr = explode('=', $id);
  1181. $codeArr = explode('-', $idArr[0]);
  1182. $file = DB::table('S002V01TAFAL')->where([
  1183. ['AFAL_NULI', '=', $codeArr[0]],
  1184. ['AFAL_COMO', '=', $codeArr[1]],
  1185. ['AFAL_CLDO', '=', $codeArr[2]],
  1186. ['AFAL_FECR', '=', $codeArr[3]],
  1187. ['AFAL_NUSE', '=', $codeArr[4]],
  1188. ['AFAL_NUVE', '=', $idArr[1]],
  1189. ])->first();
  1190. if(is_null($file)){
  1191. return ['error' => true, 'msg' => 'El archivo solicitado no está registrado.', 'response' => []];
  1192. }
  1193. $ubicArr = explode('storage', $file->AFAL_UBIC);
  1194. $publicUbi = "$ubicArr[0]public_files\\$id";
  1195. if(!file_exists($publicUbi)){
  1196. copy($file->AFAL_UBIC, $publicUbi);
  1197. }
  1198. $apiURI = $this->functionsController->getApiURI();
  1199. $publicUbiStr = str_replace('C:\inetpub\wwwroot\\', $apiURI, $publicUbi);
  1200. $publicUbiStr = str_replace('\\', '/', $publicUbiStr);
  1201. return ['error' => false, 'msg' => "EXITO", 'response' => ['public_uri' => $publicUbiStr]];
  1202. }
  1203. public function getFileByCode ($code, $user, $line) {
  1204. $response = array('error' => false, 'msg' => 'ÉXITO', 'response' => []);
  1205. $code = $this->encryptionController->decrypt($code);
  1206. if (is_null($code)) {
  1207. $response['error'] = true;
  1208. $response['msg'] = 'El código del archivo no está encriptado correctamente.';
  1209. return $response;
  1210. }
  1211. $idArr = explode('=', $code);
  1212. $codeArr = explode('-', $idArr[0]);
  1213. $file = DB::table('S002V01TAFAL')->where([
  1214. ['AFAL_NULI', '=', $codeArr[0]],
  1215. ['AFAL_COMO', '=', $codeArr[1]],
  1216. ['AFAL_CLDO', '=', $codeArr[2]],
  1217. ['AFAL_FECR', '=', $codeArr[3]],
  1218. ['AFAL_NUSE', '=', $codeArr[4]],
  1219. ['AFAL_NUVE', '=', $idArr[1]],
  1220. ])->first();
  1221. if(is_null($file)){
  1222. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  1223. }
  1224. $ubicArr = explode('storage', $file->AFAL_UBIC);
  1225. $publicUbi = "$ubicArr[0]public_files\\$code";
  1226. // $publicUbi = "$ubicArr[0]public\\public_files\\$code";
  1227. if(!file_exists($publicUbi)){
  1228. copy($file->AFAL_UBIC, $publicUbi);
  1229. }
  1230. $apiURI = $this->functionsController->getApiURI();
  1231. $publicUbiStr = str_replace('C:\inetpub\wwwroot\\', $apiURI, $publicUbi);
  1232. $publicUbiStr = str_replace('\\', '/', $publicUbiStr);
  1233. $arrResponse = [
  1234. 'ID' => $code,
  1235. 'NAME' => "{$file->AFAL_NOAR}.{$file->AFAL_EXTE}",
  1236. 'SIZE' => $this->resourcesController->formatBytes($file->AFAL_TAMA),
  1237. 'SHOW' => $publicUbiStr,
  1238. 'TYPE' => 'O',
  1239. ];
  1240. $response['response'] = $arrResponse;
  1241. return $response;
  1242. }
  1243. }