EquipmentManagementController.php 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827
  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\Carbon;
  7. class EquipmentManagementController extends Controller{
  8. private $responseController;
  9. private $encryptionController;
  10. private $functionsController;
  11. private $templatesUbic;
  12. private $documentManagementController;
  13. public function __construct(){
  14. $this->responseController = new ResponseController();
  15. $this->encryptionController = new EncryptionController();
  16. $this->functionsController = new FunctionsController();
  17. $this->templatesUbic = "C:\inetpub\wwwroot\sam\storage\app\public\pdf_templates\\01_05_GEEQ\\";
  18. $this->documentManagementController = new DocumentManagementController();
  19. }
  20. public function registerFamily(Request $request) {
  21. DB::enableQueryLog();
  22. $validator = Validator::make($request->all(), [
  23. 'id_user' => 'required|string',
  24. 'linea' => 'required|integer',
  25. 'family_code' => 'required|string',
  26. 'family_name' => 'required|string|max:50',
  27. ]);
  28. if($validator->fails()){
  29. return $this->responseController->makeResponse(
  30. true,
  31. "Se encontraron uno o más errores.",
  32. $this->responseController->makeErrors(
  33. $validator->errors()->messages()
  34. ),
  35. 401
  36. );
  37. }
  38. $form = $request->all();
  39. $idUser = $this->encryptionController->decrypt($form['id_user']);
  40. if(!$idUser){
  41. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  42. }
  43. $usr = DB::table('S002V01TUSUA')->where([
  44. ['USUA_NULI', '=', $form['linea']],
  45. ['USUA_IDUS', '=', $idUser]
  46. ])->first();
  47. if(is_null($usr)){
  48. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  49. }
  50. $familyCode = $this->encryptionController->decrypt($form['family_code']);
  51. if(!$familyCode){
  52. return $this->responseController->makeResponse(true, 'El código de la familia no fue encriptado correctamente.', [], 400);
  53. }
  54. $family = DB::table('S002V01TFAMI')->where([
  55. ['FAMI_NULI', '=', $form['linea']],
  56. ['FAMI_COFA', '=', $familyCode],
  57. ])->get()->all();
  58. if(count($family) > 0){
  59. return $this->responseController->makeResponse(true, 'El código de la familia ya se encuentra registrado.', [], 401);
  60. }
  61. $now = $this->functionsController->now();
  62. $nowStr = $now->toDateTimeString();
  63. DB::table('S002V01TFAMI')->insert([
  64. 'FAMI_NULI' => $form['linea'],
  65. 'FAMI_COFA' => $familyCode,
  66. 'FAMI_NOFA' => $form['family_name'],
  67. 'FAMI_USRE' => $idUser,
  68. 'FAMI_FERE' => $nowStr,
  69. ]);
  70. $actions = DB::getQueryLog();
  71. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  72. $idac = $this->functionsController->registerActivity(
  73. $form['linea'],
  74. 'S002V01M07GEEQ',
  75. 'S002V01F02DFSU',
  76. 'S002V01P02REFA',
  77. 'Registro',
  78. "El usuario $name (" . $usr->USUA_IDUS . ") registró la familia $form[family_name].",
  79. $idUser,
  80. $nowStr,
  81. );
  82. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  83. return $this->responseController->makeResponse(false, 'EXITO.');
  84. }
  85. public function getFamilies($idUser, $line) {
  86. DB::enableQueryLog();
  87. $idUser = $this->encryptionController->decrypt($idUser);
  88. if(!$idUser){
  89. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  90. }
  91. $usr = DB::table('S002V01TUSUA')->where([
  92. ['USUA_NULI', '=', $line],
  93. ['USUA_IDUS', '=', $idUser],
  94. ])->first();
  95. if(is_null($usr)){
  96. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  97. }
  98. $families = DB::table('S002V01TFAMI')->select([
  99. 'FAMI_COFA AS CODIGOFAMILIA',
  100. 'FAMI_NOFA AS NOMBREFAMILIA',
  101. 'FAMI_ICON AS ICONO',
  102. 'FAMI_ESTA AS ESTADO',
  103. 'FAMI_USRE AS USRREG',
  104. 'FAMI_FERE AS FECREG',
  105. 'FAMI_USMO AS USRMOD',
  106. 'FAMI_FEMO AS FECMOD'
  107. ])->where('FAMI_NULI', '=', $line)->get()->all();
  108. foreach($families as $key=>$val){
  109. $usrReg = DB::table('S002V01TUSUA')->where([
  110. ['USUA_NULI', '=', $line],
  111. ['USUA_IDUS', '=', $val->USRREG],
  112. ])->first();
  113. $nameReg = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  114. $val->USRREG = $nameReg . " (" . $val->USRREG . ")";
  115. if($val->USRMOD != null){
  116. $usrMod = DB::table('S002V01TUSUA')->where([
  117. ['USUA_NULI', '=', $line],
  118. ['USUA_IDUS', '=', $val->USRMOD],
  119. ])->first();
  120. $nameMod = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  121. $val->USRMOD = $nameMod . " (" . $val->USRMOD . ")";
  122. }
  123. $subfamilies = DB::table('S002V01TSUBF')->where([
  124. ['SUBF_NULI', '=', $line],
  125. ['SUBF_COFA', '=', $val->CODIGOFAMILIA],
  126. ])->get()->all();
  127. $val->SUBFAMILIAS = count($subfamilies);
  128. $val->CODIGOFAMILIA = $this->encryptionController->encrypt($val->CODIGOFAMILIA);
  129. $families[$key] = $val;
  130. }
  131. $now = $this->functionsController->now();
  132. $nowStr = $now->toDateTimeString();
  133. $actions = DB::getQueryLog();
  134. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  135. $idac = $this->functionsController->registerActivity(
  136. $line,
  137. 'S002V01M07GEEQ',
  138. 'S002V01F02DFSU',
  139. 'S002V01P01COFA',
  140. 'Consulta',
  141. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las familias registradas.",
  142. $idUser,
  143. $nowStr,
  144. );
  145. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  146. return $this->responseController->makeResponse(false, 'EXITO.', $families);
  147. }
  148. public function updateFamily(Request $request) {
  149. DB::enableQueryLog();
  150. $validator = Validator::make($request->all(), [
  151. 'id_user' => 'required|string',
  152. 'linea' => 'required|integer',
  153. 'family_code' => 'required|string',
  154. 'family_name' => 'required|string|max:50',
  155. ]);
  156. if($validator->fails()){
  157. return $this->responseController->makeResponse(
  158. true,
  159. "Se encontraron uno o más errores.",
  160. $this->responseController->makeErrors(
  161. $validator->errors()->messages()
  162. ),
  163. 401
  164. );
  165. }
  166. $form = $request->all();
  167. $idUser = $this->encryptionController->decrypt($form['id_user']);
  168. if(!$idUser){
  169. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  170. }
  171. $usr = DB::table('S002V01TUSUA')->where([
  172. ['USUA_NULI', '=', $form['linea']],
  173. ['USUA_IDUS', '=', $idUser]
  174. ])->first();
  175. if(is_null($usr)){
  176. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  177. }
  178. $familyCode = $this->encryptionController->decrypt($form['family_code']);
  179. if(!$familyCode){
  180. return $this->responseController->makeResponse(true, 'El código de la familia no fue encriptado correctamente.', [], 400);
  181. }
  182. $family = DB::table('S002V01TFAMI')->where([
  183. ['FAMI_NULI', '=', $form['linea']],
  184. ['FAMI_COFA', '=', $familyCode],
  185. ])->first();
  186. if(is_null($family)){
  187. return $this->responseController->makeResponse(true, 'La familia que desea actualizar no existe.', [], 404);
  188. }else if($family->FAMI_ESTA == 'Eliminado'){
  189. return $this->responseController->makeResponse(true, 'La familia que desea actualizar está eliminada.', [], 404);
  190. }
  191. $now = $this->functionsController->now();
  192. $nowStr = $now->toDateTimeString();
  193. DB::table('S002V01TFAMI')->where([
  194. ['FAMI_NULI', '=', $form['linea']],
  195. ['FAMI_COFA', '=', $familyCode],
  196. ])->update([
  197. 'FAMI_NOFA' => $form['family_name'],
  198. 'FAMI_USMO' => $idUser,
  199. 'FAMI_FEMO' => $nowStr,
  200. ]);
  201. $actions = DB::getQueryLog();
  202. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  203. $idac = $this->functionsController->registerActivity(
  204. $form['linea'],
  205. 'S002V01M07GEEQ',
  206. 'S002V01F02DFSU',
  207. 'S002V01P02REFA',
  208. 'Actualización',
  209. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó la familia $familyCode.",
  210. $idUser,
  211. $nowStr,
  212. );
  213. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  214. return $this->responseController->makeResponse(false, 'EXITO.');
  215. }
  216. public function deleteFamily(Request $request) {
  217. DB::enableQueryLog();
  218. $validator = Validator::make($request->all(), [
  219. 'id_user' => 'required|string',
  220. 'linea' => 'required|integer',
  221. 'family_code' => 'required|string',
  222. ]);
  223. if($validator->fails()){
  224. return $this->responseController->makeResponse(
  225. true,
  226. "Se encontraron uno o más errores.",
  227. $this->responseController->makeErrors(
  228. $validator->errors()->messages()
  229. ),
  230. 401
  231. );
  232. }
  233. $form = $request->all();
  234. $idUser = $this->encryptionController->decrypt($form['id_user']);
  235. if(!$idUser){
  236. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  237. }
  238. $usr = DB::table('S002V01TUSUA')->where([
  239. ['USUA_NULI', '=', $form['linea']],
  240. ['USUA_IDUS', '=', $idUser]
  241. ])->first();
  242. if(is_null($usr)){
  243. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  244. }
  245. $familyCode = $this->encryptionController->decrypt($form['family_code']);
  246. if(!$familyCode){
  247. return $this->responseController->makeResponse(true, 'El código de la familia no fue encriptado correctamente.', [], 400);
  248. }
  249. $family = DB::table('S002V01TFAMI')->where([
  250. ['FAMI_NULI', '=', $form['linea']],
  251. ['FAMI_COFA', '=', $familyCode],
  252. ])->first();
  253. if(is_null($family)){
  254. return $this->responseController->makeResponse(true, 'La familia que desea eliminar no existe.', [], 404);
  255. }else if($family->FAMI_ESTA == 'Eliminado'){
  256. return $this->responseController->makeResponse(true, 'La familia que desea eliminar está eliminada.', [], 404);
  257. }
  258. $now = $this->functionsController->now();
  259. $nowStr = $now->toDateTimeString();
  260. DB::table('S002V01TFAMI')->where([
  261. ['FAMI_NULI', '=', $form['linea']],
  262. ['FAMI_COFA', '=', $familyCode],
  263. ])->update([
  264. 'FAMI_ESTA' => 'Eliminado',
  265. 'FAMI_USMO' => $idUser,
  266. 'FAMI_FEMO' => $nowStr,
  267. ]);
  268. $actions = DB::getQueryLog();
  269. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  270. $idac = $this->functionsController->registerActivity(
  271. $form['linea'],
  272. 'S002V01M07GEEQ',
  273. 'S002V01F02DFSU',
  274. 'S002V01P02REFA',
  275. 'Eliminación',
  276. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó la familia $familyCode.",
  277. $idUser,
  278. $nowStr,
  279. );
  280. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  281. return $this->responseController->makeResponse(false, 'EXITO.');
  282. }
  283. public function registerSubfamily(Request $request) {
  284. DB::enableQueryLog();
  285. $validator = Validator::make($request->all(), [
  286. 'id_user' => 'required|string',
  287. 'linea' => 'required|integer',
  288. 'family_code' => 'required|string',
  289. 'subfamily_code' => 'required|string',
  290. 'subfamily_name' => 'required|string|max:50',
  291. ]);
  292. if($validator->fails()){
  293. return $this->responseController->makeResponse(
  294. true,
  295. "Se encontraron uno o más errores.",
  296. $this->responseController->makeErrors(
  297. $validator->errors()->messages()
  298. ),
  299. 401
  300. );
  301. }
  302. $form = $request->all();
  303. $idUser = $this->encryptionController->decrypt($form['id_user']);
  304. if(!$idUser){
  305. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  306. }
  307. $usr = DB::table('S002V01TUSUA')->where([
  308. ['USUA_NULI', '=', $form['linea']],
  309. ['USUA_IDUS', '=', $idUser]
  310. ])->first();
  311. if(is_null($usr)){
  312. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  313. }
  314. $familyCode = $this->encryptionController->decrypt($form['family_code']);
  315. if(!$familyCode){
  316. return $this->responseController->makeResponse(true, 'El código de la familia relacionada no fue encriptado correctamente.', [], 400);
  317. }
  318. $family = DB::table('S002V01TFAMI')->where([
  319. ['FAMI_NULI', '=', $form['linea']],
  320. ['FAMI_COFA', '=', $familyCode],
  321. ])->first();
  322. if(is_null($family)){
  323. return $this->responseController->makeResponse(true, 'La familia relacionada no existe.', [], 404);
  324. }else if($family->FAMI_ESTA == 'Eliminado'){
  325. return $this->responseController->makeResponse(true, 'La familia relacionada está eliminada.', [], 404);
  326. }
  327. $subfamilyCode = $this->encryptionController->decrypt($form['subfamily_code']);
  328. if(!$subfamilyCode){
  329. return $this->responseController->makeResponse(true, 'El código de la subfamilia no fue encriptado correctamente.', [], 400);
  330. }
  331. $subfamily = DB::table('S002V01TSUBF')->where([
  332. ['SUBF_NULI', '=', $form['linea']],
  333. ['SUBF_COFA', '=', $familyCode],
  334. ['SUBF_COSU', '=', $subfamilyCode],
  335. ])->get()->all();
  336. if(count($subfamily) > 0){
  337. return $this->responseController->makeResponse(true, 'El código de la subfamilia ya se encuentra registrado.', [], 401);
  338. }
  339. $now = $this->functionsController->now();
  340. $nowStr = $now->toDateTimeString();
  341. DB::table('S002V01TSUBF')->insert([
  342. 'SUBF_NULI' => $form['linea'],
  343. 'SUBF_COFA' => $familyCode,
  344. 'SUBF_COSU' => $subfamilyCode,
  345. 'SUBF_NOSU' => $form['subfamily_name'],
  346. 'SUBF_USRE' => $idUser,
  347. 'SUBF_FERE' => $nowStr,
  348. ]);
  349. $actions = DB::getQueryLog();
  350. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  351. $idac = $this->functionsController->registerActivity(
  352. $form['linea'],
  353. 'S002V01M07GEEQ',
  354. 'S002V01F02DFSU',
  355. 'S002V01P04RESU',
  356. 'Registro',
  357. "El usuario $name (" . $usr->USUA_IDUS . ") registró la subfamilia $form[subfamily_name].",
  358. $idUser,
  359. $nowStr,
  360. );
  361. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  362. return $this->responseController->makeResponse(false, 'EXITO.');
  363. }
  364. public function getSubfamilies($familyCode, $idUser, $line) {
  365. DB::enableQueryLog();
  366. $idUser = $this->encryptionController->decrypt($idUser);
  367. if(!$idUser){
  368. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  369. }
  370. $usr = DB::table('S002V01TUSUA')->where([
  371. ['USUA_NULI', '=', $line],
  372. ['USUA_IDUS', '=', $idUser],
  373. ])->first();
  374. if(is_null($usr)){
  375. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  376. }
  377. $familyCode = $this->encryptionController->decrypt($familyCode);
  378. if(!$familyCode){
  379. return $this->responseController->makeResponse(true, 'El código de la familia relacionada no fue encriptado correctamente.', [], 400);
  380. }
  381. $family = DB::table('S002V01TFAMI')->where([
  382. ['FAMI_NULI', '=', $line],
  383. ['FAMI_COFA', '=', $familyCode],
  384. ])->first();
  385. if(is_null($family)){
  386. return $this->responseController->makeResponse(true, 'La familia relacionada no existe.', [], 404);
  387. }else if($family->FAMI_ESTA == 'Eliminado'){
  388. return $this->responseController->makeResponse(true, 'La familia relacionada está eliminada.', [], 404);
  389. }
  390. $subfamilies = DB::table('S002V01TSUBF')->select([
  391. 'SUBF_COFA AS CODIGOFAMILIA',
  392. 'SUBF_COSU AS CODIGOSUBFAMILIA',
  393. 'SUBF_NOSU AS NOMBRESUBFAMILIA',
  394. 'SUBF_ICON AS ICONO',
  395. 'SUBF_ESTA AS ESTADO',
  396. 'SUBF_USRE AS USRREG',
  397. 'SUBF_FERE AS FECREG',
  398. 'SUBF_USMO AS USRMOD',
  399. 'SUBF_FEMO AS FECMOD'
  400. ])->where([
  401. ['SUBF_NULI', '=', $line],
  402. ['SUBF_COFA', '=', $familyCode],
  403. ])->get()->all();
  404. foreach($subfamilies as $key=>$val){
  405. $val->CODIGOFAMILIA = $this->encryptionController->encrypt($val->CODIGOFAMILIA);
  406. $val->CODIGOSUBFAMILIA = $this->encryptionController->encrypt($val->CODIGOSUBFAMILIA);
  407. $usrReg = DB::table('S002V01TUSUA')->where([
  408. ['USUA_NULI', '=', $line],
  409. ['USUA_IDUS', '=', $val->USRREG],
  410. ])->first();
  411. $nameReg = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  412. $val->USRREG = $nameReg . " (" . $val->USRREG . ")";
  413. if($val->USRMOD != null){
  414. $usrMod = DB::table('S002V01TUSUA')->where([
  415. ['USUA_NULI', '=', $line],
  416. ['USUA_IDUS', '=', $val->USRMOD],
  417. ])->first();
  418. $nameMod = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  419. $val->USRMOD = $nameMod . " (" . $val->USRMOD . ")";
  420. }
  421. $families[$key] = $val;
  422. }
  423. $now = $this->functionsController->now();
  424. $nowStr = $now->toDateTimeString();
  425. $actions = DB::getQueryLog();
  426. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  427. $idac = $this->functionsController->registerActivity(
  428. $line,
  429. 'S002V01M07GEEQ',
  430. 'S002V01F02DFSU',
  431. 'S002V01P03COSU',
  432. 'Consulta',
  433. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las subfamilias relacionadas a la familia $familyCode.",
  434. $idUser,
  435. $nowStr,
  436. );
  437. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  438. return $this->responseController->makeResponse(false, 'EXITO.', $subfamilies);
  439. }
  440. public function updateSubfamily(Request $request) {
  441. DB::enableQueryLog();
  442. $validator = Validator::make($request->all(), [
  443. 'id_user' => 'required|string',
  444. 'linea' => 'required|integer',
  445. 'family_code' => 'required|string',
  446. 'subfamily_code' => 'required|string',
  447. 'subfamily_name' => 'required|string|max:50',
  448. ]);
  449. if($validator->fails()){
  450. return $this->responseController->makeResponse(
  451. true,
  452. "Se encontraron uno o más errores.",
  453. $this->responseController->makeErrors(
  454. $validator->errors()->messages()
  455. ),
  456. 401
  457. );
  458. }
  459. $form = $request->all();
  460. $idUser = $this->encryptionController->decrypt($form['id_user']);
  461. if(!$idUser){
  462. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  463. }
  464. $usr = DB::table('S002V01TUSUA')->where([
  465. ['USUA_NULI', '=', $form['linea']],
  466. ['USUA_IDUS', '=', $idUser]
  467. ])->first();
  468. if(is_null($usr)){
  469. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  470. }
  471. $familyCode = $this->encryptionController->decrypt($form['family_code']);
  472. if(!$familyCode){
  473. return $this->responseController->makeResponse(true, 'El código de la familia relacionada no fue encriptado correctamente.', [], 400);
  474. }
  475. $family = DB::table('S002V01TFAMI')->where([
  476. ['FAMI_NULI', '=', $form['linea']],
  477. ['FAMI_COFA', '=', $familyCode],
  478. ])->first();
  479. if(is_null($family)){
  480. return $this->responseController->makeResponse(true, 'La familia relacionada no existe.', [], 404);
  481. }else if($family->FAMI_ESTA == 'Eliminado'){
  482. return $this->responseController->makeResponse(true, 'La familia relacionada está eliminada.', [], 404);
  483. }
  484. $subfamilyCode = $this->encryptionController->decrypt($form['subfamily_code']);
  485. if(!$subfamilyCode){
  486. return $this->responseController->makeResponse(true, 'El código de la subfamilia no fue encriptado correctamente.', [], 400);
  487. }
  488. $subfamily = DB::table('S002V01TSUBF')->where([
  489. ['SUBF_NULI', '=', $form['linea']],
  490. ['SUBF_COFA', '=', $familyCode],
  491. ['SUBF_COSU', '=', $subfamilyCode],
  492. ])->first();
  493. if(is_null($subfamily)){
  494. return $this->responseController->makeResponse(true, 'La subfamilia que desea actualizar no existe.', [], 404);
  495. }else if($subfamily->SUBF_ESTA == 'Eliminado'){
  496. return $this->responseController->makeResponse(true, 'La subfamilia que desea actualizar está eliminada.', [], 404);
  497. }
  498. $now = $this->functionsController->now();
  499. $nowStr = $now->toDateTimeString();
  500. DB::table('S002V01TSUBF')->where([
  501. 'SUBF_NULI' => $form['linea'],
  502. 'SUBF_COFA' => $familyCode,
  503. 'SUBF_COSU' => $subfamilyCode,
  504. ])->update([
  505. 'SUBF_NOSU' => $form['subfamily_name'],
  506. 'SUBF_USMO' => $idUser,
  507. 'SUBF_FEMO' => $nowStr,
  508. ]);
  509. $actions = DB::getQueryLog();
  510. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  511. $idac = $this->functionsController->registerActivity(
  512. $form['linea'],
  513. 'S002V01M07GEEQ',
  514. 'S002V01F02DFSU',
  515. 'S002V01P04RESU',
  516. 'Actualización',
  517. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó la subfamilia $subfamilyCode de la familia $familyCode.",
  518. $idUser,
  519. $nowStr,
  520. );
  521. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  522. return $this->responseController->makeResponse(false, 'EXITO.');
  523. }
  524. public function deleteSubfamily(Request $request) {
  525. DB::enableQueryLog();
  526. $validator = Validator::make($request->all(), [
  527. 'id_user' => 'required|string',
  528. 'linea' => 'required|integer',
  529. 'family_code' => 'required|string',
  530. 'subfamily_code' => 'required|string',
  531. ]);
  532. if($validator->fails()){
  533. return $this->responseController->makeResponse(
  534. true,
  535. "Se encontraron uno o más errores.",
  536. $this->responseController->makeErrors(
  537. $validator->errors()->messages()
  538. ),
  539. 401
  540. );
  541. }
  542. $form = $request->all();
  543. $idUser = $this->encryptionController->decrypt($form['id_user']);
  544. if(!$idUser){
  545. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  546. }
  547. $usr = DB::table('S002V01TUSUA')->where([
  548. ['USUA_NULI', '=', $form['linea']],
  549. ['USUA_IDUS', '=', $idUser]
  550. ])->first();
  551. if(is_null($usr)){
  552. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  553. }
  554. $familyCode = $this->encryptionController->decrypt($form['family_code']);
  555. if(!$familyCode){
  556. return $this->responseController->makeResponse(true, 'El código de la familia relacionada no fue encriptado correctamente.', [], 400);
  557. }
  558. $family = DB::table('S002V01TFAMI')->where([
  559. ['FAMI_NULI', '=', $form['linea']],
  560. ['FAMI_COFA', '=', $familyCode],
  561. ])->first();
  562. if(is_null($family)){
  563. return $this->responseController->makeResponse(true, 'La familia relacionada no existe.', [], 404);
  564. }else if($family->FAMI_ESTA == 'Eliminado'){
  565. return $this->responseController->makeResponse(true, 'La familia relacionada está eliminada.', [], 404);
  566. }
  567. $subfamilyCode = $this->encryptionController->decrypt($form['subfamily_code']);
  568. if(!$subfamilyCode){
  569. return $this->responseController->makeResponse(true, 'El código de la subfamilia no fue encriptado correctamente.', [], 400);
  570. }
  571. $subfamily = DB::table('S002V01TSUBF')->where([
  572. ['SUBF_NULI', '=', $form['linea']],
  573. ['SUBF_COFA', '=', $familyCode],
  574. ['SUBF_COSU', '=', $subfamilyCode],
  575. ])->first();
  576. if(is_null($subfamily)){
  577. return $this->responseController->makeResponse(true, 'La subfamilia que desea eliminar no existe.', [], 404);
  578. }else if($subfamily->SUBF_ESTA == 'Eliminado'){
  579. return $this->responseController->makeResponse(true, 'La subfamilia que desea eliminar está eliminada.', [], 404);
  580. }
  581. $now = $this->functionsController->now();
  582. $nowStr = $now->toDateTimeString();
  583. DB::table('S002V01TSUBF')->where([
  584. 'SUBF_NULI' => $form['linea'],
  585. 'SUBF_COFA' => $familyCode,
  586. 'SUBF_COSU' => $subfamilyCode,
  587. ])->update([
  588. 'SUBF_ESTA' => 'Eliminado',
  589. 'SUBF_USMO' => $idUser,
  590. 'SUBF_FEMO' => $nowStr,
  591. ]);
  592. $actions = DB::getQueryLog();
  593. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  594. $idac = $this->functionsController->registerActivity(
  595. $form['linea'],
  596. 'S002V01M07GEEQ',
  597. 'S002V01F02DFSU',
  598. 'S002V01P04RESU',
  599. 'Eliminación',
  600. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó la subfamilia $subfamilyCode de la familia $familyCode.",
  601. $idUser,
  602. $nowStr,
  603. );
  604. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  605. return $this->responseController->makeResponse(false, 'EXITO.');
  606. }
  607. public function getLocations($idUser, $line) {
  608. DB::enableQueryLog();
  609. $idUser = $this->encryptionController->decrypt($idUser);
  610. if(!$idUser){
  611. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  612. }
  613. $usr = DB::table('S002V01TUSUA')->where([
  614. ['USUA_NULI', '=', $line],
  615. ['USUA_IDUS', '=', $idUser],
  616. ])->first();
  617. if(is_null($usr)){
  618. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  619. }
  620. $ubic = $this->templatesUbic;
  621. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  622. $locationsEnc = file_get_contents($ubic . "locations.sam");
  623. $locationsDec = $this->encryptionController->decrypt($locationsEnc);
  624. $locationsArr = json_decode($locationsDec, true);
  625. $now = $this->functionsController->now();
  626. $nowStr = $now->toDateTimeString();
  627. $actions = DB::getQueryLog();
  628. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  629. $idac = $this->functionsController->registerActivity(
  630. $line,
  631. 'S002V01M07GEEQ',
  632. 'S002V01F01ADEQ',
  633. '-',
  634. 'Consulta',
  635. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el arreglo de ubicaciones de la línea.",
  636. $idUser,
  637. $nowStr,
  638. );
  639. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  640. return $this->responseController->makeResponse(false, 'EXITO.', $locationsArr);
  641. }
  642. public function getOccupationAreas($idUser, $line) {
  643. DB::enableQueryLog();
  644. $idUser = $this->encryptionController->decrypt($idUser);
  645. if(!$idUser){
  646. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  647. }
  648. $usr = DB::table('S002V01TUSUA')->where([
  649. ['USUA_NULI', '=', $line],
  650. ['USUA_IDUS', '=', $idUser],
  651. ])->first();
  652. if(is_null($usr)){
  653. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  654. }
  655. $ubic = $this->templatesUbic;
  656. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  657. $occupationAreasEnc = file_get_contents($ubic . "occupation-areas.sam");
  658. $occupationAreasDec = $this->encryptionController->decrypt($occupationAreasEnc);
  659. $occupationAreasArr = json_decode($occupationAreasDec, true);
  660. $now = $this->functionsController->now();
  661. $nowStr = $now->toDateTimeString();
  662. $actions = DB::getQueryLog();
  663. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  664. $idac = $this->functionsController->registerActivity(
  665. $line,
  666. 'S002V01M07GEEQ',
  667. 'S002V01F01ADEQ',
  668. '-',
  669. 'Consulta',
  670. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las áreas de ocupación.",
  671. $idUser,
  672. $nowStr,
  673. );
  674. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  675. return $this->responseController->makeResponse(false, 'EXITO.', $occupationAreasArr);
  676. }
  677. public function registerOccupation(Request $request) {
  678. DB::enableQueryLog();
  679. $validator = Validator::make($request->all(), [
  680. 'id_user' => 'required|string',
  681. 'linea' => 'required|integer',
  682. 'area' => 'required|string',
  683. 'description' => 'required|string|min:15|max:150',
  684. ]);
  685. if($validator->fails()){
  686. return $this->responseController->makeResponse(
  687. true,
  688. "Se encontraron uno o más errores.",
  689. $this->responseController->makeErrors(
  690. $validator->errors()->messages()
  691. ),
  692. 401
  693. );
  694. }
  695. $form = $request->all();
  696. $idUser = $this->encryptionController->decrypt($form['id_user']);
  697. if(!$idUser){
  698. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  699. }
  700. $usr = DB::table('S002V01TUSUA')->where([
  701. ['USUA_NULI', '=', $form['linea']],
  702. ['USUA_IDUS', '=', $idUser]
  703. ])->first();
  704. if(is_null($usr)){
  705. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  706. }
  707. $ubic = $this->templatesUbic;
  708. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  709. $occupationAreasEnc = file_get_contents($ubic . "occupation-areas.sam");
  710. $occupationAreasDec = $this->encryptionController->decrypt($occupationAreasEnc);
  711. $occupationAreasArr = json_decode($occupationAreasDec, true);
  712. $occupationAreasArrDec = [];
  713. foreach($occupationAreasArr as $area){
  714. $occupationAreasArrDec[] = [
  715. 'CODE' => $this->encryptionController->decrypt($area['CODE']),
  716. 'AREA' => $this->encryptionController->decrypt($area['AREA'])
  717. ];
  718. }
  719. $areaCode = $this->encryptionController->decrypt($form['area']);
  720. if(!$areaCode){
  721. return $this->responseController->makeResponse(true, 'El área de ocupación no fue encriptada correctamente.', [], 400);
  722. }
  723. $areaCodeExists = array_filter($occupationAreasArrDec, function($v, $k) use ($areaCode) {
  724. return $v['CODE'] == $areaCode;
  725. }, ARRAY_FILTER_USE_BOTH);
  726. if(count($areaCodeExists) < 1){
  727. return $this->responseController->makeResponse(true, 'El área de ocupación seleccionada no existe.', [], 400);
  728. }
  729. $areaName = end($areaCodeExists)['AREA'] . " ($areaCode)";
  730. $lastOccupationByArea = DB::table('S002V01TOCUP')->where([
  731. ['OCUP_NULI', '=', $form['linea']],
  732. ['OCUP_AREA', '=', $areaCode]
  733. ])->orderBy('OCUP_IDOC', 'desc')->first();
  734. $id = 0;
  735. if(is_null($lastOccupationByArea)){
  736. $id = 1;
  737. }else{
  738. $id = $lastOccupationByArea->OCUP_IDOC + 1;
  739. }
  740. if($id > 999){
  741. return $this->responseController->makeResponse(true, "El área $areaName llegó al límite de 999 elementos.", [], 401);
  742. }
  743. $now = $this->functionsController->now();
  744. $nowStr = $now->toDateTimeString();
  745. DB::table('S002V01TOCUP')->insert([
  746. 'OCUP_NULI' => $form['linea'],
  747. 'OCUP_AREA' => $areaCode,
  748. 'OCUP_IDOC' => $id,
  749. 'OCUP_DESC' => $form['description'],
  750. 'OCUP_USRE' => $idUser,
  751. 'OCUP_FERE' => $nowStr,
  752. ]);
  753. $actions = DB::getQueryLog();
  754. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  755. $idac = $this->functionsController->registerActivity(
  756. $form['linea'],
  757. 'S002V01M07GEEQ',
  758. 'S002V01F01ADEQ',
  759. '-',
  760. 'Registro',
  761. "El usuario $name (" . $usr->USUA_IDUS . ") registró una nueva ocupación para el área $areaName.",
  762. $idUser,
  763. $nowStr,
  764. );
  765. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  766. return $this->responseController->makeResponse(false, 'EXITO.');
  767. }
  768. public function getOccupations($idUser, $line) {
  769. DB::enableQueryLog();
  770. $idUser = $this->encryptionController->decrypt($idUser);
  771. if(!$idUser){
  772. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  773. }
  774. $usr = DB::table('S002V01TUSUA')->where([
  775. ['USUA_NULI', '=', $line],
  776. ['USUA_IDUS', '=', $idUser],
  777. ])->first();
  778. if(is_null($usr)){
  779. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  780. }
  781. $ubic = $this->templatesUbic;
  782. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  783. $occupationAreasEnc = file_get_contents($ubic . "occupation-areas.sam");
  784. $occupationAreasDec = $this->encryptionController->decrypt($occupationAreasEnc);
  785. $occupationAreasArr = json_decode($occupationAreasDec, true);
  786. $occupationAreasArrDec = [];
  787. foreach($occupationAreasArr as $area){
  788. $occupationAreasArrDec[] = [
  789. 'CODE' => $this->encryptionController->decrypt($area['CODE']),
  790. 'AREA' => $this->encryptionController->decrypt($area['AREA'])
  791. ];
  792. }
  793. $occupations = DB::table('S002V01TOCUP')->where('OCUP_NULI', '=', $line)->get()->all();
  794. $occupationsFn = [];
  795. foreach($occupations as $occupation){
  796. $areaInd = $occupation->OCUP_AREA;
  797. $areaStr = $areaInd < 10 ? "0$areaInd" : "$areaInd";
  798. $area = array_filter($occupationAreasArrDec, function($v, $k) use ($areaStr) {
  799. return $v['CODE'] == $areaStr;
  800. }, ARRAY_FILTER_USE_BOTH);
  801. $areaName = end($area)['AREA'] . " (" . end($area)['CODE'] . ")";
  802. $occupationInd = "" . $occupation->OCUP_IDOC . "";
  803. $occupationStr = "";
  804. for($i = strlen($occupationInd); $i < 3; $i++){
  805. $occupationStr .= '0';
  806. }
  807. $usrReg = DB::table('S002V01TUSUA')->where([
  808. ['USUA_NULI', '=', $line],
  809. ['USUA_IDUS', '=', $occupation->OCUP_USRE],
  810. ])->first();
  811. $usrRegName = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA) . " (" . $occupation->OCUP_USRE . ")";
  812. $usrModName = null;
  813. if($occupation->OCUP_USMO != null){
  814. $usrMod = DB::table('S002V01TUSUA')->where([
  815. ['USUA_NULI', '=', $line],
  816. ['USUA_IDUS', '=', $occupation->OCUP_USMO],
  817. ])->first();
  818. $usrModName = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA) . " (" . $occupation->OCUP_USMO . ")";
  819. }
  820. $occupationStr = $areaStr . $occupationStr . $occupationInd;
  821. $occupationsFn[] = [
  822. "NOMBREAREA" => $areaName,
  823. "OCUPACION" => $occupationStr,
  824. "DESCRIPCION" => $occupation->OCUP_DESC,
  825. "ESTADO" => $occupation->OCUP_ESTA,
  826. "USRREG" => $usrRegName,
  827. "FECREG" => $occupation->OCUP_FERE,
  828. "USRMOD" => $usrModName,
  829. "FECMOD" => $occupation->OCUP_FEMO,
  830. ];
  831. }
  832. $now = $this->functionsController->now();
  833. $nowStr = $now->toDateTimeString();
  834. $actions = DB::getQueryLog();
  835. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  836. $idac = $this->functionsController->registerActivity(
  837. $line,
  838. 'S002V01M07GEEQ',
  839. 'S002V01F01ADEQ',
  840. '-',
  841. 'Consulta',
  842. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las ocupaciones registradas.",
  843. $idUser,
  844. $nowStr,
  845. );
  846. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  847. return $this->responseController->makeResponse(false, 'EXITO.', $occupationsFn);
  848. }
  849. public function updateOccupation(Request $request) {
  850. DB::enableQueryLog();
  851. $validator = Validator::make($request->all(), [
  852. 'id_user' => 'required|string',
  853. 'linea' => 'required|integer',
  854. 'area' => 'required|string',
  855. 'occupation' => 'required|string',
  856. 'description' => 'required|string|min:15|max:150',
  857. ]);
  858. if($validator->fails()){
  859. return $this->responseController->makeResponse(
  860. true,
  861. "Se encontraron uno o más errores.",
  862. $this->responseController->makeErrors(
  863. $validator->errors()->messages()
  864. ),
  865. 401
  866. );
  867. }
  868. $form = $request->all();
  869. $idUser = $this->encryptionController->decrypt($form['id_user']);
  870. if(!$idUser){
  871. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  872. }
  873. $usr = DB::table('S002V01TUSUA')->where([
  874. ['USUA_NULI', '=', $form['linea']],
  875. ['USUA_IDUS', '=', $idUser]
  876. ])->first();
  877. if(is_null($usr)){
  878. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  879. }
  880. $ubic = $this->templatesUbic;
  881. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  882. $occupationAreasEnc = file_get_contents($ubic . "occupation-areas.sam");
  883. $occupationAreasDec = $this->encryptionController->decrypt($occupationAreasEnc);
  884. $occupationAreasArr = json_decode($occupationAreasDec, true);
  885. $occupationAreasArrDec = [];
  886. foreach($occupationAreasArr as $area){
  887. $occupationAreasArrDec[] = [
  888. 'CODE' => $this->encryptionController->decrypt($area['CODE']),
  889. 'AREA' => $this->encryptionController->decrypt($area['AREA'])
  890. ];
  891. }
  892. $areaCode = $this->encryptionController->decrypt($form['area']);
  893. if(!$areaCode){
  894. return $this->responseController->makeResponse(true, 'El área de ocupación no fue encriptada correctamente.', [], 400);
  895. }
  896. $areaCodeExists = array_filter($occupationAreasArrDec, function($v, $k) use ($areaCode) {
  897. return $v['CODE'] == $areaCode;
  898. }, ARRAY_FILTER_USE_BOTH);
  899. if(count($areaCodeExists) < 1){
  900. return $this->responseController->makeResponse(true, 'El área de ocupación relacionada no existe.', [], 400);
  901. }
  902. $occupation = $this->encryptionController->decrypt($form['occupation']);
  903. if(!$occupation){
  904. return $this->responseController->makeResponse(true, 'La ocupación no fue encriptada correctamente.', [], 400);
  905. }
  906. $occupationID = substr($occupation, 2);
  907. $occupationObj = DB::table('S002V01TOCUP')->where([
  908. ['OCUP_NULI', '=', $form['linea']],
  909. ['OCUP_AREA', '=', $areaCode],
  910. ['OCUP_IDOC', '=', $occupationID],
  911. ])->first();
  912. if(is_null($occupationObj)){
  913. return $this->responseController->makeResponse(true, 'La ocupación que desea actualizar no existe.', [], 404);
  914. }
  915. $now = $this->functionsController->now();
  916. $nowStr = $now->toDateTimeString();
  917. DB::table('S002V01TOCUP')->where([
  918. ['OCUP_NULI', '=', $form['linea']],
  919. ['OCUP_AREA', '=', $areaCode],
  920. ['OCUP_IDOC', '=', $occupationID],
  921. ])->update([
  922. 'OCUP_DESC' => $form['description'],
  923. 'OCUP_USMO' => $idUser,
  924. 'OCUP_FEMO' => $nowStr,
  925. ]);
  926. $actions = DB::getQueryLog();
  927. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  928. $idac = $this->functionsController->registerActivity(
  929. $form['linea'],
  930. 'S002V01M07GEEQ',
  931. 'S002V01F01ADEQ',
  932. '-',
  933. 'Actualización',
  934. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó la ocupación $occupation.",
  935. $idUser,
  936. $nowStr,
  937. );
  938. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  939. return $this->responseController->makeResponse(false, 'EXITO.');
  940. }
  941. public function deleteOccupation(Request $request) {
  942. DB::enableQueryLog();
  943. $validator = Validator::make($request->all(), [
  944. 'id_user' => 'required|string',
  945. 'linea' => 'required|integer',
  946. 'area' => 'required|string',
  947. 'occupation' => 'required|string',
  948. ]);
  949. if($validator->fails()){
  950. return $this->responseController->makeResponse(
  951. true,
  952. "Se encontraron uno o más errores.",
  953. $this->responseController->makeErrors(
  954. $validator->errors()->messages()
  955. ),
  956. 401
  957. );
  958. }
  959. $form = $request->all();
  960. $idUser = $this->encryptionController->decrypt($form['id_user']);
  961. if(!$idUser){
  962. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  963. }
  964. $usr = DB::table('S002V01TUSUA')->where([
  965. ['USUA_NULI', '=', $form['linea']],
  966. ['USUA_IDUS', '=', $idUser]
  967. ])->first();
  968. if(is_null($usr)){
  969. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  970. }
  971. $ubic = $this->templatesUbic;
  972. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  973. $occupationAreasEnc = file_get_contents($ubic . "occupation-areas.sam");
  974. $occupationAreasDec = $this->encryptionController->decrypt($occupationAreasEnc);
  975. $occupationAreasArr = json_decode($occupationAreasDec, true);
  976. $occupationAreasArrDec = [];
  977. foreach($occupationAreasArr as $area){
  978. $occupationAreasArrDec[] = [
  979. 'CODE' => $this->encryptionController->decrypt($area['CODE']),
  980. 'AREA' => $this->encryptionController->decrypt($area['AREA'])
  981. ];
  982. }
  983. $areaCode = $this->encryptionController->decrypt($form['area']);
  984. if(!$areaCode){
  985. return $this->responseController->makeResponse(true, 'El área de ocupación no fue encriptada correctamente.', [], 400);
  986. }
  987. $areaCodeExists = array_filter($occupationAreasArrDec, function($v, $k) use ($areaCode) {
  988. return $v['CODE'] == $areaCode;
  989. }, ARRAY_FILTER_USE_BOTH);
  990. if(count($areaCodeExists) < 1){
  991. return $this->responseController->makeResponse(true, 'El área de ocupación relacionada no existe.', [], 400);
  992. }
  993. $occupation = $this->encryptionController->decrypt($form['occupation']);
  994. if(!$occupation){
  995. return $this->responseController->makeResponse(true, 'La ocupación no fue encriptada correctamente.', [], 400);
  996. }
  997. $occupationID = substr($occupation, 2);
  998. $occupationObj = DB::table('S002V01TOCUP')->where([
  999. ['OCUP_NULI', '=', $form['linea']],
  1000. ['OCUP_AREA', '=', $areaCode],
  1001. ['OCUP_IDOC', '=', $occupationID],
  1002. ])->first();
  1003. if(is_null($occupationObj)){
  1004. return $this->responseController->makeResponse(true, 'La ocupación que desea eliminar no existe.', [], 404);
  1005. }
  1006. $now = $this->functionsController->now();
  1007. $nowStr = $now->toDateTimeString();
  1008. DB::table('S002V01TOCUP')->where([
  1009. ['OCUP_NULI', '=', $form['linea']],
  1010. ['OCUP_AREA', '=', $areaCode],
  1011. ['OCUP_IDOC', '=', $occupationID],
  1012. ])->update([
  1013. 'OCUP_ESTA' => 'Eliminado',
  1014. 'OCUP_USMO' => $idUser,
  1015. 'OCUP_FEMO' => $nowStr,
  1016. ]);
  1017. $actions = DB::getQueryLog();
  1018. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1019. $idac = $this->functionsController->registerActivity(
  1020. $form['linea'],
  1021. 'S002V01M07GEEQ',
  1022. 'S002V01F01ADEQ',
  1023. '-',
  1024. 'Eliminación',
  1025. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó la ocupación $occupation.",
  1026. $idUser,
  1027. $nowStr,
  1028. );
  1029. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1030. return $this->responseController->makeResponse(false, 'EXITO.');
  1031. }
  1032. public function saveEquipmentPreCodified(Request $request) {
  1033. DB::enableQueryLog();
  1034. $validator = Validator::make($request->all(), [
  1035. 'id_user' => 'required|string',
  1036. 'linea' => 'required|integer',
  1037. 'hierarchy' => 'required|string|in:Padre,Hijo',
  1038. 'parent_equipment' => 'required_if:hierarchy,=,Hijo|string',
  1039. 'code_type' => 'required|integer|between:1,6',
  1040. 'origin_location' => 'required|string',
  1041. 'origin_level' => 'required|string',
  1042. 'origin_occupation' => 'required|string',
  1043. 'origin_element' => 'required|string|max:150',
  1044. 'family' => 'required|string',
  1045. 'subfamily' => 'required|string',
  1046. 'status' => 'required|string',
  1047. 'equipment_type' => 'required|string|max:150',
  1048. 'equipment_model' => 'required|string|max:75',
  1049. 'images' => 'required|json',
  1050. 'documents' => 'required|json',
  1051. 'acquisition_date' => 'required|date',
  1052. 'warranty_start_date' => 'required|date',
  1053. 'warranty_end_date' => 'required|date',
  1054. 'equipment_provider' => 'required|string',
  1055. 'serial_number' => 'required|string|max:150',
  1056. 'has_software' => 'required|string|in:Si,No',
  1057. 'software_name' => 'required_if:has_software,=,Si|string|max:150',
  1058. 'software_version' => 'required_if:has_software,=,Si|string|max:75',
  1059. 'pre_generated_code' => 'required|string',
  1060. 'pcc_intersection' => 'required_if:code_type,=,2|string|max:5',
  1061. 'pcc_position' => 'required_if:code_type,=,2|string|max:2',
  1062. 'origin_kilometer' => 'required_if:code_type,=,3|numeric',
  1063. 'destiny_location' => 'required_if:code_type,=,3|string',
  1064. 'destiny_level' => 'required_if:code_type,=,3|string',
  1065. 'destiny_occupation' => 'required_if:code_type,=,3|string',
  1066. 'destiny_element' => 'required_if:code_type,=,3|string|max:150',
  1067. 'destiny_kilometer' => 'required_if:code_type,=,3|string',
  1068. ]);
  1069. if($validator->fails()){
  1070. return $this->responseController->makeResponse(
  1071. true,
  1072. "Se encontraron uno o más errores.",
  1073. $this->responseController->makeErrors(
  1074. $validator->errors()->messages()
  1075. ),
  1076. 401
  1077. );
  1078. }
  1079. $form = $request->all();
  1080. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1081. if(!$idUser){
  1082. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1083. }
  1084. $usr = DB::table('S002V01TUSUA')->where([
  1085. ['USUA_NULI', '=', $form['linea']],
  1086. ['USUA_IDUS', '=', $idUser]
  1087. ])->first();
  1088. if(is_null($usr)){
  1089. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1090. }
  1091. $eqpa = null;
  1092. if($form['hierarchy'] == 'Hijo'){
  1093. //PENDIENTE VERIFICAR EL EQUIPAMIENTO PADRE
  1094. }
  1095. $ubic = $this->templatesUbic;
  1096. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  1097. $locationsEnc = file_get_contents($ubic . "locations.sam");
  1098. $locationsDec = $this->encryptionController->decrypt($locationsEnc);
  1099. $locationsArr = json_decode($locationsDec, true);
  1100. $locationsArrDec = [];
  1101. foreach($locationsArr as $location){
  1102. $locationStr = $this->encryptionController->decrypt($location['LOCATION']);
  1103. $code = $this->encryptionController->decrypt($location['CODE']);
  1104. $locationsArrDec[] = [
  1105. 'LOCATION' => $locationStr,
  1106. 'CODE' => $code
  1107. ];
  1108. }
  1109. $originLocation = $this->encryptionController->decrypt($form['origin_location']);
  1110. if(!$originLocation){
  1111. return $this->responseController->makeResponse(true, 'La ubicación de origen no fue encriptada correctamente.', [], 400);
  1112. }
  1113. $originLocationFilt = array_filter($locationsArrDec, function($v, $k) use ($originLocation) {
  1114. return $v['CODE'] == $originLocation;
  1115. }, ARRAY_FILTER_USE_BOTH);
  1116. if(count($originLocationFilt) < 1){
  1117. return $this->responseController->makeResponse(true, 'La ubicación de origen no se encuentra registrada en el sistema.', [], 404);
  1118. }
  1119. $originLevel = $this->encryptionController->decrypt($form['origin_level']);
  1120. if(!$originLevel){
  1121. return $this->responseController->makeResponse(true, 'El nivel de origen no fue encriptado correctamente.', [], 400);
  1122. }
  1123. $supportedLevels = [];
  1124. for($i = 5; $i > 0; $i--){
  1125. $supportedLevels[] = "S0$i";
  1126. }
  1127. for($i = 0; $i <= 15; $i++){
  1128. $tag = $i < 10 ? "P0$i" : "P$i";
  1129. $supportedLevels[] = $tag;
  1130. }
  1131. if(!in_array($originLevel, $supportedLevels)){
  1132. return $this->responseController->makeResponse(true, 'El nivel de origen seleccionado es inválido.', [], 400);
  1133. }
  1134. $originOccupationStr = $this->encryptionController->decrypt($form['origin_occupation']);
  1135. if(!$originOccupationStr){
  1136. return $this->responseController->makeResponse(true, 'La ocupación de origen no fue encriptada correctamente.', [], 400);
  1137. }
  1138. $originOccupationArea = substr($originOccupationStr, 0, 2);
  1139. $originOccupationID = substr($originOccupationStr, 2);
  1140. $originOccupation = DB::table('S002V01TOCUP')->where([
  1141. ['OCUP_NULI', '=', $form['linea']],
  1142. ['OCUP_AREA', '=', $originOccupationArea],
  1143. ['OCUP_IDOC', '=', $originOccupationID]
  1144. ])->first();
  1145. if(is_null($originOccupation)){
  1146. return $this->responseController->makeResponse(true, 'La ocupación de origen seleccionada no existe.', [], 404);
  1147. }
  1148. $familyCode = $this->encryptionController->decrypt($form['family']);
  1149. if(!$familyCode){
  1150. return $this->responseController->makeResponse(true, 'La familia del equipamiento no fue encriptada correctamente.', [], 400);
  1151. }
  1152. $family = DB::table('S002V01TFAMI')->where([
  1153. ['FAMI_NULI', '=', $form['linea']],
  1154. ['FAMI_COFA', '=', $familyCode]
  1155. ])->first();
  1156. if(is_null($family)){
  1157. return $this->responseController->makeResponse(true, 'La familia seleccionada no existe.', [], 404);
  1158. }
  1159. $subfamilyCode = $this->encryptionController->decrypt($form['subfamily']);
  1160. if(!$subfamilyCode){
  1161. return $this->responseController->makeResponse(true, 'La subfamilia del equipamiento no fue encriptada correctamente.', [], 400);
  1162. }
  1163. $subfamily = DB::table('S002V01TSUBF')->where([
  1164. ['SUBF_NULI', '=', $form['linea']],
  1165. ['SUBF_COFA', '=', $familyCode],
  1166. ['SUBF_COSU', '=', $subfamilyCode]
  1167. ])->first();
  1168. if(is_null($subfamily)){
  1169. return $this->responseController->makeResponse(true, 'La subfamilia seleccionada no existe.', [], 404);
  1170. }
  1171. $validStatus = ["A", "S", "T", "I", "R", "D"];
  1172. $status = $this->encryptionController->decrypt($form['status']);
  1173. if(!$status){
  1174. return $this->responseController->makeResponse(true, 'La estado del equipamiento no fue encriptado correctamente.', [], 400);
  1175. }else if(!in_array($status, $validStatus)){
  1176. return $this->responseController->makeResponse(true, 'El estado seleccionado para el equipamiento es inválido.', [], 400);
  1177. }
  1178. $imagesArr = json_decode($form['images'], true);
  1179. if(count($imagesArr) < 1){
  1180. return $this->responseController->makeResponse(true, 'El arreglo de la galería de imágenes está vacío.', [], 400);
  1181. }
  1182. $imagesGallery = [];
  1183. foreach($imagesArr as $imageFile){
  1184. if(!array_key_exists('type', $imageFile)){
  1185. return $this->responseController->makeResponse(true, 'El arreglo de la galería de imágenes tiene un formato inválido.', [], 400);
  1186. }else if($imageFile['type'] == 'Nuevo'){
  1187. $tempFileID = $this->encryptionController->decrypt($imageFile['id']);
  1188. $tempFile = DB::table('S002V01TARTE')->where([
  1189. ['ARTE_IDAR', '=', $tempFileID],
  1190. ['ARTE_NULI', '=', $form['linea']]
  1191. ])->first();
  1192. $finalFile = $this->documentManagementController->moveFinalFile($form['linea'], 'GEEQ', 'FO', $tempFile, $idUser);
  1193. if(!$finalFile[0]){
  1194. return $this->responseController->makeResponse(true, $finalFile[1], [], 400);
  1195. }
  1196. $imagesGallery[] = $finalFile[1];
  1197. }else{
  1198. $fileID = $this->encryptionController->decrypt($imageFile['id']);
  1199. $imagesGallery[] = $fileID;
  1200. }
  1201. }
  1202. $imagesGalleryStr = json_encode($imagesGallery);
  1203. $documentsArr = json_decode($form['documents'], true);
  1204. if(count($documentsArr) < 1){
  1205. return $this->responseController->makeResponse(true, 'El arreglo de documentos relacionados está vacío.', [], 400);
  1206. }
  1207. $documents = [];
  1208. foreach($documentsArr as $documentFile){
  1209. if(!array_key_exists('type', $documentFile)){
  1210. return $this->responseController->makeResponse(true, 'El arreglo de documentos asociados tiene un formato inválido.', [], 400);
  1211. }else if($documentFile['type'] == 'Nuevo'){
  1212. $tempFileID = $this->encryptionController->decrypt($documentFile['id']);
  1213. $tempFile = DB::table('S002V01TARTE')->where([
  1214. ['ARTE_IDAR', '=', $tempFileID],
  1215. ['ARTE_NULI', '=', $form['linea']]
  1216. ])->first();
  1217. $finalFile = $this->documentManagementController->moveFinalFile($form['linea'], 'GEEQ', 'IN', $tempFile, $idUser);
  1218. if(!$finalFile[0]){
  1219. return $this->responseController->makeResponse(true, $finalFile[1], [], 400);
  1220. }
  1221. $documents[] = $finalFile[1];
  1222. }else{
  1223. $fileID = $this->encryptionController->decrypt($documentFile['id']);
  1224. $documents[] = $fileID;
  1225. }
  1226. }
  1227. $documentsStr = json_encode($documents);
  1228. $acquisitionDate = new Carbon($form['acquisition_date']);
  1229. $warrantyStartDate = new Carbon($form['warranty_start_date']);
  1230. $warrantyEndDate = new Carbon($form['warranty_end_date']);
  1231. if($warrantyStartDate->lt($acquisitionDate)){
  1232. return $this->responseController->makeResponse(true, 'La fecha de inicio de la grantía no puede ser menor a la fecha de adquisición.', [], 400);
  1233. }else if($warrantyEndDate->lte($acquisitionDate)){
  1234. return $this->responseController->makeResponse(true, 'La fecha de término de la grantía no puede ser menor o igual a la fecha de inicio de la garantía.', [], 400);
  1235. }
  1236. $providerID = $this->encryptionController->decrypt($form['equipment_provider']);
  1237. if(!$providerID){
  1238. return $this->responseController->makeResponse(true, 'El proveedor del equipamiento no fue encriptado correctamente.', [], 400);
  1239. }
  1240. //PENDIENTE VERIFICAR EL PROVEEDOR
  1241. $serialNumer = $this->encryptionController->decrypt($form['serial_number']);
  1242. if(!$serialNumer){
  1243. return $this->responseController->makeResponse(true, 'El número de serie del equipamiento no fue encriptado correctamente.', [], 400);
  1244. }
  1245. $serialNmberDB = DB::table('S002V01TEQUI')->where([
  1246. ['EQUI_NULI', '=', $form['linea']],
  1247. ['EQUI_NUSE', '=', $serialNumer]
  1248. ])->first();
  1249. if(!is_null($serialNmberDB)){
  1250. $serialNumberStr = $serialNmberDB->EQUI_COEQ;
  1251. return $this->responseController->makeResponse(true, "El número de serie enviado ya se encuentra relacionado al equipamiento $serialNumberStr.", [], 401);
  1252. }
  1253. $preCode = $this->encryptionController->decrypt($form['pre_generated_code']);
  1254. if(!$preCode){
  1255. return $this->responseController->makeResponse(true, 'El código pre generado no fue encriptado correctamente.', [], 400);
  1256. }
  1257. $codeVerified = $this->verifyPreCode([
  1258. "line" => $form['linea'],
  1259. "originLocation" => $originLocation,
  1260. "originLevel" => $originLevel,
  1261. "originOccupation" => $originOccupationStr,
  1262. "originElement" => $form['origin_element'],
  1263. "family" => $familyCode,
  1264. "subfamily" => $subfamilyCode,
  1265. "status" => $status,
  1266. "equipmentType" => $form['equipment_type'],
  1267. "equipmentModel" => $form['equipment_model'],
  1268. "equipmentID" => "XXXXX",
  1269. "pccIntersection" => null,
  1270. "pccPosition" => null,
  1271. "originKilometers" => null,
  1272. "destinyLocation" => null,
  1273. "destinyLevel" => null,
  1274. "destinyOccupation" => null,
  1275. "destinyElement" => null,
  1276. "destinyKilometers" => null
  1277. ], $form['code_type']);
  1278. if($codeVerified != $preCode){
  1279. return $this->responseController->makeResponse(true, 'El código generado no coincide con el código verificado a partir de la información del formulario.', [], 400);
  1280. }
  1281. $deso = isset($form['software_name']) ? $form['software_name'] : null;
  1282. $veso = isset($form['software_version']) ? $form['software_version'] : null;
  1283. $now = $this->functionsController->now();
  1284. $nowStr = $now->toDateTimeString();
  1285. DB::table('S002V01TPCEQ')->insert([
  1286. 'PCEQ_NULI' => $form['linea'],
  1287. 'PCEQ_CPGE' => $preCode,
  1288. 'PCEQ_JERA' => $form['hierarchy'],
  1289. 'PCEQ_EQPA' => $eqpa,
  1290. 'PCEQ_TICO' => $form['code_type'],
  1291. 'PCEQ_UBOR' => $originLocation,
  1292. 'PCEQ_NIOR' => $originLevel,
  1293. 'PCEQ_OCOR' => $originOccupationStr,
  1294. 'PCEQ_ELOR' => $form['origin_element'],
  1295. 'PCEQ_FAMI' => $familyCode,
  1296. 'PCEQ_SUBF' => $subfamilyCode,
  1297. 'PCEQ_ESEQ' => $status,
  1298. 'PCEQ_TIEQ' => $form['equipment_type'],
  1299. 'PCEQ_MOEQ' => $form['equipment_model'],
  1300. /*'PCEQ_IPCC' => null,
  1301. 'PCEQ_PPCC' => null,
  1302. 'PCEQ_KIOR' => '',
  1303. 'PCEQ_UBDE' => '',
  1304. 'PCEQ_NIDE' => '',
  1305. 'PCEQ_OCDE' => '',
  1306. 'PCEQ_ELDE' => '',
  1307. 'PCEQ_KIDE' => '',*/
  1308. 'PCEQ_FEAD' => $form['acquisition_date'],
  1309. 'PCEQ_FIGA' => $form['warranty_start_date'],
  1310. 'PCEQ_FTGA' => $form['warranty_end_date'],
  1311. 'PCEQ_PREQ' => $providerID,
  1312. 'PCEQ_NUSE' => $serialNumer,
  1313. 'PCEQ_DESO' => $deso,
  1314. 'PCEQ_VESO' => $veso,
  1315. 'PCEQ_GAIM' => $imagesGalleryStr,
  1316. 'PCEQ_DORE' => $documentsStr,
  1317. 'PCEQ_USRE' => $idUser,
  1318. 'PCEQ_FERE' => $nowStr,
  1319. ]);
  1320. $actions = DB::getQueryLog();
  1321. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1322. $idac = $this->functionsController->registerActivity(
  1323. $form['linea'],
  1324. 'S002V01M07GEEQ',
  1325. 'S002V01F01ADEQ',
  1326. 'S002V01P11REEQ',
  1327. 'Registro',
  1328. "El usuario $name (" . $usr->USUA_IDUS . ") solicitó el registro del equipamiento $preCode.",
  1329. $idUser,
  1330. $nowStr,
  1331. );
  1332. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1333. return $this->responseController->makeResponse(false, 'EXITO.');
  1334. }
  1335. public function verifyPreCode(array $parameters, string $type) : string {
  1336. $code = "";
  1337. switch($type){
  1338. case "1":
  1339. //CASO 1: Codificación general
  1340. $lbs = ['line', 'originLocation', 'originLevel', 'originOccupation', 'originElement'];
  1341. $lbsStr = "";
  1342. foreach($lbs as $ctrl){
  1343. $val = $parameters[$ctrl];
  1344. if($ctrl == 'line'){
  1345. $val = intval($val) < 10 ? "0$val" : "$val";
  1346. }else if($ctrl == 'originElement'){
  1347. $val = $this->processElement($val, 'element');
  1348. }
  1349. $lbsStr .= $val . ".";
  1350. }
  1351. $lbsStr = substr($lbsStr, 0, -1);
  1352. $pbs = ['family', 'subfamily', 'status'];
  1353. $pbsStr = "";
  1354. foreach($pbs as $ctrl){
  1355. $val = $parameters[$ctrl];
  1356. $pbsStr .= $val . ".";
  1357. }
  1358. $pbsStr = substr($pbsStr, 0, -1);
  1359. $equipment = ['equipmentType', 'equipmentModel', 'equipmentID'];
  1360. $equipmentStr = "";
  1361. foreach($equipment as $ctrl){
  1362. $val = $parameters[$ctrl];
  1363. if($ctrl == 'equipmentModel'){
  1364. $val = $this->processElement($val, 'model');
  1365. }else if($ctrl == 'equipmentType'){
  1366. $val = $this->processElement($val, 'element');
  1367. }
  1368. $equipmentStr .= $val . '-';
  1369. }
  1370. $equipmentStr = substr($equipmentStr, 0, -1);
  1371. $code = $lbsStr . "_" . $pbsStr . "." . $equipmentStr;
  1372. break;
  1373. }
  1374. return $code;
  1375. }
  1376. private function processElement(string $value, string $type) : string {
  1377. $value = strtoupper($value);
  1378. $validVal0 = $this->functionsController->unaccent($value);
  1379. $CONNECTORS = ['DE', 'PARA', 'Y', 'O', 'DEL', 'EL', 'LA', 'LOS', 'POR', 'EN'];
  1380. $valueArr1 = explode(' ', $validVal0);
  1381. $validVal1 = [];
  1382. foreach($valueArr1 as $word){
  1383. if(!in_array($word, $CONNECTORS)){
  1384. $validVal1[] = $word;
  1385. }
  1386. }
  1387. $validLength = count($validVal1);
  1388. $validVal2 = "";
  1389. if($validLength < 1){
  1390. return "ERROR";
  1391. }else if($validLength == 1){
  1392. if(strlen($validVal1[0]) < 5){
  1393. $validVal2 = $validVal1[0];
  1394. for($i = strlen($validVal1[0]); $i < 5; $i++){
  1395. if($type == 'model'){
  1396. $validVal2 = "X$validVal2";
  1397. }else{
  1398. $validVal2 = $validVal2 . "X";
  1399. }
  1400. }
  1401. }else{
  1402. $validVal2 = substr($validVal1[0], 0, 5);
  1403. }
  1404. }else if($validLength == 2){
  1405. $word1 = "";
  1406. if(strlen($validVal1[0]) < 2){
  1407. if($type == 'model'){
  1408. $word1 = "X$validVal1[0]";
  1409. }else{
  1410. $word1 = $validVal1[0] . "X";
  1411. }
  1412. }else{
  1413. $word1 = substr($validVal1[0], 0, 2);
  1414. }
  1415. $word2 = "";
  1416. if(strlen($validVal1[1]) < 3){
  1417. $word2 = $validVal1[1];
  1418. for($i = strlen($validVal1[1]); $i < 3; $i++){
  1419. if($type == 'model'){
  1420. $word2 = "X$word2";
  1421. }else{
  1422. $word2 = $word2 . "X";
  1423. }
  1424. }
  1425. }else{
  1426. $word2 = substr($validVal1[1], 0, 3);
  1427. }
  1428. $validVal2 = $word1 . $word2;
  1429. }else if($validLength == 3){
  1430. $word1 = "";
  1431. if(strlen($validVal1[0]) < 2){
  1432. if($type == 'model'){
  1433. $word1 = "X$validVal1[0]";
  1434. }else{
  1435. $word1 = $validVal1[0] . "X";
  1436. }
  1437. }else{
  1438. $word1 = substr($validVal1[0], 0, 2);
  1439. }
  1440. $word2 = substr($validVal1[1], 0, 1);
  1441. $word3 = "";
  1442. if(strlen($validVal1[2]) < 2){
  1443. if($type == 'model'){
  1444. $word3 = "X$validVal1[2]";
  1445. }else{
  1446. $word3 = $validVal1[2] . "X";
  1447. }
  1448. }else{
  1449. $word3 = substr($validVal1[2], 0, 2);
  1450. }
  1451. $validVal2 = $word1 . $word2 . $word3;
  1452. }else if($validLength == 4){
  1453. $word1 = "";
  1454. if(strlen($validVal1[0]) < 2){
  1455. if($type == 'model'){
  1456. $word1 = "X$validVal1[0]";
  1457. }else{
  1458. $word1 = $validVal1[0] . "X";
  1459. }
  1460. }else{
  1461. $word1 = substr($validVal1[0], 0, 2);
  1462. }
  1463. $word2 = substr($validVal1[1], 0, 1);
  1464. $word3 = substr($validVal1[2], 0, 1);
  1465. $word4 = substr($validVal1[3], 0, 1);
  1466. $validVal2 = $word1 . $word2 . $word3 . $word4;
  1467. }else if($validLength >= 5){
  1468. $word1 = substr($validVal1[0], 0, 1);
  1469. $word2 = substr($validVal1[1], 0, 1);
  1470. $word3 = substr($validVal1[2], 0, 1);
  1471. $word4 = substr($validVal1[3], 0, 1);
  1472. $word5 = substr($validVal1[4], 0, 1);
  1473. $validVal2 = $word1 . $word2 . $word3 . $word4 . $word5;
  1474. }else{
  1475. return "ERROR";
  1476. }
  1477. return $validVal2;
  1478. }
  1479. public function getPendigEquipments($idUser, $line) {
  1480. DB::enableQueryLog();
  1481. $idUser = $this->encryptionController->decrypt($idUser);
  1482. if(!$idUser){
  1483. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  1484. }
  1485. $usr = DB::table('S002V01TUSUA')->where([
  1486. ['USUA_NULI', '=', $line],
  1487. ['USUA_IDUS', '=', $idUser],
  1488. ])->first();
  1489. if(is_null($usr)){
  1490. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1491. }
  1492. $pendingEquipments = DB::table('S002V01TPCEQ')->select([
  1493. 'PCEQ_IDPR AS IDREG',
  1494. 'PCEQ_CPGE AS CODIGO',
  1495. 'PCEQ_TICO AS TIPO_CODIGO',
  1496. 'PCEQ_TIEQ AS TIPO_EQUIPAMIENTO',
  1497. 'PCEQ_MOEQ AS MODELO_EQUIPAMIENTO',
  1498. 'PCEQ_GAIM AS GALERIA_IMAGENES',
  1499. 'PCEQ_ESRE AS ESTADO_REGISTRO',
  1500. ])->where([
  1501. ['PCEQ_NULI', '=', $line],
  1502. ['PCEQ_ESRE', '=', 'Revisión'],
  1503. ])->get()->all();
  1504. $idUserEnc = $this->encryptionController->encrypt($idUser);
  1505. foreach($pendingEquipments as $key=>$equipment){
  1506. $equipment->IDREG = $this->encryptionController->encrypt($equipment->IDREG);
  1507. $equipment->CODIGO = $this->encryptionController->encrypt($equipment->CODIGO);
  1508. $imagesGalleryArr = json_decode($equipment->GALERIA_IMAGENES);
  1509. $imagesGalleryFn = [];
  1510. foreach($imagesGalleryArr as $imageCode){
  1511. $imageCodeEnc = $this->encryptionController->encrypt($imageCode);
  1512. $publicUri = $this->documentManagementController->getPublicDocumentURL(
  1513. $imageCodeEnc,
  1514. $idUserEnc,
  1515. $line
  1516. );
  1517. $response = json_decode($publicUri->original, true);
  1518. if($response['error']){
  1519. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  1520. }else{
  1521. $uriEnc = $this->encryptionController->encrypt($response['response']['public_uri']);
  1522. $imagesGalleryFn[] = $uriEnc;
  1523. }
  1524. }
  1525. $equipment->GALERIA_IMAGENES = json_encode($imagesGalleryFn);
  1526. $equipment->TIPO_EQUIPAMIENTO = $this->encryptionController->encrypt($equipment->TIPO_EQUIPAMIENTO);
  1527. $equipment->MODELO_EQUIPAMIENTO = $this->encryptionController->encrypt($equipment->MODELO_EQUIPAMIENTO);
  1528. $pendingEquipments[$key] = $equipment;
  1529. }
  1530. $now = $this->functionsController->now();
  1531. $nowStr = $now->toDateTimeString();
  1532. $actions = DB::getQueryLog();
  1533. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1534. $idac = $this->functionsController->registerActivity(
  1535. $line,
  1536. 'S002V01M07GEEQ',
  1537. 'S002V01F01ADEQ',
  1538. '-',
  1539. 'Consulta',
  1540. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los equipamientos en revisión.",
  1541. $idUser,
  1542. $nowStr,
  1543. );
  1544. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1545. return $this->responseController->makeResponse(false, 'EXITO.', $pendingEquipments);
  1546. }
  1547. public function getPendigEquipment($idReg, $idUser, $line) {
  1548. DB::enableQueryLog();
  1549. $idUser = $this->encryptionController->decrypt($idUser);
  1550. if(!$idUser){
  1551. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  1552. }
  1553. $usr = DB::table('S002V01TUSUA')->where([
  1554. ['USUA_NULI', '=', $line],
  1555. ['USUA_IDUS', '=', $idUser],
  1556. ])->first();
  1557. if(is_null($usr)){
  1558. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1559. }
  1560. $idReg = $this->encryptionController->decrypt($idReg);
  1561. if(!$idReg){
  1562. return $this->responseController->makeResponse(true, 'El ID del pre-registro no está encriptado correctamente', [], 400);
  1563. }
  1564. $pendingEquipment = DB::table('S002V01TPCEQ')->select([
  1565. 'PCEQ_IDPR AS IDREG',
  1566. 'PCEQ_CPGE AS CODIGO',
  1567. 'PCEQ_JERA AS JERARQUIA',
  1568. 'PCEQ_EQPA AS PADRE',
  1569. 'PCEQ_TICO AS TIPO_CODIGO',
  1570. 'PCEQ_UBOR AS UBICACION_ORIGEN',
  1571. 'PCEQ_NIOR AS NIVEL_ORIGEN',
  1572. 'PCEQ_OCOR AS OCUPACION_ORIGEN',
  1573. 'PCEQ_ELOR AS ELEMENTO_ORIGEN',
  1574. 'PCEQ_FAMI AS FAMILIA',
  1575. 'PCEQ_SUBF AS SUBFAMILIA',
  1576. 'PCEQ_ESEQ AS ESTADO_EQUIPAMIENTO',
  1577. 'PCEQ_TIEQ AS TIPO_EQUIPAMIENTO',
  1578. 'PCEQ_MOEQ AS MODELO_EQUIPAMIENTO',
  1579. 'PCEQ_IPCC AS INTERSECCION_PCC',
  1580. 'PCEQ_PPCC AS POSICION_PCC',
  1581. 'PCEQ_KIOR AS KILOMETRO_ORIGEN',
  1582. 'PCEQ_UBDE AS UBICACION_DESTINO',
  1583. 'PCEQ_NIDE AS NIVEL_DESTINO',
  1584. 'PCEQ_OCDE AS OCUPACION_DESTINO',
  1585. 'PCEQ_ELDE AS ELEMENTO_DESTINO',
  1586. 'PCEQ_KIDE AS KILOMETRO_DESTINO',
  1587. 'PCEQ_FEAD AS FECHA_ADQUISICION',
  1588. 'PCEQ_FIGA AS FECHA_INICIO_GARANTIA',
  1589. 'PCEQ_FTGA AS FECHA_FIN_GARANTIA',
  1590. 'PCEQ_PREQ AS PROVEEDOR',
  1591. 'PCEQ_NUSE AS NUMERO_SERIAL',
  1592. 'PCEQ_DESO AS DENOMINACION_SOFTWARE',
  1593. 'PCEQ_VESO AS VERSION_SOFTWARE',
  1594. 'PCEQ_GAIM AS GALERIA_IMAGENES',
  1595. 'PCEQ_DORE AS DOCUMENTOS',
  1596. 'PCEQ_ESRE AS ESTADO_REGISTRO',
  1597. 'PCEQ_CORE AS COMENTARIOS_RECHAZO',
  1598. 'PCEQ_COAC AS COMENTARIOS_ACEPTACION',
  1599. 'PCEQ_USRE AS USUARIO_REGISTRO',
  1600. 'PCEQ_FERE AS FECHA_REGISTRO',
  1601. 'PCEQ_USMO AS USUARIO_MODIFICO',
  1602. 'PCEQ_FEMO AS FECHA_MODIFICACION'
  1603. ])->where([
  1604. ['PCEQ_NULI', '=', $line],
  1605. ['PCEQ_IDPR', '=', $idReg],
  1606. ['PCEQ_ESRE', '=', 'Revisión'],
  1607. ])->first();
  1608. if(is_null($pendingEquipment)){
  1609. return $this->responseController->makeResponse(true, 'El pre-registro solicitado no existe', [], 404);
  1610. }
  1611. $pendingEquipment->IDREG = $this->encryptionController->encrypt($pendingEquipment->IDREG);
  1612. $pendingEquipment->CODIGO = $this->encryptionController->encrypt($pendingEquipment->CODIGO);
  1613. $ubic = $this->templatesUbic;
  1614. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  1615. $locationsEnc = file_get_contents($ubic . "locations.sam");
  1616. $locationsDec = $this->encryptionController->decrypt($locationsEnc);
  1617. $locationsArr = json_decode($locationsDec, true);
  1618. $locationsArrDec = [];
  1619. foreach($locationsArr as $location){
  1620. $locationStr = $this->encryptionController->decrypt($location['LOCATION']);
  1621. $code = $this->encryptionController->decrypt($location['CODE']);
  1622. $locationsArrDec[] = [
  1623. 'LOCATION' => $locationStr,
  1624. 'CODE' => $code
  1625. ];
  1626. }
  1627. $originLocation = $pendingEquipment->UBICACION_ORIGEN;
  1628. $originLocationFilt = array_filter($locationsArrDec, function ($v, $k) use ($originLocation) {
  1629. return $v['CODE'] == $originLocation;
  1630. }, ARRAY_FILTER_USE_BOTH);
  1631. $pendingEquipment->UBICACION_ORIGEN = end($originLocationFilt)['LOCATION'] . " (" . $pendingEquipment->UBICACION_ORIGEN . ")";
  1632. $levelsArr = [];
  1633. for($i = 5; $i > 0; $i--){
  1634. $levelsArr[] = [
  1635. 'LEVEL' => "Subterráneo S0$i",
  1636. 'CODE' => "S0$i",
  1637. ];
  1638. }
  1639. for($i = 0; $i <= 15; $i++){
  1640. $code = $i < 10 ? "P0$i" : "P$i";
  1641. $level = $i == 0 ? "Planta baja P00" : "Piso $code";
  1642. $levelsArr[] = [
  1643. 'LEVEL' => $level,
  1644. 'CODE' => $code,
  1645. ];
  1646. }
  1647. $originLevel = $pendingEquipment->NIVEL_ORIGEN;
  1648. $originLevelFilt = array_filter($levelsArr, function ($v, $k) use ($originLevel) {
  1649. return $v['CODE'] == $originLevel;
  1650. }, ARRAY_FILTER_USE_BOTH);
  1651. $pendingEquipment->NIVEL_ORIGEN = end($originLevelFilt)['LEVEL'] . " (" . $pendingEquipment->NIVEL_ORIGEN . ")";
  1652. $originOccupationStr = $pendingEquipment->OCUPACION_ORIGEN;
  1653. $originOccupationArea = substr($originOccupationStr, 0, 2);
  1654. $originOccupationID = substr($originOccupationStr, 2);
  1655. $originOccupation = DB::table('S002V01TOCUP')->where([
  1656. ['OCUP_NULI', '=', $line],
  1657. ['OCUP_AREA', '=', $originOccupationArea],
  1658. ['OCUP_IDOC', '=', $originOccupationID]
  1659. ])->first();
  1660. $pendingEquipment->OCUPACION_ORIGEN = $originOccupation->OCUP_DESC . " (" . $originOccupationStr . ")";
  1661. $family = DB::table('S002V01TFAMI')->where([
  1662. ['FAMI_NULI', '=', $line],
  1663. ['FAMI_COFA', '=', $pendingEquipment->FAMILIA],
  1664. ])->first();
  1665. $subfamily = DB::table('S002V01TSUBF')->where([
  1666. ['SUBF_NULI', '=', $line],
  1667. ['SUBF_COFA', '=', $pendingEquipment->FAMILIA],
  1668. ['SUBF_COSU', '=', $pendingEquipment->SUBFAMILIA],
  1669. ])->first();
  1670. $pendingEquipment->FAMILIA = $family->FAMI_NOFA . " (" . $pendingEquipment->FAMILIA . ")";
  1671. $pendingEquipment->SUBFAMILIA = $subfamily->SUBF_NOSU . " (" . $pendingEquipment->SUBFAMILIA . ")";
  1672. $validStatus = ["A" => "Adquisición", "S" => "Stock", "T" => "Traslado", "I" => "Instalación", "R" => "Reparación", "D" => "Disposición"];
  1673. $pendingEquipment->ESTADO_EQUIPAMIENTO = $validStatus[$pendingEquipment->ESTADO_EQUIPAMIENTO] . " (" . $pendingEquipment->ESTADO_EQUIPAMIENTO . ")";
  1674. $idUserEnc = $this->encryptionController->encrypt($idUser);
  1675. $imagesGalleryArr = json_decode($pendingEquipment->GALERIA_IMAGENES);
  1676. $imagesGalleryFn = [];
  1677. foreach($imagesGalleryArr as $imageCode){
  1678. $imageCodeEnc = $this->encryptionController->encrypt($imageCode);
  1679. $publicUri = $this->documentManagementController->getPublicDocumentURL(
  1680. $imageCodeEnc,
  1681. $idUserEnc,
  1682. $line
  1683. );
  1684. $response = json_decode($publicUri->original, true);
  1685. if($response['error']){
  1686. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  1687. }else{
  1688. $uriEnc = $this->encryptionController->encrypt($response['response']['public_uri']);
  1689. $imagesGalleryFn[] = $uriEnc;
  1690. }
  1691. }
  1692. $documentsArr = json_decode($pendingEquipment->DOCUMENTOS);
  1693. $documentsFn = [];
  1694. foreach($documentsArr as $document){
  1695. $codeArr = explode('=',$document);
  1696. $codeArr0 = explode('-', $codeArr[0]);
  1697. $file = DB::table('S002V01TAFAL')->where([
  1698. ['AFAL_NULI', '=', $line],
  1699. ['AFAL_COMO', '=', $codeArr0[1]],
  1700. ['AFAL_CLDO', '=', $codeArr0[2]],
  1701. ['AFAL_FECR', '=', $codeArr0[3]],
  1702. ['AFAL_NUSE', '=', $codeArr0[4]],
  1703. ['AFAL_NUVE', '=', $codeArr[1]],
  1704. ])->first();
  1705. if(is_null($file)){
  1706. return $this->responseController->makeResponse(true, "El archivo $document no está registrado.", [], 404);
  1707. }
  1708. $documentsFn[] = [
  1709. 'id' => $this->encryptionController->encrypt($document),
  1710. 'name' => $document,
  1711. 'size' => $file->AFAL_TAMA
  1712. ];
  1713. }
  1714. $pendingEquipment->GALERIA_IMAGENES = json_encode($imagesGalleryFn);
  1715. $pendingEquipment->DOCUMENTOS = json_encode($documentsFn);
  1716. $pendingEquipment->PROVEEDOR = $this->encryptionController->encrypt($pendingEquipment->PROVEEDOR);
  1717. $pendingEquipment->NUMERO_SERIAL = $this->encryptionController->encrypt($pendingEquipment->NUMERO_SERIAL);
  1718. $usrReg = DB::table('S002V01TUSUA')->where([
  1719. ['USUA_NULI', '=', $line],
  1720. ['USUA_IDUS', '=', $pendingEquipment->USUARIO_REGISTRO]
  1721. ])->first();
  1722. $usrRegName = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  1723. $pendingEquipment->USUARIO_REGISTRO = $usrRegName . " (" . $pendingEquipment->USUARIO_REGISTRO . ")";
  1724. if(!is_null($pendingEquipment->USUARIO_MODIFICO)){
  1725. $usrMod = DB::table('S002V01TUSUA')->where([
  1726. ['USUA_NULI', '=', $line],
  1727. ['USUA_IDUS', '=', $pendingEquipment->USUARIO_MODIFICO]
  1728. ])->first();
  1729. $usrModName = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  1730. $pendingEquipment->USUARIO_MODIFICO = $usrModName . " (" . $pendingEquipment->USUARIO_MODIFICO . ")";
  1731. }
  1732. $pendingEquipment->TIPO_EQUIPAMIENTO = $this->encryptionController->encrypt($pendingEquipment->TIPO_EQUIPAMIENTO);
  1733. $pendingEquipment->MODELO_EQUIPAMIENTO = $this->encryptionController->encrypt($pendingEquipment->MODELO_EQUIPAMIENTO);
  1734. $now = $this->functionsController->now();
  1735. $nowStr = $now->toDateTimeString();
  1736. $actions = DB::getQueryLog();
  1737. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1738. $idac = $this->functionsController->registerActivity(
  1739. $line,
  1740. 'S002V01M07GEEQ',
  1741. 'S002V01F01ADEQ',
  1742. '-',
  1743. 'Consulta',
  1744. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el equipamiento pre codificado $idReg.",
  1745. $idUser,
  1746. $nowStr,
  1747. );
  1748. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1749. return $this->responseController->makeResponse(false, 'EXITO.', $pendingEquipment);
  1750. }
  1751. public function changePreCodedEquipmentStatus(Request $request) {
  1752. DB::enableQueryLog();
  1753. $validator = Validator::make($request->all(), [
  1754. 'id_user' => 'required|string',
  1755. 'linea' => 'required|integer',
  1756. 'id_reg' => 'required|string',
  1757. 'status' => 'required|string|in:Aprobado,Rechazado',
  1758. 'comments' => 'required|string|min:15',
  1759. ]);
  1760. if($validator->fails()){
  1761. return $this->responseController->makeResponse(
  1762. true,
  1763. "Se encontraron uno o más errores.",
  1764. $this->responseController->makeErrors(
  1765. $validator->errors()->messages()
  1766. ),
  1767. 401
  1768. );
  1769. }
  1770. $form = $request->all();
  1771. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1772. if(!$idUser){
  1773. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1774. }
  1775. $usr = DB::table('S002V01TUSUA')->where([
  1776. ['USUA_NULI', '=', $form['linea']],
  1777. ['USUA_IDUS', '=', $idUser]
  1778. ])->first();
  1779. if(is_null($usr)){
  1780. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1781. }
  1782. $idReg = $this->encryptionController->decrypt($form['id_reg']);
  1783. if(!$idReg){
  1784. return $this->responseController->makeResponse(true, 'El ID del pre-registro no está encriptado correctamente', [], 400);
  1785. }
  1786. $pendingEquipment = DB::table('S002V01TPCEQ')->where([
  1787. ['PCEQ_NULI', '=', $form['linea']],
  1788. ['PCEQ_IDPR', '=', $idReg],
  1789. ])->first();
  1790. if(is_null($pendingEquipment)){
  1791. return $this->responseController->makeResponse(true, 'El pre-registro solicitado no existe.', [], 404);
  1792. }else if($pendingEquipment->PCEQ_ESRE == 'Rechazado'){
  1793. return $this->responseController->makeResponse(true, 'El pre-registro se encuentra rechazado.', [], 400);
  1794. }else if($pendingEquipment->PCEQ_ESRE == 'Aprobado'){
  1795. return $this->responseController->makeResponse(true, 'El pre-registro se encuentra aprobado.', [], 400);
  1796. }
  1797. $now = $this->functionsController->now();
  1798. $nowStr = $now->toDateTimeString();
  1799. if($form['status'] == 'Rechazado'){
  1800. DB::table('S002V01TPCEQ')->where([
  1801. ['PCEQ_IDPR', '=', $idReg],
  1802. ['PCEQ_NULI', '=', $form['linea']],
  1803. ])->update([
  1804. 'PCEQ_ESRE' => $form['status'],
  1805. 'PCEQ_CORE' => $form['comments'],
  1806. 'PCEQ_USMO' => $idUser,
  1807. 'PCEQ_FEMO' => $nowStr,
  1808. ]);
  1809. }else{
  1810. //PASO 1: SE BUSCA EL ÚLTIMO ID DEL EQUIPAMIENTO
  1811. $lastID = DB::table('S002V01TEQUI')->select([
  1812. 'EQUI_IDEQ AS LAST_ID'
  1813. ])->where([
  1814. ['EQUI_NULI', '=', $form['linea']],
  1815. ['EQUI_FAMI', '=', $pendingEquipment->PCEQ_FAMI],
  1816. ['EQUI_SUBF', '=', $pendingEquipment->PCEQ_SUBF],
  1817. ['EQUI_TIPO', '=', $pendingEquipment->PCEQ_TIEQ],
  1818. ['EQUI_MODE', '=', $pendingEquipment->PCEQ_MOEQ],
  1819. ])->orderBy('EQUI_IDEQ', 'asc')->first();
  1820. //PASO 2: SE DEFINE EL ID DEL NUEVO EQUIPAMIENTO
  1821. $IDNum = is_null($lastID) ? 1 : $lastID->LAST_ID + 1;
  1822. //PASO 3: SE DEFINE UN ESPACIO PARA RETORNAR EL PROCESO
  1823. doAgain:
  1824. //PASO 4: SE RELLENA EL ID CON CEROS A LA IZQUIERDA PARA DARLE EL FORMATO DE LA CODIFICACIÓN
  1825. $IDStr = "$IDNum";
  1826. for($i = strlen($IDStr); $i < 6; $i++){
  1827. $IDStr = "0$IDStr";
  1828. }
  1829. //PASO 5: SE INTEGRA EL ID AL CÓDIGO PRE-GENERADO
  1830. $preCodeArr = explode('_', $pendingEquipment->PCEQ_CPGE);
  1831. $previewPBS = $preCodeArr[1];
  1832. $previewPBSArr = explode('-', $previewPBS);
  1833. $previewPBSArr = array_reverse($previewPBSArr);
  1834. $previewPBSArr[0] = $IDStr;
  1835. $previewPBSArr = array_reverse($previewPBSArr);
  1836. $previewPBS = implode('-', $previewPBSArr);
  1837. //PASO 6: SE BUSCA SI EL CÓDIGO ESTÁ REGISTRADO EN ALGÚN EQUIPAMIENTO O EN EL HISTORIAL DE CÓDIGOS DE ALGÚN EQUIPAMIENTO
  1838. $previewCode = $preCodeArr[0] . "_" . $previewPBS;
  1839. $registeredCode = DB::table('S002V01TEQUI')->where([
  1840. ['EQUI_NULI', '=', $form['linea']],
  1841. ['EQUI_COEQ', '=', $previewCode]
  1842. ])->first();
  1843. $codeInHistory = DB::table('S002V01TEQUI')
  1844. ->where('EQUI_NULI', '=', $form['linea'])
  1845. ->whereJsonContains('EQUI_HICO', $previewCode)->first();
  1846. //PASO 7: VERIFICAR QUE EL CÓDIGO NO EXISTA, SI EXISTE SE REALIZA NUEVAMENTE EL PROCESO AUMENTANDO EN 1 EL ID
  1847. if(!is_null($registeredCode) || !is_null($codeInHistory)){
  1848. $IDNum++;
  1849. goto doAgain;
  1850. }
  1851. //PASO 8: REALIZAR EL REGISTRO DEL EQUIPAMIENTO
  1852. $hico = json_encode([$previewCode]);
  1853. DB::table('S002V01TEQUI')->insert([
  1854. 'EQUI_NULI' => $form['linea'],
  1855. 'EQUI_FAMI' => $pendingEquipment->PCEQ_FAMI,
  1856. 'EQUI_SUBF' => $pendingEquipment->PCEQ_SUBF,
  1857. 'EQUI_TIPO' => $pendingEquipment->PCEQ_TIEQ,
  1858. 'EQUI_MODE' => $pendingEquipment->PCEQ_MOEQ,
  1859. 'EQUI_IDEQ' => $IDNum,
  1860. 'EQUI_ESEQ' => $pendingEquipment->PCEQ_ESEQ,
  1861. 'EQUI_COEQ' => $previewCode,
  1862. 'EQUI_JERA' => $pendingEquipment->PCEQ_JERA,
  1863. 'EQUI_EQPA' => $pendingEquipment->PCEQ_EQPA,
  1864. 'EQUI_TICO' => $pendingEquipment->PCEQ_TICO,
  1865. 'EQUI_UBOR' => $pendingEquipment->PCEQ_UBOR,
  1866. 'EQUI_NIOR' => $pendingEquipment->PCEQ_NIOR,
  1867. 'EQUI_OCOR' => $pendingEquipment->PCEQ_OCOR,
  1868. 'EQUI_ELOR' => $pendingEquipment->PCEQ_ELOR,
  1869. 'EQUI_IPCC' => $pendingEquipment->PCEQ_IPCC,
  1870. 'EQUI_PPCC' => $pendingEquipment->PCEQ_PPCC,
  1871. 'EQUI_KIOR' => $pendingEquipment->PCEQ_KIOR,
  1872. 'EQUI_UBDE' => $pendingEquipment->PCEQ_UBDE,
  1873. 'EQUI_NIDE' => $pendingEquipment->PCEQ_NIDE,
  1874. 'EQUI_OCDE' => $pendingEquipment->PCEQ_OCDE,
  1875. 'EQUI_ELDE' => $pendingEquipment->PCEQ_ELDE,
  1876. 'EQUI_KIDE' => $pendingEquipment->PCEQ_KIDE,
  1877. 'EQUI_FEAD' => $pendingEquipment->PCEQ_FEAD,
  1878. 'EQUI_FIGA' => $pendingEquipment->PCEQ_FIGA,
  1879. 'EQUI_FTGA' => $pendingEquipment->PCEQ_FTGA,
  1880. 'EQUI_PREQ' => $pendingEquipment->PCEQ_PREQ,
  1881. 'EQUI_NUSE' => $pendingEquipment->PCEQ_NUSE,
  1882. 'EQUI_DESO' => $pendingEquipment->PCEQ_DESO,
  1883. 'EQUI_VESO' => $pendingEquipment->PCEQ_VESO,
  1884. 'EQUI_HICO' => $hico,
  1885. 'EQUI_GAIM' => $pendingEquipment->PCEQ_GAIM,
  1886. 'EQUI_DORE' => $pendingEquipment->PCEQ_DORE,
  1887. 'EQUI_USRE' => $idUser,
  1888. 'EQUI_FERE' => $nowStr,
  1889. ]);
  1890. DB::table('S002V01TPCEQ')->where([
  1891. ['PCEQ_IDPR', '=', $idReg],
  1892. ['PCEQ_NULI', '=', $form['linea']],
  1893. ])->update([
  1894. 'PCEQ_ESRE' => $form['status'],
  1895. 'PCEQ_CORE' => $form['comments'],
  1896. 'PCEQ_USMO' => $idUser,
  1897. 'PCEQ_FEMO' => $nowStr,
  1898. ]);
  1899. }
  1900. $actions = DB::getQueryLog();
  1901. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1902. $idac = $this->functionsController->registerActivity(
  1903. $form['linea'],
  1904. 'S002V01M07GEEQ',
  1905. 'S002V01F01ADEQ',
  1906. 'S002V01P11REEQ',
  1907. 'Registro',
  1908. "El usuario $name (" . $usr->USUA_IDUS . ") cambió el estado del pre-registro $idReg a $form[status].",
  1909. $idUser,
  1910. $nowStr,
  1911. );
  1912. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1913. return $this->responseController->makeResponse(false, 'EXITO.');
  1914. }
  1915. public function getEquipments($idUser, $line) {
  1916. DB::enableQueryLog();
  1917. $idUser = $this->encryptionController->decrypt($idUser);
  1918. if(!$idUser){
  1919. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  1920. }
  1921. $usr = DB::table('S002V01TUSUA')->where([
  1922. ['USUA_NULI', '=', $line],
  1923. ['USUA_IDUS', '=', $idUser],
  1924. ])->first();
  1925. if(is_null($usr)){
  1926. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1927. }
  1928. $equipments = DB::table('S002V01TEQUI')->select([
  1929. 'EQUI_COEQ AS CODIGO',
  1930. 'EQUI_TIPO AS TIPO',
  1931. 'EQUI_MODE AS MODELO',
  1932. 'EQUI_IDEQ AS ID_EQUIPO',
  1933. 'EQUI_ESFU AS ESTADO_FUNCIONAMIENTO',
  1934. 'EQUI_GAIM AS GALERIA_IMAGENES',
  1935. 'EQUI_ELOR AS ELEMENTO_ORIGEN',
  1936. 'EQUI_TICO AS TIPO_CODIGO'
  1937. ])->where('EQUI_NULI', '=', $line)->get()->all();
  1938. $idUserEnc = $this->encryptionController->encrypt($idUser);
  1939. foreach($equipments as $key=>$equipment){
  1940. $equipment->CODIGO = $this->encryptionController->encrypt($equipment->CODIGO);
  1941. $equipment->ID_EQUIPO = $this->encryptionController->encrypt($equipment->ID_EQUIPO);
  1942. $imagesGalleryArr = json_decode($equipment->GALERIA_IMAGENES, true);
  1943. $imagesGalleryFn = [];
  1944. foreach($imagesGalleryArr as $imageCode){
  1945. $imageCodeEnc = $this->encryptionController->encrypt($imageCode);
  1946. $publicUri = $this->documentManagementController->getPublicDocumentURL(
  1947. $imageCodeEnc,
  1948. $idUserEnc,
  1949. $line
  1950. );
  1951. $response = json_decode($publicUri->original, true);
  1952. if($response['error']){
  1953. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  1954. }else{
  1955. $uriEnc = $this->encryptionController->encrypt($response['response']['public_uri']);
  1956. $imagesGalleryFn[] = $uriEnc;
  1957. }
  1958. }
  1959. $equipment->GALERIA_IMAGENES = json_encode($imagesGalleryFn);
  1960. $equipments[$key] = $equipment;
  1961. }
  1962. $now = $this->functionsController->now();
  1963. $nowStr = $now->toDateTimeString();
  1964. $actions = DB::getQueryLog();
  1965. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1966. $idac = $this->functionsController->registerActivity(
  1967. $line,
  1968. 'S002V01M07GEEQ',
  1969. 'S002V01F01ADEQ',
  1970. 'S002V01P01MEEQ',
  1971. 'Consulta',
  1972. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los equipamientos registrados.",
  1973. $idUser,
  1974. $nowStr,
  1975. );
  1976. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1977. return $this->responseController->makeResponse(false, 'EXITO.', $equipments);
  1978. }
  1979. public function getEquipmentsBySubfamily($familyCode, $subfamilyCode, $idUser, $line) {
  1980. DB::enableQueryLog();
  1981. $idUser = $this->encryptionController->decrypt($idUser);
  1982. if(!$idUser){
  1983. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  1984. }
  1985. $usr = DB::table('S002V01TUSUA')->where([
  1986. ['USUA_NULI', '=', $line],
  1987. ['USUA_IDUS', '=', $idUser],
  1988. ])->first();
  1989. if(is_null($usr)){
  1990. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1991. }
  1992. $familyCode = $this->encryptionController->decrypt($familyCode);
  1993. if(!$familyCode){
  1994. return $this->responseController->makeResponse(true, 'El código de la familia no está encriptado correctamente', [], 400);
  1995. }
  1996. $family = DB::table('S002V01TFAMI')->where([
  1997. ['FAMI_NULI', '=', $line],
  1998. ['FAMI_COFA', '=', $familyCode],
  1999. ])->first();
  2000. if(is_null($family)){
  2001. return $this->responseController->makeResponse(true, 'La familia relacionada no está registrada.', [], 404);
  2002. }
  2003. $subfamilyCode = $this->encryptionController->decrypt($subfamilyCode);
  2004. if(!$subfamilyCode){
  2005. return $this->responseController->makeResponse(true, 'El código de la subfamilia no está encriptado correctamente', [], 400);
  2006. }
  2007. $subfamily = DB::table('S002V01TSUBF')->where([
  2008. ['SUBF_NULI', '=', $line],
  2009. ['SUBF_COFA', '=', $familyCode],
  2010. ['SUBF_COSU', '=', $subfamilyCode],
  2011. ])->first();
  2012. if(is_null($subfamily)){
  2013. return $this->responseController->makeResponse(true, 'La subfamilia relacionada no está registrada.', [], 404);
  2014. }
  2015. $equipments = DB::table('S002V01TEQUI')->select([
  2016. 'EQUI_COEQ AS CODIGO',
  2017. 'EQUI_TIPO AS TIPO',
  2018. 'EQUI_MODE AS MODELO',
  2019. 'EQUI_IDEQ AS ID_EQUIPO',
  2020. 'EQUI_ESFU AS ESTADO_FUNCIONAMIENTO',
  2021. 'EQUI_GAIM AS GALERIA_IMAGENES',
  2022. ])->where([
  2023. ['EQUI_NULI', '=', $line],
  2024. ['EQUI_FAMI', '=', $familyCode],
  2025. ['EQUI_SUBF', '=', $subfamilyCode],
  2026. ])->get()->all();
  2027. $idUserEnc = $this->encryptionController->encrypt($idUser);
  2028. foreach($equipments as $key=>$equipment){
  2029. $equipment->CODIGO = $this->encryptionController->encrypt($equipment->CODIGO);
  2030. $equipment->ID_EQUIPO = $this->encryptionController->encrypt($equipment->ID_EQUIPO);
  2031. $imagesGalleryArr = json_decode($equipment->GALERIA_IMAGENES, true);
  2032. $imagesGalleryFn = [];
  2033. foreach($imagesGalleryArr as $imageCode){
  2034. $imageCodeEnc = $this->encryptionController->encrypt($imageCode);
  2035. $publicUri = $this->documentManagementController->getPublicDocumentURL(
  2036. $imageCodeEnc,
  2037. $idUserEnc,
  2038. $line
  2039. );
  2040. $response = json_decode($publicUri->original, true);
  2041. if($response['error']){
  2042. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  2043. }else{
  2044. $uriEnc = $this->encryptionController->encrypt($response['response']['public_uri']);
  2045. $imagesGalleryFn[] = $uriEnc;
  2046. }
  2047. }
  2048. $equipment->GALERIA_IMAGENES = json_encode($imagesGalleryFn);
  2049. $equipments[$key] = $equipment;
  2050. }
  2051. $now = $this->functionsController->now();
  2052. $nowStr = $now->toDateTimeString();
  2053. $actions = DB::getQueryLog();
  2054. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2055. $idac = $this->functionsController->registerActivity(
  2056. $line,
  2057. 'S002V01M07GEEQ',
  2058. 'S002V01F01ADEQ',
  2059. 'S002V01P01MEEQ',
  2060. 'Consulta',
  2061. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los equipamientos relacionados a la subfamilia $subfamilyCode de la familia $familyCode.",
  2062. $idUser,
  2063. $nowStr,
  2064. );
  2065. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2066. return $this->responseController->makeResponse(false, 'EXITO.', $equipments);
  2067. }
  2068. public function getEquipmentDetails($code, $idUser, $line) {
  2069. DB::enableQueryLog();
  2070. $idUser = $this->encryptionController->decrypt($idUser);
  2071. if(!$idUser){
  2072. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2073. }
  2074. $usr = DB::table('S002V01TUSUA')->where([
  2075. ['USUA_NULI', '=', $line],
  2076. ['USUA_IDUS', '=', $idUser],
  2077. ])->first();
  2078. if(is_null($usr)){
  2079. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2080. }
  2081. $code = $this->encryptionController->decrypt($code);
  2082. if(!$code){
  2083. return $this->responseController->makeResponse(true, 'El código del equipamiento no está encriptado correctamente', [], 400);
  2084. }
  2085. $equipment = DB::table('S002V01TEQUI')->select([
  2086. 'EQUI_FAMI AS FAMILIA',
  2087. 'EQUI_SUBF AS SUBFAMILIA',
  2088. 'EQUI_TIPO AS TIPO_EQUIPAMIENTO',
  2089. 'EQUI_MODE AS MODELO',
  2090. 'EQUI_IDEQ AS ID_EQUIPAMIENTO',
  2091. 'EQUI_ESEQ AS ESTADO',
  2092. 'EQUI_COEQ AS CODIGO',
  2093. 'EQUI_JERA AS JERARQUIA',
  2094. 'EQUI_EQPA AS EQUIPO_PADRE',
  2095. 'EQUI_TICO AS TIPO_CODIGO',
  2096. 'EQUI_UBOR AS UBICACION_ORIGEN',
  2097. 'EQUI_NIOR AS NIVEL_ORIGEN',
  2098. 'EQUI_OCOR AS OCUPACION_ORIGEN',
  2099. 'EQUI_ELOR AS ELEMENTO_ORIGEN',
  2100. 'EQUI_IPCC AS INTERSECCION_PCC',
  2101. 'EQUI_PPCC AS POSICION_PCC',
  2102. 'EQUI_KIOR AS KILOMETRO_ORIGEN',
  2103. 'EQUI_UBDE AS UBICACION_DESTINO',
  2104. 'EQUI_NIDE AS NIVEL_DESTINO',
  2105. 'EQUI_OCDE AS OCUPACION_DESTINO',
  2106. 'EQUI_ELDE AS ELEMENTO_DESTINO',
  2107. 'EQUI_KIDE AS KILOMETRO_DESTINO',
  2108. 'EQUI_FEAD AS FECHA_ADQUISICION',
  2109. 'EQUI_FIGA AS FECHA_INICIO_GARANTIA',
  2110. 'EQUI_FTGA AS FECHA_TERMINO_GARANTIA',
  2111. 'EQUI_PREQ AS PROOVEDOR',
  2112. 'EQUI_NUSE AS NUMERO_SERIAL',
  2113. 'EQUI_DESO AS DENOMINACION_SOFTWARE',
  2114. 'EQUI_VESO AS VERSION_SOFTWARE',
  2115. 'EQUI_HICO AS HISTORIAL_CODIGOS',
  2116. 'EQUI_GAIM AS GALERIA_IMAGENES',
  2117. 'EQUI_DORE AS DOCUMENTOS',
  2118. 'EQUI_ESFU AS ESTADO_FUNCIONAMIENTO',
  2119. 'EQUI_USRE AS USUARIO_REGISTRO',
  2120. 'EQUI_FERE AS FECHA_REGISTRO',
  2121. 'EQUI_USMO AS USUARIO_MODIFICACION',
  2122. 'EQUI_FEMO AS FECHA_MODIFICACION',
  2123. ])->where([
  2124. ['EQUI_NULI', '=', $line],
  2125. ['EQUI_COEQ', '=', $code],
  2126. ])->first();
  2127. $family = DB::table('S002V01TFAMI')->where([
  2128. ['FAMI_NULI', '=', $line],
  2129. ['FAMI_COFA', '=', $equipment->FAMILIA]
  2130. ])->first();
  2131. $subfamily = DB::table('S002V01TSUBF')->where([
  2132. ['SUBF_NULI', '=', $line],
  2133. ['SUBF_COFA', '=', $equipment->FAMILIA],
  2134. ['SUBF_COSU', '=', $equipment->SUBFAMILIA]
  2135. ])->first();
  2136. $equipment->FAMILIA = $family->FAMI_NOFA . " (" . $equipment->FAMILIA . ")";
  2137. $equipment->SUBFAMILIA = $subfamily->SUBF_NOSU . " (" . $equipment->SUBFAMILIA . ")";
  2138. $equipment->ID_EQUIPAMIENTO = $this->encryptionController->encrypt($equipment->ID_EQUIPAMIENTO);
  2139. $equipment->CODIGO = $this->encryptionController->encrypt($equipment->CODIGO);
  2140. $ubic = $this->templatesUbic;
  2141. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  2142. $locationsEnc = file_get_contents($ubic . "locations.sam");
  2143. $locationsDec = $this->encryptionController->decrypt($locationsEnc);
  2144. $locationsArr = json_decode($locationsDec, true);
  2145. $locationsArrDec = [];
  2146. foreach($locationsArr as $location){
  2147. $locationStr = $this->encryptionController->decrypt($location['LOCATION']);
  2148. $code = $this->encryptionController->decrypt($location['CODE']);
  2149. $locationsArrDec[] = [
  2150. 'LOCATION' => $locationStr,
  2151. 'CODE' => $code
  2152. ];
  2153. }
  2154. $originLocation = $equipment->UBICACION_ORIGEN;
  2155. $originLocationFilt = array_filter($locationsArrDec, function ($v, $k) use ($originLocation) {
  2156. return $v['CODE'] == $originLocation;
  2157. }, ARRAY_FILTER_USE_BOTH);
  2158. $equipment->UBICACION_ORIGEN = end($originLocationFilt)['LOCATION'] . " (" . $equipment->UBICACION_ORIGEN . ")";
  2159. $levelsArr = [];
  2160. for($i = 5; $i > 0; $i--){
  2161. $levelsArr[] = [
  2162. 'LEVEL' => "Subterráneo S0$i",
  2163. 'CODE' => "S0$i",
  2164. ];
  2165. }
  2166. for($i = 0; $i <= 15; $i++){
  2167. $code = $i < 10 ? "P0$i" : "P$i";
  2168. $level = $i == 0 ? "Planta baja P00" : "Piso $code";
  2169. $levelsArr[] = [
  2170. 'LEVEL' => $level,
  2171. 'CODE' => $code,
  2172. ];
  2173. }
  2174. $originLevel = $equipment->NIVEL_ORIGEN;
  2175. $originLevelFilt = array_filter($levelsArr, function ($v, $k) use ($originLevel) {
  2176. return $v['CODE'] == $originLevel;
  2177. }, ARRAY_FILTER_USE_BOTH);
  2178. $equipment->NIVEL_ORIGEN = end($originLevelFilt)['LEVEL'] . " (" . $equipment->NIVEL_ORIGEN . ")";
  2179. $originOccupationStr = $equipment->OCUPACION_ORIGEN;
  2180. $originOccupationArea = substr($originOccupationStr, 0, 2);
  2181. $originOccupationID = substr($originOccupationStr, 2);
  2182. $originOccupation = DB::table('S002V01TOCUP')->where([
  2183. ['OCUP_NULI', '=', $line],
  2184. ['OCUP_AREA', '=', $originOccupationArea],
  2185. ['OCUP_IDOC', '=', $originOccupationID]
  2186. ])->first();
  2187. $validStatus = ["A" => "Adquisición", "S" => "Stock", "T" => "Traslado", "I" => "Instalación", "R" => "Reparación", "D" => "Disposición"];
  2188. $equipment->ESTADO = $validStatus[$equipment->ESTADO] . " (" . $equipment->ESTADO . ")";
  2189. $equipment->OCUPACION_ORIGEN = $originOccupation->OCUP_DESC . " (" . $originOccupationStr . ")";
  2190. $idUserEnc = $this->encryptionController->encrypt($idUser);
  2191. $imagesGalleryArr = json_decode($equipment->GALERIA_IMAGENES);
  2192. $imagesGalleryFn = [];
  2193. foreach($imagesGalleryArr as $imageCode){
  2194. $imageCodeEnc = $this->encryptionController->encrypt($imageCode);
  2195. $publicUri = $this->documentManagementController->getPublicDocumentURL(
  2196. $imageCodeEnc,
  2197. $idUserEnc,
  2198. $line
  2199. );
  2200. $response = json_decode($publicUri->original, true);
  2201. if($response['error']){
  2202. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  2203. }else{
  2204. $uriEnc = $this->encryptionController->encrypt($response['response']['public_uri']);
  2205. $imagesGalleryFn[] = $uriEnc;
  2206. }
  2207. }
  2208. $documentsArr = json_decode($equipment->DOCUMENTOS);
  2209. $documentsFn = [];
  2210. foreach($documentsArr as $document){
  2211. $codeArr = explode('=',$document);
  2212. $codeArr0 = explode('-', $codeArr[0]);
  2213. $file = DB::table('S002V01TAFAL')->where([
  2214. ['AFAL_NULI', '=', $line],
  2215. ['AFAL_COMO', '=', $codeArr0[1]],
  2216. ['AFAL_CLDO', '=', $codeArr0[2]],
  2217. ['AFAL_FECR', '=', $codeArr0[3]],
  2218. ['AFAL_NUSE', '=', $codeArr0[4]],
  2219. ['AFAL_NUVE', '=', $codeArr[1]],
  2220. ])->first();
  2221. if(is_null($file)){
  2222. return $this->responseController->makeResponse(true, "El archivo $document no está registrado.", [], 404);
  2223. }
  2224. $documentsFn[] = [
  2225. 'id' => $this->encryptionController->encrypt($document),
  2226. 'name' => $document,
  2227. 'size' => $file->AFAL_TAMA
  2228. ];
  2229. }
  2230. $equipment->GALERIA_IMAGENES = json_encode($imagesGalleryFn);
  2231. $equipment->DOCUMENTOS = json_encode($documentsFn);
  2232. $equipment->PROOVEDOR = $this->encryptionController->encrypt($equipment->PROOVEDOR);
  2233. $equipment->NUMERO_SERIAL = $this->encryptionController->encrypt($equipment->NUMERO_SERIAL);
  2234. $codesHistory = json_decode($equipment->HISTORIAL_CODIGOS, true);
  2235. $codesHistoryFn = [];
  2236. foreach($codesHistory as $code){
  2237. $codeEnc = $this->encryptionController->encrypt($code);
  2238. $codesHistoryFn[] = $codeEnc;
  2239. }
  2240. $equipment->HISTORIAL_CODIGOS = json_encode($codesHistoryFn);
  2241. $usrReg = DB::table('S002V01TUSUA')->where([
  2242. ['USUA_NULI', '=', $line],
  2243. ['USUA_IDUS', '=', $equipment->USUARIO_REGISTRO]
  2244. ])->first();
  2245. $usrRegName = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  2246. $equipment->USUARIO_REGISTRO = $usrRegName . " (" . $equipment->USUARIO_REGISTRO . ")";
  2247. if(!is_null($equipment->USUARIO_MODIFICACION)){
  2248. $usrMod = DB::table('S002V01TUSUA')->where([
  2249. ['USUA_NULI', '=', $line],
  2250. ['USUA_IDUS', '=', $equipment->USUARIO_MODIFICACION]
  2251. ])->first();
  2252. $usrModName = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  2253. $equipment->USUARIO_MODIFICACION = $usrModName . " (" . $equipment->USUARIO_MODIFICACION . ")";
  2254. }
  2255. $equipment->TIPO_EQUIPAMIENTO = $this->encryptionController->encrypt($equipment->TIPO_EQUIPAMIENTO);
  2256. $equipment->MODELO = $this->encryptionController->encrypt($equipment->MODELO);
  2257. $now = $this->functionsController->now();
  2258. $nowStr = $now->toDateTimeString();
  2259. $actions = DB::getQueryLog();
  2260. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2261. $idac = $this->functionsController->registerActivity(
  2262. $line,
  2263. 'S002V01M07GEEQ',
  2264. 'S002V01F01ADEQ',
  2265. 'S002V01P01MEEQ',
  2266. 'Consulta',
  2267. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el equipamiento $code.",
  2268. $idUser,
  2269. $nowStr,
  2270. );
  2271. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2272. return $this->responseController->makeResponse(false, 'EXITO.', $equipment);
  2273. }
  2274. public function getTrainOccupations($idUser, $line) {
  2275. DB::enableQueryLog();
  2276. $idUser = $this->encryptionController->decrypt($idUser);
  2277. if(!$idUser){
  2278. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2279. }
  2280. $usr = DB::table('S002V01TUSUA')->where([
  2281. ['USUA_NULI', '=', $line],
  2282. ['USUA_IDUS', '=', $idUser],
  2283. ])->first();
  2284. if(is_null($usr)){
  2285. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2286. }
  2287. $ubic = $this->templatesUbic;
  2288. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  2289. $trainOccupationsEnc = file_get_contents($ubic . "train-occupations.sam");
  2290. $trainOccupationsDec = $this->encryptionController->decrypt($trainOccupationsEnc);
  2291. $trainOccupationsArr = json_decode($trainOccupationsDec, true);
  2292. $now = $this->functionsController->now();
  2293. $nowStr = $now->toDateTimeString();
  2294. $actions = DB::getQueryLog();
  2295. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2296. $idac = $this->functionsController->registerActivity(
  2297. $line,
  2298. 'S002V01M07GEEQ',
  2299. 'S002V01F01ADEQ',
  2300. '-',
  2301. 'Consulta',
  2302. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las ocupaciones de tren.",
  2303. $idUser,
  2304. $nowStr,
  2305. );
  2306. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2307. return $this->responseController->makeResponse(false, 'EXITO.', $trainOccupationsArr);
  2308. var_dump($trainOccupationsArr);
  2309. }
  2310. public function getTrainAreas($trainOccupation, $idUser, $line) {
  2311. DB::enableQueryLog();
  2312. $idUser = $this->encryptionController->decrypt($idUser);
  2313. if(!$idUser){
  2314. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2315. }
  2316. $usr = DB::table('S002V01TUSUA')->where([
  2317. ['USUA_NULI', '=', $line],
  2318. ['USUA_IDUS', '=', $idUser],
  2319. ])->first();
  2320. if(is_null($usr)){
  2321. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2322. }
  2323. $trainOccupation = $this->encryptionController->decrypt($trainOccupation);
  2324. if(!$trainOccupation){
  2325. return $this->responseController->makeResponse(true, 'La ocupación no fue encriptada correctamente.', [], 400);
  2326. }
  2327. $ubic = $this->templatesUbic;
  2328. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  2329. $carAreasEnc = file_get_contents($ubic . "car-areas.sam");
  2330. $carAreasDec = $this->encryptionController->decrypt($carAreasEnc);
  2331. $carAreasArr = json_decode($carAreasDec, true);
  2332. $carAreasArrDec = [];
  2333. foreach($carAreasArr as $key=>$val){
  2334. $keyDec = $this->encryptionController->decrypt($key);
  2335. $carAreasArrDec[$keyDec] = $val;
  2336. }
  2337. if(!array_key_exists($trainOccupation, $carAreasArrDec)){
  2338. return $this->responseController->makeResponse(true, "La ocupación $trainOccupation no existe.", [], 404);
  2339. }
  2340. $now = $this->functionsController->now();
  2341. $nowStr = $now->toDateTimeString();
  2342. $actions = DB::getQueryLog();
  2343. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2344. $idac = $this->functionsController->registerActivity(
  2345. $line,
  2346. 'S002V01M07GEEQ',
  2347. 'S002V01F01ADEQ',
  2348. '-',
  2349. 'Consulta',
  2350. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las áreas de la ocupación $trainOccupation de tren.",
  2351. $idUser,
  2352. $nowStr,
  2353. );
  2354. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2355. return $this->responseController->makeResponse(false, 'EXITO.', $carAreasArrDec[$trainOccupation]);
  2356. }
  2357. public function getTrainElements($idUser, $line) {
  2358. DB::enableQueryLog();
  2359. $idUser = $this->encryptionController->decrypt($idUser);
  2360. if(!$idUser){
  2361. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2362. }
  2363. $usr = DB::table('S002V01TUSUA')->where([
  2364. ['USUA_NULI', '=', $line],
  2365. ['USUA_IDUS', '=', $idUser],
  2366. ])->first();
  2367. if(is_null($usr)){
  2368. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2369. }
  2370. $ubic = $this->templatesUbic;
  2371. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  2372. $trainElementsEnc = file_get_contents($ubic . "train-elements.sam");
  2373. $trainElementsDec = $this->encryptionController->decrypt($trainElementsEnc);
  2374. $trainElementsArr = json_decode($trainElementsDec, true);
  2375. $now = $this->functionsController->now();
  2376. $nowStr = $now->toDateTimeString();
  2377. $actions = DB::getQueryLog();
  2378. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2379. $idac = $this->functionsController->registerActivity(
  2380. $line,
  2381. 'S002V01M07GEEQ',
  2382. 'S002V01F01ADEQ',
  2383. '-',
  2384. 'Consulta',
  2385. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los elementos de tren.",
  2386. $idUser,
  2387. $nowStr,
  2388. );
  2389. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2390. return $this->responseController->makeResponse(false, 'EXITO.', $trainElementsArr);
  2391. }
  2392. }