SystemAdministratorController.php 261 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618
  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\Facades\Hash;
  7. use Illuminate\Support\Carbon;
  8. use Illuminate\Database\Query\JoinClause;
  9. use PhpOffice\PhpSpreadsheet\Reader\Exception as ExcelException;
  10. use PhpOffice\PhpSpreadsheet\IOFactory;
  11. use PhpOffice\PhpSpreadsheet\Shared\Date;
  12. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  13. use Jenssegers\Agent\Agent;
  14. use Firebase\JWT\JWT;
  15. use Firebase\JWT\Key;
  16. use Exception;
  17. class SystemAdministratorController extends Controller{
  18. private $responseController;
  19. private $encryptionController;
  20. private $functionsController;
  21. private $publicKey = "zOgD0uF22+xg37nTmA+bg/6/E80BJYeHeByGpeTrNFw=";
  22. private $secretKey = "ydl27x22cNsNY0z6o3Fr6XZoUvsX0QMZx6MaiwN+KCnM6APS4Xbb7GDfudOYD5uD/r8TzQElh4d4HIal5Os0XA==";
  23. public function __construct(){
  24. $this->responseController = new ResponseController();
  25. $this->encryptionController = new EncryptionController();
  26. $this->functionsController = new FunctionsController();
  27. }
  28. public function getLastConnections($idUser, $line){
  29. DB::enableQueryLog();
  30. $idUser = $this->encryptionController->decrypt($idUser);
  31. if(!$idUser){
  32. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  33. }
  34. $usr = DB::table('S002V01TUSUA')->where([
  35. ['USUA_NULI', '=', $line],
  36. ['USUA_IDUS', '=', $idUser]
  37. ])->first();
  38. if(is_null($usr)){
  39. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  40. }
  41. $lastConnections = DB::table('S002V01TUSUA')->leftJoin('S002V01TBIAC', 'USUA_ULCO', '=', 'BIAC_IDCO')->select(
  42. 'USUA_IDUS AS IDUSUARIO',
  43. 'BIAC_FECO AS ULCON'
  44. )->get()->all();
  45. $now = $this->functionsController->now();
  46. $nowStr = $now->toDateTimeString();
  47. $actions = DB::getQueryLog();
  48. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  49. $idac = $this->functionsController->registerActivity(
  50. $line,
  51. 'S002V01M01ADSI',
  52. 'S002V01F01MAUS',
  53. 'S002V01P02HICO',
  54. 'Consulta',
  55. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las últimas conexiones de los demás usuarios.",
  56. $idUser,
  57. $nowStr
  58. );
  59. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  60. return $this->responseController->makeresponse(false, "EXITO", $lastConnections);
  61. }
  62. public function setLastConnection(Request $request){
  63. DB::enableQueryLog();
  64. $validator = Validator::make($request->all(), [
  65. 'id' => 'required|string',
  66. 'linea' => 'required|integer',
  67. 'latitude' => 'required|numeric|between:-90,90',
  68. 'longitude' => 'required|numeric|between:-180,180',
  69. 'accuracy' => 'required|numeric|between:0,1000000',
  70. 'city' => 'string|max:100',
  71. 'state' => 'string|max:100',
  72. 'country' => 'required|string|max:50',
  73. ]);
  74. if($validator->fails()){
  75. return $this->responseController->makeResponse(
  76. true,
  77. "Se encontraron uno o más errores.",
  78. $this->responseController->makeErrors(
  79. $validator->errors()->messages()
  80. ),
  81. 401
  82. );
  83. }
  84. $form = $request->all();
  85. $idUser = $this->encryptionController->decrypt($form['id']);
  86. if(!$idUser){
  87. return $this->responseController->makeResponse(true, "La ID del usuario no está encriptado correctamente.", [], 400);
  88. }
  89. $usr = DB::table('S002V01TUSUA')->where([
  90. ['USUA_NULI', '=', $form['linea']],
  91. ['USUA_IDUS', '=', $idUser]
  92. ])->first();
  93. if(is_null($usr)){
  94. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  95. }
  96. $now = $this->functionsController->now();
  97. $nowStr = $now->toDateTimeString();
  98. $ipv = $request->ip();
  99. $v4 = filter_var($ipv, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ? true : false;
  100. $state = isset($login['state']) ? $login['state'] : '-';
  101. //Se obtiene el dispositivo utilizado
  102. $agent = new Agent();
  103. $device = 'Desconocido';
  104. if($agent->isDesktop()) $device = 'PC';
  105. else if($agent->device()) $device = $agent->device();
  106. //Se obtiene el SO utilizado y la versión del mismo
  107. $platform = $agent->platform() ? $agent->platform() : 'Desconocido';
  108. $version = 'N/A';
  109. if($platform != 'Desconocido') $version = $agent->version($platform);
  110. //Se obtiene el navegador web utilizado
  111. $browser = $agent->browser() ? $agent->browser() : 'Desconocido';
  112. $idLastConn = DB::table('S002V01TBIAC')->insertGetId([
  113. 'BIAC_NULI' => $form['linea'],
  114. 'BIAC_IDUS' => $idUser,
  115. 'BIAC_FECO' => $nowStr,
  116. 'BIAC_IPV4' => $v4 ? $ipv : null,
  117. 'BIAC_IPV6' => !$v4 ? $ipv : null,
  118. 'BIAC_LATI' => $form['latitude'],
  119. 'BIAC_LONG' => $form['longitude'],
  120. 'BIAC_PREC' => $form['accuracy'],
  121. 'BIAC_CIUD' => $form['city'],
  122. 'BIAC_ESTA' => $state,
  123. 'BIAC_PAIS' => $form['country'],
  124. 'BIAC_DISP' => $device,
  125. 'BIAC_SIOP' => $platform,
  126. 'BIAC_VSOP' => $version,
  127. 'BIAC_NAVE' => $browser,
  128. ]);
  129. DB::table('S002V01TUSUA')->where('USUA_IDUS', '=', $idUser)->update(['USUA_ULCO' => $idLastConn]);
  130. $actions = DB::getQueryLog();
  131. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  132. $idac = $this->functionsController->registerActivity(
  133. $form['linea'],
  134. 'S002V01M01ADSI',
  135. 'S002V01F01MAUS',
  136. 'S002V01P01MOCO',
  137. 'Registró',
  138. "El usuario $name (" . $usr->USUA_IDUS . ") registró su última conexión al sistema.",
  139. $idUser,
  140. $nowStr,
  141. );
  142. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  143. return $this->responseController->makeResponse(false, 'EXITO');
  144. return $this->responseController->makeResponse(false, "EXITO: Actualización correcta.");
  145. }
  146. public function getCatalogues($idUser, $line){
  147. DB::enableQueryLog();
  148. $idUser = $this->encryptionController->decrypt($idUser);
  149. if(!$idUser){
  150. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  151. }
  152. $usr = DB::table('S002V01TUSUA')->where([
  153. ['USUA_IDUS', '=', $idUser],
  154. ['USUA_NULI', '=', $line]
  155. ])->first();
  156. if(is_null($usr)){
  157. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  158. }
  159. $catalogues = DB::table('S002V01TCATA')->select(
  160. 'CATA_IDCA AS IDCATALOGO',
  161. 'CATA_TICA AS TIPOCATALOGO',
  162. 'CATA_VERS AS VERSION',
  163. 'CATA_ARRE AS ADJUNTOS',
  164. 'CATA_USRE AS USUARIOREGISTRO',
  165. 'CATA_FERE AS FECHAREGISTRO',
  166. )->where([
  167. ['CATA_NULI', '=', $line],
  168. ['CATA_ESTA', '=', 'Activo']
  169. ])->get()->all();
  170. $cataloguesF = [];
  171. foreach($catalogues as $catalogue){
  172. $usrReg = DB::table('S002V01TUSUA')->where([
  173. ['USUA_NULI', '=', $line],
  174. ['USUA_IDUS', '=', $catalogue->USUARIOREGISTRO],
  175. ])->first();
  176. $idReg = $catalogue->USUARIOREGISTRO;
  177. $catalogue->USUARIOREGISTRO = $this->functionsController->joinName(
  178. $usrReg->USUA_NOMB,
  179. $usrReg->USUA_APPA,
  180. $usrReg->USUA_APMA
  181. ) . " ($idReg)";
  182. $cataloguesF[] = $catalogue;
  183. }
  184. $now = $this->functionsController->now();
  185. $nowStr = $now->toDateTimeString();
  186. $actions = DB::getQueryLog();
  187. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  188. $idac = $this->functionsController->registerActivity(
  189. $line,
  190. 'S002V01M01ADSI',
  191. 'S002V01F10AGCA',
  192. 'S002V01P01HICA',
  193. 'Consulta',
  194. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los catálogos registrados.",
  195. $idUser,
  196. $nowStr
  197. );
  198. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  199. return $this->responseController->makeResponse(false, 'EXITO', $cataloguesF);
  200. }
  201. public function getUserConnections($id, $idUser, $line){
  202. DB::enableQueryLog();
  203. $idUser = $this->encryptionController->decrypt($idUser);
  204. if(!$idUser){
  205. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  206. }
  207. $usr = DB::table('S002V01TUSUA')->where([
  208. ['USUA_NULI', '=', $line],
  209. ['USUA_IDUS', '=', $idUser]
  210. ])->first();
  211. if(is_null($usr)){
  212. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  213. }
  214. $id = $this->encryptionController->decrypt($id);
  215. if(!$id){
  216. return $this->responseController->makeResponse(true, "El ID del usuario que desea consultar no está encriptado correctamente.", [], 400);
  217. }
  218. $usrCons = DB::table('S002V01TUSUA')->where([
  219. ['USUA_NULI', '=', $line],
  220. ['USUA_IDUS', '=', $id]
  221. ])->first();
  222. if(is_null($usrCons)){
  223. return $this->responseController->makeResponse(true, 'El usuario consultado no existe.', [], 404);
  224. }
  225. $connections = DB::table('S002V01TBIAC')->select(
  226. 'BIAC_IPV4 AS IP4',
  227. 'BIAC_IPV6 AS IP6',
  228. 'BIAC_FECO AS FECHA',
  229. 'BIAC_LATI AS LATITUD',
  230. 'BIAC_LONG AS LONGITUD',
  231. 'BIAC_PREC AS PRECISION',
  232. 'BIAC_CIUD AS CIUDAD',
  233. 'BIAC_ESTA AS ESTADO',
  234. 'BIAC_PAIS AS PAIS',
  235. 'BIAC_DISP AS DISPOSITIVO',
  236. 'BIAC_SIOP AS SITEMAOPERATIVO',
  237. 'BIAC_VSOP AS VERSIONSO',
  238. 'BIAC_NAVE AS NAVEGADOR',
  239. )->where([
  240. ['BIAC_IDUS', '=', $id],
  241. ['BIAC_NULI', '=', $line]
  242. ])->orderBy('BIAC_FECO', 'desc')->get()->all();
  243. $responseArr = [
  244. 'NOMBRE' => $usrCons->USUA_NOMB,
  245. 'APPAT' => $usrCons->USUA_APPA,
  246. 'APMAT' => $usrCons->USUA_APMA,
  247. 'ULTCON' => $usrCons->USUA_ULCO,
  248. 'HISTORIAL' => $connections
  249. ];
  250. $now = $this->functionsController->now();
  251. $nowStr = $now->toDateTimeString();
  252. $actions = DB::getQueryLog();
  253. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  254. $nameCons = $this->functionsController->joinName($usrCons->USUA_NOMB, $usrCons->USUA_APPA, $usrCons->USUA_APMA);
  255. $idac = $this->functionsController->registerActivity(
  256. $line,
  257. 'S002V01M01ADSI',
  258. 'S002V01F01MAUS',
  259. 'S002V01P02HICO',
  260. 'Consulta',
  261. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las conexiones del usuario $nameCons ($id).",
  262. $idUser,
  263. $nowStr,
  264. );
  265. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  266. return $this->responseController->makeResponse(false, "EXITO", $responseArr);
  267. }
  268. public function getLastActions($idUser, $line){
  269. DB::enableQueryLog();
  270. $idUser = $this->encryptionController->decrypt($idUser);
  271. if(!$idUser){
  272. return $this->responseController->makeResponse(true, "El ID el usuario que realizó la petición no está encriptado correctamente.", [], 400);
  273. }
  274. $usr = DB::table('S002V01TUSUA')->where([
  275. ['USUA_IDUS', '=', $idUser],
  276. ['USUA_NULI', '=', $line]
  277. ])->first();
  278. if(is_null($usr)){
  279. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  280. }
  281. $lastActions = DB::table('S002V01TUSUA')->leftJoin('S002V01TACCI', 'USUA_ULAC', '=', 'ACCI_IDAC')->select(
  282. 'USUA_IDUS AS IDUSUARIO',
  283. 'USUA_NOMB AS NOMBRE',
  284. 'USUA_APPA AS APEPAT',
  285. 'USUA_APMA AS APEMAT',
  286. 'ACCI_IDMO AS IDMODULO',
  287. 'ACCI_IDSM AS IDSUBMODULO',
  288. 'ACCI_IDFU AS IDFUNCION',
  289. 'ACCI_IDPA AS IDPANTALLA',
  290. 'ACCI_TIAC AS TIPOACCION',
  291. 'ACCI_DESC AS DESCRIPCION',
  292. 'ACCI_FEAC AS FECHAACCION',
  293. )->get()->all();
  294. $now = $this->functionsController->now();
  295. $nowStr = $now->toDateTimeString();
  296. $actions = DB::getQueryLog();
  297. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  298. $idac = $this->functionsController->registerActivity(
  299. $line,
  300. 'S002V01M01ADSI',
  301. 'S002V01F02SAUS',
  302. 'S002V01P01MOAC',
  303. 'Consulta',
  304. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las últimas acciónes realizadas.",
  305. $idUser,
  306. $nowStr
  307. );
  308. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  309. return $this->responseController->makeResponse(false, 'EXITO', $lastActions);
  310. }
  311. public function getUserActions($id, $idUser, $line){
  312. DB::enableQueryLog();
  313. $id = $this->encryptionController->decrypt($id);
  314. if(!$id){
  315. return $this->responseController->makeResponse(true, 'El ID del usuario que desea consultar no está encriptado correctamente.', [], 400);
  316. }
  317. $idUser = $this->encryptionController->decrypt($idUser);
  318. if(!$idUser){
  319. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  320. }
  321. $usr = DB::table('S002V01TUSUA')->where([
  322. ['USUA_IDUS', '=', $idUser],
  323. ['USUA_NULI', '=', $line]
  324. ])->first();
  325. if(is_null($usr)){
  326. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  327. }
  328. $userInfo = DB::table('S002V01TUSUA')->select(
  329. 'USUA_IDUS AS IDUSUARIO',
  330. 'USUA_NOMB AS NOMBRE',
  331. 'USUA_APPA AS APEPAT',
  332. 'USUA_APMA AS APEMAT'
  333. )->where([
  334. ['USUA_IDUS', '=', $id],
  335. ['USUA_NULI', '=', $line],
  336. ])->get()->first();
  337. if(is_null($userInfo)){
  338. return $this->responseController->makeResponse(true, 'El usuario solicitado no existe.', [], 404);
  339. }
  340. $actions = DB::table('S002V01TACCI')->select(
  341. 'MODU_NOMO AS MODULO',
  342. 'SUBM_NOMB AS SUBMODULO',
  343. 'FUNC_NOMB AS FUNCION',
  344. 'PANT_NOMB AS PANTALLA',
  345. 'ACCI_TIAC AS TIPOACCION',
  346. 'ACCI_DESC AS DESCRIPCION',
  347. 'ACCI_FEAC AS FECHA',
  348. )->leftJoin('S002V01TMODU', 'ACCI_IDMO', '=', 'MODU_IDMO')
  349. ->leftJoin('S002V01TSUBM', 'ACCI_IDSM', '=', 'SUBM_IDSM')
  350. ->leftJoin('S002V01TPANT', 'ACCI_IDPA', '=', 'PANT_IDPA')
  351. ->leftJoin('S002V01TFUNC', 'ACCI_IDFU', '=', 'FUNC_IDFU')->where([
  352. ['ACCI_IDUS', '=', $id],
  353. ['ACCI_NULI', '=', $line],
  354. ])->orderBy('ACCI_FEAC', 'desc')->get()->all();
  355. $userInfo->HISTORIAL = $actions;
  356. $now = $this->functionsController->now();
  357. $nowStr = $now->toDateTimeString();
  358. $actions = DB::getQueryLog();
  359. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  360. $nameCons = $this->functionsController->joinName($userInfo->NOMBRE, $userInfo->APEPAT, $userInfo->APEMAT);
  361. $idac = $this->functionsController->registerActivity(
  362. $line,
  363. 'S002V01M01ADSI',
  364. 'S002V01F02SAUS',
  365. 'S002V01P02HIAC',
  366. 'Consulta',
  367. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las acciones del usuario $nameCons (" . $userInfo->IDUSUARIO . ")",
  368. $idUser,
  369. $nowStr
  370. );
  371. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  372. return $this->responseController->makeResponse(false, 'EXITO', $userInfo);
  373. }
  374. public function readCatalogue($id, $idUser, $line){
  375. DB::enableQueryLog();
  376. $id = $this->encryptionController->decrypt($id);
  377. if(!$id){
  378. return $this->responseController->makeResponse(true, "El ID del catálogo no fue encriptado correctamente");
  379. }
  380. $idUser = $this->encryptionController->decrypt($idUser);
  381. if(!$idUser){
  382. return $this->responseController->makeResponse(true, "El ID del usuario no fue encriptado correctamente");
  383. }
  384. $usr = DB::table('S002V01TUSUA')->where([
  385. ['USUA_IDUS', '=', $idUser],
  386. ['USUA_NULI', '=', $line]
  387. ])->first();
  388. if(is_null($usr)){
  389. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  390. }
  391. $idArr = explode("-", $id);
  392. $idArr2 = explode("=", end($idArr));
  393. $catalogue = DB::table('S002V01TAFAL')->where([
  394. ['AFAL_NULI', '=', $idArr[0]],
  395. ['AFAL_COMO', '=', $idArr[1]],
  396. ['AFAL_CLDO', '=', $idArr[2]],
  397. ['AFAL_FECR', '=', $idArr[3]],
  398. ['AFAL_NUSE', '=', $idArr2[0]],
  399. ['AFAL_NUVE', '=', $idArr2[1]],
  400. ])->first();
  401. if(is_null($catalogue)){
  402. return $this->responseController->makeResponse(true, 'El catálogo solicitado no existe.', [], 404);
  403. }
  404. $now = $this->functionsController->now();
  405. $nowStr = $now->toDateTimeString();
  406. $response = [
  407. "IDCATALOGO" => $this->encryptionController->encrypt($id),
  408. "EXTENSION" => $catalogue->AFAL_EXTE,
  409. "FECHA" => $now->timestamp
  410. ];
  411. copy($catalogue->AFAL_UBIC, 'C:\\inetpub\\wwwroot\\sam\\public_files\\' . $id);
  412. $actions = DB::getQueryLog();
  413. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  414. $idac = $this->functionsController->registerActivity(
  415. $line,
  416. 'S002V01M01ADSI',
  417. 'S002V01F10AGCA',
  418. 'S002V01P01HICA',
  419. 'Consulta',
  420. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el contenido del catálogo $id.",
  421. $idUser,
  422. $nowStr,
  423. );
  424. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  425. return $this->responseController->makeResponse(false, "EXITO", $response);
  426. }
  427. public function getAdvices($idUser, $line){
  428. DB::enableQueryLog();
  429. $idUser = $this->encryptionController->decrypt($idUser);
  430. if(!$idUser){
  431. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  432. }
  433. $usr = DB::table('S002V01TUSUA')->where([
  434. ['USUA_IDUS', '=', $idUser],
  435. ['USUA_NULI', '=', $line]
  436. ])->first();
  437. if(is_null($usr)){
  438. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  439. }
  440. $advices = DB::table('S002V01TAVIS')->select(
  441. 'AVIS_IDAV AS IDAVISO',
  442. 'AVIS_TIAV AS TITULO',
  443. 'AVIS_CADU AS CADUCIDAD',
  444. 'AVIS_ESTA AS ESTATUS'
  445. )->orderBy('AVIS_IDAV', 'asc')->get()->all();
  446. $now = $this->functionsController->now();
  447. $nowStr = $now->toDateTimeString();
  448. $actions = DB::getQueryLog();
  449. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  450. $idac = $this->functionsController->registerActivity(
  451. $line,
  452. 'S002V01M01ADSI',
  453. 'S002V01F03CRAV',
  454. 'S002V01P01HIAV',
  455. 'Consulta',
  456. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los avisos registrados.",
  457. $idUser,
  458. $nowStr
  459. );
  460. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  461. return $this->responseController->makeResponse(false, 'EXITO', $advices);
  462. }
  463. public function getAdvice($id, $idUser, $line){
  464. DB::enableQueryLog();
  465. $idUser = $this->encryptionController->decrypt($idUser);
  466. if(!$idUser){
  467. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  468. }
  469. $usr = DB::table('S002V01TUSUA')->where([
  470. ['USUA_IDUS', '=', $idUser],
  471. ['USUA_NULI', '=', $line]
  472. ])->first();
  473. if(is_null($usr)){
  474. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  475. }
  476. $id = $this->encryptionController->decrypt($id);
  477. if(!$id){
  478. return $this->responseController->makeResponse(true, 'El ID del aviso que consultó no está encriptado correctamente.', [], 400);
  479. }
  480. $advice = DB::table('S002V01TAVIS')->where([
  481. ['AVIS_NULI', '=', $line],
  482. ['AVIS_IDAV', '=', $id]
  483. ])->first();
  484. if(is_null($advice)){
  485. return $this->responseController->makeResponse(true, 'El aviso consultado no existe.', [], 404);
  486. }else if($advice->AVIS_ESTA != 'Activo'){
  487. return $this->responseController->makeResponse(true, 'El aviso consultado no está disponible.', [], 401);
  488. }
  489. $caduStr = $advice->AVIS_CADU;
  490. $caduObj = new Carbon($caduStr);
  491. $now = $this->functionsController->now();
  492. if($now->gt($caduObj)){
  493. return $this->responseController->makeResponse(true, 'El aviso consultado está caducado.', [], 401);
  494. }
  495. $usre = DB::table('S002V01TUSUA')->where([
  496. ['USUA_IDUS', '=', $advice->AVIS_USRE],
  497. ['USUA_NULI', '=', $line]
  498. ])->first();
  499. $attachedArr = json_decode($advice->AVIS_ADJU, true);
  500. $attachedF = [];
  501. foreach($attachedArr as $fileEnc){
  502. $fileDec = $this->encryptionController->decrypt($fileEnc);
  503. $nameArr = explode('=', $fileDec);
  504. $idArr = explode('-', $nameArr[0]);
  505. $file = DB::table('S002V01TAFAL')->where([
  506. ['AFAL_NULI', '=', $idArr[0]],
  507. ['AFAL_COMO', '=', $idArr[1]],
  508. ['AFAL_CLDO', '=', $idArr[2]],
  509. ['AFAL_FECR', '=', $idArr[3]],
  510. ['AFAL_NUSE', '=', $idArr[4]],
  511. ['AFAL_NUVE', '=', $nameArr[1]],
  512. ])->first();
  513. $attachedF[] = [
  514. 'id' => $fileEnc,
  515. 'name' => $file->AFAL_NOAR . '.' . $file->AFAL_EXTE,
  516. 'size' => $file->AFAL_TAMA
  517. ];
  518. }
  519. $adviceFin = [
  520. 'TITULO' => $advice->AVIS_TIAV,
  521. 'CONTENIDO' => $advice->AVIS_COAV,
  522. 'CADUCIDAD' => $advice->AVIS_CADU,
  523. 'AUDIENCIA' => $this->encryptionController->encrypt($advice->AVIS_ALAV),
  524. 'ADJUNTOS' => json_encode($attachedF),
  525. 'IDUSUARIO' => $this->encryptionController->encrypt($advice->AVIS_USRE),
  526. 'NOMBREUSUARIO' => $this->functionsController->joinName($usre->USUA_NOMB, $usre->USUA_APPA, $usre->USUA_APMA)
  527. ];
  528. $actions = DB::getQueryLog();
  529. $nowStr = $now->toDateTimeString();
  530. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  531. $idac = $this->functionsController->registerActivity(
  532. $line,
  533. 'S002V01M01ADSI',
  534. 'S002V01F03CRAV',
  535. 'S002V01P01HIAV',
  536. 'Consulta',
  537. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el aviso " . $advice->AVIS_TIAV . " ($id).",
  538. $idUser,
  539. $nowStr
  540. );
  541. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  542. return $this->responseController->makeResponse(false, 'EXITO', $adviceFin);
  543. }
  544. public function publishAdvice(Request $request){
  545. DB::enableQueryLog();
  546. $validator = Validator::make($request->all(), [
  547. 'id_user' => 'required|string',
  548. 'linea' => 'required|integer',
  549. 'titulo' => 'required|string|max:100',
  550. 'caducidad' => 'required|date_format:Y-m-d H:i:s',
  551. 'contenido' => 'required|string',
  552. 'audiencia' => 'required|json',
  553. 'archivos' => 'required|json',
  554. ]);
  555. if($validator->fails()){
  556. return $this->responseController->makeResponse(
  557. true,
  558. "Se encontraron uno o más errores.",
  559. $this->responseController->makeErrors(
  560. $validator->errors()->messages()
  561. ),
  562. 401
  563. );
  564. }
  565. $advice = $request->all();
  566. $idUser = $this->encryptionController->decrypt($advice['id_user']);
  567. if(!$idUser){
  568. return $this->responseController->makeResponse(true, "El ID del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  569. }
  570. $usr = DB::table('S002V01TUSUA')->where([
  571. ['USUA_IDUS', '=', $idUser],
  572. ['USUA_NULI', '=', $advice['linea']]
  573. ])->first();
  574. if(is_null($usr)){
  575. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  576. }
  577. $now = $this->functionsController->now();
  578. $nowStr = $now->toDateTimeString();
  579. $filesArr = json_decode($advice['archivos'], true);
  580. $attachedArr = [];
  581. foreach($filesArr as $file){
  582. $idFile = $file['id'];
  583. $idFile = $this->encryptionController->decrypt($idFile);
  584. if(!$idFile){
  585. return $this->responseController->makeResponse(true, "El ID del archivo $file[name] no fue encriptado correctamente", [], 400);
  586. }
  587. $fileObj = DB::table('S002V01TARTE')->where([
  588. ['ARTE_IDAR', '=', $idFile],
  589. ['ARTE_NULI', '=', $advice['linea']]
  590. ])->first();
  591. if(is_null($fileObj)){
  592. return $this->responseController->makeResponse(true, "El ID del archivo $file[name] no existe", [], 404);
  593. }else if($fileObj->ARTE_ESTA == 'Eliminado'){
  594. return $this->responseController->makeResponse(true, "El archivo $file[name] está eliminado", [], 404);
  595. }
  596. $fileSaved = $this->moveFinalFile($advice['linea'], 'AV', $fileObj, 'S002V01M01ADSI', 'S002V01F03CRAV', 'S002V01P02CNAV');
  597. if(!$fileSaved[0]){
  598. return $this->responseController->makeResponse(true, "El archivo no pudo guardarse: $fileSaved[1]", [], 500);
  599. }
  600. $attachedArr[] = $this->encryptionController->encrypt($fileSaved[1]);
  601. DB::table('S002V01TARTE')->where([
  602. ['ARTE_IDAR', '=', $idFile],
  603. ['ARTE_NULI', '=', $advice['linea']],
  604. ])->update([
  605. 'ARTE_ESTA' => 'Eliminado',
  606. 'ARTE_USMO' => $idUser,
  607. 'ARTE_FEMO' => $nowStr
  608. ]);
  609. }
  610. $attachedStr = json_encode($attachedArr);
  611. $idav = DB::table('S002V01TAVIS')->insertGetId([
  612. 'AVIS_NULI' => $advice['linea'],
  613. 'AVIS_TIAV' => $advice['titulo'],
  614. 'AVIS_COAV' => $advice['contenido'],
  615. 'AVIS_ALAV' => $advice['audiencia'],
  616. 'AVIS_ADJU' => $attachedStr,
  617. 'AVIS_CADU' => $advice['caducidad'],
  618. 'AVIS_FERE' => $nowStr,
  619. 'AVIS_USRE' => $idUser
  620. ]);
  621. $actions = DB::getQueryLog();
  622. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  623. $idac = $this->functionsController->registerActivity(
  624. $advice['linea'],
  625. 'S002V01M01ADSI',
  626. 'S002V01F03CRAV',
  627. 'S002V01P02CNAV',
  628. 'Registro',
  629. "El usuario $name (" . $usr->USUA_IDUS . ") registró un nuevo aviso.",
  630. $idUser,
  631. $nowStr,
  632. );
  633. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $advice['linea']);
  634. return $this->responseController->makeresponse(false, "EXITO", ['idav' => $this->encryptionController->encrypt($idav)]);
  635. }
  636. public function updateAdvice(Request $request){
  637. DB::enableQueryLog();
  638. $validator = Validator::make($request->all(), [
  639. 'id_user' => 'required|string',
  640. 'id_advice' =>'required|string',
  641. 'linea' => 'required|integer',
  642. 'titulo' => 'required|string|max:100',
  643. 'caducidad' => 'required|date_format:Y-m-d H:i:s',
  644. 'contenido' => 'required|string',
  645. 'audiencia' => 'required|json',
  646. ]);
  647. if($validator->fails()){
  648. return $this->responseController->makeResponse(
  649. true,
  650. "Se encontraron uno o más errores.",
  651. $this->responseController->makeErrors(
  652. $validator->errors()->messages()
  653. ),
  654. 401
  655. );
  656. }
  657. $advice = $request->all();
  658. $idAdvice = $this->encryptionController->decrypt($advice['id_advice']);
  659. if(!$idAdvice){
  660. return $this->responseController->makeResponse(true, "El ID del aviso no fue encriptado correctamente.", [], 401);
  661. }
  662. $idUser = $this->encryptionController->decrypt($advice['id_user']);
  663. if(!$idUser){
  664. return $this->responseController->makeResponse(true, "El ID del usuario que realizó la solicitud no fue encriptado correctamente.", [], 401);
  665. }
  666. $usr = DB::table('S002V01TUSUA')->where([
  667. ['USUA_IDUS', '=', $idUser],
  668. ['USUA_NULI', '=', $advice['linea']]
  669. ])->first();
  670. if(is_null($usr)){
  671. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  672. }
  673. $now = $this->functionsController->now();
  674. $nowStr = $now->toDateTimeString();
  675. DB::table('S002V01TAVIS')->where([
  676. ['AVIS_NULI', '=', $advice['linea']],
  677. ['AVIS_IDAV', '=', $idAdvice]
  678. ])->update([
  679. 'AVIS_TIAV' => $advice['titulo'],
  680. 'AVIS_COAV' => $advice['contenido'],
  681. 'AVIS_ALAV' => $advice['audiencia'],
  682. 'AVIS_CADU' => $advice['caducidad'],
  683. 'AVIS_FEMO' => $nowStr,
  684. 'AVIS_USMO' => $idUser,
  685. ]);
  686. $actions = DB::getQueryLog();
  687. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  688. $idac = $this->functionsController->registerActivity(
  689. $advice['linea'],
  690. 'S002V01M01ADSI',
  691. 'S002V01F03CRAV',
  692. 'S002V01P03EDAV',
  693. 'Actualización',
  694. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó el aviso $idAdvice.",
  695. $idUser,
  696. $nowStr,
  697. );
  698. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $advice['linea']);
  699. return $this->responseController->makeresponse(false, "EXITO");
  700. }
  701. public function deleteAdvice(Request $request){
  702. DB::enableQueryLog();
  703. $validator = Validator::make($request->all(), [
  704. 'id_user' => 'required|string',
  705. 'id_advice' =>'required|string',
  706. 'linea' => 'required|integer',
  707. ]);
  708. if($validator->fails()){
  709. return $this->responseController->makeResponse(
  710. true,
  711. "Se encontraron uno o más errores.",
  712. $this->responseController->makeErrors(
  713. $validator->errors()->messages()
  714. ),
  715. 401
  716. );
  717. }
  718. $advice = $request->all();
  719. $idAdvice = $this->encryptionController->decrypt($advice['id_advice']);
  720. if(!$idAdvice){
  721. return $this->responseController->makeResponse(true, "El ID del aviso no fue encriptado correctamente.", [], 401);
  722. }
  723. $idUser = $this->encryptionController->decrypt($advice['id_user']);
  724. if(!$idUser){
  725. return $this->responseController->makeResponse(true, "El ID del usuario que realizó la solicitud no fue encriptado correctamente.", [], 401);
  726. }
  727. $usr = DB::table('S002V01TUSUA')->where([
  728. ['USUA_IDUS', '=', $idUser],
  729. ['USUA_NULI', '=', $advice['linea']]
  730. ])->first();
  731. if(is_null($usr)){
  732. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  733. }
  734. $now = $this->functionsController->now();
  735. $nowStr = $now->toDateTimeString();
  736. DB::table('S002V01TAVIS')->where([
  737. ['AVIS_NULI', '=', $advice['linea']],
  738. ['AVIS_IDAV', '=', $idAdvice]
  739. ])->update([
  740. 'AVIS_ESTA' => 'Eliminado',
  741. 'AVIS_FEMO' => $nowStr,
  742. 'AVIS_USMO' => $idUser,
  743. ]);
  744. $actions = DB::getQueryLog();
  745. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  746. $idac = $this->functionsController->registerActivity(
  747. $advice['linea'],
  748. 'S002V01M01ADSI',
  749. 'S002V01F03CRAV',
  750. 'S002V01P01HIAV',
  751. 'Eliminación',
  752. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó el aviso $idAdvice.",
  753. $idUser,
  754. $nowStr,
  755. );
  756. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $advice['linea']);
  757. return $this->responseController->makeresponse(false, "EXITO");
  758. }
  759. private function moveFinalFile(int $line, string $cldo, object $file, string $module, string $function, string $screen, string $submodule = null){
  760. $ubiTempFile = $file->ARTE_UBTE;
  761. $ubiFileArr = explode('tempFiles', $ubiTempFile);
  762. $ubic = $ubiFileArr[0] . 'files' . $ubiFileArr[1];
  763. $now = $this->functionsController->now();
  764. $como = substr($module, -4);
  765. $date = $now->toDateTimeString();
  766. $dateArr = explode(' ', $date);
  767. $dateStr = $dateArr[0];
  768. $dA = explode('-', $dateStr);
  769. $dA[0] = substr($dA[0], -2);
  770. $fecr = implode('', $dA);
  771. $nameStr = $file->ARTE_NOAR;
  772. $nameArr = explode('.', $nameStr);
  773. $nuve = 0;
  774. $nuse = 0;
  775. $lastSec = DB::table('S002V01TAFAL')->where([
  776. ['AFAL_NULI', '=', $line],
  777. ['AFAL_COMO', '=', $como],
  778. ['AFAL_CLDO', '=', $cldo]
  779. ])->orderBy('AFAL_NUSE', 'desc')->first();
  780. if(is_null($lastSec)){
  781. $nuse = 1;
  782. }else{
  783. $nuse = $lastSec->AFAL_NUSE + 1;
  784. }
  785. $lastVersion = DB::table('S002V01TAFAL')->where([
  786. ['AFAL_NULI', '=', $line],
  787. ['AFAL_NOAR', '=', $nameArr[0]],
  788. ['AFAL_EXTE', '=', $nameArr[1]],
  789. ])->orderBy('AFAL_NUVE', 'desc')->first();
  790. if(is_null($lastVersion)){
  791. $nuve = 1;
  792. }else{
  793. $nuve = $lastVersion->AFAL_NUVE + 1;
  794. }
  795. if($nuve > 99) return [false, 'El archivo llegó al número máximo de versiones'];
  796. if($nuse > 999999)return [false, 'El archivo llegó al número de secuencia máximo'];
  797. $noar = $nameArr[0];
  798. $exte = $nameArr[1];
  799. if(strlen($noar) > 100) return [false, 'El nombre del archivo tiene una longitud mayor a 100 caracteres'];
  800. $tama = $file->ARTE_TAMA;
  801. $usac = json_encode([$file->ARTE_USRE]);
  802. $usre = $file->ARTE_USRE;
  803. $fere = $file->ARTE_FERE;
  804. DB::table('S002V01TAFAL')->insert([
  805. 'AFAL_NULI' => $line,
  806. 'AFAL_COMO' => $como,
  807. 'AFAL_CLDO' => $cldo,
  808. 'AFAL_FECR' => $fecr,
  809. 'AFAL_NUSE' => $nuse,
  810. 'AFAL_NUVE' => $nuve,
  811. 'AFAL_NOAR' => $noar,
  812. 'AFAL_EXTE' => $exte,
  813. 'AFAL_TAMA' => $tama,
  814. 'AFAL_UBIC' => $ubic,
  815. 'AFAL_USAC' => $usac,
  816. 'AFAL_USRE' => $usre,
  817. 'AFAL_FERE' => $fere
  818. ]);
  819. if(file_exists($file->ARTE_UBTE)){
  820. rename($file->ARTE_UBTE, $ubic);
  821. }
  822. $codigo = "";
  823. if(strlen($line) < 2){
  824. $codigo .= "0$line-";
  825. }else{
  826. $codigo .= "$line-";
  827. }
  828. $codigo .= "$como-$cldo-$fecr-";
  829. switch(strlen($nuse)){
  830. case 1: $codigo .= "00000$nuse"; break;
  831. case 2: $codigo .= "0000$nuse"; break;
  832. case 3: $codigo .= "000$nuse"; break;
  833. case 4: $codigo .= "00$nuse"; break;
  834. case 5: $codigo .= "0$nuse"; break;
  835. default: $codigo .= "$nuse"; break;
  836. }
  837. if(strlen($nuve) < 2){
  838. $codigo .= "=0$nuve=";
  839. }else{
  840. $codigo .= "=$nuve=";
  841. }
  842. $codigo .= "$noar.$exte";
  843. return [true, $codigo];
  844. }
  845. public function getTwoStepsAuthUsers($idUser, $line){
  846. DB::enableQueryLog();
  847. $idUser = $this->encryptionController->decrypt($idUser);
  848. if(!$idUser){
  849. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  850. }
  851. $usr = DB::table('S002V01TUSUA')->where([
  852. ['USUA_IDUS', '=', $idUser],
  853. ['USUA_NULI', '=', $line]
  854. ])->first();
  855. if(is_null($usr)){
  856. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  857. }
  858. $usersAuth = DB::table('S002V01TUSUA')->select([
  859. 'USUA_IDUS',
  860. 'USUA_NOMB',
  861. 'USUA_APPA',
  862. 'USUA_APMA',
  863. 'USUA_FADO',
  864. ])->where('USUA_NULI', '=', $line)->get()->all();
  865. $arrAuth = [];
  866. foreach($usersAuth as $user){
  867. $name = $this->functionsController->joinName($user->USUA_NOMB, $user->USUA_APPA, $user->USUA_APMA);
  868. $authEnabled = $user->USUA_FADO == 'Si';
  869. $arrAuth[] = [
  870. 'IDUSUARIO' => $this->encryptionController->encrypt($user->USUA_IDUS),
  871. 'NOMBREUSUARIO' => $name,
  872. 'AUTHENABLED' => $authEnabled
  873. ];
  874. }
  875. $now = $this->functionsController->now();
  876. $nowStr = $now->toDateTimeString();
  877. $actions = DB::getQueryLog();
  878. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  879. $idac = $this->functionsController->registerActivity(
  880. $line,
  881. 'S002V01M01ADSI',
  882. 'S002V01F04POSE',
  883. 'S002V01P03ADPA',
  884. 'Consulta',
  885. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las autenticaciones en dos pasos de los usuarios.",
  886. $idUser,
  887. $nowStr
  888. );
  889. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  890. return $this->responseController->makeResponse(false, 'EXITO', $arrAuth);
  891. }
  892. public function setTwoStepsAuth(Request $request){
  893. DB::enableQueryLog();
  894. $validator = Validator::make($request->all(), [
  895. 'id_user' => 'required|string',
  896. 'factor_doble' => 'required|in:Si,No',
  897. 'usuario' => 'required|string',
  898. 'linea' => 'required|integer',
  899. ]);
  900. if($validator->fails()){
  901. return $this->responseController->makeResponse(
  902. true,
  903. "Se encontraron uno o más errores.",
  904. $this->responseController->makeErrors(
  905. $validator->errors()->messages()
  906. ),
  907. 401
  908. );
  909. }
  910. $form = $request->all();
  911. $idUser = $this->encryptionController->decrypt($form['id_user']);
  912. if(!$idUser){
  913. return $this->responseController->makeResponse(true, "El ID del usuario que realizó la solicitud no fue encriptado correctamente.", [], 401);
  914. }
  915. $usr = DB::table('S002V01TUSUA')->where([
  916. ['USUA_IDUS', '=', $idUser],
  917. ['USUA_NULI', '=', $form['linea']]
  918. ])->first();
  919. if(is_null($usr)){
  920. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  921. }
  922. $idChange = $this->encryptionController->decrypt($form['usuario']);
  923. if(!$idChange){
  924. return $this->responseController->makeResponse(true, "El ID del usuario que desea modificar no fue encriptado correctamente.", [], 401);
  925. }
  926. $usrMod = DB::table('S002V01TUSUA')->where([
  927. ['USUA_IDUS', '=', $idChange],
  928. ['USUA_NULI', '=', $form['linea']]
  929. ])->first();
  930. if(is_null($usrMod)){
  931. return $this->responseController->makeResponse(true, 'El usuario que desea actualizar no está registrado', [], 404);
  932. }
  933. $now = $this->functionsController->now();
  934. $nowStr = $now->toDateTimeString();
  935. DB::table('S002V01TUSUA')->where([
  936. ['USUA_IDUS', '=', $idChange],
  937. ['USUA_NULI', '=', $form['linea']]
  938. ])->update([
  939. 'USUA_FADO' => $form['factor_doble'],
  940. 'USUA_USMO' => $idUser,
  941. 'USUA_FEMO' => $nowStr
  942. ]);
  943. $actions = DB::getQueryLog();
  944. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  945. $nameMod = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  946. $idac = $this->functionsController->registerActivity(
  947. $form['linea'],
  948. 'S002V01M01ADSI',
  949. 'S002V01F04POSE',
  950. 'S002V01P03ADPA',
  951. 'Actualización',
  952. "El usuario $name (" . $usr->USUA_IDUS . ") cambió el factor de dos pasos del usuario $nameMod ($idChange).",
  953. $idUser,
  954. $nowStr,
  955. );
  956. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  957. return $this->responseController->makeresponse(false, "EXITO");
  958. }
  959. public function getPasswordFormat($idUser, $line){
  960. DB::enableQueryLog();
  961. $idUser = $this->encryptionController->decrypt($idUser);
  962. if(!$idUser){
  963. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  964. }
  965. $usr = DB::table('S002V01TUSUA')->where([
  966. ['USUA_IDUS', '=', $idUser],
  967. ['USUA_NULI', '=', $line]
  968. ])->first();
  969. if(is_null($usr)){
  970. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  971. }
  972. $politicsInfoRoute = str_replace("app\Http\Controllers", "", __DIR__);
  973. $politicsInfoRoute .= "storage\\app\\files\\security-politics.json";
  974. $politicsStr = file_get_contents($politicsInfoRoute);
  975. $politicsArr = json_decode($politicsStr, true);
  976. $now = $this->functionsController->now();
  977. $nowStr = $now->toDateTimeString();
  978. $passwordFormat = $politicsArr['password_format'];
  979. $actions = DB::getQueryLog();
  980. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  981. $idac = $this->functionsController->registerActivity(
  982. $line,
  983. 'S002V01M01ADSI',
  984. 'S002V01F04POSE',
  985. 'S002V01P01PACO',
  986. 'Consulta',
  987. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el formato de las contraseñas.",
  988. $idUser,
  989. $nowStr
  990. );
  991. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  992. return $this->responseController->makeResponse(false, 'EXITO', ['password_format' => $passwordFormat]);
  993. }
  994. public function savePasswordFormat(Request $request){
  995. DB::enableQueryLog();
  996. $validator = Validator::make($request->all(), [
  997. 'id_user' => 'required|string',
  998. 'linea' => 'required|integer',
  999. 'min_length' => 'required|integer|between:8,30',
  1000. 'upper_enabled' => 'required|boolean',
  1001. 'min_upper' => 'required|integer|between:1,10',
  1002. 'number_enabled' => 'required|boolean',
  1003. 'min_number' => 'required|integer|between:1,10',
  1004. 'chars_enabled' => 'required|boolean',
  1005. 'min_chars' => 'required|integer|between:1,10',
  1006. ]);
  1007. if($validator->fails()){
  1008. return $this->responseController->makeResponse(
  1009. true,
  1010. "Se encontraron uno o más errores.",
  1011. $this->responseController->makeErrors(
  1012. $validator->errors()->messages()
  1013. ),
  1014. 401
  1015. );
  1016. }
  1017. $info = $request->all();
  1018. $idUser = $this->encryptionController->decrypt($info['id_user']);
  1019. if(!$idUser){
  1020. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1021. }
  1022. $usr = DB::table('S002V01TUSUA')->where([
  1023. ['USUA_IDUS', '=', $idUser],
  1024. ['USUA_NULI', '=', $info['linea']],
  1025. ])->first();
  1026. if(is_null($usr)){
  1027. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1028. }
  1029. $filePoliticsExists = file_exists('C:\inetpub\wwwroot\sam\storage\app\files\security-politics.json');
  1030. if(!$filePoliticsExists){
  1031. return $this->responseController->makeResponse(true, 'El archivo de politicas de seguridad no fue encontrado.', [], 500);
  1032. }
  1033. $politicsStr = file_get_contents('C:\inetpub\wwwroot\sam\storage\app\files\security-politics.json');
  1034. $politicsArr = json_decode($politicsStr, true);
  1035. $passwordFormat = $politicsArr['password_format'];
  1036. $formatBKP = $politicsStr;
  1037. $passwordFormat['min_length'] = intval($info['min_length']);
  1038. $passwordFormat['upper_enabled'] = intval($info['upper_enabled']) == 1;
  1039. $passwordFormat['min_upper'] = intval($info['min_upper']);
  1040. $passwordFormat['number_enabled'] = intval($info['number_enabled']) == 1;
  1041. $passwordFormat['min_number'] = intval($info['min_number']);
  1042. $passwordFormat['chars_enabled'] = intval($info['chars_enabled']) == 1;
  1043. $passwordFormat['min_chars'] = intval($info['min_chars']);
  1044. $politicsArr['password_format'] = $passwordFormat;
  1045. $finalStr = json_encode($politicsArr);
  1046. file_put_contents('C:\inetpub\wwwroot\sam\storage\app\files\security-politics.json', $finalStr);
  1047. $now = $this->functionsController->now();
  1048. $nowStr = $now->toDateTimeString();
  1049. DB::table('S002V01TAPSE')->insert([
  1050. 'APSE_NULI' => $info['linea'],
  1051. 'APSE_USMO' => $idUser,
  1052. 'APSE_FEMO' => $nowStr,
  1053. 'APSE_VAAN' => $formatBKP,
  1054. 'APSE_VAAC' => $finalStr
  1055. ]);
  1056. $actions = DB::getQueryLog();
  1057. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1058. $idac = $this->functionsController->registerActivity(
  1059. $info['linea'],
  1060. 'S002V01M01ADSI',
  1061. 'S002V01F04POSE',
  1062. 'S002V01P01PACO',
  1063. 'Actualización',
  1064. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó el formato de las contraseñas.",
  1065. $idUser,
  1066. $nowStr
  1067. );
  1068. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $info['linea']);
  1069. return $this->responseController->makeResponse(false, 'EXITO.');
  1070. }
  1071. public function getSessionsDuration($idUser, $line){
  1072. DB::enableQueryLog();
  1073. $idUser = $this->encryptionController->decrypt($idUser);
  1074. if(!$idUser){
  1075. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  1076. }
  1077. $usr = DB::table('S002V01TUSUA')->where([
  1078. ['USUA_IDUS', '=', $idUser],
  1079. ['USUA_NULI', '=', $line]
  1080. ])->first();
  1081. if(is_null($usr)){
  1082. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1083. }
  1084. $politicsInfoRoute = str_replace("app\Http\Controllers", "", __DIR__);
  1085. $politicsInfoRoute .= "storage\\app\\files\\security-politics.json";
  1086. $politicsStr = file_get_contents($politicsInfoRoute);
  1087. $politicsArr = json_decode($politicsStr, true);
  1088. $now = $this->functionsController->now();
  1089. $nowStr = $now->toDateTimeString();
  1090. $sessionsDuration = $politicsArr['sessions_duration'];
  1091. $actions = DB::getQueryLog();
  1092. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1093. $idac = $this->functionsController->registerActivity(
  1094. $line,
  1095. 'S002V01M01ADSI',
  1096. 'S002V01F04POSE',
  1097. 'S002V01P02DUSE',
  1098. 'Consulta',
  1099. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la configuración de duración de sesiones.",
  1100. $idUser,
  1101. $nowStr
  1102. );
  1103. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1104. return $this->responseController->makeResponse(false, 'EXITO', ['sessions_duration' => $sessionsDuration]);
  1105. }
  1106. public function saveSessionsDuration(Request $request){
  1107. DB::enableQueryLog();
  1108. $validator = Validator::make($request->all(), [
  1109. 'id_user' => 'required|string',
  1110. 'linea' => 'required|integer',
  1111. 'days' => 'required|integer|max:30',
  1112. 'hours' => 'required|integer|max:23',
  1113. 'minutes' => 'required|integer|max:59',
  1114. 'seconds' => 'required|integer|max:59',
  1115. ]);
  1116. if($validator->fails()){
  1117. return $this->responseController->makeResponse(
  1118. true,
  1119. "Se encontraron uno o más errores.",
  1120. $this->responseController->makeErrors(
  1121. $validator->errors()->messages()
  1122. ),
  1123. 401
  1124. );
  1125. }
  1126. $info = $request->all();
  1127. $idUser = $this->encryptionController->decrypt($info['id_user']);
  1128. if(!$idUser){
  1129. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1130. }
  1131. $usr = DB::table('S002V01TUSUA')->where([
  1132. ['USUA_IDUS', '=', $idUser],
  1133. ['USUA_NULI', '=', $info['linea']],
  1134. ])->first();
  1135. if(is_null($usr)){
  1136. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1137. }
  1138. $filePoliticsExists = file_exists('C:\inetpub\wwwroot\sam\storage\app\files\security-politics.json');
  1139. if(!$filePoliticsExists){
  1140. return $this->responseController->makeResponse(true, 'El archivo de politicas de seguridad no fue encontrado.', [], 500);
  1141. }
  1142. $politicsStr = file_get_contents('C:\inetpub\wwwroot\sam\storage\app\files\security-politics.json');
  1143. $politicsArr = json_decode($politicsStr, true);
  1144. $sessionsDuration = $politicsArr['sessions_duration'];
  1145. $formatBKP = $politicsStr;
  1146. $sessionsDuration['days'] = intval($info['days']);
  1147. $sessionsDuration['hours'] = intval($info['hours']);
  1148. $sessionsDuration['minutes'] = intval($info['minutes']);
  1149. $sessionsDuration['seconds'] = intval($info['seconds']);
  1150. $politicsArr['sessions_duration'] = $sessionsDuration;
  1151. $finalStr = json_encode($politicsArr);
  1152. file_put_contents('C:\inetpub\wwwroot\sam\storage\app\files\security-politics.json', $finalStr);
  1153. $now = $this->functionsController->now();
  1154. $nowStr = $now->toDateTimeString();
  1155. DB::table('S002V01TAPSE')->insert([
  1156. 'APSE_NULI' => $info['linea'],
  1157. 'APSE_USMO' => $idUser,
  1158. 'APSE_FEMO' => $nowStr,
  1159. 'APSE_VAAN' => $formatBKP,
  1160. 'APSE_VAAC' => $finalStr
  1161. ]);
  1162. $actions = DB::getQueryLog();
  1163. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1164. $idac = $this->functionsController->registerActivity(
  1165. $info['linea'],
  1166. 'S002V01M01ADSI',
  1167. 'S002V01F04POSE',
  1168. 'S002V01P02DUSE',
  1169. 'Actualización',
  1170. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó la duración de las sesiones.",
  1171. $idUser,
  1172. $nowStr
  1173. );
  1174. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $info['linea']);
  1175. return $this->responseController->makeResponse(false, 'EXITO.');
  1176. }
  1177. public function getActiveSessionsNumber($idUser, $line){
  1178. DB::enableQueryLog();
  1179. $idUser = $this->encryptionController->decrypt($idUser);
  1180. if(!$idUser){
  1181. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  1182. }
  1183. $usr = DB::table('S002V01TUSUA')->where([
  1184. ['USUA_IDUS', '=', $idUser],
  1185. ['USUA_NULI', '=', $line]
  1186. ])->first();
  1187. if(is_null($usr)){
  1188. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1189. }
  1190. $politicsInfoRoute = str_replace("app\Http\Controllers", "", __DIR__);
  1191. $politicsInfoRoute .= "storage\\app\\files\\security-politics.json";
  1192. $politicsStr = file_get_contents($politicsInfoRoute);
  1193. $politicsArr = json_decode($politicsStr, true);
  1194. $now = $this->functionsController->now();
  1195. $nowStr = $now->toDateTimeString();
  1196. $activeSessionsNumber = $politicsArr['active_sessions_number'];
  1197. $actions = DB::getQueryLog();
  1198. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1199. $idac = $this->functionsController->registerActivity(
  1200. $line,
  1201. 'S002V01M01ADSI',
  1202. 'S002V01F04POSE',
  1203. 'S002V01P04NSAC',
  1204. 'Consulta',
  1205. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la configuración del número de sesiones activas.",
  1206. $idUser,
  1207. $nowStr
  1208. );
  1209. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1210. return $this->responseController->makeResponse(false, 'EXITO', ['active_sessions_number' => $activeSessionsNumber]);
  1211. }
  1212. public function saveActiveSessionsNumber(Request $request){
  1213. DB::enableQueryLog();
  1214. $validator = Validator::make($request->all(), [
  1215. 'id_user' => 'required|string',
  1216. 'linea' => 'required|integer',
  1217. 'sessions' => 'required|integer|between:1,5',
  1218. ]);
  1219. if($validator->fails()){
  1220. return $this->responseController->makeResponse(
  1221. true,
  1222. "Se encontraron uno o más errores.",
  1223. $this->responseController->makeErrors(
  1224. $validator->errors()->messages()
  1225. ),
  1226. 401
  1227. );
  1228. }
  1229. $info = $request->all();
  1230. $idUser = $this->encryptionController->decrypt($info['id_user']);
  1231. if(!$idUser){
  1232. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1233. }
  1234. $usr = DB::table('S002V01TUSUA')->where([
  1235. ['USUA_IDUS', '=', $idUser],
  1236. ['USUA_NULI', '=', $info['linea']],
  1237. ])->first();
  1238. if(is_null($usr)){
  1239. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1240. }
  1241. $filePoliticsExists = file_exists('C:\inetpub\wwwroot\sam\storage\app\files\security-politics.json');
  1242. if(!$filePoliticsExists){
  1243. return $this->responseController->makeResponse(true, 'El archivo de politicas de seguridad no fue encontrado.', [], 500);
  1244. }
  1245. $politicsStr = file_get_contents('C:\inetpub\wwwroot\sam\storage\app\files\security-politics.json');
  1246. $politicsArr = json_decode($politicsStr, true);
  1247. $formatBKP = $politicsStr;
  1248. $politicsArr['active_sessions_number'] = intval($info['sessions']);
  1249. $finalStr = json_encode($politicsArr);
  1250. file_put_contents('C:\inetpub\wwwroot\sam\storage\app\files\security-politics.json', $finalStr);
  1251. $now = $this->functionsController->now();
  1252. $nowStr = $now->toDateTimeString();
  1253. DB::table('S002V01TAPSE')->insert([
  1254. 'APSE_NULI' => $info['linea'],
  1255. 'APSE_USMO' => $idUser,
  1256. 'APSE_FEMO' => $nowStr,
  1257. 'APSE_VAAN' => $formatBKP,
  1258. 'APSE_VAAC' => $finalStr
  1259. ]);
  1260. $actions = DB::getQueryLog();
  1261. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1262. $idac = $this->functionsController->registerActivity(
  1263. $info['linea'],
  1264. 'S002V01M01ADSI',
  1265. 'S002V01F04POSE',
  1266. 'S002V01P04NSAC',
  1267. 'Actualización',
  1268. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó el número de sesiones activas.",
  1269. $idUser,
  1270. $nowStr
  1271. );
  1272. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $info['linea']);
  1273. return $this->responseController->makeResponse(false, 'EXITO.');
  1274. }
  1275. public function activateMaintenanceMode(Request $request){
  1276. DB::enableQueryLog();
  1277. $validator = Validator::make($request->all(), [
  1278. 'id_user' => 'required|string',
  1279. 'linea' => 'required|integer',
  1280. 'motivos' => 'required|string',
  1281. ]);
  1282. if($validator->fails()){
  1283. return $this->responseController->makeResponse(
  1284. true,
  1285. "Se encontraron uno o más errores.",
  1286. $this->responseController->makeErrors(
  1287. $validator->errors()->messages()
  1288. ),
  1289. 401
  1290. );
  1291. }
  1292. $info = $request->all();
  1293. $idUser = $this->encryptionController->decrypt($info['id_user']);
  1294. if(!$idUser){
  1295. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1296. }
  1297. $usr = DB::table('S002V01TUSUA')->where([
  1298. ['USUA_IDUS', '=', $idUser],
  1299. ['USUA_NULI', '=', $info['linea']],
  1300. ])->first();
  1301. if(is_null($usr)){
  1302. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1303. }
  1304. $maintenanceModeExists = file_exists('C:\inetpub\wwwroot\sam\storage\app\files\maintenance_mode.json');
  1305. if(!$maintenanceModeExists){
  1306. return $this->responseController->makeResponse(true, 'El archivo de mantenimiento no fue encontrado.', [], 500);
  1307. }
  1308. $maintenanceStr = file_get_contents('C:\inetpub\wwwroot\sam\storage\app\files\maintenance_mode.json');
  1309. $maintenanceArr = json_decode($maintenanceStr, true);
  1310. if($maintenanceArr['activated']){
  1311. return $this->responseController->makeResponse(true, 'El modo mantenimiento ya se encuentra activado', [], 401);
  1312. }
  1313. $now = $this->functionsController->now();
  1314. $nowStr = $now->toDateTimeString();
  1315. $idMant = DB::table('S002V01THMSA')->insertGetId([
  1316. 'HMSA_NULI' => $info['linea'],
  1317. 'HMSA_USAC' => $idUser,
  1318. 'HMSA_FEAC' => $nowStr,
  1319. 'HMSA_MOAC' => $info['motivos']
  1320. ]);
  1321. $maintenanceArr['activated'] = true;
  1322. $maintenanceArr['last_activation_id'] = $idMant;
  1323. $finalStr = json_encode($maintenanceArr);
  1324. file_put_contents('C:\inetpub\wwwroot\sam\storage\app\files\maintenance_mode.json', $finalStr);
  1325. $actions = DB::getQueryLog();
  1326. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1327. $idac = $this->functionsController->registerActivity(
  1328. $info['linea'],
  1329. 'S002V01M01ADSI',
  1330. 'S002V01F05MOMA',
  1331. 'S002V01P01AMMA',
  1332. 'Registro',
  1333. "El usuario $name (" . $usr->USUA_IDUS . ") activó el modo mantenimiento",
  1334. $idUser,
  1335. $nowStr
  1336. );
  1337. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $info['linea']);
  1338. return $this->responseController->makeResponse(false, 'EXITO.');
  1339. }
  1340. public function deactivateMaintenanceMode(Request $request){
  1341. DB::enableQueryLog();
  1342. $validator = Validator::make($request->all(), [
  1343. 'id_user' => 'required|string',
  1344. 'linea' => 'required|integer',
  1345. 'justificacion' => 'required|string',
  1346. ]);
  1347. if($validator->fails()){
  1348. return $this->responseController->makeResponse(
  1349. true,
  1350. "Se encontraron uno o más errores.",
  1351. $this->responseController->makeErrors(
  1352. $validator->errors()->messages()
  1353. ),
  1354. 401
  1355. );
  1356. }
  1357. $info = $request->all();
  1358. $idUser = $this->encryptionController->decrypt($info['id_user']);
  1359. if(!$idUser){
  1360. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1361. }
  1362. $usr = DB::table('S002V01TUSUA')->where([
  1363. ['USUA_IDUS', '=', $idUser],
  1364. ['USUA_NULI', '=', $info['linea']],
  1365. ])->first();
  1366. if(is_null($usr)){
  1367. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1368. }
  1369. $maintenanceModeExists = file_exists('C:\inetpub\wwwroot\sam\storage\app\files\maintenance_mode.json');
  1370. if(!$maintenanceModeExists){
  1371. return $this->responseController->makeResponse(true, 'El archivo de mantenimiento no fue encontrado.', [], 500);
  1372. }
  1373. $maintenanceStr = file_get_contents('C:\inetpub\wwwroot\sam\storage\app\files\maintenance_mode.json');
  1374. $maintenanceArr = json_decode($maintenanceStr, true);
  1375. if(!$maintenanceArr['activated']){
  1376. return $this->responseController->makeResponse(true, 'El modo mantenimiento ya se encuentra desactivado', [], 401);
  1377. }
  1378. $maintenanceDB = DB::table('S002V01THMSA')->where([
  1379. ['HMSA_IDAC', '=', $maintenanceArr['last_activation_id']],
  1380. ['HMSA_NULI', '=', $info['linea']],
  1381. ])->first();
  1382. if(!is_null($maintenanceDB->HMSA_USDE)){
  1383. $maintenanceArr['activated'] = false;
  1384. $finalStr = json_encode($maintenanceArr);
  1385. file_put_contents('C:\inetpub\wwwroot\sam\storage\app\files\maintenance_mode.json', $finalStr);
  1386. return $this->responseController->makeResponse(true, 'El modo mantenimiento ya fue desactivado en la base de datos.', [], 500);
  1387. }
  1388. $now = $this->functionsController->now();
  1389. $nowStr = $now->toDateTimeString();
  1390. DB::table('S002V01THMSA')->where([
  1391. ['HMSA_IDAC', '=', $maintenanceArr['last_activation_id']],
  1392. ['HMSA_NULI', '=', $info['linea']],
  1393. ])->update([
  1394. 'HMSA_USDE' => $idUser,
  1395. 'HMSA_FEDE' => $nowStr,
  1396. 'HMSA_JUDE' => $info['justificacion']
  1397. ]);
  1398. $maintenanceArr['activated'] = false;
  1399. $finalStr = json_encode($maintenanceArr);
  1400. file_put_contents('C:\inetpub\wwwroot\sam\storage\app\files\maintenance_mode.json', $finalStr);
  1401. $actions = DB::getQueryLog();
  1402. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1403. $idac = $this->functionsController->registerActivity(
  1404. $info['linea'],
  1405. 'S002V01M01ADSI',
  1406. 'S002V01F05MOMA',
  1407. 'S002V01P01AMMA',
  1408. 'Actualización',
  1409. "El usuario $name (" . $usr->USUA_IDUS . ") desactivó el modo mantenimiento",
  1410. $idUser,
  1411. $nowStr
  1412. );
  1413. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $info['linea']);
  1414. return $this->responseController->makeResponse(false, 'EXITO.');
  1415. }
  1416. public function getMaintenanceHistory($idUser, $line){
  1417. DB::enableQueryLog();
  1418. $idUser = $this->encryptionController->decrypt($idUser);
  1419. if(!$idUser){
  1420. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  1421. }
  1422. $usr = DB::table('S002V01TUSUA')->where([
  1423. ['USUA_IDUS', '=', $idUser],
  1424. ['USUA_NULI', '=', $line]
  1425. ])->first();
  1426. if(is_null($usr)){
  1427. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1428. }
  1429. $history = DB::table('S002V01THMSA')->where('HMSA_NULI', '=', $line)->get()->all();
  1430. $historyF = array();
  1431. foreach($history as $val){
  1432. $activationDate = new Carbon($val->HMSA_FEAC, 'America/Mexico_city');
  1433. $deactivationDate = new Carbon($val->HMSA_FEDE, 'America/Mexico_city');
  1434. $diffDays = $deactivationDate->diffInDays($activationDate);
  1435. $activationDate->addDays($diffDays);
  1436. $diffHours = $deactivationDate->diffInHours($activationDate);
  1437. $activationDate->addHours($diffHours);
  1438. $diffMinutes = $deactivationDate->diffInMinutes($activationDate);
  1439. $activationDate->addMinutes($diffMinutes);
  1440. $diffSeconds = $deactivationDate->diffInSeconds($activationDate);
  1441. $duration = "";
  1442. if($diffDays > 0) $duration .= "$diffDays días, ";
  1443. if($diffHours > 0) $duration .= "$diffHours horas, ";
  1444. if($diffMinutes > 0) $duration .= "$diffMinutes minutos, ";
  1445. if($diffSeconds > 0) $duration .= "$diffSeconds segundos, ";
  1446. $duration = substr($duration, 0, -2);
  1447. $usac = DB::table('S002V01TUSUA')->select([
  1448. 'USUA_NOMB', 'USUA_APPA', 'USUA_APMA'
  1449. ])->where([
  1450. ['USUA_NULI', '=', $line],
  1451. ['USUA_IDUS', '=', $val->HMSA_USAC]
  1452. ])->first();
  1453. $nomUSAC = $this->functionsController->joinName($usac->USUA_NOMB, $usac->USUA_APPA, $usac->USUA_APMA);
  1454. $usde = DB::table('S002V01TUSUA')->select([
  1455. 'USUA_NOMB', 'USUA_APPA', 'USUA_APMA'
  1456. ])->where([
  1457. ['USUA_NULI', '=', $line],
  1458. ['USUA_IDUS', '=', $val->HMSA_USDE]
  1459. ])->first();
  1460. $nomUSDE = $this->functionsController->joinName($usde->USUA_NOMB, $usde->USUA_APPA, $usde->USUA_APMA);
  1461. $historyF[] = [
  1462. "IDACTIVACION" => $val->HMSA_IDAC,
  1463. "LINEA" => $val->HMSA_NULI,
  1464. "USUARIOACTIVO" => $nomUSAC . " (" . $val->HMSA_USAC . ")",
  1465. "FECHAACTIVACION" => $val->HMSA_FEAC,
  1466. "MOTIVOS" => $val->HMSA_MOAC,
  1467. "USUARIODESACTIVO" => $nomUSDE . " (" . $val->HMSA_USDE . ")",
  1468. "FECHADESACTIVACION" => $val->HMSA_FEDE,
  1469. "JUSTIFICACION" => $val->HMSA_JUDE,
  1470. "DURACION" => $duration
  1471. ];
  1472. }
  1473. $now = $this->functionsController->now();
  1474. $actions = DB::getQueryLog();
  1475. $nowStr = $now->toDateTimeString();
  1476. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1477. $idac = $this->functionsController->registerActivity(
  1478. $line,
  1479. 'S002V01M01ADSI',
  1480. 'S002V01F05MOMA',
  1481. 'S002V01P02HIMA',
  1482. 'Consulta',
  1483. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el historial de mantenimientos.",
  1484. $idUser,
  1485. $nowStr
  1486. );
  1487. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1488. return $this->responseController->makeResponse(false, 'EXITO', $historyF);
  1489. }
  1490. public function getMaintenanceStatus($idUser, $line){
  1491. DB::enableQueryLog();
  1492. $idUser = $this->encryptionController->decrypt($idUser);
  1493. if(!$idUser){
  1494. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  1495. }
  1496. $usr = DB::table('S002V01TUSUA')->where([
  1497. ['USUA_IDUS', '=', $idUser],
  1498. ['USUA_NULI', '=', $line]
  1499. ])->first();
  1500. if(is_null($usr)){
  1501. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1502. }
  1503. $maintenanceModeExists = file_exists('C:\inetpub\wwwroot\sam\storage\app\files\maintenance_mode.json');
  1504. if(!$maintenanceModeExists){
  1505. return $this->responseController->makeResponse(true, 'El archivo de mantenimiento no fue encontrado.', [], 500);
  1506. }
  1507. $maintenanceStr = file_get_contents('C:\inetpub\wwwroot\sam\storage\app\files\maintenance_mode.json');
  1508. $maintenanceArr = json_decode($maintenanceStr, true);
  1509. $now = $this->functionsController->now();
  1510. $nowStr = $now->toDateTimeString();
  1511. $actions = DB::getQueryLog();
  1512. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1513. $idac = $this->functionsController->registerActivity(
  1514. $line,
  1515. 'S002V01M01ADSI',
  1516. 'S002V01F05MOMA',
  1517. 'S002V01P02HIMA',
  1518. 'Consulta',
  1519. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el estado actual del modo mantenimiento.",
  1520. $idUser,
  1521. $nowStr
  1522. );
  1523. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1524. return $this->responseController->makeResponse(false, 'EXITO', ['activated' => $maintenanceArr['activated']]);
  1525. }
  1526. public function getEmailServers($idUser, $line){
  1527. DB::enableQueryLog();
  1528. $idUser = $this->encryptionController->decrypt($idUser);
  1529. if(!$idUser){
  1530. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  1531. }
  1532. $usr = DB::table('S002V01TUSUA')->where([
  1533. ['USUA_IDUS', '=', $idUser],
  1534. ['USUA_NULI', '=', $line]
  1535. ])->first();
  1536. if(is_null($usr)){
  1537. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1538. }
  1539. $emailServers = DB::table('S002V01TSCEL')->where('SCEL_NULI', '=', $line)->orderBy('SCEL_PRIO')->get()->all();
  1540. $serversArr = [];
  1541. foreach($emailServers as $server){
  1542. $usal = DB::table('S002V01TUSUA')->where([
  1543. ['USUA_IDUS', '=', $server->SCEL_USAL],
  1544. ['USUA_NULI', '=', $line],
  1545. ])->first();
  1546. $usalName = $this->functionsController->joinName($usal->USUA_NOMB, $usal->USUA_APPA, $usal->USUA_APMA);
  1547. $usmo = DB::table('S002V01TUSUA')->where([
  1548. ['USUA_IDUS', '=', $server->SCEL_USMO],
  1549. ['USUA_NULI', '=', $line],
  1550. ])->first();
  1551. $usmoName = is_null($usmo) ? '-' : $this->functionsController->joinName($usmo->USUA_NOMB, $usmo->USUA_APPA, $usmo->USUA_APMA);
  1552. $serversArr[] = [
  1553. 'USUARIO' => $server->SCEL_USUA,
  1554. 'HOST' => $server->SCEL_HOST,
  1555. 'PRIORIDAD' => $server->SCEL_PRIO,
  1556. 'USRREG' => "$usalName (" . $server->SCEL_USAL . ")",
  1557. 'FECREG' => $server->SCEL_FEAL,
  1558. 'USRMOD' => is_null($usmo) ? $usmoName : "$usalName (" . $server->SCEL_USMO . ")",
  1559. 'FECMOD' => is_null($server->SCEL_FEMO) ? '-' : $server->SCEL_FEMO,
  1560. 'ESTADO' => $server->SCEL_ESCO,
  1561. ];
  1562. }
  1563. $now = $this->functionsController->now();
  1564. $nowStr = $now->toDateTimeString();
  1565. $actions = DB::getQueryLog();
  1566. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1567. $idac = $this->functionsController->registerActivity(
  1568. $line,
  1569. 'S002V01M01ADSI',
  1570. 'S002V01F07CCEL',
  1571. 'S002V01P01CNSC',
  1572. 'Consulta',
  1573. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los servidores de correo electrónico registrados.",
  1574. $idUser,
  1575. $nowStr
  1576. );
  1577. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1578. return $this->responseController->makeResponse(false, 'EXITO', $serversArr);
  1579. }
  1580. public function registerEmailServer(Request $request){
  1581. DB::enableQueryLog();
  1582. $validator = Validator::make($request->all(), [
  1583. 'id_user' => 'required|string',
  1584. 'linea' => 'required|integer',
  1585. 'user' => 'required|string|max:50',
  1586. 'host' => 'required|string|max:50',
  1587. 'priority' => 'required|integer|between:1,100',
  1588. 'smtpsec' => 'string|max:50',
  1589. 'port' => 'required|string|max:10',
  1590. 'password' => 'required|string|max:2000',
  1591. 'timeout' => 'required|string|max:3',
  1592. 'smtpaut' => 'required|string|max:2',
  1593. 'portals' => 'required|json',
  1594. ]);
  1595. if($validator->fails()){
  1596. return $this->responseController->makeResponse(
  1597. true,
  1598. "Se encontraron uno o más errores.",
  1599. $this->responseController->makeErrors(
  1600. $validator->errors()->messages()
  1601. ),
  1602. 401
  1603. );
  1604. }
  1605. $form = $request->all();
  1606. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1607. if(!$idUser){
  1608. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1609. }
  1610. $usr = DB::table('S002V01TUSUA')->where([
  1611. ['USUA_IDUS', '=', $idUser],
  1612. ['USUA_NULI', '=', $form['linea']],
  1613. ])->first();
  1614. if(is_null($usr)){
  1615. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1616. }
  1617. $now = $this->functionsController->now();
  1618. $nowStr = $now->toDateTimeString();
  1619. $smtpsec = isset($form['smtpsec']) ? $form['smtpsec'] : null;
  1620. DB::table('S002V01TSCEL')->insert([
  1621. 'SCEL_NULI' => $form['linea'],
  1622. 'SCEL_USUA' => $form['user'],
  1623. 'SCEL_HOST' => $form['host'],
  1624. 'SCEL_PRIO' => $form['priority'],
  1625. 'SCEL_SESM' => $smtpsec,
  1626. 'SCEL_PUER' => $form['port'],
  1627. 'SCEL_CONT' => $form['password'],
  1628. 'SCEL_TIES' => $form['timeout'],
  1629. 'SCEL_AUSM' => $form['smtpaut'],
  1630. 'SCEL_ADPO' => $form['portals'],
  1631. 'SCEL_USAL' => $idUser,
  1632. 'SCEL_FEAL' => $nowStr,
  1633. ]);
  1634. $actions = DB::getQueryLog();
  1635. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1636. $idac = $this->functionsController->registerActivity(
  1637. $form['linea'],
  1638. 'S002V01M01ADSI',
  1639. 'S002V01F07CCEL',
  1640. 'S002V01P01CNSC',
  1641. 'Registro',
  1642. "El usuario $name (" . $usr->USUA_IDUS . ") registró el servidor $form[host]",
  1643. $idUser,
  1644. $nowStr
  1645. );
  1646. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1647. return $this->responseController->makeResponse(false, 'EXITO.');
  1648. }
  1649. public function getEmailServer($host, $user, $idUser, $line){
  1650. DB::enableQueryLog();
  1651. $idUser = $this->encryptionController->decrypt($idUser);
  1652. if(!$idUser){
  1653. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  1654. }
  1655. $usr = DB::table('S002V01TUSUA')->where([
  1656. ['USUA_IDUS', '=', $idUser],
  1657. ['USUA_NULI', '=', $line]
  1658. ])->first();
  1659. if(is_null($usr)){
  1660. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1661. }
  1662. $host = $this->encryptionController->decrypt($host);
  1663. if(!$host){
  1664. return $this->responseController->makeResponse(true, 'El HOST del servidor no está encriptado correctamente.', [], 400);
  1665. }
  1666. $user = $this->encryptionController->decrypt($user);
  1667. if(!$user){
  1668. return $this->responseController->makeResponse(true, 'El USUARIO del servidor no está encriptado correctamente.', [], 400);
  1669. }
  1670. $server = DB::table('S002V01TSCEL')->select([
  1671. 'SCEL_USUA AS USUARIO',
  1672. 'SCEL_HOST AS HOST',
  1673. 'SCEL_PRIO AS PRIORIDAD',
  1674. 'SCEL_SESM AS SEGSMTP',
  1675. 'SCEL_PUER AS PUERTO',
  1676. 'SCEL_CONT AS CONTRASENA',
  1677. 'SCEL_TIES AS TIEMPOESPERA',
  1678. 'SCEL_AUSM AS AUTSMTP',
  1679. 'SCEL_ADPO AS PORTALES'
  1680. ])->where([
  1681. ['SCEL_NULI', '=', $line],
  1682. ['SCEL_USUA', '=', $user],
  1683. ['SCEL_HOST', '=', $host],
  1684. ])->first();
  1685. $now = $this->functionsController->now();
  1686. $nowStr = $now->toDateTimeString();
  1687. $actions = DB::getQueryLog();
  1688. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1689. $idac = $this->functionsController->registerActivity(
  1690. $line,
  1691. 'S002V01M01ADSI',
  1692. 'S002V01F07CCEL',
  1693. 'S002V01P01CNSC',
  1694. 'Consulta',
  1695. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el servidor $host.",
  1696. $idUser,
  1697. $nowStr
  1698. );
  1699. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1700. return $this->responseController->makeResponse(false, 'EXITO', $server);
  1701. }
  1702. public function updateEmailServer(Request $request){
  1703. DB::enableQueryLog();
  1704. $validator = Validator::make($request->all(), [
  1705. 'id_user' => 'required|string',
  1706. 'linea' => 'required|integer',
  1707. 'user' => 'required|string|max:50',
  1708. 'host' => 'required|string|max:50',
  1709. 'priority' => 'required|integer|between:1,100',
  1710. 'smtpsec' => 'string|max:50',
  1711. 'port' => 'required|string|max:10',
  1712. 'password' => 'required|string|max:2000',
  1713. 'timeout' => 'required|string|max:3',
  1714. 'smtpaut' => 'required|string|max:2',
  1715. 'portals' => 'required|json',
  1716. ]);
  1717. if($validator->fails()){
  1718. return $this->responseController->makeResponse(
  1719. true,
  1720. "Se encontraron uno o más errores.",
  1721. $this->responseController->makeErrors(
  1722. $validator->errors()->messages()
  1723. ),
  1724. 401
  1725. );
  1726. }
  1727. $form = $request->all();
  1728. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1729. if(!$idUser){
  1730. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1731. }
  1732. $usr = DB::table('S002V01TUSUA')->where([
  1733. ['USUA_IDUS', '=', $idUser],
  1734. ['USUA_NULI', '=', $form['linea']]
  1735. ])->first();
  1736. if(is_null($usr)){
  1737. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1738. }
  1739. $server = DB::table('S002V01TSCEL')->where([
  1740. ['SCEL_NULI', '=', $form['linea']],
  1741. ['SCEL_USUA', '=', $form['user']],
  1742. ['SCEL_HOST', '=', $form['host']],
  1743. ])->first();
  1744. if(is_null($server)){
  1745. return $this->responseController->makeResponse(true, 'El servidor que desea modificar no existe.', [], 404);
  1746. }
  1747. $now = $this->functionsController->now();
  1748. $nowStr = $now->toDateTimeString();
  1749. $smtpsec = isset($form['smtpsec']) ? $form['smtpsec'] : null;
  1750. DB::table('S002V01TSCEL')->where([
  1751. ['SCEL_NULI', '=', $form['linea']],
  1752. ['SCEL_USUA', '=', $form['user']],
  1753. ['SCEL_HOST', '=', $form['host']],
  1754. ])->update([
  1755. 'SCEL_PRIO' => $form['priority'],
  1756. 'SCEL_SESM' => $smtpsec,
  1757. 'SCEL_PUER' => $form['port'],
  1758. 'SCEL_CONT' => $form['password'],
  1759. 'SCEL_TIES' => $form['timeout'],
  1760. 'SCEL_AUSM' => $form['smtpaut'],
  1761. 'SCEL_ADPO' => $form['portals'],
  1762. 'SCEL_USMO' => $idUser,
  1763. 'SCEL_FEMO' => $nowStr
  1764. ]);
  1765. $actions = DB::getQueryLog();
  1766. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1767. $idac = $this->functionsController->registerActivity(
  1768. $form['linea'],
  1769. 'S002V01M01ADSI',
  1770. 'S002V01F07CCEL',
  1771. 'S002V01P01CNSC',
  1772. 'Actualización',
  1773. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó el servidor $form[host]",
  1774. $idUser,
  1775. $nowStr
  1776. );
  1777. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1778. return $this->responseController->makeResponse(false, 'EXITO.');
  1779. }
  1780. public function deleteEmailServer(Request $request){
  1781. DB::enableQueryLog();
  1782. $validator = Validator::make($request->all(), [
  1783. 'id_user' => 'required|string',
  1784. 'linea' => 'required|integer',
  1785. 'user' => 'required|string|max:50',
  1786. 'host' => 'required|string|max:50'
  1787. ]);
  1788. if($validator->fails()){
  1789. return $this->responseController->makeResponse(
  1790. true,
  1791. "Se encontraron uno o más errores.",
  1792. $this->responseController->makeErrors(
  1793. $validator->errors()->messages()
  1794. ),
  1795. 401
  1796. );
  1797. }
  1798. $form = $request->all();
  1799. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1800. if(!$idUser){
  1801. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1802. }
  1803. $usr = DB::table('S002V01TUSUA')->where([
  1804. ['USUA_IDUS', '=', $idUser],
  1805. ['USUA_NULI', '=', $form['linea']]
  1806. ])->first();
  1807. if(is_null($usr)){
  1808. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1809. }
  1810. $server = DB::table('S002V01TSCEL')->where([
  1811. ['SCEL_NULI', '=', $form['linea']],
  1812. ['SCEL_USUA', '=', $form['user']],
  1813. ['SCEL_HOST', '=', $form['host']],
  1814. ])->first();
  1815. if(is_null($server)){
  1816. return $this->responseController->makeResponse(true, 'El servidor que desea eliminar no existe.', [], 404);
  1817. }
  1818. $now = $this->functionsController->now();
  1819. $nowStr = $now->toDateTimeString();
  1820. DB::table('S002V01TSCEL')->where([
  1821. ['SCEL_NULI', '=', $form['linea']],
  1822. ['SCEL_USUA', '=', $form['user']],
  1823. ['SCEL_HOST', '=', $form['host']],
  1824. ])->update([
  1825. 'SCEL_ESCO' => 'Eliminado',
  1826. 'SCEL_USMO' => $idUser,
  1827. 'SCEL_FEMO' => $nowStr
  1828. ]);
  1829. $actions = DB::getQueryLog();
  1830. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1831. $idac = $this->functionsController->registerActivity(
  1832. $form['linea'],
  1833. 'S002V01M01ADSI',
  1834. 'S002V01F07CCEL',
  1835. 'S002V01P01CNSC',
  1836. 'Eliminación',
  1837. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó el servidor $form[host]",
  1838. $idUser,
  1839. $nowStr
  1840. );
  1841. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1842. return $this->responseController->makeResponse(false, 'EXITO.');
  1843. }
  1844. public function deactivateEmailServer(Request $request){
  1845. DB::enableQueryLog();
  1846. $validator = Validator::make($request->all(), [
  1847. 'id_user' => 'required|string',
  1848. 'linea' => 'required|integer',
  1849. 'user' => 'required|string|max:50',
  1850. 'host' => 'required|string|max:50',
  1851. 'state' => 'required|string|in:Activo,Inactivo',
  1852. ]);
  1853. if($validator->fails()){
  1854. return $this->responseController->makeResponse(
  1855. true,
  1856. "Se encontraron uno o más errores.",
  1857. $this->responseController->makeErrors(
  1858. $validator->errors()->messages()
  1859. ),
  1860. 401
  1861. );
  1862. }
  1863. $form = $request->all();
  1864. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1865. if(!$idUser){
  1866. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1867. }
  1868. $usr = DB::table('S002V01TUSUA')->where([
  1869. ['USUA_IDUS', '=', $idUser],
  1870. ['USUA_NULI', '=', $form['linea']]
  1871. ])->first();
  1872. if(is_null($usr)){
  1873. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1874. }
  1875. $server = DB::table('S002V01TSCEL')->where([
  1876. ['SCEL_NULI', '=', $form['linea']],
  1877. ['SCEL_USUA', '=', $form['user']],
  1878. ['SCEL_HOST', '=', $form['host']],
  1879. ])->first();
  1880. if(is_null($server)){
  1881. return $this->responseController->makeResponse(true, 'El servidor que desea modificar no existe.', [], 404);
  1882. }
  1883. $now = $this->functionsController->now();
  1884. $nowStr = $now->toDateTimeString();
  1885. DB::table('S002V01TSCEL')->where([
  1886. ['SCEL_NULI', '=', $form['linea']],
  1887. ['SCEL_USUA', '=', $form['user']],
  1888. ['SCEL_HOST', '=', $form['host']],
  1889. ])->update([
  1890. 'SCEL_ESCO' => $form['state'],
  1891. 'SCEL_USMO' => $idUser,
  1892. 'SCEL_FEMO' => $nowStr
  1893. ]);
  1894. $actions = DB::getQueryLog();
  1895. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1896. $idac = $this->functionsController->registerActivity(
  1897. $form['linea'],
  1898. 'S002V01M01ADSI',
  1899. 'S002V01F07CCEL',
  1900. 'S002V01P01CNSC',
  1901. 'Actualización',
  1902. "El usuario $name (" . $usr->USUA_IDUS . ") cambió el estado del servidor $form[host]",
  1903. $idUser,
  1904. $nowStr
  1905. );
  1906. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1907. return $this->responseController->makeResponse(false, 'EXITO.');
  1908. }
  1909. public function registerEmailTemplate(Request $request){
  1910. DB::enableQueryLog();
  1911. $validator = Validator::make($request->all(), [
  1912. 'id_user' => 'required|string',
  1913. 'linea' => 'required|integer',
  1914. 'module' => 'required|string|max:25',
  1915. 'submodule' => 'string|max:25',
  1916. 'function' => 'required|string|max:25',
  1917. 'screen' => 'required|string|max:25',
  1918. 'subject' => 'required|string|max:50',
  1919. 'body' => 'required|string',
  1920. ]);
  1921. if($validator->fails()){
  1922. return $this->responseController->makeResponse(
  1923. true,
  1924. "Se encontraron uno o más errores.",
  1925. $this->responseController->makeErrors(
  1926. $validator->errors()->messages()
  1927. ),
  1928. 401
  1929. );
  1930. }
  1931. $form = $request->all();
  1932. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1933. if(!$idUser){
  1934. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1935. }
  1936. $usr = DB::table('S002V01TUSUA')->where([
  1937. ['USUA_IDUS', '=', $idUser],
  1938. ['USUA_NULI', '=', $form['linea']]
  1939. ])->first();
  1940. if(is_null($usr)){
  1941. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1942. }
  1943. $module = DB::table('S002V01TMODU')->where([
  1944. ['MODU_IDMO', '=', $form['module']],
  1945. ['MODU_NULI', '=', $form['linea']]
  1946. ])->first();
  1947. if(is_null($module)){
  1948. return $this->responseController->makeResponse(true, 'El módulo seleccionado no existe.', [], 404);
  1949. }
  1950. $hasSubmodule = isset($form['submodule']);
  1951. if($hasSubmodule){
  1952. $submodule = DB::table('S002V01TSUBM')->where([
  1953. ['SUBM_IDSM', '=', $form['submodule']],
  1954. ['SUBM_IDMO', '=', $form['module']],
  1955. ['SUBM_NULI', '=', $form['linea']],
  1956. ])->first();
  1957. if(is_null($submodule)){
  1958. return $this->responseController->makeResponse(true, 'El submódulo seleccionado no existe.', [], 404);
  1959. }
  1960. }
  1961. $submodule = $hasSubmodule ? $form['submodule'] : null;
  1962. $function = DB::table('S002V01TFUNC')->where([
  1963. ['FUNC_NULI', '=', $form['linea']],
  1964. ['FUNC_IDFU', '=', $form['function']],
  1965. ['FUNC_IDMO', '=', $form['module']],
  1966. ['FUNC_IDSM', '=', $submodule],
  1967. ])->first();
  1968. if(is_null($function)){
  1969. return $this->responseController->makeResponse(true, 'La función seleccionada no existe.', [], 404);
  1970. }
  1971. $screen = DB::table('S002V01TPANT')->where([
  1972. ['PANT_NULI', '=', $form['linea']],
  1973. ['PANT_IDPA', '=', $form['screen']],
  1974. ['PANT_IDFU', '=', $form['function']],
  1975. ])->first();
  1976. if(is_null($screen)){
  1977. return $this->responseController->makeResponse(true, 'La pantalla seleccionada no existe.', [], 404);
  1978. }
  1979. $now = $this->functionsController->now();
  1980. $nowStr = $now->toDateTimeString();
  1981. $idPlan = DB::table('S002V01TPCEL')->insertGetId([
  1982. 'PCEL_NULI' => $form['linea'],
  1983. 'PCEL_IDMO' => $form['module'],
  1984. 'PCEL_IDSM' => $submodule,
  1985. 'PCEL_IDFU' => $form['function'],
  1986. 'PCEL_IDPA' => $form['screen'],
  1987. 'PCEL_ASCO' => $form['subject'],
  1988. 'PCEL_CUCO' => $form['body'],
  1989. 'PCEL_USRE' => $idUser,
  1990. 'PCEL_FERE' => $nowStr,
  1991. ]);
  1992. $actions = DB::getQueryLog();
  1993. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1994. $idac = $this->functionsController->registerActivity(
  1995. $form['linea'],
  1996. 'S002V01M01ADSI',
  1997. 'S002V01F08CPCE',
  1998. 'S002V01P02NPEP',
  1999. 'Registro',
  2000. "El usuario $name (" . $usr->USUA_IDUS . ") registró la plantilla $form[subject] ($idPlan)",
  2001. $idUser,
  2002. $nowStr
  2003. );
  2004. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2005. return $this->responseController->makeResponse(false, 'EXITO.');
  2006. }
  2007. public function getEmailTemplates($idUser, $line){
  2008. DB::enableQueryLog();
  2009. $idUser = $this->encryptionController->decrypt($idUser);
  2010. if(!$idUser){
  2011. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  2012. }
  2013. $usr = DB::table('S002V01TUSUA')->where([
  2014. ['USUA_IDUS', '=', $idUser],
  2015. ['USUA_NULI', '=', $line]
  2016. ])->first();
  2017. if(is_null($usr)){
  2018. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2019. }
  2020. $emailTemplates = DB::table('S002V01TPCEL')->select([
  2021. 'PCEL_IDPL AS IDPLANTILLA',
  2022. 'PCEL_NULI AS LINEA',
  2023. 'MODU_NOMO AS NOMBREMODULO',
  2024. 'SUBM_NOMB AS NOMBRESUBMODULO',
  2025. 'FUNC_NOMB AS NOMBREFUNCION',
  2026. 'PANT_NOMB AS NOMBREPANTALLA',
  2027. 'PCEL_ASCO AS ASUNTO',
  2028. 'PCEL_CUCO AS CUERPO',
  2029. 'PCEL_ESTA AS ESTADO',
  2030. 'PCEL_USRE AS USUARIOREGISTRA',
  2031. 'PCEL_FERE AS FECHAREGISTRO',
  2032. 'PCEL_USMO AS USUARIOMODIFICA',
  2033. 'PCEL_FEMO AS FECHAMODIFICACION'
  2034. ])->join('S002V01TMODU', 'MODU_IDMO', '=', 'PCEL_IDMO')
  2035. ->leftJoin('S002V01TSUBM', 'SUBM_IDSM', '=', 'PCEL_IDSM')
  2036. ->join('S002V01TFUNC', 'FUNC_IDFU', '=', 'PCEL_IDFU')
  2037. ->join('S002V01TPANT', 'PANT_IDPA', '=', 'PCEL_IDPA')->where([
  2038. ['PCEL_NULI', '=', $line]
  2039. ])->orderBy('PCEL_IDPL', 'asc')->get()->all();
  2040. $emailTemplatesF = [];
  2041. foreach($emailTemplates as $template){
  2042. $usrTemplate = DB::table('S002V01TUSUA')->where([
  2043. ['USUA_NULI', '=', $line],
  2044. ['USUA_IDUS', '=', $template->USUARIOREGISTRA],
  2045. ])->first();
  2046. $idus = $template->USUARIOREGISTRA;
  2047. $template->USUARIOREGISTRA = $this->functionsController->joinName(
  2048. $usrTemplate->USUA_NOMB,
  2049. $usrTemplate->USUA_APPA,
  2050. $usrTemplate->USUA_APMA
  2051. ) . " ($idus)";
  2052. if(!is_null($template->USUARIOMODIFICA)){
  2053. $usrTemplate = DB::table('S002V01TUSUA')->where([
  2054. ['USUA_NULI', '=', $line],
  2055. ['USUA_IDUS', '=', $template->USUARIOMODIFICA],
  2056. ])->first();
  2057. $idus = $template->USUARIOMODIFICA;
  2058. $template->USUARIOMODIFICA = $this->functionsController->joinName(
  2059. $usrTemplate->USUA_NOMB,
  2060. $usrTemplate->USUA_APPA,
  2061. $usrTemplate->USUA_APMA
  2062. ) . " ($idus)";
  2063. }
  2064. $emailTemplatesF[] = $template;
  2065. }
  2066. $now = $this->functionsController->now();
  2067. $nowStr = $now->toDateTimeString();
  2068. $actions = DB::getQueryLog();
  2069. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2070. $idac = $this->functionsController->registerActivity(
  2071. $line,
  2072. 'S002V01M01ADSI',
  2073. 'S002V01F09MACS',
  2074. 'S002V01P01HIPL',
  2075. 'Consulta',
  2076. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las plantillas de correo electrónico registradas.",
  2077. $idUser,
  2078. $nowStr
  2079. );
  2080. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2081. return $this->responseController->makeResponse(false, 'EXITO', $emailTemplatesF);
  2082. }
  2083. public function getEmailTemplate($id, $idUser, $line){
  2084. DB::enableQueryLog();
  2085. $idUser = $this->encryptionController->decrypt($idUser);
  2086. if(!$idUser){
  2087. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  2088. }
  2089. $usr = DB::table('S002V01TUSUA')->where([
  2090. ['USUA_IDUS', '=', $idUser],
  2091. ['USUA_NULI', '=', $line]
  2092. ])->first();
  2093. if(is_null($usr)){
  2094. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  2095. }
  2096. $id = $this->encryptionController->decrypt($id);
  2097. if(!$id){
  2098. return $this->responseController->makeResponse(true, 'El ID de la plantilla consultada no está encriptado correctamente.', [], 400);
  2099. }
  2100. $emailTemplate = DB::table('S002V01TPCEL')->select([
  2101. 'PCEL_IDPL AS IDPLANTILLA',
  2102. 'PCEL_IDMO AS MODULO',
  2103. 'PCEL_IDSM AS SUBMODULO',
  2104. 'PCEL_IDFU AS FUNCION',
  2105. 'PCEL_IDPA AS PANTALLA',
  2106. 'PCEL_ASCO AS ASUNTO',
  2107. 'PCEL_CUCO AS CUERPO'
  2108. ])->where([
  2109. ['PCEL_IDPL', '=', $id],
  2110. ['PCEL_NULI', '=', $line],
  2111. ])->first();
  2112. if(is_null($emailTemplate)){
  2113. return $this->responseController->makeResponse(true, 'La plantilla solicitada no existe.', [], 404);
  2114. }
  2115. $now = $this->functionsController->now();
  2116. $nowStr = $now->toDateTimeString();
  2117. $actions = DB::getQueryLog();
  2118. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2119. $idac = $this->functionsController->registerActivity(
  2120. $line,
  2121. 'S002V01M01ADSI',
  2122. 'S002V01F09MACS',
  2123. 'S002V01P01HIPL',
  2124. 'Consulta',
  2125. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la plantilla " . $emailTemplate->ASUNTO . " ($id).",
  2126. $idUser,
  2127. $nowStr
  2128. );
  2129. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2130. return $this->responseController->makeResponse(false, 'EXITO', $emailTemplate);
  2131. }
  2132. public function updateEmailTemplate(Request $request){
  2133. DB::enableQueryLog();
  2134. $validator = Validator::make($request->all(), [
  2135. 'id_user' => 'required|string',
  2136. 'linea' => 'required|integer',
  2137. 'id_template' => 'required|string',
  2138. 'module' => 'required|string|max:25',
  2139. 'submodule' => 'string|max:25',
  2140. 'function' => 'required|string|max:25',
  2141. 'screen' => 'required|string|max:25',
  2142. 'subject' => 'required|string|max:50',
  2143. 'body' => 'required|string',
  2144. ]);
  2145. if($validator->fails()){
  2146. return $this->responseController->makeResponse(
  2147. true,
  2148. "Se encontraron uno o más errores.",
  2149. $this->responseController->makeErrors(
  2150. $validator->errors()->messages()
  2151. ),
  2152. 401
  2153. );
  2154. }
  2155. $form = $request->all();
  2156. $idUser = $this->encryptionController->decrypt($form['id_user']);
  2157. if(!$idUser){
  2158. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  2159. }
  2160. $usr = DB::table('S002V01TUSUA')->where([
  2161. ['USUA_IDUS', '=', $idUser],
  2162. ['USUA_NULI', '=', $form['linea']]
  2163. ])->first();
  2164. if(is_null($usr)){
  2165. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  2166. }
  2167. $idTemplate = $this->encryptionController->decrypt($form['id_template']);
  2168. if(!$idTemplate){
  2169. return $this->responseController->makeResponse(true, 'El ID de la plantilla no fue encriptado correctamente.', [], 400);
  2170. }
  2171. $now = $this->functionsController->now();
  2172. $nowStr = $now->toDateTimeString();
  2173. $submodule = isset($form['submodule']) ? $form['submodule'] : null;
  2174. DB::table('S002V01TPCEL')->where([
  2175. ['PCEL_NULI', '=', $form['linea']],
  2176. ['PCEL_IDPL', '=', $idTemplate]
  2177. ])->update([
  2178. 'PCEL_IDMO' => $form['module'],
  2179. 'PCEL_IDSM' => $submodule,
  2180. 'PCEL_IDFU' => $form['function'],
  2181. 'PCEL_IDPA' => $form['screen'],
  2182. 'PCEL_ASCO' => $form['subject'],
  2183. 'PCEL_CUCO' => $form['body'],
  2184. 'PCEL_USMO' => $idUser,
  2185. 'PCEL_FEMO' => $nowStr,
  2186. ]);
  2187. $actions = DB::getQueryLog();
  2188. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2189. $idac = $this->functionsController->registerActivity(
  2190. $form['linea'],
  2191. 'S002V01M01ADSI',
  2192. 'S002V01F08CPCE',
  2193. 'S002V01P02NPEP',
  2194. 'Actualización',
  2195. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó la plantilla $form[subject] ($idTemplate)",
  2196. $idUser,
  2197. $nowStr
  2198. );
  2199. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2200. return $this->responseController->makeResponse(false, 'EXITO.');
  2201. }
  2202. public function deleteEmailTemplate(Request $request){
  2203. DB::enableQueryLog();
  2204. $validator = Validator::make($request->all(), [
  2205. 'id_user' => 'required|string',
  2206. 'linea' => 'required|integer',
  2207. 'id_template' => 'required|string',
  2208. ]);
  2209. if($validator->fails()){
  2210. return $this->responseController->makeResponse(
  2211. true,
  2212. "Se encontraron uno o más errores.",
  2213. $this->responseController->makeErrors(
  2214. $validator->errors()->messages()
  2215. ),
  2216. 401
  2217. );
  2218. }
  2219. $form = $request->all();
  2220. $idUser = $this->encryptionController->decrypt($form['id_user']);
  2221. if(!$idUser){
  2222. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  2223. }
  2224. $usr = DB::table('S002V01TUSUA')->where([
  2225. ['USUA_IDUS', '=', $idUser],
  2226. ['USUA_NULI', '=', $form['linea']]
  2227. ])->first();
  2228. if(is_null($usr)){
  2229. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  2230. }
  2231. $idTemplate = $this->encryptionController->decrypt($form['id_template']);
  2232. if(!$idTemplate){
  2233. return $this->responseController->makeResponse(true, 'El ID de la plantilla no fue encriptado correctamente.', [], 400);
  2234. }
  2235. $template = DB::table('S002V01TPCEL')->where([
  2236. ['PCEL_IDPL', '=', $idTemplate],
  2237. ['PCEL_NULI', '=', $form['linea']]
  2238. ])->first();
  2239. if(is_null($template)){
  2240. return $this->responseController->makeResponse(true, 'La plantilla solicitada no existe.', [], 404);
  2241. }
  2242. $now = $this->functionsController->now();
  2243. $nowStr = $now->toDateTimeString();
  2244. DB::table('S002V01TPCEL')->where([
  2245. ['PCEL_IDPL', '=', $idTemplate],
  2246. ['PCEL_NULI', '=', $form['linea']]
  2247. ])->update([
  2248. 'PCEL_ESTA' => 'Eliminado',
  2249. 'PCEL_USMO' => $idUser,
  2250. 'PCEL_FEMO' => $nowStr,
  2251. ]);
  2252. $actions = DB::getQueryLog();
  2253. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2254. $idac = $this->functionsController->registerActivity(
  2255. $form['linea'],
  2256. 'S002V01M01ADSI',
  2257. 'S002V01F08CPCE',
  2258. 'S002V01P01HIPL',
  2259. 'Eliminación',
  2260. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó la plantilla ($idTemplate)",
  2261. $idUser,
  2262. $nowStr
  2263. );
  2264. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2265. return $this->responseController->makeResponse(false, 'EXITO.');
  2266. }
  2267. public function saveCatalogue(Request $request){
  2268. DB::enableQueryLog();
  2269. $validator = Validator::make($request->all(), [
  2270. 'id_user' => 'required|string',
  2271. 'linea' => 'required|integer',
  2272. 'tipo' => 'required|string|max:35',
  2273. 'adjuntos' => 'required|json',
  2274. ]);
  2275. if($validator->fails()){
  2276. return $this->responseController->makeResponse(
  2277. true,
  2278. "Se encontraron uno o más errores.",
  2279. $this->responseController->makeErrors(
  2280. $validator->errors()->messages()
  2281. ),
  2282. 401
  2283. );
  2284. }
  2285. $form = $request->all();
  2286. $idUser = $this->encryptionController->decrypt($form['id_user']);
  2287. if(!$idUser){
  2288. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  2289. }
  2290. $usr = DB::table('S002V01TUSUA')->where([
  2291. ['USUA_IDUS', '=', $idUser],
  2292. ['USUA_NULI', '=', $form['linea']]
  2293. ])->first();
  2294. if(is_null($usr)){
  2295. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  2296. }
  2297. $allowedFiles = ["Colonias", "Códigos postales", "Estados", "Localidades", "Municipios", "Países", "Régimen fiscal", "General"];
  2298. if(!in_array($form['tipo'], $allowedFiles)){
  2299. return $this->responseController->makeResponse(true, "El tipo de catálogo no está soportado.", [], 400);
  2300. }
  2301. $now = $this->functionsController->now();
  2302. $nowStr = $now->toDateTimeString();
  2303. $attachedFiles = json_decode($form['adjuntos'], true);
  2304. $attachedArr = [];
  2305. foreach($attachedFiles as $file){
  2306. $idFile = $file['id'];
  2307. $idFile = $this->encryptionController->decrypt($idFile);
  2308. if(!$idFile){
  2309. return $this->responseController->makeResponse(true, "El ID del archivo $file[name] no fue encriptado correctamente", [], 400);
  2310. }
  2311. $fileObj = DB::table('S002V01TARTE')->where([
  2312. ['ARTE_IDAR', '=', $idFile],
  2313. ['ARTE_NULI', '=', $form['linea']]
  2314. ])->first();
  2315. if(is_null($fileObj)){
  2316. return $this->responseController->makeResponse(true, "El ID del archivo $file[name] no existe", [], 404);
  2317. }else if($fileObj->ARTE_ESTA == 'Eliminado'){
  2318. return $this->responseController->makeResponse(true, "El archivo $file[name] está eliminado", [], 404);
  2319. }
  2320. $fileSaved = $this->moveFinalFile($form['linea'], 'CA', $fileObj, 'S002V01M01ADSI', 'S002V01F10AGCA', 'S002V01P02NUCA');
  2321. if(!$fileSaved[0]){
  2322. return $this->responseController->makeResponse(true, "El archivo no pudo guardarse: $fileSaved[1]", [], 500);
  2323. }
  2324. $attachedArr[] = $this->encryptionController->encrypt($fileSaved[1]);
  2325. DB::table('S002V01TARTE')->where([
  2326. ['ARTE_IDAR', '=', $idFile],
  2327. ['ARTE_NULI', '=', $form['linea']],
  2328. ])->update([
  2329. 'ARTE_ESTA' => 'Eliminado',
  2330. 'ARTE_USMO' => $idUser,
  2331. 'ARTE_FEMO' => $nowStr
  2332. ]);
  2333. }
  2334. $lastVersionObj = DB::table('S002V01TCATA')->where([
  2335. ['CATA_NULI', '=', $form['linea']],
  2336. ['CATA_TICA', '=', $form['tipo']]
  2337. ])->orderBy('CATA_IDCA', 'desc')->first();
  2338. $lastVersion = 0;
  2339. if(is_null($lastVersionObj)){
  2340. $lastVersion = 1;
  2341. }else{
  2342. DB::table('S002V01TCATA')->where([
  2343. ['CATA_NULI', '=', $form['linea']],
  2344. ['CATA_IDCA', '=', $lastVersionObj->CATA_IDCA],
  2345. ])->update([
  2346. 'CATA_ESTA' => 'Obsoleto',
  2347. ]);
  2348. $lastVersion = intval($lastVersionObj->CATA_VERS) + 1;
  2349. }
  2350. $attachedStr = json_encode($attachedArr);
  2351. DB::table('S002V01TCATA')->insert([
  2352. 'CATA_NULI' => $form['linea'],
  2353. 'CATA_TICA' => $form['tipo'],
  2354. 'CATA_VERS' => $lastVersion,
  2355. 'CATA_ARRE' => $attachedStr,
  2356. 'CATA_USRE' => $idUser,
  2357. 'CATA_FERE' => $nowStr,
  2358. ]);
  2359. $actions = DB::getQueryLog();
  2360. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2361. $idac = $this->functionsController->registerActivity(
  2362. $form['linea'],
  2363. 'S002V01M01ADSI',
  2364. 'S002V01F10AGCA',
  2365. 'S002V01P02NUCA',
  2366. 'Registro',
  2367. "El usuario $name (" . $usr->USUA_IDUS . ") registró un nuevo catálogo.",
  2368. $idUser,
  2369. $nowStr,
  2370. );
  2371. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2372. return $this->responseController->makeresponse(false, "EXITO");
  2373. }
  2374. public function getCatalogueHistory($type, $idUser, $line){
  2375. DB::enableQueryLog();
  2376. $idUser = $this->encryptionController->decrypt($idUser);
  2377. if(!$idUser){
  2378. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  2379. }
  2380. $usr = DB::table('S002V01TUSUA')->where([
  2381. ['USUA_IDUS', '=', $idUser],
  2382. ['USUA_NULI', '=', $line]
  2383. ])->first();
  2384. if(is_null($usr)){
  2385. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  2386. }
  2387. $types = ['Colonias', 'Códigos postales', 'Estados', 'Localidades', 'Municipios', 'Países', 'Régimen fiscal'];
  2388. if(!in_array($type, $types)){
  2389. return $this->responseController->makeResponse(true, 'El tipo de catálogo no está soportado.', [], 404);
  2390. }
  2391. $catalogueHistory = DB::table('S002V01TCATA')->select([
  2392. 'CATA_IDCA AS IDCATALOGO',
  2393. 'CATA_VERS AS VERSION',
  2394. 'CATA_ARRE AS ARCHIVO',
  2395. 'CATA_ESTA AS ESTADO',
  2396. 'CATA_USRE AS USUARIOREGISTRA',
  2397. 'CATA_FERE AS FECHAREGISTRO',
  2398. ])->where([
  2399. ['CATA_NULI', '=', $line],
  2400. ['CATA_TICA', '=', $type],
  2401. ])->get()->all();
  2402. $cont = 0;
  2403. foreach($catalogueHistory as $reg){
  2404. $usrReg = DB::table('S002V01TUSUA')->where([
  2405. ['USUA_NULI', '=', $line],
  2406. ['USUA_IDUS', '=', $reg->USUARIOREGISTRA]
  2407. ])->first();
  2408. $catalogueHistory[$cont]->USUARIOREGISTRA = $this->functionsController->joinName(
  2409. $usrReg->USUA_NOMB,
  2410. $usrReg->USUA_APPA,
  2411. $usrReg->USUA_APMA,
  2412. ) . " (" . $reg->USUARIOREGISTRA . ")";
  2413. $cont++;
  2414. }
  2415. $now = $this->functionsController->now();
  2416. $nowStr = $now->toDateTimeString();
  2417. $actions = DB::getQueryLog();
  2418. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2419. $idac = $this->functionsController->registerActivity(
  2420. $line,
  2421. 'S002V01M01ADSI',
  2422. 'S002V01F10AGCA',
  2423. 'S002V01P01HICA',
  2424. 'Consulta',
  2425. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el historial de catálogos $type.",
  2426. $idUser,
  2427. $nowStr
  2428. );
  2429. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2430. return $this->responseController->makeResponse(false, 'EXITO', $catalogueHistory);
  2431. }
  2432. public function getMaterialIcons($idUser, $line){
  2433. DB::enableQueryLog();
  2434. $idUser = $this->encryptionController->decrypt($idUser);
  2435. if(!$idUser){
  2436. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  2437. }
  2438. $usr = DB::table('S002V01TUSUA')->where([
  2439. ['USUA_IDUS', '=', $idUser],
  2440. ['USUA_NULI', '=', $line]
  2441. ])->first();
  2442. if(is_null($usr)){
  2443. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  2444. }
  2445. $iconsStr = file_get_contents("C:\\inetpub\\wwwroot\\sam\\storage\\app\\files\\icons.json");
  2446. $iconsArr = json_decode($iconsStr, true);
  2447. $icons = $iconsArr['icons'];
  2448. $iconsF = [];
  2449. foreach($icons as $icon){
  2450. if(!array_key_exists($icon['name'], $iconsF)){
  2451. $iconsF[$icon['name']] = [
  2452. 'name' => $icon['name'],
  2453. 'categorie' => $icon['categories'],
  2454. ];
  2455. }
  2456. }
  2457. $now = $this->functionsController->now();
  2458. $nowStr = $now->toDateTimeString();
  2459. $actions = DB::getQueryLog();
  2460. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2461. $idac = $this->functionsController->registerActivity(
  2462. $line,
  2463. '-',
  2464. '-',
  2465. '-',
  2466. 'Consulta',
  2467. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los íconos de Material.",
  2468. $idUser,
  2469. $nowStr
  2470. );
  2471. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2472. return $this->responseController->makeResponse(false, 'EXITO', array_values($iconsF));
  2473. }
  2474. public function getOrderPriorities($idUser, $line){
  2475. DB::enableQueryLog();
  2476. $idUser = $this->encryptionController->decrypt($idUser);
  2477. if(!$idUser){
  2478. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  2479. }
  2480. $usr = DB::table('S002V01TUSUA')->where([
  2481. ['USUA_NULI', '=', $line],
  2482. ['USUA_IDUS', '=', $idUser],
  2483. ])->first();
  2484. if(is_null($usr)){
  2485. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2486. }
  2487. $systemParamsExists = file_exists('C:\inetpub\wwwroot\sam\storage\app\files\system-params.json');
  2488. if(!$systemParamsExists){
  2489. return $this->responseController->makeResponse(true, 'El archivo de parámetros del sistema no fue encontrado.', [], 500);
  2490. }
  2491. $paramsStr = file_get_contents('C:\inetpub\wwwroot\sam\storage\app\files\system-params.json');
  2492. $paramsArr = json_decode($paramsStr, true);
  2493. $now = $this->functionsController->now();
  2494. $nowStr = $now->toDateTimeString();
  2495. $actions = DB::getQueryLog();
  2496. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2497. $idac = $this->functionsController->registerActivity(
  2498. $line,
  2499. 'S002V01M01ADSI',
  2500. 'S002V01F11PASI',
  2501. 'S002V01P08EPOT',
  2502. 'Consulta',
  2503. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la configuración de las prioridades de las órdenes de trabajo.",
  2504. $idUser,
  2505. $nowStr
  2506. );
  2507. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2508. return $this->responseController->makeResponse(false, 'EXITO', ['order_priorities' => $paramsArr['order_priorities']]);
  2509. }
  2510. public function updateOrderPriorities(Request $request){
  2511. DB::enableQueryLog();
  2512. $validator = Validator::make($request->all(), [
  2513. 'id_user' => 'required|string',
  2514. 'linea' => 'required|integer',
  2515. 'priorities' => 'required|json',
  2516. ]);
  2517. if($validator->fails()){
  2518. return $this->responseController->makeResponse(
  2519. true,
  2520. "Se encontraron uno o más errores.",
  2521. $this->responseController->makeErrors(
  2522. $validator->errors()->messages()
  2523. ),
  2524. 401
  2525. );
  2526. }
  2527. $form = $request->all();
  2528. $idUser = $this->encryptionController->decrypt($form['id_user']);
  2529. if(!$idUser){
  2530. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  2531. }
  2532. $usr = DB::table('S002V01TUSUA')->where([
  2533. ['USUA_IDUS', '=', $idUser],
  2534. ['USUA_NULI', '=', $form['linea']]
  2535. ])->first();
  2536. if(is_null($usr)){
  2537. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  2538. }
  2539. $systemParamsExists = file_exists('C:\inetpub\wwwroot\sam\storage\app\files\system-params.json');
  2540. if(!$systemParamsExists){
  2541. return $this->responseController->makeResponse(true, 'El archivo de parámetros del sistema no fue encontrado.', [], 500);
  2542. }
  2543. $paramsStr = file_get_contents('C:\inetpub\wwwroot\sam\storage\app\files\system-params.json');
  2544. $paramsArr = json_decode($paramsStr, true);
  2545. $newPriorities = json_decode($form['priorities'], true);
  2546. if(empty($newPriorities)){
  2547. return $this->responseController->makeResponse(true, 'El arreglo de prioridades enviado está vacío', [], 400);
  2548. }
  2549. $paramsArr['order_priorities'] = $newPriorities;
  2550. $paramsStr = json_encode($paramsArr);
  2551. file_put_contents('C:\inetpub\wwwroot\sam\storage\app\files\system-params.json', $paramsStr);
  2552. $now = $this->functionsController->now();
  2553. $nowStr = $now->toDateTimeString();
  2554. $actions = DB::getQueryLog();
  2555. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2556. $idac = $this->functionsController->registerActivity(
  2557. $form['linea'],
  2558. 'S002V01M01ADSI',
  2559. 'S002V01F11PASI',
  2560. 'S002V01P08EPOT',
  2561. 'Actualización',
  2562. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó la configuración de las prioridades de las órdenes de trabajo.",
  2563. $idUser,
  2564. $nowStr
  2565. );
  2566. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2567. return $this->responseController->makeResponse(false, 'EXITO');
  2568. }
  2569. public function getLoginImages($idUser, $line){
  2570. DB::enableQueryLog();
  2571. $idUser = $this->encryptionController->decrypt($idUser);
  2572. if(!$idUser){
  2573. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  2574. }
  2575. $usr = DB::table('S002V01TUSUA')->where([
  2576. ['USUA_IDUS', '=', $idUser],
  2577. ['USUA_NULI', '=', $line]
  2578. ])->first();
  2579. if(is_null($usr)){
  2580. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2581. }
  2582. $systemParamsExists = file_exists('C:\inetpub\wwwroot\sam\storage\app\files\system-params.json');
  2583. if(!$systemParamsExists){
  2584. return $this->responseController->makeResponse(true, 'El archivo de parámetros del sistema no fue encontrado.', [], 500);
  2585. }
  2586. $paramsStr = file_get_contents('C:\inetpub\wwwroot\sam\storage\app\files\system-params.json');
  2587. $paramsArr = json_decode($paramsStr, true);
  2588. $now = $this->functionsController->now();
  2589. $nowStr = $now->toDateTimeString();
  2590. $actions = DB::getQueryLog();
  2591. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2592. $idac = $this->functionsController->registerActivity(
  2593. $line,
  2594. 'S002V01M01ADSI',
  2595. 'S002V01F11PASI',
  2596. 'S002V01P04EILO',
  2597. 'Consulta',
  2598. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las imágenes de la pantalla del inicio de sesión.",
  2599. $idUser,
  2600. $nowStr
  2601. );
  2602. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2603. return $this->responseController->makeResponse(false, 'EXITO', ['login_params' => $paramsArr['login_params']]);
  2604. }
  2605. public function saveLoginImage(Request $request){
  2606. DB::enableQueryLog();
  2607. $validator = Validator::make($request->all(), [
  2608. 'id_user' => 'required|string',
  2609. 'linea' => 'required|integer',
  2610. 'tipo' => 'required|string|in:image,background',
  2611. 'archivo' => 'required|string',
  2612. ]);
  2613. if($validator->fails()){
  2614. return $this->responseController->makeResponse(
  2615. true,
  2616. "Se encontraron uno o más errores.",
  2617. $this->responseController->makeErrors(
  2618. $validator->errors()->messages()
  2619. ),
  2620. 401
  2621. );
  2622. }
  2623. $form = $request->all();
  2624. $idUser = $this->encryptionController->decrypt($form['id_user']);
  2625. if(!$idUser){
  2626. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  2627. }
  2628. $usr = DB::table('S002V01TUSUA')->where([
  2629. ['USUA_IDUS', '=', $idUser],
  2630. ['USUA_NULI', '=', $form['linea']]
  2631. ])->first();
  2632. if(is_null($usr)){
  2633. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  2634. }
  2635. $systemParamsExists = file_exists('C:\inetpub\wwwroot\sam\storage\app\files\system-params.json');
  2636. if(!$systemParamsExists){
  2637. return $this->responseController->makeResponse(true, 'El archivo de parámetros del sistema no fue encontrado.', [], 500);
  2638. }
  2639. $paramsStr = file_get_contents('C:\inetpub\wwwroot\sam\storage\app\files\system-params.json');
  2640. $paramsArr = json_decode($paramsStr, true);
  2641. $actualImage = $paramsArr["login_params"]["login_$form[tipo]"];
  2642. $replaceImage = $this->encryptionController->decrypt($form['archivo']);
  2643. if(!$replaceImage){
  2644. return $this->responseController->makeResponse(true, 'El ID de la imágen enviada no fue encriptado correctamente.', [], 500);
  2645. }
  2646. $imageObj = DB::table('S002V01TARTE')->where([
  2647. ['ARTE_IDAR', '=', $replaceImage],
  2648. ['ARTE_NULI', '=', $form['linea']]
  2649. ])->first();
  2650. $ubiImgTmp = $imageObj->ARTE_UBTE;
  2651. if(!file_exists($ubiImgTmp)){
  2652. return $this->responseController->makeResponse(true, 'El archivo de la imagen enviada no existe.', [], 500);
  2653. }
  2654. $ubiImgAct = "C:\\inetpub\\wwwroot\\sam\\public\\assets\\$actualImage";
  2655. if(!file_exists($ubiImgAct)){
  2656. return $this->responseController->makeResponse(true, 'La imagen actual no existe.', [], 500);
  2657. }
  2658. $now = $this->functionsController->now();
  2659. $timestamp = $now->timestamp;
  2660. rename($ubiImgAct, "C:\\inetpub\\wwwroot\\sam\\public\\assets\\UPDATED_$timestamp.png");
  2661. copy($ubiImgTmp, $ubiImgAct);
  2662. $nowStr = $now->toDateTimeString();
  2663. $actions = DB::getQueryLog();
  2664. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2665. $idac = $this->functionsController->registerActivity(
  2666. $form['linea'],
  2667. 'S002V01M01ADSI',
  2668. 'S002V01F11PASI',
  2669. 'S002V01P04EILO',
  2670. 'Actualización',
  2671. "El usuario $name (" . $usr->USUA_IDUS . ") cambió la apariencia del login.",
  2672. $idUser,
  2673. $nowStr
  2674. );
  2675. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2676. return $this->responseController->makeResponse(false, 'EXITO.');
  2677. }
  2678. public function updateModuleIcons(Request $request){
  2679. DB::enableQueryLog();
  2680. $validator = Validator::make($request->all(), [
  2681. 'id_user' => 'required|string',
  2682. 'linea' => 'required|integer',
  2683. 'module_icons' => 'required|string',
  2684. ]);
  2685. if($validator->fails()){
  2686. return $this->responseController->makeResponse(
  2687. true,
  2688. "Se encontraron uno o más errores.",
  2689. $this->responseController->makeErrors(
  2690. $validator->errors()->messages()
  2691. ),
  2692. 401
  2693. );
  2694. }
  2695. $form = $request->all();
  2696. $idUser = $this->encryptionController->decrypt($form['id_user']);
  2697. if(!$idUser){
  2698. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  2699. }
  2700. $usr = DB::table('S002V01TUSUA')->where([
  2701. ['USUA_IDUS', '=', $idUser],
  2702. ['USUA_NULI', '=', $form['linea']]
  2703. ])->first();
  2704. if(is_null($usr)){
  2705. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  2706. }
  2707. $moduleIconsStr = $form['module_icons'];
  2708. $moduleIconsDec = $this->encryptionController->decrypt($moduleIconsStr);
  2709. if(!$moduleIconsDec){
  2710. return $this->responseController->makeResponse(true, 'El arreglo de módulos no fue encriptado correctamente.', [], 400);
  2711. }
  2712. $now = $this->functionsController->now();
  2713. $nowStr = $now->toDateTimeString();
  2714. $moduleIconsArr = json_decode($moduleIconsDec, true);
  2715. foreach($moduleIconsArr as $modIcon){
  2716. DB::table('S002V01TMODU')->where([
  2717. ['MODU_NULI', '=', $form['linea']],
  2718. ['MODU_IDMO', '=', $modIcon['IDMODULO']]
  2719. ])->update([
  2720. 'MODU_ICON' => $modIcon['ICONOMODULO'],
  2721. 'MODU_USMO' => $idUser,
  2722. 'MODU_FEMO' => $nowStr,
  2723. ]);
  2724. }
  2725. $actions = DB::getQueryLog();
  2726. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2727. $idac = $this->functionsController->registerActivity(
  2728. $form['linea'],
  2729. 'S002V01M01ADSI',
  2730. 'S002V01F11PASI',
  2731. 'S002V01P05EIMO',
  2732. 'Actualización',
  2733. "El usuario $name (" . $usr->USUA_IDUS . ") cambió los íconos de los módulos.",
  2734. $idUser,
  2735. $nowStr
  2736. );
  2737. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2738. return $this->responseController->makeResponse(false, 'EXITO.');
  2739. }
  2740. public function updateSubmoduleIcons(Request $request){
  2741. DB::enableQueryLog();
  2742. $validator = Validator::make($request->all(), [
  2743. 'id_user' => 'required|string',
  2744. 'linea' => 'required|integer',
  2745. 'submodule_icons' => 'required|string',
  2746. ]);
  2747. if($validator->fails()){
  2748. return $this->responseController->makeResponse(
  2749. true,
  2750. "Se encontraron uno o más errores.",
  2751. $this->responseController->makeErrors(
  2752. $validator->errors()->messages()
  2753. ),
  2754. 401
  2755. );
  2756. }
  2757. $form = $request->all();
  2758. $idUser = $this->encryptionController->decrypt($form['id_user']);
  2759. if(!$idUser){
  2760. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  2761. }
  2762. $usr = DB::table('S002V01TUSUA')->where([
  2763. ['USUA_IDUS', '=', $idUser],
  2764. ['USUA_NULI', '=', $form['linea']]
  2765. ])->first();
  2766. if(is_null($usr)){
  2767. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  2768. }
  2769. $submoduleIconsDec = $this->encryptionController->decrypt($form['submodule_icons']);
  2770. if(!$submoduleIconsDec){
  2771. return $this->responseController->makeResponse(true, 'El arreglo de submódulos no fue encriptado correctamente.', [], 400);
  2772. }
  2773. $now = $this->functionsController->now();
  2774. $nowStr = $now->toDateTimeString();
  2775. $submodulesArr = json_decode($submoduleIconsDec, true);
  2776. foreach($submodulesArr as $module=>$submodules){
  2777. foreach($submodules as $submodule){
  2778. DB::table('S002V01TSUBM')->where([
  2779. ['SUBM_NULI', '=', $form['linea']],
  2780. ['SUBM_IDSM', '=', $submodule['IDSUBMODULO']],
  2781. ['SUBM_IDMO', '=', $module],
  2782. ])->update([
  2783. 'SUBM_ICON' => $submodule['ICONOSUBMODULO'],
  2784. 'SUBM_USMO' => $idUser,
  2785. 'SUBM_FEMO' => $nowStr,
  2786. ]);
  2787. }
  2788. }
  2789. $actions = DB::getQueryLog();
  2790. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2791. $idac = $this->functionsController->registerActivity(
  2792. $form['linea'],
  2793. 'S002V01M01ADSI',
  2794. 'S002V01F11PASI',
  2795. 'S002V01P06EISU',
  2796. 'Actualización',
  2797. "El usuario $name (" . $usr->USUA_IDUS . ") cambió los íconos de los submódulos.",
  2798. $idUser,
  2799. $nowStr
  2800. );
  2801. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2802. return $this->responseController->makeResponse(false, 'EXITO.');
  2803. }
  2804. public function updateFunctionIcons(Request $request){
  2805. DB::enableQueryLog();
  2806. $validator = Validator::make($request->all(), [
  2807. 'id_user' => 'required|string',
  2808. 'linea' => 'required|integer',
  2809. 'module_functions_icons' => 'required|string',
  2810. 'submodule_functions_icons' => 'required|string',
  2811. ]);
  2812. if($validator->fails()){
  2813. return $this->responseController->makeResponse(
  2814. true,
  2815. "Se encontraron uno o más errores.",
  2816. $this->responseController->makeErrors(
  2817. $validator->errors()->messages()
  2818. ),
  2819. 401
  2820. );
  2821. }
  2822. $form = $request->all();
  2823. $idUser = $this->encryptionController->decrypt($form['id_user']);
  2824. if(!$idUser){
  2825. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  2826. }
  2827. $usr = DB::table('S002V01TUSUA')->where([
  2828. ['USUA_IDUS', '=', $idUser],
  2829. ['USUA_NULI', '=', $form['linea']]
  2830. ])->first();
  2831. if(is_null($usr)){
  2832. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  2833. }
  2834. $moduleFunctionIconsDec = $this->encryptionController->decrypt($form['module_functions_icons']);
  2835. if(!$moduleFunctionIconsDec){
  2836. return $this->responseController->makeResponse(true, 'El arreglo de funciones por módulo no fue encriptado correctamente.', [], 400);
  2837. }
  2838. $submoduleFunctionIconsDec = $this->encryptionController->decrypt($form['submodule_functions_icons']);
  2839. if(!$submoduleFunctionIconsDec){
  2840. return $this->responseController->makeResponse(true, 'El arreglo de funciones por submódulo no fue encriptado correctamente.', [], 400);
  2841. }
  2842. $now = $this->functionsController->now();
  2843. $nowStr = $now->toDateTimeString();
  2844. $moduleFunctionIconsArr = json_decode($moduleFunctionIconsDec, true);
  2845. foreach($moduleFunctionIconsArr as $module=>$functions){
  2846. foreach($functions as $function){
  2847. DB::table('S002V01TFUNC')->where([
  2848. ['FUNC_NULI', '=', $form['linea']],
  2849. ['FUNC_IDFU', '=', $function['IDFUNCION']],
  2850. ['FUNC_IDMO', '=', $module],
  2851. ['FUNC_IDSM', '=', null],
  2852. ])->update([
  2853. 'FUNC_ICON' => $function['ICONOFUNCION'],
  2854. 'FUNC_USMO' => $idUser,
  2855. 'FUNC_FEMO' => $nowStr,
  2856. ]);
  2857. }
  2858. }
  2859. $submoduleFunctionIconsArr = json_decode($submoduleFunctionIconsDec, true);
  2860. foreach($submoduleFunctionIconsArr as $module=>$submodules){
  2861. foreach($submodules as $submodule=>$functions){
  2862. foreach($functions as $function){
  2863. DB::table('S002V01TFUNC')->where([
  2864. ['FUNC_NULI', '=', $form['linea']],
  2865. ['FUNC_IDFU', '=', $function['IDFUNCION']],
  2866. ['FUNC_IDMO', '=', $module],
  2867. ['FUNC_IDSM', '=', $submodule],
  2868. ])->update([
  2869. 'FUNC_ICON' => $function['ICONOFUNCION'],
  2870. 'FUNC_USMO' => $idUser,
  2871. 'FUNC_FEMO' => $nowStr,
  2872. ]);
  2873. }
  2874. }
  2875. }
  2876. $actions = DB::getQueryLog();
  2877. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2878. $idac = $this->functionsController->registerActivity(
  2879. $form['linea'],
  2880. 'S002V01M01ADSI',
  2881. 'S002V01F11PASI',
  2882. 'S002V01P07EIFU',
  2883. 'Actualización',
  2884. "El usuario $name (" . $usr->USUA_IDUS . ") cambió los íconos de las funciones.",
  2885. $idUser,
  2886. $nowStr
  2887. );
  2888. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2889. return $this->responseController->makeResponse(false, 'EXITO.');
  2890. }
  2891. public function getModuleFunctions($idMod, $idUser, $line){
  2892. DB::enableQueryLog();
  2893. $idUser = $this->encryptionController->decrypt($idUser);
  2894. if(!$idUser){
  2895. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2896. }
  2897. $usr = DB::table('S002V01TUSUA')->where([
  2898. ['USUA_IDUS', '=', $idUser],
  2899. ['USUA_NULI', '=', $line]
  2900. ])->first();
  2901. if(is_null($usr)){
  2902. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2903. }
  2904. $idMod = $this->encryptionController->decrypt($idMod);
  2905. if(!$idMod){
  2906. return $this->responseController->makeResponse(true, 'El ID del módulo no fue encriptado correctamente', [], 400);
  2907. }
  2908. $mod = DB::table('S002V01TMODU')->where([
  2909. ['MODU_NULI', '=', $line],
  2910. ['MODU_IDMO', '=', $idMod]
  2911. ])->first();
  2912. if(is_null($mod)){
  2913. return $this->responseController->makeResponse(true, 'El módulo consultado no existe', [], 404);
  2914. }
  2915. $functions = DB::table('S002V01TFUNC')->where([
  2916. ['FUNC_NULI', '=', $line],
  2917. ['FUNC_IDMO', '=', $idMod],
  2918. ['FUNC_IDSM', '=', null],
  2919. ])->select([
  2920. 'FUNC_IDFU AS IDFUNCION',
  2921. 'FUNC_NOMB AS NOMBREFUNCION',
  2922. 'FUNC_ICON AS ICONOFUNCION'
  2923. ])->get()->all();
  2924. $now = $this->functionsController->now();
  2925. $nowStr = $now->toDateTimeString();
  2926. $actions = DB::getQueryLog();
  2927. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2928. $idac = $this->functionsController->registerActivity(
  2929. $line,
  2930. '-',
  2931. '-',
  2932. '-',
  2933. 'Consulta',
  2934. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las funciones del módulo '" . $mod->MODU_NOMO . "'.",
  2935. $idUser,
  2936. $nowStr
  2937. );
  2938. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2939. return $this->responseController->makeResponse(false, 'EXITO', $functions);
  2940. }
  2941. public function getSubmodules($idMod, $idUser, $line){
  2942. DB::enableQueryLog();
  2943. $idUser = $this->encryptionController->decrypt($idUser);
  2944. if(!$idUser){
  2945. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  2946. }
  2947. $usr = DB::table('S002V01TUSUA')->where([
  2948. ['USUA_IDUS', '=', $idUser],
  2949. ['USUA_NULI', '=', $line]
  2950. ])->first();
  2951. if(is_null($usr)){
  2952. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  2953. }
  2954. $idMod = $this->encryptionController->decrypt($idMod);
  2955. if(!$idMod){
  2956. return $this->responseController->makeResponse(true, 'El ID del módulo no fue encriptado correctamente', [], 400);
  2957. }
  2958. $mod = DB::table('S002V01TMODU')->where([
  2959. ['MODU_NULI', '=', $line],
  2960. ['MODU_IDMO', '=', $idMod]
  2961. ])->first();
  2962. if(is_null($mod)){
  2963. return $this->responseController->makeResponse(true, 'El módulo consultado no existe', [], 404);
  2964. }
  2965. $submodules = DB::table('S002V01TSUBM')->where([
  2966. ['SUBM_NULI', '=', $line],
  2967. ['SUBM_IDMO', '=', $idMod],
  2968. ])->select([
  2969. 'SUBM_IDSM AS IDSUBMODULO',
  2970. 'SUBM_NOMB AS NOMBRESUBMODULO',
  2971. 'SUBM_ICON AS ICONOSUBMODULO',
  2972. 'SUBM_IMAG AS IMAGENSUBMODULO',
  2973. ])->get()->all();
  2974. $now = $this->functionsController->now();
  2975. $nowStr = $now->toDateTimeString();
  2976. $actions = DB::getQueryLog();
  2977. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2978. $idac = $this->functionsController->registerActivity(
  2979. $line,
  2980. '-',
  2981. '-',
  2982. '-',
  2983. 'Consulta',
  2984. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los submódulos del módulo '" . $mod->MODU_NOMO . "'.",
  2985. $idUser,
  2986. $nowStr
  2987. );
  2988. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2989. return $this->responseController->makeResponse(false, 'EXITO', $submodules);
  2990. }
  2991. public function registerSCADA(Request $request){
  2992. DB::enableQueryLog();
  2993. $validator = Validator::make($request->all(), [
  2994. 'id_user' => 'required|string',
  2995. 'linea' => 'required|integer',
  2996. 'scada_name' => 'required|string|max:100',
  2997. ]);
  2998. if($validator->fails()){
  2999. return $this->responseController->makeResponse(
  3000. true,
  3001. "Se encontraron uno o más errores.",
  3002. $this->responseController->makeErrors(
  3003. $validator->errors()->messages()
  3004. ),
  3005. 401
  3006. );
  3007. }
  3008. $form = $request->all();
  3009. $idUser = $this->encryptionController->decrypt($form['id_user']);
  3010. if(!$idUser){
  3011. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  3012. }
  3013. $usr = DB::table('S002V01TUSUA')->where([
  3014. ['USUA_IDUS', '=', $idUser],
  3015. ['USUA_NULI', '=', $form['linea']]
  3016. ])->first();
  3017. if(is_null($usr)){
  3018. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  3019. }
  3020. $now = $this->functionsController->now();
  3021. $nowStr = $now->toDateTimeString();
  3022. DB::table('S002V01TLISC')->insert([
  3023. 'LISC_NULI' => $form['linea'],
  3024. 'LISC_NOSC' => $form['scada_name'],
  3025. 'LISC_USRE' => $idUser,
  3026. 'LISC_FERE' => $nowStr,
  3027. ]);
  3028. $actions = DB::getQueryLog();
  3029. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3030. $idac = $this->functionsController->registerActivity(
  3031. $form['linea'],
  3032. 'S002V01M01ADSI',
  3033. 'S002V01F06ASWA',
  3034. 'S002V01P02RESC',
  3035. 'Registro',
  3036. "El usuario $name (" . $usr->USUA_IDUS . ") registró el SCADA $form[scada_name].",
  3037. $idUser,
  3038. $nowStr
  3039. );
  3040. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  3041. return $this->responseController->makeResponse(false, 'EXITO.');
  3042. }
  3043. public function getSCADAList($idUser, $line){
  3044. DB::enableQueryLog();
  3045. $idUser = $this->encryptionController->decrypt($idUser);
  3046. if(!$idUser){
  3047. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  3048. }
  3049. $usr = DB::table('S002V01TUSUA')->where([
  3050. ['USUA_IDUS', '=', $idUser],
  3051. ['USUA_NULI', '=', $line]
  3052. ])->first();
  3053. if(is_null($usr)){
  3054. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  3055. }
  3056. $list = DB::table('S002V01TLISC')->select([
  3057. 'LISC_IDSC AS IDSCADA',
  3058. 'LISC_NOSC AS NOMBRESCADA',
  3059. 'LISC_ESTA AS ESTATUS',
  3060. 'LISC_USRE AS USUARIOREGISTRO',
  3061. 'LISC_FERE AS FECHAREGISTRO',
  3062. 'LISC_USMO AS USUARIOMODIFICO',
  3063. 'LISC_FEMO AS FECHAMODIFICACION',
  3064. ])->where([
  3065. ['LISC_NULI', '=', $line],
  3066. ['LISC_ESTA', '=', 'Activo'],
  3067. ])->get()->all();
  3068. foreach($list as $item){
  3069. $usrReg = DB::table('S002V01TUSUA')->where([
  3070. ['USUA_NULI', '=', $line],
  3071. ['USUA_IDUS', '=', $item->USUARIOREGISTRO],
  3072. ])->first();
  3073. $nameReg = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA) . " (" . $item->USUARIOREGISTRO . ")";
  3074. $item->USUARIOREGISTRO = $nameReg;
  3075. $usrMod = DB::table('S002V01TUSUA')->where([
  3076. ['USUA_NULI', '=', $line],
  3077. ['USUA_IDUS', '=', $item->USUARIOMODIFICO],
  3078. ])->first();
  3079. if(!is_null($usrMod)){
  3080. $nameMod = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA) . " (" . $item->USUARIOMODIFICO . ")";
  3081. $item->USUARIOMODIFICO = $nameMod;
  3082. }
  3083. }
  3084. $now = $this->functionsController->now();
  3085. $nowStr = $now->toDateTimeString();
  3086. $actions = DB::getQueryLog();
  3087. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3088. $idac = $this->functionsController->registerActivity(
  3089. $line,
  3090. 'S002V01M01ADSI',
  3091. 'S002V01F06ASWA',
  3092. 'S002V01P01ADSC',
  3093. 'Consulta',
  3094. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los SCADA registrados en el sistema.",
  3095. $idUser,
  3096. $nowStr
  3097. );
  3098. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  3099. return $this->responseController->makeResponse(false, 'EXITO', $list);
  3100. }
  3101. public function updateSCADA(Request $request){
  3102. DB::enableQueryLog();
  3103. $validator = Validator::make($request->all(), [
  3104. 'id_user' => 'required|string',
  3105. 'id_scada' => 'required|string',
  3106. 'linea' => 'required|integer',
  3107. 'scada_name' => 'required|string|max:100',
  3108. ]);
  3109. if($validator->fails()){
  3110. return $this->responseController->makeResponse(
  3111. true,
  3112. "Se encontraron uno o más errores.",
  3113. $this->responseController->makeErrors(
  3114. $validator->errors()->messages()
  3115. ),
  3116. 401
  3117. );
  3118. }
  3119. $form = $request->all();
  3120. $idUser = $this->encryptionController->decrypt($form['id_user']);
  3121. if(!$idUser){
  3122. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  3123. }
  3124. $usr = DB::table('S002V01TUSUA')->where([
  3125. ['USUA_NULI', '=', $form['linea']],
  3126. ['USUA_IDUS', '=', $idUser]
  3127. ])->first();
  3128. if(is_null($usr)){
  3129. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  3130. }
  3131. $idSCADA = $this->encryptionController->decrypt($form['id_scada']);
  3132. if(!$idSCADA){
  3133. return $this->responseController->makeResponse(true, 'El ID del SCADA no fue encriptado correctamente.', [], 400);
  3134. }
  3135. $scada = DB::table('S002V01TLISC')->where([
  3136. ['LISC_NULI', '=', $form['linea']],
  3137. ['LISC_IDSC', '=', $idSCADA],
  3138. ])->first();
  3139. if(is_null($scada)){
  3140. return $this->responseController->makeResponse(true, 'El SCADA solicitado no existe.', [], 404);
  3141. }
  3142. $now = $this->functionsController->now();
  3143. $nowStr = $now->toDateTimeString();
  3144. DB::table('S002V01TLISC')->where([
  3145. ['LISC_NULI', '=', $form['linea']],
  3146. ['LISC_IDSC', '=', $idSCADA],
  3147. ])->update([
  3148. 'LISC_NOSC' => $form['scada_name'],
  3149. 'LISC_USMO' => $idUser,
  3150. 'LISC_FEMO' => $nowStr,
  3151. ]);
  3152. $actions = DB::getQueryLog();
  3153. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3154. $idac = $this->functionsController->registerActivity(
  3155. $form['linea'],
  3156. 'S002V01M01ADSI',
  3157. 'S002V01F06ASWA',
  3158. 'S002V01P02RESC',
  3159. 'Actualización',
  3160. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó el SCADA $form[scada_name] ($idSCADA).",
  3161. $idUser,
  3162. $nowStr
  3163. );
  3164. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  3165. return $this->responseController->makeResponse(false, 'EXITO.');
  3166. }
  3167. public function deleteSCADA(Request $request){
  3168. DB::enableQueryLog();
  3169. $validator = Validator::make($request->all(), [
  3170. 'id_user' => 'required|string',
  3171. 'id_scada' => 'required|string',
  3172. 'linea' => 'required|integer',
  3173. ]);
  3174. if($validator->fails()){
  3175. return $this->responseController->makeResponse(
  3176. true,
  3177. "Se encontraron uno o más errores.",
  3178. $this->responseController->makeErrors(
  3179. $validator->errors()->messages()
  3180. ),
  3181. 401
  3182. );
  3183. }
  3184. $form = $request->all();
  3185. $idUser = $this->encryptionController->decrypt($form['id_user']);
  3186. if(!$idUser){
  3187. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  3188. }
  3189. $usr = DB::table('S002V01TUSUA')->where([
  3190. ['USUA_IDUS', '=', $idUser],
  3191. ['USUA_NULI', '=', $form['linea']]
  3192. ])->first();
  3193. if(is_null($usr)){
  3194. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  3195. }
  3196. $idSCADA = $this->encryptionController->decrypt($form['id_scada']);
  3197. if(!$idSCADA){
  3198. return $this->responseController->makeResponse(true, 'El ID del SCADA no fue encriptado correctamente.', [], 400);
  3199. }
  3200. $scada = DB::table('S002V01TLISC')->where([
  3201. ['LISC_NULI', '=', $form['linea']],
  3202. ['LISC_IDSC', '=', $idSCADA],
  3203. ])->first();
  3204. if(is_null($scada)){
  3205. return $this->responseController->makeResponse(true, 'El SCADA solicitado no existe.', [], 404);
  3206. }
  3207. $tokensSCADA = DB::table('S002V01TTASW')->where([
  3208. ['TASW_NULI', '=', $form['linea']],
  3209. ['TASW_IDSC', '=', $idSCADA],
  3210. ])->get()->all();
  3211. if(!empty($tokensSCADA)){
  3212. return $this->responseController->makeResponse(true, 'No se pudo eliminar el SCADA porque tiene tokens asignados.', [], 400);
  3213. }
  3214. $now = $this->functionsController->now();
  3215. $nowStr = $now->toDateTimeString();
  3216. DB::table('S002V01TLISC')->where([
  3217. ['LISC_NULI', '=', $form['linea']],
  3218. ['LISC_IDSC', '=', $idSCADA],
  3219. ])->update([
  3220. 'LISC_ESTA' => 'Eliminado',
  3221. 'LISC_USMO' => $idUser,
  3222. 'LISC_FEMO' => $nowStr,
  3223. ]);
  3224. $actions = DB::getQueryLog();
  3225. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3226. $idac = $this->functionsController->registerActivity(
  3227. $form['linea'],
  3228. 'S002V01M01ADSI',
  3229. 'S002V01F06ASWA',
  3230. 'S002V01P01ADSC',
  3231. 'Eliminación',
  3232. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó el SCADA " . $scada->LISC_NOSC . " ($idSCADA).",
  3233. $idUser,
  3234. $nowStr
  3235. );
  3236. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  3237. return $this->responseController->makeResponse(false, 'EXITO.');
  3238. }
  3239. public function generateSCADAtoken(Request $request){
  3240. DB::enableQueryLog();
  3241. $validator = Validator::make($request->all(), [
  3242. 'id_user' => 'required|string',
  3243. 'id_scada' => 'required|string',
  3244. 'linea' => 'required|integer',
  3245. 'descripcion' => 'required|string',
  3246. ]);
  3247. if($validator->fails()){
  3248. return $this->responseController->makeResponse(
  3249. true,
  3250. "Se encontraron uno o más errores.",
  3251. $this->responseController->makeErrors(
  3252. $validator->errors()->messages()
  3253. ),
  3254. 401
  3255. );
  3256. }
  3257. $form = $request->all();
  3258. $idUser = $this->encryptionController->decrypt($form['id_user']);
  3259. if(!$idUser){
  3260. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  3261. }
  3262. $usr = DB::table('S002V01TUSUA')->where([
  3263. ['USUA_IDUS', '=', $idUser],
  3264. ['USUA_NULI', '=', $form['linea']]
  3265. ])->first();
  3266. if(is_null($usr)){
  3267. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  3268. }
  3269. $id = $this->encryptionController->decrypt($form['id_scada']);
  3270. if(!$id){
  3271. return $this->responseController->makeResponse(true, 'El ID del SCADA no está encriptado correctamente', [], 400);
  3272. }
  3273. $scada = DB::table('S002V01TLISC')->where([
  3274. ['LISC_NULI', '=', $form['linea']],
  3275. ['LISC_IDSC', '=', $id],
  3276. ])->first();
  3277. if(is_null($scada)){
  3278. return $this->responseController->makeResponse(true, 'El SCADA consultado no existe', [], 404);
  3279. }
  3280. $now = $this->functionsController->now();
  3281. $iat = $now->timestamp;
  3282. $cad = $now->addYear()->timestamp;
  3283. $payload = [
  3284. "iss" => $scada->LISC_IDSC,
  3285. "aud" => "dominio.syp.mx",
  3286. "iat" => $iat,
  3287. "cad" => $cad
  3288. ];
  3289. $token = JWT::encode($payload, $this->secretKey, 'EdDSA');
  3290. $nowStr = $now->subYear()->toDateTimeString();
  3291. DB::table('S002V01TTASW')->insert([
  3292. 'TASW_NULI' => $form['linea'],
  3293. 'TASW_IDSC' => $scada->LISC_IDSC,
  3294. 'TASW_TOKE' => $token,
  3295. 'TASW_DETO' => $form['descripcion'],
  3296. 'TASW_TIMO' => "PRUEBA",
  3297. 'TASW_USRE' => $idUser,
  3298. 'TASW_FERE' => $nowStr,
  3299. ]);
  3300. $actions = DB::getQueryLog();
  3301. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3302. $idac = $this->functionsController->registerActivity(
  3303. $form['linea'],
  3304. 'S002V01M01ADSI',
  3305. 'S002V01F06ASWA',
  3306. 'S002V01P04GETO',
  3307. 'Registro',
  3308. "El usuario $name (" . $usr->USUA_IDUS . ") registró y asignó el token \"$token\" al SCADA \"" . $scada->LISC_NOSC . " ($id)\".",
  3309. $idUser,
  3310. $nowStr
  3311. );
  3312. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  3313. return $this->responseController->makeResponse(false, 'EXITO.', ['token' => $token]);
  3314. }
  3315. public function getGeneratedTokens($idUser, $line){
  3316. DB::enableQueryLog();
  3317. $idUser = $this->encryptionController->decrypt($idUser);
  3318. if(!$idUser){
  3319. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  3320. }
  3321. $usr = DB::table('S002V01TUSUA')->where([
  3322. ['USUA_IDUS', '=', $idUser],
  3323. ['USUA_NULI', '=', $line]
  3324. ])->first();
  3325. if(is_null($usr)){
  3326. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  3327. }
  3328. $tokens = DB::table('S002V01TTASW')->join('S002V01TLISC', 'LISC_IDSC', '=', 'TASW_IDSC')->select([
  3329. 'LISC_NOSC AS NOMBRESCADA',
  3330. 'TASW_DETO AS DESCRIPCION',
  3331. 'TASW_ESTA AS ESTATUS',
  3332. 'TASW_USRE AS USUARIOREGISTRO',
  3333. 'TASW_FERE AS FECHAREGISTRO',
  3334. 'TASW_USMO AS USUARIOMODIFICACION',
  3335. 'TASW_FEMO AS FECHAMODIFICACION',
  3336. 'TASW_IDSC AS IDSCADA',
  3337. 'TASW_TOKE AS TOKEN'
  3338. ])->where([
  3339. ['TASW_NULI', '=', $line],
  3340. ['LISC_ESTA', '=', 'Activo'],
  3341. ])->get()->all();
  3342. foreach($tokens as $token){
  3343. $usre = DB::table('S002V01TUSUA')->where([
  3344. ['USUA_NULI', '=', $line],
  3345. ['USUA_IDUS', '=', $token->USUARIOREGISTRO]
  3346. ])->first();
  3347. $usreName = $this->functionsController->joinName($usre->USUA_NOMB, $usre->USUA_APPA, $usre->USUA_APMA) . " (" . $token->USUARIOREGISTRO . ")";
  3348. $token->USUARIOREGISTRO = $usreName;
  3349. $usmo = DB::table('S002V01TUSUA')->where([
  3350. ['USUA_NULI', '=', $line],
  3351. ['USUA_IDUS', '=', $token->USUARIOMODIFICACION]
  3352. ])->first();
  3353. if(!is_null($usmo)){
  3354. $usmoName = $this->functionsController->joinName($usmo->USUA_NOMB, $usmo->USUA_APPA, $usmo->USUA_APMA) . " (" . $token->USUARIOMODIFICACION . ")";
  3355. $token->USUARIOMODIFICACION = $usmoName;
  3356. }
  3357. $keyStr = $token->IDSCADA . "|" . $token->TOKEN;
  3358. $keyEnc = $this->encryptionController->encrypt($keyStr);
  3359. unset($token->IDSCADA);
  3360. unset($token->TOKEN);
  3361. $token->KEY = $keyEnc;
  3362. }
  3363. $now = $this->functionsController->now();
  3364. $nowStr = $now->toDateTimeString();
  3365. $actions = DB::getQueryLog();
  3366. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3367. $idac = $this->functionsController->registerActivity(
  3368. $line,
  3369. 'S002V01M01ADSI',
  3370. 'S002V01F06ASWA',
  3371. 'S002V01P03ADTO',
  3372. 'Consulta',
  3373. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los tokens generados.",
  3374. $idUser,
  3375. $nowStr
  3376. );
  3377. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  3378. return $this->responseController->makeResponse(false, 'EXITO', $tokens);
  3379. }
  3380. public function revokeToken(Request $request){
  3381. DB::enableQueryLog();
  3382. $validator = Validator::make($request->all(), [
  3383. 'id_user' => 'required|string',
  3384. 'token_key' => 'required|string',
  3385. 'linea' => 'required|integer',
  3386. ]);
  3387. if($validator->fails()){
  3388. return $this->responseController->makeResponse(
  3389. true,
  3390. "Se encontraron uno o más errores.",
  3391. $this->responseController->makeErrors(
  3392. $validator->errors()->messages()
  3393. ),
  3394. 401
  3395. );
  3396. }
  3397. $form = $request->all();
  3398. $idUser = $this->encryptionController->decrypt($form['id_user']);
  3399. if(!$idUser){
  3400. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  3401. }
  3402. $usr = DB::table('S002V01TUSUA')->where([
  3403. ['USUA_IDUS', '=', $idUser],
  3404. ['USUA_NULI', '=', $form['linea']]
  3405. ])->first();
  3406. if(is_null($usr)){
  3407. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  3408. }
  3409. $tokenKey = $this->encryptionController->decrypt($form['token_key']);
  3410. if(!$tokenKey){
  3411. return $this->responseController->makeResponse(true, 'La llave del token solicitado no fue encriptado correctamente.', [], 400);
  3412. }
  3413. $keyArr = explode("|", $tokenKey);
  3414. $token = DB::table('S002V01TTASW')->where([
  3415. ['TASW_NULI', '=', $form['linea']],
  3416. ['TASW_IDSC', '=', $keyArr[0]],
  3417. ['TASW_TOKE', '=', $keyArr[1]]
  3418. ])->first();
  3419. if(is_null($token)){
  3420. return $this->responseController->makeResponse(true, 'El token solicitado no existe.', [], 404);
  3421. }
  3422. $now = $this->functionsController->now();
  3423. $nowStr = $now->toDateTimeString();
  3424. DB::table('S002V01TTASW')->where([
  3425. ['TASW_NULI', '=', $form['linea']],
  3426. ['TASW_IDSC', '=', $keyArr[0]],
  3427. ['TASW_TOKE', '=', $keyArr[1]]
  3428. ])->update([
  3429. 'TASW_ESTA' => 'Revocado',
  3430. 'TASW_USMO' => $idUser,
  3431. 'TASW_FEMO' => $nowStr,
  3432. ]);
  3433. $actions = DB::getQueryLog();
  3434. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3435. $idac = $this->functionsController->registerActivity(
  3436. $form['linea'],
  3437. 'S002V01M01ADSI',
  3438. 'S002V01F06ASWA',
  3439. 'S002V01P03ADTO',
  3440. 'Actualización',
  3441. "El usuario $name (" . $usr->USUA_IDUS . ") revocó el token \"$keyArr[1]\".",
  3442. $idUser,
  3443. $nowStr
  3444. );
  3445. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  3446. return $this->responseController->makeResponse(false, 'EXITO.');
  3447. }
  3448. public function registerWebService(Request $request){
  3449. DB::enableQueryLog();
  3450. $validator = Validator::make($request->all(), [
  3451. 'id_user' => 'required|string',
  3452. 'linea' => 'required|integer',
  3453. 'url' => 'required|string|max:250',
  3454. 'description' => 'required|string|max:100',
  3455. ]);
  3456. if($validator->fails()){
  3457. return $this->responseController->makeResponse(
  3458. true,
  3459. "Se encontraron uno o más errores.",
  3460. $this->responseController->makeErrors(
  3461. $validator->errors()->messages()
  3462. ),
  3463. 401
  3464. );
  3465. }
  3466. $form = $request->all();
  3467. $idUser = $this->encryptionController->decrypt($form['id_user']);
  3468. if(!$idUser){
  3469. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  3470. }
  3471. $usr = DB::table('S002V01TUSUA')->where([
  3472. ['USUA_NULI', '=', $form['linea']],
  3473. ['USUA_IDUS', '=', $idUser]
  3474. ])->first();
  3475. if(is_null($usr)){
  3476. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  3477. }
  3478. $now = $this->functionsController->now();
  3479. $nowStr = $now->toDateTimeString();
  3480. $idWebService = DB::table('S002V01TLSWE')->insertGetId([
  3481. 'LSWE_NULI' => $form['linea'],
  3482. 'LSWE_URLX' => $form['url'],
  3483. 'LSWE_DESC' => $form['description'],
  3484. 'LSWE_USRE' => $idUser,
  3485. 'LSWE_FERE' => $nowStr
  3486. ]);
  3487. $actions = DB::getQueryLog();
  3488. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3489. $idac = $this->functionsController->registerActivity(
  3490. $form['linea'],
  3491. 'S002V01M01ADSI',
  3492. 'S002V01F06ASWA',
  3493. 'S002V01P07RESW',
  3494. 'Registro',
  3495. "El usuario $name (" . $usr->USUA_IDUS . ") registró el servicio web $form[url] ($idWebService).",
  3496. $idUser,
  3497. $nowStr
  3498. );
  3499. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  3500. return $this->responseController->makeResponse(false, 'EXITO.');
  3501. }
  3502. public function getWebServices($idUser, $line){
  3503. DB::enableQueryLog();
  3504. $idUser = $this->encryptionController->decrypt($idUser);
  3505. if(!$idUser){
  3506. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  3507. }
  3508. $usr = DB::table('S002V01TUSUA')->where([
  3509. ['USUA_IDUS', '=', $idUser],
  3510. ['USUA_NULI', '=', $line]
  3511. ])->first();
  3512. if(is_null($usr)){
  3513. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  3514. }
  3515. $webServices = DB::table('S002V01TLSWE')->where([
  3516. ['LSWE_NULI', '=', $line]
  3517. ])->select([
  3518. 'LSWE_IDSW AS IDSERVICIO',
  3519. 'LSWE_URLX AS URL',
  3520. 'LSWE_DESC AS DESCRIPCION',
  3521. 'LSWE_ESTA AS ESTADO',
  3522. 'LSWE_USRE AS USUREG',
  3523. 'LSWE_FERE AS FECREG',
  3524. 'LSWE_USMO AS USUMOD',
  3525. 'LSWE_FEMO AS FECMOD'
  3526. ])->get()->all();
  3527. foreach($webServices as $webService){
  3528. $usre = DB::table('S002V01TUSUA')->where([
  3529. ['USUA_NULI', '=', $line],
  3530. ['USUA_IDUS', '=', $webService->USUREG],
  3531. ])->first();
  3532. $usreName = $this->functionsController->joinName($usre->USUA_NOMB, $usre->USUA_APPA, $usre->USUA_APMA) . " (" . $webService->USUREG . ")";
  3533. $webService->USUREG = $usreName;
  3534. $usmo = DB::table('S002V01TUSUA')->where([
  3535. ['USUA_NULI', '=', $line],
  3536. ['USUA_IDUS', '=', $webService->USUMOD],
  3537. ])->first();
  3538. if(!is_null($usmo)){
  3539. $usmoName = $this->functionsController->joinName($usmo->USUA_NOMB, $usmo->USUA_APPA, $usmo->USUA_APMA) . " (" . $webService->USUMOD . ")";
  3540. $webService->USUMOD = $usmoName;
  3541. }
  3542. $vinculos = DB::table('S002V01TTASW')->where([
  3543. ['TASW_NULI', '=', $line],
  3544. ['TASW_IDSW', '=', $webService->IDSERVICIO],
  3545. ])->get()->all();
  3546. $webService->VINCULOS = count($vinculos);
  3547. }
  3548. $now = $this->functionsController->now();
  3549. $actions = DB::getQueryLog();
  3550. $nowStr = $now->toDateTimeString();
  3551. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3552. $idac = $this->functionsController->registerActivity(
  3553. $line,
  3554. 'S002V01M01ADSI',
  3555. 'S002V01F06ASWA',
  3556. 'S002V01P06LSWE',
  3557. 'Consulta',
  3558. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los servicios web almacenados.",
  3559. $idUser,
  3560. $nowStr
  3561. );
  3562. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  3563. return $this->responseController->makeResponse(false, 'EXITO', $webServices);
  3564. }
  3565. public function getRoutedTokens($idUser, $line){
  3566. DB::enableQueryLog();
  3567. $idUser = $this->encryptionController->decrypt($idUser);
  3568. if(!$idUser){
  3569. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  3570. }
  3571. $usr = DB::table('S002V01TUSUA')->where([
  3572. ['USUA_IDUS', '=', $idUser],
  3573. ['USUA_NULI', '=', $line]
  3574. ])->first();
  3575. if(is_null($usr)){
  3576. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  3577. }
  3578. $list = DB::table('S002V01TTASW')
  3579. ->join('S002V01TLISC', 'LISC_IDSC', '=', 'TASW_IDSC')
  3580. ->leftJoin('S002V01TLSWE', 'LSWE_IDSW', '=', 'TASW_IDSW')
  3581. ->where([
  3582. ['TASW_NULI', '=', $line],
  3583. ['TASW_ESTA', '!=', 'Revocado'],
  3584. ])->get()->all();
  3585. $arrList = [];
  3586. foreach($list as $val){
  3587. $key = $this->encryptionController->encrypt($val->TASW_IDSC . "|" . $val->TASW_TOKE);
  3588. $arrList[] = [
  3589. 'SCADA' => $val->LISC_NOSC . " (" . $val->LISC_IDSC . ")",
  3590. 'DESCRIPCIONSC' => $val->TASW_DETO,
  3591. 'SERVICIOWEB' => is_null($val->LSWE_IDSW) ? 'No asignado' : $val->LSWE_URLX . " (" . $val->LSWE_IDSW . ")",
  3592. 'DESCRIPCIONSW' => $val->LSWE_DESC,
  3593. 'ESTADO' => $val->TASW_ESTA,
  3594. "KEY" => $key
  3595. ];
  3596. }
  3597. $now = $this->functionsController->now();
  3598. $nowStr = $now->toDateTimeString();
  3599. $actions = DB::getQueryLog();
  3600. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3601. $idac = $this->functionsController->registerActivity(
  3602. $line,
  3603. 'S002V01M01ADSI',
  3604. 'S002V01F06ASWA',
  3605. 'S002V01P05ESSW',
  3606. 'Consulta',
  3607. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el enrutamiento de los SCADA a los servicios web.",
  3608. $idUser,
  3609. $nowStr
  3610. );
  3611. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  3612. return $this->responseController->makeResponse(false, 'EXITO', $arrList);
  3613. }
  3614. public function changeTokenAccess(Request $request){
  3615. DB::enableQueryLog();
  3616. $validator = Validator::make($request->all(), [
  3617. 'id_user' => 'required|string',
  3618. 'linea' => 'required|integer',
  3619. 'estado' => 'required|string|in:Activo,Inactivo',
  3620. 'key' => 'required|string',
  3621. ]);
  3622. if($validator->fails()){
  3623. return $this->responseController->makeResponse(
  3624. true,
  3625. "Se encontraron uno o más errores.",
  3626. $this->responseController->makeErrors(
  3627. $validator->errors()->messages()
  3628. ),
  3629. 401
  3630. );
  3631. }
  3632. $form = $request->all();
  3633. $idUser = $this->encryptionController->decrypt($form['id_user']);
  3634. if(!$idUser){
  3635. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  3636. }
  3637. $usr = DB::table('S002V01TUSUA')->where([
  3638. ['USUA_NULI', '=', $form['linea']],
  3639. ['USUA_IDUS', '=', $idUser]
  3640. ])->first();
  3641. if(is_null($usr)){
  3642. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  3643. }
  3644. $keyStr = $this->encryptionController->decrypt($form['key']);
  3645. if(!$keyStr){
  3646. return $this->responseController->makeResponse(true, 'La llave del token no fue encriptada correctamente.', [], 400);
  3647. }
  3648. $keyArr = explode('|', $keyStr);
  3649. if(count($keyArr) != 2){
  3650. return $this->responseController->makeResponse(true, 'La llave del token no tiene un formato correcto.', [], 400);
  3651. }
  3652. $now = $this->functionsController->now();
  3653. $nowStr = $now->toDateTimeString();
  3654. DB::table('S002V01TTASW')->where([
  3655. ['TASW_NULI', '=', $form['linea']],
  3656. ['TASW_IDSC', '=', $keyArr[0]],
  3657. ['TASW_TOKE', '=', $keyArr[1]],
  3658. ])->update([
  3659. 'TASW_ESTA' => $form['estado'],
  3660. 'TASW_TIMO' => 'Cambio de estado del token',
  3661. 'TASW_USMO' => $idUser,
  3662. 'TASW_FEMO' => $nowStr,
  3663. ]);
  3664. $actions = DB::getQueryLog();
  3665. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3666. $idac = $this->functionsController->registerActivity(
  3667. $form['linea'],
  3668. 'S002V01M01ADSI',
  3669. 'S002V01F06ASWA',
  3670. 'S002V01P05ESSW',
  3671. 'Actualización',
  3672. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó el estado de un token.",
  3673. $idUser,
  3674. $nowStr
  3675. );
  3676. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  3677. return $this->responseController->makeResponse(false, 'EXITO.');
  3678. }
  3679. public function changeWebService(Request $request){
  3680. DB::enableQueryLog();
  3681. $validator = Validator::make($request->all(), [
  3682. 'id_user' => 'required|string',
  3683. 'linea' => 'required|integer',
  3684. 'key' => 'required|string',
  3685. 'id_web_service' => 'required|string',
  3686. ]);
  3687. if($validator->fails()){
  3688. return $this->responseController->makeResponse(
  3689. true,
  3690. "Se encontraron uno o más errores.",
  3691. $this->responseController->makeErrors(
  3692. $validator->errors()->messages()
  3693. ),
  3694. 401
  3695. );
  3696. }
  3697. $form = $request->all();
  3698. $idUser = $this->encryptionController->decrypt($form['id_user']);
  3699. if(!$idUser){
  3700. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  3701. }
  3702. $usr = DB::table('S002V01TUSUA')->where([
  3703. ['USUA_NULI', '=', $form['linea']],
  3704. ['USUA_IDUS', '=', $idUser]
  3705. ])->first();
  3706. if(is_null($usr)){
  3707. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  3708. }
  3709. $keyToken = $this->encryptionController->decrypt($form['key']);
  3710. if(!$keyToken){
  3711. return $this->responseController->makeResponse(true, 'La llave del token no está encriptada correctamente.', [], 400);
  3712. }
  3713. $keyArr = explode('|', $keyToken);
  3714. $token = DB::table('S002V01TTASW')->where([
  3715. ['TASW_NULI', '=', $form['linea']],
  3716. ['TASW_IDSC', '=', $keyArr[0]],
  3717. ['TASW_TOKE', '=', $keyArr[1]],
  3718. ])->first();
  3719. if(is_null($token)){
  3720. return $this->responseController->makeResponse(true, 'El token solicitado no existe.', [], 404);
  3721. }
  3722. $idWS = $this->encryptionController->decrypt($form['id_web_service']);
  3723. if(!$idWS){
  3724. return $this->responseController->makeResponse(true, 'El ID del servicio web no fue encriptado correctamente.', [], 400);
  3725. }
  3726. $webService = DB::table('S002V01TLSWE')->where([
  3727. ['LSWE_NULI', '=', $form['linea']],
  3728. ['LSWE_IDSW', '=', $idWS]
  3729. ])->first();
  3730. if(is_null($webService)){
  3731. return $this->responseController->makeResponse(true, 'El servicio web solicitado no existe', [], 404);
  3732. }
  3733. $now = $this->functionsController->now();
  3734. $nowStr = $now->toDateTimeString();
  3735. DB::table('S002V01TTASW')->where([
  3736. ['TASW_NULI', '=', $form['linea']],
  3737. ['TASW_IDSC', '=', $keyArr[0]],
  3738. ['TASW_TOKE', '=', $keyArr[1]],
  3739. ])->update([
  3740. 'TASW_IDSW' => $idWS,
  3741. 'TASW_TIMO' => 'Cambio de servicio web',
  3742. 'TASW_USMO' => $idUser,
  3743. 'TASW_FEMO' => $nowStr,
  3744. ]);
  3745. $actions = DB::getQueryLog();
  3746. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3747. $idac = $this->functionsController->registerActivity(
  3748. $form['linea'],
  3749. 'S002V01M01ADSI',
  3750. 'S002V01F06ASWA',
  3751. 'S002V01P05ESSW',
  3752. 'Actualización',
  3753. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó el servicio web de un SCADA.",
  3754. $idUser,
  3755. $nowStr
  3756. );
  3757. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  3758. return $this->responseController->makeResponse(false, 'EXITO.');
  3759. }
  3760. public function updateWebService(Request $request){
  3761. DB::enableQueryLog();
  3762. $validator = Validator::make($request->all(), [
  3763. 'id_user' => 'required|string',
  3764. 'linea' => 'required|integer',
  3765. 'id_web_service' => 'required|string',
  3766. 'url' => 'required|string|max:250',
  3767. 'description' => 'required|string|max:100',
  3768. ]);
  3769. if($validator->fails()){
  3770. return $this->responseController->makeResponse(
  3771. true,
  3772. "Se encontraron uno o más errores.",
  3773. $this->responseController->makeErrors(
  3774. $validator->errors()->messages()
  3775. ),
  3776. 401
  3777. );
  3778. }
  3779. $form = $request->all();
  3780. $idUser = $this->encryptionController->decrypt($form['id_user']);
  3781. if(!$idUser){
  3782. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  3783. }
  3784. $usr = DB::table('S002V01TUSUA')->where([
  3785. ['USUA_NULI', '=', $form['linea']],
  3786. ['USUA_IDUS', '=', $idUser]
  3787. ])->first();
  3788. if(is_null($usr)){
  3789. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  3790. }
  3791. $idWS = $this->encryptionController->decrypt($form['id_web_service']);
  3792. if(!$idWS){
  3793. return $this->responseController->makeResponse(true, 'El ID del servicio web no fue encriptado correctamente.', [], 400);
  3794. }
  3795. $now = $this->functionsController->now();
  3796. $nowStr = $now ->toDateTimeString();
  3797. DB::table('S002V01TLSWE')->where([
  3798. ['LSWE_IDSW', '=', $idWS],
  3799. ['LSWE_NULI', '=', $form['linea']]
  3800. ])->update([
  3801. 'LSWE_URLX' => $form['url'],
  3802. 'LSWE_DESC' => $form['description'],
  3803. 'LSWE_USMO' => $idUser,
  3804. 'LSWE_FEMO' => $nowStr,
  3805. ]);
  3806. $actions = DB::getQueryLog();
  3807. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3808. $idac = $this->functionsController->registerActivity(
  3809. $form['linea'],
  3810. 'S002V01M01ADSI',
  3811. 'S002V01F06ASWA',
  3812. 'S002V01P07RESW',
  3813. 'Actualización',
  3814. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó el servicio web $form[url] ($idWS).",
  3815. $idUser,
  3816. $nowStr
  3817. );
  3818. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  3819. return $this->responseController->makeResponse(false, 'EXITO.');
  3820. }
  3821. public function changeWSstatus(Request $request){
  3822. DB::enableQueryLog();
  3823. $validator = Validator::make($request->all(), [
  3824. 'id_user' => 'required|string',
  3825. 'linea' => 'required|integer',
  3826. 'id_web_service' => 'required|string',
  3827. 'status' => 'required|string|in:Activo,Inactivo',
  3828. ]);
  3829. if($validator->fails()){
  3830. return $this->responseController->makeResponse(
  3831. true,
  3832. "Se encontraron uno o más errores.",
  3833. $this->responseController->makeErrors(
  3834. $validator->errors()->messages()
  3835. ),
  3836. 401
  3837. );
  3838. }
  3839. $form = $request->all();
  3840. $idUser = $this->encryptionController->decrypt($form['id_user']);
  3841. if(!$idUser){
  3842. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  3843. }
  3844. $usr = DB::table('S002V01TUSUA')->where([
  3845. ['USUA_NULI', '=', $form['linea']],
  3846. ['USUA_IDUS', '=', $idUser]
  3847. ])->first();
  3848. if(is_null($usr)){
  3849. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  3850. }
  3851. $idWS = $this->encryptionController->decrypt($form['id_web_service']);
  3852. if(!$idWS){
  3853. return $this->responseController->makeResponse(true, 'El ID del servicio web no fue encriptado correctamente.', [], 400);
  3854. }
  3855. $webService = DB::table('S002V01TLSWE')->where([
  3856. ['LSWE_NULI', '=', $form['linea']],
  3857. ['LSWE_IDSW', '=', $idWS]
  3858. ])->first();
  3859. if(is_null($webService)){
  3860. return $this->responseController->makeResponse(true, 'El servicio web solicitado no existe.', [], 404);
  3861. }
  3862. $now = $this->functionsController->now();
  3863. $nowStr = $now->toDateTimeString();
  3864. DB::table('S002V01TLSWE')->where([
  3865. ['LSWE_NULI', '=', $form['linea']],
  3866. ['LSWE_IDSW', '=', $idWS],
  3867. ])->update([
  3868. 'LSWE_ESTA' => $form['status'],
  3869. 'LSWE_USMO' => $idUser,
  3870. 'LSWE_FEMO' => $nowStr,
  3871. ]);
  3872. $actions = DB::getQueryLog();
  3873. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3874. $idac = $this->functionsController->registerActivity(
  3875. $form['linea'],
  3876. 'S002V01M01ADSI',
  3877. 'S002V01F06ASWA',
  3878. 'S002V01P07RESW',
  3879. 'Actualización',
  3880. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó el estado servicio web de un SCADA.",
  3881. $idUser,
  3882. $nowStr
  3883. );
  3884. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  3885. return $this->responseController->makeResponse(false, 'EXITO.');
  3886. }
  3887. public function getBackupsList($idUser, $line){
  3888. DB::enableQueryLog();
  3889. $idUser = $this->encryptionController->decrypt($idUser);
  3890. if(!$idUser){
  3891. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  3892. }
  3893. $usr = DB::table('S002V01TUSUA')->where([
  3894. ['USUA_IDUS', '=', $idUser],
  3895. ['USUA_NULI', '=', $line]
  3896. ])->first();
  3897. if(is_null($usr)){
  3898. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  3899. }
  3900. $backups = DB::table('S002V01TRESP')->select([
  3901. 'RESP_IDRE AS IDRESPALDO',
  3902. 'RESP_FERE AS FECHARESPALDO',
  3903. 'RESP_ESTA AS ESTADO',
  3904. 'RESP_LORE AS LOG',
  3905. ])->where('RESP_NULI', '=', $line)->get()->all();
  3906. $now = $this->functionsController->now();
  3907. $nowStr = $now->toDateTimeString();
  3908. $actions = DB::getQueryLog();
  3909. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3910. $idac = $this->functionsController->registerActivity(
  3911. $line,
  3912. 'S002V01M01ADSI',
  3913. 'S002V01F12GERE',
  3914. 'S002V01P01HRES',
  3915. 'Consulta',
  3916. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la lista de respaldos.",
  3917. $idUser,
  3918. $nowStr
  3919. );
  3920. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  3921. return $this->responseController->makeResponse(false, 'EXITO', $backups);
  3922. }
  3923. public function getSubmoduleFunctions($idMod, $idSub, $idUser, $line){
  3924. DB::enableQueryLog();
  3925. $idUser = $this->encryptionController->decrypt($idUser);
  3926. if(!$idUser){
  3927. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  3928. }
  3929. $usr = DB::table('S002V01TUSUA')->where([
  3930. ['USUA_IDUS', '=', $idUser],
  3931. ['USUA_NULI', '=', $line]
  3932. ])->first();
  3933. if(is_null($usr)){
  3934. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  3935. }
  3936. $idMod = $this->encryptionController->decrypt($idMod);
  3937. if(!$idMod){
  3938. return $this->responseController->makeResponse(true, 'El ID del módulo no está encriptado correctamente', [], 400);
  3939. }
  3940. $mod = DB::table('S002V01TMODU')->where([
  3941. ['MODU_NULI', '=', $line],
  3942. ['MODU_IDMO', '=', $idMod]
  3943. ])->first();
  3944. if(is_null($mod)){
  3945. return $this->responseController->makeResponse(true, 'El módulo enviado no existe', [], 404);
  3946. }
  3947. $idSub = $this->encryptionController->decrypt($idSub);
  3948. if(!$idSub){
  3949. return $this->responseController->makeResponse(true, 'El ID del submódulo no está encriptado correctamente', [], 400);
  3950. }
  3951. $sub = DB::table('S002V01TSUBM')->where([
  3952. ['SUBM_NULI', '=', $line],
  3953. ['SUBM_IDSM', '=', $idSub],
  3954. ['SUBM_IDMO', '=', $idMod],
  3955. ])->first();
  3956. if(is_null($sub)){
  3957. return $this->responseController->makeResponse(true, 'El submódulo enviado no existe', [], 404);
  3958. }
  3959. $functions = DB::table('S002V01TFUNC')->select([
  3960. 'FUNC_IDFU AS IDFUNCION',
  3961. 'FUNC_NOMB AS NOMBREFUNCION',
  3962. 'FUNC_ICON AS ICONOFUNCION',
  3963. ])->where([
  3964. ['FUNC_NULI', '=', $line],
  3965. ['FUNC_IDMO', '=', $idMod],
  3966. ['FUNC_IDSM', '=', $idSub],
  3967. ])->get()->all();
  3968. foreach($functions as $function){
  3969. $screens = DB::table('S002V01TPANT')->where([
  3970. ['PANT_NULI', '=', $line],
  3971. ['PANT_IDFU', '=', $function->IDFUNCION],
  3972. ])->get()->all();
  3973. $function->HASSCREENS = count($screens) > 0;
  3974. }
  3975. $now = $this->functionsController->now();
  3976. $nowStr = $now->toDateTimeString();
  3977. $actions = DB::getQueryLog();
  3978. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3979. $idac = $this->functionsController->registerActivity(
  3980. $line,
  3981. '-',
  3982. '-',
  3983. '-',
  3984. 'Consulta',
  3985. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las funciones del submódulo " . $sub->SUBM_NOMB . " ($idSub).",
  3986. $idUser,
  3987. $nowStr
  3988. );
  3989. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  3990. return $this->responseController->makeResponse(false, 'EXITO', $functions);
  3991. }
  3992. public function getTaxInformation($line){
  3993. try {
  3994. $getTaxInformation = DB::table('S002V01TINFI')
  3995. ->where('INFI_NULI','=',$line)
  3996. ->where('INFI_ESTA','=','Activo')
  3997. ->first([
  3998. 'INFI_IDIN',
  3999. 'INFI_NOFI',
  4000. 'INFI_XRFC',
  4001. 'INFI_COEL',
  4002. 'INFI_CORF',
  4003. 'INFI_LAD1',
  4004. 'INFI_TEL1',
  4005. 'INFI_LAD2',
  4006. 'INFI_TEL2',
  4007. 'INFI_COPO',
  4008. 'INFI_TIVI',
  4009. 'INFI_VIAL',
  4010. 'INFI_ENCA',
  4011. 'INFI_YCAL',
  4012. 'INFI_NUEX',
  4013. 'INFI_NUIN',
  4014. 'INFI_COLO',
  4015. 'INFI_LOCA',
  4016. 'INFI_MUNI',
  4017. 'INFI_ENFE',
  4018. 'INFI_PAIS',
  4019. 'INFI_USRE',
  4020. 'INFI_FERE',
  4021. 'INFI_USMO',
  4022. 'INFI_FEMO',
  4023. ]);
  4024. } catch (\Throwable $th) {
  4025. DB::rollBack();
  4026. return $this->responseController->makeResponse(true, "ERR_SYSTEM_ADMINISTRATOR_GET_TAX_INFORMATION000: Ocurrió un error al obtener la información fiscal.", [], 401);
  4027. }
  4028. return $this->responseController->makeResponse(false, 'EXITO', $getTaxInformation);
  4029. }
  4030. public function getCountriesList($idUser, $line) {
  4031. DB::enableQueryLog();
  4032. $idUser = $this->encryptionController->decrypt($idUser);
  4033. if(!$idUser){
  4034. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  4035. }
  4036. $usr = DB::table('S002V01TUSUA')->where([
  4037. ['USUA_NULI', '=', $line],
  4038. ['USUA_IDUS', '=', $idUser],
  4039. ])->first();
  4040. if(is_null($usr)){
  4041. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  4042. }
  4043. $catalogue = DB::table('S002V01TCATA')->where([
  4044. ['CATA_TICA', '=', 'Países'],
  4045. ['CATA_ESTA', '=', 'Activo'],
  4046. ['CATA_NULI', '=', $line],
  4047. ])->orderBy('CATA_VERS', 'desc')->first();
  4048. if(is_null($catalogue)){
  4049. return $this->responseController->makeResponse(true, 'Aún no se ha registrado ningún catálogo de paises', [], 404);
  4050. }
  4051. $filesArr = json_decode($catalogue->CATA_ARRE, true);
  4052. $fileDec = $this->encryptionController->decrypt($filesArr[0]);
  4053. if(!$fileDec){
  4054. return $this->responseController->makeResponse(true, 'El ID catálogo vigente de países no fue encriptado correctamente.', [], 400);
  4055. }
  4056. $fileArr = explode("=", $fileDec);
  4057. $idArr = explode("-", $fileArr[0]);
  4058. $file = DB::table('S002V01TAFAL')->where([
  4059. ['AFAL_NULI', '=', $line],
  4060. ['AFAL_COMO', '=', $idArr[1]],
  4061. ['AFAL_CLDO', '=', $idArr[2]],
  4062. ['AFAL_FECR', '=', $idArr[3]],
  4063. ['AFAL_NUSE', '=', $idArr[4]],
  4064. ['AFAL_NUVE', '=', $fileArr[1]],
  4065. ])->first();
  4066. if(is_null($file)){
  4067. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de países no está registrado.', [], 404);
  4068. }else if($file->AFAL_ESTA == 'Eliminado'){
  4069. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de países está eliminado.', [], 404);
  4070. }else if(!file_exists($file->AFAL_UBIC)){
  4071. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de países no existe en el almacenamiento final.', [], 404);
  4072. }
  4073. $spreadsheet = IOFactory::load($file->AFAL_UBIC);
  4074. $sheetNames = $spreadsheet->getSheetNames();
  4075. if(!in_array('c_Pais', $sheetNames)){
  4076. return $this->responseController->makeResponse(true, 'El archivo asociado no contiene la hoja c_Pais.', [], 400);
  4077. }
  4078. $worksheet = $spreadsheet->getActiveSheet();
  4079. $fileValidatorCell = $worksheet->getCell('A5')->getValue();
  4080. if($fileValidatorCell != 'c_Pais'){
  4081. return $this->responseController->makeResponse(true, 'El archivo asociado no tiene un formato válido.', [], 400);
  4082. }
  4083. $countries = [];
  4084. $maxRow = $worksheet->getHighestRow();
  4085. for($row = 6; $row <= $maxRow; $row++){
  4086. $countryKey = $worksheet->getCell("A$row")->getValue();
  4087. $countryName = $worksheet->getCell("B$row")->getValue();
  4088. $countryInfo = DB::table('S002V01TPAIS')->where([
  4089. ['PAIS_NULI', '=', $line],
  4090. ['PAIS_IDPA', '=', $countryKey],
  4091. ])->first();
  4092. $countries[] = [
  4093. 'COUNTRY_ID' => $countryKey,
  4094. 'NAME' => $countryName,
  4095. 'LADA' => is_null($countryInfo) ? null : $countryInfo->PAIS_LADA,
  4096. 'NOMECLARUTA_ISO2' => is_null($countryInfo) ? null : $countryInfo->PAIS_NOME
  4097. ];
  4098. }
  4099. $now = $this->functionsController->now();
  4100. $nowStr = $now->toDateTimeString();
  4101. $actions = DB::getQueryLog();
  4102. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4103. $idac = $this->functionsController->registerActivity(
  4104. $line,
  4105. 'S002V01M01ADSI',
  4106. 'S002V01F10AGCA',
  4107. 'S002V01P01HICA',
  4108. 'Consulta',
  4109. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el contenido del catálogo de Países.",
  4110. $idUser,
  4111. $nowStr
  4112. );
  4113. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4114. return $this->responseController->makeResponse(false, 'EXITO', $countries);
  4115. }
  4116. public function saveCatalogueConfig(Request $request) {
  4117. DB::enableQueryLog();
  4118. $validator = Validator::make($request->all(), [
  4119. 'NOMBRE_FISCAL' => 'required|string',
  4120. 'RFC' => 'required|string',
  4121. 'CORREO_ELECTRONICO' => 'required|string',
  4122. 'REGIMEN_FISCAL' => 'required|string',
  4123. 'LADA1' => 'required|string',
  4124. 'TELEFONO_FIJO' => 'required|integer',
  4125. 'LADA2' => 'required|string',
  4126. 'TELEFONO_MOVIL' => 'required|integer',
  4127. 'CODIGO_POSTAL' => 'required|integer',
  4128. 'TIPOVIALIDAD' => 'required|string',
  4129. 'NOMBRE_VIALIDAD' => 'required|string',
  4130. 'ENTRE_CALLE' => 'required|string',
  4131. 'Y_CALLE' => 'required|string',
  4132. 'NUMERO_EXTERIOR' => 'required|string',
  4133. // 'NUMERO_INTERIOR' => 'required|string',
  4134. 'COLONIA' => 'required|string',
  4135. 'LOCALIDAD' => 'required|string',
  4136. 'MUNICIPIO' => 'required|string',
  4137. 'ENTIDAD_FEDERATIVA' => 'required|string',
  4138. 'PAIS' => 'required|string',
  4139. 'USER' => 'required|string',
  4140. 'LINE_NUMBER' => 'required|integer',
  4141. 'id_user' => 'required|string',
  4142. 'linea' => 'required|integer',
  4143. 'catalogue_type' => 'required|string',
  4144. 'config' => 'required|json',
  4145. ]);
  4146. if($validator->fails()){
  4147. return $this->responseController->makeResponse(
  4148. true,
  4149. "Se encontraron uno o más errores.",
  4150. $this->responseController->makeErrors(
  4151. $validator->errors()->messages()
  4152. ),
  4153. 401
  4154. );
  4155. }
  4156. $form = $request->all();
  4157. $idUser = $this->encryptionController->decrypt($form['id_user']);
  4158. if(!$idUser){
  4159. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  4160. }
  4161. $usr = DB::table('S002V01TUSUA')->where([
  4162. ['USUA_NULI', '=', $form['linea']],
  4163. ['USUA_IDUS', '=', $idUser]
  4164. ])->first();
  4165. if(is_null($usr)){
  4166. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  4167. }
  4168. $allowedFiles = ["Colonias", "Códigos postales", "Estados", "Localidades", "Municipios", "Países", "Régimen fiscal"];
  4169. if(!in_array($form['catalogue_type'], $allowedFiles)){
  4170. return $this->responseController->makeResponse(true, "El tipo de catálogo no está soportado.", [], 400);
  4171. }
  4172. $catalogue = DB::table('S002V01TCATA')->where([
  4173. ['CATA_TICA', '=', $form['catalogue_type']],
  4174. ['CATA_ESTA', '=', 'Activo'],
  4175. ['CATA_NULI', '=', $form['linea']],
  4176. ])->orderBy('CATA_VERS', 'desc')->first();
  4177. if(is_null($catalogue)){
  4178. return $this->responseController->makeResponse(true, "Aún no se ha registrado ningún catálogo de $form[catalogue_type]", [], 404);
  4179. }
  4180. $now = $this->functionsController->now();
  4181. $nowStr = $now->toDateTimeString();
  4182. switch($form['catalogue_type']){
  4183. case "Países":
  4184. $configArr = json_decode($form['config'], true);
  4185. foreach($configArr as $val){
  4186. $country = DB::table('S002V01TPAIS')->where([
  4187. ['PAIS_NULI', '=', $form['linea']],
  4188. ['PAIS_IDPA', '=', $val['COUNTRY_ID']],
  4189. ])->first();
  4190. if(is_null($val['COUNTRY_ID'])) continue;
  4191. if(is_null($country)){
  4192. DB::table('S002V01TPAIS')->insert([
  4193. 'PAIS_NULI' => $form['linea'],
  4194. 'PAIS_IDPA' => $val['COUNTRY_ID'],
  4195. 'PAIS_NOMB' => $val['NAME'],
  4196. 'PAIS_NOME' => $val['NOMECLARUTA_ISO2'],
  4197. 'PAIS_LADA' => $val['LADA'],
  4198. 'PAIS_USRE' => $idUser,
  4199. 'PAIS_FERE' => $nowStr,
  4200. ]);
  4201. }else{
  4202. DB::table('S002V01TPAIS')->where([
  4203. ['PAIS_NULI', '=', $form['linea']],
  4204. ['PAIS_IDPA', '=', $val['COUNTRY_ID']],
  4205. ])->update([
  4206. 'PAIS_NOME' => $val['NOMECLARUTA_ISO2'],
  4207. 'PAIS_LADA' => $val['LADA'],
  4208. 'PAIS_USMO' => $idUser,
  4209. 'PAIS_FEMO' => $nowStr,
  4210. ]);
  4211. }
  4212. }
  4213. break;
  4214. case "Códigos postales":
  4215. $configArr = json_decode($form['config'], true);
  4216. foreach($configArr as $val){
  4217. $zipCode = DB::table('S002V01TCOPO')->where([
  4218. ['COPO_NULI', '=', $form['linea']],
  4219. ['COPO_COPO', '=', $val['CODIGO_POSTAL']],
  4220. ['COPO_COES', '=', $val['ESTADO']],
  4221. ])->first();
  4222. if(is_null($val['CODIGO_POSTAL'])) continue;
  4223. if(is_null($zipCode)){
  4224. DB::table('S002V01TCOPO')->insert([
  4225. 'COPO_NULI' => $form['linea'],
  4226. 'COPO_COPO' => $val['CODIGO_POSTAL'],
  4227. 'COPO_COES' => $val['ESTADO'],
  4228. 'COPO_COMU' => $val['MUNICIPIO'],
  4229. 'COPO_COLO' => $val['LOCALIDAD'],
  4230. 'COPO_USRE' => $idUser,
  4231. 'COPO_FERE' => $nowStr,
  4232. ]);
  4233. }else{
  4234. DB::table('S002V01TCOPO')->where([
  4235. ['COPO_NULI', '=', $form['linea']],
  4236. ['COPO_COPO', '=', $val['CODIGO_POSTAL']],
  4237. ['COPO_COES', '=', $val['ESTADO']],
  4238. ])->update([
  4239. 'COPO_COMU' => $val['MUNICIPIO'],
  4240. 'COPO_COLO' => $val['LOCALIDAD'],
  4241. 'COPO_USMO' => $idUser,
  4242. 'COPO_FEMO' => $nowStr,
  4243. ]);
  4244. }
  4245. }
  4246. break;
  4247. case "Estados":
  4248. $configArr = json_decode($form['config'], true);
  4249. foreach($configArr as $val){
  4250. $state = DB::table('S002V01TESTA')->where([
  4251. ['ESTA_NULI', '=', $form['linea']],
  4252. ['ESTA_COES', '=', $val['STATE_KEY']],
  4253. ['ESTA_COPA', '=', $val['COUNTRY_KEY']],
  4254. ])->first();
  4255. if(is_null($val['STATE_KEY'])) continue;
  4256. if(is_null($state)){
  4257. DB::table('S002V01TESTA')->insert([
  4258. 'ESTA_NULI' => $form['linea'],
  4259. 'ESTA_COES' => $val['STATE_KEY'],
  4260. 'ESTA_COPA' => $val['COUNTRY_KEY'],
  4261. 'ESTA_NOES' => $val['STATE_NAME'],
  4262. 'ESTA_USRE' => $idUser,
  4263. 'ESTA_FERE' => $nowStr,
  4264. ]);
  4265. }else{
  4266. DB::table('S002V01TESTA')->where([
  4267. ['ESTA_NULI', '=', $form['linea']],
  4268. ['ESTA_COES', '=', $val['STATE_KEY']],
  4269. ['ESTA_COPA', '=', $val['COUNTRY_KEY']],
  4270. ])->update([
  4271. 'ESTA_NOES' => $val['STATE_NAME'],
  4272. 'ESTA_USMO' => $idUser,
  4273. 'ESTA_FEMO' => $nowStr,
  4274. ]);
  4275. }
  4276. }
  4277. break;
  4278. case "Municipios":
  4279. $configArr = json_decode($form['config'], true);
  4280. foreach($configArr as $val){
  4281. $city = DB::table('S002V01TMUNI')->where([
  4282. ['MUNI_NULI', '=', $form['linea']],
  4283. ['MUNI_COMU', '=', $val['CITY_KEY']],
  4284. ['MUNI_COES', '=', $val['STATE_KEY']],
  4285. ])->first();
  4286. if(is_null($val['CITY_KEY'])) continue;
  4287. if(is_null($city)){
  4288. DB::table('S002V01TMUNI')->insert([
  4289. 'MUNI_NULI' => $form['linea'],
  4290. 'MUNI_COMU' => $val['CITY_KEY'],
  4291. 'MUNI_COES' => $val['STATE_KEY'],
  4292. 'MUNI_NOMU' => $val['CITY_NAME'],
  4293. 'MUNI_USRE' => $idUser,
  4294. 'MUNI_FERE' => $nowStr,
  4295. ]);
  4296. }else{
  4297. DB::table('S002V01TMUNI')->where([
  4298. ['MUNI_NULI', '=', $form['linea']],
  4299. ['MUNI_COMU', '=', $val['CITY_KEY']],
  4300. ['MUNI_COES', '=', $val['STATE_KEY']],
  4301. ])->update([
  4302. 'MUNI_NOMU' => $val['CITY_NAME'],
  4303. 'MUNI_USMO' => $idUser,
  4304. 'MUNI_FEMO' => $nowStr,
  4305. ]);
  4306. }
  4307. }
  4308. break;
  4309. case "Localidades":
  4310. $configArr = json_decode($form['config'], true);
  4311. foreach($configArr as $val){
  4312. $town = DB::table('S002V01TLOCA')->where([
  4313. ['LOCA_NULI', '=', $form['linea']],
  4314. ['LOCA_COLO', '=', $val['TOWN_KEY']],
  4315. ['LOCA_COES', '=', $val['STATE_KEY']],
  4316. ])->first();
  4317. if(is_null($val['TOWN_KEY'])) continue;
  4318. if(is_null($town)){
  4319. DB::table('S002V01TLOCA')->insert([
  4320. 'LOCA_NULI' => $form['linea'],
  4321. 'LOCA_COLO' => $val['TOWN_KEY'],
  4322. 'LOCA_COES' => $val['STATE_KEY'],
  4323. 'LOCA_NOLO' => $val['TOWN_NAME'],
  4324. 'LOCA_USRE' => $idUser,
  4325. 'LOCA_FERE' => $nowStr,
  4326. ]);
  4327. }else{
  4328. DB::table('S002V01TLOCA')->where([
  4329. ['LOCA_NULI', '=', $form['linea']],
  4330. ['LOCA_COLO', '=', $val['TOWN_KEY']],
  4331. ['LOCA_COES', '=', $val['CITY_NAME']],
  4332. ])->update([
  4333. 'LOCA_NOLO' => $val['CITY_NAME'],
  4334. 'LOCA_USMO' => $idUser,
  4335. 'LOCA_FEMO' => $nowStr,
  4336. ]);
  4337. }
  4338. }
  4339. break;
  4340. case "Colonias":
  4341. $configArr = json_decode($form['config'], true);
  4342. foreach($configArr as $val){
  4343. $town = DB::table('S002V01TCOLO')->where([
  4344. ['COLO_NULI', '=', $form['linea']],
  4345. ['COLO_COCO', '=', $val['SETTING_KEY']],
  4346. ['COLO_COPO', '=', $val['CODIGO_POSTAL']],
  4347. ])->first();
  4348. if(is_null($val['SETTING_KEY'])) continue;
  4349. if(is_null($town)){
  4350. DB::table('S002V01TCOLO')->insert([
  4351. 'COLO_NULI' => $form['linea'],
  4352. 'COLO_COCO' => $val['SETTING_KEY'],
  4353. 'COLO_COPO' => $val['CODIGO_POSTAL'],
  4354. 'COLO_NOCO' => $val['SETTING_NAME'],
  4355. 'COLO_USRE' => $idUser,
  4356. 'COLO_FERE' => $nowStr,
  4357. ]);
  4358. }else{
  4359. DB::table('S002V01TCOLO')->where([
  4360. ['COLO_NULI', '=', $form['linea']],
  4361. ['COLO_COCO', '=', $val['SETTING_KEY']],
  4362. ['COLO_COPO', '=', $val['CODIGO_POSTAL']],
  4363. ])->update([
  4364. 'COLO_NOLO' => $val['SETTING_NAME'],
  4365. 'COLO_USMO' => $idUser,
  4366. 'COLO_FEMO' => $nowStr,
  4367. ]);
  4368. }
  4369. }
  4370. break;
  4371. case "Régimen fiscal":
  4372. $configArr = json_decode($form['config'], true);
  4373. foreach($configArr as $val){
  4374. $taxRegime = DB::table('S002V01TREFI')->where([
  4375. ['REFI_NULI', '=', $form['linea']],
  4376. ['REFI_CRFI', '=', $val['TAX_REGIME_KEY']],
  4377. ])->first();
  4378. if(is_null($val['TAX_REGIME_KEY'])) continue;
  4379. if(is_null($taxRegime)){
  4380. DB::table('S002V01TREFI')->insert([
  4381. 'REFI_NULI' => $form['linea'],
  4382. 'REFI_CRFI' => $val['TAX_REGIME_KEY'],
  4383. 'REFI_DRFI' => $val['TAX_REGIME_DESC'],
  4384. 'REFI_APFI' => $val['APPLY_PHISIC'],
  4385. 'REFI_APMO' => $val['APPLY_MORAL'],
  4386. 'REFI_FERE' => $nowStr,
  4387. 'REFI_USRE' => $idUser,
  4388. ]);
  4389. }else{
  4390. DB::table('S002V01TREFI')->where([
  4391. ['REFI_NULI', '=', $form['linea']],
  4392. ['REFI_CRFI', '=', $val['TAX_REGIME_KEY']],
  4393. ])->update([
  4394. 'REFI_DRFI' => $val['TAX_REGIME_DESC'],
  4395. 'REFI_APFI' => $val['APPLY_PHISIC'],
  4396. 'REFI_APMO' => $val['APPLY_MORAL'],
  4397. 'REFI_USMO' => $idUser,
  4398. 'REFI_FEMO' => $nowStr,
  4399. ]);
  4400. }
  4401. }
  4402. break;
  4403. }
  4404. $actions = DB::getQueryLog();
  4405. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4406. $idac = $this->functionsController->registerActivity(
  4407. $form['linea'],
  4408. 'S002V01M01ADSI',
  4409. 'S002V01F10AGCA',
  4410. 'S002V01P02NUCA',
  4411. 'Actualización',
  4412. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó el catálogo $form[catalogue_type].",
  4413. $idUser,
  4414. $nowStr,
  4415. );
  4416. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  4417. return $this->responseController->makeresponse(false, "EXITO");
  4418. }
  4419. public function getZipCodes($idUser, $line){
  4420. DB::enableQueryLog();
  4421. $idUser = $this->encryptionController->decrypt($idUser);
  4422. if(!$idUser){
  4423. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  4424. }
  4425. $usr = DB::table('S002V01TUSUA')->where([
  4426. ['USUA_NULI', '=', $line],
  4427. ['USUA_IDUS', '=', $idUser],
  4428. ])->first();
  4429. if(is_null($usr)){
  4430. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  4431. }
  4432. $catalogue = DB::table('S002V01TCATA')->where([
  4433. ['CATA_TICA', '=', 'Códigos postales'],
  4434. ['CATA_ESTA', '=', 'Activo'],
  4435. ['CATA_NULI', '=', $line],
  4436. ])->orderBy('CATA_VERS', 'desc')->first();
  4437. if(is_null($catalogue)){
  4438. return $this->responseController->makeResponse(true, 'Aún no se ha registrado ningún catálogo de códigos postales', [], 404);
  4439. }
  4440. $filesArr = json_decode($catalogue->CATA_ARRE, true);
  4441. $fileDec = $this->encryptionController->decrypt($filesArr[0]);
  4442. if(!$fileDec){
  4443. return $this->responseController->makeResponse(true, 'El ID catálogo vigente de códigos postales no fue encriptado correctamente.', [], 400);
  4444. }
  4445. $fileArr = explode("=", $fileDec);
  4446. $idArr = explode("-", $fileArr[0]);
  4447. $file = DB::table('S002V01TAFAL')->where([
  4448. ['AFAL_NULI', '=', $line],
  4449. ['AFAL_COMO', '=', $idArr[1]],
  4450. ['AFAL_CLDO', '=', $idArr[2]],
  4451. ['AFAL_FECR', '=', $idArr[3]],
  4452. ['AFAL_NUSE', '=', $idArr[4]],
  4453. ['AFAL_NUVE', '=', $fileArr[1]],
  4454. ])->first();
  4455. if(is_null($file)){
  4456. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de códigos postales no está registrado.', [], 404);
  4457. }else if($file->AFAL_ESTA == 'Eliminado'){
  4458. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de códigos postales está eliminado.', [], 404);
  4459. }else if(!file_exists($file->AFAL_UBIC)){
  4460. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de códigos postales no existe en el almacenamiento final.', [], 404);
  4461. }
  4462. $now = $this->functionsController->now();
  4463. $nowStr = $now->toDateTimeString();
  4464. $spreadsheet = IOFactory::load($file->AFAL_UBIC);
  4465. $sheetNames = $spreadsheet->getSheetNames();
  4466. $zipCodes = [];
  4467. foreach($sheetNames as $name){
  4468. if(!str_contains($name, 'c_CodigoPostal')){
  4469. return $this->responseController->makeResponse(true, "La hoja $name no pertenece al catálogo de códigos postales.", [], 400);
  4470. }
  4471. $worksheet = $spreadsheet->getSheetByName($name);
  4472. $fileValidatorCell = $worksheet->getCell('A6')->getValue();
  4473. if($fileValidatorCell != 'c_CodigoPostal'){
  4474. return $this->responseController->makeResponse(true, 'El archivo asociado no tiene un formato válido.', [], 400);
  4475. }
  4476. $maxRow = $worksheet->getHighestRow();
  4477. for($row = 8; $row <= $maxRow; $row++){
  4478. $zipCode = $worksheet->getCell("A$row")->getValue();
  4479. if($zipCode == null) continue;
  4480. $state = $worksheet->getCell("B$row")->getValue();
  4481. $city = $worksheet->getCell("C$row")->getValue();
  4482. $town = $worksheet->getCell("D$row")->getValue();
  4483. if(is_null($state)) continue;
  4484. $endDateNum = $worksheet->getCell("G$row")->getValue();
  4485. if(is_null($endDateNum)){
  4486. $zipCodes[] = [
  4487. 'CODIGO_POSTAL' => $zipCode,
  4488. 'ESTADO' => $state,
  4489. 'MUNICIPIO' => $city,
  4490. 'LOCALIDAD' => $town,
  4491. ];
  4492. }else{
  4493. $endDateStr = Date::excelToTimestamp($endDateNum);
  4494. $endDateObj = Carbon::createFromTimestamp($endDateStr);
  4495. if($endDateObj->gte($now)){
  4496. $zipCodes[] = [
  4497. 'CODIGO_POSTAL' => $zipCode,
  4498. 'ESTADO' => $state,
  4499. 'MUNICIPIO' => $city,
  4500. 'LOCALIDAD' => $town,
  4501. ];
  4502. }
  4503. }
  4504. }
  4505. }
  4506. $actions = DB::getQueryLog();
  4507. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4508. $idac = $this->functionsController->registerActivity(
  4509. $line,
  4510. 'S002V01M01ADSI',
  4511. 'S002V01F10AGCA',
  4512. 'S002V01P01HICA',
  4513. 'Consulta',
  4514. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el contenido del catálogo de Códigos postales.",
  4515. $idUser,
  4516. $nowStr
  4517. );
  4518. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4519. return $this->responseController->makeResponse(false, 'EXITO', $zipCodes);
  4520. }
  4521. public function getZipCodesTotal($idUser, $line) {
  4522. DB::enableQueryLog();
  4523. $idUser = $this->encryptionController->decrypt($idUser);
  4524. if(!$idUser){
  4525. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  4526. }
  4527. $usr = DB::table('S002V01TUSUA')->where([
  4528. ['USUA_NULI', '=', $line],
  4529. ['USUA_IDUS', '=', $idUser],
  4530. ])->first();
  4531. if(is_null($usr)){
  4532. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  4533. }
  4534. $totalZipCodes = DB::table('S002V01TCOPO')->select(DB::raw('count(*) as total_zip_codes'))->where(
  4535. 'COPO_NULI', '=', $line
  4536. )->first();
  4537. $now = $this->functionsController->now();
  4538. $nowStr = $now->toDateTimeString();
  4539. $actions = DB::getQueryLog();
  4540. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4541. $idac = $this->functionsController->registerActivity(
  4542. $line,
  4543. 'S002V01M01ADSI',
  4544. 'S002V01F10AGCA',
  4545. 'S002V01P01HICA',
  4546. 'Consulta',
  4547. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el total de Códigos postales.",
  4548. $idUser,
  4549. $nowStr
  4550. );
  4551. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4552. return $this->responseController->makeResponse(false, 'EXITO', ["TOTAL_ZIP_CODES" => $totalZipCodes->total_zip_codes]);
  4553. }
  4554. public function getZipCodesPage($limit, $offset, $idUser, $line) {
  4555. DB::enableQueryLog();
  4556. $idUser = $this->encryptionController->decrypt($idUser);
  4557. if(!$idUser){
  4558. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  4559. }
  4560. $usr = DB::table('S002V01TUSUA')->where([
  4561. ['USUA_NULI', '=', $line],
  4562. ['USUA_IDUS', '=', $idUser],
  4563. ])->first();
  4564. if(is_null($usr)){
  4565. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  4566. }
  4567. $limit = intval($limit);
  4568. $offset = intval($offset);
  4569. $zipCodesPaginated = DB::table('S002V01TCOPO')->select([
  4570. 'COPO_COPO AS CODIGO_POSTAL',
  4571. 'COPO_COES AS ESTADO',
  4572. 'COPO_COMU AS MUNICIPIO',
  4573. 'COPO_COLO AS LOCALIDAD',
  4574. ])->where([
  4575. ['COPO_NULI', '=', $line],
  4576. ['COPO_ESTA', '=', 'Activo']
  4577. ])->offset($offset)->limit($limit)->orderBy('COPO_COES', 'ASC')->orderBy('COPO_COPO', 'ASC')->get()->all();
  4578. $now = $this->functionsController->now();
  4579. $nowStr = $now->toDateTimeString();
  4580. $actions = DB::getQueryLog();
  4581. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4582. $idac = $this->functionsController->registerActivity(
  4583. $line,
  4584. 'S002V01M01ADSI',
  4585. 'S002V01F10AGCA',
  4586. 'S002V01P01HICA',
  4587. 'Consulta',
  4588. "El usuario $name (" . $usr->USUA_IDUS . ") consultó $limit registros desde el $offset del catálogo de Códigos postales.",
  4589. $idUser,
  4590. $nowStr
  4591. );
  4592. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4593. return $this->responseController->makeResponse(false, 'EXITO', $zipCodesPaginated);
  4594. }
  4595. public function getAddressByZipCode($zipCode, $idUser, $line) {
  4596. DB::enableQueryLog();
  4597. $idUser = $this->encryptionController->decrypt($idUser);
  4598. if(!$idUser){
  4599. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  4600. }
  4601. $usr = DB::table('S002V01TUSUA')->where([
  4602. ['USUA_NULI', '=', $line],
  4603. ['USUA_IDUS', '=', $idUser],
  4604. ])->first();
  4605. if(is_null($usr)){
  4606. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  4607. }
  4608. $zipCode = $this->encryptionController->decrypt($zipCode);
  4609. if(!$zipCode){
  4610. return $this->responseController->makeResponse(true, 'El código postal enviado no está encriptado correctamente', [], 400);
  4611. }
  4612. $code = DB::table('S002V01TCOPO')->where([
  4613. ['COPO_NULI', '=', $line],
  4614. ['COPO_COPO', '=', $zipCode]
  4615. ])->first();
  4616. if(is_null($code)){
  4617. return $this->responseController->makeResponse(true, "El código postal $zipCode no está registrado.", [], 404);
  4618. }else if($code->COPO_ESTA == 'Eliminado'){
  4619. return $this->responseController->makeResponse(true, "El código postal $zipCode está eliminado.", [], 404);
  4620. }
  4621. $address = DB::table('S002V01TCOPO')->select([
  4622. 'COPO_COPO AS CODIGO_POSTAL',
  4623. 'COPO_COES AS CODIGO_ESTADO',
  4624. 'ESTA_NOES AS NOMBRE_ESTADO',
  4625. 'COPO_COMU AS CODIGO_MUNICIPIO',
  4626. 'MUNI_NOMU AS NOMBRE_MUNICIPIO',
  4627. 'COPO_COLO AS CODIGO_LOCALIDAD',
  4628. 'LOCA_NOLO AS NOMBRE_LOCALIDAD'
  4629. ])->where([
  4630. ['COPO_NULI', '=', $line],
  4631. ['COPO_COPO', '=', $zipCode]
  4632. ])->join('S002V01TESTA', 'COPO_COES', '=', 'ESTA_COES')
  4633. ->leftJoin('S002V01TMUNI', function(JoinClause $join) {
  4634. $join->on('COPO_COMU', '=', 'MUNI_COMU')
  4635. ->on('COPO_COES', '=', 'MUNI_COES');
  4636. })->leftJoin('S002V01TLOCA', function(JoinClause $join) {
  4637. $join->on('COPO_COLO', '=', 'LOCA_COLO')
  4638. ->on('COPO_COES', '=', 'LOCA_COES');
  4639. })->first();
  4640. $settings = DB::table('S002V01TCOLO')->select([
  4641. 'COLO_COCO AS CODIGO_COLONIA',
  4642. 'COLO_NOCO AS NOMBRE_COLONIA'
  4643. ])->where([
  4644. ['COLO_NULI', '=', $line],
  4645. ['COLO_COPO', '=', $zipCode],
  4646. ])->get()->all();
  4647. $address->COLONIAS = $settings;
  4648. $now = $this->functionsController->now();
  4649. $nowStr = $now->toDateTimeString();
  4650. $actions = DB::getQueryLog();
  4651. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4652. $idac = $this->functionsController->registerActivity(
  4653. $line,
  4654. 'S002V01M01ADSI',
  4655. 'S002V01F10AGCA',
  4656. 'S002V01P02NUCA',
  4657. 'Consulta',
  4658. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la dirección del código postal $zipCode.",
  4659. $idUser,
  4660. $nowStr,
  4661. );
  4662. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4663. return $this->responseController->makeresponse(false, "EXITO", $address);
  4664. }
  4665. public function getStatesList($idUser, $line) {
  4666. DB::enableQueryLog();
  4667. $idUser = $this->encryptionController->decrypt($idUser);
  4668. if(!$idUser){
  4669. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  4670. }
  4671. $usr = DB::table('S002V01TUSUA')->where([
  4672. ['USUA_NULI', '=', $line],
  4673. ['USUA_IDUS', '=', $idUser],
  4674. ])->first();
  4675. if(is_null($usr)){
  4676. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  4677. }
  4678. $catalogue = DB::table('S002V01TCATA')->where([
  4679. ['CATA_TICA', '=', 'Estados'],
  4680. ['CATA_ESTA', '=', 'Activo'],
  4681. ['CATA_NULI', '=', $line],
  4682. ])->orderBy('CATA_VERS', 'desc')->first();
  4683. if(is_null($catalogue)){
  4684. return $this->responseController->makeResponse(true, 'Aún no se ha registrado ningún catálogo de Estados', [], 404);
  4685. }
  4686. $filesArr = json_decode($catalogue->CATA_ARRE, true);
  4687. $fileDec = $this->encryptionController->decrypt($filesArr[0]);
  4688. if(!$fileDec){
  4689. return $this->responseController->makeResponse(true, 'El ID catálogo vigente de países no fue encriptado correctamente.', [], 400);
  4690. }
  4691. $fileArr = explode("=", $fileDec);
  4692. $idArr = explode("-", $fileArr[0]);
  4693. $file = DB::table('S002V01TAFAL')->where([
  4694. ['AFAL_NULI', '=', $line],
  4695. ['AFAL_COMO', '=', $idArr[1]],
  4696. ['AFAL_CLDO', '=', $idArr[2]],
  4697. ['AFAL_FECR', '=', $idArr[3]],
  4698. ['AFAL_NUSE', '=', $idArr[4]],
  4699. ['AFAL_NUVE', '=', $fileArr[1]],
  4700. ])->first();
  4701. if(is_null($file)){
  4702. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de países no está registrado.', [], 404);
  4703. }else if($file->AFAL_ESTA == 'Eliminado'){
  4704. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de países está eliminado.', [], 404);
  4705. }else if(!file_exists($file->AFAL_UBIC)){
  4706. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de países no existe en el almacenamiento final.', [], 404);
  4707. }
  4708. $spreadsheet = IOFactory::load($file->AFAL_UBIC);
  4709. $sheetNames = $spreadsheet->getSheetNames();
  4710. if(!in_array('c_Estado', $sheetNames)){
  4711. return $this->responseController->makeResponse(true, 'El archivo asociado no contiene la hoja c_Estado.', [], 400);
  4712. }
  4713. $worksheet = $spreadsheet->getActiveSheet();
  4714. $fileValidatorCell = $worksheet->getCell('A5')->getValue();
  4715. if($fileValidatorCell != 'c_Estado'){
  4716. return $this->responseController->makeResponse(true, 'El archivo asociado no tiene un formato válido.', [], 400);
  4717. }
  4718. $now = $this->functionsController->now();
  4719. $nowStr = $now->toDateTimeString();
  4720. $states = [];
  4721. $maxRow = $worksheet->getHighestRow();
  4722. for($row = 6; $row <= $maxRow; $row++){
  4723. $stateKey = $worksheet->getCell("A$row")->getValue();
  4724. $countryKey = $worksheet->getCell("B$row")->getValue();
  4725. $stateName = $worksheet->getCell("C$row")->getValue();
  4726. $endDateNum = $worksheet->getCell("E$row")->getValue();
  4727. if(is_null($endDateNum)){
  4728. $states[] = [
  4729. 'STATE_KEY' => $stateKey,
  4730. 'COUNTRY_KEY' => $countryKey,
  4731. 'STATE_NAME' => $stateName,
  4732. ];
  4733. }else{
  4734. $endDateStr = Date::excelToTimestamp($endDateNum);
  4735. $endDateObj = Carbon::createFromTimestamp($endDateStr);
  4736. if($endDateObj->gte($now)){
  4737. $states[] = [
  4738. 'STATE_KEY' => $stateKey,
  4739. 'COUNTRY_KEY' => $countryKey,
  4740. 'STATE_NAME' => $stateName,
  4741. ];
  4742. }
  4743. }
  4744. }
  4745. $actions = DB::getQueryLog();
  4746. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4747. $idac = $this->functionsController->registerActivity(
  4748. $line,
  4749. 'S002V01M01ADSI',
  4750. 'S002V01F10AGCA',
  4751. 'S002V01P01HICA',
  4752. 'Consulta',
  4753. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el contenido del catálogo de Estados.",
  4754. $idUser,
  4755. $nowStr
  4756. );
  4757. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4758. return $this->responseController->makeResponse(false, 'EXITO', $states);
  4759. }
  4760. public function getCitiesList($idUser, $line) {
  4761. DB::enableQueryLog();
  4762. $idUser = $this->encryptionController->decrypt($idUser);
  4763. if(!$idUser){
  4764. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  4765. }
  4766. $usr = DB::table('S002V01TUSUA')->where([
  4767. ['USUA_NULI', '=', $line],
  4768. ['USUA_IDUS', '=', $idUser],
  4769. ])->first();
  4770. if(is_null($usr)){
  4771. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  4772. }
  4773. $catalogue = DB::table('S002V01TCATA')->where([
  4774. ['CATA_TICA', '=', 'Municipios'],
  4775. ['CATA_ESTA', '=', 'Activo'],
  4776. ['CATA_NULI', '=', $line],
  4777. ])->orderBy('CATA_VERS', 'desc')->first();
  4778. if(is_null($catalogue)){
  4779. return $this->responseController->makeResponse(true, 'Aún no se ha registrado ningún catálogo de Estados', [], 404);
  4780. }
  4781. $filesArr = json_decode($catalogue->CATA_ARRE, true);
  4782. $fileDec = $this->encryptionController->decrypt($filesArr[0]);
  4783. if(!$fileDec){
  4784. return $this->responseController->makeResponse(true, 'El ID catálogo vigente de países no fue encriptado correctamente.', [], 400);
  4785. }
  4786. $fileArr = explode("=", $fileDec);
  4787. $idArr = explode("-", $fileArr[0]);
  4788. $file = DB::table('S002V01TAFAL')->where([
  4789. ['AFAL_NULI', '=', $line],
  4790. ['AFAL_COMO', '=', $idArr[1]],
  4791. ['AFAL_CLDO', '=', $idArr[2]],
  4792. ['AFAL_FECR', '=', $idArr[3]],
  4793. ['AFAL_NUSE', '=', $idArr[4]],
  4794. ['AFAL_NUVE', '=', $fileArr[1]],
  4795. ])->first();
  4796. if(is_null($file)){
  4797. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de países no está registrado.', [], 404);
  4798. }else if($file->AFAL_ESTA == 'Eliminado'){
  4799. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de países está eliminado.', [], 404);
  4800. }else if(!file_exists($file->AFAL_UBIC)){
  4801. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de países no existe en el almacenamiento final.', [], 404);
  4802. }
  4803. $spreadsheet = IOFactory::load($file->AFAL_UBIC);
  4804. $sheetNames = $spreadsheet->getSheetNames();
  4805. if(!in_array('c_Municipio', $sheetNames)){
  4806. return $this->responseController->makeResponse(true, 'El archivo asociado no contiene la hoja c_Municipio.', [], 400);
  4807. }
  4808. $worksheet = $spreadsheet->getActiveSheet();
  4809. $fileValidatorCell = $worksheet->getCell('A5')->getValue();
  4810. if($fileValidatorCell != 'c_Municipio'){
  4811. return $this->responseController->makeResponse(true, 'El archivo asociado no tiene un formato válido.', [], 400);
  4812. }
  4813. $now = $this->functionsController->now();
  4814. $nowStr = $now->toDateTimeString();
  4815. $cities = [];
  4816. $maxRow = $worksheet->getHighestRow();
  4817. for($row = 6; $row <= $maxRow; $row++){
  4818. $cityKey = $worksheet->getCell("A$row")->getValue();
  4819. $stateKey = $worksheet->getCell("B$row")->getValue();
  4820. $cityName = $worksheet->getCell("C$row")->getValue();
  4821. $endDateNum = $worksheet->getCell("E$row")->getValue();
  4822. if(is_null($endDateNum)){
  4823. $cities[] = [
  4824. 'CITY_KEY' => $cityKey,
  4825. 'STATE_KEY' => $stateKey,
  4826. 'CITY_NAME' => $cityName,
  4827. ];
  4828. }else{
  4829. $endDateStr = Date::excelToTimestamp($endDateNum);
  4830. $endDateObj = Carbon::createFromTimestamp($endDateStr);
  4831. if($endDateObj->gte($now)){
  4832. $cities[] = [
  4833. 'CITY_KEY' => $cityKey,
  4834. 'STATE_KEY' => $stateKey,
  4835. 'CITY_NAME' => $cityName,
  4836. ];
  4837. }
  4838. }
  4839. }
  4840. $actions = DB::getQueryLog();
  4841. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4842. $idac = $this->functionsController->registerActivity(
  4843. $line,
  4844. 'S002V01M01ADSI',
  4845. 'S002V01F10AGCA',
  4846. 'S002V01P01HICA',
  4847. 'Consulta',
  4848. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el contenido del catálogo de Municipios.",
  4849. $idUser,
  4850. $nowStr
  4851. );
  4852. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4853. return $this->responseController->makeResponse(false, 'EXITO', $cities);
  4854. }
  4855. public function getTownsList($idUser, $line) {
  4856. DB::enableQueryLog();
  4857. $idUser = $this->encryptionController->decrypt($idUser);
  4858. if(!$idUser){
  4859. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  4860. }
  4861. $usr = DB::table('S002V01TUSUA')->where([
  4862. ['USUA_NULI', '=', $line],
  4863. ['USUA_IDUS', '=', $idUser],
  4864. ])->first();
  4865. if(is_null($usr)){
  4866. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  4867. }
  4868. $catalogue = DB::table('S002V01TCATA')->where([
  4869. ['CATA_TICA', '=', 'Localidades'],
  4870. ['CATA_ESTA', '=', 'Activo'],
  4871. ['CATA_NULI', '=', $line],
  4872. ])->orderBy('CATA_VERS', 'desc')->first();
  4873. if(is_null($catalogue)){
  4874. return $this->responseController->makeResponse(true, 'Aún no se ha registrado ningún catálogo de Estados', [], 404);
  4875. }
  4876. $filesArr = json_decode($catalogue->CATA_ARRE, true);
  4877. $fileDec = $this->encryptionController->decrypt($filesArr[0]);
  4878. if(!$fileDec){
  4879. return $this->responseController->makeResponse(true, 'El ID catálogo vigente de países no fue encriptado correctamente.', [], 400);
  4880. }
  4881. $fileArr = explode("=", $fileDec);
  4882. $idArr = explode("-", $fileArr[0]);
  4883. $file = DB::table('S002V01TAFAL')->where([
  4884. ['AFAL_NULI', '=', $line],
  4885. ['AFAL_COMO', '=', $idArr[1]],
  4886. ['AFAL_CLDO', '=', $idArr[2]],
  4887. ['AFAL_FECR', '=', $idArr[3]],
  4888. ['AFAL_NUSE', '=', $idArr[4]],
  4889. ['AFAL_NUVE', '=', $fileArr[1]],
  4890. ])->first();
  4891. if(is_null($file)){
  4892. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de países no está registrado.', [], 404);
  4893. }else if($file->AFAL_ESTA == 'Eliminado'){
  4894. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de países está eliminado.', [], 404);
  4895. }else if(!file_exists($file->AFAL_UBIC)){
  4896. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de países no existe en el almacenamiento final.', [], 404);
  4897. }
  4898. $spreadsheet = IOFactory::load($file->AFAL_UBIC);
  4899. $sheetNames = $spreadsheet->getSheetNames();
  4900. if(!in_array('c_Localidad', $sheetNames)){
  4901. return $this->responseController->makeResponse(true, 'El archivo asociado no contiene la hoja c_Localidad.', [], 400);
  4902. }
  4903. $worksheet = $spreadsheet->getActiveSheet();
  4904. $fileValidatorCell = $worksheet->getCell('A5')->getValue();
  4905. if($fileValidatorCell != 'c_Localidad'){
  4906. return $this->responseController->makeResponse(true, 'El archivo asociado no tiene un formato válido.', [], 400);
  4907. }
  4908. $now = $this->functionsController->now();
  4909. $nowStr = $now->toDateTimeString();
  4910. $towns = [];
  4911. $maxRow = $worksheet->getHighestRow();
  4912. for($row = 6; $row <= $maxRow; $row++){
  4913. $townKey = $worksheet->getCell("A$row")->getValue();
  4914. $stateKey = $worksheet->getCell("B$row")->getValue();
  4915. $townName = $worksheet->getCell("C$row")->getValue();
  4916. $endDateNum = $worksheet->getCell("E$row")->getValue();
  4917. if(is_null($endDateNum)){
  4918. $towns[] = [
  4919. 'TOWN_KEY' => $townKey,
  4920. 'STATE_KEY' => $stateKey,
  4921. 'TOWN_NAME' => $townName,
  4922. ];
  4923. }else{
  4924. $endDateStr = Date::excelToTimestamp($endDateNum);
  4925. $endDateObj = Carbon::createFromTimestamp($endDateStr);
  4926. if($endDateObj->gte($now)){
  4927. $towns[] = [
  4928. 'TOWN_KEY' => $townKey,
  4929. 'STATE_KEY' => $stateKey,
  4930. 'TOWN_NAME' => $townName,
  4931. ];
  4932. }
  4933. }
  4934. }
  4935. $actions = DB::getQueryLog();
  4936. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4937. $idac = $this->functionsController->registerActivity(
  4938. $line,
  4939. 'S002V01M01ADSI',
  4940. 'S002V01F10AGCA',
  4941. 'S002V01P01HICA',
  4942. 'Consulta',
  4943. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el contenido del catálogo de Localidades.",
  4944. $idUser,
  4945. $nowStr
  4946. );
  4947. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4948. return $this->responseController->makeResponse(false, 'EXITO', $towns);
  4949. }
  4950. public function getSettingsList($idUser, $line) {
  4951. DB::enableQueryLog();
  4952. $idUser = $this->encryptionController->decrypt($idUser);
  4953. if(!$idUser){
  4954. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  4955. }
  4956. $usr = DB::table('S002V01TUSUA')->where([
  4957. ['USUA_NULI', '=', $line],
  4958. ['USUA_IDUS', '=', $idUser],
  4959. ])->first();
  4960. if(is_null($usr)){
  4961. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  4962. }
  4963. $catalogue = DB::table('S002V01TCATA')->where([
  4964. ['CATA_TICA', '=', 'Colonias'],
  4965. ['CATA_ESTA', '=', 'Activo'],
  4966. ['CATA_NULI', '=', $line],
  4967. ])->orderBy('CATA_VERS', 'desc')->first();
  4968. if(is_null($catalogue)){
  4969. return $this->responseController->makeResponse(true, 'Aún no se ha registrado ningún catálogo de Estados', [], 404);
  4970. }
  4971. $filesArr = json_decode($catalogue->CATA_ARRE, true);
  4972. $fileDec = $this->encryptionController->decrypt($filesArr[0]);
  4973. if(!$fileDec){
  4974. return $this->responseController->makeResponse(true, 'El ID catálogo vigente de países no fue encriptado correctamente.', [], 400);
  4975. }
  4976. $fileArr = explode("=", $fileDec);
  4977. $idArr = explode("-", $fileArr[0]);
  4978. $file = DB::table('S002V01TAFAL')->where([
  4979. ['AFAL_NULI', '=', $line],
  4980. ['AFAL_COMO', '=', $idArr[1]],
  4981. ['AFAL_CLDO', '=', $idArr[2]],
  4982. ['AFAL_FECR', '=', $idArr[3]],
  4983. ['AFAL_NUSE', '=', $idArr[4]],
  4984. ['AFAL_NUVE', '=', $fileArr[1]],
  4985. ])->first();
  4986. if(is_null($file)){
  4987. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de países no está registrado.', [], 404);
  4988. }else if($file->AFAL_ESTA == 'Eliminado'){
  4989. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de países está eliminado.', [], 404);
  4990. }else if(!file_exists($file->AFAL_UBIC)){
  4991. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de países no existe en el almacenamiento final.', [], 404);
  4992. }
  4993. $now = $this->functionsController->now();
  4994. $nowStr = $now->toDateTimeString();
  4995. $spreadsheet = IOFactory::load($file->AFAL_UBIC);
  4996. $sheetNames = $spreadsheet->getSheetNames();
  4997. $settings = [];
  4998. foreach($sheetNames as $name){
  4999. if(!str_contains($name, 'c_Colonia')){
  5000. return $this->responseController->makeResponse(true, "La hoja $name no pertenece al catálogo de colonias.", [], 400);
  5001. }
  5002. $worksheet = $spreadsheet->getSheetByName($name);
  5003. $fileValidatorCell = $worksheet->getCell('A5')->getValue();
  5004. if($fileValidatorCell != 'c_Colonia'){
  5005. return $this->responseController->makeResponse(true, 'El archivo asociado no tiene un formato válido.', [], 400);
  5006. }
  5007. $maxRow = $worksheet->getHighestRow();
  5008. for($row = 6; $row <= $maxRow; $row++){
  5009. $settingKey = $worksheet->getCell("A$row")->getValue();
  5010. if($settingKey == null) continue;
  5011. $zipCode = $worksheet->getCell("B$row")->getValue();
  5012. $settingName = $worksheet->getCell("C$row")->getValue();
  5013. if(is_null($zipCode)) continue;
  5014. $settings[] = [
  5015. 'SETTING_KEY' => $settingKey,
  5016. 'CODIGO_POSTAL' => $zipCode,
  5017. 'SETTING_NAME' => $settingName,
  5018. ];
  5019. }
  5020. }
  5021. $actions = DB::getQueryLog();
  5022. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  5023. $idac = $this->functionsController->registerActivity(
  5024. $line,
  5025. 'S002V01M01ADSI',
  5026. 'S002V01F10AGCA',
  5027. 'S002V01P01HICA',
  5028. 'Consulta',
  5029. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el contenido del catálogo de Colonias.",
  5030. $idUser,
  5031. $nowStr
  5032. );
  5033. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  5034. return $this->responseController->makeResponse(false, 'EXITO', $settings);
  5035. }
  5036. public function getTaxRegimeList($idUser, $line) {
  5037. DB::enableQueryLog();
  5038. $idUser = $this->encryptionController->decrypt($idUser);
  5039. if(!$idUser){
  5040. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  5041. }
  5042. $usr = DB::table('S002V01TUSUA')->where([
  5043. ['USUA_NULI', '=', $line],
  5044. ['USUA_IDUS', '=', $idUser],
  5045. ])->first();
  5046. if(is_null($usr)){
  5047. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  5048. }
  5049. $catalogue = DB::table('S002V01TCATA')->where([
  5050. ['CATA_TICA', '=', 'Régimen fiscal'],
  5051. ['CATA_ESTA', '=', 'Activo'],
  5052. ['CATA_NULI', '=', $line],
  5053. ])->orderBy('CATA_VERS', 'desc')->first();
  5054. if(is_null($catalogue)){
  5055. return $this->responseController->makeResponse(true, 'Aún no se ha registrado ningún catálogo de Estados', [], 404);
  5056. }
  5057. $filesArr = json_decode($catalogue->CATA_ARRE, true);
  5058. $fileDec = $this->encryptionController->decrypt($filesArr[0]);
  5059. if(!$fileDec){
  5060. return $this->responseController->makeResponse(true, 'El ID catálogo vigente de países no fue encriptado correctamente.', [], 400);
  5061. }
  5062. $fileArr = explode("=", $fileDec);
  5063. $idArr = explode("-", $fileArr[0]);
  5064. $file = DB::table('S002V01TAFAL')->where([
  5065. ['AFAL_NULI', '=', $line],
  5066. ['AFAL_COMO', '=', $idArr[1]],
  5067. ['AFAL_CLDO', '=', $idArr[2]],
  5068. ['AFAL_FECR', '=', $idArr[3]],
  5069. ['AFAL_NUSE', '=', $idArr[4]],
  5070. ['AFAL_NUVE', '=', $fileArr[1]],
  5071. ])->first();
  5072. if(is_null($file)){
  5073. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de países no está registrado.', [], 404);
  5074. }else if($file->AFAL_ESTA == 'Eliminado'){
  5075. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de países está eliminado.', [], 404);
  5076. }else if(!file_exists($file->AFAL_UBIC)){
  5077. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo vigente de países no existe en el almacenamiento final.', [], 404);
  5078. }
  5079. $spreadsheet = IOFactory::load($file->AFAL_UBIC);
  5080. $sheetNames = $spreadsheet->getSheetNames();
  5081. if(!in_array('c_RegimenFiscal', $sheetNames)){
  5082. return $this->responseController->makeResponse(true, 'El archivo asociado no contiene la hoja c_RegimenFiscal.', [], 400);
  5083. }
  5084. $worksheet = $spreadsheet->getActiveSheet();
  5085. $fileValidatorCell = $worksheet->getCell('A6')->getValue();
  5086. if($fileValidatorCell != 'c_RegimenFiscal'){
  5087. return $this->responseController->makeResponse(true, 'El archivo asociado no tiene un formato válido.', [], 400);
  5088. }
  5089. $now = $this->functionsController->now();
  5090. $nowStr = $now->toDateTimeString();
  5091. $taxRegime = [];
  5092. $maxRow = $worksheet->getHighestRow();
  5093. for($row = 7; $row <= $maxRow; $row++){
  5094. $taxRegimeKey = $worksheet->getCell("A$row")->getValue();
  5095. $taxRegimeDesc = $worksheet->getCell("B$row")->getValue();
  5096. $applyPhisic = $worksheet->getCell("C$row")->getValue();
  5097. $applyMoral = $worksheet->getCell("D$row")->getValue();
  5098. $endDateNum = $worksheet->getCell("F$row")->getValue();
  5099. if(is_null($endDateNum)){
  5100. $taxRegime[] = [
  5101. 'TAX_REGIME_KEY' => $taxRegimeKey,
  5102. 'TAX_REGIME_DESC' => $taxRegimeDesc,
  5103. 'APPLY_PHISIC' => $applyPhisic,
  5104. 'APPLY_MORAL' => $applyMoral,
  5105. ];
  5106. }else{
  5107. $endDateStr = Date::excelToTimestamp($endDateNum);
  5108. $endDateObj = Carbon::createFromTimestamp($endDateStr);
  5109. if($endDateObj->gte($now)){
  5110. $taxRegime[] = [
  5111. 'TAX_REGIME_KEY' => $taxRegimeKey,
  5112. 'TAX_REGIME_DESC' => $taxRegimeDesc,
  5113. 'APPLY_PHISIC' => $applyPhisic,
  5114. 'APPLY_MORAL' => $applyMoral,
  5115. ];
  5116. }
  5117. }
  5118. }
  5119. $actions = DB::getQueryLog();
  5120. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  5121. $idac = $this->functionsController->registerActivity(
  5122. $line,
  5123. 'S002V01M01ADSI',
  5124. 'S002V01F10AGCA',
  5125. 'S002V01P01HICA',
  5126. 'Consulta',
  5127. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el contenido del catálogo de Régimen fiscal.",
  5128. $idUser,
  5129. $nowStr
  5130. );
  5131. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  5132. return $this->responseController->makeResponse(false, 'EXITO', $taxRegime);
  5133. }
  5134. public function getStatesByCountryCode($countryCode, $idUser, $line) {
  5135. DB::enableQueryLog();
  5136. $idUser = $this->encryptionController->decrypt($idUser);
  5137. if(!$idUser){
  5138. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  5139. }
  5140. $usr = DB::table('S002V01TUSUA')->where([
  5141. ['USUA_NULI', '=', $line],
  5142. ['USUA_IDUS', '=', $idUser],
  5143. ])->first();
  5144. if(is_null($usr)){
  5145. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  5146. }
  5147. $countryCode = $this->encryptionController->decrypt($countryCode);
  5148. if(!$countryCode){
  5149. return $this->responseController->makeResponse(true, 'El código del país seleccionado no está encriptado correctamente', [], 400);
  5150. }
  5151. $country = DB::table('S002V01TPAIS')->where([
  5152. ['PAIS_NULI', '=', $line],
  5153. ['PAIS_IDPA', '=', $countryCode]
  5154. ])->first();
  5155. if(is_null($country)){
  5156. return $this->responseController->makeResponse(true, 'El país seleccionado no está registrado', [], 404);
  5157. }
  5158. $relatedStates = DB::table('S002V01TESTA')->select([
  5159. 'ESTA_COES AS CODIGOESTADO',
  5160. 'ESTA_NOES AS NOMBREESTADO'
  5161. ])->where([
  5162. ['ESTA_COPA', '=', $countryCode],
  5163. ['ESTA_NULI', '=', $line],
  5164. ])->orderBy('ESTA_COES', 'asc')->get()->all();
  5165. $now = $this->functionsController->now();
  5166. $nowStr = $now->toDateTimeString();
  5167. $actions = DB::getQueryLog();
  5168. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  5169. $idac = $this->functionsController->registerActivity(
  5170. $line,
  5171. 'S002V01M01ADSI',
  5172. 'S002V01F10AGCA',
  5173. 'S002V01P01HICA',
  5174. 'Consulta',
  5175. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los estados relacionados al país " . $country->PAIS_NOMB . " ($countryCode).",
  5176. $idUser,
  5177. $nowStr
  5178. );
  5179. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  5180. return $this->responseController->makeResponse(false, 'EXITO', $relatedStates);
  5181. }
  5182. public function getCitiesByStateCode($stateCode, $idUser, $line) {
  5183. DB::enableQueryLog();
  5184. $idUser = $this->encryptionController->decrypt($idUser);
  5185. if(!$idUser){
  5186. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  5187. }
  5188. $usr = DB::table('S002V01TUSUA')->where([
  5189. ['USUA_NULI', '=', $line],
  5190. ['USUA_IDUS', '=', $idUser],
  5191. ])->first();
  5192. if(is_null($usr)){
  5193. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  5194. }
  5195. $stateCode = $this->encryptionController->decrypt($stateCode);
  5196. if(!$stateCode){
  5197. return $this->responseController->makeResponse(true, 'El código del estado seleccionado no está encriptado correctamente', [], 400);
  5198. }
  5199. $state = DB::table('S002V01TESTA')->where([
  5200. ['ESTA_NULI', '=', $line],
  5201. ['ESTA_COES', '=', $stateCode]
  5202. ])->first();
  5203. if(is_null($state)){
  5204. return $this->responseController->makeResponse(true, 'El estado seleccionado no está registrado', [], 404);
  5205. }
  5206. $relatedCities = DB::table('S002V01TMUNI')->select([
  5207. 'MUNI_COMU AS CODIGOMUNICIPIO',
  5208. 'MUNI_NOMU AS NOMBREMUNICIPIO'
  5209. ])->where([
  5210. ['MUNI_COES', '=', $stateCode],
  5211. ['MUNI_NULI', '=', $line],
  5212. ])->orderBy('MUNI_COMU', 'asc')->get()->all();
  5213. $now = $this->functionsController->now();
  5214. $nowStr = $now->toDateTimeString();
  5215. $actions = DB::getQueryLog();
  5216. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  5217. $idac = $this->functionsController->registerActivity(
  5218. $line,
  5219. 'S002V01M01ADSI',
  5220. 'S002V01F10AGCA',
  5221. 'S002V01P01HICA',
  5222. 'Consulta',
  5223. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los municipios relacionados al estado " . $state->ESTA_NOES . " ($stateCode).",
  5224. $idUser,
  5225. $nowStr
  5226. );
  5227. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  5228. return $this->responseController->makeResponse(false, 'EXITO', $relatedCities);
  5229. }
  5230. public function getTowns($stateCode, $cityCode, $idUser, $line) {
  5231. DB::enableQueryLog();
  5232. $idUser = $this->encryptionController->decrypt($idUser);
  5233. if(!$idUser){
  5234. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  5235. }
  5236. $usr = DB::table('S002V01TUSUA')->where([
  5237. ['USUA_NULI', '=', $line],
  5238. ['USUA_IDUS', '=', $idUser],
  5239. ])->first();
  5240. if(is_null($usr)){
  5241. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  5242. }
  5243. $stateCode = $this->encryptionController->decrypt($stateCode);
  5244. if(!$stateCode){
  5245. return $this->responseController->makeResponse(true, 'El código del estado seleccionado no está encriptado correctamente', [], 400);
  5246. }
  5247. $state = DB::table('S002V01TESTA')->where([
  5248. ['ESTA_NULI', '=', $line],
  5249. ['ESTA_COES', '=', $stateCode]
  5250. ])->first();
  5251. if(is_null($state)){
  5252. return $this->responseController->makeResponse(true, 'El estado seleccionado no está registrado', [], 404);
  5253. }
  5254. $cityCode = $this->encryptionController->decrypt($cityCode);
  5255. if(!$cityCode){
  5256. return $this->responseController->makeResponse(true, 'El código del municipio seleccionado no está encriptado correctamente', [], 400);
  5257. }
  5258. $city = DB::table('S002V01TMUNI')->where([
  5259. ['MUNI_NULI', '=', $line],
  5260. ['MUNI_COMU', '=', $cityCode],
  5261. ['MUNI_COES', '=', $stateCode],
  5262. ])->first();
  5263. if(is_null($city)){
  5264. return $this->responseController->makeResponse(true, 'El municipio seleccionado no está registrado', [], 404);
  5265. }
  5266. $relatedZipCodes = DB::table('S002V01TCOPO')->select([
  5267. DB::raw('DISTINCT(COPO_COLO) AS CODIGOLOCALIDAD'),
  5268. 'LOCA_NOLO AS NOMBRELOCALIDAD'
  5269. ])->where([
  5270. ['COPO_NULI', '=', $line],
  5271. ['COPO_COES', '=', $stateCode],
  5272. ['COPO_COMU', '=', $cityCode],
  5273. ])->leftJoin('S002V01TLOCA', function(JoinClause $join) {
  5274. $join->on('LOCA_COLO', '=', 'COPO_COLO')
  5275. ->on('LOCA_COES', '=', 'COPO_COES');
  5276. })->get()->all();
  5277. $now = $this->functionsController->now();
  5278. $nowStr = $now->toDateTimeString();
  5279. $actions = DB::getQueryLog();
  5280. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  5281. $idac = $this->functionsController->registerActivity(
  5282. $line,
  5283. 'S002V01M01ADSI',
  5284. 'S002V01F10AGCA',
  5285. 'S002V01P01HICA',
  5286. 'Consulta',
  5287. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las localidades relacionadas al estado " . $state->ESTA_NOES . " ($stateCode).",
  5288. $idUser,
  5289. $nowStr
  5290. );
  5291. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  5292. return $this->responseController->makeResponse(false, 'EXITO', $relatedZipCodes);
  5293. }
  5294. public function getCitySettings($stateCode, $cityCode, $townCode, $idUser, $line) {
  5295. DB::enableQueryLog();
  5296. $idUser = $this->encryptionController->decrypt($idUser);
  5297. if(!$idUser){
  5298. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  5299. }
  5300. $usr = DB::table('S002V01TUSUA')->where([
  5301. ['USUA_NULI', '=', $line],
  5302. ['USUA_IDUS', '=', $idUser],
  5303. ])->first();
  5304. if(is_null($usr)){
  5305. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  5306. }
  5307. $stateCode = $this->encryptionController->decrypt($stateCode);
  5308. if(!$stateCode){
  5309. return $this->responseController->makeResponse(true, 'El código del estado seleccionado no está encriptado correctamente', [], 400);
  5310. }
  5311. $state = DB::table('S002V01TESTA')->where([
  5312. ['ESTA_NULI', '=', $line],
  5313. ['ESTA_COES', '=', $stateCode]
  5314. ])->first();
  5315. if(is_null($state)){
  5316. return $this->responseController->makeResponse(true, 'El estado seleccionado no está registrado', [], 404);
  5317. }
  5318. $cityCode = $this->encryptionController->decrypt($cityCode);
  5319. if(!$cityCode){
  5320. return $this->responseController->makeResponse(true, 'El código del municipio seleccionado no está encriptado correctamente', [], 400);
  5321. }
  5322. $city = DB::table('S002V01TMUNI')->where([
  5323. ['MUNI_NULI', '=', $line],
  5324. ['MUNI_COMU', '=', $cityCode],
  5325. ['MUNI_COES', '=', $stateCode],
  5326. ])->first();
  5327. if(is_null($city)){
  5328. return $this->responseController->makeResponse(true, 'El municipio seleccionado no está registrado', [], 404);
  5329. }
  5330. $townCode = $this->encryptionController->decrypt($townCode);
  5331. if(!$townCode){
  5332. return $this->responseController->makeResponse(true, 'El código de la localidad seleccionada no está encriptado correctamente', [], 400);
  5333. }
  5334. $townCode = $townCode == '-' ? null : $townCode;
  5335. if(!is_null($townCode)){
  5336. $town = DB::table('S002V01TLOCA')->where([
  5337. ['LOCA_NULI', '=', $line],
  5338. ['LOCA_COLO', '=', $townCode],
  5339. ['LOCA_COES', '=', $stateCode]
  5340. ])->first();
  5341. if(is_null($town)){
  5342. return $this->responseController->makeResponse(true, 'La localidad seleccionada no está registrada', [], 404);
  5343. }
  5344. }
  5345. $settings = DB::table('S002V01TCOPO')->select([
  5346. 'COLO_COCO AS CODIGOCOLONIA',
  5347. 'COPO_COPO AS CODIGOPOSTAL',
  5348. 'COLO_NOCO AS NOMBRECOLONIA',
  5349. ])->where([
  5350. ['COPO_NULI', '=', $line],
  5351. ['COPO_COES', '=', $stateCode],
  5352. ['COPO_COMU', '=', $cityCode],
  5353. ['COPO_COLO', '=', $townCode],
  5354. ])->join('S002V01TCOLO', 'COLO_COPO', '=', 'COPO_COPO')->get()->all();
  5355. $now = $this->functionsController->now();
  5356. $nowStr = $now->toDateTimeString();
  5357. $actions = DB::getQueryLog();
  5358. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  5359. $idac = $this->functionsController->registerActivity(
  5360. $line,
  5361. 'S002V01M01ADSI',
  5362. 'S002V01F10AGCA',
  5363. 'S002V01P01HICA',
  5364. 'Consulta',
  5365. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las colonias relacionadas al estado " . $state->ESTA_NOES . " ($stateCode).",
  5366. $idUser,
  5367. $nowStr
  5368. );
  5369. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  5370. return $this->responseController->makeResponse(false, 'EXITO', $settings);
  5371. }
  5372. public function getGeneralCatalogueInfo($type, $headerRow, $idUser, $line) {
  5373. DB::enableQueryLog();
  5374. $idUser = $this->encryptionController->decrypt($idUser);
  5375. if(!$idUser){
  5376. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  5377. }
  5378. $usr = DB::table('S002V01TUSUA')->where([
  5379. ['USUA_NULI', '=', $line],
  5380. ['USUA_IDUS', '=', $idUser],
  5381. ])->first();
  5382. if(is_null($usr)){
  5383. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  5384. }
  5385. $catalogue = DB::table('S002V01TCATA')->where([
  5386. ['CATA_TICA', '=', 'General'],
  5387. ['CATA_ESTA', '=', 'Activo'],
  5388. ['CATA_NULI', '=', $line],
  5389. ])->orderBy('CATA_VERS', 'desc')->first();
  5390. if(is_null($catalogue)){
  5391. return $this->responseController->makeResponse(true, 'Aún no se ha registrado ningún catálogo de General', [], 404);
  5392. }
  5393. $filesArr = json_decode($catalogue->CATA_ARRE, true);
  5394. $fileDec = $this->encryptionController->decrypt($filesArr[0]);
  5395. if(!$fileDec){
  5396. return $this->responseController->makeResponse(true, 'El ID catálogo vigente de países no fue encriptado correctamente.', [], 400);
  5397. }
  5398. $fileArr = explode("=", $fileDec);
  5399. $idArr = explode("-", $fileArr[0]);
  5400. $file = DB::table('S002V01TAFAL')->where([
  5401. ['AFAL_NULI', '=', $line],
  5402. ['AFAL_COMO', '=', $idArr[1]],
  5403. ['AFAL_CLDO', '=', $idArr[2]],
  5404. ['AFAL_FECR', '=', $idArr[3]],
  5405. ['AFAL_NUSE', '=', $idArr[4]],
  5406. ['AFAL_NUVE', '=', $fileArr[1]],
  5407. ])->first();
  5408. if(is_null($file)){
  5409. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo general vigente no está registrado.', [], 404);
  5410. }else if($file->AFAL_ESTA == 'Eliminado'){
  5411. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo general vigente está eliminado.', [], 404);
  5412. }else if(!file_exists($file->AFAL_UBIC)){
  5413. return $this->responseController->makeResponse(true, 'El archivo asociado al catálogo general vigente no existe en el almacenamiento final.', [], 404);
  5414. }
  5415. ini_set("memory_limit", "-1");
  5416. set_time_limit(0);
  5417. $spreadsheet = IOFactory::load($file->AFAL_UBIC);
  5418. $sheetNames = $spreadsheet->getSheetNames();
  5419. $relatedSheets = [];
  5420. foreach($sheetNames as $sheet){
  5421. if(str_contains($sheet, $type)){
  5422. $relatedSheets[] = $sheet;
  5423. }
  5424. }
  5425. if(count($relatedSheets) == 0){
  5426. return $this->responseController->makeResponse(true, "No se encontraron hojas relacionadas al tipo $type.", [], 404);
  5427. }
  5428. $responseArr = [];
  5429. foreach($relatedSheets as $sheet){
  5430. $worksheet = $spreadsheet->getSheetByName($sheet);
  5431. $maxCol = $worksheet->getHighestColumn();
  5432. $maxColInd = Coordinate::columnIndexFromString($maxCol);
  5433. $headers = [];
  5434. $startRow = intval($headerRow);
  5435. if($startRow == 0){
  5436. return $this->responseController->makeResponse(true, 'La fila del encabezado no tiene un formato válido.', [], 400);
  5437. }
  5438. for($col = 1; $col <= $maxColInd; $col++){
  5439. $colStr = Coordinate::stringFromColumnIndex($col);
  5440. $header = $worksheet->getCell($colStr . $startRow)->getValue();
  5441. if(!is_null($header)){
  5442. $headers[$colStr] = $header;
  5443. }else{
  5444. $headers[$colStr] = $colStr;
  5445. }
  5446. }
  5447. $startRow++;
  5448. $maxRow = $worksheet->getHighestRow();
  5449. for($row = $startRow; $row <= $maxRow; $row++){
  5450. $arrAux = [];
  5451. for($col = 1; $col <= count($headers); $col++){
  5452. $colStr = Coordinate::stringFromColumnIndex($col);
  5453. $val = $worksheet->getCell($colStr . $row)->getCalculatedValue();
  5454. $header = $headers[$colStr];
  5455. $isDate = Date::isDateTime($worksheet->getCell($colStr . $row));
  5456. if($isDate){
  5457. $val = Date::excelToTimestamp($val);
  5458. $val = Carbon::createFromTimestamp($val);
  5459. $val = $val->toDateTimeString();
  5460. }
  5461. $header = str_replace(' ', '_', $header);
  5462. $arrAux[$header] = $val;
  5463. }
  5464. $responseArr[] = $arrAux;
  5465. }
  5466. }
  5467. $now = $this->functionsController->now();
  5468. $nowStr = $now->toDateTimeString();
  5469. $actions = DB::getQueryLog();
  5470. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  5471. $idac = $this->functionsController->registerActivity(
  5472. $line,
  5473. 'S002V01M01ADSI',
  5474. 'S002V01F10AGCA',
  5475. 'S002V01P01HICA',
  5476. 'Consulta',
  5477. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el contenido del catálogo General.",
  5478. $idUser,
  5479. $nowStr
  5480. );
  5481. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  5482. return $this->responseController->makeResponse(false, 'EXITO', $responseArr);
  5483. }
  5484. public function saveTaxInformation(Request $request) {
  5485. $validator = Validator::make($request->all(), [
  5486. 'NOMBRE_FISCAL' => 'required|string',
  5487. 'RFC' => 'required|string',
  5488. 'CORREO_ELECTRONICO' => 'required|string',
  5489. 'REGIMEN_FISCAL' => 'required|string',
  5490. 'LADA1' => 'required|string',
  5491. 'TELEFONO_FIJO' => 'required|integer',
  5492. 'LADA2' => 'required|string',
  5493. 'TELEFONO_MOVIL' => 'required|integer',
  5494. 'CODIGO_POSTAL' => 'required|integer',
  5495. 'TIPOVIALIDAD' => 'required|string',
  5496. 'NOMBRE_VIALIDAD' => 'required|string',
  5497. 'ENTRE_CALLE' => 'required|string',
  5498. 'Y_CALLE' => 'required|string',
  5499. 'NUMERO_EXTERIOR' => 'required|string',
  5500. // 'NUMERO_INTERIOR' => 'required|string',
  5501. 'COLONIA' => 'required|string',
  5502. 'LOCALIDAD' => 'required|string',
  5503. 'MUNICIPIO' => 'required|string',
  5504. 'ENTIDAD_FEDERATIVA' => 'required|string',
  5505. 'PAIS' => 'required|string',
  5506. 'USER' => 'required|string',
  5507. 'LINE_NUMBER' => 'required|integer',
  5508. ]);
  5509. if ($validator->fails()) {
  5510. return $this->responseController->makeResponse(
  5511. true,
  5512. "ERR_SYSTEM_ADMINISTRATOR_TAX_INFORMATION000: Se encontraron uno o más errores.",
  5513. $this->responseController->makeErrors($validator->errors()->messages()),
  5514. 401
  5515. );
  5516. }
  5517. DB::beginTransaction();
  5518. $responseData = $request->all();
  5519. try {
  5520. $user = $this->encryptionController->decrypt($responseData['USER']);
  5521. } catch (\Throwable $th) {
  5522. DB::rollBack();
  5523. return $this->responseController->makeResponse(true, "ERR_SYSTEM_ADMINISTRATOR_TAX_INFORMATION001: Ocurrió un error al desencriptar los datos.", [], 401);
  5524. }
  5525. $now = $this->functionsController->now();
  5526. $currentDate = $now->toDateTimeString();
  5527. try {
  5528. $existsActives = DB::table('S002V01TINFI')
  5529. ->where('INFI_ESTA','=','Activo')
  5530. ->where('INFI_NULI','=',$responseData['LINE_NUMBER'])
  5531. ->exists();
  5532. } catch (\Throwable $th) {
  5533. DB::rollBack();
  5534. return $this->responseController->makeResponse(true, "ERR_SYSTEM_ADMINISTRATOR_TAX_INFORMATION002: Ocurrió un error al validar los registros anteriores.", [], 401);
  5535. }
  5536. if ($existsActives) {
  5537. try {
  5538. $validateUpdate = DB::table('S002V01TINFI')
  5539. ->where('INFI_ESTA','=','Activo')
  5540. ->where('INFI_NULI','=',$responseData['LINE_NUMBER'])
  5541. ->update([
  5542. 'INFI_ESTA' => 'Inactivo',
  5543. 'INFI_USMO' => $user,
  5544. 'INFI_FEMO' => $currentDate,
  5545. 'INFI_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  5546. ]);
  5547. } catch (\Throwable $th) {
  5548. DB::rollBack();
  5549. return $this->responseController->makeResponse(true, "ERR_SYSTEM_ADMINISTRATOR_TAX_INFORMATION002: Ocurrió un error al modificar los registros anteriores.", $th->getMessage(), 401);
  5550. }
  5551. if (!$validateUpdate) {
  5552. DB::rollBack();
  5553. return $this->responseController->makeResponse(true, "ERR_SYSTEM_ADMINISTRATOR_TAX_INFORMATION003: No se pudo modificar los registros anteriores.", [], 401);
  5554. }
  5555. }
  5556. try {
  5557. $validateInsert = DB::table('S002V01TINFI')->insert([
  5558. 'INFI_NOFI' => $responseData['NOMBRE_FISCAL'],
  5559. 'INFI_XRFC' => $responseData['RFC'],
  5560. 'INFI_COEL' => $responseData['CORREO_ELECTRONICO'],
  5561. 'INFI_CORF' => $responseData['REGIMEN_FISCAL'],
  5562. 'INFI_LAD1' => $responseData['LADA1'],
  5563. 'INFI_TEL1' => $responseData['TELEFONO_FIJO'],
  5564. 'INFI_LAD2' => $responseData['LADA2'],
  5565. 'INFI_TEL2' => $responseData['TELEFONO_MOVIL'],
  5566. 'INFI_COPO' => $responseData['CODIGO_POSTAL'],
  5567. 'INFI_TIVI' => $responseData['TIPOVIALIDAD'],
  5568. 'INFI_VIAL' => $responseData['NOMBRE_VIALIDAD'],
  5569. 'INFI_ENCA' => $responseData['ENTRE_CALLE'],
  5570. 'INFI_YCAL' => $responseData['Y_CALLE'],
  5571. 'INFI_NUEX' => $responseData['NUMERO_EXTERIOR'],
  5572. 'INFI_NUIN' => $responseData['NUMERO_INTERIOR'],
  5573. 'INFI_COLO' => $responseData['COLONIA'],
  5574. 'INFI_LOCA' => $responseData['LOCALIDAD'],
  5575. 'INFI_MUNI' => $responseData['MUNICIPIO'],
  5576. 'INFI_ENFE' => $responseData['ENTIDAD_FEDERATIVA'],
  5577. 'INFI_PAIS' => $responseData['PAIS'],
  5578. 'INFI_NULI' => $responseData['LINE_NUMBER'],
  5579. 'INFI_USRE' => $user,
  5580. 'INFI_FERE' => $currentDate,
  5581. 'INFI_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  5582. ]);
  5583. } catch (\Throwable $th) {
  5584. DB::rollBack();
  5585. return $this->responseController->makeResponse(true, "ERR_SYSTEM_ADMINISTRATOR_TAX_INFORMATION004: Ocurrió un error al insertar los datos en la base de datos.", $th->getMessage(), 401);
  5586. }
  5587. if (!$validateInsert) {
  5588. DB::rollBack();
  5589. return $this->responseController->makeResponse(true, "ERR_SYSTEM_ADMINISTRATOR_TAX_INFORMATION005: No se pudo insertar los datos en la base de datos.", [], 401);
  5590. }
  5591. DB::commit();
  5592. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  5593. }
  5594. }