EquipmentManagementController.php 133 KB

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