SystemAdministratorController.php 308 KB

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