DocumentManagementController.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  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->shortDec($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->shortDec($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->shortDec($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->shortDec($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->shortEnc($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->shortDec($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->shortDec($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->shortDec($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 && $extStr === 'xml') {
  376. $isValid = $this->functionsController->checkInvoice($request->file('file')->getContent());
  377. }
  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->shortDec($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->shortDec($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\\public_files\\$id";
  695. if(!file_exists($publicUbi)){
  696. copy($file->AFAL_UBIC, $publicUbi);
  697. }
  698. // $publicUbiStr = str_replace('C:\inetpub\wwwroot\\', 'http://git.ittec.mx/', $publicUbi);
  699. $publicUbiStr = str_replace('C:\ITTEC\SAM\Dev\SistemaMantenimiento\sistema-mantenimiento-back\public', 'http://192.168.100.105:8000', $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 getFileAccess($id, $idUser, $line) {
  719. DB::enableQueryLog();
  720. $idUser = $this->encryptionController->shortDec($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->shortDec($id);
  732. if(!$id){
  733. return $this->responseController->makeResponse(true, 'El ID del archivo solicitado no está encriptado correctamente.', [], 400);
  734. }
  735. $idArr = explode('=', $id);
  736. $codeArr = explode('-', $idArr[0]);
  737. $file = DB::table('S002V01TAFAL')->where([
  738. ['AFAL_NULI', '=', $codeArr[0]],
  739. ['AFAL_COMO', '=', $codeArr[1]],
  740. ['AFAL_CLDO', '=', $codeArr[2]],
  741. ['AFAL_FECR', '=', $codeArr[3]],
  742. ['AFAL_NUSE', '=', $codeArr[4]],
  743. ['AFAL_NUVE', '=', $idArr[1]],
  744. ])->first();
  745. if(is_null($file)){
  746. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  747. }
  748. $owner = $this->encryptionController->encrypt($file->AFAL_USRE);
  749. $permissionsArr = json_decode($file->AFAL_USAC, true);
  750. foreach($permissionsArr as $k=>$v){
  751. $idEnc = $this->encryptionController->encrypt($v);
  752. $permissionsArr[$k] = $idEnc;
  753. }
  754. $now = $this->functionsController->now();
  755. $nowStr = $now->toDateTimeString();
  756. $actions = DB::getQueryLog();
  757. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  758. $idac = $this->functionsController->registerActivity(
  759. $line,
  760. 'S002V01M04GDEL',
  761. 'S002V01F01ADDO',
  762. 'S002V01P01GEDO',
  763. 'Consulta',
  764. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los usuarios con acceso al archivo $id.",
  765. $idUser,
  766. $nowStr
  767. );
  768. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  769. return $this->responseController->makeresponse(false, "EXITO", [
  770. 'access' => $permissionsArr,
  771. 'owner' => $owner
  772. ]);
  773. }
  774. public function updateFilePermissions(Request $request) {
  775. DB::enableQueryLog();
  776. $validator = Validator::make($request->all(), [
  777. 'id_user' => 'required|string',
  778. 'id_file' => 'required|string',
  779. 'linea' => 'required|integer',
  780. 'permissions' => 'required|json',
  781. ]);
  782. if($validator->fails()){
  783. return $this->responseController->makeResponse(
  784. true,
  785. "Se encontraron uno o más errores.",
  786. $this->responseController->makeErrors(
  787. $validator->errors()->messages()
  788. ),
  789. 401
  790. );
  791. }
  792. $form = $request->all();
  793. $idUser = $this->encryptionController->decrypt($form['id_user']);
  794. if(!$idUser){
  795. return $this->responseController->makeResponse(true, "El ID del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  796. }
  797. $usr = DB::table('S002V01TUSUA')->where([
  798. ['USUA_NULI', '=', $form['linea']],
  799. ['USUA_IDUS', '=', $idUser],
  800. ])->first();
  801. if(is_null($usr)){
  802. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  803. }
  804. $idFile = $this->encryptionController->decrypt($form['id_file']);
  805. if(!$idFile){
  806. return $this->responseController->makeResponse(true, "El ID del archivo solicitado no fue encriptado correctamente.", [], 400);
  807. }
  808. $idFileArr = explode('=', $idFile);
  809. $codeArr = explode('-', $idFileArr[0]);
  810. $file = DB::table('S002V01TAFAL')->where([
  811. ['AFAL_NULI', '=', $codeArr[0]],
  812. ['AFAL_COMO', '=', $codeArr[1]],
  813. ['AFAL_CLDO', '=', $codeArr[2]],
  814. ['AFAL_FECR', '=', $codeArr[3]],
  815. ['AFAL_NUSE', '=', $codeArr[4]],
  816. ['AFAL_NUVE', '=', $idFileArr[1]],
  817. ])->first();
  818. if(is_null($file)){
  819. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  820. }
  821. $permissionsArr = json_decode($file->AFAL_USAC, true);
  822. if(!in_array($idUser, $permissionsArr)){
  823. $usrName = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA) . " ($idUser)";
  824. return $this->responseController->makeResponse(true, "El usuario $usrName no tiene permisos para modificar el archivo $idFile.", [], 401);
  825. }
  826. $newPermissionsArr = json_decode($form['permissions'], true);
  827. if(count($newPermissionsArr) <= 0){
  828. return $this->responseController->makeResponse(true, "El arreglo de permisos está vacío.", [], 400);
  829. }
  830. $newPermissionsArrDec = [];
  831. foreach($newPermissionsArr as $val){
  832. $idDec = $this->encryptionController->decrypt($val);
  833. if(!$idDec){
  834. return $this->responseController->makeResponse(true, "Alguno de los ID's enviados no fue encriptado correctamente.", [], 400);
  835. }
  836. $newPermissionsArrDec[] = $idDec;
  837. }
  838. if(!in_array($file->AFAL_USRE, $newPermissionsArrDec)){
  839. return $this->responseController->makeResponse(true, "El arreglo de permisos no incluye el ID del propietario.", [], 401);
  840. }
  841. $now = $this->functionsController->now();
  842. $nowStr = $now->toDateTimeString();
  843. $usac = json_encode($newPermissionsArrDec);
  844. DB::table('S002V01TAFAL')->where([
  845. ['AFAL_NULI', '=', $codeArr[0]],
  846. ['AFAL_COMO', '=', $codeArr[1]],
  847. ['AFAL_CLDO', '=', $codeArr[2]],
  848. ['AFAL_FECR', '=', $codeArr[3]],
  849. ['AFAL_NUSE', '=', $codeArr[4]],
  850. ['AFAL_NUVE', '=', $idFileArr[1]],
  851. ])->update([
  852. 'AFAL_USAC' => $usac,
  853. 'AFAL_USMO' => $idUser,
  854. 'AFAL_FEMO' => $nowStr,
  855. ]);
  856. $actions = DB::getQueryLog();
  857. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  858. $idac = $this->functionsController->registerActivity(
  859. $form['linea'],
  860. 'S002V01M04GDEL',
  861. 'S002V01F01ADDO',
  862. 'S002V01P01GEDO',
  863. 'Actualización',
  864. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó los usuarios con acceso del archivo $idFile.",
  865. $idUser,
  866. $nowStr
  867. );
  868. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  869. return $this->responseController->makeresponse(false, "EXITO");
  870. }
  871. public function deleteFile(Request $request) {
  872. DB::enableQueryLog();
  873. $validator = Validator::make($request->all(), [
  874. 'id_user' => 'required|string',
  875. 'id_file' => 'required|string',
  876. 'linea' => 'required|integer',
  877. ]);
  878. if($validator->fails()){
  879. return $this->responseController->makeResponse(
  880. true,
  881. "Se encontraron uno o más errores.",
  882. $this->responseController->makeErrors(
  883. $validator->errors()->messages()
  884. ),
  885. 401
  886. );
  887. }
  888. $form = $request->all();
  889. $idUser = $this->encryptionController->decrypt($form['id_user']);
  890. if(!$idUser){
  891. return $this->responseController->makeResponse(true, "El ID del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  892. }
  893. $usr = DB::table('S002V01TUSUA')->where([
  894. ['USUA_NULI', '=', $form['linea']],
  895. ['USUA_IDUS', '=', $idUser],
  896. ])->first();
  897. if(is_null($usr)){
  898. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  899. }
  900. $idFile = $this->encryptionController->decrypt($form['id_file']);
  901. if(!$idFile){
  902. return $this->responseController->makeResponse(true, "El ID del archivo solicitado no fue encriptado correctamente.", [], 400);
  903. }
  904. $idFileArr = explode('=', $idFile);
  905. $codeArr = explode('-', $idFileArr[0]);
  906. $file = DB::table('S002V01TAFAL')->where([
  907. ['AFAL_NULI', '=', $codeArr[0]],
  908. ['AFAL_COMO', '=', $codeArr[1]],
  909. ['AFAL_CLDO', '=', $codeArr[2]],
  910. ['AFAL_FECR', '=', $codeArr[3]],
  911. ['AFAL_NUSE', '=', $codeArr[4]],
  912. ['AFAL_NUVE', '=', $idFileArr[1]],
  913. ])->first();
  914. if(is_null($file)){
  915. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  916. }else if($file->AFAL_ESTA == 'Eliminado'){
  917. return $this->responseController->makeResponse(true, 'El archivo solicitado está eliminado.', [], 401);
  918. }
  919. $now = $this->functionsController->now();
  920. $nowStr = $now->toDateTimeString();
  921. DB::table('S002V01TAFAL')->where([
  922. ['AFAL_NULI', '=', $codeArr[0]],
  923. ['AFAL_COMO', '=', $codeArr[1]],
  924. ['AFAL_CLDO', '=', $codeArr[2]],
  925. ['AFAL_FECR', '=', $codeArr[3]],
  926. ['AFAL_NUSE', '=', $codeArr[4]],
  927. ['AFAL_NUVE', '=', $idFileArr[1]],
  928. ])->update([
  929. 'AFAL_ESTA' => 'Eliminado',
  930. 'AFAL_USMO' => $idUser,
  931. 'AFAL_FEMO' => $nowStr
  932. ]);
  933. $actions = DB::getQueryLog();
  934. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  935. $idac = $this->functionsController->registerActivity(
  936. $form['linea'],
  937. 'S002V01M04GDEL',
  938. 'S002V01F01ADDO',
  939. 'S002V01P01GEDO',
  940. 'Eliminación',
  941. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó el archivo $idFile.",
  942. $idUser,
  943. $nowStr
  944. );
  945. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  946. return $this->responseController->makeresponse(false, "EXITO");
  947. }
  948. public function getAssociatedWorkOrders($idFile, $idUser, $line) {
  949. DB::enableQueryLog();
  950. $idUser = $this->encryptionController->shortDec($idUser);
  951. if(!$idUser){
  952. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  953. }
  954. $usr = DB::table('S002V01TUSUA')->where([
  955. ['USUA_NULI', '=', $line],
  956. ['USUA_IDUS', '=', $idUser]
  957. ])->first();
  958. if(is_null($usr)){
  959. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  960. }
  961. $idFile = $this->encryptionController->shortDec($idFile);
  962. if(!$idFile){
  963. return $this->responseController->makeResponse(true, 'El ID del archivo solicitado no está encriptado correctamente.', [], 400);
  964. }
  965. $idFileArr = explode('=', $idFile);
  966. $codeArr = explode('-', $idFileArr[0]);
  967. $file = DB::table('S002V01TAFAL')->where([
  968. ['AFAL_NULI', '=', $line],
  969. ['AFAL_COMO', '=', $codeArr[1]],
  970. ['AFAL_CLDO', '=', $codeArr[2]],
  971. ['AFAL_FECR', '=', $codeArr[3]],
  972. ['AFAL_NUSE', '=', $codeArr[4]],
  973. ['AFAL_NUVE', '=', $idFileArr[1]],
  974. ])->first();
  975. if(is_null($file)){
  976. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  977. }
  978. $orders = DB::table('S002V01TOTPR')
  979. ->select([
  980. 'OTPR_IDOT AS IDORDEN',
  981. 'OTPR_EQIN AS EQUIPAMIENTO',
  982. 'OTPR_ACAS AS ACTIVADOR',
  983. 'OTPR_CLAS AS CLASIFICACION',
  984. 'ACTI_PRIO AS PRIORIDAD',
  985. 'ACTI_TIAC AS TIPOACTIVADOR'
  986. ])->join('S002V01TACTI', 'OTPR_ACAS', '=', 'ACTI_IDCO')
  987. ->where('OTPR_NULI', '=', $line)
  988. ->whereJsonContains('OTPR_DONE', $idFile)->get()->all();
  989. $now = $this->functionsController->now();
  990. $nowStr = $now->toDateTimeString();
  991. $actions = DB::getQueryLog();
  992. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  993. $idac = $this->functionsController->registerActivity(
  994. $line,
  995. 'S002V01M04GDEL',
  996. 'S002V01F01ADDO',
  997. 'S002V01P01GEDO',
  998. 'Consulta',
  999. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las órdenes de trabajo relacionadas al archivo $idFile.",
  1000. $idUser,
  1001. $nowStr
  1002. );
  1003. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1004. return $this->responseController->makeresponse(false, "EXITO", $orders);
  1005. }
  1006. public function changeAssociationStatus(Request $request) {
  1007. DB::enableQueryLog();
  1008. $validator = Validator::make($request->all(), [
  1009. 'id_user' => 'required|string',
  1010. 'linea' => 'required|integer',
  1011. 'id_file' => 'required|string',
  1012. 'action' => 'required|string|in:disassociate,associate',
  1013. 'id_order' => 'required|string',
  1014. ]);
  1015. if($validator->fails()){
  1016. return $this->responseController->makeResponse(
  1017. true,
  1018. "Se encontraron uno o más errores.",
  1019. $this->responseController->makeErrors(
  1020. $validator->errors()->messages()
  1021. ),
  1022. 401
  1023. );
  1024. }
  1025. $form = $request->all();
  1026. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1027. if(!$idUser){
  1028. return $this->responseController->makeResponse(true, "El ID del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  1029. }
  1030. $usr = DB::table('S002V01TUSUA')->where([
  1031. ['USUA_NULI', '=', $form['linea']],
  1032. ['USUA_IDUS', '=', $idUser],
  1033. ])->first();
  1034. if(is_null($usr)){
  1035. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1036. }
  1037. $idFile = $this->encryptionController->decrypt($form['id_file']);
  1038. if(!$idFile){
  1039. return $this->responseController->makeResponse(true, "El ID del archivo solicitado no fue encriptado correctamente.", [], 400);
  1040. }
  1041. $idFileArr = explode('=', $idFile);
  1042. $codeArr = explode('-', $idFileArr[0]);
  1043. $file = DB::table('S002V01TAFAL')->where([
  1044. ['AFAL_NULI', '=', $codeArr[0]],
  1045. ['AFAL_COMO', '=', $codeArr[1]],
  1046. ['AFAL_CLDO', '=', $codeArr[2]],
  1047. ['AFAL_FECR', '=', $codeArr[3]],
  1048. ['AFAL_NUSE', '=', $codeArr[4]],
  1049. ['AFAL_NUVE', '=', $idFileArr[1]],
  1050. ])->first();
  1051. if(is_null($file)){
  1052. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  1053. }else if($file->AFAL_ESTA == 'Eliminado'){
  1054. return $this->responseController->makeResponse(true, 'El archivo solicitado está eliminado.', [], 401);
  1055. }
  1056. $idOrder = $this->encryptionController->decrypt($form['id_order']);
  1057. if(!$idOrder){
  1058. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no fue encriptado correctamente.', [], 400);
  1059. }
  1060. $workOrder = DB::table('S002V01TOTPR')->where([
  1061. ['OTPR_NULI', '=', $form['linea']],
  1062. ['OTPR_IDOT', '=', $idOrder]
  1063. ])->first();
  1064. if(is_null($workOrder)){
  1065. return $this->responseController->makeResponse(true, 'La orden solicitada no existe', [], 404);
  1066. }else if($workOrder->OTPR_ESTA == 'E'){
  1067. return $this->responseController->makeResponse(true, 'La orden solicitada está eliminada', [], 401);
  1068. }
  1069. $filesArr = json_decode($workOrder->OTPR_DONE, true);
  1070. if($form['action'] == 'disassociate'){
  1071. if(!in_array($idFile, $filesArr)){
  1072. return $this->responseController->makeResponse(true, 'El archivo enviado no está asociado a la orden de trabajo.', [], 401);
  1073. }
  1074. $filesArrAux = [];
  1075. foreach($filesArr as $fileStr){
  1076. if($fileStr != $idFile){
  1077. $filesArrAux[] = $fileStr;
  1078. }
  1079. }
  1080. $filesArr = $filesArrAux;
  1081. }else{
  1082. if(in_array($idFile, $filesArr)){
  1083. return $this->responseController->makeResponse(true, 'El archivo enviado ya está asociado a la orden de trabajo.', [], 401);
  1084. }
  1085. $filesArr[] = $idFile;
  1086. }
  1087. $now = $this->functionsController->now();
  1088. $nowStr = $now->toDateTimeString();
  1089. $filesStr = json_encode($filesArr);
  1090. DB::table('S002V01TOTPR')->where([
  1091. ['OTPR_NULI', '=', $form['linea']],
  1092. ['OTPR_IDOT', '=', $idOrder]
  1093. ])->update([
  1094. 'OTPR_DONE' => $filesStr,
  1095. 'OTPR_USMO' => $idUser,
  1096. 'OTPR_FEMO' => $nowStr,
  1097. ]);
  1098. $actions = DB::getQueryLog();
  1099. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1100. $idac = $this->functionsController->registerActivity(
  1101. $form['linea'],
  1102. 'S002V01M04GDEL',
  1103. 'S002V01F01ADDO',
  1104. 'S002V01P01GEDO',
  1105. 'Actualización',
  1106. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó la relación del archivo $idFile y la orden de trabajo #$idOrder.",
  1107. $idUser,
  1108. $nowStr
  1109. );
  1110. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1111. return $this->responseController->makeresponse(false, "EXITO");
  1112. }
  1113. }