DocumentManagementController.php 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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 = 'http://192.168.100.105:8000/';
  1231. $apiURI = $this->functionsController->getApiURI();
  1232. $publicUbiStr = str_replace('C:\inetpub\wwwroot\\', $apiURI, $publicUbi);
  1233. // $publicUbiStr = str_replace('C:\ITTEC\SAM\Dev\SistemaMantenimiento\sistema-mantenimiento-back\public\\', $apiURI, $publicUbi);
  1234. $publicUbiStr = str_replace('\\', '/', $publicUbiStr);
  1235. $arrResponse = [
  1236. 'ID' => $code,
  1237. 'NAME' => "{$file->AFAL_NOAR}.{$file->AFAL_EXTE}",
  1238. 'SIZE' => $this->resourcesController->formatBytes($file->AFAL_TAMA),
  1239. 'SHOW' => $publicUbiStr,
  1240. 'TYPE' => 'O',
  1241. ];
  1242. $response['response'] = $arrResponse;
  1243. return $response;
  1244. }
  1245. }