EquipmentManagementController.php 155 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658
  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. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  18. $this->templatesUbic = "C:\ITTEC\SAM\Dev\SistemaMantenimiento\sistema-mantenimiento-back\storage\app\public\pdf_templates\\01_05_GEEQ\\";
  19. } else {
  20. $this->templatesUbic = "C:\inetpub\wwwroot\sam\storage\app\public\pdf_templates\\01_05_GEEQ\\";
  21. }
  22. $this->documentManagementController = new DocumentManagementController();
  23. }
  24. public function registerFamily(Request $request) {
  25. DB::enableQueryLog();
  26. $validator = Validator::make($request->all(), [
  27. 'id_user' => 'required|string',
  28. 'linea' => 'required|integer',
  29. 'family_code' => 'required|string',
  30. 'family_name' => 'required|string|max:50',
  31. ]);
  32. if($validator->fails()){
  33. return $this->responseController->makeResponse(
  34. true,
  35. "Se encontraron uno o más errores.",
  36. $this->responseController->makeErrors(
  37. $validator->errors()->messages()
  38. ),
  39. 401
  40. );
  41. }
  42. $form = $request->all();
  43. $idUser = $this->encryptionController->decrypt($form['id_user']);
  44. if(!$idUser){
  45. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  46. }
  47. $usr = DB::table('S002V01TUSUA')->where([
  48. ['USUA_NULI', '=', $form['linea']],
  49. ['USUA_IDUS', '=', $idUser]
  50. ])->first();
  51. if(is_null($usr)){
  52. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  53. }
  54. $familyCode = $this->encryptionController->decrypt($form['family_code']);
  55. if(!$familyCode){
  56. return $this->responseController->makeResponse(true, 'El código de la familia no fue encriptado correctamente.', [], 400);
  57. }
  58. $family = DB::table('S002V01TFAMI')->where([
  59. ['FAMI_NULI', '=', $form['linea']],
  60. ['FAMI_COFA', '=', $familyCode],
  61. ])->get()->all();
  62. if(count($family) > 0){
  63. return $this->responseController->makeResponse(true, 'El código de la familia ya se encuentra registrado.', [], 401);
  64. }
  65. $now = $this->functionsController->now();
  66. $nowStr = $now->toDateTimeString();
  67. DB::table('S002V01TFAMI')->insert([
  68. 'FAMI_NULI' => $form['linea'],
  69. 'FAMI_COFA' => $familyCode,
  70. 'FAMI_NOFA' => $form['family_name'],
  71. 'FAMI_USRE' => $idUser,
  72. 'FAMI_FERE' => $nowStr,
  73. ]);
  74. $actions = DB::getQueryLog();
  75. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  76. $idac = $this->functionsController->registerActivity(
  77. $form['linea'],
  78. 'S002V01M07GEEQ',
  79. 'S002V01F02DFSU',
  80. 'S002V01P02REFA',
  81. 'Registro',
  82. "El usuario $name (" . $usr->USUA_IDUS . ") registró la familia $form[family_name].",
  83. $idUser,
  84. $nowStr,
  85. );
  86. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  87. return $this->responseController->makeResponse(false, 'EXITO.');
  88. }
  89. public function getFamilies($idUser, $line) {
  90. DB::enableQueryLog();
  91. $idUser = $this->encryptionController->decrypt($idUser);
  92. if(!$idUser){
  93. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  94. }
  95. $usr = DB::table('S002V01TUSUA')->where([
  96. ['USUA_NULI', '=', $line],
  97. ['USUA_IDUS', '=', $idUser],
  98. ])->first();
  99. if(is_null($usr)){
  100. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  101. }
  102. $families = DB::table('S002V01TFAMI')->select([
  103. 'FAMI_COFA AS CODIGOFAMILIA',
  104. 'FAMI_NOFA AS NOMBREFAMILIA',
  105. 'FAMI_ICON AS ICONO',
  106. 'FAMI_ICPE AS ICONO_PERSONALIZADO',
  107. 'FAMI_ESTA AS ESTADO',
  108. 'FAMI_USRE AS USRREG',
  109. 'FAMI_FERE AS FECREG',
  110. 'FAMI_USMO AS USRMOD',
  111. 'FAMI_FEMO AS FECMOD'
  112. ])->where('FAMI_NULI', '=', $line)->get()->all();
  113. foreach($families as $key=>$val){
  114. $usrReg = DB::table('S002V01TUSUA')->where([
  115. ['USUA_NULI', '=', $line],
  116. ['USUA_IDUS', '=', $val->USRREG],
  117. ])->first();
  118. $nameReg = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  119. $val->USRREG = $nameReg . " (" . $val->USRREG . ")";
  120. if($val->USRMOD != null){
  121. $usrMod = DB::table('S002V01TUSUA')->where([
  122. ['USUA_NULI', '=', $line],
  123. ['USUA_IDUS', '=', $val->USRMOD],
  124. ])->first();
  125. $nameMod = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  126. $val->USRMOD = $nameMod . " (" . $val->USRMOD . ")";
  127. }
  128. $subfamilies = DB::table('S002V01TSUBF')->where([
  129. ['SUBF_NULI', '=', $line],
  130. ['SUBF_COFA', '=', $val->CODIGOFAMILIA],
  131. ])->get()->all();
  132. $val->SUBFAMILIAS = count($subfamilies);
  133. $val->CODIGOFAMILIA = $this->encryptionController->encrypt($val->CODIGOFAMILIA);
  134. $families[$key] = $val;
  135. }
  136. $now = $this->functionsController->now();
  137. $nowStr = $now->toDateTimeString();
  138. $actions = DB::getQueryLog();
  139. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  140. $idac = $this->functionsController->registerActivity(
  141. $line,
  142. 'S002V01M07GEEQ',
  143. 'S002V01F02DFSU',
  144. 'S002V01P01COFA',
  145. 'Consulta',
  146. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las familias registradas.",
  147. $idUser,
  148. $nowStr,
  149. );
  150. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  151. return $this->responseController->makeResponse(false, 'EXITO.', $families);
  152. }
  153. public function updateFamily(Request $request) {
  154. DB::enableQueryLog();
  155. $validator = Validator::make($request->all(), [
  156. 'id_user' => 'required|string',
  157. 'linea' => 'required|integer',
  158. 'family_code' => 'required|string',
  159. 'family_name' => 'required|string|max:50',
  160. ]);
  161. if($validator->fails()){
  162. return $this->responseController->makeResponse(
  163. true,
  164. "Se encontraron uno o más errores.",
  165. $this->responseController->makeErrors(
  166. $validator->errors()->messages()
  167. ),
  168. 401
  169. );
  170. }
  171. $form = $request->all();
  172. $idUser = $this->encryptionController->decrypt($form['id_user']);
  173. if(!$idUser){
  174. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  175. }
  176. $usr = DB::table('S002V01TUSUA')->where([
  177. ['USUA_NULI', '=', $form['linea']],
  178. ['USUA_IDUS', '=', $idUser]
  179. ])->first();
  180. if(is_null($usr)){
  181. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  182. }
  183. $familyCode = $this->encryptionController->decrypt($form['family_code']);
  184. if(!$familyCode){
  185. return $this->responseController->makeResponse(true, 'El código de la familia no fue encriptado correctamente.', [], 400);
  186. }
  187. $family = DB::table('S002V01TFAMI')->where([
  188. ['FAMI_NULI', '=', $form['linea']],
  189. ['FAMI_COFA', '=', $familyCode],
  190. ])->first();
  191. if(is_null($family)){
  192. return $this->responseController->makeResponse(true, 'La familia que desea actualizar no existe.', [], 404);
  193. }else if($family->FAMI_ESTA == 'Eliminado'){
  194. return $this->responseController->makeResponse(true, 'La familia que desea actualizar está eliminada.', [], 404);
  195. }
  196. $now = $this->functionsController->now();
  197. $nowStr = $now->toDateTimeString();
  198. DB::table('S002V01TFAMI')->where([
  199. ['FAMI_NULI', '=', $form['linea']],
  200. ['FAMI_COFA', '=', $familyCode],
  201. ])->update([
  202. 'FAMI_NOFA' => $form['family_name'],
  203. 'FAMI_USMO' => $idUser,
  204. 'FAMI_FEMO' => $nowStr,
  205. ]);
  206. $actions = DB::getQueryLog();
  207. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  208. $idac = $this->functionsController->registerActivity(
  209. $form['linea'],
  210. 'S002V01M07GEEQ',
  211. 'S002V01F02DFSU',
  212. 'S002V01P02REFA',
  213. 'Actualización',
  214. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó la familia $familyCode.",
  215. $idUser,
  216. $nowStr,
  217. );
  218. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  219. return $this->responseController->makeResponse(false, 'EXITO.');
  220. }
  221. public function deleteFamily(Request $request) {
  222. DB::enableQueryLog();
  223. $validator = Validator::make($request->all(), [
  224. 'id_user' => 'required|string',
  225. 'linea' => 'required|integer',
  226. 'family_code' => 'required|string',
  227. ]);
  228. if($validator->fails()){
  229. return $this->responseController->makeResponse(
  230. true,
  231. "Se encontraron uno o más errores.",
  232. $this->responseController->makeErrors(
  233. $validator->errors()->messages()
  234. ),
  235. 401
  236. );
  237. }
  238. $form = $request->all();
  239. $idUser = $this->encryptionController->decrypt($form['id_user']);
  240. if(!$idUser){
  241. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  242. }
  243. $usr = DB::table('S002V01TUSUA')->where([
  244. ['USUA_NULI', '=', $form['linea']],
  245. ['USUA_IDUS', '=', $idUser]
  246. ])->first();
  247. if(is_null($usr)){
  248. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  249. }
  250. $familyCode = $this->encryptionController->decrypt($form['family_code']);
  251. if(!$familyCode){
  252. return $this->responseController->makeResponse(true, 'El código de la familia no fue encriptado correctamente.', [], 400);
  253. }
  254. $family = DB::table('S002V01TFAMI')->where([
  255. ['FAMI_NULI', '=', $form['linea']],
  256. ['FAMI_COFA', '=', $familyCode],
  257. ])->first();
  258. if(is_null($family)){
  259. return $this->responseController->makeResponse(true, 'La familia que desea eliminar no existe.', [], 404);
  260. }else if($family->FAMI_ESTA == 'Eliminado'){
  261. return $this->responseController->makeResponse(true, 'La familia que desea eliminar está eliminada.', [], 404);
  262. }
  263. $now = $this->functionsController->now();
  264. $nowStr = $now->toDateTimeString();
  265. DB::table('S002V01TFAMI')->where([
  266. ['FAMI_NULI', '=', $form['linea']],
  267. ['FAMI_COFA', '=', $familyCode],
  268. ])->update([
  269. 'FAMI_ESTA' => 'Eliminado',
  270. 'FAMI_USMO' => $idUser,
  271. 'FAMI_FEMO' => $nowStr,
  272. ]);
  273. $actions = DB::getQueryLog();
  274. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  275. $idac = $this->functionsController->registerActivity(
  276. $form['linea'],
  277. 'S002V01M07GEEQ',
  278. 'S002V01F02DFSU',
  279. 'S002V01P02REFA',
  280. 'Eliminación',
  281. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó la familia $familyCode.",
  282. $idUser,
  283. $nowStr,
  284. );
  285. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  286. return $this->responseController->makeResponse(false, 'EXITO.');
  287. }
  288. public function registerSubfamily(Request $request) {
  289. DB::enableQueryLog();
  290. $validator = Validator::make($request->all(), [
  291. 'id_user' => 'required|string',
  292. 'linea' => 'required|integer',
  293. 'family_code' => 'required|string',
  294. 'subfamily_code' => 'required|string',
  295. 'subfamily_name' => 'required|string|max:50',
  296. ]);
  297. if($validator->fails()){
  298. return $this->responseController->makeResponse(
  299. true,
  300. "Se encontraron uno o más errores.",
  301. $this->responseController->makeErrors(
  302. $validator->errors()->messages()
  303. ),
  304. 401
  305. );
  306. }
  307. $form = $request->all();
  308. $idUser = $this->encryptionController->decrypt($form['id_user']);
  309. if(!$idUser){
  310. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  311. }
  312. $usr = DB::table('S002V01TUSUA')->where([
  313. ['USUA_NULI', '=', $form['linea']],
  314. ['USUA_IDUS', '=', $idUser]
  315. ])->first();
  316. if(is_null($usr)){
  317. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  318. }
  319. $familyCode = $this->encryptionController->decrypt($form['family_code']);
  320. if(!$familyCode){
  321. return $this->responseController->makeResponse(true, 'El código de la familia relacionada no fue encriptado correctamente.', [], 400);
  322. }
  323. $family = DB::table('S002V01TFAMI')->where([
  324. ['FAMI_NULI', '=', $form['linea']],
  325. ['FAMI_COFA', '=', $familyCode],
  326. ])->first();
  327. if(is_null($family)){
  328. return $this->responseController->makeResponse(true, 'La familia relacionada no existe.', [], 404);
  329. }else if($family->FAMI_ESTA == 'Eliminado'){
  330. return $this->responseController->makeResponse(true, 'La familia relacionada está eliminada.', [], 404);
  331. }
  332. $subfamilyCode = $this->encryptionController->decrypt($form['subfamily_code']);
  333. if(!$subfamilyCode){
  334. return $this->responseController->makeResponse(true, 'El código de la subfamilia no fue encriptado correctamente.', [], 400);
  335. }
  336. $subfamily = DB::table('S002V01TSUBF')->where([
  337. ['SUBF_NULI', '=', $form['linea']],
  338. ['SUBF_COFA', '=', $familyCode],
  339. ['SUBF_COSU', '=', $subfamilyCode],
  340. ])->get()->all();
  341. if(count($subfamily) > 0){
  342. return $this->responseController->makeResponse(true, 'El código de la subfamilia ya se encuentra registrado.', [], 401);
  343. }
  344. $now = $this->functionsController->now();
  345. $nowStr = $now->toDateTimeString();
  346. DB::table('S002V01TSUBF')->insert([
  347. 'SUBF_NULI' => $form['linea'],
  348. 'SUBF_COFA' => $familyCode,
  349. 'SUBF_COSU' => $subfamilyCode,
  350. 'SUBF_NOSU' => $form['subfamily_name'],
  351. 'SUBF_USRE' => $idUser,
  352. 'SUBF_FERE' => $nowStr,
  353. ]);
  354. $actions = DB::getQueryLog();
  355. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  356. $idac = $this->functionsController->registerActivity(
  357. $form['linea'],
  358. 'S002V01M07GEEQ',
  359. 'S002V01F02DFSU',
  360. 'S002V01P04RESU',
  361. 'Registro',
  362. "El usuario $name (" . $usr->USUA_IDUS . ") registró la subfamilia $form[subfamily_name].",
  363. $idUser,
  364. $nowStr,
  365. );
  366. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  367. return $this->responseController->makeResponse(false, 'EXITO.');
  368. }
  369. public function getSubfamilies($familyCode, $idUser, $line) {
  370. DB::enableQueryLog();
  371. $idUser = $this->encryptionController->decrypt($idUser);
  372. if(!$idUser){
  373. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  374. }
  375. $usr = DB::table('S002V01TUSUA')->where([
  376. ['USUA_NULI', '=', $line],
  377. ['USUA_IDUS', '=', $idUser],
  378. ])->first();
  379. if(is_null($usr)){
  380. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  381. }
  382. $familyCode = $this->encryptionController->decrypt($familyCode);
  383. if(!$familyCode){
  384. return $this->responseController->makeResponse(true, 'El código de la familia relacionada no fue encriptado correctamente.', [], 400);
  385. }
  386. $family = DB::table('S002V01TFAMI')->where([
  387. ['FAMI_NULI', '=', $line],
  388. ['FAMI_COFA', '=', $familyCode],
  389. ])->first();
  390. if(is_null($family)){
  391. return $this->responseController->makeResponse(true, 'La familia relacionada no existe.', [], 404);
  392. }else if($family->FAMI_ESTA == 'Eliminado'){
  393. return $this->responseController->makeResponse(true, 'La familia relacionada está eliminada.', [], 404);
  394. }
  395. $subfamilies = DB::table('S002V01TSUBF')->select([
  396. 'SUBF_COFA AS CODIGOFAMILIA',
  397. 'SUBF_COSU AS CODIGOSUBFAMILIA',
  398. 'SUBF_NOSU AS NOMBRESUBFAMILIA',
  399. 'SUBF_ICON AS ICONO',
  400. 'SUBF_ICPE AS ICONO_PERSONALIZADO',
  401. 'SUBF_ESTA AS ESTADO',
  402. 'SUBF_USRE AS USRREG',
  403. 'SUBF_FERE AS FECREG',
  404. 'SUBF_USMO AS USRMOD',
  405. 'SUBF_FEMO AS FECMOD'
  406. ])->where([
  407. ['SUBF_NULI', '=', $line],
  408. ['SUBF_COFA', '=', $familyCode],
  409. ])->get()->all();
  410. foreach($subfamilies as $key=>$val){
  411. $val->CODIGOFAMILIA = $this->encryptionController->encrypt($val->CODIGOFAMILIA);
  412. $val->CODIGOSUBFAMILIA = $this->encryptionController->encrypt($val->CODIGOSUBFAMILIA);
  413. $usrReg = DB::table('S002V01TUSUA')->where([
  414. ['USUA_NULI', '=', $line],
  415. ['USUA_IDUS', '=', $val->USRREG],
  416. ])->first();
  417. $nameReg = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  418. $val->USRREG = $nameReg . " (" . $val->USRREG . ")";
  419. if($val->USRMOD != null){
  420. $usrMod = DB::table('S002V01TUSUA')->where([
  421. ['USUA_NULI', '=', $line],
  422. ['USUA_IDUS', '=', $val->USRMOD],
  423. ])->first();
  424. $nameMod = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  425. $val->USRMOD = $nameMod . " (" . $val->USRMOD . ")";
  426. }
  427. $families[$key] = $val;
  428. }
  429. $now = $this->functionsController->now();
  430. $nowStr = $now->toDateTimeString();
  431. $actions = DB::getQueryLog();
  432. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  433. $idac = $this->functionsController->registerActivity(
  434. $line,
  435. 'S002V01M07GEEQ',
  436. 'S002V01F02DFSU',
  437. 'S002V01P03COSU',
  438. 'Consulta',
  439. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las subfamilias relacionadas a la familia $familyCode.",
  440. $idUser,
  441. $nowStr,
  442. );
  443. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  444. return $this->responseController->makeResponse(false, 'EXITO.', $subfamilies);
  445. }
  446. public function updateSubfamily(Request $request) {
  447. DB::enableQueryLog();
  448. $validator = Validator::make($request->all(), [
  449. 'id_user' => 'required|string',
  450. 'linea' => 'required|integer',
  451. 'family_code' => 'required|string',
  452. 'subfamily_code' => 'required|string',
  453. 'subfamily_name' => 'required|string|max:50',
  454. ]);
  455. if($validator->fails()){
  456. return $this->responseController->makeResponse(
  457. true,
  458. "Se encontraron uno o más errores.",
  459. $this->responseController->makeErrors(
  460. $validator->errors()->messages()
  461. ),
  462. 401
  463. );
  464. }
  465. $form = $request->all();
  466. $idUser = $this->encryptionController->decrypt($form['id_user']);
  467. if(!$idUser){
  468. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  469. }
  470. $usr = DB::table('S002V01TUSUA')->where([
  471. ['USUA_NULI', '=', $form['linea']],
  472. ['USUA_IDUS', '=', $idUser]
  473. ])->first();
  474. if(is_null($usr)){
  475. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  476. }
  477. $familyCode = $this->encryptionController->decrypt($form['family_code']);
  478. if(!$familyCode){
  479. return $this->responseController->makeResponse(true, 'El código de la familia relacionada no fue encriptado correctamente.', [], 400);
  480. }
  481. $family = DB::table('S002V01TFAMI')->where([
  482. ['FAMI_NULI', '=', $form['linea']],
  483. ['FAMI_COFA', '=', $familyCode],
  484. ])->first();
  485. if(is_null($family)){
  486. return $this->responseController->makeResponse(true, 'La familia relacionada no existe.', [], 404);
  487. }else if($family->FAMI_ESTA == 'Eliminado'){
  488. return $this->responseController->makeResponse(true, 'La familia relacionada está eliminada.', [], 404);
  489. }
  490. $subfamilyCode = $this->encryptionController->decrypt($form['subfamily_code']);
  491. if(!$subfamilyCode){
  492. return $this->responseController->makeResponse(true, 'El código de la subfamilia no fue encriptado correctamente.', [], 400);
  493. }
  494. $subfamily = DB::table('S002V01TSUBF')->where([
  495. ['SUBF_NULI', '=', $form['linea']],
  496. ['SUBF_COFA', '=', $familyCode],
  497. ['SUBF_COSU', '=', $subfamilyCode],
  498. ])->first();
  499. if(is_null($subfamily)){
  500. return $this->responseController->makeResponse(true, 'La subfamilia que desea actualizar no existe.', [], 404);
  501. }else if($subfamily->SUBF_ESTA == 'Eliminado'){
  502. return $this->responseController->makeResponse(true, 'La subfamilia que desea actualizar está eliminada.', [], 404);
  503. }
  504. $now = $this->functionsController->now();
  505. $nowStr = $now->toDateTimeString();
  506. DB::table('S002V01TSUBF')->where([
  507. 'SUBF_NULI' => $form['linea'],
  508. 'SUBF_COFA' => $familyCode,
  509. 'SUBF_COSU' => $subfamilyCode,
  510. ])->update([
  511. 'SUBF_NOSU' => $form['subfamily_name'],
  512. 'SUBF_USMO' => $idUser,
  513. 'SUBF_FEMO' => $nowStr,
  514. ]);
  515. $actions = DB::getQueryLog();
  516. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  517. $idac = $this->functionsController->registerActivity(
  518. $form['linea'],
  519. 'S002V01M07GEEQ',
  520. 'S002V01F02DFSU',
  521. 'S002V01P04RESU',
  522. 'Actualización',
  523. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó la subfamilia $subfamilyCode de la familia $familyCode.",
  524. $idUser,
  525. $nowStr,
  526. );
  527. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  528. return $this->responseController->makeResponse(false, 'EXITO.');
  529. }
  530. public function deleteSubfamily(Request $request) {
  531. DB::enableQueryLog();
  532. $validator = Validator::make($request->all(), [
  533. 'id_user' => 'required|string',
  534. 'linea' => 'required|integer',
  535. 'family_code' => 'required|string',
  536. 'subfamily_code' => 'required|string',
  537. ]);
  538. if($validator->fails()){
  539. return $this->responseController->makeResponse(
  540. true,
  541. "Se encontraron uno o más errores.",
  542. $this->responseController->makeErrors(
  543. $validator->errors()->messages()
  544. ),
  545. 401
  546. );
  547. }
  548. $form = $request->all();
  549. $idUser = $this->encryptionController->decrypt($form['id_user']);
  550. if(!$idUser){
  551. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  552. }
  553. $usr = DB::table('S002V01TUSUA')->where([
  554. ['USUA_NULI', '=', $form['linea']],
  555. ['USUA_IDUS', '=', $idUser]
  556. ])->first();
  557. if(is_null($usr)){
  558. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  559. }
  560. $familyCode = $this->encryptionController->decrypt($form['family_code']);
  561. if(!$familyCode){
  562. return $this->responseController->makeResponse(true, 'El código de la familia relacionada no fue encriptado correctamente.', [], 400);
  563. }
  564. $family = DB::table('S002V01TFAMI')->where([
  565. ['FAMI_NULI', '=', $form['linea']],
  566. ['FAMI_COFA', '=', $familyCode],
  567. ])->first();
  568. if(is_null($family)){
  569. return $this->responseController->makeResponse(true, 'La familia relacionada no existe.', [], 404);
  570. }else if($family->FAMI_ESTA == 'Eliminado'){
  571. return $this->responseController->makeResponse(true, 'La familia relacionada está eliminada.', [], 404);
  572. }
  573. $subfamilyCode = $this->encryptionController->decrypt($form['subfamily_code']);
  574. if(!$subfamilyCode){
  575. return $this->responseController->makeResponse(true, 'El código de la subfamilia no fue encriptado correctamente.', [], 400);
  576. }
  577. $subfamily = DB::table('S002V01TSUBF')->where([
  578. ['SUBF_NULI', '=', $form['linea']],
  579. ['SUBF_COFA', '=', $familyCode],
  580. ['SUBF_COSU', '=', $subfamilyCode],
  581. ])->first();
  582. if(is_null($subfamily)){
  583. return $this->responseController->makeResponse(true, 'La subfamilia que desea eliminar no existe.', [], 404);
  584. }else if($subfamily->SUBF_ESTA == 'Eliminado'){
  585. return $this->responseController->makeResponse(true, 'La subfamilia que desea eliminar está eliminada.', [], 404);
  586. }
  587. $now = $this->functionsController->now();
  588. $nowStr = $now->toDateTimeString();
  589. DB::table('S002V01TSUBF')->where([
  590. 'SUBF_NULI' => $form['linea'],
  591. 'SUBF_COFA' => $familyCode,
  592. 'SUBF_COSU' => $subfamilyCode,
  593. ])->update([
  594. 'SUBF_ESTA' => 'Eliminado',
  595. 'SUBF_USMO' => $idUser,
  596. 'SUBF_FEMO' => $nowStr,
  597. ]);
  598. $actions = DB::getQueryLog();
  599. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  600. $idac = $this->functionsController->registerActivity(
  601. $form['linea'],
  602. 'S002V01M07GEEQ',
  603. 'S002V01F02DFSU',
  604. 'S002V01P04RESU',
  605. 'Eliminación',
  606. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó la subfamilia $subfamilyCode de la familia $familyCode.",
  607. $idUser,
  608. $nowStr,
  609. );
  610. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  611. return $this->responseController->makeResponse(false, 'EXITO.');
  612. }
  613. public function getLocations($idUser, $line) {
  614. DB::enableQueryLog();
  615. $idUser = $this->encryptionController->decrypt($idUser);
  616. if(!$idUser){
  617. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  618. }
  619. $usr = DB::table('S002V01TUSUA')->where([
  620. ['USUA_NULI', '=', $line],
  621. ['USUA_IDUS', '=', $idUser],
  622. ])->first();
  623. if(is_null($usr)){
  624. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  625. }
  626. $ubic = $this->templatesUbic;
  627. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  628. $locationsEnc = file_get_contents($ubic . "locations.sam");
  629. $locationsDec = $this->encryptionController->decrypt($locationsEnc);
  630. $locationsArr = json_decode($locationsDec, true);
  631. $now = $this->functionsController->now();
  632. $nowStr = $now->toDateTimeString();
  633. $actions = DB::getQueryLog();
  634. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  635. $idac = $this->functionsController->registerActivity(
  636. $line,
  637. 'S002V01M07GEEQ',
  638. 'S002V01F01ADEQ',
  639. '-',
  640. 'Consulta',
  641. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el arreglo de ubicaciones de la línea.",
  642. $idUser,
  643. $nowStr,
  644. );
  645. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  646. return $this->responseController->makeResponse(false, 'EXITO.', $locationsArr);
  647. }
  648. public function getOccupationAreas($idUser, $line) {
  649. DB::enableQueryLog();
  650. $idUser = $this->encryptionController->decrypt($idUser);
  651. if(!$idUser){
  652. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  653. }
  654. $usr = DB::table('S002V01TUSUA')->where([
  655. ['USUA_NULI', '=', $line],
  656. ['USUA_IDUS', '=', $idUser],
  657. ])->first();
  658. if(is_null($usr)){
  659. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  660. }
  661. $ubic = $this->templatesUbic;
  662. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  663. $occupationAreasEnc = file_get_contents($ubic . "occupation-areas.sam");
  664. $occupationAreasDec = $this->encryptionController->decrypt($occupationAreasEnc);
  665. $occupationAreasArr = json_decode($occupationAreasDec, true);
  666. $now = $this->functionsController->now();
  667. $nowStr = $now->toDateTimeString();
  668. $actions = DB::getQueryLog();
  669. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  670. $idac = $this->functionsController->registerActivity(
  671. $line,
  672. 'S002V01M07GEEQ',
  673. 'S002V01F01ADEQ',
  674. '-',
  675. 'Consulta',
  676. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las áreas de ocupación.",
  677. $idUser,
  678. $nowStr,
  679. );
  680. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  681. return $this->responseController->makeResponse(false, 'EXITO.', $occupationAreasArr);
  682. }
  683. public function registerOccupation(Request $request) {
  684. DB::enableQueryLog();
  685. $validator = Validator::make($request->all(), [
  686. 'id_user' => 'required|string',
  687. 'linea' => 'required|integer',
  688. 'area' => 'required|string',
  689. 'description' => 'required|string|min:15|max:150',
  690. ]);
  691. if($validator->fails()){
  692. return $this->responseController->makeResponse(
  693. true,
  694. "Se encontraron uno o más errores.",
  695. $this->responseController->makeErrors(
  696. $validator->errors()->messages()
  697. ),
  698. 401
  699. );
  700. }
  701. $form = $request->all();
  702. $idUser = $this->encryptionController->decrypt($form['id_user']);
  703. if(!$idUser){
  704. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  705. }
  706. $usr = DB::table('S002V01TUSUA')->where([
  707. ['USUA_NULI', '=', $form['linea']],
  708. ['USUA_IDUS', '=', $idUser]
  709. ])->first();
  710. if(is_null($usr)){
  711. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  712. }
  713. $ubic = $this->templatesUbic;
  714. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  715. $occupationAreasEnc = file_get_contents($ubic . "occupation-areas.sam");
  716. $occupationAreasDec = $this->encryptionController->decrypt($occupationAreasEnc);
  717. $occupationAreasArr = json_decode($occupationAreasDec, true);
  718. $occupationAreasArrDec = [];
  719. foreach($occupationAreasArr as $area){
  720. $occupationAreasArrDec[] = [
  721. 'CODE' => $this->encryptionController->decrypt($area['CODE']),
  722. 'AREA' => $this->encryptionController->decrypt($area['AREA'])
  723. ];
  724. }
  725. $areaCode = $this->encryptionController->decrypt($form['area']);
  726. if(!$areaCode){
  727. return $this->responseController->makeResponse(true, 'El área de ocupación no fue encriptada correctamente.', [], 400);
  728. }
  729. $areaCodeExists = array_filter($occupationAreasArrDec, function($v, $k) use ($areaCode) {
  730. return $v['CODE'] == $areaCode;
  731. }, ARRAY_FILTER_USE_BOTH);
  732. if(count($areaCodeExists) < 1){
  733. return $this->responseController->makeResponse(true, 'El área de ocupación seleccionada no existe.', [], 400);
  734. }
  735. $areaName = end($areaCodeExists)['AREA'] . " ($areaCode)";
  736. $lastOccupationByArea = DB::table('S002V01TOCUP')->where([
  737. ['OCUP_NULI', '=', $form['linea']],
  738. ['OCUP_AREA', '=', $areaCode]
  739. ])->orderBy('OCUP_IDOC', 'desc')->first();
  740. $id = 0;
  741. if(is_null($lastOccupationByArea)){
  742. $id = 1;
  743. }else{
  744. $id = $lastOccupationByArea->OCUP_IDOC + 1;
  745. }
  746. if($id > 999){
  747. return $this->responseController->makeResponse(true, "El área $areaName llegó al límite de 999 elementos.", [], 401);
  748. }
  749. $now = $this->functionsController->now();
  750. $nowStr = $now->toDateTimeString();
  751. DB::table('S002V01TOCUP')->insert([
  752. 'OCUP_NULI' => $form['linea'],
  753. 'OCUP_AREA' => $areaCode,
  754. 'OCUP_IDOC' => $id,
  755. 'OCUP_DESC' => $form['description'],
  756. 'OCUP_USRE' => $idUser,
  757. 'OCUP_FERE' => $nowStr,
  758. ]);
  759. $actions = DB::getQueryLog();
  760. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  761. $idac = $this->functionsController->registerActivity(
  762. $form['linea'],
  763. 'S002V01M07GEEQ',
  764. 'S002V01F01ADEQ',
  765. '-',
  766. 'Registro',
  767. "El usuario $name (" . $usr->USUA_IDUS . ") registró una nueva ocupación para el área $areaName.",
  768. $idUser,
  769. $nowStr,
  770. );
  771. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  772. return $this->responseController->makeResponse(false, 'EXITO.');
  773. }
  774. public function getOccupations($idUser, $line) {
  775. DB::enableQueryLog();
  776. $idUser = $this->encryptionController->decrypt($idUser);
  777. if(!$idUser){
  778. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  779. }
  780. $usr = DB::table('S002V01TUSUA')->where([
  781. ['USUA_NULI', '=', $line],
  782. ['USUA_IDUS', '=', $idUser],
  783. ])->first();
  784. if(is_null($usr)){
  785. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  786. }
  787. $ubic = $this->templatesUbic;
  788. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  789. $occupationAreasEnc = file_get_contents($ubic . "occupation-areas.sam");
  790. $occupationAreasDec = $this->encryptionController->decrypt($occupationAreasEnc);
  791. $occupationAreasArr = json_decode($occupationAreasDec, true);
  792. $occupationAreasArrDec = [];
  793. foreach($occupationAreasArr as $area){
  794. $occupationAreasArrDec[] = [
  795. 'CODE' => $this->encryptionController->decrypt($area['CODE']),
  796. 'AREA' => $this->encryptionController->decrypt($area['AREA'])
  797. ];
  798. }
  799. $occupations = DB::table('S002V01TOCUP')->where('OCUP_NULI', '=', $line)->get()->all();
  800. $occupationsFn = [];
  801. foreach($occupations as $occupation){
  802. $areaInd = $occupation->OCUP_AREA;
  803. $areaStr = $areaInd < 10 ? "0$areaInd" : "$areaInd";
  804. $area = array_filter($occupationAreasArrDec, function($v, $k) use ($areaStr) {
  805. return $v['CODE'] == $areaStr;
  806. }, ARRAY_FILTER_USE_BOTH);
  807. $areaName = end($area)['AREA'] . " (" . end($area)['CODE'] . ")";
  808. $occupationInd = "" . $occupation->OCUP_IDOC . "";
  809. $occupationStr = "";
  810. for($i = strlen($occupationInd); $i < 3; $i++){
  811. $occupationStr .= '0';
  812. }
  813. $usrReg = DB::table('S002V01TUSUA')->where([
  814. ['USUA_NULI', '=', $line],
  815. ['USUA_IDUS', '=', $occupation->OCUP_USRE],
  816. ])->first();
  817. $usrRegName = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA) . " (" . $occupation->OCUP_USRE . ")";
  818. $usrModName = null;
  819. if($occupation->OCUP_USMO != null){
  820. $usrMod = DB::table('S002V01TUSUA')->where([
  821. ['USUA_NULI', '=', $line],
  822. ['USUA_IDUS', '=', $occupation->OCUP_USMO],
  823. ])->first();
  824. $usrModName = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA) . " (" . $occupation->OCUP_USMO . ")";
  825. }
  826. $occupationStr = $areaStr . $occupationStr . $occupationInd;
  827. $occupationsFn[] = [
  828. "NOMBREAREA" => $areaName,
  829. "OCUPACION" => $occupationStr,
  830. "DESCRIPCION" => $occupation->OCUP_DESC,
  831. "ESTADO" => $occupation->OCUP_ESTA,
  832. "USRREG" => $usrRegName,
  833. "FECREG" => $occupation->OCUP_FERE,
  834. "USRMOD" => $usrModName,
  835. "FECMOD" => $occupation->OCUP_FEMO,
  836. ];
  837. }
  838. $now = $this->functionsController->now();
  839. $nowStr = $now->toDateTimeString();
  840. $actions = DB::getQueryLog();
  841. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  842. $idac = $this->functionsController->registerActivity(
  843. $line,
  844. 'S002V01M07GEEQ',
  845. 'S002V01F01ADEQ',
  846. '-',
  847. 'Consulta',
  848. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las ocupaciones registradas.",
  849. $idUser,
  850. $nowStr,
  851. );
  852. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  853. return $this->responseController->makeResponse(false, 'EXITO.', $occupationsFn);
  854. }
  855. public function updateOccupation(Request $request) {
  856. DB::enableQueryLog();
  857. $validator = Validator::make($request->all(), [
  858. 'id_user' => 'required|string',
  859. 'linea' => 'required|integer',
  860. 'area' => 'required|string',
  861. 'occupation' => 'required|string',
  862. 'description' => 'required|string|min:15|max:150',
  863. ]);
  864. if($validator->fails()){
  865. return $this->responseController->makeResponse(
  866. true,
  867. "Se encontraron uno o más errores.",
  868. $this->responseController->makeErrors(
  869. $validator->errors()->messages()
  870. ),
  871. 401
  872. );
  873. }
  874. $form = $request->all();
  875. $idUser = $this->encryptionController->decrypt($form['id_user']);
  876. if(!$idUser){
  877. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  878. }
  879. $usr = DB::table('S002V01TUSUA')->where([
  880. ['USUA_NULI', '=', $form['linea']],
  881. ['USUA_IDUS', '=', $idUser]
  882. ])->first();
  883. if(is_null($usr)){
  884. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  885. }
  886. $ubic = $this->templatesUbic;
  887. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  888. $occupationAreasEnc = file_get_contents($ubic . "occupation-areas.sam");
  889. $occupationAreasDec = $this->encryptionController->decrypt($occupationAreasEnc);
  890. $occupationAreasArr = json_decode($occupationAreasDec, true);
  891. $occupationAreasArrDec = [];
  892. foreach($occupationAreasArr as $area){
  893. $occupationAreasArrDec[] = [
  894. 'CODE' => $this->encryptionController->decrypt($area['CODE']),
  895. 'AREA' => $this->encryptionController->decrypt($area['AREA'])
  896. ];
  897. }
  898. $areaCode = $this->encryptionController->decrypt($form['area']);
  899. if(!$areaCode){
  900. return $this->responseController->makeResponse(true, 'El área de ocupación no fue encriptada correctamente.', [], 400);
  901. }
  902. $areaCodeExists = array_filter($occupationAreasArrDec, function($v, $k) use ($areaCode) {
  903. return $v['CODE'] == $areaCode;
  904. }, ARRAY_FILTER_USE_BOTH);
  905. if(count($areaCodeExists) < 1){
  906. return $this->responseController->makeResponse(true, 'El área de ocupación relacionada no existe.', [], 400);
  907. }
  908. $occupation = $this->encryptionController->decrypt($form['occupation']);
  909. if(!$occupation){
  910. return $this->responseController->makeResponse(true, 'La ocupación no fue encriptada correctamente.', [], 400);
  911. }
  912. $occupationID = substr($occupation, 2);
  913. $occupationObj = DB::table('S002V01TOCUP')->where([
  914. ['OCUP_NULI', '=', $form['linea']],
  915. ['OCUP_AREA', '=', $areaCode],
  916. ['OCUP_IDOC', '=', $occupationID],
  917. ])->first();
  918. if(is_null($occupationObj)){
  919. return $this->responseController->makeResponse(true, 'La ocupación que desea actualizar no existe.', [], 404);
  920. }
  921. $now = $this->functionsController->now();
  922. $nowStr = $now->toDateTimeString();
  923. DB::table('S002V01TOCUP')->where([
  924. ['OCUP_NULI', '=', $form['linea']],
  925. ['OCUP_AREA', '=', $areaCode],
  926. ['OCUP_IDOC', '=', $occupationID],
  927. ])->update([
  928. 'OCUP_DESC' => $form['description'],
  929. 'OCUP_USMO' => $idUser,
  930. 'OCUP_FEMO' => $nowStr,
  931. ]);
  932. $actions = DB::getQueryLog();
  933. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  934. $idac = $this->functionsController->registerActivity(
  935. $form['linea'],
  936. 'S002V01M07GEEQ',
  937. 'S002V01F01ADEQ',
  938. '-',
  939. 'Actualización',
  940. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó la ocupación $occupation.",
  941. $idUser,
  942. $nowStr,
  943. );
  944. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  945. return $this->responseController->makeResponse(false, 'EXITO.');
  946. }
  947. public function deleteOccupation(Request $request) {
  948. DB::enableQueryLog();
  949. $validator = Validator::make($request->all(), [
  950. 'id_user' => 'required|string',
  951. 'linea' => 'required|integer',
  952. 'area' => 'required|string',
  953. 'occupation' => 'required|string',
  954. ]);
  955. if($validator->fails()){
  956. return $this->responseController->makeResponse(
  957. true,
  958. "Se encontraron uno o más errores.",
  959. $this->responseController->makeErrors(
  960. $validator->errors()->messages()
  961. ),
  962. 401
  963. );
  964. }
  965. $form = $request->all();
  966. $idUser = $this->encryptionController->decrypt($form['id_user']);
  967. if(!$idUser){
  968. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  969. }
  970. $usr = DB::table('S002V01TUSUA')->where([
  971. ['USUA_NULI', '=', $form['linea']],
  972. ['USUA_IDUS', '=', $idUser]
  973. ])->first();
  974. if(is_null($usr)){
  975. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  976. }
  977. $ubic = $this->templatesUbic;
  978. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  979. $occupationAreasEnc = file_get_contents($ubic . "occupation-areas.sam");
  980. $occupationAreasDec = $this->encryptionController->decrypt($occupationAreasEnc);
  981. $occupationAreasArr = json_decode($occupationAreasDec, true);
  982. $occupationAreasArrDec = [];
  983. foreach($occupationAreasArr as $area){
  984. $occupationAreasArrDec[] = [
  985. 'CODE' => $this->encryptionController->decrypt($area['CODE']),
  986. 'AREA' => $this->encryptionController->decrypt($area['AREA'])
  987. ];
  988. }
  989. $areaCode = $this->encryptionController->decrypt($form['area']);
  990. if(!$areaCode){
  991. return $this->responseController->makeResponse(true, 'El área de ocupación no fue encriptada correctamente.', [], 400);
  992. }
  993. $areaCodeExists = array_filter($occupationAreasArrDec, function($v, $k) use ($areaCode) {
  994. return $v['CODE'] == $areaCode;
  995. }, ARRAY_FILTER_USE_BOTH);
  996. if(count($areaCodeExists) < 1){
  997. return $this->responseController->makeResponse(true, 'El área de ocupación relacionada no existe.', [], 400);
  998. }
  999. $occupation = $this->encryptionController->decrypt($form['occupation']);
  1000. if(!$occupation){
  1001. return $this->responseController->makeResponse(true, 'La ocupación no fue encriptada correctamente.', [], 400);
  1002. }
  1003. $occupationID = substr($occupation, 2);
  1004. $occupationObj = DB::table('S002V01TOCUP')->where([
  1005. ['OCUP_NULI', '=', $form['linea']],
  1006. ['OCUP_AREA', '=', $areaCode],
  1007. ['OCUP_IDOC', '=', $occupationID],
  1008. ])->first();
  1009. if(is_null($occupationObj)){
  1010. return $this->responseController->makeResponse(true, 'La ocupación que desea eliminar no existe.', [], 404);
  1011. }
  1012. $now = $this->functionsController->now();
  1013. $nowStr = $now->toDateTimeString();
  1014. DB::table('S002V01TOCUP')->where([
  1015. ['OCUP_NULI', '=', $form['linea']],
  1016. ['OCUP_AREA', '=', $areaCode],
  1017. ['OCUP_IDOC', '=', $occupationID],
  1018. ])->update([
  1019. 'OCUP_ESTA' => 'Eliminado',
  1020. 'OCUP_USMO' => $idUser,
  1021. 'OCUP_FEMO' => $nowStr,
  1022. ]);
  1023. $actions = DB::getQueryLog();
  1024. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1025. $idac = $this->functionsController->registerActivity(
  1026. $form['linea'],
  1027. 'S002V01M07GEEQ',
  1028. 'S002V01F01ADEQ',
  1029. '-',
  1030. 'Eliminación',
  1031. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó la ocupación $occupation.",
  1032. $idUser,
  1033. $nowStr,
  1034. );
  1035. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1036. return $this->responseController->makeResponse(false, 'EXITO.');
  1037. }
  1038. public function saveEquipmentPreCodified(Request $request) {
  1039. DB::enableQueryLog();
  1040. $validator = Validator::make($request->all(), [
  1041. 'id_user' => 'required|string',
  1042. 'linea' => 'required|integer',
  1043. 'hierarchy' => 'required|string|in:Padre,Hijo',
  1044. 'parent_equipment' => 'required_if:hierarchy,=,Hijo|string',
  1045. 'code_type' => 'required|integer|between:1,6',
  1046. 'origin_location' => 'required|string',
  1047. 'origin_level' => 'required|string',
  1048. 'origin_occupation' => 'required|string',
  1049. 'origin_element' => 'required|string|max:150',
  1050. 'family' => 'required|string',
  1051. 'subfamily' => 'required|string',
  1052. 'status' => 'required|string',
  1053. 'equipment_type' => 'required|string|max:150',
  1054. 'equipment_model' => 'required|string|max:75',
  1055. 'images' => 'required|json',
  1056. 'documents' => 'required|json',
  1057. 'acquisition_date' => 'required|date',
  1058. 'warranty_start_date' => 'required|date',
  1059. 'warranty_end_date' => 'required|date',
  1060. 'equipment_provider' => 'required|string',
  1061. 'serial_number' => 'required|string|max:150',
  1062. 'has_software' => 'required|string|in:Si,No',
  1063. 'software_name' => 'required_if:has_software,=,Si|string|max:150',
  1064. 'software_version' => 'required_if:has_software,=,Si|string|max:75',
  1065. 'pre_generated_code' => 'required|string',
  1066. 'pcc_intersection' => 'required_if:code_type,=,2|string|max:5',
  1067. 'pcc_position' => 'required_if:code_type,=,2|string|max:2',
  1068. 'origin_kilometer' => 'required_if:code_type,=,3|numeric',
  1069. 'destiny_location' => 'required_if:code_type,=,3|string',
  1070. 'destiny_level' => 'required_if:code_type,=,3|string',
  1071. 'destiny_occupation' => 'required_if:code_type,=,3|string',
  1072. 'destiny_element' => 'required_if:code_type,=,3|string|max:150',
  1073. 'destiny_kilometer' => 'required_if:code_type,=,3|string',
  1074. ]);
  1075. if($validator->fails()){
  1076. return $this->responseController->makeResponse(
  1077. true,
  1078. "Se encontraron uno o más errores.",
  1079. $this->responseController->makeErrors(
  1080. $validator->errors()->messages()
  1081. ),
  1082. 401
  1083. );
  1084. }
  1085. $form = $request->all();
  1086. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1087. if(!$idUser){
  1088. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1089. }
  1090. $usr = DB::table('S002V01TUSUA')->where([
  1091. ['USUA_NULI', '=', $form['linea']],
  1092. ['USUA_IDUS', '=', $idUser]
  1093. ])->first();
  1094. if(is_null($usr)){
  1095. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1096. }
  1097. $eqpa = null;
  1098. if($form['hierarchy'] == 'Hijo'){
  1099. $eqpa = $this->encryptionController->decrypt($form['parent_equipment']);
  1100. if(!$eqpa){
  1101. return $this->responseController->makeResponse(true, 'El ID del equipamiento padre no fue encriptado correctamente.', [], 400);
  1102. }
  1103. $parentEquipment = DB::table('S002V01TEQUI')->where([
  1104. ['EQUI_NULI', '=', $form['linea']],
  1105. ['EQUI_COEQ', '=', $eqpa],
  1106. ])->first();
  1107. if(is_null($parentEquipment)){
  1108. return $this->responseController->makeResponse(true, 'El equipamiento padre no existe.', [], 404);
  1109. }
  1110. }
  1111. $ubic = $this->templatesUbic;
  1112. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  1113. $locationsEnc = file_get_contents($ubic . "locations.sam");
  1114. $locationsDec = $this->encryptionController->decrypt($locationsEnc);
  1115. $locationsArr = json_decode($locationsDec, true);
  1116. $locationsArrDec = [];
  1117. foreach($locationsArr as $location){
  1118. $locationStr = $this->encryptionController->decrypt($location['LOCATION']);
  1119. $code = $this->encryptionController->decrypt($location['CODE']);
  1120. $locationsArrDec[] = [
  1121. 'LOCATION' => $locationStr,
  1122. 'CODE' => $code
  1123. ];
  1124. }
  1125. $originLocation = $this->encryptionController->decrypt($form['origin_location']);
  1126. if(!$originLocation){
  1127. return $this->responseController->makeResponse(true, 'La ubicación de origen no fue encriptada correctamente.', [], 400);
  1128. }
  1129. $originLocationFilt = array_filter($locationsArrDec, function($v, $k) use ($originLocation) {
  1130. return $v['CODE'] == $originLocation;
  1131. }, ARRAY_FILTER_USE_BOTH);
  1132. if(count($originLocationFilt) < 1){
  1133. return $this->responseController->makeResponse(true, 'La ubicación de origen no se encuentra registrada en el sistema.', [], 404);
  1134. }
  1135. $originLevel = $this->encryptionController->decrypt($form['origin_level']);
  1136. if(!$originLevel){
  1137. return $this->responseController->makeResponse(true, 'El nivel de origen no fue encriptado correctamente.', [], 400);
  1138. }
  1139. $supportedLevels = [];
  1140. for($i = 5; $i > 0; $i--){
  1141. $supportedLevels[] = "S0$i";
  1142. }
  1143. for($i = 0; $i <= 15; $i++){
  1144. $tag = $i < 10 ? "P0$i" : "P$i";
  1145. $supportedLevels[] = $tag;
  1146. }
  1147. if(!in_array($originLevel, $supportedLevels)){
  1148. return $this->responseController->makeResponse(true, 'El nivel de origen seleccionado es inválido.', [], 400);
  1149. }
  1150. $originOccupationStr = $this->encryptionController->decrypt($form['origin_occupation']);
  1151. if(!$originOccupationStr){
  1152. return $this->responseController->makeResponse(true, 'La ocupación de origen no fue encriptada correctamente.', [], 400);
  1153. }
  1154. $originOccupationArea = substr($originOccupationStr, 0, 2);
  1155. $originOccupationID = substr($originOccupationStr, 2);
  1156. $originOccupation = DB::table('S002V01TOCUP')->where([
  1157. ['OCUP_NULI', '=', $form['linea']],
  1158. ['OCUP_AREA', '=', $originOccupationArea],
  1159. ['OCUP_IDOC', '=', $originOccupationID]
  1160. ])->first();
  1161. if(is_null($originOccupation)){
  1162. return $this->responseController->makeResponse(true, 'La ocupación de origen seleccionada no existe.', [], 404);
  1163. }
  1164. $familyCode = $this->encryptionController->decrypt($form['family']);
  1165. if(!$familyCode){
  1166. return $this->responseController->makeResponse(true, 'La familia del equipamiento no fue encriptada correctamente.', [], 400);
  1167. }
  1168. $family = DB::table('S002V01TFAMI')->where([
  1169. ['FAMI_NULI', '=', $form['linea']],
  1170. ['FAMI_COFA', '=', $familyCode]
  1171. ])->first();
  1172. if(is_null($family)){
  1173. return $this->responseController->makeResponse(true, 'La familia seleccionada no existe.', [], 404);
  1174. }
  1175. $subfamilyCode = $this->encryptionController->decrypt($form['subfamily']);
  1176. if(!$subfamilyCode){
  1177. return $this->responseController->makeResponse(true, 'La subfamilia del equipamiento no fue encriptada correctamente.', [], 400);
  1178. }
  1179. $subfamily = DB::table('S002V01TSUBF')->where([
  1180. ['SUBF_NULI', '=', $form['linea']],
  1181. ['SUBF_COFA', '=', $familyCode],
  1182. ['SUBF_COSU', '=', $subfamilyCode]
  1183. ])->first();
  1184. if(is_null($subfamily)){
  1185. return $this->responseController->makeResponse(true, 'La subfamilia seleccionada no existe.', [], 404);
  1186. }
  1187. $validStatus = ["A", "S", "T", "I", "R", "D"];
  1188. $status = $this->encryptionController->decrypt($form['status']);
  1189. if(!$status){
  1190. return $this->responseController->makeResponse(true, 'La estado del equipamiento no fue encriptado correctamente.', [], 400);
  1191. }else if(!in_array($status, $validStatus)){
  1192. return $this->responseController->makeResponse(true, 'El estado seleccionado para el equipamiento es inválido.', [], 400);
  1193. }
  1194. $imagesArr = json_decode($form['images'], true);
  1195. if(count($imagesArr) < 1){
  1196. return $this->responseController->makeResponse(true, 'El arreglo de la galería de imágenes está vacío.', [], 400);
  1197. }
  1198. $imagesGallery = [];
  1199. foreach($imagesArr as $imageFile){
  1200. if(!array_key_exists('type', $imageFile)){
  1201. return $this->responseController->makeResponse(true, 'El arreglo de la galería de imágenes tiene un formato inválido.', [], 400);
  1202. }else if($imageFile['type'] == 'Nuevo'){
  1203. $tempFileID = $this->encryptionController->decrypt($imageFile['id']);
  1204. $tempFile = DB::table('S002V01TARTE')->where([
  1205. ['ARTE_IDAR', '=', $tempFileID],
  1206. ['ARTE_NULI', '=', $form['linea']]
  1207. ])->first();
  1208. $finalFile = $this->documentManagementController->moveFinalFile($form['linea'], 'GEEQ', 'FO', $tempFile, $idUser);
  1209. if(!$finalFile[0]){
  1210. return $this->responseController->makeResponse(true, $finalFile[1], [], 400);
  1211. }
  1212. $imagesGallery[] = $finalFile[1];
  1213. }else{
  1214. $fileID = $this->encryptionController->decrypt($imageFile['id']);
  1215. $imagesGallery[] = $fileID;
  1216. }
  1217. }
  1218. $imagesGalleryStr = json_encode($imagesGallery);
  1219. $documentsArr = json_decode($form['documents'], true);
  1220. if(count($documentsArr) < 1){
  1221. return $this->responseController->makeResponse(true, 'El arreglo de documentos relacionados está vacío.', [], 400);
  1222. }
  1223. $documents = [];
  1224. foreach($documentsArr as $documentFile){
  1225. if(!array_key_exists('type', $documentFile)){
  1226. return $this->responseController->makeResponse(true, 'El arreglo de documentos asociados tiene un formato inválido.', [], 400);
  1227. }else if($documentFile['type'] == 'Nuevo'){
  1228. $tempFileID = $this->encryptionController->decrypt($documentFile['id']);
  1229. $tempFile = DB::table('S002V01TARTE')->where([
  1230. ['ARTE_IDAR', '=', $tempFileID],
  1231. ['ARTE_NULI', '=', $form['linea']]
  1232. ])->first();
  1233. $finalFile = $this->documentManagementController->moveFinalFile($form['linea'], 'GEEQ', 'IN', $tempFile, $idUser);
  1234. if(!$finalFile[0]){
  1235. return $this->responseController->makeResponse(true, $finalFile[1], [], 400);
  1236. }
  1237. $documents[] = $finalFile[1];
  1238. }else{
  1239. $fileID = $this->encryptionController->decrypt($documentFile['id']);
  1240. $documents[] = $fileID;
  1241. }
  1242. }
  1243. $documentsStr = json_encode($documents);
  1244. $acquisitionDate = new Carbon($form['acquisition_date']);
  1245. $warrantyStartDate = new Carbon($form['warranty_start_date']);
  1246. $warrantyEndDate = new Carbon($form['warranty_end_date']);
  1247. if($warrantyStartDate->lt($acquisitionDate)){
  1248. return $this->responseController->makeResponse(true, 'La fecha de inicio de la grantía no puede ser menor a la fecha de adquisición.', [], 400);
  1249. }else if($warrantyEndDate->lte($acquisitionDate)){
  1250. 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);
  1251. }
  1252. $providerID = $this->encryptionController->decrypt($form['equipment_provider']);
  1253. if(!$providerID){
  1254. return $this->responseController->makeResponse(true, 'El proveedor del equipamiento no fue encriptado correctamente.', [], 400);
  1255. }
  1256. $provider = DB::table('S002V01TPROV')->where([
  1257. ['PROV_NUPR', '=', $providerID],
  1258. ['PROV_NULI', '=', $form['linea']]
  1259. ])->first();
  1260. if(is_null($provider)){
  1261. return $this->responseController->makeResponse(true, 'La proveedor seleccionada no existe.', [], 404);
  1262. }
  1263. $serialNumer = $this->encryptionController->decrypt($form['serial_number']);
  1264. if(!$serialNumer){
  1265. return $this->responseController->makeResponse(true, 'El número de serie del equipamiento no fue encriptado correctamente.', [], 400);
  1266. }
  1267. $serialNmberDB = DB::table('S002V01TEQUI')->where([
  1268. ['EQUI_NULI', '=', $form['linea']],
  1269. ['EQUI_NUSE', '=', $serialNumer]
  1270. ])->first();
  1271. if(!is_null($serialNmberDB)){
  1272. $serialNumberStr = $serialNmberDB->EQUI_COEQ;
  1273. return $this->responseController->makeResponse(true, "El número de serie enviado ya se encuentra relacionado al equipamiento $serialNumberStr.", [], 401);
  1274. }
  1275. $preCode = $this->encryptionController->decrypt($form['pre_generated_code']);
  1276. if(!$preCode){
  1277. return $this->responseController->makeResponse(true, 'El código pre generado no fue encriptado correctamente.', [], 400);
  1278. }
  1279. $codeVerified = $this->verifyPreCode([
  1280. "line" => $form['linea'],
  1281. "originLocation" => $originLocation,
  1282. "originLevel" => $originLevel,
  1283. "originOccupation" => $originOccupationStr,
  1284. "originElement" => $form['origin_element'],
  1285. "family" => $familyCode,
  1286. "subfamily" => $subfamilyCode,
  1287. "status" => $status,
  1288. "equipmentType" => $form['equipment_type'],
  1289. "equipmentModel" => $form['equipment_model'],
  1290. "equipmentID" => "XXXXX",
  1291. "pccIntersection" => null,
  1292. "pccPosition" => null,
  1293. "originKilometers" => null,
  1294. "destinyLocation" => null,
  1295. "destinyLevel" => null,
  1296. "destinyOccupation" => null,
  1297. "destinyElement" => null,
  1298. "destinyKilometers" => null
  1299. ], $form['code_type'], $form['hierarchy'], $eqpa);
  1300. if($codeVerified != $preCode){
  1301. 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);
  1302. }
  1303. $deso = isset($form['software_name']) ? $form['software_name'] : null;
  1304. $veso = isset($form['software_version']) ? $form['software_version'] : null;
  1305. $now = $this->functionsController->now();
  1306. $nowStr = $now->toDateTimeString();
  1307. DB::table('S002V01TPCEQ')->insert([
  1308. 'PCEQ_NULI' => $form['linea'],
  1309. 'PCEQ_CPGE' => $preCode,
  1310. 'PCEQ_JERA' => $form['hierarchy'],
  1311. 'PCEQ_EQPA' => $eqpa,
  1312. 'PCEQ_TICO' => $form['code_type'],
  1313. 'PCEQ_UBOR' => $originLocation,
  1314. 'PCEQ_NIOR' => $originLevel,
  1315. 'PCEQ_OCOR' => $originOccupationStr,
  1316. 'PCEQ_ELOR' => $form['origin_element'],
  1317. 'PCEQ_FAMI' => $familyCode,
  1318. 'PCEQ_SUBF' => $subfamilyCode,
  1319. 'PCEQ_ESEQ' => $status,
  1320. 'PCEQ_TIEQ' => $form['equipment_type'],
  1321. 'PCEQ_MOEQ' => $form['equipment_model'],
  1322. 'PCEQ_IPCC' => null,
  1323. 'PCEQ_PPCC' => null,
  1324. 'PCEQ_KIOR' => 0.0,
  1325. 'PCEQ_UBDE' => '',
  1326. 'PCEQ_NIDE' => '',
  1327. 'PCEQ_OCDE' => '',
  1328. 'PCEQ_ELDE' => '',
  1329. 'PCEQ_KIDE' => 0.0,
  1330. 'PCEQ_FEAD' => $form['acquisition_date'],
  1331. 'PCEQ_FIGA' => $form['warranty_start_date'],
  1332. 'PCEQ_FTGA' => $form['warranty_end_date'],
  1333. 'PCEQ_PREQ' => $providerID,
  1334. 'PCEQ_NUSE' => $serialNumer,
  1335. 'PCEQ_DESO' => $deso,
  1336. 'PCEQ_VESO' => $veso,
  1337. 'PCEQ_GAIM' => $imagesGalleryStr,
  1338. 'PCEQ_DORE' => $documentsStr,
  1339. 'PCEQ_USRE' => $idUser,
  1340. 'PCEQ_FERE' => $nowStr,
  1341. ]);
  1342. $actions = DB::getQueryLog();
  1343. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1344. $idac = $this->functionsController->registerActivity(
  1345. $form['linea'],
  1346. 'S002V01M07GEEQ',
  1347. 'S002V01F01ADEQ',
  1348. 'S002V01P11REEQ',
  1349. 'Registro',
  1350. "El usuario $name (" . $usr->USUA_IDUS . ") solicitó el registro del equipamiento $preCode.",
  1351. $idUser,
  1352. $nowStr,
  1353. );
  1354. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1355. return $this->responseController->makeResponse(false, 'EXITO.');
  1356. }
  1357. public function saveEquipmentPreCodifiedStock(Request $request) {
  1358. DB::enableQueryLog();
  1359. $validator = Validator::make($request->all(), [
  1360. 'id_user' => 'required|string',
  1361. 'linea' => 'required|integer',
  1362. 'hierarchy' => 'required|string|in:Padre,Hijo',
  1363. 'parent_equipment' => 'required_if:hierarchy,=,Hijo|string',
  1364. 'code_type' => 'required|integer|between:1,7',
  1365. 'origin_location' => 'required_unless:code_type,7|string|nullable',
  1366. 'origin_level' => 'required_unless:code_type,7|string|nullable',
  1367. 'origin_occupation' => 'required_unless:code_type,7|string|nullable',
  1368. 'origin_element' => 'required_unless:code_type,7|string|max:150|nullable',
  1369. 'family' => 'required|string',
  1370. 'subfamily' => 'required|string',
  1371. 'status' => 'required|string',
  1372. 'equipment_type' => 'required|string|max:150',
  1373. 'equipment_model' => 'required|string|max:75',
  1374. 'images' => 'required|json',
  1375. 'documents' => 'required|json',
  1376. 'acquisition_date' => 'required|date',
  1377. 'warranty_start_date' => 'required|date',
  1378. 'warranty_end_date' => 'required|date',
  1379. 'equipment_provider' => 'required|string',
  1380. 'serial_number' => 'nullable|string',
  1381. 'has_software' => 'required|string|in:Si,No',
  1382. 'software_name' => 'required_if:has_software,=,Si|string|max:150',
  1383. 'software_version' => 'required_if:has_software,=,Si|string|max:75',
  1384. 'pre_generated_code' => 'required|string',
  1385. 'pcc_intersection' => 'required_if:code_type,=,2|string|max:5',
  1386. 'pcc_position' => 'required_if:code_type,=,2|string|max:2',
  1387. 'origin_kilometer' => 'required_if:code_type,=,3|numeric',
  1388. 'destiny_location' => 'required_if:code_type,=,3|string',
  1389. 'destiny_level' => 'required_if:code_type,=,3|string',
  1390. 'destiny_occupation' => 'required_if:code_type,=,3|string',
  1391. 'destiny_element' => 'required_if:code_type,=,3|string|max:150',
  1392. 'destiny_kilometer' => 'required_if:code_type,=,3|string',
  1393. 'warehouse' => 'required_if:code_type,=,7|string',
  1394. 'area' => 'required_if:code_type,=,7|string',
  1395. 'level' => 'required_if:code_type,=,7|string',
  1396. 'zone' => 'required_if:code_type,=,7|string',
  1397. 'number_items' => 'required|integer',
  1398. ]);
  1399. if($validator->fails()){
  1400. return $this->responseController->makeResponse(
  1401. true,
  1402. "Se encontraron uno o más errores.",
  1403. $this->responseController->makeErrors(
  1404. $validator->errors()->messages()
  1405. ),
  1406. 401
  1407. );
  1408. }
  1409. $form = $request->all();
  1410. // Desencriptación del usuario
  1411. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1412. if(!$idUser){
  1413. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1414. }
  1415. // Obtención del usuario
  1416. $usr = DB::table('S002V01TUSUA')->where([
  1417. ['USUA_NULI', '=', $form['linea']],
  1418. ['USUA_IDUS', '=', $idUser]
  1419. ])->first();
  1420. // Verificación de la existencia del usuario
  1421. if(is_null($usr)){
  1422. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1423. }
  1424. // El código del equipo padre se inicializa
  1425. $eqpa = null;
  1426. // Si la jerarquía es de tipo "Hijo", Entonces
  1427. if($form['hierarchy'] == 'Hijo'){
  1428. // Se desencripta el código del equipamiento padre
  1429. $eqpa = $this->encryptionController->decrypt($form['parent_equipment']);
  1430. if(!$eqpa){
  1431. return $this->responseController->makeResponse(true, 'El ID del equipamiento padre no fue encriptado correctamente.', [], 400);
  1432. }
  1433. // Se obtiene la información del equipo padre
  1434. $parentEquipment = DB::table('S002V01TEQUI')->where([
  1435. ['EQUI_NULI', '=', $form['linea']],
  1436. ['EQUI_COEQ', '=', $eqpa],
  1437. ])->first();
  1438. // Se verifica que el equipo padre exista
  1439. if(is_null($parentEquipment)){
  1440. return $this->responseController->makeResponse(true, 'El equipamiento padre no existe.', [], 404);
  1441. }
  1442. }
  1443. // Se obtiene la ubicación de las plantillas
  1444. $ubic = $this->templatesUbic;
  1445. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  1446. $locationsEnc = file_get_contents($ubic . "locations.sam");
  1447. $locationsDec = $this->encryptionController->decrypt($locationsEnc);
  1448. $locationsArr = json_decode($locationsDec, true);
  1449. $locationsArrDec = [];
  1450. foreach($locationsArr as $location){
  1451. $locationStr = $this->encryptionController->decrypt($location['LOCATION']);
  1452. $code = $this->encryptionController->decrypt($location['CODE']);
  1453. $locationsArrDec[] = [
  1454. 'LOCATION' => $locationStr,
  1455. 'CODE' => $code
  1456. ];
  1457. }
  1458. $originLocation = null;
  1459. $originLevel = null;
  1460. $originOccupationStr = null;
  1461. $idWarehouse = null;
  1462. $idArea = null;
  1463. $idLevel = null;
  1464. $idZone = null;
  1465. if ($form['code_type'] !== '7') {
  1466. // Se desencripta la ubicación de origen
  1467. $originLocation = $this->encryptionController->decrypt($form['origin_location']);
  1468. if(!$originLocation){
  1469. return $this->responseController->makeResponse(true, 'La ubicación de origen no fue encriptada correctamente.', [], 400);
  1470. }
  1471. // Se verifica con la información del sistema
  1472. $originLocationFilt = array_filter($locationsArrDec, function($v, $k) use ($originLocation) {
  1473. return $v['CODE'] == $originLocation;
  1474. }, ARRAY_FILTER_USE_BOTH);
  1475. // Se verifica que la ubicacion exista en la información del sistema
  1476. if(count($originLocationFilt) < 1){
  1477. return $this->responseController->makeResponse(true, 'La ubicación de origen no se encuentra registrada en el sistema.', [], 404);
  1478. }
  1479. // Se desencripta el nivel del origen
  1480. $originLevel = $this->encryptionController->decrypt($form['origin_level']);
  1481. if(!$originLevel){
  1482. return $this->responseController->makeResponse(true, 'El nivel de origen no fue encriptado correctamente.', [], 400);
  1483. }
  1484. // Se obtienen los niveles disponibles
  1485. $supportedLevels = [];
  1486. for($i = 5; $i > 0; $i--){
  1487. $supportedLevels[] = "S0$i";
  1488. }
  1489. for($i = 0; $i <= 15; $i++){
  1490. $tag = $i < 10 ? "P0$i" : "P$i";
  1491. $supportedLevels[] = $tag;
  1492. }
  1493. // Se verifica que el origen sea válido
  1494. if(!in_array($originLevel, $supportedLevels)){
  1495. return $this->responseController->makeResponse(true, 'El nivel de origen seleccionado es inválido.', [], 400);
  1496. }
  1497. // Se desencripta la ocupación de orden
  1498. $originOccupationStr = $this->encryptionController->decrypt($form['origin_occupation']);
  1499. if(!$originOccupationStr){
  1500. return $this->responseController->makeResponse(true, 'La ocupación de origen no fue encriptada correctamente.', [], 400);
  1501. }
  1502. // Se obtiene la información de la ocupación
  1503. $originOccupationArea = substr($originOccupationStr, 0, 2);
  1504. $originOccupationID = substr($originOccupationStr, 2);
  1505. $originOccupation = DB::table('S002V01TOCUP')->where([
  1506. ['OCUP_NULI', '=', $form['linea']],
  1507. ['OCUP_AREA', '=', $originOccupationArea],
  1508. ['OCUP_IDOC', '=', $originOccupationID]
  1509. ])->first();
  1510. // Se verifica que exista la ocupació
  1511. if(is_null($originOccupation)){
  1512. return $this->responseController->makeResponse(true, 'La ocupación de origen seleccionada no existe.', [], 404);
  1513. }
  1514. } else {
  1515. $form['origin_element'] = null;
  1516. $idWarehouse = $this->encryptionController->decrypt($form['warehouse']);
  1517. if ($idWarehouse === false) {
  1518. return $this->responseController->makeResponse(true, 'El almacen no está encriptado correctamente.', [], 404);
  1519. }
  1520. $idArea = $this->encryptionController->decrypt($form['area']);
  1521. if ($idArea === false) {
  1522. return $this->responseController->makeResponse(true, 'El área no está encriptado correctamente.', [], 404);
  1523. }
  1524. $idLevel = $this->encryptionController->decrypt($form['level']);
  1525. if ($idLevel === false) {
  1526. return $this->responseController->makeResponse(true, 'El nivel no está encriptado correctamente.', [], 404);
  1527. }
  1528. $idZone = $this->encryptionController->decrypt($form['zone']);
  1529. if ($idZone === false) {
  1530. return $this->responseController->makeResponse(true, 'La zona no está encriptado correctamente.', [], 404);
  1531. }
  1532. try {
  1533. $validateWarehouse = DB::table('S002V01TALMA')
  1534. ->where('ALMA_COAL', '=', $idWarehouse)
  1535. ->where('ALMA_NULI', '=', $form['linea'])
  1536. ->exists();
  1537. } catch (\Throwable $th) {
  1538. return $this->responseController->makeResponse(true, 'Ocurrió un error al validar el almacen.', [], 500);
  1539. }
  1540. if (!$validateWarehouse) {
  1541. return $this->responseController->makeResponse(true, 'El almacen no existe.', [], 500);
  1542. }
  1543. try {
  1544. $validateArea = DB::table('S002V01TAREA')
  1545. ->where('AREA_COAL', '=', $idWarehouse)
  1546. ->where('AREA_COAR', '=', $idArea)
  1547. ->where('AREA_NULI', '=', $form['linea'])
  1548. ->exists();
  1549. } catch (\Throwable $th) {
  1550. return $this->responseController->makeResponse(true, 'Ocurrió un error al validar el almacen.', [], 500);
  1551. }
  1552. if (!$validateArea) {
  1553. return $this->responseController->makeResponse(true, 'El almacen no existe.', [], 500);
  1554. }
  1555. try {
  1556. $validateLevel = DB::table('S002V01TNIVE')
  1557. ->where('NIVE_COAL', '=', $idWarehouse)
  1558. ->where('NIVE_COAR', '=', $idArea)
  1559. ->where('NIVE_CONI', '=', $idLevel)
  1560. ->where('NIVE_NULI', '=', $form['linea'])
  1561. ->exists();
  1562. } catch (\Throwable $th) {
  1563. return $this->responseController->makeResponse(true, 'Ocurrió un error al validar el almacen.', [], 500);
  1564. }
  1565. if (!$validateLevel) {
  1566. return $this->responseController->makeResponse(true, 'El almacen no existe.', [], 500);
  1567. }
  1568. try {
  1569. $validateZone = DB::table('S002V01TZONA')
  1570. ->where('ZONA_COAL', '=', $idWarehouse)
  1571. ->where('ZONA_COAR', '=', $idArea)
  1572. ->where('ZONA_CONI', '=', $idLevel)
  1573. ->where('ZONA_COZO', '=', $idZone)
  1574. ->where('ZONA_NULI', '=', $form['linea'])
  1575. ->exists();
  1576. } catch (\Throwable $th) {
  1577. return $this->responseController->makeResponse(true, 'Ocurrió un error al validar el almacen.', [], 500);
  1578. }
  1579. if (!$validateZone) {
  1580. return $this->responseController->makeResponse(true, 'El almacen no existe.', [], 500);
  1581. }
  1582. }
  1583. // Se desencripta la familia
  1584. $familyCode = $this->encryptionController->decrypt($form['family']);
  1585. if(!$familyCode){
  1586. return $this->responseController->makeResponse(true, 'La familia del equipamiento no fue encriptada correctamente.', [], 400);
  1587. }
  1588. // Se obtienen los datos de la familia
  1589. $family = DB::table('S002V01TFAMI')->where([
  1590. ['FAMI_NULI', '=', $form['linea']],
  1591. ['FAMI_COFA', '=', $familyCode]
  1592. ])->first();
  1593. // Se verifica que la familia exista
  1594. if(is_null($family)){
  1595. return $this->responseController->makeResponse(true, 'La familia seleccionada no existe.', [], 404);
  1596. }
  1597. // Se desencripta la subfamilia
  1598. $subfamilyCode = $this->encryptionController->decrypt($form['subfamily']);
  1599. if(!$subfamilyCode){
  1600. return $this->responseController->makeResponse(true, 'La subfamilia del equipamiento no fue encriptada correctamente.', [], 400);
  1601. }
  1602. // Se obtienen los datos de la subfamilia
  1603. $subfamily = DB::table('S002V01TSUBF')->where([
  1604. ['SUBF_NULI', '=', $form['linea']],
  1605. ['SUBF_COFA', '=', $familyCode],
  1606. ['SUBF_COSU', '=', $subfamilyCode]
  1607. ])->first();
  1608. // Se verifica que la subfamilia exista
  1609. if(is_null($subfamily)){
  1610. return $this->responseController->makeResponse(true, 'La subfamilia seleccionada no existe.', [], 404);
  1611. }
  1612. // Se inicializa arreglo con los estados disponibles
  1613. $validStatus = ["A", "S", "T", "I", "R", "D"];
  1614. // Se desencripta el estado
  1615. $status = $this->encryptionController->decrypt($form['status']);
  1616. // Se verifica que el estado sea un valor válido
  1617. if(!$status){
  1618. return $this->responseController->makeResponse(true, 'La estado del equipamiento no fue encriptado correctamente.', [], 400);
  1619. }else if(!in_array($status, $validStatus)){
  1620. return $this->responseController->makeResponse(true, 'El estado seleccionado para el equipamiento es inválido.', [], 400);
  1621. }
  1622. // Se obtiene el arreglo de las imagenes
  1623. $imagesArr = json_decode($form['images'], true);
  1624. // Se verifica que el arreglo contenga imagenes
  1625. if(count($imagesArr) < 1){
  1626. return $this->responseController->makeResponse(true, 'El arreglo de la galería de imágenes está vacío.', [], 400);
  1627. }
  1628. $imagesGallery = [];
  1629. // Se iteran las imagenes
  1630. foreach($imagesArr as $imageFile){
  1631. // Se verifica que tenga un formato correcto
  1632. if(!array_key_exists('type', $imageFile)){
  1633. return $this->responseController->makeResponse(true, 'El arreglo de la galería de imágenes tiene un formato inválido.', [], 400);
  1634. // Si la imagen es nueva, entonces...
  1635. }else if($imageFile['type'] == 'Nuevo'){
  1636. // Se obtiene el ID de la imagen
  1637. $tempFileID = $this->encryptionController->decrypt($imageFile['id']);
  1638. // Se obtiene la información registrada en la tabla de archivos temporales
  1639. $tempFile = DB::table('S002V01TARTE')->where([
  1640. ['ARTE_IDAR', '=', $tempFileID],
  1641. ['ARTE_NULI', '=', $form['linea']]
  1642. ])->first();
  1643. // La imagen es colocada en su posición final
  1644. $finalFile = $this->documentManagementController->moveFinalFile($form['linea'], 'GEEQ', 'FO', $tempFile, $idUser);
  1645. // Si ocurrió un error, entonces se manda el error
  1646. if(!$finalFile[0]){
  1647. return $this->responseController->makeResponse(true, $finalFile[1], [], 400);
  1648. }
  1649. // La información es guardada en un arreglo
  1650. $imagesGallery[] = $finalFile[1];
  1651. }else{
  1652. // Se desencripta el id de la imagen
  1653. $fileID = $this->encryptionController->decrypt($imageFile['id']);
  1654. // Es guardada en un arreglo
  1655. $imagesGallery[] = $fileID;
  1656. }
  1657. }
  1658. // El arreglo de las imagenes es pasada en formato string
  1659. $imagesGalleryStr = json_encode($imagesGallery);
  1660. // Se obtiene el arreglo de los documentos
  1661. $documentsArr = json_decode($form['documents'], true);
  1662. if(count($documentsArr) < 1){
  1663. return $this->responseController->makeResponse(true, 'El arreglo de documentos relacionados está vacío.', [], 400);
  1664. }
  1665. $documents = [];
  1666. // Se iteran los documentos
  1667. foreach($documentsArr as $documentFile){
  1668. // Se verifica que tenga un formato correcto
  1669. if(!array_key_exists('type', $documentFile)){
  1670. return $this->responseController->makeResponse(true, 'El arreglo de documentos asociados tiene un formato inválido.', [], 400);
  1671. // Si el documento es nuevo, entonces...
  1672. }else if($documentFile['type'] == 'Nuevo'){
  1673. // Se obtiene el ID del documento
  1674. $tempFileID = $this->encryptionController->decrypt($documentFile['id']);
  1675. // Se obtiene la información registrada en la tabla de archivos temporales
  1676. $tempFile = DB::table('S002V01TARTE')->where([
  1677. ['ARTE_IDAR', '=', $tempFileID],
  1678. ['ARTE_NULI', '=', $form['linea']]
  1679. ])->first();
  1680. // El documento es colocada en su posición final
  1681. $finalFile = $this->documentManagementController->moveFinalFile($form['linea'], 'GEEQ', 'IN', $tempFile, $idUser);
  1682. // Si ocurrió un error, entonces se manda el error
  1683. if(!$finalFile[0]){
  1684. return $this->responseController->makeResponse(true, $finalFile[1], [], 400);
  1685. }
  1686. // La información es guardada en un arreglo
  1687. $documents[] = $finalFile[1];
  1688. }else{
  1689. // Se desencripta el id del documento
  1690. $fileID = $this->encryptionController->decrypt($documentFile['id']);
  1691. // Es guardada en un arreglo
  1692. $documents[] = $fileID;
  1693. }
  1694. }
  1695. // El arreglo de los documentos es pasada en formato string
  1696. $documentsStr = json_encode($documents);
  1697. // Se obtiene el formato de la fecha en formato manejable
  1698. $acquisitionDate = new Carbon($form['acquisition_date']);
  1699. $warrantyStartDate = new Carbon($form['warranty_start_date']);
  1700. $warrantyEndDate = new Carbon($form['warranty_end_date']);
  1701. // Se verifica que la fecha de inicio de la garantia sea menor a la fecha final de la garantia
  1702. if($warrantyStartDate->lt($acquisitionDate)){
  1703. return $this->responseController->makeResponse(true, 'La fecha de inicio de la grantía no puede ser menor a la fecha de adquisición.', [], 400);
  1704. }else if($warrantyEndDate->lte($acquisitionDate)){
  1705. 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);
  1706. }
  1707. // Se desencripta el numero del proveedor
  1708. $providerID = $this->encryptionController->decrypt($form['equipment_provider']);
  1709. if(!$providerID){
  1710. return $this->responseController->makeResponse(true, 'El proveedor del equipamiento no fue encriptado correctamente.', [], 400);
  1711. }
  1712. // Se obtiene la información del proveedor
  1713. $provider = DB::table('S002V01TPROV')->where([
  1714. ['PROV_NUPR', '=', $providerID],
  1715. ['PROV_NULI', '=', $form['linea']]
  1716. ])->first();
  1717. // Se verifica que el proveedor exista
  1718. if(is_null($provider)){
  1719. return $this->responseController->makeResponse(true, 'La proveedor seleccionada no existe.', [], 404);
  1720. }
  1721. $arrSerialNumber = array();
  1722. if ($form['serial_number'] !== '' && !is_null($form['serial_number'])) {
  1723. // Se desencripta el número de serie
  1724. $serialNumer = $this->encryptionController->decrypt($form['serial_number']);
  1725. if(!$serialNumer){
  1726. return $this->responseController->makeResponse(true, 'El número de serie del equipamiento no fue encriptado correctamente.', [], 400);
  1727. }
  1728. $arrSerialNumber = explode(',', $serialNumer);
  1729. if (intval($form['number_items']) !== count($arrSerialNumber)) {
  1730. return $this->responseController->makeResponse(true, 'La cantidad de número de series no corresponde con la cantidad de artículos a registrar.', [], 400);
  1731. }
  1732. foreach ($arrSerialNumber as $key => $serialNumber) {
  1733. // Se obtiene la información del equipamiento por medio del número de serie
  1734. $serialNmberDB = DB::table('S002V01TPCEQ')->where([
  1735. ['PCEQ_NULI', '=', $form['linea']],
  1736. ['PCEQ_NUSE', '=', $serialNumber]
  1737. ])->first();
  1738. // Se verifica que exista el código de serie
  1739. if(!is_null($serialNmberDB)){
  1740. $serialNumberStr = $serialNmberDB->PCEQ_CPGE;
  1741. return $this->responseController->makeResponse(true, "El número de serie $serialNumber enviado ya se encuentra relacionado al equipamiento $serialNumberStr.", [], 401);
  1742. }
  1743. }
  1744. $temp_array = array_unique($arrSerialNumber);
  1745. $duplicates = sizeof($temp_array) != sizeof($arrSerialNumber);
  1746. if ($duplicates) {
  1747. return $this->responseController->makeResponse(true, 'Los número de serie no se pueden repetir.', [], 400);
  1748. }
  1749. } else {
  1750. try {
  1751. $getEquipment = DB::table('S002V01TPCEQ')
  1752. ->where('PCEQ_NULI', '=', $form['linea'])
  1753. ->where('PCEQ_NUSE', 'LIKE', '0-%')
  1754. ->get(['PCEQ_NUSE AS NUMERO_SERIE']);
  1755. } catch (\Throwable $th) {
  1756. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener los artículos.', $th->getMessage(), 400);
  1757. }
  1758. $lastSerie = 0;
  1759. if (count($getEquipment) > 0) {
  1760. foreach ($getEquipment as $key => $equipment) {
  1761. $serie = str_replace('0-', '', $equipment->NUMERO_SERIE);
  1762. $serie = intval($serie);
  1763. if (is_null($lastSerie)) {
  1764. $lastSerie = $serie;
  1765. } else if ($serie > $lastSerie) {
  1766. $lastSerie = $serie;
  1767. }
  1768. }
  1769. }
  1770. for ($i=0; $i < $form['number_items']; $i++) {
  1771. $lastSerie = $lastSerie + 1;
  1772. $arrSerialNumber[] = '0-'.$lastSerie;
  1773. }
  1774. }
  1775. if ( empty($arrSerialNumber) ) {
  1776. return $this->responseController->makeResponse(true, 'No hay números de serie disponibles.', [], 400);
  1777. }
  1778. // Se desencripta el código pregenerado en Angular
  1779. $preCode = $this->encryptionController->decrypt($form['pre_generated_code']);
  1780. if(!$preCode){
  1781. return $this->responseController->makeResponse(true, 'El código pre generado no fue encriptado correctamente.', [], 400);
  1782. }
  1783. // Se obtiene el código pregenerado en Laravel
  1784. $codeVerified = $this->verifyPreCode([
  1785. "line" => $form['linea'],
  1786. "originLocation" => $originLocation,
  1787. "originLevel" => $originLevel,
  1788. "originOccupation" => $originOccupationStr,
  1789. "originElement" => $form['origin_element'],
  1790. "warehouse" => $idWarehouse,
  1791. "area" => $idArea,
  1792. "level" => $idLevel,
  1793. "zone" => $idZone,
  1794. "family" => $familyCode,
  1795. "subfamily" => $subfamilyCode,
  1796. "status" => $status,
  1797. "equipmentType" => $form['equipment_type'],
  1798. "equipmentModel" => $form['equipment_model'],
  1799. "equipmentID" => "XXXXX",
  1800. "pccIntersection" => null,
  1801. "pccPosition" => null,
  1802. "originKilometers" => null,
  1803. "destinyLocation" => null,
  1804. "destinyLevel" => null,
  1805. "destinyOccupation" => null,
  1806. "destinyElement" => null,
  1807. "destinyKilometers" => null
  1808. ], $form['code_type'], $form['hierarchy'], $eqpa);
  1809. // Se verifica que el código pregenerado en Angular y el pregenerado en Laravel
  1810. if($codeVerified != $preCode){
  1811. 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);
  1812. }
  1813. // Se verifica que la información del sfotware
  1814. $deso = isset($form['software_name']) ? $form['software_name'] : null;
  1815. $veso = isset($form['software_version']) ? $form['software_version'] : null;
  1816. // Se obtiene la fecha actual
  1817. $now = $this->functionsController->now();
  1818. $nowStr = $now->toDateTimeString();
  1819. $arrPreCodification = array();
  1820. $numberItems = intval($form['number_items']);
  1821. for ($i=0; $i < $numberItems; $i++) {
  1822. // Se ingresa la información a la tabla de los precódigos de los equipamientos
  1823. $idPreCodification = DB::table('S002V01TPCEQ')->insertGetId([
  1824. 'PCEQ_NULI' => $form['linea'],
  1825. 'PCEQ_CPGE' => $preCode,
  1826. 'PCEQ_JERA' => $form['hierarchy'],
  1827. 'PCEQ_EQPA' => $eqpa,
  1828. 'PCEQ_TICO' => $form['code_type'],
  1829. 'PCEQ_UBOR' => $originLocation,
  1830. 'PCEQ_NIOR' => $originLevel,
  1831. 'PCEQ_OCOR' => $originOccupationStr,
  1832. 'PCEQ_ELOR' => $form['origin_element'],
  1833. 'PCEQ_ALMA' => $idWarehouse,
  1834. 'PCEQ_AREA' => $idArea,
  1835. 'PCEQ_NIVE' => $idLevel,
  1836. 'PCEQ_ZONA' => $idZone,
  1837. 'PCEQ_FAMI' => $familyCode,
  1838. 'PCEQ_SUBF' => $subfamilyCode,
  1839. 'PCEQ_ESEQ' => $status,
  1840. 'PCEQ_TIEQ' => $form['equipment_type'],
  1841. 'PCEQ_MOEQ' => $form['equipment_model'],
  1842. 'PCEQ_IPCC' => null,
  1843. 'PCEQ_PPCC' => null,
  1844. 'PCEQ_KIOR' => 0.0,
  1845. 'PCEQ_UBDE' => '',
  1846. 'PCEQ_NIDE' => '',
  1847. 'PCEQ_OCDE' => '',
  1848. 'PCEQ_ELDE' => '',
  1849. 'PCEQ_KIDE' => 0.0,
  1850. 'PCEQ_FEAD' => $form['acquisition_date'],
  1851. 'PCEQ_FIGA' => $form['warranty_start_date'],
  1852. 'PCEQ_FTGA' => $form['warranty_end_date'],
  1853. 'PCEQ_PREQ' => $providerID,
  1854. 'PCEQ_NUSE' => count($arrSerialNumber) === 1 ? $arrSerialNumber[0] : $arrSerialNumber[$i],
  1855. 'PCEQ_DESO' => $deso,
  1856. 'PCEQ_VESO' => $veso,
  1857. 'PCEQ_GAIM' => $imagesGalleryStr,
  1858. 'PCEQ_DORE' => $documentsStr,
  1859. 'PCEQ_USRE' => $idUser,
  1860. 'PCEQ_FERE' => $nowStr,
  1861. ]);
  1862. $idPreCodification = $this->encryptionController->encrypt($idPreCodification);
  1863. $arrPreCodification[] = $idPreCodification;
  1864. }
  1865. $actions = DB::getQueryLog();
  1866. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1867. $idac = $this->functionsController->registerActivity(
  1868. $form['linea'],
  1869. 'S002V01M07GEEQ',
  1870. 'S002V01F01ADEQ',
  1871. 'S002V01P11REEQ',
  1872. 'Registro',
  1873. "El usuario $name (" . $usr->USUA_IDUS . ") solicitó el registro del equipamiento $preCode.",
  1874. $idUser,
  1875. $nowStr,
  1876. );
  1877. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1878. return $this->responseController->makeResponse(false, 'EXITO.', $arrPreCodification);
  1879. }
  1880. public function verifyPreCode(array $parameters, string $type, string $mode, string | null $parentCode = '') : string {
  1881. $code = "";
  1882. if($mode == 'Hijo'){
  1883. $code .= $parentCode;
  1884. $equipment = ['equipmentType', 'equipmentModel', 'equipmentID'];
  1885. $equipmentStr = "";
  1886. foreach($equipment as $ctrl){
  1887. $val = $parameters[$ctrl];
  1888. if($ctrl == 'equipmentModel'){
  1889. $val = $this->processElement($val, 'model');
  1890. }else if($ctrl == 'equipmentType'){
  1891. $val = $this->processElement($val, 'element');
  1892. }
  1893. $equipmentStr .= $val . '-';
  1894. }
  1895. $equipmentStr = substr($equipmentStr, 0, -1);
  1896. $code .= "." . $equipmentStr;
  1897. }else{
  1898. switch($type){
  1899. case "1":
  1900. //CASO 1: Codificación general
  1901. $lbs = ['line', 'originLocation', 'originLevel', 'originOccupation', 'originElement'];
  1902. $lbsStr = "";
  1903. foreach($lbs as $ctrl){
  1904. $val = $parameters[$ctrl];
  1905. if($ctrl == 'line'){
  1906. $val = intval($val) < 10 ? "0$val" : "$val";
  1907. }else if($ctrl == 'originElement'){
  1908. $val = $this->processElement($val, 'element');
  1909. }
  1910. $lbsStr .= $val . ".";
  1911. }
  1912. $lbsStr = substr($lbsStr, 0, -1);
  1913. $pbs = ['family', 'subfamily', 'status'];
  1914. $pbsStr = "";
  1915. foreach($pbs as $ctrl){
  1916. $val = $parameters[$ctrl];
  1917. $pbsStr .= $val . ".";
  1918. }
  1919. $pbsStr = substr($pbsStr, 0, -1);
  1920. $equipment = ['equipmentType', 'equipmentModel', 'equipmentID'];
  1921. $equipmentStr = "";
  1922. foreach($equipment as $ctrl){
  1923. $val = $parameters[$ctrl];
  1924. if($ctrl == 'equipmentModel'){
  1925. $val = $this->processElement($val, 'model');
  1926. }else if($ctrl == 'equipmentType'){
  1927. $val = $this->processElement($val, 'element');
  1928. }
  1929. $equipmentStr .= $val . '-';
  1930. }
  1931. $equipmentStr = substr($equipmentStr, 0, -1);
  1932. $code = $lbsStr . "_" . $pbsStr . "." . $equipmentStr;
  1933. break;
  1934. case "7":
  1935. $siteTag = '';
  1936. $site = ['line', 'area', 'level'];
  1937. foreach($site as $ctrl){
  1938. $val = $parameters[$ctrl];
  1939. if($ctrl == 'line'){
  1940. $val = intval($val) < 10 ? "0$val" : "$val";
  1941. }
  1942. $siteTag .= $val.'.';
  1943. }
  1944. $siteTag = substr($siteTag, 0, -1);
  1945. $areaTag = '';
  1946. $area = ['warehouse', 'zone'];
  1947. foreach($area as $ctrl){
  1948. $val = $parameters[$ctrl];
  1949. $areaTag .= $val.'.';
  1950. }
  1951. $areaTag = substr($areaTag, 0, -1);
  1952. $technicalSpecificationTag = '';
  1953. $technicalSpecification = ['family', 'subfamily', 'status'];
  1954. foreach($technicalSpecification as $ctrl){
  1955. $val = $parameters[$ctrl];
  1956. $technicalSpecificationTag .= $val.'.';
  1957. }
  1958. $technicalSpecificationTag = substr($technicalSpecificationTag, 0, -1);
  1959. $equipmentIdentifierTag = '';
  1960. $equipmentIdentifier = ['equipmentType', 'equipmentModel', 'equipmentID'];
  1961. foreach($equipmentIdentifier as $ctrl){
  1962. $val = $parameters[$ctrl];
  1963. if($ctrl == 'equipmentModel'){
  1964. $val = $this->processElement($val, 'model');
  1965. }else if($ctrl == 'equipmentType'){
  1966. $val = $this->processElement($val, 'element');
  1967. }
  1968. $equipmentIdentifierTag .= $val.'-';
  1969. }
  1970. $equipmentIdentifierTag = substr($equipmentIdentifierTag, 0, -1);
  1971. $code = $siteTag.'-'.$areaTag.'_'.$technicalSpecificationTag.'.'.$equipmentIdentifierTag;
  1972. break;
  1973. }
  1974. }
  1975. return $code;
  1976. }
  1977. private function processElement(string $value, string $type) : string {
  1978. $value = strtoupper($value);
  1979. $validVal0 = $this->functionsController->unaccent($value);
  1980. $CONNECTORS = ['DE', 'PARA', 'Y', 'O', 'DEL', 'EL', 'LA', 'LOS', 'POR', 'EN'];
  1981. $valueArr1 = explode(' ', $validVal0);
  1982. $validVal1 = [];
  1983. foreach($valueArr1 as $word){
  1984. if(!in_array($word, $CONNECTORS)){
  1985. $validVal1[] = $word;
  1986. }
  1987. }
  1988. $validLength = count($validVal1);
  1989. $validVal2 = "";
  1990. if($validLength < 1){
  1991. return "ERROR";
  1992. }else if($validLength == 1){
  1993. if(strlen($validVal1[0]) < 5){
  1994. $validVal2 = $validVal1[0];
  1995. for($i = strlen($validVal1[0]); $i < 5; $i++){
  1996. if($type == 'model'){
  1997. $validVal2 = "X$validVal2";
  1998. }else{
  1999. $validVal2 = $validVal2 . "X";
  2000. }
  2001. }
  2002. }else{
  2003. $validVal2 = substr($validVal1[0], 0, 5);
  2004. }
  2005. }else if($validLength == 2){
  2006. $word1 = "";
  2007. if(strlen($validVal1[0]) < 2){
  2008. if($type == 'model'){
  2009. $word1 = "X$validVal1[0]";
  2010. }else{
  2011. $word1 = $validVal1[0] . "X";
  2012. }
  2013. }else{
  2014. $word1 = substr($validVal1[0], 0, 2);
  2015. }
  2016. $word2 = "";
  2017. if(strlen($validVal1[1]) < 3){
  2018. $word2 = $validVal1[1];
  2019. for($i = strlen($validVal1[1]); $i < 3; $i++){
  2020. if($type == 'model'){
  2021. $word2 = "X$word2";
  2022. }else{
  2023. $word2 = $word2 . "X";
  2024. }
  2025. }
  2026. }else{
  2027. $word2 = substr($validVal1[1], 0, 3);
  2028. }
  2029. $validVal2 = $word1 . $word2;
  2030. }else if($validLength == 3){
  2031. $word1 = "";
  2032. if(strlen($validVal1[0]) < 2){
  2033. if($type == 'model'){
  2034. $word1 = "X$validVal1[0]";
  2035. }else{
  2036. $word1 = $validVal1[0] . "X";
  2037. }
  2038. }else{
  2039. $word1 = substr($validVal1[0], 0, 2);
  2040. }
  2041. $word2 = substr($validVal1[1], 0, 1);
  2042. $word3 = "";
  2043. if(strlen($validVal1[2]) < 2){
  2044. if($type == 'model'){
  2045. $word3 = "X$validVal1[2]";
  2046. }else{
  2047. $word3 = $validVal1[2] . "X";
  2048. }
  2049. }else{
  2050. $word3 = substr($validVal1[2], 0, 2);
  2051. }
  2052. $validVal2 = $word1 . $word2 . $word3;
  2053. }else if($validLength == 4){
  2054. $word1 = "";
  2055. if(strlen($validVal1[0]) < 2){
  2056. if($type == 'model'){
  2057. $word1 = "X$validVal1[0]";
  2058. }else{
  2059. $word1 = $validVal1[0] . "X";
  2060. }
  2061. }else{
  2062. $word1 = substr($validVal1[0], 0, 2);
  2063. }
  2064. $word2 = substr($validVal1[1], 0, 1);
  2065. $word3 = substr($validVal1[2], 0, 1);
  2066. $word4 = substr($validVal1[3], 0, 1);
  2067. $validVal2 = $word1 . $word2 . $word3 . $word4;
  2068. }else if($validLength >= 5){
  2069. $word1 = substr($validVal1[0], 0, 1);
  2070. $word2 = substr($validVal1[1], 0, 1);
  2071. $word3 = substr($validVal1[2], 0, 1);
  2072. $word4 = substr($validVal1[3], 0, 1);
  2073. $word5 = substr($validVal1[4], 0, 1);
  2074. $validVal2 = $word1 . $word2 . $word3 . $word4 . $word5;
  2075. }else{
  2076. return "ERROR";
  2077. }
  2078. return $validVal2;
  2079. }
  2080. public function getPendigEquipments($idUser, $line) {
  2081. DB::enableQueryLog();
  2082. $idUser = $this->encryptionController->decrypt($idUser);
  2083. if(!$idUser){
  2084. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2085. }
  2086. $usr = DB::table('S002V01TUSUA')->where([
  2087. ['USUA_NULI', '=', $line],
  2088. ['USUA_IDUS', '=', $idUser],
  2089. ])->first();
  2090. if(is_null($usr)){
  2091. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2092. }
  2093. $pendingEquipments = DB::table('S002V01TPCEQ')->select([
  2094. 'PCEQ_IDPR AS IDREG',
  2095. 'PCEQ_CPGE AS CODIGO',
  2096. 'PCEQ_TICO AS TIPO_CODIGO',
  2097. 'PCEQ_TIEQ AS TIPO_EQUIPAMIENTO',
  2098. 'PCEQ_MOEQ AS MODELO_EQUIPAMIENTO',
  2099. 'PCEQ_GAIM AS GALERIA_IMAGENES',
  2100. 'PCEQ_ESRE AS ESTADO_REGISTRO',
  2101. ])->where([
  2102. ['PCEQ_NULI', '=', $line],
  2103. ['PCEQ_ESRE', '=', 'Revisión'],
  2104. ])->get()->all();
  2105. $idUserEnc = $this->encryptionController->encrypt($idUser);
  2106. foreach($pendingEquipments as $key=>$equipment){
  2107. $equipment->IDREG = $this->encryptionController->encrypt($equipment->IDREG);
  2108. $equipment->CODIGO = $this->encryptionController->encrypt($equipment->CODIGO);
  2109. $imagesGalleryArr = json_decode($equipment->GALERIA_IMAGENES);
  2110. $imagesGalleryFn = [];
  2111. foreach($imagesGalleryArr as $imageCode){
  2112. $imageCodeEnc = $this->encryptionController->encrypt($imageCode);
  2113. $publicUri = $this->documentManagementController->privateGetPublicDocumentURL(
  2114. $imageCodeEnc,
  2115. $idUserEnc,
  2116. $line
  2117. );
  2118. $response = json_decode($publicUri->original, true);
  2119. if($response['error']){
  2120. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  2121. }else{
  2122. $uriEnc = $this->encryptionController->encrypt($response['response']['public_uri']);
  2123. $imagesGalleryFn[] = $uriEnc;
  2124. }
  2125. }
  2126. $equipment->GALERIA_IMAGENES = json_encode($imagesGalleryFn);
  2127. $equipment->TIPO_EQUIPAMIENTO = $this->encryptionController->encrypt($equipment->TIPO_EQUIPAMIENTO);
  2128. $equipment->MODELO_EQUIPAMIENTO = $this->encryptionController->encrypt($equipment->MODELO_EQUIPAMIENTO);
  2129. $pendingEquipments[$key] = $equipment;
  2130. }
  2131. $now = $this->functionsController->now();
  2132. $nowStr = $now->toDateTimeString();
  2133. $actions = DB::getQueryLog();
  2134. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2135. $idac = $this->functionsController->registerActivity(
  2136. $line,
  2137. 'S002V01M07GEEQ',
  2138. 'S002V01F01ADEQ',
  2139. '-',
  2140. 'Consulta',
  2141. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los equipamientos en revisión.",
  2142. $idUser,
  2143. $nowStr,
  2144. );
  2145. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2146. return $this->responseController->makeResponse(false, 'EXITO.', $pendingEquipments);
  2147. }
  2148. public function getPendigEquipment($idReg, $idUser, $line) {
  2149. DB::enableQueryLog();
  2150. $idUser = $this->encryptionController->decrypt($idUser);
  2151. if(!$idUser){
  2152. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2153. }
  2154. $usr = DB::table('S002V01TUSUA')->where([
  2155. ['USUA_NULI', '=', $line],
  2156. ['USUA_IDUS', '=', $idUser],
  2157. ])->first();
  2158. if(is_null($usr)){
  2159. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2160. }
  2161. $idReg = $this->encryptionController->decrypt($idReg);
  2162. if(!$idReg){
  2163. return $this->responseController->makeResponse(true, 'El ID del pre-registro no está encriptado correctamente', [], 400);
  2164. }
  2165. $pendingEquipment = DB::table('S002V01TPCEQ')->select([
  2166. 'PCEQ_IDPR AS IDREG',
  2167. 'PCEQ_CPGE AS CODIGO',
  2168. 'PCEQ_JERA AS JERARQUIA',
  2169. 'PCEQ_EQPA AS PADRE',
  2170. 'PCEQ_TICO AS TIPO_CODIGO',
  2171. 'PCEQ_UBOR AS UBICACION_ORIGEN',
  2172. 'PCEQ_NIOR AS NIVEL_ORIGEN',
  2173. 'PCEQ_OCOR AS OCUPACION_ORIGEN',
  2174. 'PCEQ_ELOR AS ELEMENTO_ORIGEN',
  2175. 'PCEQ_FAMI AS FAMILIA',
  2176. 'PCEQ_SUBF AS SUBFAMILIA',
  2177. 'PCEQ_ESEQ AS ESTADO_EQUIPAMIENTO',
  2178. 'PCEQ_TIEQ AS TIPO_EQUIPAMIENTO',
  2179. 'PCEQ_MOEQ AS MODELO_EQUIPAMIENTO',
  2180. 'PCEQ_IPCC AS INTERSECCION_PCC',
  2181. 'PCEQ_PPCC AS POSICION_PCC',
  2182. 'PCEQ_KIOR AS KILOMETRO_ORIGEN',
  2183. 'PCEQ_UBDE AS UBICACION_DESTINO',
  2184. 'PCEQ_NIDE AS NIVEL_DESTINO',
  2185. 'PCEQ_OCDE AS OCUPACION_DESTINO',
  2186. 'PCEQ_ELDE AS ELEMENTO_DESTINO',
  2187. 'PCEQ_KIDE AS KILOMETRO_DESTINO',
  2188. 'PCEQ_FEAD AS FECHA_ADQUISICION',
  2189. 'PCEQ_FIGA AS FECHA_INICIO_GARANTIA',
  2190. 'PCEQ_FTGA AS FECHA_FIN_GARANTIA',
  2191. 'PCEQ_PREQ AS PROVEEDOR',
  2192. 'PCEQ_NUSE AS NUMERO_SERIAL',
  2193. 'PCEQ_DESO AS DENOMINACION_SOFTWARE',
  2194. 'PCEQ_VESO AS VERSION_SOFTWARE',
  2195. 'PCEQ_GAIM AS GALERIA_IMAGENES',
  2196. 'PCEQ_DORE AS DOCUMENTOS',
  2197. 'PCEQ_ESRE AS ESTADO_REGISTRO',
  2198. 'PCEQ_CORE AS COMENTARIOS_RECHAZO',
  2199. 'PCEQ_COAC AS COMENTARIOS_ACEPTACION',
  2200. 'PCEQ_USRE AS USUARIO_REGISTRO',
  2201. 'PCEQ_FERE AS FECHA_REGISTRO',
  2202. 'PCEQ_USMO AS USUARIO_MODIFICO',
  2203. 'PCEQ_FEMO AS FECHA_MODIFICACION'
  2204. ])->where([
  2205. ['PCEQ_NULI', '=', $line],
  2206. ['PCEQ_IDPR', '=', $idReg],
  2207. ['PCEQ_ESRE', '=', 'Revisión'],
  2208. ])->first();
  2209. if(is_null($pendingEquipment)){
  2210. return $this->responseController->makeResponse(true, 'El pre-registro solicitado no existe', [], 404);
  2211. }
  2212. $pendingEquipment->IDREG = $this->encryptionController->encrypt($pendingEquipment->IDREG);
  2213. $pendingEquipment->CODIGO = $this->encryptionController->encrypt($pendingEquipment->CODIGO);
  2214. $ubic = $this->templatesUbic;
  2215. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  2216. $locationsEnc = file_get_contents($ubic . "locations.sam");
  2217. $locationsDec = $this->encryptionController->decrypt($locationsEnc);
  2218. $locationsArr = json_decode($locationsDec, true);
  2219. $locationsArrDec = [];
  2220. foreach($locationsArr as $location){
  2221. $locationStr = $this->encryptionController->decrypt($location['LOCATION']);
  2222. $code = $this->encryptionController->decrypt($location['CODE']);
  2223. $locationsArrDec[] = [
  2224. 'LOCATION' => $locationStr,
  2225. 'CODE' => $code
  2226. ];
  2227. }
  2228. $originLocation = $pendingEquipment->UBICACION_ORIGEN;
  2229. $originLocationFilt = array_filter($locationsArrDec, function ($v, $k) use ($originLocation) {
  2230. return $v['CODE'] == $originLocation;
  2231. }, ARRAY_FILTER_USE_BOTH);
  2232. $pendingEquipment->UBICACION_ORIGEN = end($originLocationFilt)['LOCATION'] . " (" . $pendingEquipment->UBICACION_ORIGEN . ")";
  2233. $levelsArr = [];
  2234. for($i = 5; $i > 0; $i--){
  2235. $levelsArr[] = [
  2236. 'LEVEL' => "Subterráneo S0$i",
  2237. 'CODE' => "S0$i",
  2238. ];
  2239. }
  2240. for($i = 0; $i <= 15; $i++){
  2241. $code = $i < 10 ? "P0$i" : "P$i";
  2242. $level = $i == 0 ? "Planta baja P00" : "Piso $code";
  2243. $levelsArr[] = [
  2244. 'LEVEL' => $level,
  2245. 'CODE' => $code,
  2246. ];
  2247. }
  2248. $originLevel = $pendingEquipment->NIVEL_ORIGEN;
  2249. $originLevelFilt = array_filter($levelsArr, function ($v, $k) use ($originLevel) {
  2250. return $v['CODE'] == $originLevel;
  2251. }, ARRAY_FILTER_USE_BOTH);
  2252. $pendingEquipment->NIVEL_ORIGEN = end($originLevelFilt)['LEVEL'] . " (" . $pendingEquipment->NIVEL_ORIGEN . ")";
  2253. $originOccupationStr = $pendingEquipment->OCUPACION_ORIGEN;
  2254. $originOccupationArea = substr($originOccupationStr, 0, 2);
  2255. $originOccupationID = substr($originOccupationStr, 2);
  2256. $originOccupation = DB::table('S002V01TOCUP')->where([
  2257. ['OCUP_NULI', '=', $line],
  2258. ['OCUP_AREA', '=', $originOccupationArea],
  2259. ['OCUP_IDOC', '=', $originOccupationID]
  2260. ])->first();
  2261. $pendingEquipment->OCUPACION_ORIGEN = $originOccupation->OCUP_DESC . " (" . $originOccupationStr . ")";
  2262. $family = DB::table('S002V01TFAMI')->where([
  2263. ['FAMI_NULI', '=', $line],
  2264. ['FAMI_COFA', '=', $pendingEquipment->FAMILIA],
  2265. ])->first();
  2266. $subfamily = DB::table('S002V01TSUBF')->where([
  2267. ['SUBF_NULI', '=', $line],
  2268. ['SUBF_COFA', '=', $pendingEquipment->FAMILIA],
  2269. ['SUBF_COSU', '=', $pendingEquipment->SUBFAMILIA],
  2270. ])->first();
  2271. $pendingEquipment->FAMILIA = $family->FAMI_NOFA . " (" . $pendingEquipment->FAMILIA . ")";
  2272. $pendingEquipment->SUBFAMILIA = $subfamily->SUBF_NOSU . " (" . $pendingEquipment->SUBFAMILIA . ")";
  2273. $validStatus = ["A" => "Adquisición", "S" => "Stock", "T" => "Traslado", "I" => "Instalación", "R" => "Reparación", "D" => "Disposición"];
  2274. $pendingEquipment->ESTADO_EQUIPAMIENTO = $validStatus[$pendingEquipment->ESTADO_EQUIPAMIENTO] . " (" . $pendingEquipment->ESTADO_EQUIPAMIENTO . ")";
  2275. $idUserEnc = $this->encryptionController->encrypt($idUser);
  2276. $imagesGalleryArr = json_decode($pendingEquipment->GALERIA_IMAGENES);
  2277. $imagesGalleryFn = [];
  2278. foreach($imagesGalleryArr as $imageCode){
  2279. $imageCodeEnc = $this->encryptionController->encrypt($imageCode);
  2280. $publicUri = $this->documentManagementController->privateGetPublicDocumentURL(
  2281. $imageCodeEnc,
  2282. $idUserEnc,
  2283. $line
  2284. );
  2285. $response = json_decode($publicUri->original, true);
  2286. if($response['error']){
  2287. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  2288. }else{
  2289. $uriEnc = $this->encryptionController->encrypt($response['response']['public_uri']);
  2290. $imagesGalleryFn[] = $uriEnc;
  2291. }
  2292. }
  2293. $documentsArr = json_decode($pendingEquipment->DOCUMENTOS);
  2294. $documentsFn = [];
  2295. foreach($documentsArr as $document){
  2296. $codeArr = explode('=',$document);
  2297. $codeArr0 = explode('-', $codeArr[0]);
  2298. $file = DB::table('S002V01TAFAL')->where([
  2299. ['AFAL_NULI', '=', $line],
  2300. ['AFAL_COMO', '=', $codeArr0[1]],
  2301. ['AFAL_CLDO', '=', $codeArr0[2]],
  2302. ['AFAL_FECR', '=', $codeArr0[3]],
  2303. ['AFAL_NUSE', '=', $codeArr0[4]],
  2304. ['AFAL_NUVE', '=', $codeArr[1]],
  2305. ])->first();
  2306. if(is_null($file)){
  2307. return $this->responseController->makeResponse(true, "El archivo $document no está registrado.", [], 404);
  2308. }
  2309. $documentsFn[] = [
  2310. 'id' => $this->encryptionController->encrypt($document),
  2311. 'name' => $document,
  2312. 'size' => $file->AFAL_TAMA
  2313. ];
  2314. }
  2315. $pendingEquipment->GALERIA_IMAGENES = json_encode($imagesGalleryFn);
  2316. $pendingEquipment->DOCUMENTOS = json_encode($documentsFn);
  2317. $pendingEquipment->PROVEEDOR = $this->encryptionController->encrypt($pendingEquipment->PROVEEDOR);
  2318. $pendingEquipment->NUMERO_SERIAL = $this->encryptionController->encrypt($pendingEquipment->NUMERO_SERIAL);
  2319. $usrReg = DB::table('S002V01TUSUA')->where([
  2320. ['USUA_NULI', '=', $line],
  2321. ['USUA_IDUS', '=', $pendingEquipment->USUARIO_REGISTRO]
  2322. ])->first();
  2323. $usrRegName = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  2324. $pendingEquipment->USUARIO_REGISTRO = $usrRegName . " (" . $pendingEquipment->USUARIO_REGISTRO . ")";
  2325. if(!is_null($pendingEquipment->USUARIO_MODIFICO)){
  2326. $usrMod = DB::table('S002V01TUSUA')->where([
  2327. ['USUA_NULI', '=', $line],
  2328. ['USUA_IDUS', '=', $pendingEquipment->USUARIO_MODIFICO]
  2329. ])->first();
  2330. $usrModName = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  2331. $pendingEquipment->USUARIO_MODIFICO = $usrModName . " (" . $pendingEquipment->USUARIO_MODIFICO . ")";
  2332. }
  2333. $pendingEquipment->TIPO_EQUIPAMIENTO = $this->encryptionController->encrypt($pendingEquipment->TIPO_EQUIPAMIENTO);
  2334. $pendingEquipment->MODELO_EQUIPAMIENTO = $this->encryptionController->encrypt($pendingEquipment->MODELO_EQUIPAMIENTO);
  2335. $now = $this->functionsController->now();
  2336. $nowStr = $now->toDateTimeString();
  2337. $actions = DB::getQueryLog();
  2338. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2339. $idac = $this->functionsController->registerActivity(
  2340. $line,
  2341. 'S002V01M07GEEQ',
  2342. 'S002V01F01ADEQ',
  2343. '-',
  2344. 'Consulta',
  2345. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el equipamiento pre codificado $idReg.",
  2346. $idUser,
  2347. $nowStr,
  2348. );
  2349. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2350. return $this->responseController->makeResponse(false, 'EXITO.', $pendingEquipment);
  2351. }
  2352. public function changePreCodedEquipmentStatus(Request $request) {
  2353. DB::enableQueryLog();
  2354. $validator = Validator::make($request->all(), [
  2355. 'id_user' => 'required|string',
  2356. 'linea' => 'required|integer',
  2357. 'id_reg' => 'required|string',
  2358. 'status' => 'required|string|in:Aprobado,Rechazado',
  2359. 'comments' => 'required|string|min:15',
  2360. ]);
  2361. if($validator->fails()){
  2362. return $this->responseController->makeResponse(
  2363. true,
  2364. "Se encontraron uno o más errores.",
  2365. $this->responseController->makeErrors(
  2366. $validator->errors()->messages()
  2367. ),
  2368. 401
  2369. );
  2370. }
  2371. $form = $request->all();
  2372. $idUser = $this->encryptionController->decrypt($form['id_user']);
  2373. if(!$idUser){
  2374. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  2375. }
  2376. $usr = DB::table('S002V01TUSUA')->where([
  2377. ['USUA_NULI', '=', $form['linea']],
  2378. ['USUA_IDUS', '=', $idUser]
  2379. ])->first();
  2380. if(is_null($usr)){
  2381. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  2382. }
  2383. $idReg = $this->encryptionController->decrypt($form['id_reg']);
  2384. if(!$idReg){
  2385. return $this->responseController->makeResponse(true, 'El ID del pre-registro no está encriptado correctamente', [], 400);
  2386. }
  2387. $pendingEquipment = DB::table('S002V01TPCEQ')->where([
  2388. ['PCEQ_NULI', '=', $form['linea']],
  2389. ['PCEQ_IDPR', '=', $idReg],
  2390. ])->first();
  2391. if(is_null($pendingEquipment)){
  2392. return $this->responseController->makeResponse(true, 'El pre-registro solicitado no existe.', [], 404);
  2393. }else if($pendingEquipment->PCEQ_ESRE == 'Rechazado'){
  2394. return $this->responseController->makeResponse(true, 'El pre-registro se encuentra rechazado.', [], 400);
  2395. }else if($pendingEquipment->PCEQ_ESRE == 'Aprobado'){
  2396. return $this->responseController->makeResponse(true, 'El pre-registro se encuentra aprobado.', [], 400);
  2397. }
  2398. $now = $this->functionsController->now();
  2399. $nowStr = $now->toDateTimeString();
  2400. if($form['status'] == 'Rechazado'){
  2401. DB::table('S002V01TPCEQ')->where([
  2402. ['PCEQ_IDPR', '=', $idReg],
  2403. ['PCEQ_NULI', '=', $form['linea']],
  2404. ])->update([
  2405. 'PCEQ_ESRE' => $form['status'],
  2406. 'PCEQ_CORE' => $form['comments'],
  2407. 'PCEQ_USMO' => $idUser,
  2408. 'PCEQ_FEMO' => $nowStr,
  2409. ]);
  2410. }else{
  2411. //PASO 1: SE BUSCA EL ÚLTIMO ID DEL EQUIPAMIENTO
  2412. $lastID = DB::table('S002V01TEQUI')->select([
  2413. 'EQUI_IDEQ AS LAST_ID'
  2414. ])->where([
  2415. ['EQUI_NULI', '=', $form['linea']],
  2416. ['EQUI_FAMI', '=', $pendingEquipment->PCEQ_FAMI],
  2417. ['EQUI_SUBF', '=', $pendingEquipment->PCEQ_SUBF],
  2418. ['EQUI_TIPO', '=', $pendingEquipment->PCEQ_TIEQ],
  2419. ['EQUI_MODE', '=', $pendingEquipment->PCEQ_MOEQ],
  2420. ])->orderBy('EQUI_IDEQ', 'asc')->first();
  2421. //PASO 2: SE DEFINE EL ID DEL NUEVO EQUIPAMIENTO
  2422. $IDNum = is_null($lastID) ? 1 : $lastID->LAST_ID + 1;
  2423. //PASO 3: SE DEFINE UN ESPACIO PARA RETORNAR EL PROCESO
  2424. doAgain:
  2425. //PASO 4: SE RELLENA EL ID CON CEROS A LA IZQUIERDA PARA DARLE EL FORMATO DE LA CODIFICACIÓN
  2426. $IDStr = "$IDNum";
  2427. for($i = strlen($IDStr); $i < 6; $i++){
  2428. $IDStr = "0$IDStr";
  2429. }
  2430. //PASO 5: SE INTEGRA EL ID AL CÓDIGO PRE-GENERADO
  2431. $preCodeArr = explode('_', $pendingEquipment->PCEQ_CPGE);
  2432. $previewPBS = $preCodeArr[1];
  2433. $previewPBSArr = explode('-', $previewPBS);
  2434. $previewPBSArr = array_reverse($previewPBSArr);
  2435. $previewPBSArr[0] = $IDStr;
  2436. $previewPBSArr = array_reverse($previewPBSArr);
  2437. $previewPBS = implode('-', $previewPBSArr);
  2438. //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
  2439. $previewCode = $preCodeArr[0] . "_" . $previewPBS;
  2440. $registeredCode = DB::table('S002V01TEQUI')->where([
  2441. ['EQUI_NULI', '=', $form['linea']],
  2442. ['EQUI_COEQ', '=', $previewCode]
  2443. ])->first();
  2444. $codeInHistory = DB::table('S002V01TEQUI')
  2445. ->where('EQUI_NULI', '=', $form['linea'])
  2446. ->whereJsonContains('EQUI_HICO', $previewCode)->first();
  2447. //PASO 7: VERIFICAR QUE EL CÓDIGO NO EXISTA, SI EXISTE SE REALIZA NUEVAMENTE EL PROCESO AUMENTANDO EN 1 EL ID
  2448. if(!is_null($registeredCode) || !is_null($codeInHistory)){
  2449. $IDNum++;
  2450. goto doAgain;
  2451. }
  2452. //PASO 8: REALIZAR EL REGISTRO DEL EQUIPAMIENTO
  2453. $hico = json_encode([$previewCode]);
  2454. DB::table('S002V01TEQUI')->insert([
  2455. 'EQUI_NULI' => $form['linea'],
  2456. 'EQUI_FAMI' => $pendingEquipment->PCEQ_FAMI,
  2457. 'EQUI_SUBF' => $pendingEquipment->PCEQ_SUBF,
  2458. 'EQUI_TIPO' => $pendingEquipment->PCEQ_TIEQ,
  2459. 'EQUI_MODE' => $pendingEquipment->PCEQ_MOEQ,
  2460. 'EQUI_IDEQ' => $IDNum,
  2461. 'EQUI_ESEQ' => $pendingEquipment->PCEQ_ESEQ,
  2462. 'EQUI_COEQ' => $previewCode,
  2463. 'EQUI_JERA' => $pendingEquipment->PCEQ_JERA,
  2464. 'EQUI_EQPA' => $pendingEquipment->PCEQ_EQPA,
  2465. 'EQUI_TICO' => $pendingEquipment->PCEQ_TICO,
  2466. 'EQUI_UBOR' => $pendingEquipment->PCEQ_UBOR,
  2467. 'EQUI_NIOR' => $pendingEquipment->PCEQ_NIOR,
  2468. 'EQUI_OCOR' => $pendingEquipment->PCEQ_OCOR,
  2469. 'EQUI_ELOR' => $pendingEquipment->PCEQ_ELOR,
  2470. 'EQUI_ALMA' => $pendingEquipment->PCEQ_ALMA,
  2471. 'EQUI_AREA' => $pendingEquipment->PCEQ_AREA,
  2472. 'EQUI_NIVE' => $pendingEquipment->PCEQ_NIVE,
  2473. 'EQUI_ZONA' => $pendingEquipment->PCEQ_ZONA,
  2474. 'EQUI_IPCC' => $pendingEquipment->PCEQ_IPCC,
  2475. 'EQUI_PPCC' => $pendingEquipment->PCEQ_PPCC,
  2476. 'EQUI_KIOR' => $pendingEquipment->PCEQ_KIOR,
  2477. 'EQUI_UBDE' => $pendingEquipment->PCEQ_UBDE,
  2478. 'EQUI_NIDE' => $pendingEquipment->PCEQ_NIDE,
  2479. 'EQUI_OCDE' => $pendingEquipment->PCEQ_OCDE,
  2480. 'EQUI_ELDE' => $pendingEquipment->PCEQ_ELDE,
  2481. 'EQUI_KIDE' => $pendingEquipment->PCEQ_KIDE,
  2482. 'EQUI_FEAD' => $pendingEquipment->PCEQ_FEAD,
  2483. 'EQUI_FIGA' => $pendingEquipment->PCEQ_FIGA,
  2484. 'EQUI_FTGA' => $pendingEquipment->PCEQ_FTGA,
  2485. 'EQUI_PREQ' => $pendingEquipment->PCEQ_PREQ,
  2486. 'EQUI_NUSE' => $pendingEquipment->PCEQ_NUSE,
  2487. 'EQUI_DESO' => $pendingEquipment->PCEQ_DESO,
  2488. 'EQUI_VESO' => $pendingEquipment->PCEQ_VESO,
  2489. 'EQUI_HICO' => $hico,
  2490. 'EQUI_GAIM' => $pendingEquipment->PCEQ_GAIM,
  2491. 'EQUI_DORE' => $pendingEquipment->PCEQ_DORE,
  2492. 'EQUI_USRE' => $idUser,
  2493. 'EQUI_FERE' => $nowStr,
  2494. ]);
  2495. DB::table('S002V01TPCEQ')->where([
  2496. ['PCEQ_IDPR', '=', $idReg],
  2497. ['PCEQ_NULI', '=', $form['linea']],
  2498. ])->update([
  2499. 'PCEQ_ESRE' => $form['status'],
  2500. 'PCEQ_CORE' => $form['comments'],
  2501. 'PCEQ_USMO' => $idUser,
  2502. 'PCEQ_FEMO' => $nowStr,
  2503. ]);
  2504. }
  2505. if ($pendingEquipment->PCEQ_TICO === '7') {
  2506. try {
  2507. $star = (array) DB::table('S002V01TSTAR')
  2508. ->where('STAR_IDPC', '=', $pendingEquipment->PCEQ_IDPR)
  2509. ->where('STAR_ESTA', '=', 'Pendiente')
  2510. ->where('STAR_NULI', '=', $form['linea'])
  2511. ->first();
  2512. } catch (\Throwable $th) {
  2513. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener los artículos en Stock.', [], 500);
  2514. }
  2515. if ( !empty($star) && !is_null($star) ) {
  2516. try {
  2517. $validateUpdate = DB::table('S002V01TSTAR')
  2518. ->where('STAR_IDPC', '=', $pendingEquipment->PCEQ_IDPR)
  2519. ->where('STAR_ESTA', '=', 'Pendiente')
  2520. ->where('STAR_NULI', '=', $form['linea'])
  2521. ->update([
  2522. 'STAR_ESTA' => 'Activo',
  2523. 'STAR_USMO' => $idUser,
  2524. 'STAR_FEMO' => $nowStr,
  2525. 'STAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2526. ]);
  2527. } catch (\Throwable $th) {
  2528. return $this->responseController->makeResponse(true, 'Ocurrió un al actualizar el stock.', [], 500);
  2529. }
  2530. if (!$validateUpdate) {
  2531. return $this->responseController->makeResponse(true, 'No se pudo actualizar la información en stock.', [], 500);
  2532. }
  2533. try {
  2534. $validateUpdate = DB::table('S002V01TUBAR')
  2535. ->where('UBAR_IDST', '=', $star['STAR_IDST'])
  2536. ->where('UBAR_ESTA', '=', 'Pendiente')
  2537. ->where('UBAR_NULI', '=', $form['linea'])
  2538. ->update([
  2539. 'UBAR_COUB' => $previewCode,
  2540. 'UBAR_ESTA' => 'Activo',
  2541. 'UBAR_USMO' => $idUser,
  2542. 'UBAR_FEMO' => $nowStr,
  2543. 'UBAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2544. ]);
  2545. } catch (\Throwable $th) {
  2546. return $this->responseController->makeResponse(true, 'Ocurrió un al actualizar el stock.', [], 500);
  2547. }
  2548. if (!$validateUpdate) {
  2549. return $this->responseController->makeResponse(true, 'No se pudo actualizar la información en stock.', [], 500);
  2550. }
  2551. }
  2552. }
  2553. $actions = DB::getQueryLog();
  2554. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2555. $idac = $this->functionsController->registerActivity(
  2556. $form['linea'],
  2557. 'S002V01M07GEEQ',
  2558. 'S002V01F01ADEQ',
  2559. 'S002V01P11REEQ',
  2560. 'Registro',
  2561. "El usuario $name (" . $usr->USUA_IDUS . ") cambió el estado del pre-registro $idReg a $form[status].",
  2562. $idUser,
  2563. $nowStr,
  2564. );
  2565. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2566. return $this->responseController->makeResponse(false, 'EXITO.');
  2567. }
  2568. public function getEquipments($idUser, $line) {
  2569. DB::enableQueryLog();
  2570. $idUser = $this->encryptionController->decrypt($idUser);
  2571. if(!$idUser){
  2572. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2573. }
  2574. $usr = DB::table('S002V01TUSUA')->where([
  2575. ['USUA_NULI', '=', $line],
  2576. ['USUA_IDUS', '=', $idUser],
  2577. ])->first();
  2578. if(is_null($usr)){
  2579. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2580. }
  2581. $equipments = DB::table('S002V01TEQUI')->select([
  2582. 'EQUI_COEQ AS CODIGO',
  2583. 'EQUI_TIPO AS TIPO',
  2584. 'EQUI_MODE AS MODELO',
  2585. 'EQUI_IDEQ AS ID_EQUIPO',
  2586. 'EQUI_ESFU AS ESTADO_FUNCIONAMIENTO',
  2587. 'EQUI_GAIM AS GALERIA_IMAGENES',
  2588. 'EQUI_ELOR AS ELEMENTO_ORIGEN',
  2589. 'EQUI_TICO AS TIPO_CODIGO',
  2590. 'EQUI_EQPA AS EQUIPO_PADRE',
  2591. ])->where('EQUI_NULI', '=', $line)->get()->all();
  2592. $idUserEnc = $this->encryptionController->encrypt($idUser);
  2593. foreach($equipments as $key=>$equipment){
  2594. $children = DB::table('S002V01TEQUI')->where([
  2595. ['EQUI_NULI', '=', $line],
  2596. ['EQUI_EQPA', '=', $equipment->CODIGO],
  2597. ])->get()->all();
  2598. $equipment->CODIGO = $this->encryptionController->encrypt($equipment->CODIGO);
  2599. $equipment->ID_EQUIPO = $this->encryptionController->encrypt($equipment->ID_EQUIPO);
  2600. $equipment->EQUIPO_PADRE = is_null($equipment->EQUIPO_PADRE) ? null : $this->encryptionController->encrypt($equipment->EQUIPO_PADRE);
  2601. $imagesGalleryArr = json_decode($equipment->GALERIA_IMAGENES, true);
  2602. $imagesGalleryFn = [];
  2603. foreach($imagesGalleryArr as $imageCode){
  2604. $imageCodeEnc = $this->encryptionController->encrypt($imageCode);
  2605. $publicUri = $this->documentManagementController->privateGetPublicDocumentURL(
  2606. $imageCodeEnc,
  2607. $idUserEnc,
  2608. $line
  2609. );
  2610. $response = json_decode($publicUri->original, true);
  2611. if($response['error']){
  2612. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  2613. }else{
  2614. $uriEnc = $this->encryptionController->encrypt($response['response']['public_uri']);
  2615. $imagesGalleryFn[] = $uriEnc;
  2616. }
  2617. }
  2618. $equipment->GALERIA_IMAGENES = json_encode($imagesGalleryFn);
  2619. $equipment->TIENE_HIJOS = count($children) > 0 ? 'Si' : 'No';
  2620. $equipments[$key] = $equipment;
  2621. }
  2622. $now = $this->functionsController->now();
  2623. $nowStr = $now->toDateTimeString();
  2624. $actions = DB::getQueryLog();
  2625. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2626. $idac = $this->functionsController->registerActivity(
  2627. $line,
  2628. 'S002V01M07GEEQ',
  2629. 'S002V01F01ADEQ',
  2630. 'S002V01P01MEEQ',
  2631. 'Consulta',
  2632. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los equipamientos registrados.",
  2633. $idUser,
  2634. $nowStr,
  2635. );
  2636. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2637. return $this->responseController->makeResponse(false, 'EXITO.', $equipments);
  2638. }
  2639. public function getEquipmentsBySubfamily($familyCode, $subfamilyCode, $idUser, $line) {
  2640. DB::enableQueryLog();
  2641. $idUser = $this->encryptionController->decrypt($idUser);
  2642. if(!$idUser){
  2643. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2644. }
  2645. $usr = DB::table('S002V01TUSUA')->where([
  2646. ['USUA_NULI', '=', $line],
  2647. ['USUA_IDUS', '=', $idUser],
  2648. ])->first();
  2649. if(is_null($usr)){
  2650. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2651. }
  2652. $familyCode = $this->encryptionController->decrypt($familyCode);
  2653. if(!$familyCode){
  2654. return $this->responseController->makeResponse(true, 'El código de la familia no está encriptado correctamente', [], 400);
  2655. }
  2656. $family = DB::table('S002V01TFAMI')->where([
  2657. ['FAMI_NULI', '=', $line],
  2658. ['FAMI_COFA', '=', $familyCode],
  2659. ])->first();
  2660. if(is_null($family)){
  2661. return $this->responseController->makeResponse(true, 'La familia relacionada no está registrada.', [], 404);
  2662. }
  2663. $subfamilyCode = $this->encryptionController->decrypt($subfamilyCode);
  2664. if(!$subfamilyCode){
  2665. return $this->responseController->makeResponse(true, 'El código de la subfamilia no está encriptado correctamente', [], 400);
  2666. }
  2667. $subfamily = DB::table('S002V01TSUBF')->where([
  2668. ['SUBF_NULI', '=', $line],
  2669. ['SUBF_COFA', '=', $familyCode],
  2670. ['SUBF_COSU', '=', $subfamilyCode],
  2671. ])->first();
  2672. if(is_null($subfamily)){
  2673. return $this->responseController->makeResponse(true, 'La subfamilia relacionada no está registrada.', [], 404);
  2674. }
  2675. $equipments = DB::table('S002V01TEQUI')->select([
  2676. 'EQUI_COEQ AS CODIGO',
  2677. 'EQUI_TIPO AS TIPO',
  2678. 'EQUI_MODE AS MODELO',
  2679. 'EQUI_IDEQ AS ID_EQUIPO',
  2680. 'EQUI_ESFU AS ESTADO_FUNCIONAMIENTO',
  2681. 'EQUI_GAIM AS GALERIA_IMAGENES',
  2682. ])->where([
  2683. ['EQUI_NULI', '=', $line],
  2684. ['EQUI_FAMI', '=', $familyCode],
  2685. ['EQUI_SUBF', '=', $subfamilyCode],
  2686. ['EQUI_JERA', '=', 'Padre']
  2687. ])->get()->all();
  2688. $idUserEnc = $this->encryptionController->encrypt($idUser);
  2689. foreach($equipments as $key=>$equipment){
  2690. $equipment->CODIGO = $this->encryptionController->encrypt($equipment->CODIGO);
  2691. $equipment->ID_EQUIPO = $this->encryptionController->encrypt($equipment->ID_EQUIPO);
  2692. $imagesGalleryArr = json_decode($equipment->GALERIA_IMAGENES, true);
  2693. $imagesGalleryFn = [];
  2694. foreach($imagesGalleryArr as $imageCode){
  2695. $imageCodeEnc = $this->encryptionController->encrypt($imageCode);
  2696. $publicUri = $this->documentManagementController->privateGetPublicDocumentURL(
  2697. $imageCodeEnc,
  2698. $idUserEnc,
  2699. $line
  2700. );
  2701. $response = json_decode($publicUri->original, true);
  2702. if($response['error']){
  2703. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  2704. }else{
  2705. $uriEnc = $this->encryptionController->encrypt($response['response']['public_uri']);
  2706. $imagesGalleryFn[] = $uriEnc;
  2707. }
  2708. }
  2709. $equipment->GALERIA_IMAGENES = json_encode($imagesGalleryFn);
  2710. $equipments[$key] = $equipment;
  2711. }
  2712. $now = $this->functionsController->now();
  2713. $nowStr = $now->toDateTimeString();
  2714. $actions = DB::getQueryLog();
  2715. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2716. $idac = $this->functionsController->registerActivity(
  2717. $line,
  2718. 'S002V01M07GEEQ',
  2719. 'S002V01F01ADEQ',
  2720. 'S002V01P01MEEQ',
  2721. 'Consulta',
  2722. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los equipamientos relacionados a la subfamilia $subfamilyCode de la familia $familyCode.",
  2723. $idUser,
  2724. $nowStr,
  2725. );
  2726. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2727. return $this->responseController->makeResponse(false, 'EXITO.', $equipments);
  2728. }
  2729. public function getEquipmentsByParent($parentCode, $idUser, $line) {
  2730. DB::enableQueryLog();
  2731. $idUser = $this->encryptionController->decrypt($idUser);
  2732. if(!$idUser){
  2733. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2734. }
  2735. $usr = DB::table('S002V01TUSUA')->where([
  2736. ['USUA_NULI', '=', $line],
  2737. ['USUA_IDUS', '=', $idUser],
  2738. ])->first();
  2739. if(is_null($usr)){
  2740. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2741. }
  2742. $parentCode = $this->encryptionController->decrypt($parentCode);
  2743. if(!$parentCode){
  2744. return $this->responseController->makeResponse(true, 'El código del equipamiento padre no está encriptado correctamente', [], 400);
  2745. }
  2746. $equipments = DB::table('S002V01TEQUI')->select([
  2747. 'EQUI_COEQ AS CODIGO',
  2748. 'EQUI_TIPO AS TIPO',
  2749. 'EQUI_MODE AS MODELO',
  2750. 'EQUI_IDEQ AS ID_EQUIPO',
  2751. 'EQUI_ESFU AS ESTADO_FUNCIONAMIENTO',
  2752. 'EQUI_GAIM AS GALERIA_IMAGENES',
  2753. 'EQUI_EQPA AS EQUIPO_PADRE',
  2754. ])->where([
  2755. ['EQUI_NULI', '=', $line],
  2756. ['EQUI_JERA', '=', 'Hijo'],
  2757. ['EQUI_EQPA', '=', $parentCode],
  2758. ])->get()->all();
  2759. $idUserEnc = $this->encryptionController->encrypt($idUser);
  2760. foreach($equipments as $key=>$equipment){
  2761. $children = DB::table('S002V01TEQUI')->where([
  2762. ['EQUI_NULI', '=', $line],
  2763. ['EQUI_EQPA', '=', $equipment->CODIGO],
  2764. ])->get()->all();
  2765. $equipment->CODIGO = $this->encryptionController->encrypt($equipment->CODIGO);
  2766. $equipment->ID_EQUIPO = $this->encryptionController->encrypt($equipment->ID_EQUIPO);
  2767. $equipment->EQUIPO_PADRE = is_null($equipment->EQUIPO_PADRE) ? null : $this->encryptionController->encrypt($equipment->EQUIPO_PADRE);
  2768. $imagesGalleryArr = json_decode($equipment->GALERIA_IMAGENES, true);
  2769. $imagesGalleryFn = [];
  2770. foreach($imagesGalleryArr as $imageCode){
  2771. $imageCodeEnc = $this->encryptionController->encrypt($imageCode);
  2772. $publicUri = $this->documentManagementController->privateGetPublicDocumentURL(
  2773. $imageCodeEnc,
  2774. $idUserEnc,
  2775. $line
  2776. );
  2777. $response = json_decode($publicUri->original, true);
  2778. if($response['error']){
  2779. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  2780. }else{
  2781. $uriEnc = $this->encryptionController->encrypt($response['response']['public_uri']);
  2782. $imagesGalleryFn[] = $uriEnc;
  2783. }
  2784. }
  2785. $equipment->GALERIA_IMAGENES = json_encode($imagesGalleryFn);
  2786. $equipment->TIENE_HIJOS = count($children) > 0 ? 'Si' : 'No';
  2787. $equipments[$key] = $equipment;
  2788. }
  2789. $now = $this->functionsController->now();
  2790. $nowStr = $now->toDateTimeString();
  2791. $actions = DB::getQueryLog();
  2792. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2793. $idac = $this->functionsController->registerActivity(
  2794. $line,
  2795. 'S002V01M07GEEQ',
  2796. 'S002V01F01ADEQ',
  2797. 'S002V01P02DEEQ',
  2798. 'Consulta',
  2799. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los equipamientos relacionados al equipamiento $parentCode.",
  2800. $idUser,
  2801. $nowStr,
  2802. );
  2803. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2804. return $this->responseController->makeResponse(false, 'EXITO.', $equipments);
  2805. }
  2806. public function getEquipmentDetails($code, $idUser, $line) {
  2807. DB::enableQueryLog();
  2808. $idUser = $this->encryptionController->decrypt($idUser);
  2809. if(!$idUser){
  2810. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2811. }
  2812. $usr = DB::table('S002V01TUSUA')->where([
  2813. ['USUA_NULI', '=', $line],
  2814. ['USUA_IDUS', '=', $idUser],
  2815. ])->first();
  2816. if(is_null($usr)){
  2817. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2818. }
  2819. $code = $this->encryptionController->decrypt($code);
  2820. if(!$code){
  2821. return $this->responseController->makeResponse(true, 'El código del equipamiento no está encriptado correctamente', [], 400);
  2822. }
  2823. $equipment = DB::table('S002V01TEQUI')->select([
  2824. 'EQUI_FAMI AS FAMILIA',
  2825. 'EQUI_SUBF AS SUBFAMILIA',
  2826. 'EQUI_TIPO AS TIPO_EQUIPAMIENTO',
  2827. 'EQUI_MODE AS MODELO',
  2828. 'EQUI_IDEQ AS ID_EQUIPAMIENTO',
  2829. 'EQUI_ESEQ AS ESTADO',
  2830. 'EQUI_COEQ AS CODIGO',
  2831. 'EQUI_JERA AS JERARQUIA',
  2832. 'EQUI_EQPA AS EQUIPO_PADRE',
  2833. 'EQUI_TICO AS TIPO_CODIGO',
  2834. 'EQUI_UBOR AS UBICACION_ORIGEN',
  2835. 'EQUI_NIOR AS NIVEL_ORIGEN',
  2836. 'EQUI_OCOR AS OCUPACION_ORIGEN',
  2837. 'EQUI_ELOR AS ELEMENTO_ORIGEN',
  2838. 'EQUI_IPCC AS INTERSECCION_PCC',
  2839. 'EQUI_PPCC AS POSICION_PCC',
  2840. 'EQUI_KIOR AS KILOMETRO_ORIGEN',
  2841. 'EQUI_UBDE AS UBICACION_DESTINO',
  2842. 'EQUI_NIDE AS NIVEL_DESTINO',
  2843. 'EQUI_OCDE AS OCUPACION_DESTINO',
  2844. 'EQUI_ELDE AS ELEMENTO_DESTINO',
  2845. 'EQUI_KIDE AS KILOMETRO_DESTINO',
  2846. 'EQUI_FEAD AS FECHA_ADQUISICION',
  2847. 'EQUI_FIGA AS FECHA_INICIO_GARANTIA',
  2848. 'EQUI_FTGA AS FECHA_TERMINO_GARANTIA',
  2849. 'EQUI_PREQ AS PROVEEDOR',
  2850. 'EQUI_NUSE AS NUMERO_SERIAL',
  2851. 'EQUI_DESO AS DENOMINACION_SOFTWARE',
  2852. 'EQUI_VESO AS VERSION_SOFTWARE',
  2853. 'EQUI_HICO AS HISTORIAL_CODIGOS',
  2854. 'EQUI_GAIM AS GALERIA_IMAGENES',
  2855. 'EQUI_DORE AS DOCUMENTOS',
  2856. 'EQUI_ESFU AS ESTADO_FUNCIONAMIENTO',
  2857. 'EQUI_USRE AS USUARIO_REGISTRO',
  2858. 'EQUI_FERE AS FECHA_REGISTRO',
  2859. 'EQUI_USMO AS USUARIO_MODIFICACION',
  2860. 'EQUI_FEMO AS FECHA_MODIFICACION',
  2861. ])->where([
  2862. ['EQUI_NULI', '=', $line],
  2863. ['EQUI_COEQ', '=', $code],
  2864. ])->first();
  2865. $family = DB::table('S002V01TFAMI')->where([
  2866. ['FAMI_NULI', '=', $line],
  2867. ['FAMI_COFA', '=', $equipment->FAMILIA]
  2868. ])->first();
  2869. $subfamily = DB::table('S002V01TSUBF')->where([
  2870. ['SUBF_NULI', '=', $line],
  2871. ['SUBF_COFA', '=', $equipment->FAMILIA],
  2872. ['SUBF_COSU', '=', $equipment->SUBFAMILIA]
  2873. ])->first();
  2874. $equipment->FAMILIA = $family->FAMI_NOFA . " (" . $equipment->FAMILIA . ")";
  2875. $equipment->SUBFAMILIA = $subfamily->SUBF_NOSU . " (" . $equipment->SUBFAMILIA . ")";
  2876. $equipment->ID_EQUIPAMIENTO = $this->encryptionController->encrypt($equipment->ID_EQUIPAMIENTO);
  2877. $equipment->CODIGO = $this->encryptionController->encrypt($equipment->CODIGO);
  2878. $ubic = $this->templatesUbic;
  2879. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  2880. $locationsEnc = file_get_contents($ubic . "locations.sam");
  2881. $locationsDec = $this->encryptionController->decrypt($locationsEnc);
  2882. $locationsArr = json_decode($locationsDec, true);
  2883. $locationsArrDec = [];
  2884. foreach($locationsArr as $location){
  2885. $locationStr = $this->encryptionController->decrypt($location['LOCATION']);
  2886. $code = $this->encryptionController->decrypt($location['CODE']);
  2887. $locationsArrDec[] = [
  2888. 'LOCATION' => $locationStr,
  2889. 'CODE' => $code
  2890. ];
  2891. }
  2892. $originLocation = $equipment->UBICACION_ORIGEN;
  2893. $originLocationFilt = array_filter($locationsArrDec, function ($v, $k) use ($originLocation) {
  2894. return $v['CODE'] == $originLocation;
  2895. }, ARRAY_FILTER_USE_BOTH);
  2896. $equipment->UBICACION_ORIGEN = end($originLocationFilt)['LOCATION'] . " (" . $equipment->UBICACION_ORIGEN . ")";
  2897. $levelsArr = [];
  2898. for($i = 5; $i > 0; $i--){
  2899. $levelsArr[] = [
  2900. 'LEVEL' => "Subterráneo S0$i",
  2901. 'CODE' => "S0$i",
  2902. ];
  2903. }
  2904. for($i = 0; $i <= 15; $i++){
  2905. $code = $i < 10 ? "P0$i" : "P$i";
  2906. $level = $i == 0 ? "Planta baja P00" : "Piso $code";
  2907. $levelsArr[] = [
  2908. 'LEVEL' => $level,
  2909. 'CODE' => $code,
  2910. ];
  2911. }
  2912. $originLevel = $equipment->NIVEL_ORIGEN;
  2913. $originLevelFilt = array_filter($levelsArr, function ($v, $k) use ($originLevel) {
  2914. return $v['CODE'] == $originLevel;
  2915. }, ARRAY_FILTER_USE_BOTH);
  2916. $equipment->NIVEL_ORIGEN = end($originLevelFilt)['LEVEL'] . " (" . $equipment->NIVEL_ORIGEN . ")";
  2917. $originOccupationStr = $equipment->OCUPACION_ORIGEN;
  2918. $originOccupationArea = substr($originOccupationStr, 0, 2);
  2919. $originOccupationID = substr($originOccupationStr, 2);
  2920. $originOccupation = DB::table('S002V01TOCUP')->where([
  2921. ['OCUP_NULI', '=', $line],
  2922. ['OCUP_AREA', '=', $originOccupationArea],
  2923. ['OCUP_IDOC', '=', $originOccupationID]
  2924. ])->first();
  2925. $validStatus = ["A" => "Adquisición", "S" => "Stock", "T" => "Traslado", "I" => "Instalación", "R" => "Reparación", "D" => "Disposición"];
  2926. $equipment->ESTADO = $validStatus[$equipment->ESTADO] . " (" . $equipment->ESTADO . ")";
  2927. $equipment->OCUPACION_ORIGEN = $originOccupation->OCUP_DESC . " (" . $originOccupationStr . ")";
  2928. $idUserEnc = $this->encryptionController->encrypt($idUser);
  2929. $imagesGalleryArr = json_decode($equipment->GALERIA_IMAGENES);
  2930. $imagesGalleryFn = [];
  2931. foreach($imagesGalleryArr as $imageCode){
  2932. $imageCodeEnc = $this->encryptionController->encrypt($imageCode);
  2933. $publicUri = $this->documentManagementController->privateGetPublicDocumentURL(
  2934. $imageCodeEnc,
  2935. $idUserEnc,
  2936. $line
  2937. );
  2938. $response = json_decode($publicUri->original, true);
  2939. if($response['error']){
  2940. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  2941. }else{
  2942. $uriEnc = $this->encryptionController->encrypt($response['response']['public_uri']);
  2943. $imagesGalleryFn[] = $uriEnc;
  2944. }
  2945. }
  2946. $documentsArr = json_decode($equipment->DOCUMENTOS);
  2947. $documentsFn = [];
  2948. foreach($documentsArr as $document){
  2949. $codeArr = explode('=',$document);
  2950. $codeArr0 = explode('-', $codeArr[0]);
  2951. $file = DB::table('S002V01TAFAL')->where([
  2952. ['AFAL_NULI', '=', $line],
  2953. ['AFAL_COMO', '=', $codeArr0[1]],
  2954. ['AFAL_CLDO', '=', $codeArr0[2]],
  2955. ['AFAL_FECR', '=', $codeArr0[3]],
  2956. ['AFAL_NUSE', '=', $codeArr0[4]],
  2957. ['AFAL_NUVE', '=', $codeArr[1]],
  2958. ])->first();
  2959. if(is_null($file)){
  2960. return $this->responseController->makeResponse(true, "El archivo $document no está registrado.", [], 404);
  2961. }
  2962. $documentsFn[] = [
  2963. 'id' => $this->encryptionController->encrypt($document),
  2964. 'name' => $document,
  2965. 'size' => $file->AFAL_TAMA
  2966. ];
  2967. }
  2968. $equipment->GALERIA_IMAGENES = json_encode($imagesGalleryFn);
  2969. $equipment->DOCUMENTOS = json_encode($documentsFn);
  2970. $provider = DB::table('S002V01TPROV')->where([
  2971. ['PROV_NULI', '=', $line],
  2972. ['PROV_NUPR', '=', $equipment->PROVEEDOR],
  2973. ])->first();
  2974. $equipment->PROVEEDOR = $provider->PROV_NOCO . " (" . $equipment->PROVEEDOR . ")";
  2975. $equipment->NUMERO_SERIAL = $this->encryptionController->encrypt($equipment->NUMERO_SERIAL);
  2976. $codesHistory = json_decode($equipment->HISTORIAL_CODIGOS, true);
  2977. $codesHistoryFn = [];
  2978. foreach($codesHistory as $code){
  2979. $codeEnc = $this->encryptionController->encrypt($code);
  2980. $codesHistoryFn[] = $codeEnc;
  2981. }
  2982. $equipment->HISTORIAL_CODIGOS = json_encode($codesHistoryFn);
  2983. $usrReg = DB::table('S002V01TUSUA')->where([
  2984. ['USUA_NULI', '=', $line],
  2985. ['USUA_IDUS', '=', $equipment->USUARIO_REGISTRO]
  2986. ])->first();
  2987. $usrRegName = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  2988. $equipment->USUARIO_REGISTRO = $usrRegName . " (" . $equipment->USUARIO_REGISTRO . ")";
  2989. if(!is_null($equipment->USUARIO_MODIFICACION)){
  2990. $usrMod = DB::table('S002V01TUSUA')->where([
  2991. ['USUA_NULI', '=', $line],
  2992. ['USUA_IDUS', '=', $equipment->USUARIO_MODIFICACION]
  2993. ])->first();
  2994. $usrModName = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  2995. $equipment->USUARIO_MODIFICACION = $usrModName . " (" . $equipment->USUARIO_MODIFICACION . ")";
  2996. }
  2997. $equipment->TIPO_EQUIPAMIENTO = $this->encryptionController->encrypt($equipment->TIPO_EQUIPAMIENTO);
  2998. $equipment->MODELO = $this->encryptionController->encrypt($equipment->MODELO);
  2999. $now = $this->functionsController->now();
  3000. $nowStr = $now->toDateTimeString();
  3001. $actions = DB::getQueryLog();
  3002. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3003. $idac = $this->functionsController->registerActivity(
  3004. $line,
  3005. 'S002V01M07GEEQ',
  3006. 'S002V01F01ADEQ',
  3007. 'S002V01P01MEEQ',
  3008. 'Consulta',
  3009. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el equipamiento $code.",
  3010. $idUser,
  3011. $nowStr,
  3012. );
  3013. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  3014. return $this->responseController->makeResponse(false, 'EXITO.', $equipment);
  3015. }
  3016. public function getTrainOccupations($idUser, $line) {
  3017. DB::enableQueryLog();
  3018. $idUser = $this->encryptionController->decrypt($idUser);
  3019. if(!$idUser){
  3020. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  3021. }
  3022. $usr = DB::table('S002V01TUSUA')->where([
  3023. ['USUA_NULI', '=', $line],
  3024. ['USUA_IDUS', '=', $idUser],
  3025. ])->first();
  3026. if(is_null($usr)){
  3027. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  3028. }
  3029. $ubic = $this->templatesUbic;
  3030. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  3031. $trainOccupationsEnc = file_get_contents($ubic . "train-occupations.sam");
  3032. $trainOccupationsDec = $this->encryptionController->decrypt($trainOccupationsEnc);
  3033. $trainOccupationsArr = json_decode($trainOccupationsDec, true);
  3034. $now = $this->functionsController->now();
  3035. $nowStr = $now->toDateTimeString();
  3036. $actions = DB::getQueryLog();
  3037. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3038. $idac = $this->functionsController->registerActivity(
  3039. $line,
  3040. 'S002V01M07GEEQ',
  3041. 'S002V01F01ADEQ',
  3042. '-',
  3043. 'Consulta',
  3044. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las ocupaciones de tren.",
  3045. $idUser,
  3046. $nowStr,
  3047. );
  3048. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  3049. return $this->responseController->makeResponse(false, 'EXITO.', $trainOccupationsArr);
  3050. }
  3051. public function getTrainAreas($trainOccupation, $idUser, $line) {
  3052. DB::enableQueryLog();
  3053. $idUser = $this->encryptionController->decrypt($idUser);
  3054. if(!$idUser){
  3055. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  3056. }
  3057. $usr = DB::table('S002V01TUSUA')->where([
  3058. ['USUA_NULI', '=', $line],
  3059. ['USUA_IDUS', '=', $idUser],
  3060. ])->first();
  3061. if(is_null($usr)){
  3062. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  3063. }
  3064. $trainOccupation = $this->encryptionController->decrypt($trainOccupation);
  3065. if(!$trainOccupation){
  3066. return $this->responseController->makeResponse(true, 'La ocupación no fue encriptada correctamente.', [], 400);
  3067. }
  3068. $ubic = $this->templatesUbic;
  3069. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  3070. $carAreasEnc = file_get_contents($ubic . "car-areas.sam");
  3071. $carAreasDec = $this->encryptionController->decrypt($carAreasEnc);
  3072. $carAreasArr = json_decode($carAreasDec, true);
  3073. $carAreasArrDec = [];
  3074. foreach($carAreasArr as $key=>$val){
  3075. $keyDec = $this->encryptionController->decrypt($key);
  3076. $carAreasArrDec[$keyDec] = $val;
  3077. }
  3078. if(!array_key_exists($trainOccupation, $carAreasArrDec)){
  3079. return $this->responseController->makeResponse(true, "La ocupación $trainOccupation no existe.", [], 404);
  3080. }
  3081. $now = $this->functionsController->now();
  3082. $nowStr = $now->toDateTimeString();
  3083. $actions = DB::getQueryLog();
  3084. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3085. $idac = $this->functionsController->registerActivity(
  3086. $line,
  3087. 'S002V01M07GEEQ',
  3088. 'S002V01F01ADEQ',
  3089. '-',
  3090. 'Consulta',
  3091. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las áreas de la ocupación $trainOccupation de tren.",
  3092. $idUser,
  3093. $nowStr,
  3094. );
  3095. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  3096. return $this->responseController->makeResponse(false, 'EXITO.', $carAreasArrDec[$trainOccupation]);
  3097. }
  3098. public function getTrainElements($idUser, $line) {
  3099. DB::enableQueryLog();
  3100. $idUser = $this->encryptionController->decrypt($idUser);
  3101. if(!$idUser){
  3102. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  3103. }
  3104. $usr = DB::table('S002V01TUSUA')->where([
  3105. ['USUA_NULI', '=', $line],
  3106. ['USUA_IDUS', '=', $idUser],
  3107. ])->first();
  3108. if(is_null($usr)){
  3109. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  3110. }
  3111. $ubic = $this->templatesUbic;
  3112. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  3113. $trainElementsEnc = file_get_contents($ubic . "train-elements.sam");
  3114. $trainElementsDec = $this->encryptionController->decrypt($trainElementsEnc);
  3115. $trainElementsArr = json_decode($trainElementsDec, true);
  3116. $now = $this->functionsController->now();
  3117. $nowStr = $now->toDateTimeString();
  3118. $actions = DB::getQueryLog();
  3119. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3120. $idac = $this->functionsController->registerActivity(
  3121. $line,
  3122. 'S002V01M07GEEQ',
  3123. 'S002V01F01ADEQ',
  3124. '-',
  3125. 'Consulta',
  3126. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los elementos de tren.",
  3127. $idUser,
  3128. $nowStr,
  3129. );
  3130. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  3131. return $this->responseController->makeResponse(false, 'EXITO.', $trainElementsArr);
  3132. }
  3133. }