EquipmentManagementController.php 122 KB

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