EquipmentManagementController.php 154 KB

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