DocumentManagementController.php 48 KB

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