DocumentManagementController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. class DocumentManagementController extends Controller
  9. {
  10. private $responseController;
  11. private $encryptionController;
  12. private $functionsController;
  13. public function __construct()
  14. {
  15. $this->responseController = new ResponseController();
  16. $this->encryptionController = new EncryptionController();
  17. $this->functionsController = new FunctionsController();
  18. }
  19. public function downloadFile($token, $idUser, $line)
  20. {
  21. $tokenInfo = DB::table('S002V01TTODE')->where([
  22. ['TODE_NULI', '=', $line],
  23. ['TODE_TOKE', '=', $token]
  24. ])->first();
  25. if (is_null($tokenInfo)) {
  26. return $this->responseController->makeResponse(true, 'El token de descarga no existe.', [], 404);
  27. } else if ($tokenInfo->TODE_ESTA != 'Activo') {
  28. return $this->responseController->makeResponse(true, 'El token de descarga ya fue utilizado.', [], 401);
  29. }
  30. $token = $this->encryptionController->shortDec($token);
  31. if (!$token) {
  32. return $this->responseController->makeResponse(true, 'El token de descarga no está encriptado correctamente.', [], 400);
  33. }
  34. $tokenArr = explode("|", $token);
  35. if (count($tokenArr) != 3) {
  36. return $this->responseController->makeResponse(true, 'Estructura de token inválida.', [], 401);
  37. } else if (intval($tokenArr[1]) != $tokenInfo->TODE_LLAL) {
  38. return $this->responseController->makeResponse(true, 'Token inválido.', [], 401);
  39. } else if ($tokenArr[2] != 'syp') {
  40. return $this->responseController->makeResponse(true, 'Token inválido.', [], 401);
  41. }
  42. $idUser = $this->encryptionController->shortDec($idUser);
  43. if (!$idUser) {
  44. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  45. }
  46. $usr = DB::table('S002V01TUSUA')->where([
  47. ['USUA_IDUS', '=', $idUser],
  48. ['USUA_NULI', '=', $line]
  49. ])->first();
  50. if (is_null($usr)) {
  51. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  52. }
  53. $id = $tokenArr[0];
  54. $codiArr = explode("=", $id);
  55. $prefArr = explode("-", $codiArr[0]);
  56. $suffArr = explode(".", $codiArr[2]);
  57. $nuli = intval($prefArr[0]);
  58. $como = $prefArr[1];
  59. $cldo = $prefArr[2];
  60. $fecr = $prefArr[3];
  61. $nuse = intval($prefArr[4]);
  62. $nuve = $codiArr[1];
  63. $noar = $suffArr[0];
  64. $exte = $suffArr[1];
  65. $nowStr = Carbon::now('America/Mexico_city')->toDateTimeString();
  66. DB::table('S002V01TTODE')->where([
  67. ['TODE_IDTO', '=', $tokenInfo->TODE_IDTO],
  68. ['TODE_NULI', '=', $line]
  69. ])->update([
  70. 'TODE_ESTA' => 'Usado',
  71. 'TODE_USDE' => $idUser,
  72. 'TODE_FEDE' => $nowStr
  73. ]);
  74. $file = DB::table('S002V01TAFAL')->where([
  75. ['AFAL_NULI', '=', $nuli],
  76. ['AFAL_COMO', '=', $como],
  77. ['AFAL_CLDO', '=', $cldo],
  78. ['AFAL_FECR', '=', $fecr],
  79. ['AFAL_NUSE', '=', $nuse],
  80. ['AFAL_NUVE', '=', $nuve],
  81. ['AFAL_NOAR', '=', $noar],
  82. ['AFAL_EXTE', '=', $exte],
  83. ])->first();
  84. $aux = DB::getQueryLog();
  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);
  89. }
  90. public function getDownloadToken($idFile, $idUser, $line)
  91. {
  92. DB::enableQueryLog();
  93. $idFile = $this->encryptionController->shortDec($idFile);
  94. if (!$idFile) {
  95. return $this->responseController->makeResponse(true, 'El ID del archivo requerido no está encriptado correctamente.', [], 400);
  96. }
  97. $idUser = $this->encryptionController->shortDec($idUser);
  98. if (!$idUser) {
  99. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  100. }
  101. $usr = DB::table('S002V01TUSUA')->where([
  102. ['USUA_IDUS', '=', $idUser],
  103. ['USUA_NULI', '=', $line]
  104. ])->first();
  105. if (is_null($usr)) {
  106. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  107. }
  108. $codiArr = explode("=", $idFile);
  109. $prefArr = explode("-", $codiArr[0]);
  110. $suffArr = explode(".", $codiArr[2]);
  111. $nuli = intval($prefArr[0]);
  112. $como = $prefArr[1];
  113. $cldo = $prefArr[2];
  114. $fecr = $prefArr[3];
  115. $nuse = intval($prefArr[4]);
  116. $nuve = $codiArr[1];
  117. $noar = $suffArr[0];
  118. $exte = $suffArr[1];
  119. $file = DB::table('S002V01TAFAL')->where([
  120. ['AFAL_NULI', '=', $nuli],
  121. ['AFAL_COMO', '=', $como],
  122. ['AFAL_CLDO', '=', $cldo],
  123. ['AFAL_FECR', '=', $fecr],
  124. ['AFAL_NUSE', '=', $nuse],
  125. ['AFAL_NUVE', '=', $nuve],
  126. ['AFAL_NOAR', '=', $noar],
  127. ['AFAL_EXTE', '=', $exte],
  128. ])->first();
  129. $aux = DB::getQueryLog();
  130. if (is_null($file)) {
  131. return $this->responseController->makeResponse(true, 'El archivo solicitado no existe.', [], 404);
  132. }
  133. $rand = rand(0, 99999);
  134. $tokenArr = [$idFile, $rand, "syp"];
  135. $tokenStr = join('|', $tokenArr);
  136. $token = $this->encryptionController->shortEnc($tokenStr);
  137. $token = str_replace("+", "=P=", $token);
  138. $token = str_replace("/", "=S=", $token);
  139. $nowStr = Carbon::now('America/Mexico_city')->toDateTimeString();
  140. DB::table('S002V01TTODE')->insert([
  141. 'TODE_NULI' => $line,
  142. 'TODE_TOKE' => $token,
  143. 'TODE_CODO' => $idFile,
  144. 'TODE_LLAL' => $rand,
  145. 'TODE_USRE' => $idUser,
  146. 'TODE_FERE' => $nowStr
  147. ]);
  148. return $this->responseController->makeResponse(false, 'EXITO', ['TOKEN' => $token]);
  149. }
  150. public function getFileInfo($id, $idUser, $line)
  151. {
  152. DB::enableQueryLog();
  153. $id = $this->encryptionController->shortDec($id);
  154. if (!$id) {
  155. return $this->responseController->makeResponse(true, 'El ID del archivo requerido no está encriptado correctamente.', [], 400);
  156. }
  157. $idUser = $this->encryptionController->shortDec($idUser);
  158. if (!$idUser) {
  159. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  160. }
  161. $usr = DB::table('S002V01TUSUA')->where([
  162. ['USUA_IDUS', '=', $idUser],
  163. ['USUA_NULI', '=', $line]
  164. ])->first();
  165. if (is_null($usr)) {
  166. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  167. }
  168. $idArr = explode("=", $id);
  169. $codiArr = explode("-", $idArr[0]);
  170. $nuli = $line;
  171. $como = $codiArr[1];
  172. $cldo = $codiArr[2];
  173. $fecr = $codiArr[3];
  174. $nuse = intval($codiArr[4]);
  175. $nuve = intval($idArr[1]);
  176. $fileInfo = DB::table('S002V01TAFAL')->select([
  177. 'AFAL_NOAR AS NOMBREARCHIVO',
  178. 'AFAL_EXTE AS EXTENSION',
  179. 'AFAL_TAMA AS PESO',
  180. 'AFAL_USAC AS ACCESO',
  181. 'AFAL_ESTA AS ESTADO'
  182. ])->where([
  183. ['AFAL_NULI', '=', $nuli],
  184. ['AFAL_COMO', '=', $como],
  185. ['AFAL_CLDO', '=', $cldo],
  186. ['AFAL_FECR', '=', $fecr],
  187. ['AFAL_NUSE', '=', $nuse],
  188. ['AFAL_NUVE', '=', $nuve],
  189. ])->first();
  190. if (is_null($fileInfo)) {
  191. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  192. } else if ($fileInfo->ESTADO != 'Activo') {
  193. return $this->responseController->makeResponse(true, 'El archivo solicitado no está disponible.', [], 404);
  194. }
  195. return $this->responseController->makeResponse(false, 'EXITO', $fileInfo);
  196. }
  197. public function getDocuments($idUser, $line)
  198. {
  199. DB::enableQueryLog();
  200. $idUser = $this->encryptionController->shortDec($idUser);
  201. if (!$idUser) {
  202. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  203. }
  204. $documents = DB::table('s002v01tafal')->select(
  205. 'AFAL_NOAR as NOMBRE_ARC',
  206. 'AFAL_EXTE as EXTENSION',
  207. 'AFAL_TAMA as TAMANIO',
  208. 'AFAL_NUVE as VERSION',
  209. 'AFAL_FECR as FECHA_REGISTRO',
  210. 'AFAL_USRE as PROPIETARIO',
  211. 'AFAL_UBIC as URL',
  212. 'AFAL_USAC AS ACCESO',
  213. 'AFAL_ESTA AS ESTADO',
  214. 'AFAL_NULI AS NUMERO_LINEA',
  215. 'AFAL_CLDO AS CLASIFICACION',
  216. 'AFAL_COMO AS MODULO',
  217. 'AFAL_NUSE AS SECUENCIAL'
  218. )->where('AFAL_USRE', $idUser)->get()->all();
  219. $nowStr = Carbon::now('America/Mexico_city')->toDateTimeString();
  220. $actions = DB::getQueryLog();
  221. //$this->functionsController->registerActivity($actions, $idUser, $nowStr, $line);
  222. return $this->responseController->makeresponse(false, "EXITO", $documents);
  223. }
  224. public function getInfoDocument($idUser, $line, $idDocument)
  225. {
  226. DB::enableQueryLog();
  227. $idUser = $this->encryptionController->shortDec($idUser);
  228. if (!$idUser) {
  229. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  230. }
  231. $idDocument = $this->encryptionController->shortDec($idDocument);
  232. if (!$idDocument) {
  233. return $this->responseController->makeResponse(true, 'El ID del documento no está encriptado correctamente', [], 400);
  234. }
  235. $document = DB::table('s002v01tafal')->where('AFAL_USRE', $idUser)->where('AFAL_ID', $idDocument)->get();
  236. $nowStr = Carbon::now('America/Mexico_city')->toDateTimeString();
  237. $actions = DB::getQueryLog();
  238. //$this->functionsController->registerActivity($actions, $idUser, $nowStr, $line);
  239. return $this->responseController->makeresponse(false, "EXITO", $document);
  240. }
  241. public function moveFinalFile(Request $request)
  242. {
  243. DB::enableQueryLog();
  244. $validator = Validator::make($request->all(), [
  245. 'id_user' => 'required|string',
  246. 'linea' => 'required|integer',
  247. 'audiencia' => 'required|json',
  248. 'archivos' => 'required|json',
  249. 'cldo' => 'required|string',
  250. ''
  251. ]);
  252. if($validator->fails()){
  253. return $this->responseController->makeResponse(
  254. true,
  255. "Se encontraron uno o más errores.",
  256. $this->responseController->makeErrors(
  257. $validator->errors()->messages()
  258. ),
  259. 401
  260. );
  261. }
  262. $advice = $request->all();
  263. $idUser = $this->encryptionController->decrypt($advice['id_user']);
  264. if(!$idUser){
  265. return $this->responseController->makeResponse(true, "El ID del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  266. }
  267. $usr = DB::table('S002V01TUSUA')->where([
  268. ['USUA_IDUS', '=', $idUser],
  269. ['USUA_NULI', '=', $advice['linea']]
  270. ])->first();
  271. if(is_null($usr)){
  272. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  273. }
  274. $filesArr = json_decode($advice['archivos'], true);
  275. $nowStr = Carbon::now('America/Mexico_city')->toDateTimeString();
  276. $attachedArr = [];
  277. foreach($filesArr as $file){
  278. $idFile = $file['id'];
  279. $idFile = $this->encryptionController->decrypt($idFile);
  280. if(!$idFile){
  281. return $this->responseController->makeResponse(true, "El ID del archivo $file[name] no fue encriptado correctamente", [], 400);
  282. }
  283. $fileObj = DB::table('S002V01TARTE')->where([
  284. ['ARTE_IDAR', '=', $idFile],
  285. ['ARTE_NULI', '=', $advice['linea']]
  286. ])->first();
  287. if(is_null($fileObj)){
  288. return $this->responseController->makeResponse(true, "El ID del archivo $file[name] no existe", [], 404);
  289. }else if($fileObj->ARTE_ESTA == 'Eliminado'){
  290. return $this->responseController->makeResponse(true, "El archivo $file[name] está eliminado", [], 404);
  291. }
  292. $fileSaved = $this->moveFinalFile($advice['linea'], 'AV', $fileObj, 'S002V01M01ADSI', 'S002V01F03CRAV', 'S002V01P02CNAV');
  293. if(!$fileSaved[0]){
  294. return $this->responseController->makeResponse(true, "El archivo no pudo guardarse: $fileSaved[1]", [], 500);
  295. }
  296. $attachedArr[] = $this->encryptionController->encrypt($fileSaved[1]);
  297. DB::table('S002V01TARTE')->where([
  298. ['ARTE_IDAR', '=', $idFile],
  299. ['ARTE_NULI', '=', $advice['linea']],
  300. ])->update([
  301. 'ARTE_ESTA' => 'Eliminado',
  302. 'ARTE_USMO' => $idUser,
  303. 'ARTE_FEMO' => $nowStr
  304. ]);
  305. }
  306. $ubiTempFile = $file->ARTE_UBTE;
  307. $ubiFileArr = explode('tempFiles', $ubiTempFile);
  308. $ubic = $ubiFileArr[0] . 'files' . $ubiFileArr[1];
  309. $como = substr($module, -4);
  310. $date = Carbon::now('America/Mexico_city')->toDateTimeString();
  311. $dateArr = explode(' ', $date);
  312. $dateStr = $dateArr[0];
  313. $dA = explode('-', $dateStr);
  314. $dA[0] = substr($dA[0], -2);
  315. $fecr = implode('', $dA);
  316. $nameStr = $file->ARTE_NOAR;
  317. $nameArr = explode('.', $nameStr);
  318. $nuve = 0;
  319. $nuse = 0;
  320. $lastSec = DB::table('S002V01TAFAL')->where([
  321. ['AFAL_NULI', '=', $line],
  322. ['AFAL_COMO', '=', $como],
  323. ['AFAL_CLDO', '=', $cldo]
  324. ])->orderBy('AFAL_NUSE', 'desc')->first();
  325. if (is_null($lastSec)) {
  326. $nuse = 1;
  327. } else {
  328. $nuse = $lastSec->AFAL_NUSE + 1;
  329. }
  330. $lastVersion = DB::table('S002V01TAFAL')->where([
  331. ['AFAL_NULI', '=', $line],
  332. ['AFAL_NOAR', '=', $nameArr[0]],
  333. ['AFAL_EXTE', '=', $nameArr[1]],
  334. ])->orderBy('AFAL_NUVE', 'desc')->first();
  335. if (is_null($lastVersion)) {
  336. $nuve = 1;
  337. } else {
  338. $nuve = $lastVersion->AFAL_NUVE + 1;
  339. }
  340. if ($nuve > 99) return [false, 'El archivo llegó al número máximo de versiones'];
  341. if ($nuse > 999999) return [false, 'El archivo llegó al número de secuencia máximo'];
  342. $noar = $nameArr[0];
  343. $exte = $nameArr[1];
  344. if (strlen($noar) > 100) return [false, 'El nombre del archivo tiene una longitud mayor a 100 caracteres'];
  345. $tama = $file->ARTE_TAMA;
  346. $usac = json_encode([$file->ARTE_USRE]);
  347. $usre = $file->ARTE_USRE;
  348. $fere = $file->ARTE_FERE;
  349. DB::table('S002V01TAFAL')->insert([
  350. 'AFAL_NULI' => $line,
  351. 'AFAL_COMO' => $como,
  352. 'AFAL_CLDO' => $cldo,
  353. 'AFAL_FECR' => $fecr,
  354. 'AFAL_NUSE' => $nuse,
  355. 'AFAL_NUVE' => $nuve,
  356. 'AFAL_NOAR' => $noar,
  357. 'AFAL_EXTE' => $exte,
  358. 'AFAL_TAMA' => $tama,
  359. 'AFAL_UBIC' => $ubic,
  360. 'AFAL_USAC' => $usac,
  361. 'AFAL_USRE' => $usre,
  362. 'AFAL_FERE' => $fere
  363. ]);
  364. if (file_exists($file->ARTE_UBTE)) {
  365. rename($file->ARTE_UBTE, $ubic);
  366. }
  367. $codigo = "";
  368. if (strlen($line) < 2) {
  369. $codigo .= "0$line-";
  370. } else {
  371. $codigo .= "$line-";
  372. }
  373. $codigo .= "$como-$cldo-$fecr-";
  374. switch (strlen($nuse)) {
  375. case 1:
  376. $codigo .= "00000$nuse";
  377. break;
  378. case 2:
  379. $codigo .= "0000$nuse";
  380. break;
  381. case 3:
  382. $codigo .= "000$nuse";
  383. break;
  384. case 4:
  385. $codigo .= "00$nuse";
  386. break;
  387. case 5:
  388. $codigo .= "0$nuse";
  389. break;
  390. default:
  391. $codigo .= "$nuse";
  392. break;
  393. }
  394. if (strlen($nuve) < 2) {
  395. $codigo .= "=0$nuve=";
  396. } else {
  397. $codigo .= "=$nuve=";
  398. }
  399. $codigo .= "$noar.$exte";
  400. return [true, $codigo];
  401. }
  402. }