EquipmentManagementController.php 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233
  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,7',
  1044. 'origin_location' => 'required_unless:code_type,7|string|nullable',
  1045. 'origin_level' => 'required_unless:code_type,7|string|nullable',
  1046. 'origin_occupation' => 'required_unless:code_type,7|string|nullable',
  1047. 'origin_element' => 'required_unless:code_type,7|string|max:150|nullable',
  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' => 'nullable|string',
  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. 'warehouse' => 'required_if:code_type,=,7|string',
  1073. 'area' => 'required_if:code_type,=,7|string',
  1074. 'level' => 'required_if:code_type,=,7|string',
  1075. 'zone' => 'required_if:code_type,=,7|string',
  1076. 'number_items' => 'required|integer',
  1077. ]);
  1078. if($validator->fails()){
  1079. return $this->responseController->makeResponse(
  1080. true,
  1081. "Se encontraron uno o más errores.",
  1082. $this->responseController->makeErrors(
  1083. $validator->errors()->messages()
  1084. ),
  1085. 401
  1086. );
  1087. }
  1088. $form = $request->all();
  1089. // Desencriptación del usuario
  1090. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1091. if(!$idUser){
  1092. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1093. }
  1094. // Obtención del usuario
  1095. $usr = DB::table('S002V01TUSUA')->where([
  1096. ['USUA_NULI', '=', $form['linea']],
  1097. ['USUA_IDUS', '=', $idUser]
  1098. ])->first();
  1099. // Verificación de la existencia del usuario
  1100. if(is_null($usr)){
  1101. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1102. }
  1103. // El código del equipo padre se inicializa
  1104. $eqpa = null;
  1105. // Si la jerarquía es de tipo "Hijo", Entonces
  1106. if($form['hierarchy'] == 'Hijo'){
  1107. // Se desencripta el código del equipamiento padre
  1108. $eqpa = $this->encryptionController->decrypt($form['parent_equipment']);
  1109. if(!$eqpa){
  1110. return $this->responseController->makeResponse(true, 'El ID del equipamiento padre no fue encriptado correctamente.', [], 400);
  1111. }
  1112. // Se obtiene la información del equipo padre
  1113. $parentEquipment = DB::table('S002V01TEQUI')->where([
  1114. ['EQUI_NULI', '=', $form['linea']],
  1115. ['EQUI_COEQ', '=', $eqpa],
  1116. ])->first();
  1117. // Se verifica que el equipo padre exista
  1118. if(is_null($parentEquipment)){
  1119. return $this->responseController->makeResponse(true, 'El equipamiento padre no existe.', [], 404);
  1120. }
  1121. }
  1122. // Se obtiene la ubicación de las plantillas
  1123. $ubic = $this->templatesUbic;
  1124. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  1125. $locationsEnc = file_get_contents($ubic . "locations.sam");
  1126. $locationsDec = $this->encryptionController->decrypt($locationsEnc);
  1127. $locationsArr = json_decode($locationsDec, true);
  1128. $locationsArrDec = [];
  1129. foreach($locationsArr as $location){
  1130. $locationStr = $this->encryptionController->decrypt($location['LOCATION']);
  1131. $code = $this->encryptionController->decrypt($location['CODE']);
  1132. $locationsArrDec[] = [
  1133. 'LOCATION' => $locationStr,
  1134. 'CODE' => $code
  1135. ];
  1136. }
  1137. $originLocation = null;
  1138. $originLevel = null;
  1139. $originOccupationStr = null;
  1140. $idWarehouse = null;
  1141. $idArea = null;
  1142. $idLevel = null;
  1143. $idZone = null;
  1144. if ($form['code_type'] !== '7') {
  1145. // Se desencripta la ubicación de origen
  1146. $originLocation = $this->encryptionController->decrypt($form['origin_location']);
  1147. if(!$originLocation){
  1148. return $this->responseController->makeResponse(true, 'La ubicación de origen no fue encriptada correctamente.', [], 400);
  1149. }
  1150. // Se verifica con la información del sistema
  1151. $originLocationFilt = array_filter($locationsArrDec, function($v, $k) use ($originLocation) {
  1152. return $v['CODE'] == $originLocation;
  1153. }, ARRAY_FILTER_USE_BOTH);
  1154. // Se verifica que la ubicacion exista en la información del sistema
  1155. if(count($originLocationFilt) < 1){
  1156. return $this->responseController->makeResponse(true, 'La ubicación de origen no se encuentra registrada en el sistema.', [], 404);
  1157. }
  1158. // Se desencripta el nivel del origen
  1159. $originLevel = $this->encryptionController->decrypt($form['origin_level']);
  1160. if(!$originLevel){
  1161. return $this->responseController->makeResponse(true, 'El nivel de origen no fue encriptado correctamente.', [], 400);
  1162. }
  1163. // Se obtienen los niveles disponibles
  1164. $supportedLevels = [];
  1165. for($i = 5; $i > 0; $i--){
  1166. $supportedLevels[] = "S0$i";
  1167. }
  1168. for($i = 0; $i <= 15; $i++){
  1169. $tag = $i < 10 ? "P0$i" : "P$i";
  1170. $supportedLevels[] = $tag;
  1171. }
  1172. // Se verifica que el origen sea válido
  1173. if(!in_array($originLevel, $supportedLevels)){
  1174. return $this->responseController->makeResponse(true, 'El nivel de origen seleccionado es inválido.', [], 400);
  1175. }
  1176. // Se desencripta la ocupación de orden
  1177. $originOccupationStr = $this->encryptionController->decrypt($form['origin_occupation']);
  1178. if(!$originOccupationStr){
  1179. return $this->responseController->makeResponse(true, 'La ocupación de origen no fue encriptada correctamente.', [], 400);
  1180. }
  1181. // Se obtiene la información de la ocupación
  1182. $originOccupationArea = substr($originOccupationStr, 0, 2);
  1183. $originOccupationID = substr($originOccupationStr, 2);
  1184. $originOccupation = DB::table('S002V01TOCUP')->where([
  1185. ['OCUP_NULI', '=', $form['linea']],
  1186. ['OCUP_AREA', '=', $originOccupationArea],
  1187. ['OCUP_IDOC', '=', $originOccupationID]
  1188. ])->first();
  1189. // Se verifica que exista la ocupació
  1190. if(is_null($originOccupation)){
  1191. return $this->responseController->makeResponse(true, 'La ocupación de origen seleccionada no existe.', [], 404);
  1192. }
  1193. } else {
  1194. $form['origin_element'] = null;
  1195. $idWarehouse = $this->encryptionController->decrypt($form['warehouse']);
  1196. if ($idWarehouse === false) {
  1197. return $this->responseController->makeResponse(true, 'El almacen no está encriptado correctamente.', [], 404);
  1198. }
  1199. $idArea = $this->encryptionController->decrypt($form['area']);
  1200. if ($idArea === false) {
  1201. return $this->responseController->makeResponse(true, 'El área no está encriptado correctamente.', [], 404);
  1202. }
  1203. $idLevel = $this->encryptionController->decrypt($form['level']);
  1204. if ($idLevel === false) {
  1205. return $this->responseController->makeResponse(true, 'El nivel no está encriptado correctamente.', [], 404);
  1206. }
  1207. $idZone = $this->encryptionController->decrypt($form['zone']);
  1208. if ($idZone === false) {
  1209. return $this->responseController->makeResponse(true, 'La zona no está encriptado correctamente.', [], 404);
  1210. }
  1211. try {
  1212. $validateWarehouse = DB::table('S002V01TALMA')
  1213. ->where('ALMA_COAL', '=', $idWarehouse)
  1214. ->where('ALMA_NULI', '=', $form['linea'])
  1215. ->exists();
  1216. } catch (\Throwable $th) {
  1217. return $this->responseController->makeResponse(true, 'Ocurrió un error al validar el almacen.', [], 500);
  1218. }
  1219. if (!$validateWarehouse) {
  1220. return $this->responseController->makeResponse(true, 'El almacen no existe.', [], 500);
  1221. }
  1222. try {
  1223. $validateArea = DB::table('S002V01TAREA')
  1224. ->where('AREA_COAL', '=', $idWarehouse)
  1225. ->where('AREA_COAR', '=', $idArea)
  1226. ->where('AREA_NULI', '=', $form['linea'])
  1227. ->exists();
  1228. } catch (\Throwable $th) {
  1229. return $this->responseController->makeResponse(true, 'Ocurrió un error al validar el almacen.', [], 500);
  1230. }
  1231. if (!$validateArea) {
  1232. return $this->responseController->makeResponse(true, 'El almacen no existe.', [], 500);
  1233. }
  1234. try {
  1235. $validateLevel = DB::table('S002V01TNIVE')
  1236. ->where('NIVE_COAL', '=', $idWarehouse)
  1237. ->where('NIVE_COAR', '=', $idArea)
  1238. ->where('NIVE_CONI', '=', $idLevel)
  1239. ->where('NIVE_NULI', '=', $form['linea'])
  1240. ->exists();
  1241. } catch (\Throwable $th) {
  1242. return $this->responseController->makeResponse(true, 'Ocurrió un error al validar el almacen.', [], 500);
  1243. }
  1244. if (!$validateLevel) {
  1245. return $this->responseController->makeResponse(true, 'El almacen no existe.', [], 500);
  1246. }
  1247. try {
  1248. $validateZone = DB::table('S002V01TZONA')
  1249. ->where('ZONA_COAL', '=', $idWarehouse)
  1250. ->where('ZONA_COAR', '=', $idArea)
  1251. ->where('ZONA_CONI', '=', $idLevel)
  1252. ->where('ZONA_COZO', '=', $idZone)
  1253. ->where('ZONA_NULI', '=', $form['linea'])
  1254. ->exists();
  1255. } catch (\Throwable $th) {
  1256. return $this->responseController->makeResponse(true, 'Ocurrió un error al validar el almacen.', [], 500);
  1257. }
  1258. if (!$validateZone) {
  1259. return $this->responseController->makeResponse(true, 'El almacen no existe.', [], 500);
  1260. }
  1261. }
  1262. // Se desencripta la familia
  1263. $familyCode = $this->encryptionController->decrypt($form['family']);
  1264. if(!$familyCode){
  1265. return $this->responseController->makeResponse(true, 'La familia del equipamiento no fue encriptada correctamente.', [], 400);
  1266. }
  1267. // Se obtienen los datos de la familia
  1268. $family = DB::table('S002V01TFAMI')->where([
  1269. ['FAMI_NULI', '=', $form['linea']],
  1270. ['FAMI_COFA', '=', $familyCode]
  1271. ])->first();
  1272. // Se verifica que la familia exista
  1273. if(is_null($family)){
  1274. return $this->responseController->makeResponse(true, 'La familia seleccionada no existe.', [], 404);
  1275. }
  1276. // Se desencripta la subfamilia
  1277. $subfamilyCode = $this->encryptionController->decrypt($form['subfamily']);
  1278. if(!$subfamilyCode){
  1279. return $this->responseController->makeResponse(true, 'La subfamilia del equipamiento no fue encriptada correctamente.', [], 400);
  1280. }
  1281. // Se obtienen los datos de la subfamilia
  1282. $subfamily = DB::table('S002V01TSUBF')->where([
  1283. ['SUBF_NULI', '=', $form['linea']],
  1284. ['SUBF_COFA', '=', $familyCode],
  1285. ['SUBF_COSU', '=', $subfamilyCode]
  1286. ])->first();
  1287. // Se verifica que la subfamilia exista
  1288. if(is_null($subfamily)){
  1289. return $this->responseController->makeResponse(true, 'La subfamilia seleccionada no existe.', [], 404);
  1290. }
  1291. // Se inicializa arreglo con los estados disponibles
  1292. $validStatus = ["A", "S", "T", "I", "R", "D"];
  1293. // Se desencripta el estado
  1294. $status = $this->encryptionController->decrypt($form['status']);
  1295. // Se verifica que el estado sea un valor válido
  1296. if(!$status){
  1297. return $this->responseController->makeResponse(true, 'La estado del equipamiento no fue encriptado correctamente.', [], 400);
  1298. }else if(!in_array($status, $validStatus)){
  1299. return $this->responseController->makeResponse(true, 'El estado seleccionado para el equipamiento es inválido.', [], 400);
  1300. }
  1301. // Se obtiene el arreglo de las imagenes
  1302. $imagesArr = json_decode($form['images'], true);
  1303. // Se verifica que el arreglo contenga imagenes
  1304. if(count($imagesArr) < 1){
  1305. return $this->responseController->makeResponse(true, 'El arreglo de la galería de imágenes está vacío.', [], 400);
  1306. }
  1307. $imagesGallery = [];
  1308. // Se iteran las imagenes
  1309. foreach($imagesArr as $imageFile){
  1310. // Se verifica que tenga un formato correcto
  1311. if(!array_key_exists('type', $imageFile)){
  1312. return $this->responseController->makeResponse(true, 'El arreglo de la galería de imágenes tiene un formato inválido.', [], 400);
  1313. // Si la imagen es nueva, entonces...
  1314. }else if($imageFile['type'] == 'Nuevo'){
  1315. // Se obtiene el ID de la imagen
  1316. $tempFileID = $this->encryptionController->decrypt($imageFile['id']);
  1317. // Se obtiene la información registrada en la tabla de archivos temporales
  1318. $tempFile = DB::table('S002V01TARTE')->where([
  1319. ['ARTE_IDAR', '=', $tempFileID],
  1320. ['ARTE_NULI', '=', $form['linea']]
  1321. ])->first();
  1322. // La imagen es colocada en su posición final
  1323. $finalFile = $this->documentManagementController->moveFinalFile($form['linea'], 'GEEQ', 'FO', $tempFile, $idUser);
  1324. // Si ocurrió un error, entonces se manda el error
  1325. if(!$finalFile[0]){
  1326. return $this->responseController->makeResponse(true, $finalFile[1], [], 400);
  1327. }
  1328. // La información es guardada en un arreglo
  1329. $imagesGallery[] = $finalFile[1];
  1330. }else{
  1331. // Se desencripta el id de la imagen
  1332. $fileID = $this->encryptionController->decrypt($imageFile['id']);
  1333. // Es guardada en un arreglo
  1334. $imagesGallery[] = $fileID;
  1335. }
  1336. }
  1337. // El arreglo de las imagenes es pasada en formato string
  1338. $imagesGalleryStr = json_encode($imagesGallery);
  1339. // Se obtiene el arreglo de los documentos
  1340. $documentsArr = json_decode($form['documents'], true);
  1341. if(count($documentsArr) < 1){
  1342. return $this->responseController->makeResponse(true, 'El arreglo de documentos relacionados está vacío.', [], 400);
  1343. }
  1344. $documents = [];
  1345. // Se iteran los documentos
  1346. foreach($documentsArr as $documentFile){
  1347. // Se verifica que tenga un formato correcto
  1348. if(!array_key_exists('type', $documentFile)){
  1349. return $this->responseController->makeResponse(true, 'El arreglo de documentos asociados tiene un formato inválido.', [], 400);
  1350. // Si el documento es nuevo, entonces...
  1351. }else if($documentFile['type'] == 'Nuevo'){
  1352. // Se obtiene el ID del documento
  1353. $tempFileID = $this->encryptionController->decrypt($documentFile['id']);
  1354. // Se obtiene la información registrada en la tabla de archivos temporales
  1355. $tempFile = DB::table('S002V01TARTE')->where([
  1356. ['ARTE_IDAR', '=', $tempFileID],
  1357. ['ARTE_NULI', '=', $form['linea']]
  1358. ])->first();
  1359. // El documento es colocada en su posición final
  1360. $finalFile = $this->documentManagementController->moveFinalFile($form['linea'], 'GEEQ', 'IN', $tempFile, $idUser);
  1361. // Si ocurrió un error, entonces se manda el error
  1362. if(!$finalFile[0]){
  1363. return $this->responseController->makeResponse(true, $finalFile[1], [], 400);
  1364. }
  1365. // La información es guardada en un arreglo
  1366. $documents[] = $finalFile[1];
  1367. }else{
  1368. // Se desencripta el id del documento
  1369. $fileID = $this->encryptionController->decrypt($documentFile['id']);
  1370. // Es guardada en un arreglo
  1371. $documents[] = $fileID;
  1372. }
  1373. }
  1374. // El arreglo de los documentos es pasada en formato string
  1375. $documentsStr = json_encode($documents);
  1376. // Se obtiene el formato de la fecha en formato manejable
  1377. $acquisitionDate = new Carbon($form['acquisition_date']);
  1378. $warrantyStartDate = new Carbon($form['warranty_start_date']);
  1379. $warrantyEndDate = new Carbon($form['warranty_end_date']);
  1380. // Se verifica que la fecha de inicio de la garantia sea menor a la fecha final de la garantia
  1381. if($warrantyStartDate->lt($acquisitionDate)){
  1382. return $this->responseController->makeResponse(true, 'La fecha de inicio de la grantía no puede ser menor a la fecha de adquisición.', [], 400);
  1383. }else if($warrantyEndDate->lte($acquisitionDate)){
  1384. 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);
  1385. }
  1386. // Se desencripta el numero del proveedor
  1387. $providerID = $this->encryptionController->decrypt($form['equipment_provider']);
  1388. if(!$providerID){
  1389. return $this->responseController->makeResponse(true, 'El proveedor del equipamiento no fue encriptado correctamente.', [], 400);
  1390. }
  1391. // Se obtiene la información del proveedor
  1392. $provider = DB::table('S002V01TPROV')->where([
  1393. ['PROV_NUPR', '=', $providerID],
  1394. ['PROV_NULI', '=', $form['linea']]
  1395. ])->first();
  1396. // Se verifica que el proveedor exista
  1397. if(is_null($provider)){
  1398. return $this->responseController->makeResponse(true, 'La proveedor seleccionada no existe.', [], 404);
  1399. }
  1400. $arrSerialNumber = array();
  1401. if ($form['serial_number'] !== '' && !is_null($form['serial_number'])) {
  1402. // Se desencripta el número de serie
  1403. $serialNumer = $this->encryptionController->decrypt($form['serial_number']);
  1404. if(!$serialNumer){
  1405. return $this->responseController->makeResponse(true, 'El número de serie del equipamiento no fue encriptado correctamente.', [], 400);
  1406. }
  1407. $arrSerialNumber = explode(',', $serialNumer);
  1408. if (intval($form['number_items']) !== count($arrSerialNumber)) {
  1409. return $this->responseController->makeResponse(true, 'La cantidad de número de series no corresponde con la cantidad de artículos a registrar.', [], 400);
  1410. }
  1411. foreach ($arrSerialNumber as $key => $serialNumber) {
  1412. // Se obtiene la información del equipamiento por medio del número de serie
  1413. $serialNmberDB = DB::table('S002V01TPCEQ')->where([
  1414. ['PCEQ_NULI', '=', $form['linea']],
  1415. ['PCEQ_NUSE', '=', $serialNumber]
  1416. ])->first();
  1417. // Se verifica que exista el código de serie
  1418. if(!is_null($serialNmberDB)){
  1419. $serialNumberStr = $serialNmberDB->EQUI_COEQ;
  1420. return $this->responseController->makeResponse(true, "El número de serie $serialNumber enviado ya se encuentra relacionado al equipamiento $serialNumberStr.", [], 401);
  1421. }
  1422. }
  1423. $temp_array = array_unique($arrSerialNumber);
  1424. $duplicates = sizeof($temp_array) != sizeof($arrSerialNumber);
  1425. if ($duplicates) {
  1426. return $this->responseController->makeResponse(true, 'Los número de serie no se pueden repetir.', [], 400);
  1427. }
  1428. } else {
  1429. try {
  1430. $getEquipment = DB::table('S002V01TPCEQ')
  1431. ->where('PCEQ_NULI', '=', $form['linea'])
  1432. ->where('PCEQ_NUSE', 'LIKE', '0-%')
  1433. ->get(['PCEQ_NUSE AS NUMERO_SERIE']);
  1434. } catch (\Throwable $th) {
  1435. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener los artículos.', $th->getMessage(), 400);
  1436. }
  1437. $lastSerie = 0;
  1438. if (count($getEquipment) > 0) {
  1439. foreach ($getEquipment as $key => $equipment) {
  1440. $serie = str_replace('0-', '', $equipment->NUMERO_SERIE);
  1441. $serie = intval($serie);
  1442. if (is_null($lastSerie)) {
  1443. $lastSerie = $serie;
  1444. } else if ($serie > $lastSerie) {
  1445. $lastSerie = $serie;
  1446. }
  1447. }
  1448. }
  1449. for ($i=0; $i < $form['number_items']; $i++) {
  1450. $lastSerie = $lastSerie + 1;
  1451. $arrSerialNumber[] = '0-'.$lastSerie;
  1452. }
  1453. }
  1454. if ( empty($arrSerialNumber) ) {
  1455. return $this->responseController->makeResponse(true, 'No hay números de serie disponibles.', [], 400);
  1456. }
  1457. // Se desencripta el código pregenerado en Angular
  1458. $preCode = $this->encryptionController->decrypt($form['pre_generated_code']);
  1459. if(!$preCode){
  1460. return $this->responseController->makeResponse(true, 'El código pre generado no fue encriptado correctamente.', [], 400);
  1461. }
  1462. // Se obtiene el código pregenerado en Laravel
  1463. $codeVerified = $this->verifyPreCode([
  1464. "line" => $form['linea'],
  1465. "originLocation" => $originLocation,
  1466. "originLevel" => $originLevel,
  1467. "originOccupation" => $originOccupationStr,
  1468. "originElement" => $form['origin_element'],
  1469. "warehouse" => $idWarehouse,
  1470. "area" => $idArea,
  1471. "level" => $idLevel,
  1472. "zone" => $idZone,
  1473. "family" => $familyCode,
  1474. "subfamily" => $subfamilyCode,
  1475. "status" => $status,
  1476. "equipmentType" => $form['equipment_type'],
  1477. "equipmentModel" => $form['equipment_model'],
  1478. "equipmentID" => "XXXXX",
  1479. "pccIntersection" => null,
  1480. "pccPosition" => null,
  1481. "originKilometers" => null,
  1482. "destinyLocation" => null,
  1483. "destinyLevel" => null,
  1484. "destinyOccupation" => null,
  1485. "destinyElement" => null,
  1486. "destinyKilometers" => null
  1487. ], $form['code_type'], $form['hierarchy'], $eqpa);
  1488. // Se verifica que el código pregenerado en Angular y el pregenerado en Laravel
  1489. if($codeVerified != $preCode){
  1490. 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);
  1491. }
  1492. // Se verifica que la información del sfotware
  1493. $deso = isset($form['software_name']) ? $form['software_name'] : null;
  1494. $veso = isset($form['software_version']) ? $form['software_version'] : null;
  1495. // Se obtiene la fecha actual
  1496. $now = $this->functionsController->now();
  1497. $nowStr = $now->toDateTimeString();
  1498. $arrPreCodification = array();
  1499. $numberItems = intval($form['number_items']);
  1500. for ($i=0; $i < $numberItems; $i++) {
  1501. // Se ingresa la información a la tabla de los precódigos de los equipamientos
  1502. $idPreCodification = DB::table('S002V01TPCEQ')->insertGetId([
  1503. 'PCEQ_NULI' => $form['linea'],
  1504. 'PCEQ_CPGE' => $preCode,
  1505. 'PCEQ_JERA' => $form['hierarchy'],
  1506. 'PCEQ_EQPA' => $eqpa,
  1507. 'PCEQ_TICO' => $form['code_type'],
  1508. 'PCEQ_UBOR' => $originLocation,
  1509. 'PCEQ_NIOR' => $originLevel,
  1510. 'PCEQ_OCOR' => $originOccupationStr,
  1511. 'PCEQ_ELOR' => $form['origin_element'],
  1512. 'PCEQ_ALMA' => $idWarehouse,
  1513. 'PCEQ_AREA' => $idArea,
  1514. 'PCEQ_NIVE' => $idLevel,
  1515. 'PCEQ_ZONA' => $idZone,
  1516. 'PCEQ_FAMI' => $familyCode,
  1517. 'PCEQ_SUBF' => $subfamilyCode,
  1518. 'PCEQ_ESEQ' => $status,
  1519. 'PCEQ_TIEQ' => $form['equipment_type'],
  1520. 'PCEQ_MOEQ' => $form['equipment_model'],
  1521. 'PCEQ_IPCC' => null,
  1522. 'PCEQ_PPCC' => null,
  1523. 'PCEQ_KIOR' => 0.0,
  1524. 'PCEQ_UBDE' => '',
  1525. 'PCEQ_NIDE' => '',
  1526. 'PCEQ_OCDE' => '',
  1527. 'PCEQ_ELDE' => '',
  1528. 'PCEQ_KIDE' => 0.0,
  1529. 'PCEQ_FEAD' => $form['acquisition_date'],
  1530. 'PCEQ_FIGA' => $form['warranty_start_date'],
  1531. 'PCEQ_FTGA' => $form['warranty_end_date'],
  1532. 'PCEQ_PREQ' => $providerID,
  1533. 'PCEQ_NUSE' => count($arrSerialNumber) === 1 ? $arrSerialNumber[0] : $arrSerialNumber[$i],
  1534. 'PCEQ_DESO' => $deso,
  1535. 'PCEQ_VESO' => $veso,
  1536. 'PCEQ_GAIM' => $imagesGalleryStr,
  1537. 'PCEQ_DORE' => $documentsStr,
  1538. 'PCEQ_USRE' => $idUser,
  1539. 'PCEQ_FERE' => $nowStr,
  1540. ]);
  1541. $idPreCodification = $this->encryptionController->encrypt($idPreCodification);
  1542. $arrPreCodification[] = $idPreCodification;
  1543. }
  1544. $actions = DB::getQueryLog();
  1545. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1546. $idac = $this->functionsController->registerActivity(
  1547. $form['linea'],
  1548. 'S002V01M07GEEQ',
  1549. 'S002V01F01ADEQ',
  1550. 'S002V01P11REEQ',
  1551. 'Registro',
  1552. "El usuario $name (" . $usr->USUA_IDUS . ") solicitó el registro del equipamiento $preCode.",
  1553. $idUser,
  1554. $nowStr,
  1555. );
  1556. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1557. return $this->responseController->makeResponse(false, 'EXITO.', $arrPreCodification);
  1558. }
  1559. public function verifyPreCode(array $parameters, string $type, string $mode, string | null $parentCode = '') : string {
  1560. $code = "";
  1561. if($mode == 'Hijo'){
  1562. $code .= $parentCode;
  1563. $equipment = ['equipmentType', 'equipmentModel', 'equipmentID'];
  1564. $equipmentStr = "";
  1565. foreach($equipment as $ctrl){
  1566. $val = $parameters[$ctrl];
  1567. if($ctrl == 'equipmentModel'){
  1568. $val = $this->processElement($val, 'model');
  1569. }else if($ctrl == 'equipmentType'){
  1570. $val = $this->processElement($val, 'element');
  1571. }
  1572. $equipmentStr .= $val . '-';
  1573. }
  1574. $equipmentStr = substr($equipmentStr, 0, -1);
  1575. $code .= "." . $equipmentStr;
  1576. }else{
  1577. switch($type){
  1578. case "1":
  1579. //CASO 1: Codificación general
  1580. $lbs = ['line', 'originLocation', 'originLevel', 'originOccupation', 'originElement'];
  1581. $lbsStr = "";
  1582. foreach($lbs as $ctrl){
  1583. $val = $parameters[$ctrl];
  1584. if($ctrl == 'line'){
  1585. $val = intval($val) < 10 ? "0$val" : "$val";
  1586. }else if($ctrl == 'originElement'){
  1587. $val = $this->processElement($val, 'element');
  1588. }
  1589. $lbsStr .= $val . ".";
  1590. }
  1591. $lbsStr = substr($lbsStr, 0, -1);
  1592. $pbs = ['family', 'subfamily', 'status'];
  1593. $pbsStr = "";
  1594. foreach($pbs as $ctrl){
  1595. $val = $parameters[$ctrl];
  1596. $pbsStr .= $val . ".";
  1597. }
  1598. $pbsStr = substr($pbsStr, 0, -1);
  1599. $equipment = ['equipmentType', 'equipmentModel', 'equipmentID'];
  1600. $equipmentStr = "";
  1601. foreach($equipment as $ctrl){
  1602. $val = $parameters[$ctrl];
  1603. if($ctrl == 'equipmentModel'){
  1604. $val = $this->processElement($val, 'model');
  1605. }else if($ctrl == 'equipmentType'){
  1606. $val = $this->processElement($val, 'element');
  1607. }
  1608. $equipmentStr .= $val . '-';
  1609. }
  1610. $equipmentStr = substr($equipmentStr, 0, -1);
  1611. $code = $lbsStr . "_" . $pbsStr . "." . $equipmentStr;
  1612. break;
  1613. case "7":
  1614. $siteTag = '';
  1615. $site = ['line', 'area', 'level'];
  1616. foreach($site as $ctrl){
  1617. $val = $parameters[$ctrl];
  1618. if($ctrl == 'line'){
  1619. $val = intval($val) < 10 ? "0$val" : "$val";
  1620. }
  1621. $siteTag .= $val.'.';
  1622. }
  1623. $siteTag = substr($siteTag, 0, -1);
  1624. $areaTag = '';
  1625. $area = ['warehouse', 'zone'];
  1626. foreach($area as $ctrl){
  1627. $val = $parameters[$ctrl];
  1628. $areaTag .= $val.'.';
  1629. }
  1630. $areaTag = substr($areaTag, 0, -1);
  1631. $technicalSpecificationTag = '';
  1632. $technicalSpecification = ['family', 'subfamily', 'status'];
  1633. foreach($technicalSpecification as $ctrl){
  1634. $val = $parameters[$ctrl];
  1635. $technicalSpecificationTag .= $val.'.';
  1636. }
  1637. $technicalSpecificationTag = substr($technicalSpecificationTag, 0, -1);
  1638. $equipmentIdentifierTag = '';
  1639. $equipmentIdentifier = ['equipmentType', 'equipmentModel', 'equipmentID'];
  1640. foreach($equipmentIdentifier as $ctrl){
  1641. $val = $parameters[$ctrl];
  1642. if($ctrl == 'equipmentModel'){
  1643. $val = $this->processElement($val, 'model');
  1644. }else if($ctrl == 'equipmentType'){
  1645. $val = $this->processElement($val, 'element');
  1646. }
  1647. $equipmentIdentifierTag .= $val.'-';
  1648. }
  1649. $equipmentIdentifierTag = substr($equipmentIdentifierTag, 0, -1);
  1650. $code = $siteTag.'-'.$areaTag.'_'.$technicalSpecificationTag.'.'.$equipmentIdentifierTag;
  1651. break;
  1652. }
  1653. }
  1654. return $code;
  1655. }
  1656. private function processElement(string $value, string $type) : string {
  1657. $value = strtoupper($value);
  1658. $validVal0 = $this->functionsController->unaccent($value);
  1659. $CONNECTORS = ['DE', 'PARA', 'Y', 'O', 'DEL', 'EL', 'LA', 'LOS', 'POR', 'EN'];
  1660. $valueArr1 = explode(' ', $validVal0);
  1661. $validVal1 = [];
  1662. foreach($valueArr1 as $word){
  1663. if(!in_array($word, $CONNECTORS)){
  1664. $validVal1[] = $word;
  1665. }
  1666. }
  1667. $validLength = count($validVal1);
  1668. $validVal2 = "";
  1669. if($validLength < 1){
  1670. return "ERROR";
  1671. }else if($validLength == 1){
  1672. if(strlen($validVal1[0]) < 5){
  1673. $validVal2 = $validVal1[0];
  1674. for($i = strlen($validVal1[0]); $i < 5; $i++){
  1675. if($type == 'model'){
  1676. $validVal2 = "X$validVal2";
  1677. }else{
  1678. $validVal2 = $validVal2 . "X";
  1679. }
  1680. }
  1681. }else{
  1682. $validVal2 = substr($validVal1[0], 0, 5);
  1683. }
  1684. }else if($validLength == 2){
  1685. $word1 = "";
  1686. if(strlen($validVal1[0]) < 2){
  1687. if($type == 'model'){
  1688. $word1 = "X$validVal1[0]";
  1689. }else{
  1690. $word1 = $validVal1[0] . "X";
  1691. }
  1692. }else{
  1693. $word1 = substr($validVal1[0], 0, 2);
  1694. }
  1695. $word2 = "";
  1696. if(strlen($validVal1[1]) < 3){
  1697. $word2 = $validVal1[1];
  1698. for($i = strlen($validVal1[1]); $i < 3; $i++){
  1699. if($type == 'model'){
  1700. $word2 = "X$word2";
  1701. }else{
  1702. $word2 = $word2 . "X";
  1703. }
  1704. }
  1705. }else{
  1706. $word2 = substr($validVal1[1], 0, 3);
  1707. }
  1708. $validVal2 = $word1 . $word2;
  1709. }else if($validLength == 3){
  1710. $word1 = "";
  1711. if(strlen($validVal1[0]) < 2){
  1712. if($type == 'model'){
  1713. $word1 = "X$validVal1[0]";
  1714. }else{
  1715. $word1 = $validVal1[0] . "X";
  1716. }
  1717. }else{
  1718. $word1 = substr($validVal1[0], 0, 2);
  1719. }
  1720. $word2 = substr($validVal1[1], 0, 1);
  1721. $word3 = "";
  1722. if(strlen($validVal1[2]) < 2){
  1723. if($type == 'model'){
  1724. $word3 = "X$validVal1[2]";
  1725. }else{
  1726. $word3 = $validVal1[2] . "X";
  1727. }
  1728. }else{
  1729. $word3 = substr($validVal1[2], 0, 2);
  1730. }
  1731. $validVal2 = $word1 . $word2 . $word3;
  1732. }else if($validLength == 4){
  1733. $word1 = "";
  1734. if(strlen($validVal1[0]) < 2){
  1735. if($type == 'model'){
  1736. $word1 = "X$validVal1[0]";
  1737. }else{
  1738. $word1 = $validVal1[0] . "X";
  1739. }
  1740. }else{
  1741. $word1 = substr($validVal1[0], 0, 2);
  1742. }
  1743. $word2 = substr($validVal1[1], 0, 1);
  1744. $word3 = substr($validVal1[2], 0, 1);
  1745. $word4 = substr($validVal1[3], 0, 1);
  1746. $validVal2 = $word1 . $word2 . $word3 . $word4;
  1747. }else if($validLength >= 5){
  1748. $word1 = substr($validVal1[0], 0, 1);
  1749. $word2 = substr($validVal1[1], 0, 1);
  1750. $word3 = substr($validVal1[2], 0, 1);
  1751. $word4 = substr($validVal1[3], 0, 1);
  1752. $word5 = substr($validVal1[4], 0, 1);
  1753. $validVal2 = $word1 . $word2 . $word3 . $word4 . $word5;
  1754. }else{
  1755. return "ERROR";
  1756. }
  1757. return $validVal2;
  1758. }
  1759. public function getPendigEquipments($idUser, $line) {
  1760. DB::enableQueryLog();
  1761. $idUser = $this->encryptionController->decrypt($idUser);
  1762. if(!$idUser){
  1763. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  1764. }
  1765. $usr = DB::table('S002V01TUSUA')->where([
  1766. ['USUA_NULI', '=', $line],
  1767. ['USUA_IDUS', '=', $idUser],
  1768. ])->first();
  1769. if(is_null($usr)){
  1770. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1771. }
  1772. $pendingEquipments = DB::table('S002V01TPCEQ')->select([
  1773. 'PCEQ_IDPR AS IDREG',
  1774. 'PCEQ_CPGE AS CODIGO',
  1775. 'PCEQ_TICO AS TIPO_CODIGO',
  1776. 'PCEQ_TIEQ AS TIPO_EQUIPAMIENTO',
  1777. 'PCEQ_MOEQ AS MODELO_EQUIPAMIENTO',
  1778. 'PCEQ_GAIM AS GALERIA_IMAGENES',
  1779. 'PCEQ_ESRE AS ESTADO_REGISTRO',
  1780. ])->where([
  1781. ['PCEQ_NULI', '=', $line],
  1782. ['PCEQ_ESRE', '=', 'Revisión'],
  1783. ])->get()->all();
  1784. $idUserEnc = $this->encryptionController->encrypt($idUser);
  1785. foreach($pendingEquipments as $key=>$equipment){
  1786. $equipment->IDREG = $this->encryptionController->encrypt($equipment->IDREG);
  1787. $equipment->CODIGO = $this->encryptionController->encrypt($equipment->CODIGO);
  1788. $imagesGalleryArr = json_decode($equipment->GALERIA_IMAGENES);
  1789. $imagesGalleryFn = [];
  1790. foreach($imagesGalleryArr as $imageCode){
  1791. $imageCodeEnc = $this->encryptionController->encrypt($imageCode);
  1792. $publicUri = $this->documentManagementController->getPublicDocumentURL(
  1793. $imageCodeEnc,
  1794. $idUserEnc,
  1795. $line
  1796. );
  1797. $response = json_decode($publicUri->original, true);
  1798. if($response['error']){
  1799. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  1800. }else{
  1801. $uriEnc = $this->encryptionController->encrypt($response['response']['public_uri']);
  1802. $imagesGalleryFn[] = $uriEnc;
  1803. }
  1804. }
  1805. $equipment->GALERIA_IMAGENES = json_encode($imagesGalleryFn);
  1806. $equipment->TIPO_EQUIPAMIENTO = $this->encryptionController->encrypt($equipment->TIPO_EQUIPAMIENTO);
  1807. $equipment->MODELO_EQUIPAMIENTO = $this->encryptionController->encrypt($equipment->MODELO_EQUIPAMIENTO);
  1808. $pendingEquipments[$key] = $equipment;
  1809. }
  1810. $now = $this->functionsController->now();
  1811. $nowStr = $now->toDateTimeString();
  1812. $actions = DB::getQueryLog();
  1813. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1814. $idac = $this->functionsController->registerActivity(
  1815. $line,
  1816. 'S002V01M07GEEQ',
  1817. 'S002V01F01ADEQ',
  1818. '-',
  1819. 'Consulta',
  1820. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los equipamientos en revisión.",
  1821. $idUser,
  1822. $nowStr,
  1823. );
  1824. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1825. return $this->responseController->makeResponse(false, 'EXITO.', $pendingEquipments);
  1826. }
  1827. public function getPendigEquipment($idReg, $idUser, $line) {
  1828. DB::enableQueryLog();
  1829. $idUser = $this->encryptionController->decrypt($idUser);
  1830. if(!$idUser){
  1831. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  1832. }
  1833. $usr = DB::table('S002V01TUSUA')->where([
  1834. ['USUA_NULI', '=', $line],
  1835. ['USUA_IDUS', '=', $idUser],
  1836. ])->first();
  1837. if(is_null($usr)){
  1838. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1839. }
  1840. $idReg = $this->encryptionController->decrypt($idReg);
  1841. if(!$idReg){
  1842. return $this->responseController->makeResponse(true, 'El ID del pre-registro no está encriptado correctamente', [], 400);
  1843. }
  1844. $pendingEquipment = DB::table('S002V01TPCEQ')->select([
  1845. 'PCEQ_IDPR AS IDREG',
  1846. 'PCEQ_CPGE AS CODIGO',
  1847. 'PCEQ_JERA AS JERARQUIA',
  1848. 'PCEQ_EQPA AS PADRE',
  1849. 'PCEQ_TICO AS TIPO_CODIGO',
  1850. 'PCEQ_UBOR AS UBICACION_ORIGEN',
  1851. 'PCEQ_NIOR AS NIVEL_ORIGEN',
  1852. 'PCEQ_OCOR AS OCUPACION_ORIGEN',
  1853. 'PCEQ_ELOR AS ELEMENTO_ORIGEN',
  1854. 'PCEQ_FAMI AS FAMILIA',
  1855. 'PCEQ_SUBF AS SUBFAMILIA',
  1856. 'PCEQ_ESEQ AS ESTADO_EQUIPAMIENTO',
  1857. 'PCEQ_TIEQ AS TIPO_EQUIPAMIENTO',
  1858. 'PCEQ_MOEQ AS MODELO_EQUIPAMIENTO',
  1859. 'PCEQ_IPCC AS INTERSECCION_PCC',
  1860. 'PCEQ_PPCC AS POSICION_PCC',
  1861. 'PCEQ_KIOR AS KILOMETRO_ORIGEN',
  1862. 'PCEQ_UBDE AS UBICACION_DESTINO',
  1863. 'PCEQ_NIDE AS NIVEL_DESTINO',
  1864. 'PCEQ_OCDE AS OCUPACION_DESTINO',
  1865. 'PCEQ_ELDE AS ELEMENTO_DESTINO',
  1866. 'PCEQ_KIDE AS KILOMETRO_DESTINO',
  1867. 'PCEQ_FEAD AS FECHA_ADQUISICION',
  1868. 'PCEQ_FIGA AS FECHA_INICIO_GARANTIA',
  1869. 'PCEQ_FTGA AS FECHA_FIN_GARANTIA',
  1870. 'PCEQ_PREQ AS PROVEEDOR',
  1871. 'PCEQ_NUSE AS NUMERO_SERIAL',
  1872. 'PCEQ_DESO AS DENOMINACION_SOFTWARE',
  1873. 'PCEQ_VESO AS VERSION_SOFTWARE',
  1874. 'PCEQ_GAIM AS GALERIA_IMAGENES',
  1875. 'PCEQ_DORE AS DOCUMENTOS',
  1876. 'PCEQ_ESRE AS ESTADO_REGISTRO',
  1877. 'PCEQ_CORE AS COMENTARIOS_RECHAZO',
  1878. 'PCEQ_COAC AS COMENTARIOS_ACEPTACION',
  1879. 'PCEQ_USRE AS USUARIO_REGISTRO',
  1880. 'PCEQ_FERE AS FECHA_REGISTRO',
  1881. 'PCEQ_USMO AS USUARIO_MODIFICO',
  1882. 'PCEQ_FEMO AS FECHA_MODIFICACION'
  1883. ])->where([
  1884. ['PCEQ_NULI', '=', $line],
  1885. ['PCEQ_IDPR', '=', $idReg],
  1886. ['PCEQ_ESRE', '=', 'Revisión'],
  1887. ])->first();
  1888. if(is_null($pendingEquipment)){
  1889. return $this->responseController->makeResponse(true, 'El pre-registro solicitado no existe', [], 404);
  1890. }
  1891. $pendingEquipment->IDREG = $this->encryptionController->encrypt($pendingEquipment->IDREG);
  1892. $pendingEquipment->CODIGO = $this->encryptionController->encrypt($pendingEquipment->CODIGO);
  1893. $ubic = $this->templatesUbic;
  1894. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  1895. $locationsEnc = file_get_contents($ubic . "locations.sam");
  1896. $locationsDec = $this->encryptionController->decrypt($locationsEnc);
  1897. $locationsArr = json_decode($locationsDec, true);
  1898. $locationsArrDec = [];
  1899. foreach($locationsArr as $location){
  1900. $locationStr = $this->encryptionController->decrypt($location['LOCATION']);
  1901. $code = $this->encryptionController->decrypt($location['CODE']);
  1902. $locationsArrDec[] = [
  1903. 'LOCATION' => $locationStr,
  1904. 'CODE' => $code
  1905. ];
  1906. }
  1907. $originLocation = $pendingEquipment->UBICACION_ORIGEN;
  1908. $originLocationFilt = array_filter($locationsArrDec, function ($v, $k) use ($originLocation) {
  1909. return $v['CODE'] == $originLocation;
  1910. }, ARRAY_FILTER_USE_BOTH);
  1911. $pendingEquipment->UBICACION_ORIGEN = end($originLocationFilt)['LOCATION'] . " (" . $pendingEquipment->UBICACION_ORIGEN . ")";
  1912. $levelsArr = [];
  1913. for($i = 5; $i > 0; $i--){
  1914. $levelsArr[] = [
  1915. 'LEVEL' => "Subterráneo S0$i",
  1916. 'CODE' => "S0$i",
  1917. ];
  1918. }
  1919. for($i = 0; $i <= 15; $i++){
  1920. $code = $i < 10 ? "P0$i" : "P$i";
  1921. $level = $i == 0 ? "Planta baja P00" : "Piso $code";
  1922. $levelsArr[] = [
  1923. 'LEVEL' => $level,
  1924. 'CODE' => $code,
  1925. ];
  1926. }
  1927. $originLevel = $pendingEquipment->NIVEL_ORIGEN;
  1928. $originLevelFilt = array_filter($levelsArr, function ($v, $k) use ($originLevel) {
  1929. return $v['CODE'] == $originLevel;
  1930. }, ARRAY_FILTER_USE_BOTH);
  1931. $pendingEquipment->NIVEL_ORIGEN = end($originLevelFilt)['LEVEL'] . " (" . $pendingEquipment->NIVEL_ORIGEN . ")";
  1932. $originOccupationStr = $pendingEquipment->OCUPACION_ORIGEN;
  1933. $originOccupationArea = substr($originOccupationStr, 0, 2);
  1934. $originOccupationID = substr($originOccupationStr, 2);
  1935. $originOccupation = DB::table('S002V01TOCUP')->where([
  1936. ['OCUP_NULI', '=', $line],
  1937. ['OCUP_AREA', '=', $originOccupationArea],
  1938. ['OCUP_IDOC', '=', $originOccupationID]
  1939. ])->first();
  1940. $pendingEquipment->OCUPACION_ORIGEN = $originOccupation->OCUP_DESC . " (" . $originOccupationStr . ")";
  1941. $family = DB::table('S002V01TFAMI')->where([
  1942. ['FAMI_NULI', '=', $line],
  1943. ['FAMI_COFA', '=', $pendingEquipment->FAMILIA],
  1944. ])->first();
  1945. $subfamily = DB::table('S002V01TSUBF')->where([
  1946. ['SUBF_NULI', '=', $line],
  1947. ['SUBF_COFA', '=', $pendingEquipment->FAMILIA],
  1948. ['SUBF_COSU', '=', $pendingEquipment->SUBFAMILIA],
  1949. ])->first();
  1950. $pendingEquipment->FAMILIA = $family->FAMI_NOFA . " (" . $pendingEquipment->FAMILIA . ")";
  1951. $pendingEquipment->SUBFAMILIA = $subfamily->SUBF_NOSU . " (" . $pendingEquipment->SUBFAMILIA . ")";
  1952. $validStatus = ["A" => "Adquisición", "S" => "Stock", "T" => "Traslado", "I" => "Instalación", "R" => "Reparación", "D" => "Disposición"];
  1953. $pendingEquipment->ESTADO_EQUIPAMIENTO = $validStatus[$pendingEquipment->ESTADO_EQUIPAMIENTO] . " (" . $pendingEquipment->ESTADO_EQUIPAMIENTO . ")";
  1954. $idUserEnc = $this->encryptionController->encrypt($idUser);
  1955. $imagesGalleryArr = json_decode($pendingEquipment->GALERIA_IMAGENES);
  1956. $imagesGalleryFn = [];
  1957. foreach($imagesGalleryArr as $imageCode){
  1958. $imageCodeEnc = $this->encryptionController->encrypt($imageCode);
  1959. $publicUri = $this->documentManagementController->getPublicDocumentURL(
  1960. $imageCodeEnc,
  1961. $idUserEnc,
  1962. $line
  1963. );
  1964. $response = json_decode($publicUri->original, true);
  1965. if($response['error']){
  1966. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  1967. }else{
  1968. $uriEnc = $this->encryptionController->encrypt($response['response']['public_uri']);
  1969. $imagesGalleryFn[] = $uriEnc;
  1970. }
  1971. }
  1972. $documentsArr = json_decode($pendingEquipment->DOCUMENTOS);
  1973. $documentsFn = [];
  1974. foreach($documentsArr as $document){
  1975. $codeArr = explode('=',$document);
  1976. $codeArr0 = explode('-', $codeArr[0]);
  1977. $file = DB::table('S002V01TAFAL')->where([
  1978. ['AFAL_NULI', '=', $line],
  1979. ['AFAL_COMO', '=', $codeArr0[1]],
  1980. ['AFAL_CLDO', '=', $codeArr0[2]],
  1981. ['AFAL_FECR', '=', $codeArr0[3]],
  1982. ['AFAL_NUSE', '=', $codeArr0[4]],
  1983. ['AFAL_NUVE', '=', $codeArr[1]],
  1984. ])->first();
  1985. if(is_null($file)){
  1986. return $this->responseController->makeResponse(true, "El archivo $document no está registrado.", [], 404);
  1987. }
  1988. $documentsFn[] = [
  1989. 'id' => $this->encryptionController->encrypt($document),
  1990. 'name' => $document,
  1991. 'size' => $file->AFAL_TAMA
  1992. ];
  1993. }
  1994. $pendingEquipment->GALERIA_IMAGENES = json_encode($imagesGalleryFn);
  1995. $pendingEquipment->DOCUMENTOS = json_encode($documentsFn);
  1996. $pendingEquipment->PROVEEDOR = $this->encryptionController->encrypt($pendingEquipment->PROVEEDOR);
  1997. $pendingEquipment->NUMERO_SERIAL = $this->encryptionController->encrypt($pendingEquipment->NUMERO_SERIAL);
  1998. $usrReg = DB::table('S002V01TUSUA')->where([
  1999. ['USUA_NULI', '=', $line],
  2000. ['USUA_IDUS', '=', $pendingEquipment->USUARIO_REGISTRO]
  2001. ])->first();
  2002. $usrRegName = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  2003. $pendingEquipment->USUARIO_REGISTRO = $usrRegName . " (" . $pendingEquipment->USUARIO_REGISTRO . ")";
  2004. if(!is_null($pendingEquipment->USUARIO_MODIFICO)){
  2005. $usrMod = DB::table('S002V01TUSUA')->where([
  2006. ['USUA_NULI', '=', $line],
  2007. ['USUA_IDUS', '=', $pendingEquipment->USUARIO_MODIFICO]
  2008. ])->first();
  2009. $usrModName = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  2010. $pendingEquipment->USUARIO_MODIFICO = $usrModName . " (" . $pendingEquipment->USUARIO_MODIFICO . ")";
  2011. }
  2012. $pendingEquipment->TIPO_EQUIPAMIENTO = $this->encryptionController->encrypt($pendingEquipment->TIPO_EQUIPAMIENTO);
  2013. $pendingEquipment->MODELO_EQUIPAMIENTO = $this->encryptionController->encrypt($pendingEquipment->MODELO_EQUIPAMIENTO);
  2014. $now = $this->functionsController->now();
  2015. $nowStr = $now->toDateTimeString();
  2016. $actions = DB::getQueryLog();
  2017. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2018. $idac = $this->functionsController->registerActivity(
  2019. $line,
  2020. 'S002V01M07GEEQ',
  2021. 'S002V01F01ADEQ',
  2022. '-',
  2023. 'Consulta',
  2024. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el equipamiento pre codificado $idReg.",
  2025. $idUser,
  2026. $nowStr,
  2027. );
  2028. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2029. return $this->responseController->makeResponse(false, 'EXITO.', $pendingEquipment);
  2030. }
  2031. public function changePreCodedEquipmentStatus(Request $request) {
  2032. DB::enableQueryLog();
  2033. $validator = Validator::make($request->all(), [
  2034. 'id_user' => 'required|string',
  2035. 'linea' => 'required|integer',
  2036. 'id_reg' => 'required|string',
  2037. 'status' => 'required|string|in:Aprobado,Rechazado',
  2038. 'comments' => 'required|string|min:15',
  2039. ]);
  2040. if($validator->fails()){
  2041. return $this->responseController->makeResponse(
  2042. true,
  2043. "Se encontraron uno o más errores.",
  2044. $this->responseController->makeErrors(
  2045. $validator->errors()->messages()
  2046. ),
  2047. 401
  2048. );
  2049. }
  2050. $form = $request->all();
  2051. $idUser = $this->encryptionController->decrypt($form['id_user']);
  2052. if(!$idUser){
  2053. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  2054. }
  2055. $usr = DB::table('S002V01TUSUA')->where([
  2056. ['USUA_NULI', '=', $form['linea']],
  2057. ['USUA_IDUS', '=', $idUser]
  2058. ])->first();
  2059. if(is_null($usr)){
  2060. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  2061. }
  2062. $idReg = $this->encryptionController->decrypt($form['id_reg']);
  2063. if(!$idReg){
  2064. return $this->responseController->makeResponse(true, 'El ID del pre-registro no está encriptado correctamente', [], 400);
  2065. }
  2066. $pendingEquipment = DB::table('S002V01TPCEQ')->where([
  2067. ['PCEQ_NULI', '=', $form['linea']],
  2068. ['PCEQ_IDPR', '=', $idReg],
  2069. ])->first();
  2070. if(is_null($pendingEquipment)){
  2071. return $this->responseController->makeResponse(true, 'El pre-registro solicitado no existe.', [], 404);
  2072. }else if($pendingEquipment->PCEQ_ESRE == 'Rechazado'){
  2073. return $this->responseController->makeResponse(true, 'El pre-registro se encuentra rechazado.', [], 400);
  2074. }else if($pendingEquipment->PCEQ_ESRE == 'Aprobado'){
  2075. return $this->responseController->makeResponse(true, 'El pre-registro se encuentra aprobado.', [], 400);
  2076. }
  2077. $now = $this->functionsController->now();
  2078. $nowStr = $now->toDateTimeString();
  2079. if($form['status'] == 'Rechazado'){
  2080. DB::table('S002V01TPCEQ')->where([
  2081. ['PCEQ_IDPR', '=', $idReg],
  2082. ['PCEQ_NULI', '=', $form['linea']],
  2083. ])->update([
  2084. 'PCEQ_ESRE' => $form['status'],
  2085. 'PCEQ_CORE' => $form['comments'],
  2086. 'PCEQ_USMO' => $idUser,
  2087. 'PCEQ_FEMO' => $nowStr,
  2088. ]);
  2089. }else{
  2090. //PASO 1: SE BUSCA EL ÚLTIMO ID DEL EQUIPAMIENTO
  2091. $lastID = DB::table('S002V01TEQUI')->select([
  2092. 'EQUI_IDEQ AS LAST_ID'
  2093. ])->where([
  2094. ['EQUI_NULI', '=', $form['linea']],
  2095. ['EQUI_FAMI', '=', $pendingEquipment->PCEQ_FAMI],
  2096. ['EQUI_SUBF', '=', $pendingEquipment->PCEQ_SUBF],
  2097. ['EQUI_TIPO', '=', $pendingEquipment->PCEQ_TIEQ],
  2098. ['EQUI_MODE', '=', $pendingEquipment->PCEQ_MOEQ],
  2099. ])->orderBy('EQUI_IDEQ', 'asc')->first();
  2100. //PASO 2: SE DEFINE EL ID DEL NUEVO EQUIPAMIENTO
  2101. $IDNum = is_null($lastID) ? 1 : $lastID->LAST_ID + 1;
  2102. //PASO 3: SE DEFINE UN ESPACIO PARA RETORNAR EL PROCESO
  2103. doAgain:
  2104. //PASO 4: SE RELLENA EL ID CON CEROS A LA IZQUIERDA PARA DARLE EL FORMATO DE LA CODIFICACIÓN
  2105. $IDStr = "$IDNum";
  2106. for($i = strlen($IDStr); $i < 6; $i++){
  2107. $IDStr = "0$IDStr";
  2108. }
  2109. //PASO 5: SE INTEGRA EL ID AL CÓDIGO PRE-GENERADO
  2110. $preCodeArr = explode('_', $pendingEquipment->PCEQ_CPGE);
  2111. $previewPBS = $preCodeArr[1];
  2112. $previewPBSArr = explode('-', $previewPBS);
  2113. $previewPBSArr = array_reverse($previewPBSArr);
  2114. $previewPBSArr[0] = $IDStr;
  2115. $previewPBSArr = array_reverse($previewPBSArr);
  2116. $previewPBS = implode('-', $previewPBSArr);
  2117. //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
  2118. $previewCode = $preCodeArr[0] . "_" . $previewPBS;
  2119. $registeredCode = DB::table('S002V01TEQUI')->where([
  2120. ['EQUI_NULI', '=', $form['linea']],
  2121. ['EQUI_COEQ', '=', $previewCode]
  2122. ])->first();
  2123. $codeInHistory = DB::table('S002V01TEQUI')
  2124. ->where('EQUI_NULI', '=', $form['linea'])
  2125. ->whereJsonContains('EQUI_HICO', $previewCode)->first();
  2126. //PASO 7: VERIFICAR QUE EL CÓDIGO NO EXISTA, SI EXISTE SE REALIZA NUEVAMENTE EL PROCESO AUMENTANDO EN 1 EL ID
  2127. if(!is_null($registeredCode) || !is_null($codeInHistory)){
  2128. $IDNum++;
  2129. goto doAgain;
  2130. }
  2131. //PASO 8: REALIZAR EL REGISTRO DEL EQUIPAMIENTO
  2132. $hico = json_encode([$previewCode]);
  2133. DB::table('S002V01TEQUI')->insert([
  2134. 'EQUI_NULI' => $form['linea'],
  2135. 'EQUI_FAMI' => $pendingEquipment->PCEQ_FAMI,
  2136. 'EQUI_SUBF' => $pendingEquipment->PCEQ_SUBF,
  2137. 'EQUI_TIPO' => $pendingEquipment->PCEQ_TIEQ,
  2138. 'EQUI_MODE' => $pendingEquipment->PCEQ_MOEQ,
  2139. 'EQUI_IDEQ' => $IDNum,
  2140. 'EQUI_ESEQ' => $pendingEquipment->PCEQ_ESEQ,
  2141. 'EQUI_COEQ' => $previewCode,
  2142. 'EQUI_JERA' => $pendingEquipment->PCEQ_JERA,
  2143. 'EQUI_EQPA' => $pendingEquipment->PCEQ_EQPA,
  2144. 'EQUI_TICO' => $pendingEquipment->PCEQ_TICO,
  2145. 'EQUI_UBOR' => $pendingEquipment->PCEQ_UBOR,
  2146. 'EQUI_NIOR' => $pendingEquipment->PCEQ_NIOR,
  2147. 'EQUI_OCOR' => $pendingEquipment->PCEQ_OCOR,
  2148. 'EQUI_ELOR' => $pendingEquipment->PCEQ_ELOR,
  2149. 'EQUI_ALMA' => $pendingEquipment->PCEQ_ALMA,
  2150. 'EQUI_AREA' => $pendingEquipment->PCEQ_AREA,
  2151. 'EQUI_NIVE' => $pendingEquipment->PCEQ_NIVE,
  2152. 'EQUI_ZONA' => $pendingEquipment->PCEQ_ZONA,
  2153. 'EQUI_IPCC' => $pendingEquipment->PCEQ_IPCC,
  2154. 'EQUI_PPCC' => $pendingEquipment->PCEQ_PPCC,
  2155. 'EQUI_KIOR' => $pendingEquipment->PCEQ_KIOR,
  2156. 'EQUI_UBDE' => $pendingEquipment->PCEQ_UBDE,
  2157. 'EQUI_NIDE' => $pendingEquipment->PCEQ_NIDE,
  2158. 'EQUI_OCDE' => $pendingEquipment->PCEQ_OCDE,
  2159. 'EQUI_ELDE' => $pendingEquipment->PCEQ_ELDE,
  2160. 'EQUI_KIDE' => $pendingEquipment->PCEQ_KIDE,
  2161. 'EQUI_FEAD' => $pendingEquipment->PCEQ_FEAD,
  2162. 'EQUI_FIGA' => $pendingEquipment->PCEQ_FIGA,
  2163. 'EQUI_FTGA' => $pendingEquipment->PCEQ_FTGA,
  2164. 'EQUI_PREQ' => $pendingEquipment->PCEQ_PREQ,
  2165. 'EQUI_NUSE' => $pendingEquipment->PCEQ_NUSE,
  2166. 'EQUI_DESO' => $pendingEquipment->PCEQ_DESO,
  2167. 'EQUI_VESO' => $pendingEquipment->PCEQ_VESO,
  2168. 'EQUI_HICO' => $hico,
  2169. 'EQUI_GAIM' => $pendingEquipment->PCEQ_GAIM,
  2170. 'EQUI_DORE' => $pendingEquipment->PCEQ_DORE,
  2171. 'EQUI_USRE' => $idUser,
  2172. 'EQUI_FERE' => $nowStr,
  2173. ]);
  2174. DB::table('S002V01TPCEQ')->where([
  2175. ['PCEQ_IDPR', '=', $idReg],
  2176. ['PCEQ_NULI', '=', $form['linea']],
  2177. ])->update([
  2178. 'PCEQ_ESRE' => $form['status'],
  2179. 'PCEQ_CORE' => $form['comments'],
  2180. 'PCEQ_USMO' => $idUser,
  2181. 'PCEQ_FEMO' => $nowStr,
  2182. ]);
  2183. }
  2184. $actions = DB::getQueryLog();
  2185. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2186. $idac = $this->functionsController->registerActivity(
  2187. $form['linea'],
  2188. 'S002V01M07GEEQ',
  2189. 'S002V01F01ADEQ',
  2190. 'S002V01P11REEQ',
  2191. 'Registro',
  2192. "El usuario $name (" . $usr->USUA_IDUS . ") cambió el estado del pre-registro $idReg a $form[status].",
  2193. $idUser,
  2194. $nowStr,
  2195. );
  2196. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2197. return $this->responseController->makeResponse(false, 'EXITO.');
  2198. }
  2199. public function getEquipments($idUser, $line) {
  2200. DB::enableQueryLog();
  2201. $idUser = $this->encryptionController->decrypt($idUser);
  2202. if(!$idUser){
  2203. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2204. }
  2205. $usr = DB::table('S002V01TUSUA')->where([
  2206. ['USUA_NULI', '=', $line],
  2207. ['USUA_IDUS', '=', $idUser],
  2208. ])->first();
  2209. if(is_null($usr)){
  2210. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2211. }
  2212. $equipments = DB::table('S002V01TEQUI')->select([
  2213. 'EQUI_COEQ AS CODIGO',
  2214. 'EQUI_TIPO AS TIPO',
  2215. 'EQUI_MODE AS MODELO',
  2216. 'EQUI_IDEQ AS ID_EQUIPO',
  2217. 'EQUI_ESFU AS ESTADO_FUNCIONAMIENTO',
  2218. 'EQUI_GAIM AS GALERIA_IMAGENES',
  2219. 'EQUI_ELOR AS ELEMENTO_ORIGEN',
  2220. 'EQUI_TICO AS TIPO_CODIGO',
  2221. 'EQUI_EQPA AS EQUIPO_PADRE',
  2222. ])->where('EQUI_NULI', '=', $line)->get()->all();
  2223. $idUserEnc = $this->encryptionController->encrypt($idUser);
  2224. foreach($equipments as $key=>$equipment){
  2225. $children = DB::table('S002V01TEQUI')->where([
  2226. ['EQUI_NULI', '=', $line],
  2227. ['EQUI_EQPA', '=', $equipment->CODIGO],
  2228. ])->get()->all();
  2229. $equipment->CODIGO = $this->encryptionController->encrypt($equipment->CODIGO);
  2230. $equipment->ID_EQUIPO = $this->encryptionController->encrypt($equipment->ID_EQUIPO);
  2231. $equipment->EQUIPO_PADRE = is_null($equipment->EQUIPO_PADRE) ? null : $this->encryptionController->encrypt($equipment->EQUIPO_PADRE);
  2232. $imagesGalleryArr = json_decode($equipment->GALERIA_IMAGENES, true);
  2233. $imagesGalleryFn = [];
  2234. foreach($imagesGalleryArr as $imageCode){
  2235. $imageCodeEnc = $this->encryptionController->encrypt($imageCode);
  2236. $publicUri = $this->documentManagementController->getPublicDocumentURL(
  2237. $imageCodeEnc,
  2238. $idUserEnc,
  2239. $line
  2240. );
  2241. $response = json_decode($publicUri->original, true);
  2242. if($response['error']){
  2243. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  2244. }else{
  2245. $uriEnc = $this->encryptionController->encrypt($response['response']['public_uri']);
  2246. $imagesGalleryFn[] = $uriEnc;
  2247. }
  2248. }
  2249. $equipment->GALERIA_IMAGENES = json_encode($imagesGalleryFn);
  2250. $equipment->TIENE_HIJOS = count($children) > 0 ? 'Si' : 'No';
  2251. $equipments[$key] = $equipment;
  2252. }
  2253. $now = $this->functionsController->now();
  2254. $nowStr = $now->toDateTimeString();
  2255. $actions = DB::getQueryLog();
  2256. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2257. $idac = $this->functionsController->registerActivity(
  2258. $line,
  2259. 'S002V01M07GEEQ',
  2260. 'S002V01F01ADEQ',
  2261. 'S002V01P01MEEQ',
  2262. 'Consulta',
  2263. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los equipamientos registrados.",
  2264. $idUser,
  2265. $nowStr,
  2266. );
  2267. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2268. return $this->responseController->makeResponse(false, 'EXITO.', $equipments);
  2269. }
  2270. public function getEquipmentsBySubfamily($familyCode, $subfamilyCode, $idUser, $line) {
  2271. DB::enableQueryLog();
  2272. $idUser = $this->encryptionController->decrypt($idUser);
  2273. if(!$idUser){
  2274. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2275. }
  2276. $usr = DB::table('S002V01TUSUA')->where([
  2277. ['USUA_NULI', '=', $line],
  2278. ['USUA_IDUS', '=', $idUser],
  2279. ])->first();
  2280. if(is_null($usr)){
  2281. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2282. }
  2283. $familyCode = $this->encryptionController->decrypt($familyCode);
  2284. if(!$familyCode){
  2285. return $this->responseController->makeResponse(true, 'El código de la familia no está encriptado correctamente', [], 400);
  2286. }
  2287. $family = DB::table('S002V01TFAMI')->where([
  2288. ['FAMI_NULI', '=', $line],
  2289. ['FAMI_COFA', '=', $familyCode],
  2290. ])->first();
  2291. if(is_null($family)){
  2292. return $this->responseController->makeResponse(true, 'La familia relacionada no está registrada.', [], 404);
  2293. }
  2294. $subfamilyCode = $this->encryptionController->decrypt($subfamilyCode);
  2295. if(!$subfamilyCode){
  2296. return $this->responseController->makeResponse(true, 'El código de la subfamilia no está encriptado correctamente', [], 400);
  2297. }
  2298. $subfamily = DB::table('S002V01TSUBF')->where([
  2299. ['SUBF_NULI', '=', $line],
  2300. ['SUBF_COFA', '=', $familyCode],
  2301. ['SUBF_COSU', '=', $subfamilyCode],
  2302. ])->first();
  2303. if(is_null($subfamily)){
  2304. return $this->responseController->makeResponse(true, 'La subfamilia relacionada no está registrada.', [], 404);
  2305. }
  2306. $equipments = DB::table('S002V01TEQUI')->select([
  2307. 'EQUI_COEQ AS CODIGO',
  2308. 'EQUI_TIPO AS TIPO',
  2309. 'EQUI_MODE AS MODELO',
  2310. 'EQUI_IDEQ AS ID_EQUIPO',
  2311. 'EQUI_ESFU AS ESTADO_FUNCIONAMIENTO',
  2312. 'EQUI_GAIM AS GALERIA_IMAGENES',
  2313. ])->where([
  2314. ['EQUI_NULI', '=', $line],
  2315. ['EQUI_FAMI', '=', $familyCode],
  2316. ['EQUI_SUBF', '=', $subfamilyCode],
  2317. ['EQUI_JERA', '=', 'Padre']
  2318. ])->get()->all();
  2319. $idUserEnc = $this->encryptionController->encrypt($idUser);
  2320. foreach($equipments as $key=>$equipment){
  2321. $equipment->CODIGO = $this->encryptionController->encrypt($equipment->CODIGO);
  2322. $equipment->ID_EQUIPO = $this->encryptionController->encrypt($equipment->ID_EQUIPO);
  2323. $imagesGalleryArr = json_decode($equipment->GALERIA_IMAGENES, true);
  2324. $imagesGalleryFn = [];
  2325. foreach($imagesGalleryArr as $imageCode){
  2326. $imageCodeEnc = $this->encryptionController->encrypt($imageCode);
  2327. $publicUri = $this->documentManagementController->getPublicDocumentURL(
  2328. $imageCodeEnc,
  2329. $idUserEnc,
  2330. $line
  2331. );
  2332. $response = json_decode($publicUri->original, true);
  2333. if($response['error']){
  2334. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  2335. }else{
  2336. $uriEnc = $this->encryptionController->encrypt($response['response']['public_uri']);
  2337. $imagesGalleryFn[] = $uriEnc;
  2338. }
  2339. }
  2340. $equipment->GALERIA_IMAGENES = json_encode($imagesGalleryFn);
  2341. $equipments[$key] = $equipment;
  2342. }
  2343. $now = $this->functionsController->now();
  2344. $nowStr = $now->toDateTimeString();
  2345. $actions = DB::getQueryLog();
  2346. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2347. $idac = $this->functionsController->registerActivity(
  2348. $line,
  2349. 'S002V01M07GEEQ',
  2350. 'S002V01F01ADEQ',
  2351. 'S002V01P01MEEQ',
  2352. 'Consulta',
  2353. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los equipamientos relacionados a la subfamilia $subfamilyCode de la familia $familyCode.",
  2354. $idUser,
  2355. $nowStr,
  2356. );
  2357. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2358. return $this->responseController->makeResponse(false, 'EXITO.', $equipments);
  2359. }
  2360. public function getEquipmentsByParent($parentCode, $idUser, $line) {
  2361. DB::enableQueryLog();
  2362. $idUser = $this->encryptionController->decrypt($idUser);
  2363. if(!$idUser){
  2364. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2365. }
  2366. $usr = DB::table('S002V01TUSUA')->where([
  2367. ['USUA_NULI', '=', $line],
  2368. ['USUA_IDUS', '=', $idUser],
  2369. ])->first();
  2370. if(is_null($usr)){
  2371. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2372. }
  2373. $parentCode = $this->encryptionController->decrypt($parentCode);
  2374. if(!$parentCode){
  2375. return $this->responseController->makeResponse(true, 'El código del equipamiento padre no está encriptado correctamente', [], 400);
  2376. }
  2377. $equipments = DB::table('S002V01TEQUI')->select([
  2378. 'EQUI_COEQ AS CODIGO',
  2379. 'EQUI_TIPO AS TIPO',
  2380. 'EQUI_MODE AS MODELO',
  2381. 'EQUI_IDEQ AS ID_EQUIPO',
  2382. 'EQUI_ESFU AS ESTADO_FUNCIONAMIENTO',
  2383. 'EQUI_GAIM AS GALERIA_IMAGENES',
  2384. 'EQUI_EQPA AS EQUIPO_PADRE',
  2385. ])->where([
  2386. ['EQUI_NULI', '=', $line],
  2387. ['EQUI_JERA', '=', 'Hijo'],
  2388. ['EQUI_EQPA', '=', $parentCode],
  2389. ])->get()->all();
  2390. $idUserEnc = $this->encryptionController->encrypt($idUser);
  2391. foreach($equipments as $key=>$equipment){
  2392. $children = DB::table('S002V01TEQUI')->where([
  2393. ['EQUI_NULI', '=', $line],
  2394. ['EQUI_EQPA', '=', $equipment->CODIGO],
  2395. ])->get()->all();
  2396. $equipment->CODIGO = $this->encryptionController->encrypt($equipment->CODIGO);
  2397. $equipment->ID_EQUIPO = $this->encryptionController->encrypt($equipment->ID_EQUIPO);
  2398. $equipment->EQUIPO_PADRE = is_null($equipment->EQUIPO_PADRE) ? null : $this->encryptionController->encrypt($equipment->EQUIPO_PADRE);
  2399. $imagesGalleryArr = json_decode($equipment->GALERIA_IMAGENES, true);
  2400. $imagesGalleryFn = [];
  2401. foreach($imagesGalleryArr as $imageCode){
  2402. $imageCodeEnc = $this->encryptionController->encrypt($imageCode);
  2403. $publicUri = $this->documentManagementController->getPublicDocumentURL(
  2404. $imageCodeEnc,
  2405. $idUserEnc,
  2406. $line
  2407. );
  2408. $response = json_decode($publicUri->original, true);
  2409. if($response['error']){
  2410. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  2411. }else{
  2412. $uriEnc = $this->encryptionController->encrypt($response['response']['public_uri']);
  2413. $imagesGalleryFn[] = $uriEnc;
  2414. }
  2415. }
  2416. $equipment->GALERIA_IMAGENES = json_encode($imagesGalleryFn);
  2417. $equipment->TIENE_HIJOS = count($children) > 0 ? 'Si' : 'No';
  2418. $equipments[$key] = $equipment;
  2419. }
  2420. $now = $this->functionsController->now();
  2421. $nowStr = $now->toDateTimeString();
  2422. $actions = DB::getQueryLog();
  2423. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2424. $idac = $this->functionsController->registerActivity(
  2425. $line,
  2426. 'S002V01M07GEEQ',
  2427. 'S002V01F01ADEQ',
  2428. 'S002V01P02DEEQ',
  2429. 'Consulta',
  2430. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los equipamientos relacionados al equipamiento $parentCode.",
  2431. $idUser,
  2432. $nowStr,
  2433. );
  2434. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2435. return $this->responseController->makeResponse(false, 'EXITO.', $equipments);
  2436. }
  2437. public function getEquipmentDetails($code, $idUser, $line) {
  2438. DB::enableQueryLog();
  2439. $idUser = $this->encryptionController->decrypt($idUser);
  2440. if(!$idUser){
  2441. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2442. }
  2443. $usr = DB::table('S002V01TUSUA')->where([
  2444. ['USUA_NULI', '=', $line],
  2445. ['USUA_IDUS', '=', $idUser],
  2446. ])->first();
  2447. if(is_null($usr)){
  2448. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2449. }
  2450. $code = $this->encryptionController->decrypt($code);
  2451. if(!$code){
  2452. return $this->responseController->makeResponse(true, 'El código del equipamiento no está encriptado correctamente', [], 400);
  2453. }
  2454. $equipment = DB::table('S002V01TEQUI')->select([
  2455. 'EQUI_FAMI AS FAMILIA',
  2456. 'EQUI_SUBF AS SUBFAMILIA',
  2457. 'EQUI_TIPO AS TIPO_EQUIPAMIENTO',
  2458. 'EQUI_MODE AS MODELO',
  2459. 'EQUI_IDEQ AS ID_EQUIPAMIENTO',
  2460. 'EQUI_ESEQ AS ESTADO',
  2461. 'EQUI_COEQ AS CODIGO',
  2462. 'EQUI_JERA AS JERARQUIA',
  2463. 'EQUI_EQPA AS EQUIPO_PADRE',
  2464. 'EQUI_TICO AS TIPO_CODIGO',
  2465. 'EQUI_UBOR AS UBICACION_ORIGEN',
  2466. 'EQUI_NIOR AS NIVEL_ORIGEN',
  2467. 'EQUI_OCOR AS OCUPACION_ORIGEN',
  2468. 'EQUI_ELOR AS ELEMENTO_ORIGEN',
  2469. 'EQUI_IPCC AS INTERSECCION_PCC',
  2470. 'EQUI_PPCC AS POSICION_PCC',
  2471. 'EQUI_KIOR AS KILOMETRO_ORIGEN',
  2472. 'EQUI_UBDE AS UBICACION_DESTINO',
  2473. 'EQUI_NIDE AS NIVEL_DESTINO',
  2474. 'EQUI_OCDE AS OCUPACION_DESTINO',
  2475. 'EQUI_ELDE AS ELEMENTO_DESTINO',
  2476. 'EQUI_KIDE AS KILOMETRO_DESTINO',
  2477. 'EQUI_FEAD AS FECHA_ADQUISICION',
  2478. 'EQUI_FIGA AS FECHA_INICIO_GARANTIA',
  2479. 'EQUI_FTGA AS FECHA_TERMINO_GARANTIA',
  2480. 'EQUI_PREQ AS PROVEEDOR',
  2481. 'EQUI_NUSE AS NUMERO_SERIAL',
  2482. 'EQUI_DESO AS DENOMINACION_SOFTWARE',
  2483. 'EQUI_VESO AS VERSION_SOFTWARE',
  2484. 'EQUI_HICO AS HISTORIAL_CODIGOS',
  2485. 'EQUI_GAIM AS GALERIA_IMAGENES',
  2486. 'EQUI_DORE AS DOCUMENTOS',
  2487. 'EQUI_ESFU AS ESTADO_FUNCIONAMIENTO',
  2488. 'EQUI_USRE AS USUARIO_REGISTRO',
  2489. 'EQUI_FERE AS FECHA_REGISTRO',
  2490. 'EQUI_USMO AS USUARIO_MODIFICACION',
  2491. 'EQUI_FEMO AS FECHA_MODIFICACION',
  2492. ])->where([
  2493. ['EQUI_NULI', '=', $line],
  2494. ['EQUI_COEQ', '=', $code],
  2495. ])->first();
  2496. $family = DB::table('S002V01TFAMI')->where([
  2497. ['FAMI_NULI', '=', $line],
  2498. ['FAMI_COFA', '=', $equipment->FAMILIA]
  2499. ])->first();
  2500. $subfamily = DB::table('S002V01TSUBF')->where([
  2501. ['SUBF_NULI', '=', $line],
  2502. ['SUBF_COFA', '=', $equipment->FAMILIA],
  2503. ['SUBF_COSU', '=', $equipment->SUBFAMILIA]
  2504. ])->first();
  2505. $equipment->FAMILIA = $family->FAMI_NOFA . " (" . $equipment->FAMILIA . ")";
  2506. $equipment->SUBFAMILIA = $subfamily->SUBF_NOSU . " (" . $equipment->SUBFAMILIA . ")";
  2507. $equipment->ID_EQUIPAMIENTO = $this->encryptionController->encrypt($equipment->ID_EQUIPAMIENTO);
  2508. $equipment->CODIGO = $this->encryptionController->encrypt($equipment->CODIGO);
  2509. $ubic = $this->templatesUbic;
  2510. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  2511. $locationsEnc = file_get_contents($ubic . "locations.sam");
  2512. $locationsDec = $this->encryptionController->decrypt($locationsEnc);
  2513. $locationsArr = json_decode($locationsDec, true);
  2514. $locationsArrDec = [];
  2515. foreach($locationsArr as $location){
  2516. $locationStr = $this->encryptionController->decrypt($location['LOCATION']);
  2517. $code = $this->encryptionController->decrypt($location['CODE']);
  2518. $locationsArrDec[] = [
  2519. 'LOCATION' => $locationStr,
  2520. 'CODE' => $code
  2521. ];
  2522. }
  2523. $originLocation = $equipment->UBICACION_ORIGEN;
  2524. $originLocationFilt = array_filter($locationsArrDec, function ($v, $k) use ($originLocation) {
  2525. return $v['CODE'] == $originLocation;
  2526. }, ARRAY_FILTER_USE_BOTH);
  2527. $equipment->UBICACION_ORIGEN = end($originLocationFilt)['LOCATION'] . " (" . $equipment->UBICACION_ORIGEN . ")";
  2528. $levelsArr = [];
  2529. for($i = 5; $i > 0; $i--){
  2530. $levelsArr[] = [
  2531. 'LEVEL' => "Subterráneo S0$i",
  2532. 'CODE' => "S0$i",
  2533. ];
  2534. }
  2535. for($i = 0; $i <= 15; $i++){
  2536. $code = $i < 10 ? "P0$i" : "P$i";
  2537. $level = $i == 0 ? "Planta baja P00" : "Piso $code";
  2538. $levelsArr[] = [
  2539. 'LEVEL' => $level,
  2540. 'CODE' => $code,
  2541. ];
  2542. }
  2543. $originLevel = $equipment->NIVEL_ORIGEN;
  2544. $originLevelFilt = array_filter($levelsArr, function ($v, $k) use ($originLevel) {
  2545. return $v['CODE'] == $originLevel;
  2546. }, ARRAY_FILTER_USE_BOTH);
  2547. $equipment->NIVEL_ORIGEN = end($originLevelFilt)['LEVEL'] . " (" . $equipment->NIVEL_ORIGEN . ")";
  2548. $originOccupationStr = $equipment->OCUPACION_ORIGEN;
  2549. $originOccupationArea = substr($originOccupationStr, 0, 2);
  2550. $originOccupationID = substr($originOccupationStr, 2);
  2551. $originOccupation = DB::table('S002V01TOCUP')->where([
  2552. ['OCUP_NULI', '=', $line],
  2553. ['OCUP_AREA', '=', $originOccupationArea],
  2554. ['OCUP_IDOC', '=', $originOccupationID]
  2555. ])->first();
  2556. $validStatus = ["A" => "Adquisición", "S" => "Stock", "T" => "Traslado", "I" => "Instalación", "R" => "Reparación", "D" => "Disposición"];
  2557. $equipment->ESTADO = $validStatus[$equipment->ESTADO] . " (" . $equipment->ESTADO . ")";
  2558. $equipment->OCUPACION_ORIGEN = $originOccupation->OCUP_DESC . " (" . $originOccupationStr . ")";
  2559. $idUserEnc = $this->encryptionController->encrypt($idUser);
  2560. $imagesGalleryArr = json_decode($equipment->GALERIA_IMAGENES);
  2561. $imagesGalleryFn = [];
  2562. foreach($imagesGalleryArr as $imageCode){
  2563. $imageCodeEnc = $this->encryptionController->encrypt($imageCode);
  2564. $publicUri = $this->documentManagementController->getPublicDocumentURL(
  2565. $imageCodeEnc,
  2566. $idUserEnc,
  2567. $line
  2568. );
  2569. $response = json_decode($publicUri->original, true);
  2570. if($response['error']){
  2571. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  2572. }else{
  2573. $uriEnc = $this->encryptionController->encrypt($response['response']['public_uri']);
  2574. $imagesGalleryFn[] = $uriEnc;
  2575. }
  2576. }
  2577. $documentsArr = json_decode($equipment->DOCUMENTOS);
  2578. $documentsFn = [];
  2579. foreach($documentsArr as $document){
  2580. $codeArr = explode('=',$document);
  2581. $codeArr0 = explode('-', $codeArr[0]);
  2582. $file = DB::table('S002V01TAFAL')->where([
  2583. ['AFAL_NULI', '=', $line],
  2584. ['AFAL_COMO', '=', $codeArr0[1]],
  2585. ['AFAL_CLDO', '=', $codeArr0[2]],
  2586. ['AFAL_FECR', '=', $codeArr0[3]],
  2587. ['AFAL_NUSE', '=', $codeArr0[4]],
  2588. ['AFAL_NUVE', '=', $codeArr[1]],
  2589. ])->first();
  2590. if(is_null($file)){
  2591. return $this->responseController->makeResponse(true, "El archivo $document no está registrado.", [], 404);
  2592. }
  2593. $documentsFn[] = [
  2594. 'id' => $this->encryptionController->encrypt($document),
  2595. 'name' => $document,
  2596. 'size' => $file->AFAL_TAMA
  2597. ];
  2598. }
  2599. $equipment->GALERIA_IMAGENES = json_encode($imagesGalleryFn);
  2600. $equipment->DOCUMENTOS = json_encode($documentsFn);
  2601. $provider = DB::table('S002V01TPROV')->where([
  2602. ['PROV_NULI', '=', $line],
  2603. ['PROV_NUPR', '=', $equipment->PROVEEDOR],
  2604. ])->first();
  2605. $equipment->PROVEEDOR = $provider->PROV_NOCO . " (" . $equipment->PROVEEDOR . ")";
  2606. $equipment->NUMERO_SERIAL = $this->encryptionController->encrypt($equipment->NUMERO_SERIAL);
  2607. $codesHistory = json_decode($equipment->HISTORIAL_CODIGOS, true);
  2608. $codesHistoryFn = [];
  2609. foreach($codesHistory as $code){
  2610. $codeEnc = $this->encryptionController->encrypt($code);
  2611. $codesHistoryFn[] = $codeEnc;
  2612. }
  2613. $equipment->HISTORIAL_CODIGOS = json_encode($codesHistoryFn);
  2614. $usrReg = DB::table('S002V01TUSUA')->where([
  2615. ['USUA_NULI', '=', $line],
  2616. ['USUA_IDUS', '=', $equipment->USUARIO_REGISTRO]
  2617. ])->first();
  2618. $usrRegName = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  2619. $equipment->USUARIO_REGISTRO = $usrRegName . " (" . $equipment->USUARIO_REGISTRO . ")";
  2620. if(!is_null($equipment->USUARIO_MODIFICACION)){
  2621. $usrMod = DB::table('S002V01TUSUA')->where([
  2622. ['USUA_NULI', '=', $line],
  2623. ['USUA_IDUS', '=', $equipment->USUARIO_MODIFICACION]
  2624. ])->first();
  2625. $usrModName = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  2626. $equipment->USUARIO_MODIFICACION = $usrModName . " (" . $equipment->USUARIO_MODIFICACION . ")";
  2627. }
  2628. $equipment->TIPO_EQUIPAMIENTO = $this->encryptionController->encrypt($equipment->TIPO_EQUIPAMIENTO);
  2629. $equipment->MODELO = $this->encryptionController->encrypt($equipment->MODELO);
  2630. $now = $this->functionsController->now();
  2631. $nowStr = $now->toDateTimeString();
  2632. $actions = DB::getQueryLog();
  2633. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2634. $idac = $this->functionsController->registerActivity(
  2635. $line,
  2636. 'S002V01M07GEEQ',
  2637. 'S002V01F01ADEQ',
  2638. 'S002V01P01MEEQ',
  2639. 'Consulta',
  2640. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el equipamiento $code.",
  2641. $idUser,
  2642. $nowStr,
  2643. );
  2644. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2645. return $this->responseController->makeResponse(false, 'EXITO.', $equipment);
  2646. }
  2647. public function getTrainOccupations($idUser, $line) {
  2648. DB::enableQueryLog();
  2649. $idUser = $this->encryptionController->decrypt($idUser);
  2650. if(!$idUser){
  2651. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2652. }
  2653. $usr = DB::table('S002V01TUSUA')->where([
  2654. ['USUA_NULI', '=', $line],
  2655. ['USUA_IDUS', '=', $idUser],
  2656. ])->first();
  2657. if(is_null($usr)){
  2658. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2659. }
  2660. $ubic = $this->templatesUbic;
  2661. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  2662. $trainOccupationsEnc = file_get_contents($ubic . "train-occupations.sam");
  2663. $trainOccupationsDec = $this->encryptionController->decrypt($trainOccupationsEnc);
  2664. $trainOccupationsArr = json_decode($trainOccupationsDec, true);
  2665. $now = $this->functionsController->now();
  2666. $nowStr = $now->toDateTimeString();
  2667. $actions = DB::getQueryLog();
  2668. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2669. $idac = $this->functionsController->registerActivity(
  2670. $line,
  2671. 'S002V01M07GEEQ',
  2672. 'S002V01F01ADEQ',
  2673. '-',
  2674. 'Consulta',
  2675. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las ocupaciones de tren.",
  2676. $idUser,
  2677. $nowStr,
  2678. );
  2679. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2680. return $this->responseController->makeResponse(false, 'EXITO.', $trainOccupationsArr);
  2681. }
  2682. public function getTrainAreas($trainOccupation, $idUser, $line) {
  2683. DB::enableQueryLog();
  2684. $idUser = $this->encryptionController->decrypt($idUser);
  2685. if(!$idUser){
  2686. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2687. }
  2688. $usr = DB::table('S002V01TUSUA')->where([
  2689. ['USUA_NULI', '=', $line],
  2690. ['USUA_IDUS', '=', $idUser],
  2691. ])->first();
  2692. if(is_null($usr)){
  2693. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2694. }
  2695. $trainOccupation = $this->encryptionController->decrypt($trainOccupation);
  2696. if(!$trainOccupation){
  2697. return $this->responseController->makeResponse(true, 'La ocupación no fue encriptada correctamente.', [], 400);
  2698. }
  2699. $ubic = $this->templatesUbic;
  2700. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  2701. $carAreasEnc = file_get_contents($ubic . "car-areas.sam");
  2702. $carAreasDec = $this->encryptionController->decrypt($carAreasEnc);
  2703. $carAreasArr = json_decode($carAreasDec, true);
  2704. $carAreasArrDec = [];
  2705. foreach($carAreasArr as $key=>$val){
  2706. $keyDec = $this->encryptionController->decrypt($key);
  2707. $carAreasArrDec[$keyDec] = $val;
  2708. }
  2709. if(!array_key_exists($trainOccupation, $carAreasArrDec)){
  2710. return $this->responseController->makeResponse(true, "La ocupación $trainOccupation no existe.", [], 404);
  2711. }
  2712. $now = $this->functionsController->now();
  2713. $nowStr = $now->toDateTimeString();
  2714. $actions = DB::getQueryLog();
  2715. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2716. $idac = $this->functionsController->registerActivity(
  2717. $line,
  2718. 'S002V01M07GEEQ',
  2719. 'S002V01F01ADEQ',
  2720. '-',
  2721. 'Consulta',
  2722. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las áreas de la ocupación $trainOccupation de tren.",
  2723. $idUser,
  2724. $nowStr,
  2725. );
  2726. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2727. return $this->responseController->makeResponse(false, 'EXITO.', $carAreasArrDec[$trainOccupation]);
  2728. }
  2729. public function getTrainElements($idUser, $line) {
  2730. DB::enableQueryLog();
  2731. $idUser = $this->encryptionController->decrypt($idUser);
  2732. if(!$idUser){
  2733. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2734. }
  2735. $usr = DB::table('S002V01TUSUA')->where([
  2736. ['USUA_NULI', '=', $line],
  2737. ['USUA_IDUS', '=', $idUser],
  2738. ])->first();
  2739. if(is_null($usr)){
  2740. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2741. }
  2742. $ubic = $this->templatesUbic;
  2743. $ubic = str_replace("pdf_templates\\01_05_GEEQ", "global_resources", $ubic);
  2744. $trainElementsEnc = file_get_contents($ubic . "train-elements.sam");
  2745. $trainElementsDec = $this->encryptionController->decrypt($trainElementsEnc);
  2746. $trainElementsArr = json_decode($trainElementsDec, true);
  2747. $now = $this->functionsController->now();
  2748. $nowStr = $now->toDateTimeString();
  2749. $actions = DB::getQueryLog();
  2750. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2751. $idac = $this->functionsController->registerActivity(
  2752. $line,
  2753. 'S002V01M07GEEQ',
  2754. 'S002V01F01ADEQ',
  2755. '-',
  2756. 'Consulta',
  2757. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los elementos de tren.",
  2758. $idUser,
  2759. $nowStr,
  2760. );
  2761. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2762. return $this->responseController->makeResponse(false, 'EXITO.', $trainElementsArr);
  2763. }
  2764. }