PreventiveMaintenanceController.php 273 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Validator;
  6. use Illuminate\Support\Carbon;
  7. use Illuminate\Support\Facades\Log;
  8. use Dompdf\Dompdf;
  9. use Illuminate\Support\Facades\Storage;
  10. use Illuminate\Http\File;
  11. use PhpOffice\PhpSpreadsheet\IOFactory;
  12. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  13. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  14. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  15. use ElephantIO\Client;
  16. class PreventiveMaintenanceController extends Controller
  17. {
  18. private $responseController;
  19. private $encryptionController;
  20. private $functionsController;
  21. private $templatesUbic;
  22. private $documentManagementController;
  23. private $notificationsController;
  24. public function __construct()
  25. {
  26. $this->responseController = new ResponseController();
  27. $this->encryptionController = new EncryptionController();
  28. $this->functionsController = new FunctionsController();
  29. $this->templatesUbic = $this->functionsController->getBasePath() . "\storage\app\public\pdf_templates\\01_03_GMPR\\";
  30. $this->documentManagementController = new DocumentManagementController();
  31. $this->notificationsController = new NotificationsController();
  32. }
  33. private function getSocketClient()
  34. {
  35. $url = 'http://localhost:3200';
  36. $socketClient = new Client(Client::engine(Client::CLIENT_4X, $url));
  37. $socketClient->initialize();
  38. $socketClient->of('/');
  39. return $socketClient;
  40. }
  41. public function registerWorkOrder(Request $request)
  42. {
  43. DB::enableQueryLog();
  44. $validator = Validator::make($request->all(), [
  45. 'id_user' => 'required|string',
  46. 'linea' => 'required|integer',
  47. 'description' => 'required|string',
  48. 'instructions' => 'required|json',
  49. 'start_date' => 'required|date',
  50. 'start_hour' => 'required|string',
  51. 'end_date' => 'date',
  52. 'end_hour' => 'string',
  53. 'equipment' => 'required|string',
  54. 'inm_time' => 'required|numeric',
  55. 'total_time' => 'required|numeric',
  56. 'resources' => 'required|json',
  57. 'activator' => 'required|string',
  58. 'attached' => 'json',
  59. 'exists' => 'required|string|in:S,N',
  60. 'id_order' => 'required_if:exists,=,S|string',
  61. 'clasification' => 'required|string|max:100',
  62. 'staff' => 'required|json',
  63. ]);
  64. if ($validator->fails()) {
  65. return $this->responseController->makeResponse(
  66. true,
  67. "Se encontraron uno o más errores.",
  68. $this->responseController->makeErrors(
  69. $validator->errors()->messages()
  70. ),
  71. 401
  72. );
  73. }
  74. $form = $request->all();
  75. $idUser = $this->encryptionController->decrypt($form['id_user']);
  76. if (!$idUser) {
  77. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  78. }
  79. $usr = DB::table('S002V01TUSUA')->where([
  80. ['USUA_NULI', '=', $form['linea']],
  81. ['USUA_IDUS', '=', $idUser]
  82. ])->first();
  83. if (is_null($usr)) {
  84. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  85. }
  86. $resources = json_decode($form['resources'], true);
  87. if (empty($resources)) {
  88. return $this->responseController->makeResponse(true, 'El JSON de recursos tiene un formato inválido.', [], 400);
  89. }
  90. foreach ($resources as $key => $item) {
  91. if (!array_key_exists('ID', $item)) {
  92. return $this->responseController->makeResponse(true, "No se pudo encontrar el ID del elemento en la posición $key del arreglo de recursos.", [], 400);
  93. }
  94. if ($item['ID'] != 'SH') {
  95. $idItemDec = $this->encryptionController->decrypt($item['ID']);
  96. $resource = DB::table('S002V01TINST')->where([
  97. ['INST_NULI', '=', $form['linea']],
  98. ['INST_IDIS', '=', $idItemDec],
  99. ])->first();
  100. if (is_null($resource)) {
  101. return $this->responseController->makeResponse(true, "El elemento en la posición $key del arreglo de recursos no existe.", [], 404);
  102. }
  103. $item['ID'] = $idItemDec;
  104. }
  105. $resources[$key] = $item;
  106. }
  107. $rhre = json_encode($resources);
  108. $idActivator = $this->encryptionController->decrypt($form['activator']);
  109. if (!$idActivator) {
  110. return $this->responseController->makeResponse(true, 'El ID del activador seleccionado no fue encriptado correctamente.', [], 400);
  111. }
  112. $activator = DB::table('S002V01TACTI')->where([
  113. ['ACTI_NULI', '=', $form['linea']],
  114. ['ACTI_IDAC', '=', $idActivator]
  115. ])->join('S002V01TCONA', 'CONA_IDCO', '=', 'ACTI_CORE')->first();
  116. if (is_null($activator)) {
  117. return $this->responseController->makeResponse(true, 'El activador seleccionado no está registrado.', [], 404);
  118. }
  119. $startDateTime = "$form[start_date] $form[start_hour]";
  120. $validStartDateTime = $this->functionsController->validateDate($startDateTime);
  121. if (!$validStartDateTime) {
  122. return $this->responseController->makeResponse(true, 'La fecha u hora de inicio tienen un formato inválido.', [], 400);
  123. }
  124. $dateTimeStart = new Carbon($startDateTime);
  125. if ($activator->ACTI_TIAC == 'Calendario' || $activator->ACTI_TIAC == 'Sintoma') {
  126. $activatorConfig = json_decode($activator->ACTI_COAC, true);
  127. $activatorStartDate = explode('T', $activatorConfig['startDate'])[0];
  128. $activatorStartDateTime = "$activatorStartDate $activatorConfig[startHour]";
  129. $activatorStartDateTimeObj = new Carbon($activatorStartDateTime);
  130. if ($dateTimeStart->lt($activatorStartDateTimeObj)) {
  131. return $this->responseController->makeResponse(true, 'La fecha de inicio de la orden no puede ser menor a la fecha de inicio configurada en el activador.', [], 400);
  132. }
  133. }
  134. $ftap = '0001-01-01 00:00:00';
  135. if (isset($form['end_hour'])) {
  136. $endDateTime = "$form[end_date] $form[end_hour]";
  137. $validEndDateTime = $this->functionsController->validateDate($endDateTime);
  138. if (!$validEndDateTime) {
  139. return $this->responseController->makeResponse(true, 'La fecha u hora de término tienen un formato inválido.', [], 400);
  140. }
  141. $dateTimeEnd = new Carbon("$endDateTime");
  142. if ($dateTimeStart->gt($dateTimeEnd)) {
  143. return $this->responseController->makeResponse(true, 'La hora de término es menor a la hora de inicio.', [], 400);
  144. }
  145. $ftap = $dateTimeEnd->toDateTimeString();
  146. }
  147. $equipmentCode = $this->encryptionController->decrypt($form['equipment']);
  148. if (!$equipmentCode) {
  149. return $this->responseController->makeResponse(true, 'El código del equipamiento relacionado no fue encriptado correctamente.', [], 400);
  150. }
  151. $equipment = DB::table('S002V01TEQUI')->where([
  152. ['EQUI_NULI', '=', $form['linea']],
  153. ['EQUI_COEQ', '=', $equipmentCode]
  154. ])->first();
  155. if (is_null($equipment)) {
  156. return $this->responseController->makeResponse(true, 'El equipaiento seleccionado no existe.', [], 404);
  157. } else if ($activator->CONA_COEQ != $equipmentCode) {
  158. return $this->responseController->makeResponse(true, 'El activador seleccionado no puede ser asignado al equipamiento relacionado, ya que el contador pertenece a otro equipo.', [], 401);
  159. } else if (floatval($form['inm_time']) > floatval($form['total_time'])) {
  160. return $this->responseController->makeResponse(true, 'El tiempo de inmovilización aproximado no puede ser mayor al tiempo de duración aproximada.', [], 400);
  161. }
  162. $staffArr = json_decode($form['staff'], true);
  163. if (empty($staffArr)) {
  164. return $this->responseController->makeResponse(true, 'El JSON de personal está vacío.', [], 400);
  165. }
  166. $staff = [];
  167. foreach ($staffArr as $key => $specialty) {
  168. $specialtyDec = $this->encryptionController->decrypt($specialty['SPECIALTY']);
  169. if (!$specialtyDec) {
  170. return $this->responseController->makeResponse(true, "El código en la posición $key del arreglo de especialidades no fue encriptado correctamente.", [], 400);
  171. }
  172. $specialtyObj = DB::table('S002V01TGEES')->where([
  173. ['GEES_NULI', '=', $form['linea']],
  174. ['GEES_COES', '=', $specialtyDec]
  175. ])->first();
  176. if (is_null($specialtyObj)) {
  177. return $this->responseController->makeResponse(true, "El item en la posición $key del arreglo de especialidades no existe.", [], 404);
  178. }
  179. $staff[] = [
  180. 'ID' => $specialtyDec,
  181. 'CANT' => $specialty['CANT']
  182. ];
  183. }
  184. $instructionsArr = json_decode($form['instructions'], true);
  185. foreach ($instructionsArr as $key => $instruction) {
  186. $instructionInd = $key + 1;
  187. $instructionStartArr = explode(' ', $instruction['INICIO']);
  188. if (count($instructionStartArr) != 3) {
  189. return $this->responseController->makeResponse(true, "La fecha de inicio de la instrucción $instructionInd tiene un formato inválido.", [], 400);
  190. }
  191. $startHourArr = explode(':', $instructionStartArr[1]);
  192. $startHourInt = intval($startHourArr[0]);
  193. if ($instructionStartArr[2] == 'PM' && $startHourInt < 12) {
  194. $startHourInt = $startHourInt + 12;
  195. }
  196. $startOrigin = $instruction['INICIO'];
  197. $startHourStr = $startHourInt < 10 ? "0$startHourInt" : "$startHourInt";
  198. $instructionStartStr = "$startHourStr:$startHourArr[1]:00";
  199. $instruction['INICIO'] = "$instructionStartArr[0] $instructionStartStr";
  200. $instructionStartDateTime = new Carbon($instruction['INICIO']);
  201. if ($instructionStartDateTime->lt($dateTimeStart)) {
  202. return $this->responseController->makeResponse(true, "La fecha de inicio de la instrucción $instructionInd no puede ser menor a la fecha de inicio del activador.", [], 400);
  203. }
  204. $instructionEndArr = explode(' ', $instruction['FIN']);
  205. if (count($instructionEndArr) != 3) {
  206. return $this->responseController->makeResponse(true, "La fecha de término de la instrucción $instructionInd tiene un formato inválido.", [], 400);
  207. }
  208. $endHourArr = explode(':', $instructionEndArr[1]);
  209. $endHourInt = intval($endHourArr[0]);
  210. if ($instructionEndArr[2] == 'PM' && $endHourInt < 12) {
  211. $endHourInt = $endHourInt + 12;
  212. }
  213. $endOrigin = $instruction['FIN'];
  214. $endHourStr = $endHourInt < 10 ? "0$endHourInt" : "$endHourInt";
  215. $instructionEndStr = "$endHourStr:$endHourArr[1]:00";
  216. $instruction['FIN'] = "$instructionEndArr[0] $instructionEndStr";
  217. if (isset($form['end_hour'])) {
  218. $dateTimeEnd = new Carbon($ftap);
  219. $instructionEndDateTime = new Carbon($instruction['FIN']);
  220. if ($instructionEndDateTime->gt($dateTimeEnd)) {
  221. return $this->responseController->makeResponse(true, "La fecha de término de la instrucción $instructionInd no puede ser mayor a la fecha de término del activador.", [], 400);
  222. }
  223. }
  224. $instruction['ID'] = $this->encryptionController->decrypt($instruction['ID']);
  225. $instruction['INICIO'] = $startOrigin;
  226. $instruction['FIN'] = $endOrigin;
  227. $instructionsArr[$key] = $instruction;
  228. }
  229. $inin = json_encode($instructionsArr);
  230. $oppr = json_encode($staff);
  231. $now = $this->functionsController->now();
  232. $nowStr = $now->toDateTimeString();
  233. $fiap = $dateTimeStart->toDateTimeString();
  234. $attachedArrFn = [];
  235. if (isset($form['attached'])) {
  236. $attachedArr = json_decode($form['attached'], true);
  237. foreach ($attachedArr as $key => $attached) {
  238. $idDec = $this->encryptionController->decrypt($attached['id']);
  239. if (!$idDec) {
  240. return $this->responseController->makeResponse(true, "El ID del documento en la posición $key no fue encriptado correctamente.", [], 400);
  241. }
  242. if ($attached['type'] == 'Existente') {
  243. $codeArr = explode('=', $idDec);
  244. $codeArr0 = explode('-', $codeArr[0]);
  245. $file = DB::table('S002V01TAFAL')->where([
  246. ['AFAL_NULI', '=', $form['linea']],
  247. ['AFAL_COMO', '=', $codeArr0[1]],
  248. ['AFAL_CLDO', '=', $codeArr0[2]],
  249. ['AFAL_FECR', '=', $codeArr0[3]],
  250. ['AFAL_NUSE', '=', $codeArr0[4]],
  251. ['AFAL_NUVE', '=', $codeArr[1]],
  252. ])->first();
  253. if (is_null($file)) {
  254. return $this->responseController->makeResponse(true, "El documento en la posición $key no existe.", [], 404);
  255. } else if ($file->AFAL_ESTA == 'Eliminado') {
  256. return $this->responseController->makeResponse(true, "El documento en la posición $key está eliminado.", [], 404);
  257. }
  258. $attachedArrFn[] = $idDec;
  259. } else if ($attached['type'] == 'Nuevo') {
  260. $tempFile = DB::table('S002V01TARTE')->where([
  261. ['ARTE_IDAR', '=', $idDec],
  262. ['ARTE_NULI', '=', $form['linea']]
  263. ])->first();
  264. if (is_null($tempFile)) {
  265. return $this->responseController->makeResponse(true, "El documento en la posición $key no existe.", [], 404);
  266. } else if ($tempFile->ARTE_ESTA == 'Eliminado') {
  267. return $this->responseController->makeResponse(true, "El documento en la posición $key está eliminado.", [], 404);
  268. }
  269. $finalFile = $this->documentManagementController->moveFinalFile($form['linea'], 'GMPR', 'OR', $tempFile, $idUser);
  270. if (!$finalFile) {
  271. return $this->responseController->makeResponse(true, $finalFile[1], [], 400);
  272. } else {
  273. $attachedArrFn[] = $finalFile[1];
  274. }
  275. }
  276. }
  277. }
  278. $done = json_encode($attachedArrFn);
  279. if ($form['exists'] == 'S') {
  280. $idOrder = $this->encryptionController->decrypt($form['id_order']);
  281. if (!$idOrder) {
  282. return $this->responseController->makeResponse(true, 'El ID de la orden no fue encriptado correctamente.', [], 400);
  283. }
  284. $order = DB::table('S002V01TOTPR')->where([
  285. ['OTPR_IDOT', '=', $idOrder],
  286. ['OTPR_NULI', '=', $form['linea']]
  287. ])->first();
  288. if (is_null($order)) {
  289. return $this->responseController->makeResponse(true, 'La orden enviada no existe.', [], 404);
  290. }
  291. DB::table('S002V01TOTPR')->where([
  292. ['OTPR_IDOT', '=', $idOrder],
  293. ['OTPR_NULI', '=', $form['linea']]
  294. ])->update([
  295. 'OTPR_DEIN' => $form['description'],
  296. 'OTPR_ININ' => $inin,
  297. 'OTPR_EQIN' => $equipmentCode,
  298. 'OTPR_FIAP' => $fiap,
  299. 'OTPR_FTAP' => $ftap,
  300. 'OTPR_TIIN' => $form['inm_time'],
  301. 'OTPR_OPPR' => $oppr,
  302. 'OTPR_DTIN' => $form['total_time'],
  303. 'OTPR_RHRE' => $rhre,
  304. 'OTPR_DONE' => $done,
  305. 'OTPR_ACAS' => $idActivator,
  306. 'OTPR_CLAS' => $form['clasification'],
  307. 'OTPR_ESTA' => 'R',
  308. 'OTPR_USMO' => $idUser,
  309. 'OTPR_FEMO' => $nowStr,
  310. ]);
  311. } else {
  312. $idOrder = DB::table('S002V01TOTPR')->insertGetId([
  313. 'OTPR_NULI' => $form['linea'],
  314. 'OTPR_DEIN' => $form['description'],
  315. 'OTPR_ININ' => $inin,
  316. 'OTPR_EQIN' => $equipmentCode,
  317. 'OTPR_FIAP' => $fiap,
  318. 'OTPR_FTAP' => $ftap,
  319. 'OTPR_TIIN' => $form['inm_time'],
  320. 'OTPR_OPPR' => $oppr,
  321. 'OTPR_DTIN' => $form['total_time'],
  322. 'OTPR_RHRE' => $rhre,
  323. 'OTPR_DONE' => $done,
  324. 'OTPR_ACAS' => $idActivator,
  325. 'OTPR_CLAS' => $form['clasification'],
  326. 'OTPR_ESTA' => 'R',
  327. 'OTPR_USRE' => $idUser,
  328. 'OTPR_FERE' => $nowStr,
  329. ]);
  330. }
  331. $actions = DB::getQueryLog();
  332. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  333. $idac = $this->functionsController->registerActivity(
  334. $form['linea'],
  335. 'S002V01M10GMPR',
  336. 'S002V01F01COTP',
  337. 'S002V01P01ROTR',
  338. 'Registro',
  339. "El usuario $name (" . $usr->USUA_IDUS . ") registró la orden de trabajo #$idOrder.",
  340. $idUser,
  341. $nowStr,
  342. 'S002V01S01ORTR'
  343. );
  344. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  345. return $this->responseController->makeResponse(false, 'EXITO.');
  346. }
  347. public function getWorkOrders($idUser, $line)
  348. {
  349. DB::enableQueryLog();
  350. $idUser = $this->encryptionController->decrypt($idUser);
  351. if (!$idUser) {
  352. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  353. }
  354. $usr = DB::table('S002V01TUSUA')->where([
  355. ['USUA_NULI', '=', $line],
  356. ['USUA_IDUS', '=', $idUser],
  357. ])->first();
  358. if (is_null($usr)) {
  359. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  360. }
  361. $workOrders = DB::table('S002V01TOTPR')->select([
  362. 'OTPR_IDOT AS IDORDEN',
  363. 'OTPR_EQIN AS EQUIPO',
  364. 'OTPR_FIAP AS FECHAINICIO',
  365. 'OTPR_FTAP AS FECHAFINAL',
  366. 'OTPR_ACAS AS ACTIVADOR',
  367. 'ACTI_TIAC AS TIPOACTIVADOR',
  368. 'ACTI_PRIO AS PRIORIDAD',
  369. 'OTPR_ESTA AS ESTATUS'
  370. ])->leftJoin('S002V01TACTI', 'ACTI_IDAC', '=', 'OTPR_ACAS')
  371. ->where('OTPR_NULI', '=', $line)
  372. ->orderBy('OTPR_IDOT', 'desc')->get()->all();
  373. foreach ($workOrders as $key => $order) {
  374. $order->IDORDEN = $this->encryptionController->encrypt($order->IDORDEN);
  375. $order->EQUIPO = $this->encryptionController->encrypt($order->EQUIPO);
  376. $order->ACTIVADOR = $this->encryptionController->encrypt($order->ACTIVADOR);
  377. if (!is_null($order->PRIORIDAD)) {
  378. $order->PRIORIDAD = $this->encryptionController->encrypt($order->PRIORIDAD);
  379. }
  380. if ($order->FECHAFINAL == '0001-01-01 00:00:00') $order->FECHAFINAL = null;
  381. if ($order->FECHAINICIO == '0001-01-01 00:00:00') $order->FECHAINICIO = null;
  382. $workOrders[$key] = $order;
  383. }
  384. $now = $this->functionsController->now();
  385. $nowStr = $now->toDateTimeString();
  386. $actions = DB::getQueryLog();
  387. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  388. $idac = $this->functionsController->registerActivity(
  389. $line,
  390. 'S002V01M10GMPR',
  391. 'S002V01F01COTP',
  392. 'S002V01P01HOTP',
  393. 'Consulta',
  394. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las órdenes de trabajo registradas.",
  395. $idUser,
  396. $nowStr,
  397. 'S002V01S01ORTR'
  398. );
  399. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  400. return $this->responseController->makeResponse(false, 'EXITO.', $workOrders);
  401. }
  402. public function getWorkOrder($idOrder, $idUser, $line)
  403. {
  404. DB::enableQueryLog();
  405. $idUser = $this->encryptionController->decrypt($idUser);
  406. if (!$idUser) {
  407. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  408. }
  409. $usr = DB::table('S002V01TUSUA')->where([
  410. ['USUA_NULI', '=', $line],
  411. ['USUA_IDUS', '=', $idUser],
  412. ])->first();
  413. if (is_null($usr)) {
  414. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  415. }
  416. $idOrder = $this->encryptionController->decrypt($idOrder);
  417. if (!$idOrder) {
  418. return $this->responseController->makeResponse(true, 'El ID de la orden no está encriptado correctamente.', [], 400);
  419. }
  420. $workOrder = DB::table('S002V01TOTPR')->select([
  421. 'OTPR_IDOT AS IDORDEN',
  422. 'OTPR_DEIN AS DESCRIPCION',
  423. 'OTPR_ININ AS INSTRUCCIONES',
  424. 'OTPR_EQIN AS EQUIPAMIENTO',
  425. 'EQUI_TIPO AS TIPO_EQUIPAMIENTO',
  426. 'EQUI_MODE AS MODELO_EQUIPAMIENTO',
  427. 'EQUI_IDEQ AS ID_EQUIPAMIENTO',
  428. 'OTPR_FIAP AS FECHAINICIO',
  429. 'OTPR_FTAP AS FECHAFINAL',
  430. 'OTPR_SEAN AS ANALISIS',
  431. 'OTPR_TIIN AS TIEMPOINMOESTI',
  432. 'OTPR_CLAS AS CLASIFICACION',
  433. 'OTPR_OPPR AS OPERARIOS',
  434. 'OTPR_DTIN AS DURACIONTOTAL',
  435. 'OTPR_RHRE AS RECURSOS',
  436. 'OTPR_DONE AS DOCUMENTOS',
  437. 'OTPR_RECO AS CONTRATOS',
  438. 'OTPR_ACAS AS ACTIVADOR',
  439. 'ACTI_TIAC AS TIPOACTIVADOR',
  440. 'ACTI_PRIO AS PRIORIDAD',
  441. 'OTPR_ESTA AS ESTADO',
  442. 'OTPR_USRE AS USUARIOREGISTRA',
  443. 'OTPR_FERE AS FECHAREGISTRO',
  444. 'OTPR_USMO AS USUARIOMODIFICA',
  445. 'OTPR_FEMO AS FECHAMODIFICACION',
  446. ])->leftJoin('S002V01TEQUI', 'EQUI_COEQ', '=', 'OTPR_EQIN')
  447. ->leftJoin('S002V01TACTI', 'ACTI_IDAC', '=', 'OTPR_ACAS')->where([
  448. ['OTPR_IDOT', '=', $idOrder],
  449. ['OTPR_NULI', '=', $line],
  450. ])->first();
  451. if (is_null($workOrder)) {
  452. return $this->responseController->makeResponse(true, 'La orden de trabajo consultada no existe.', [], 404);
  453. }
  454. $workOrder->IDORDEN = $this->encryptionController->encrypt($workOrder->IDORDEN);
  455. $workOrder->EQUIPAMIENTO = $this->encryptionController->encrypt($workOrder->EQUIPAMIENTO);
  456. $workOrder->ACTIVADOR = $this->encryptionController->encrypt($workOrder->ACTIVADOR);
  457. if (!is_null($workOrder->ID_EQUIPAMIENTO)) {
  458. $workOrder->ID_EQUIPAMIENTO = $this->encryptionController->encrypt($workOrder->ID_EQUIPAMIENTO);
  459. }
  460. if (!is_null($workOrder->PRIORIDAD)) {
  461. $workOrder->PRIORIDAD = $this->encryptionController->encrypt($workOrder->PRIORIDAD);
  462. }
  463. $workOrder->FECHAFINAL = $workOrder->FECHAFINAL == '0001-01-01 00:00:00' ? null : $workOrder->FECHAFINAL;
  464. $workOrder->FECHAINICIO = $workOrder->FECHAINICIO == '0001-01-01 00:00:00' ? null : $workOrder->FECHAINICIO;
  465. $instructionsArr = json_decode($workOrder->INSTRUCCIONES, true);
  466. foreach ($instructionsArr as $key => $instruction) {
  467. $instruction['ID'] = $this->encryptionController->encrypt($instruction['ID']);
  468. $instructionsArr[$key] = $instruction;
  469. }
  470. $workOrder->INSTRUCCIONES = json_encode($instructionsArr);
  471. $staffArr = json_decode($workOrder->OPERARIOS, true);
  472. foreach ($staffArr as $key => $val) {
  473. $specialty = DB::table('S002V01TGEES')->where([
  474. ['GEES_NULI', '=', $line],
  475. ['GEES_COES', '=', $val['ID']]
  476. ])->first();
  477. $val['ID'] = $this->encryptionController->encrypt($val['ID']);
  478. $val['NAME'] = $specialty->GEES_NOES;
  479. $staffArr[$key] = $val;
  480. }
  481. $workOrder->OPERARIOS = json_encode($staffArr);
  482. $resources = json_decode($workOrder->RECURSOS, true);
  483. foreach ($resources as $key => $resource) {
  484. if ($resource['ID'] != 'SH') {
  485. $resourceObj = DB::table('S002V01TINST')->where([
  486. ['INST_NULI', '=', $line],
  487. ['INST_IDIS', '=', $resource['ID']],
  488. ])->join('S002V01TSTAR', 'STAR_IDIS', '=', 'INST_IDIS')
  489. ->join('S002V01TUNID', 'UNID_IDUN', '=', 'STAR_IDUN')->first();
  490. if (!is_null($resourceObj)) {
  491. $resource['NAME'] = $resourceObj->INST_MODE;
  492. $resource['UNIT'] = $resourceObj->UNID_NOMB;
  493. }
  494. $resource['ID'] = $this->encryptionController->encrypt($resource['ID']);
  495. }
  496. $resources[$key] = $resource;
  497. }
  498. $workOrder->RECURSOS = json_encode($resources);
  499. $documentsArr = json_decode($workOrder->DOCUMENTOS, true);
  500. $documentsFn = [];
  501. foreach ($documentsArr as $document) {
  502. $documentArr = explode('=', $document);
  503. $codeArr = explode('-', $documentArr[0]);
  504. $file = DB::table('S002V01TAFAL')->where([
  505. ['AFAL_NULI', '=', $line],
  506. ['AFAL_COMO', '=', $codeArr[1]],
  507. ['AFAL_CLDO', '=', $codeArr[2]],
  508. ['AFAL_FECR', '=', $codeArr[3]],
  509. ['AFAL_NUSE', '=', $codeArr[4]],
  510. ['AFAL_NUVE', '=', $documentArr[1]]
  511. ])->first();
  512. $documentsFn[] = [
  513. 'id' => $this->encryptionController->encrypt($document),
  514. 'name' => $file->AFAL_NOAR . '.' . $file->AFAL_EXTE,
  515. 'size' => $file->AFAL_TAMA,
  516. 'type' => 'Existente'
  517. ];
  518. }
  519. $workOrder->DOCUMENTOS = json_encode($documentsFn);
  520. if (!is_null($workOrder->ANALISIS)) {
  521. $analysisArr = json_decode($workOrder->ANALISIS, true);
  522. $teamsConf = $analysisArr['teamsConf'];
  523. foreach ($teamsConf as $key => $val) {
  524. $idEnc = $this->encryptionController->encrypt($val['ID']);
  525. $val['ID'] = $idEnc;
  526. $teamsConf[$key] = $val;
  527. }
  528. $analysisArr['teamsConf'] = $teamsConf;
  529. $workOrder->ANALISIS = json_encode($analysisArr);
  530. }
  531. $usrReg = DB::table('S002V01TUSUA')->where([
  532. ['USUA_NULI', '=', $line],
  533. ['USUA_IDUS', '=', $workOrder->USUARIOREGISTRA],
  534. ])->first();
  535. $nameReg = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  536. $workOrder->USUARIOREGISTRA = $nameReg . " (" . $workOrder->USUARIOREGISTRA . ")";
  537. if (!is_null($workOrder->USUARIOMODIFICA)) {
  538. $usrMod = DB::table('S002V01TUSUA')->where([
  539. ['USUA_NULI', '=', $line],
  540. ['USUA_IDUS', '=', $workOrder->USUARIOMODIFICA],
  541. ])->first();
  542. $nameMod = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  543. $workOrder->USUARIOMODIFICA = $nameMod . " (" . $workOrder->USUARIOMODIFICA . ")";
  544. }
  545. $now = $this->functionsController->now();
  546. $nowStr = $now->toDateTimeString();
  547. $actions = DB::getQueryLog();
  548. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  549. $idac = $this->functionsController->registerActivity(
  550. $line,
  551. 'S002V01M10GMPR',
  552. 'S002V01F01COTP',
  553. 'S002V01P04COIN',
  554. 'Consulta',
  555. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la orden de trabajo #$idOrder.",
  556. $idUser,
  557. $nowStr,
  558. 'S002V01S01ORTR'
  559. );
  560. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  561. return $this->responseController->makeResponse(false, 'EXITO.', $workOrder);
  562. }
  563. public function executePreventiveWorkOrder(Request $request)
  564. {
  565. DB::enableQueryLog();
  566. $validator = Validator::make($request->all(), [
  567. 'id_user' => 'required|string',
  568. 'linea' => 'required|integer',
  569. 'id_order' => 'required|string',
  570. 'configuration' => 'required|string',
  571. 'execution_date' => 'required|date'
  572. ]);
  573. if ($validator->fails()) {
  574. return $this->responseController->makeResponse(
  575. true,
  576. "Se encontraron uno o más errores.",
  577. $this->responseController->makeErrors(
  578. $validator->errors()->messages()
  579. ),
  580. 401
  581. );
  582. }
  583. $form = $request->all();
  584. $idUser = $this->encryptionController->decrypt($form['id_user']);
  585. if (!$idUser) {
  586. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  587. }
  588. $usr = DB::table('S002V01TUSUA')->where([
  589. ['USUA_NULI', '=', $form['linea']],
  590. ['USUA_IDUS', '=', $idUser]
  591. ])->first();
  592. if (is_null($usr)) {
  593. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  594. }
  595. $idOrder = $this->encryptionController->decrypt($form['id_order']);
  596. if (!$idOrder) {
  597. return $this->responseController->makeResponse(true, 'El ID de la orden seleccionada no fue encriptado correctamente.', [], 400);
  598. }
  599. $workOrder = DB::table('S002V01TOTPR')->where([
  600. ['OTPR_NULI', '=', $form['linea']],
  601. ['OTPR_IDOT', '=', $idOrder]
  602. ])->first();
  603. if (is_null($workOrder)) {
  604. return $this->responseController->makeResponse(true, 'La orden solicitada no existe.', [], 404);
  605. } else if ($workOrder->OTPR_ESTA == 'Eliminado') {
  606. return $this->responseController->makeResponse(true, 'La orden solicitada está eliminada.', [], 404);
  607. }
  608. $configurationStr = $this->encryptionController->decrypt($form['configuration']);
  609. if (!$configurationStr) {
  610. return $this->responseController->makeResponse(true, 'La cadena de configuración no fue encriptada correctamente.', [], 400);
  611. }
  612. $configurationArr = json_decode($configurationStr, true);
  613. $staffArr = [];
  614. foreach ($configurationArr as $key => $value) {
  615. $specialty = DB::table('S002V01TGEES')->where([
  616. ['GEES_NULI', '=', $form['linea']],
  617. ['GEES_COES', '=', $key]
  618. ])->first();
  619. if (!is_null($specialty)) {
  620. $specialtyEmployees = [];
  621. foreach ($value as $key0 => $value0) {
  622. $idEmployeeDec = $this->encryptionController->decrypt($value0);
  623. if (!$idEmployeeDec) {
  624. return $this->responseController->makeResponse(true, "El epmleado en la posición $key0 del arreglo de la especialidad $key no fue encriptado correctamente.", [], 400);
  625. }
  626. $employee = DB::table('S002V01TPERS')->where([
  627. ['PERS_NULI', '=', $form['linea']],
  628. ['PERS_IDPE', '=', $idEmployeeDec]
  629. ])->first();
  630. if (is_null($employee)) {
  631. return $this->responseController->makeResponse(true, "El epmleado en la posición $key0 del arreglo de la especialidad $key no existe.", [], 404);
  632. } else if ($employee->PERS_ESTA == 'Eliminado') {
  633. return $this->responseController->makeResponse(true, "El epmleado en la posición $key0 del arreglo de la especialidad $key está eliminado.", [], 404);
  634. }
  635. $specialtyEmployees[] = $idEmployeeDec;
  636. }
  637. $staffArr[$key] = $specialtyEmployees;
  638. }
  639. }
  640. $now = $this->functionsController->now();
  641. $nowStr = $now->toDateTimeString();
  642. $staffStr = json_encode($staffArr);
  643. $validExecutionDate = $this->functionsController->validateDate($form['execution_date']);
  644. if (!$validExecutionDate) {
  645. return $this->responseController->makeResponse(true, 'La fecha de ejecución tiene un formato inválido.', [], 400);
  646. }
  647. $execDate = new Carbon($form['execution_date']);
  648. $fecIni = new Carbon($workOrder->OTPR_FIAP);
  649. if ($fecIni->gt($execDate)) {
  650. $tmp = $fecIni->toDateTimeString() . '|' . $execDate->toDateTimeString();
  651. return $this->responseController->makeResponse(true, 'La fecha de ejecución es menor a la fecha de inicio.' . $tmp, [], 400);
  652. }
  653. $execDateStr = $execDate->toDateString();
  654. $execution = DB::table('S002V01TBEOT')->where([
  655. ['BEOT_NULI', '=', $form['linea']],
  656. ['BEOT_IDOT', '=', $form['id_order']],
  657. ['BEOT_FEPR', '=', $execDateStr]
  658. ])->first();
  659. if (!is_null($execution)) {
  660. $estatus = $execution->BEOT_TIAC;
  661. if ($estatus == 'Ejecucion') {
  662. return $this->responseController->makeResponse(true, "La ejecución de la orden para la fecha $execDateStr ya fue realizada.", [], 401);
  663. } else if ($estatus == 'Cancelacion') {
  664. return $this->responseController->makeResponse(true, "La ejecución de la orden para la fecha $execDateStr fue cancelada.", [], 401);
  665. } else if ($estatus == 'Finalizado') {
  666. return $this->responseController->makeResponse(true, "La ejecución de la orden para la fecha $execDateStr ya ha finalizado.", [], 401);
  667. }
  668. }
  669. DB::table('S002V01TBEOT')->insert([
  670. 'BEOT_NULI' => $form['linea'],
  671. 'BEOT_IDOT' => $idOrder,
  672. 'BEOT_FEPR' => $execDateStr,
  673. 'BEOT_TIAC' => 'Ejecucion',
  674. 'BEOT_OPER' => $staffStr,
  675. 'BEOT_FEEJ' => $nowStr,
  676. 'BEOT_USEJ' => $idUser,
  677. ]);
  678. $actions = DB::getQueryLog();
  679. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  680. $idac = $this->functionsController->registerActivity(
  681. $form['linea'],
  682. 'S002V01M10GMPR',
  683. 'S002V01F02EMOT',
  684. '-',
  685. 'Registro',
  686. "El usuario $name (" . $usr->USUA_IDUS . ") ejecutó la orden de trabajo #$form[id_order] programada para la fecha $execDateStr.",
  687. $idUser,
  688. $nowStr,
  689. 'S002V01S01ORTR'
  690. );
  691. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  692. return $this->responseController->makeResponse(false, 'EXITO.');
  693. }
  694. public function cancelWorkOrder(Request $request)
  695. {
  696. //PENDIENTE REVISAR
  697. DB::enableQueryLog();
  698. $validator = Validator::make($request->all(), [
  699. 'id_user' => 'required|string',
  700. 'linea' => 'required|integer',
  701. 'id_order' => 'required|integer',
  702. 'execution_date' => 'required|date',
  703. 'observations' => 'required|string',
  704. ]);
  705. if ($validator->fails()) {
  706. return $this->responseController->makeResponse(
  707. true,
  708. "Se encontraron uno o más errores.",
  709. $this->responseController->makeErrors(
  710. $validator->errors()->messages()
  711. ),
  712. 401
  713. );
  714. }
  715. $form = $request->all();
  716. $idUser = $this->encryptionController->decrypt($form['id_user']);
  717. if (!$idUser) {
  718. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  719. }
  720. $usr = DB::table('S002V01TUSUA')->where([
  721. ['USUA_NULI', '=', $form['linea']],
  722. ['USUA_IDUS', '=', $idUser]
  723. ])->first();
  724. if (is_null($usr)) {
  725. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  726. }
  727. $workOrder = DB::table('S002V01TOTPR')->where([
  728. ['OTPR_NULI', '=', $form['linea']],
  729. ['OTPR_IDOT', '=', $form['id_order']]
  730. ])->first();
  731. if (is_null($workOrder)) {
  732. return $this->responseController->makeResponse(true, 'La orden solicitada no existe', [], 404);
  733. }
  734. $execDate = new Carbon($form['execution_date']);
  735. $fecIni = new Carbon($workOrder->OTPR_FIAP);
  736. if ($fecIni->gt($execDate)) {
  737. return $this->responseController->makeResponse(true, 'La fecha de ejecución es menor a la fecha de inicio.', [], 400);
  738. }
  739. $execDateStr = $execDate->toDateString();
  740. $execution = DB::table('S002V01TBEOT')->where([
  741. ['BEOT_NULI', '=', $form['linea']],
  742. ['BEOT_IDOT', '=', $form['id_order']],
  743. ['BEOT_FEPR', '=', $execDateStr]
  744. ])->first();
  745. if (!is_null($execution)) {
  746. $estatus = $execution->BEOT_TIAC;
  747. if ($estatus == 'Ejecucion') {
  748. return $this->responseController->makeResponse(true, "La programación de la orden para la fecha $execDateStr no se puede cancelar porque ya se ha iniciado su ejecución.", [], 401);
  749. } else if ($estatus == 'Finalizado') {
  750. return $this->responseController->makeResponse(true, "La programación de la orden para la fecha $execDateStr no se puede cancelar porque ya se ha finalizado.", [], 401);
  751. } else if ($estatus == 'Cancelacion') {
  752. return $this->responseController->makeResponse(true, "La ejecución de la orden para la fecha $execDateStr ya ha sido cancelada.", [], 401);
  753. }
  754. }
  755. $now = $this->functionsController->now();
  756. $nowStr = $now->toDateTimeString();
  757. DB::table('S002V01TBEOT')->insert([
  758. 'BEOT_NULI' => $form['linea'],
  759. 'BEOT_IDOT' => $form['id_order'],
  760. 'BEOT_FEPR' => $execDateStr,
  761. 'BEOT_TIAC' => 'Cancelacion',
  762. 'BEOT_OBSE' => $form['observations'],
  763. 'BEOT_FEEJ' => $nowStr,
  764. 'BEOT_USEJ' => $idUser,
  765. ]);
  766. $actions = DB::getQueryLog();
  767. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  768. $idac = $this->functionsController->registerActivity(
  769. $form['linea'],
  770. 'S002V01M10GMPR',
  771. 'S002V01F02EMOT',
  772. '-',
  773. 'Registro',
  774. "El usuario $name (" . $usr->USUA_IDUS . ") canceló la orden de trabajo #$form[id_order] programada para la fecha $execDateStr.",
  775. $idUser,
  776. $nowStr,
  777. 'S002V01S01ORTR'
  778. );
  779. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  780. return $this->responseController->makeResponse(false, 'EXITO.');
  781. }
  782. public function deleteWorkOrder(Request $request)
  783. {
  784. DB::enableQueryLog();
  785. $validator = Validator::make($request->all(), [
  786. 'id_user' => 'required|string',
  787. 'linea' => 'required|integer',
  788. 'id_order' => 'required|string',
  789. ]);
  790. if ($validator->fails()) {
  791. return $this->responseController->makeResponse(
  792. true,
  793. "Se encontraron uno o más errores.",
  794. $this->responseController->makeErrors(
  795. $validator->errors()->messages()
  796. ),
  797. 401
  798. );
  799. }
  800. $form = $request->all();
  801. $idUser = $this->encryptionController->decrypt($form['id_user']);
  802. if (!$idUser) {
  803. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  804. }
  805. $usr = DB::table('S002V01TUSUA')->where([
  806. ['USUA_NULI', '=', $form['linea']],
  807. ['USUA_IDUS', '=', $idUser]
  808. ])->first();
  809. if (is_null($usr)) {
  810. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  811. }
  812. $idOrder = $this->encryptionController->decrypt($form['id_order']);
  813. if (!$idOrder) {
  814. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no fue encriptado correctamente.', [], 400);
  815. }
  816. $workOrder = DB::table('S002V01TOTPR')->where([
  817. ['OTPR_NULI', '=', $form['linea']],
  818. ['OTPR_IDOT', '=', $idOrder]
  819. ])->first();
  820. if (is_null($workOrder)) {
  821. return $this->responseController->makeResponse(true, 'La orden solicitada no existe', [], 404);
  822. } else if ($workOrder->OTPR_ESTA == 'E') {
  823. return $this->responseController->makeResponse(true, 'La orden solicitada está eliminada', [], 401);
  824. }
  825. $now = $this->functionsController->now();
  826. $nowStr = $now->toDateTimeString();
  827. DB::table('S002V01TOTPR')->where([
  828. ['OTPR_IDOT', '=', $idOrder],
  829. ['OTPR_NULI', '=', $form['linea']]
  830. ])->update([
  831. 'OTPR_ESTA' => 'E',
  832. 'OTPR_USMO' => $idUser,
  833. 'OTPR_FEMO' => $nowStr,
  834. ]);
  835. $actions = DB::getQueryLog();
  836. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  837. $idac = $this->functionsController->registerActivity(
  838. $form['linea'],
  839. 'S002V01M10GMPR',
  840. 'S002V01F04EOTP',
  841. '-',
  842. 'Eliminación',
  843. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó la orden de trabajo #$form[id_order].",
  844. $idUser,
  845. $nowStr,
  846. 'S002V01S01ORTR'
  847. );
  848. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  849. return $this->responseController->makeResponse(false, 'EXITO.');
  850. }
  851. public function updateWorkOrder(Request $request)
  852. {
  853. //PENDIENTE ELIMINAR
  854. DB::enableQueryLog();
  855. $validator = Validator::make($request->all(), [
  856. 'id_user' => 'required|string',
  857. 'linea' => 'required|integer',
  858. 'id_order' => 'required|integer',
  859. 'title' => 'required|string',
  860. 'repeat' => 'required|json',
  861. 'start_date' => 'required|date',
  862. 'start_hour' => 'required|string',
  863. 'inm_time' => 'required|numeric',
  864. 'description' => 'required|string|max:100',
  865. 'clasification' => 'required|string|max:50',
  866. 'priority' => 'required|string|in:1,2,3',
  867. 'equipment' => 'required|string',
  868. 'analisis' => 'required|string',
  869. 'specialties' => 'required|json',
  870. 'attached' => 'json',
  871. ]);
  872. if ($validator->fails()) {
  873. return $this->responseController->makeResponse(
  874. true,
  875. "Se encontraron uno o más errores.",
  876. $this->responseController->makeErrors(
  877. $validator->errors()->messages()
  878. ),
  879. 401
  880. );
  881. }
  882. $form = $request->all();
  883. $idUser = $this->encryptionController->decrypt($form['id_user']);
  884. if (!$idUser) {
  885. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  886. }
  887. $usr = DB::table('S002V01TUSUA')->where([
  888. ['USUA_NULI', '=', $form['linea']],
  889. ['USUA_IDUS', '=', $idUser]
  890. ])->first();
  891. if (is_null($usr)) {
  892. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  893. }
  894. $workOrder = DB::table('S002V01TOTPR')->where([
  895. ['OTPR_NULI', '=', $form['linea']],
  896. ['OTPR_IDOT', '=', $form['id_order']]
  897. ])->first();
  898. if (is_null($workOrder)) {
  899. return $this->responseController->makeResponse(true, 'La orden solicitada no existe', [], 404);
  900. }
  901. $repeArr = json_decode($form['repeat'], true);
  902. if (empty($repeArr)) {
  903. return $this->responseController->makeResponse(true, 'El JSON de repetición tiene un formato inválido.', [], 400);
  904. } else if (!array_key_exists('type', $repeArr)) {
  905. return $this->responseController->makeResponse(true, 'El campo type no fue enviado en el JSON de repetición.', [], 400);
  906. } else if (!array_key_exists('config', $repeArr)) {
  907. return $this->responseController->makeResponse(true, 'El campo config no fue enviado en el JSON de repetición.', [], 400);
  908. }
  909. $hourArr = explode(' ', $form['start_hour']);
  910. $timeArr = '';
  911. if (count($hourArr) < 2) {
  912. return $this->responseController->makeResponse(true, 'La hora de inicio tiene un formato inválido.', [], 400);
  913. } else if ($hourArr[1] == 'PM') {
  914. $hour = explode(':', $hourArr[0]);
  915. $h = intval($hour[0]);
  916. if ($h != 12) {
  917. $h = $h + 12;
  918. }
  919. $hourStr = "$h:$hour[1]:00";
  920. $timeArr .= " $hourStr";
  921. } else if ($hourArr[1] == 'AM') {
  922. $hour = explode(':', $hourArr[0]);
  923. $h = intval($hour[0]);
  924. $hStr = $h < 10 ? "0$h" : "$h";
  925. $hStr .= ":$hour[1]:00";
  926. $timeArr .= " $hStr";
  927. } else {
  928. return $this->responseController->makeResponse(true, 'No se pudo determinar el periodo del horario.', [], 400);
  929. }
  930. $specialtiesArr = json_decode($form['specialties'], true);
  931. if (empty($specialtiesArr)) {
  932. return $this->responseController->makeResponse(true, 'El JSON de especialidades tiene un formato inválido.', [], 400);
  933. }
  934. foreach ($specialtiesArr as $specialty) {
  935. if (!array_key_exists('specialty', $specialty)) {
  936. return $this->responseController->makeResponse(true, 'El campo specialty no fue enviado en el JSON de especialidad.', [], 400);
  937. } else if (!array_key_exists('type', $specialty)) {
  938. return $this->responseController->makeResponse(true, 'El campo type no fue enviado en el JSON de especialidad.', [], 400);
  939. } else if (!array_key_exists('staff', $specialty)) {
  940. return $this->responseController->makeResponse(true, 'El campo staff no fue enviado en el JSON de especialidad.', [], 400);
  941. }
  942. }
  943. $now = $this->functionsController->now();
  944. $nowStr = $now->toDateTimeString();
  945. $documents = isset($form['attached']) ? $form['attached'] : null;
  946. DB::table('S002V01TOTPR')->where([
  947. ['OTPR_IDOT', '=', $form['id_order']],
  948. ['OTPR_NULI', '=', $form['linea']],
  949. ])->update([
  950. 'OTPR_TIOR' => $form['title'],
  951. 'OTPR_REPE' => $form['repeat'],
  952. 'OTPR_FIIN' => $form['start_date'],
  953. 'OTPR_HIIN' => $timeArr,
  954. 'OTPR_TIES' => $form['inm_time'],
  955. 'OTPR_DEIN' => $form['description'],
  956. 'OTPR_ANIN' => $form['analisis'],
  957. 'OTPR_CEIN' => $form['equipment'],
  958. 'OTPR_PERE' => $form['specialties'],
  959. 'OTPR_CLAS' => $form['clasification'],
  960. 'OTPR_PRIO' => $form['priority'],
  961. 'OTPR_DONE' => $documents,
  962. 'OTPR_USRE' => $idUser,
  963. 'OTPR_FERE' => $nowStr,
  964. ]);
  965. $actions = DB::getQueryLog();
  966. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  967. $idac = $this->functionsController->registerActivity(
  968. $form['linea'],
  969. 'S002V01M10GMPR',
  970. 'S002V01F01COTP',
  971. 'S002V01P01ROTR',
  972. 'Actualización',
  973. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó la orden de trabajo $form[title] ($form[id_order]).",
  974. $idUser,
  975. $nowStr,
  976. 'S002V01S01ORTR'
  977. );
  978. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  979. return $this->responseController->makeResponse(false, 'EXITO.');
  980. }
  981. public function geStartedtWorkOrders($idUser, $line)
  982. {
  983. //PENDIENTE REVISAR
  984. DB::enableQueryLog();
  985. $idUser = $this->encryptionController->decrypt($idUser);
  986. if (!$idUser) {
  987. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  988. }
  989. $usr = DB::table('S002V01TUSUA')->where([
  990. ['USUA_NULI', '=', $line],
  991. ['USUA_IDUS', '=', $idUser]
  992. ])->first();
  993. if (is_null($usr)) {
  994. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  995. }
  996. $startedWorkOrders = DB::table('S002V01TBEOT')->select([
  997. 'BEOT_IDRE AS IDREGISTRO',
  998. 'BEOT_IDOT AS IDORDEN',
  999. 'BEOT_FEPR AS FECHAPROGRAMA',
  1000. 'BEOT_FEEJ AS FECHAEJECUCION',
  1001. 'BEOT_USEJ AS USUARIOEJECUCION',
  1002. 'BEOT_TIAC AS ESTADO',
  1003. 'OTPR_TIOR AS TITULOORDEN',
  1004. 'OTPR_HIIN AS HORAPROGRAMA',
  1005. 'OTPR_TIES AS TIEMPOESTIMADO',
  1006. 'OTPR_PRIO AS PRIORIDAD',
  1007. ])->join('S002V01TOTPR', 'OTPR_IDOT', '=', 'BEOT_IDOT')->where([
  1008. ['BEOT_NULI', '=', $line],
  1009. ])->get()->all();
  1010. foreach ($startedWorkOrders as $workOrder) {
  1011. $usrEje = DB::table('S002V01TUSUA')->where([
  1012. ['USUA_NULI', '=', $line],
  1013. ['USUA_IDUS', '=', $workOrder->USUARIOEJECUCION]
  1014. ])->first();
  1015. $nameEje = $this->functionsController->joinName($usrEje->USUA_NOMB, $usrEje->USUA_APPA, $usrEje->USUA_APMA);
  1016. $workOrder->USUARIOEJECUCION = $nameEje . " (" . $workOrder->USUARIOEJECUCION . ")";
  1017. }
  1018. $now = $this->functionsController->now();
  1019. $nowStr = $now->toDateTimeString();
  1020. $actions = DB::getQueryLog();
  1021. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1022. $idac = $this->functionsController->registerActivity(
  1023. $line,
  1024. 'S002V01M10GMPR',
  1025. 'S002V01F05BEOT',
  1026. 'S002V01P01HOEJ',
  1027. 'Consulta',
  1028. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las órdenes de trabajo en ejecución.",
  1029. $idUser,
  1030. $nowStr,
  1031. 'S002V01S01ORTR'
  1032. );
  1033. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1034. return $this->responseController->makeResponse(false, 'EXITO.', $startedWorkOrders);
  1035. }
  1036. public function endOrderExecution(Request $request)
  1037. {
  1038. DB::enableQueryLog();
  1039. $validator = Validator::make($request->all(), [
  1040. 'id_user' => 'required|string',
  1041. 'linea' => 'required|integer',
  1042. 'id_order' => 'required|integer',
  1043. 'id_reg' => 'required|integer',
  1044. 'observations' => 'required|string|min:15',
  1045. ]);
  1046. if ($validator->fails()) {
  1047. return $this->responseController->makeResponse(
  1048. true,
  1049. "Se encontraron uno o más errores.",
  1050. $this->responseController->makeErrors(
  1051. $validator->errors()->messages()
  1052. ),
  1053. 401
  1054. );
  1055. }
  1056. $form = $request->all();
  1057. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1058. if (!$idUser) {
  1059. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1060. }
  1061. $usr = DB::table('S002V01TUSUA')->where([
  1062. ['USUA_NULI', '=', $form['linea']],
  1063. ['USUA_IDUS', '=', $idUser]
  1064. ])->first();
  1065. if (is_null($usr)) {
  1066. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1067. }
  1068. $workOrder = DB::table('S002V01TOTPR')->where([
  1069. ['OTPR_NULI', '=', $form['linea']],
  1070. ['OTPR_IDOT', '=', $form['id_order']]
  1071. ])->first();
  1072. if (is_null($workOrder)) {
  1073. return $this->responseController->makeResponse(true, 'La orden solicitada no existe', [], 404);
  1074. }
  1075. $register = DB::table('S002V01TBEOT')->where([
  1076. ['BEOT_NULI', '=', $form['linea']],
  1077. ['BEOT_IDRE', '=', $form['id_reg']],
  1078. ['BEOT_IDOT', '=', $form['id_order']]
  1079. ])->first();
  1080. if (is_null($register)) {
  1081. return $this->responseController->makeResponse(true, 'El registro solicitado no existe', [], 404);
  1082. } else if ($register->BEOT_TIAC == 'Cancelacion') {
  1083. return $this->responseController->makeResponse(true, 'El registro solicitado fue cancelado', [], 401);
  1084. } else if ($register->BEOT_TIAC == 'Finalizado') {
  1085. return $this->responseController->makeResponse(true, 'El registro solicitado ya fue finalizado', [], 401);
  1086. }
  1087. $now = $this->functionsController->now();
  1088. $executionDate = new Carbon($register->BEOT_FEEJ);
  1089. $diff = $now->diffInMilliseconds($executionDate); //Milisegundos
  1090. $diff = $diff / 1000; //Segundos
  1091. $diff = $diff / 60; //Minutos
  1092. $diff = $diff / 60; //Horas
  1093. $nowStr = $now->toDateTimeString();
  1094. DB::table('S002V01TBEOT')->where([
  1095. ['BEOT_NULI', '=', $form['linea']],
  1096. ['BEOT_IDRE', '=', $form['id_reg']],
  1097. ['BEOT_IDOT', '=', $form['id_order']]
  1098. ])->update([
  1099. "BEOT_TIAC" => 'Finalizado',
  1100. "BEOT_DTEJ" => $diff,
  1101. "BEOT_OBSE" => $form['observations'],
  1102. "BEOT_FEFI" => $nowStr,
  1103. "BEOT_USFI" => $idUser,
  1104. ]);
  1105. $actions = DB::getQueryLog();
  1106. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1107. $idac = $this->functionsController->registerActivity(
  1108. $form['linea'],
  1109. 'S002V01M10GMPR',
  1110. 'S002V01F05SEOR',
  1111. '-',
  1112. 'Actualización',
  1113. "El usuario $name (" . $usr->USUA_IDUS . ") finalizó la ejecución del día " . $register->BEOT_FEPR . " de la orden #$form[id_order].",
  1114. $idUser,
  1115. $nowStr,
  1116. 'S002V01S02AOTR'
  1117. );
  1118. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1119. return $this->responseController->makeResponse(false, 'EXITO.');
  1120. }
  1121. public function getExecRegister($date, $idRegister, $idUser, $line)
  1122. {
  1123. DB::enableQueryLog();
  1124. $idUser = $this->encryptionController->decrypt($idUser);
  1125. if (!$idUser) {
  1126. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  1127. }
  1128. $usr = DB::table('S002V01TUSUA')->where([
  1129. ['USUA_NULI', '=', $line],
  1130. ['USUA_IDUS', '=', $idUser],
  1131. ])->first();
  1132. if (is_null($usr)) {
  1133. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  1134. }
  1135. $idRegister = $this->encryptionController->decrypt($idRegister);
  1136. if (!$idRegister) {
  1137. return $this->responseController->makeResponse(true, 'El ID de la orden relacionada no fue encriptado correctamente.', [], 400);
  1138. }
  1139. $dateArr = explode('|', $date);
  1140. $workOrder = null;
  1141. if (count($dateArr) == 2) {
  1142. if ($dateArr[1] == 'O') {
  1143. $workOrder = DB::table('S002V01TOTPR')->select(
  1144. 'OTPR_IDOT AS IDORDEN',
  1145. 'OTPR_DEIN AS DESCRIPCION',
  1146. DB::raw("CONCAT(
  1147. OTPR_EQIN, ' - ',
  1148. EQUI_TIPO, ' - ',
  1149. EQUI_MODE, ' (',
  1150. EQUI_IDEQ, ')'
  1151. ) AS EQUIPAMIENTO"),
  1152. 'OTPR_FIAP AS FECHAINICIO',
  1153. DB::raw("'Programada' AS ESTADODEEJECUCION"),
  1154. DB::raw("'-' AS IDREG"),
  1155. DB::raw("NULL AS OBSERVACIONES"),
  1156. DB::raw("NULL AS OPERARIOS"),
  1157. )->join('S002V01TEQUI', 'EQUI_COEQ', '=', 'OTPR_EQIN')->where([
  1158. ['OTPR_NULI', '=', $line],
  1159. ['OTPR_IDOT', '=', $idRegister],
  1160. ])->first();
  1161. } else {
  1162. $validDate = $this->functionsController->validDate($dateArr[0]);
  1163. if (!$validDate) {
  1164. return $this->responseController->makeResponse(true, 'La fecha de ejecución enviada tiene un formato inválido.', [], 400);
  1165. }
  1166. $workOrder = DB::table('S002V01TBEOT')->select([
  1167. 'BEOT_IDOT AS IDORDEN',
  1168. 'OTPR_DEIN AS DESCRIPCION',
  1169. DB::raw("CONCAT(
  1170. OTPR_EQIN, ' - ',
  1171. EQUI_TIPO, ' - ',
  1172. EQUI_MODE, ' (',
  1173. EQUI_IDEQ, ')'
  1174. ) AS EQUIPAMIENTO"),
  1175. 'OTPR_FIAP AS FECHAINICIO',
  1176. 'BEOT_TIAC AS ESTADODEEJECUCION',
  1177. 'BEOT_IDRE AS IDREG',
  1178. 'BEOT_OBSE AS OBSERVACIONES',
  1179. 'BEOT_OPER AS OPERARIOS'
  1180. ])->join('S002V01TOTPR', 'OTPR_IDOT', '=', 'BEOT_IDOT')
  1181. ->join('S002V01TEQUI', 'EQUI_COEQ', '=', 'OTPR_EQIN')->where([
  1182. ['BEOT_NULI', '=', $line],
  1183. ['BEOT_IDOT', '=', $idRegister],
  1184. ['BEOT_FEPR', '=', $dateArr[0]],
  1185. ])->first();
  1186. }
  1187. } else {
  1188. $workOrder = DB::table('S002V01TBEOT')->select([
  1189. 'BEOT_IDOT AS IDORDEN',
  1190. 'OTPR_DEIN AS DESCRIPCION',
  1191. DB::raw("CONCAT(
  1192. OTPR_EQIN, ' - ',
  1193. EQUI_TIPO, ' - ',
  1194. EQUI_MODE, ' (',
  1195. EQUI_IDEQ, ')'
  1196. ) AS EQUIPAMIENTO"),
  1197. 'OTPR_FIAP AS FECHAINICIO',
  1198. 'BEOT_TIAC AS ESTADODEEJECUCION',
  1199. 'BEOT_IDRE AS IDREG',
  1200. 'BEOT_OBSE AS OBSERVACIONES',
  1201. 'BEOT_OPER AS OPERARIOS'
  1202. ])->join('S002V01TOTPR', 'OTPR_IDOT', '=', 'BEOT_IDOT')
  1203. ->join('S002V01TEQUI', 'EQUI_COEQ', '=', 'OTPR_EQIN')->where([
  1204. ['BEOT_NULI', '=', $line],
  1205. ['BEOT_IDRE', '=', $idRegister],
  1206. ])->first();
  1207. }
  1208. if (is_null($workOrder)) {
  1209. return $this->responseController->makeResponse(true, 'La orden de trabajo solicitada no existe.', [], 404);
  1210. }
  1211. $idOrder = $workOrder->IDORDEN;
  1212. $workOrder->IDORDEN = $this->encryptionController->encrypt($workOrder->IDORDEN);
  1213. $workOrder->EQUIPAMIENTO = $this->encryptionController->encrypt($workOrder->EQUIPAMIENTO);
  1214. $workOrder->IDREG = $this->encryptionController->encrypt($workOrder->IDREG);
  1215. if (!is_null($workOrder->OPERARIOS)) {
  1216. $staffArr = json_decode($workOrder->OPERARIOS);
  1217. $staffArrFn = [];
  1218. foreach ($staffArr as $key => $val) {
  1219. $specialtyObj = DB::table('S002V01TGEES')->where([
  1220. ['GEES_NULI', '=', $line],
  1221. ['GEES_COES', '=', $key]
  1222. ])->first();
  1223. $specialtyConf = [];
  1224. if (!is_null($specialtyObj)) {
  1225. $specialtyConf['CODIGO_ESPECIALIDAD'] = $this->encryptionController->encrypt($specialtyObj->GEES_COES);
  1226. $specialtyConf['NOMBRE_ESPECIALIDAD'] = $specialtyObj->GEES_NOES;
  1227. $staff = [];
  1228. foreach ($val as $val0) {
  1229. $employee = DB::table('S002V01TPERS')->join('S002V01TUSUA', 'USUA_IDUS', '=', 'PERS_IDUS')->where([
  1230. ['PERS_NULI', '=', $line],
  1231. ['PERS_IDPE', '=', $val0]
  1232. ])->first();
  1233. $employeeObj = [
  1234. 'ID_EMPLEADO' => $this->encryptionController->encrypt($employee->PERS_IDPE),
  1235. 'ID_USUARIO' => $this->encryptionController->encrypt($employee->USUA_IDUS),
  1236. 'NOMBRE_EMPLEADO' => $this->functionsController->joinName($employee->USUA_NOMB, $employee->USUA_APPA, $employee->USUA_APMA),
  1237. 'TIPO_EMPLEADO' => $employee->PERS_TICO
  1238. ];
  1239. $staff[] = $employeeObj;
  1240. }
  1241. $specialtyConf['OPERARIOS'] = $staff;
  1242. }
  1243. $staffArrFn[] = $specialtyConf;
  1244. }
  1245. $workOrder->OPERARIOS = $staffArrFn;
  1246. }
  1247. $now = $this->functionsController->now();
  1248. $nowStr = $now->toDateTimeString();
  1249. $actions = DB::getQueryLog();
  1250. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1251. $idac = $this->functionsController->registerActivity(
  1252. $line,
  1253. 'S002V01M10GMPR',
  1254. 'S002V01F08VCPR',
  1255. '-',
  1256. 'Consulta',
  1257. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el estado de la ejecución de la orden #" . $idOrder . " para la fecha $date.",
  1258. $idUser,
  1259. $nowStr,
  1260. 'S002V01S02AOTR'
  1261. );
  1262. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1263. return $this->responseController->makeResponse(false, 'EXITO.', $workOrder);
  1264. }
  1265. public function copyWorkOrder(Request $request)
  1266. {
  1267. DB::enableQueryLog();
  1268. $validator = Validator::make($request->all(), [
  1269. 'id_user' => 'required|string',
  1270. 'linea' => 'required|integer',
  1271. 'id_order' => 'required|string',
  1272. 'equipment' => 'required|string',
  1273. ]);
  1274. if ($validator->fails()) {
  1275. return $this->responseController->makeResponse(
  1276. true,
  1277. "Se encontraron uno o más errores.",
  1278. $this->responseController->makeErrors(
  1279. $validator->errors()->messages()
  1280. ),
  1281. 401
  1282. );
  1283. }
  1284. $form = $request->all();
  1285. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1286. if (!$idUser) {
  1287. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1288. }
  1289. $usr = DB::table('S002V01TUSUA')->where([
  1290. ['USUA_NULI', '=', $form['linea']],
  1291. ['USUA_IDUS', '=', $idUser],
  1292. ])->first();
  1293. if (is_null($usr)) {
  1294. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1295. }
  1296. $idOrder = $this->encryptionController->decrypt($form['id_order']);
  1297. if (!$idOrder) {
  1298. return $this->responseController->makeResponse(true, 'El ID de la orden que desea copiar no fue encriptado correctamente.', [], 400);
  1299. }
  1300. $workOrder = DB::table('S002V01TOTPR')->where([
  1301. ['OTPR_NULI', '=', $form['linea']],
  1302. ['OTPR_IDOT', '=', $idOrder]
  1303. ])->first();
  1304. if (is_null($workOrder)) {
  1305. return $this->responseController->makeResponse(true, 'La orden solicitada no existe.', [], 404);
  1306. }
  1307. $equipmentCode = $this->encryptionController->decrypt($form['equipment']);
  1308. if (!$equipmentCode) {
  1309. return $this->responseController->makeResponse(true, 'El código del equipamiento no fue encriptado correctamente.', [], 400);
  1310. }
  1311. $equipment = DB::table('S002V01TEQUI')->where([
  1312. ['EQUI_NULI', '=', $form['linea']],
  1313. ['EQUI_COEQ', '=', $equipmentCode],
  1314. ])->first();
  1315. if (is_null($equipment)) {
  1316. return $this->responseController->makeResponse(true, 'El equipamiento seleccionado no existe.', [], 404);
  1317. } else if ($equipmentCode == $workOrder->OTPR_EQIN) {
  1318. return $this->responseController->makeResponse(true, 'El equipo seleccionado es igual al de la orden copiada.', [], 400);
  1319. }
  1320. $now = $this->functionsController->now();
  1321. $nowStr = $now->toDateTimeString();
  1322. $idOrderNew = DB::table('S002V01TOTPR')->insertGetId([
  1323. "OTPR_NULI" => $form['linea'],
  1324. "OTPR_DEIN" => $workOrder->OTPR_DEIN,
  1325. "OTPR_ININ" => $workOrder->OTPR_ININ,
  1326. "OTPR_EQIN" => $equipmentCode,
  1327. "OTPR_FIAP" => $workOrder->OTPR_FIAP,
  1328. "OTPR_FTAP" => $workOrder->OTPR_FTAP,
  1329. "OTPR_SEAN" => $workOrder->OTPR_SEAN,
  1330. "OTPR_TIIN" => $workOrder->OTPR_TIIN,
  1331. "OTPR_OPPR" => $workOrder->OTPR_OPPR,
  1332. "OTPR_DTIN" => $workOrder->OTPR_DTIN,
  1333. "OTPR_RHRE" => $workOrder->OTPR_RHRE,
  1334. "OTPR_DONE" => '[]',
  1335. "OTPR_RECO" => $workOrder->OTPR_RECO,
  1336. "OTPR_ACAS" => $workOrder->OTPR_ACAS,
  1337. "OTPR_CLAS" => $workOrder->OTPR_CLAS,
  1338. "OTPR_ESTA" => 'R',
  1339. "OTPR_USRE" => $idUser,
  1340. "OTPR_FERE" => $nowStr,
  1341. ]);
  1342. $actions = DB::getQueryLog();
  1343. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1344. $idac = $this->functionsController->registerActivity(
  1345. $form['linea'],
  1346. 'S002V01M10GMPR',
  1347. 'S002V01F06COVE',
  1348. '-',
  1349. 'Registro',
  1350. "El usuario $name (" . $usr->USUA_IDUS . ") copió la orden #$idOrder para el equipamiento $equipmentCode dentro de la orden #$idOrderNew.",
  1351. $idUser,
  1352. $nowStr,
  1353. 'S002V01S01ORTR'
  1354. );
  1355. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1356. return $this->responseController->makeResponse(false, 'EXITO.');
  1357. }
  1358. public function savePresetWorkOrder(Request $request)
  1359. {
  1360. DB::enableQueryLog();
  1361. $validator = Validator::make($request->all(), [
  1362. 'id_user' => 'required|string',
  1363. 'linea' => 'required|integer',
  1364. 'exists' => 'required|string|in:S,N',
  1365. 'id_order' => 'required_if:exists,=,S|string',
  1366. 'description' => 'required|string',
  1367. 'instructions' => 'required|string',
  1368. 'start_date' => 'required|string',
  1369. 'start_hour' => 'required|string',
  1370. 'end_date' => 'required|string',
  1371. 'end_hour' => 'required|string',
  1372. 'equipment' => 'required|string',
  1373. 'inm_time' => 'required|string',
  1374. 'total_time' => 'required|string',
  1375. 'staff' => 'required|string',
  1376. 'attached' => 'required|string',
  1377. 'activator_type' => 'required|string',
  1378. 'activator' => 'required|string',
  1379. 'clasification' => 'required|string|max:100',
  1380. 'resources' => 'required|json',
  1381. ]);
  1382. if ($validator->fails()) {
  1383. return $this->responseController->makeResponse(
  1384. true,
  1385. "Se encontraron uno o más errores.",
  1386. $this->responseController->makeErrors(
  1387. $validator->errors()->messages()
  1388. ),
  1389. 401
  1390. );
  1391. }
  1392. $form = $request->all();
  1393. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1394. if (!$idUser) {
  1395. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1396. }
  1397. $usr = DB::table('S002V01TUSUA')->where([
  1398. ['USUA_NULI', '=', $form['linea']],
  1399. ['USUA_IDUS', '=', $idUser],
  1400. ])->first();
  1401. if (is_null($usr)) {
  1402. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1403. }
  1404. $acas = '0';
  1405. if ($form['activator'] != '-') {
  1406. $idActivator = $this->encryptionController->decrypt($form['activator']);
  1407. if (!$idActivator) {
  1408. return $this->responseController->makeResponse(true, 'El ID del activador seleccionado no fue encriptado correctamente.', [], 400);
  1409. }
  1410. $activator = DB::table('S002V01TACTI')->where([
  1411. ['ACTI_NULI', '=', $form['linea']],
  1412. ['ACTI_IDAC', '=', $idActivator]
  1413. ])->join('S002V01TCONA', 'CONA_IDCO', '=', 'ACTI_CORE')->first();
  1414. if (is_null($activator)) {
  1415. return $this->responseController->makeResponse(true, 'El activador seleccionado no está registrado.', [], 404);
  1416. }
  1417. $acas = $idActivator;
  1418. }
  1419. $rhre = '[]';
  1420. if ($form['resources'] != '[]') {
  1421. $resources = json_decode($form['resources'], true);
  1422. if (empty($resources)) {
  1423. return $this->responseController->makeResponse(true, 'El JSON de recursos tiene un formato inválido.', [], 400);
  1424. }
  1425. foreach ($resources as $key => $item) {
  1426. if (!array_key_exists('ID', $item)) {
  1427. return $this->responseController->makeResponse(true, "No se pudo encontrar el ID del elemento en la posición $key del arreglo de recursos.", [], 400);
  1428. }
  1429. if ($item['ID'] != 'SH') {
  1430. $idItemDec = $this->encryptionController->decrypt($item['ID']);
  1431. $resource = DB::table('S002V01TINST')->where([
  1432. ['INST_NULI', '=', $form['linea']],
  1433. ['INST_IDIS', '=', $idItemDec],
  1434. ])->first();
  1435. if (is_null($resource)) {
  1436. return $this->responseController->makeResponse(true, "El elemento en la posición $key del arreglo de recursos no existe.", [], 404);
  1437. }
  1438. $item['ID'] = $idItemDec;
  1439. }
  1440. $resources[$key] = $item;
  1441. }
  1442. $rhre = json_encode($resources);
  1443. }
  1444. $startDateTime = '0001-01-01 00:00:00';
  1445. if ($form['start_date'] != '-' && $form['start_hour'] != '-') {
  1446. $startDateTime = "$form[start_date] $form[start_hour]";
  1447. $validStartDateTime = $this->functionsController->validateDate($startDateTime);
  1448. if (!$validStartDateTime) {
  1449. return $this->responseController->makeResponse(true, 'La fecha u hora de inicio tienen un formato inválido.', [], 400);
  1450. }
  1451. }
  1452. $endDateTime = '0001-01-01 00:00:00';
  1453. if ($form['end_date'] != '-' && $form['end_hour'] != '-') {
  1454. $endDateTime = "$form[end_date] $form[end_hour]";
  1455. $validEndDateTime = $this->functionsController->validateDate($endDateTime);
  1456. if (!$validEndDateTime) {
  1457. return $this->responseController->makeResponse(true, 'La fecha u hora de término tienen un formato inválido.', [], 400);
  1458. }
  1459. }
  1460. $now = $this->functionsController->now();
  1461. $nowStr = $now->toDateTimeString();
  1462. $dein = $form['description'];
  1463. $eqin = '-';
  1464. if ($form['equipment'] != '-') {
  1465. $equipmentCode = $this->encryptionController->decrypt($form['equipment']);
  1466. if (!$equipmentCode) {
  1467. return $this->responseController->makeResponse(true, 'El código del equipamiento relacionado no fue encriptado correctamente.', [], 400);
  1468. }
  1469. $equipment = DB::table('S002V01TEQUI')->where([
  1470. ['EQUI_NULI', '=', $form['linea']],
  1471. ['EQUI_COEQ', '=', $equipmentCode]
  1472. ])->first();
  1473. if (is_null($equipment)) {
  1474. return $this->responseController->makeResponse(true, 'El equipaiento seleccionado no existe.', [], 404);
  1475. }
  1476. $eqin = $equipmentCode;
  1477. }
  1478. $tiin = $form['inm_time'] == '-' ? 0 : floatval($form['inm_time']);
  1479. $dtin = $form['total_time'] == '-' ? 0 : floatval($form['total_time']);
  1480. $staffArr = json_decode($form['staff'], true);
  1481. $staff = [];
  1482. if ($form['staff'] != '-' && !empty($staffArr)) {
  1483. $staffArr = json_decode($form['staff'], true);
  1484. foreach ($staffArr as $key => $specialty) {
  1485. $specialtyDec = $this->encryptionController->decrypt($specialty['SPECIALTY']);
  1486. if (!$specialtyDec) {
  1487. return $this->responseController->makeResponse(true, "El código en la posición $key del arreglo de especialidades no fue encriptado correctamente.", [], 400);
  1488. }
  1489. $specialtyObj = DB::table('S002V01TGEES')->where([
  1490. ['GEES_NULI', '=', $form['linea']],
  1491. ['GEES_COES', '=', $specialtyDec]
  1492. ])->first();
  1493. if (is_null($specialtyObj)) {
  1494. return $this->responseController->makeResponse(true, "El item en la posición $key del arreglo de especialidades no existe.", [], 404);
  1495. }
  1496. $staff[] = [
  1497. 'ID' => $specialtyDec,
  1498. 'CANT' => $specialty['CANT']
  1499. ];
  1500. }
  1501. }
  1502. $oppr = json_encode($staff);
  1503. $clas = $form['clasification'];
  1504. $inin = '[]';
  1505. if ($form['instructions'] != '-') {
  1506. $instructions = json_decode($form['instructions'], true);
  1507. foreach ($instructions as $key => $instruction) {
  1508. $instruction['ID'] = $this->encryptionController->decrypt($instruction['ID']);
  1509. $instructions[$key] = $instruction;
  1510. }
  1511. $inin = json_encode($instructions);
  1512. }
  1513. $attachedArrFn = [];
  1514. if (isset($form['attached'])) {
  1515. $attachedArr = json_decode($form['attached'], true);
  1516. foreach ($attachedArr as $key => $attached) {
  1517. $idDec = $this->encryptionController->decrypt($attached['id']);
  1518. if (!$idDec) {
  1519. return $this->responseController->makeResponse(true, "El ID del documento en la posición $key no fue encriptado correctamente.", [], 400);
  1520. }
  1521. if ($attached['type'] == 'Existente') {
  1522. $codeArr = explode('=', $idDec);
  1523. $codeArr0 = explode('-', $codeArr[0]);
  1524. $file = DB::table('S002V01TAFAL')->where([
  1525. ['AFAL_NULI', '=', $form['linea']],
  1526. ['AFAL_COMO', '=', $codeArr0[1]],
  1527. ['AFAL_CLDO', '=', $codeArr0[2]],
  1528. ['AFAL_FECR', '=', $codeArr0[3]],
  1529. ['AFAL_NUSE', '=', $codeArr0[4]],
  1530. ['AFAL_NUVE', '=', $codeArr[1]],
  1531. ])->first();
  1532. if (is_null($file)) {
  1533. return $this->responseController->makeResponse(true, "El documento en la posición $key no existe.", [], 404);
  1534. } else if ($file->AFAL_ESTA == 'Eliminado') {
  1535. return $this->responseController->makeResponse(true, "El documento en la posición $key está eliminado.", [], 404);
  1536. }
  1537. $attachedArrFn[] = $idDec;
  1538. } else if ($attached['type'] == 'Nuevo') {
  1539. $tempFile = DB::table('S002V01TARTE')->where([
  1540. ['ARTE_IDAR', '=', $idDec],
  1541. ['ARTE_NULI', '=', $form['linea']]
  1542. ])->first();
  1543. if (is_null($tempFile)) {
  1544. return $this->responseController->makeResponse(true, "El documento en la posición $key no existe.", [], 404);
  1545. } else if ($tempFile->ARTE_ESTA == 'Eliminado') {
  1546. return $this->responseController->makeResponse(true, "El documento en la posición $key está eliminado.", [], 404);
  1547. }
  1548. $finalFile = $this->documentManagementController->moveFinalFile($form['linea'], 'GMPR', 'OR', $tempFile, $idUser);
  1549. if (!$finalFile) {
  1550. return $this->responseController->makeResponse(true, $finalFile[1], [], 400);
  1551. } else {
  1552. $attachedArrFn[] = $finalFile[1];
  1553. }
  1554. }
  1555. }
  1556. }
  1557. $done = json_encode($attachedArrFn);
  1558. if ($form['exists'] == 'N') {
  1559. $idOrder = DB::table('S002V01TOTPR')->insertGetId([
  1560. 'OTPR_NULI' => $form['linea'],
  1561. 'OTPR_DEIN' => $dein,
  1562. 'OTPR_ININ' => $inin,
  1563. 'OTPR_EQIN' => $eqin,
  1564. 'OTPR_FIAP' => $startDateTime,
  1565. 'OTPR_FTAP' => $endDateTime,
  1566. 'OTPR_TIIN' => $tiin,
  1567. 'OTPR_OPPR' => $oppr,
  1568. 'OTPR_DTIN' => $dtin,
  1569. 'OTPR_RHRE' => $rhre,
  1570. 'OTPR_DONE' => $done,
  1571. 'OTPR_ACAS' => $acas,
  1572. 'OTPR_CLAS' => $clas,
  1573. 'OTPR_ESTA' => 'B',
  1574. 'OTPR_USRE' => $idUser,
  1575. 'OTPR_FERE' => $nowStr,
  1576. ]);
  1577. } else {
  1578. $idOrder = $this->encryptionController->decrypt($form['id_order']);
  1579. if (!$idOrder) {
  1580. return $this->responseController->makeResponse(true, 'El ID de la orden no fue encriptado correctamente.', [], 400);
  1581. }
  1582. $order = DB::table('S002V01TOTPR')->where([
  1583. ['OTPR_NULI', '=', $form['linea']],
  1584. ['OTPR_IDOT', '=', $idOrder],
  1585. ])->first();
  1586. if (is_null($order)) {
  1587. return $this->responseController->makeResponse(true, 'La orden enviada no existe.', [], 404);
  1588. }
  1589. DB::table('S002V01TOTPR')->where([
  1590. ['OTPR_NULI', '=', $form['linea']],
  1591. ['OTPR_IDOT', '=', $idOrder]
  1592. ])->update([
  1593. 'OTPR_DEIN' => $dein,
  1594. 'OTPR_ININ' => $inin,
  1595. 'OTPR_EQIN' => $eqin,
  1596. 'OTPR_FIAP' => $startDateTime,
  1597. 'OTPR_FTAP' => $endDateTime,
  1598. 'OTPR_TIIN' => $tiin,
  1599. 'OTPR_OPPR' => $oppr,
  1600. 'OTPR_DTIN' => $dtin,
  1601. 'OTPR_RHRE' => $rhre,
  1602. 'OTPR_DONE' => $done,
  1603. 'OTPR_ACAS' => $acas,
  1604. 'OTPR_CLAS' => $clas,
  1605. 'OTPR_USMO' => $idUser,
  1606. 'OTPR_FEMO' => $nowStr,
  1607. ]);
  1608. }
  1609. $actions = DB::getQueryLog();
  1610. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1611. $idac = $this->functionsController->registerActivity(
  1612. $form['linea'],
  1613. 'S002V01M10GMPR',
  1614. 'S002V01F01COTP',
  1615. 'S002V01P02ROTP',
  1616. 'Registro',
  1617. "El usuario $name (" . $usr->USUA_IDUS . ") registró un borrador para la orden de trabajo #$idOrder.",
  1618. $idUser,
  1619. $nowStr,
  1620. 'S002V01S01ORTR'
  1621. );
  1622. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1623. return $this->responseController->makeresponse(false, "EXITO");
  1624. }
  1625. public function getActiveOrders($idUser, $line)
  1626. {
  1627. DB::enableQueryLog();
  1628. $idUser = $this->encryptionController->decrypt($idUser);
  1629. if (!$idUser) {
  1630. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  1631. }
  1632. $usr = DB::table('S002V01TUSUA')->where([
  1633. ['USUA_NULI', '=', $line],
  1634. ['USUA_IDUS', '=', $idUser]
  1635. ])->first();
  1636. if (is_null($usr)) {
  1637. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  1638. }
  1639. $activeWorkOrders = DB::table('S002V01TOTPR')->select([
  1640. 'OTPR_IDOT AS IDORDEN',
  1641. 'OTPR_EQIN AS EQUIPAMIENTO',
  1642. 'OTPR_FIAP AS FECHAINICIO',
  1643. 'OTPR_FTAP AS FECHATERMINO',
  1644. 'OTPR_TIIN AS TIEMPOESTIMADO',
  1645. 'OTPR_DTIN AS TIEMPOTOTAL',
  1646. 'OTPR_CLAS AS CLASIFICACION',
  1647. 'OTPR_ACAS AS ACTIVADOR',
  1648. 'ACTI_PRIO AS PRIORIDAD',
  1649. 'ACTI_TIAC AS TIPOACTIVADOR'
  1650. ])->join('S002V01TACTI', 'ACTI_IDAC', '=', 'OTPR_ACAS')->where([
  1651. ['OTPR_NULI', '=', $line],
  1652. ['OTPR_ESTA', '=', 'A']
  1653. ])->get()->all();
  1654. $now = $this->functionsController->now();
  1655. $nowStr = $now->toDateTimeString();
  1656. $actions = DB::getQueryLog();
  1657. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1658. $idac = $this->functionsController->registerActivity(
  1659. $line,
  1660. 'S002V01M10GMPR',
  1661. 'S002V01F01ACMA',
  1662. 'S002V01P01OTAC',
  1663. 'Consulta',
  1664. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las órdenes de mantenimiento preventivo activas.",
  1665. $idUser,
  1666. $nowStr,
  1667. 'S002V01S02AOTR'
  1668. );
  1669. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1670. return $this->responseController->makeResponse(false, 'EXITO.', $activeWorkOrders);
  1671. }
  1672. public function getPreventiveCalendar($fecIni, $fecFin, $idUser, $line)
  1673. {
  1674. DB::enableQueryLog();
  1675. $idUser = $this->encryptionController->decrypt($idUser);
  1676. if (!$idUser) {
  1677. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  1678. }
  1679. $usr = DB::table('S002V01TUSUA')->where([
  1680. ['USUA_NULI', '=', $line],
  1681. ['USUA_IDUS', '=', $idUser],
  1682. ])->first();
  1683. if (is_null($usr)) {
  1684. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  1685. }
  1686. if (!$this->functionsController->validDate($fecIni)) {
  1687. return $this->responseController->makeResponse(true, 'La fecha de inicio es inválida.', [], 400);
  1688. }
  1689. if (!$this->functionsController->validDate($fecFin)) {
  1690. return $this->responseController->makeResponse(true, 'La fecha final es inválida.', [], 400);
  1691. }
  1692. $fecIniObj = new Carbon($fecIni . ' 00:00:00');
  1693. $fecFinObj = new Carbon($fecFin . ' 23:59:59');
  1694. $workOrders = DB::table('S002V01TOTPR')->join('S002V01TACTI', 'ACTI_IDAC', '=', 'OTPR_ACAS')->where([
  1695. ['OTPR_NULI', '=', $line],
  1696. ['OTPR_ESTA', '=', 'A'],
  1697. ])->get()->all();
  1698. $programmedWorkOrders = [];
  1699. foreach ($workOrders as $order) {
  1700. $orderStart = new Carbon($order->OTPR_FIAP);
  1701. if ($order->ACTI_TIAC == 'Calendario' || $order->ACTI_TIAC == 'Sintoma') {
  1702. $repeatConfig = json_decode($order->ACTI_COAC, true);
  1703. switch ($repeatConfig['repeat']) {
  1704. case 'AN':
  1705. while ($orderStart->lte($fecFinObj)) {
  1706. if ($orderStart->gte($fecIniObj)) {
  1707. $reg = DB::table('S002V01TBEOT')->where([
  1708. ['BEOT_NULI', '=', $line],
  1709. ['BEOT_IDOT', '=', $order->OTPR_IDOT],
  1710. ['BEOT_FEPR', '=', $orderStart->toDateString()]
  1711. ])->first();
  1712. $programmedWorkOrders[] = [
  1713. "IDORDEN" => $order->OTPR_IDOT,
  1714. "EVENTCOLOR" => $repeatConfig['color'],
  1715. "DATE" => $orderStart->toDateString(),
  1716. "TYPE" => $order->ACTI_TIAC,
  1717. "REGEST" => is_null($reg) ? 'Programada' : $reg->BEOT_TIAC,
  1718. ];
  1719. }
  1720. $orderStart->addYear();
  1721. }
  1722. break;
  1723. case 'ME':
  1724. while ($orderStart->lte($fecFinObj)) {
  1725. if ($orderStart->gte($fecIniObj)) {
  1726. $reg = DB::table('S002V01TBEOT')->where([
  1727. ['BEOT_NULI', '=', $line],
  1728. ['BEOT_IDOT', '=', $order->OTPR_IDOT],
  1729. ['BEOT_FEPR', '=', $orderStart->toDateString()]
  1730. ])->first();
  1731. $programmedWorkOrders[] = [
  1732. "IDORDEN" => $order->OTPR_IDOT,
  1733. "EVENTCOLOR" => $repeatConfig['color'],
  1734. "DATE" => $orderStart->toDateString(),
  1735. "TYPE" => $order->ACTI_TIAC,
  1736. "REGEST" => is_null($reg) ? 'Programada' : $reg->BEOT_TIAC,
  1737. ];
  1738. }
  1739. $orderStart->addMonth();
  1740. }
  1741. break;
  1742. case 'SE':
  1743. while ($orderStart->lte($fecFinObj)) {
  1744. if ($orderStart->gte($fecIniObj)) {
  1745. $reg = DB::table('S002V01TBEOT')->where([
  1746. ['BEOT_NULI', '=', $line],
  1747. ['BEOT_IDOT', '=', $order->OTPR_IDOT],
  1748. ['BEOT_FEPR', '=', $orderStart->toDateString()]
  1749. ])->first();
  1750. $programmedWorkOrders[] = [
  1751. "IDORDEN" => $order->OTPR_IDOT,
  1752. "EVENTCOLOR" => $repeatConfig['color'],
  1753. "DATE" => $orderStart->toDateString(),
  1754. "TYPE" => $order->ACTI_TIAC,
  1755. "REGEST" => is_null($reg) ? 'Programada' : $reg->BEOT_TIAC,
  1756. ];
  1757. }
  1758. $orderStart->addWeek();
  1759. }
  1760. break;
  1761. case 'TD':
  1762. while ($orderStart->lte($fecFinObj)) {
  1763. if ($orderStart->gte($fecIniObj)) {
  1764. $reg = DB::table('S002V01TBEOT')->where([
  1765. ['BEOT_NULI', '=', $line],
  1766. ['BEOT_IDOT', '=', $order->OTPR_IDOT],
  1767. ['BEOT_FEPR', '=', $orderStart->toDateString()]
  1768. ])->first();
  1769. $programmedWorkOrders[] = [
  1770. "IDORDEN" => $order->OTPR_IDOT,
  1771. "EVENTCOLOR" => $repeatConfig['color'],
  1772. "DATE" => $orderStart->toDateString(),
  1773. "TYPE" => $order->ACTI_TIAC,
  1774. "REGEST" => is_null($reg) ? 'Programada' : $reg->BEOT_TIAC,
  1775. ];
  1776. }
  1777. $orderStart->addDay();
  1778. }
  1779. break;
  1780. case 'NR':
  1781. if ($orderStart->gte($fecIniObj) && $orderStart->lte($fecFinObj)) {
  1782. $reg = DB::table('S002V01TBEOT')->where([
  1783. ['BEOT_NULI', '=', $line],
  1784. ['BEOT_IDOT', '=', $order->OTPR_IDOT],
  1785. ['BEOT_FEPR', '=', $orderStart->toDateString()]
  1786. ])->first();
  1787. $programmedWorkOrders[] = [
  1788. "IDORDEN" => $order->OTPR_IDOT,
  1789. "EVENTCOLOR" => $repeatConfig['color'],
  1790. "DATE" => $orderStart->toDateString(),
  1791. "TYPE" => $order->ACTI_TIAC,
  1792. "REGEST" => is_null($reg) ? 'Programada' : $reg->BEOT_TIAC,
  1793. ];
  1794. }
  1795. break;
  1796. case 'PE':
  1797. $customRepeat = json_decode($repeatConfig['customRepeat'], true);
  1798. $repeatEvery = explode('|', $customRepeat['repeatEvery']);
  1799. switch ($repeatEvery[1]) {
  1800. case '0':
  1801. //Repeticiones por día
  1802. $days = intval($repeatEvery[0]);
  1803. while ($orderStart->lte($fecFinObj)) {
  1804. if ($orderStart->gte($fecIniObj)) {
  1805. $reg = DB::table('S002V01TBEOT')->where([
  1806. ['BEOT_NULI', '=', $line],
  1807. ['BEOT_IDOT', '=', $order->OTPR_IDOT],
  1808. ['BEOT_FEPR', '=', $orderStart->toDateString()]
  1809. ])->first();
  1810. $programmedWorkOrders[] = [
  1811. "IDORDEN" => $order->OTPR_IDOT,
  1812. "EVENTCOLOR" => $repeatConfig['color'],
  1813. "DATE" => $orderStart->toDateString(),
  1814. "TYPE" => $order->ACTI_TIAC,
  1815. "REGEST" => is_null($reg) ? 'Programada' : $reg->BEOT_TIAC,
  1816. ];
  1817. }
  1818. $orderStart->addDays($days);
  1819. }
  1820. break;
  1821. case '1':
  1822. //Repeticiones por semana
  1823. $weeks = intval($repeatEvery[0]);
  1824. $orderStartAux = new Carbon($orderStart->toDateTimeString());
  1825. $repeatOn = $customRepeat['repeatOn'];
  1826. while ($orderStart->lte($fecFinObj)) {
  1827. $dayInd = $orderStartAux->dayOfWeek;
  1828. for ($i = $dayInd; $i < 7; $i++) {
  1829. $dayName = strtolower($orderStartAux->dayName);
  1830. $dayKey = substr($dayName, 0, 3);
  1831. $shouldRepeat = $repeatOn[$dayKey];
  1832. if ($shouldRepeat && $orderStartAux->lte($fecFinObj) && $orderStartAux->gte($fecIniObj)) {
  1833. $reg = DB::table('S002V01TBEOT')->where([
  1834. ['BEOT_NULI', '=', $line],
  1835. ['BEOT_IDOT', '=', $order->OTPR_IDOT],
  1836. ['BEOT_FEPR', '=', $orderStartAux->toDateString()]
  1837. ])->first();
  1838. $programmedWorkOrders[] = [
  1839. "IDORDEN" => $order->OTPR_IDOT,
  1840. "EVENTCOLOR" => $repeatConfig['color'],
  1841. "DATE" => $orderStartAux->toDateString(),
  1842. "TYPE" => $order->ACTI_TIAC,
  1843. "REGEST" => is_null($reg) ? 'Programada' : $reg->BEOT_TIAC,
  1844. ];
  1845. }
  1846. $orderStartAux->addDay();
  1847. }
  1848. $orderStart->addWeeks($weeks);
  1849. }
  1850. break;
  1851. case '2':
  1852. //Repeticiones por mes
  1853. $months = intval($repeatEvery[0]);
  1854. while ($orderStart->lte($fecFinObj)) {
  1855. if ($orderStart->gte($fecIniObj)) {
  1856. $reg = DB::table('S002V01TBEOT')->where([
  1857. ['BEOT_NULI', '=', $line],
  1858. ['BEOT_IDOT', '=', $order->OTPR_IDOT],
  1859. ['BEOT_FEPR', '=', $orderStart->toDateString()]
  1860. ])->first();
  1861. $programmedWorkOrders[] = [
  1862. "IDORDEN" => $order->OTPR_IDOT,
  1863. "EVENTCOLOR" => $repeatConfig['color'],
  1864. "DATE" => $orderStart->toDateString(),
  1865. "TYPE" => $order->ACTI_TIAC,
  1866. "REGEST" => is_null($reg) ? 'Programada' : $reg->BEOT_TIAC,
  1867. ];
  1868. }
  1869. $orderStart->addMonths($months);
  1870. }
  1871. break;
  1872. case '3':
  1873. //Repeticiones por año
  1874. $years = intval($repeatEvery[0]);
  1875. while ($orderStart->lte($fecFinObj)) {
  1876. if ($orderStart->gte($fecIniObj)) {
  1877. $reg = DB::table('S002V01TBEOT')->where([
  1878. ['BEOT_NULI', '=', $line],
  1879. ['BEOT_IDOT', '=', $order->OTPR_IDOT],
  1880. ['BEOT_FEPR', '=', $orderStart->toDateString()]
  1881. ])->first();
  1882. $programmedWorkOrders[] = [
  1883. "IDORDEN" => $order->OTPR_IDOT,
  1884. "EVENTCOLOR" => $repeatConfig['color'],
  1885. "DATE" => $orderStart->toDateString(),
  1886. "TYPE" => $order->ACTI_TIAC,
  1887. "REGEST" => is_null($reg) ? 'Programada' : $reg->BEOT_TIAC,
  1888. ];
  1889. }
  1890. $orderStart->addYears($years);
  1891. }
  1892. break;
  1893. }
  1894. break;
  1895. }
  1896. }
  1897. }
  1898. $now = $this->functionsController->now();
  1899. $nowStr = $now->toDateTimeString();
  1900. $actions = DB::getQueryLog();
  1901. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1902. $idac = $this->functionsController->registerActivity(
  1903. $line,
  1904. 'S002V01M10GMPR',
  1905. 'S002V01F08VCPR',
  1906. '-',
  1907. 'Consulta',
  1908. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los eventos programados entre las fechas $fecIni y $fecFin.",
  1909. $idUser,
  1910. $nowStr,
  1911. 'S002V01S02AOTR'
  1912. );
  1913. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1914. return $this->responseController->makeResponse(false, 'EXITO.', $programmedWorkOrders);
  1915. }
  1916. // Visitas técnicas no programadas (Preventivas)
  1917. public function newUnprogrammedOrder(Request $request)
  1918. {
  1919. DB::enableQueryLog();
  1920. $validator = Validator::make($request->all(), [
  1921. 'id_user' => 'required|string',
  1922. 'linea' => 'required|integer',
  1923. 'equipment' => 'required|string',
  1924. 'resources' => 'required|json',
  1925. 'comments' => 'required|string|min:35',
  1926. 'staff' => 'required|json',
  1927. ]);
  1928. if ($validator->fails()) {
  1929. return $this->responseController->makeResponse(
  1930. true,
  1931. "Se encontraron uno o más errores.",
  1932. $this->responseController->makeErrors(
  1933. $validator->errors()->messages()
  1934. ),
  1935. 401
  1936. );
  1937. }
  1938. $form = $request->all();
  1939. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1940. if (!$idUser) {
  1941. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1942. }
  1943. $usr = DB::table('S002V01TUSUA')->where([
  1944. ['USUA_NULI', '=', $form['linea']],
  1945. ['USUA_IDUS', '=', $idUser]
  1946. ])->first();
  1947. if (is_null($usr)) {
  1948. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1949. }
  1950. $equipmentCode = $this->encryptionController->decrypt($form['equipment']);
  1951. if (!$equipmentCode) {
  1952. return $this->responseController->makeResponse(true, 'El código del equipamiento relacionado no fue encriptado correctamente.', [], 400);
  1953. }
  1954. $equipment = DB::table('S002V01TEQUI')->where([
  1955. ['EQUI_NULI', '=', $form['linea']],
  1956. ['EQUI_COEQ', '=', $equipmentCode]
  1957. ])->first();
  1958. if (is_null($equipment)) {
  1959. return $this->responseController->makeResponse(true, 'El equipamiento seleccionado no existe.', [], 404);
  1960. }
  1961. $staffArr = json_decode($form['staff'], true);
  1962. if (empty($staffArr)) {
  1963. return $this->responseController->makeResponse(true, 'El JSON de personal está vacío.', [], 400);
  1964. }
  1965. $staff = [];
  1966. foreach ($staffArr as $val) {
  1967. if (!array_key_exists('TYPE', $val) && !array_key_exists('ID', $val)) {
  1968. return $this->responseController->makeResponse(true, "El arreglo del personal no tiene un formato válido.", [], 400);
  1969. }
  1970. $typeStr = '';
  1971. if ($val['TYPE'] == 'EQ') {
  1972. $typeStr = 'equipo de trabajo';
  1973. } else if ($val['TYPE'] == 'SU') {
  1974. $typeStr = 'subcontratista';
  1975. } else if ($val['TYPE'] == 'EM') {
  1976. $typeStr = 'empleado';
  1977. }
  1978. $key = $this->encryptionController->decrypt($val['ID']);
  1979. if (!$key) {
  1980. return $this->responseController->makeResponse(true, "El ID del $typeStr no fue encriptado correctamente.", [], 400);
  1981. }
  1982. $res = null;
  1983. if ($val['TYPE'] == 'EQ') {
  1984. $res = DB::table('S002V01TEQMA')->where([
  1985. ['EQMA_NULI', '=', $form['linea']],
  1986. ['EQMA_IDEQ', '=', $key],
  1987. ])->first();
  1988. } else if ($val['TYPE'] == 'SU') {
  1989. $res = DB::table('S002V01TPESU')->where([
  1990. ['PESU_NULI', '=', $form['linea']],
  1991. ['PESU_IDPS', '=', $key],
  1992. ])->first();
  1993. } else if ($val['TYPE'] == 'EM') {
  1994. $res = DB::table('S002V01TPERS')->where([
  1995. ['PERS_NULI', '=', $form['linea']],
  1996. ['PERS_IDPE', '=', $key],
  1997. ])->first();
  1998. }
  1999. if (is_null($res)) {
  2000. return $this->responseController->makeResponse(true, "El $typeStr no está registrado.", [], 400);
  2001. }
  2002. $staff[] = [
  2003. 'ID' => $key,
  2004. 'TYPE' => $val['TYPE']
  2005. ];
  2006. }
  2007. $resources = json_decode($form['resources'], true);
  2008. if (empty($resources)) {
  2009. return $this->responseController->makeResponse(true, 'El JSON de recursos tiene un formato inválido.', [], 400);
  2010. }
  2011. $commentsArr = [
  2012. 'CI' => $form['comments']
  2013. ];
  2014. $now = $this->functionsController->now();
  2015. $nowStr = $now->toDateTimeString();
  2016. $statusHistoryStr = json_encode([
  2017. [
  2018. 'FECHA' => $nowStr,
  2019. 'ESTADO' => 'P',
  2020. 'USUARIO' => $idUser,
  2021. ]
  2022. ]);
  2023. $commentsStr = json_encode($commentsArr);
  2024. $staffStr = json_encode($staff);
  2025. $idVisit = DB::table('S002V01TRVTN')->insertGetId([
  2026. 'RVTN_NULI' => $form['linea'],
  2027. 'RVTN_EQRE' => $equipmentCode,
  2028. 'RVTN_DEIN' => $form['description'],
  2029. 'RVTN_TIAC' => 'M',
  2030. 'RVTN_PEIN' => $staffStr,
  2031. 'RVTN_MAUT' => $form['resources'],
  2032. 'RVTN_ESTA' => 'P',
  2033. 'RVTN_HIES' => $statusHistoryStr,
  2034. 'RVTN_COME' => $commentsStr,
  2035. 'RVTN_USRE' => $idUser,
  2036. 'RVTN_FERE' => $nowStr,
  2037. ]);
  2038. $actions = DB::getQueryLog();
  2039. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2040. $idac = $this->functionsController->registerActivity(
  2041. $form['linea'],
  2042. 'S002V01M10GMPR',
  2043. 'S002V01F11RVTP',
  2044. 'S002V01P01REVI',
  2045. 'Registro',
  2046. "El usuario $name (" . $usr->USUA_IDUS . ") registró la visita no programada #$idVisit.",
  2047. $idUser,
  2048. $nowStr,
  2049. 'S002V01S02AOTR'
  2050. );
  2051. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2052. return $this->responseController->makeResponse(false, 'EXITO.');
  2053. }
  2054. // Visitas técnicas no programadas (Preventivas)
  2055. public function getUnprogrammedVisits($idUser, $line)
  2056. {
  2057. DB::enableQueryLog();
  2058. $idUser = $this->encryptionController->decrypt($idUser);
  2059. if (!$idUser) {
  2060. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  2061. }
  2062. $usr = DB::table('S002V01TUSUA')->where([
  2063. ['USUA_NULI', '=', $line],
  2064. ['USUA_IDUS', '=', $idUser],
  2065. ])->first();
  2066. if (is_null($usr)) {
  2067. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  2068. }
  2069. $visits = DB::table('S002V01TRVTN')->select([
  2070. 'RVTN_IDVI AS IDVISITA',
  2071. 'RVTN_EQRE AS EQUIPAMIENTO',
  2072. 'EQUI_TIPO AS TIPO_EQUIPAMIENTO',
  2073. 'EQUI_MODE AS MODELO_EQUIPAMIENTO',
  2074. 'EQUI_IDEQ AS ID_EQUIPAMIENTO',
  2075. 'RVTN_ESTA AS ESTADO',
  2076. 'RVTN_USRE AS USRREG',
  2077. 'RVTN_FERE AS FECREG',
  2078. 'RVTN_FEMO AS FECMOD',
  2079. 'RVTN_PRIO AS PRIORIDAD',
  2080. 'RVTN_DEIN AS DESCRIPCION',
  2081. 'RVTN_TIAC AS TIPO_ACTIVACION',
  2082. 'usrReg.USUA_NOMB AS USRREG_NOMB',
  2083. 'usrReg.USUA_APPA AS USRREG_APPA',
  2084. 'usrReg.USUA_APMA AS USRREG_APMA'
  2085. ])->where('RVTN_NULI', '=', $line)
  2086. ->join('S002V01TEQUI', 'EQUI_COEQ', '=', 'RVTN_EQRE')
  2087. ->leftJoin('S002V01TUSUA as usrReg', function ($join) {
  2088. $join->on('usrReg.USUA_IDUS', '=', 'S002V01TRVTN.RVTN_USRE')
  2089. ->on('usrReg.USUA_NULI', '=', 'S002V01TRVTN.RVTN_NULI');
  2090. })
  2091. ->orderBy('RVTN_IDVI', 'desc')->get()->all();
  2092. foreach ($visits as $visit) {
  2093. $visit->IDVISITA = $this->encryptionController->encrypt($visit->IDVISITA);
  2094. $visit->EQUIPAMIENTO = $this->encryptionController->encrypt($visit->EQUIPAMIENTO);
  2095. $visit->ID_EQUIPAMIENTO = $this->encryptionController->encrypt($visit->ID_EQUIPAMIENTO);
  2096. $nameReg = $this->functionsController->joinName($visit->USRREG_NOMB, $visit->USRREG_APPA, $visit->USRREG_APMA);
  2097. if (!empty($nameReg)) {
  2098. $visit->USRREG = trim((string) $nameReg) . " (" . $visit->USRREG . ")";
  2099. }
  2100. unset($visit->USRREG_NOMB, $visit->USRREG_APPA, $visit->USRREG_APMA);
  2101. }
  2102. $now = $this->functionsController->now();
  2103. $nowStr = $now->toDateTimeString();
  2104. $actions = DB::getQueryLog();
  2105. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2106. $idac = $this->functionsController->registerActivity(
  2107. $line,
  2108. 'S002V01M10GMPR',
  2109. 'S002V01F11RVTP',
  2110. 'S002V01P02COVI',
  2111. 'Consulta',
  2112. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la lista de visitas no programadas.",
  2113. $idUser,
  2114. $nowStr,
  2115. 'S002V01S02AOTR'
  2116. );
  2117. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2118. return $this->responseController->makeResponse(false, 'EXITO.', $visits);
  2119. }
  2120. // Visitas técnicas no programadas (Preventivas)
  2121. public function getUnprogrammedVisit($idVisit, $idUser, $line)
  2122. {
  2123. DB::enableQueryLog();
  2124. $idUser = $this->encryptionController->decrypt($idUser);
  2125. if (!$idUser) {
  2126. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  2127. }
  2128. $usr = DB::table('S002V01TUSUA')->where([
  2129. ['USUA_IDUS', '=', $idUser],
  2130. ['USUA_NULI', '=', $line]
  2131. ])->first();
  2132. if (is_null($usr)) {
  2133. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  2134. }
  2135. $idVisit = $this->encryptionController->decrypt($idVisit);
  2136. if (!$idVisit) {
  2137. return $this->responseController->makeResponse(true, 'El ID de la visita solicitada no está encriptado correctamente.', [], 400);
  2138. }
  2139. $visit = DB::table('S002V01TRVTN')->select([
  2140. 'RVTN_IDVI AS IDVISITA',
  2141. 'RVTN_EQRE AS EQUIPAMIENTO',
  2142. 'EQUI_TIPO AS TIPO_EQUIPAMIENTO',
  2143. 'EQUI_MODE AS MODELO_EQUIPAMIENTO',
  2144. 'EQUI_IDEQ AS ID_EQUIPAMIENTO',
  2145. 'RVTN_PEIN AS PERSONAL',
  2146. 'RVTN_MAUT AS MATERIAL',
  2147. 'RVTN_COME AS COMENTARIOS',
  2148. 'RVTN_ESTA AS ESTADO',
  2149. 'RVTN_USRE AS USRREG',
  2150. 'RVTN_FERE AS FECREG',
  2151. 'RVTN_UARE AS USAURE',
  2152. 'RVTN_FARE AS FEAURE',
  2153. 'RVTN_USCA AS USRCAN',
  2154. 'RVTN_FECA AS FECCAN',
  2155. 'RVTN_USFI AS USUFIN',
  2156. 'RVTN_FEFI AS FECFIN',
  2157. 'RVTN_USMO AS USRMOD',
  2158. 'RVTN_FEMO AS FECMOD',
  2159. ])->where([
  2160. ['RVTN_IDVI', '=', $idVisit],
  2161. ['RVTN_NULI', '=', $line]
  2162. ])->join('S002V01TEQUI', 'EQUI_COEQ', '=', 'RVTN_EQRE')->first();
  2163. if (is_null($visit)) {
  2164. return $this->responseController->makeResponse(true, 'La visita consultada no existe.', [], 404);
  2165. }
  2166. $visit->IDVISITA = $this->encryptionController->encrypt($visit->IDVISITA);
  2167. $visit->EQUIPAMIENTO = $this->encryptionController->encrypt($visit->EQUIPAMIENTO);
  2168. $visit->ID_EQUIPAMIENTO = $this->encryptionController->encrypt($visit->ID_EQUIPAMIENTO);
  2169. $staffFn = [];
  2170. $staffArr = json_decode($visit->PERSONAL, true);
  2171. foreach ($staffArr as $item) {
  2172. $name = '';
  2173. if ($item['TYPE'] == 'EQ') {
  2174. $workTeam = DB::table('S002V01TEQMA')->where([
  2175. ['EQMA_NULI', '=', $line],
  2176. ['EQMA_IDEQ', '=', $item['ID']]
  2177. ])->first();
  2178. $name = $workTeam->EQMA_NOMB;
  2179. } else if ($item['TYPE'] == 'SU') {
  2180. $subcontratist = DB::table('S002V01TPESU')->where([
  2181. ['PESU_NULI', '=', $line],
  2182. ['PESU_IDPS', '=', $item['ID']]
  2183. ])->first();
  2184. $name = $subcontratist->PESU_RASO;
  2185. } else if ($item['TYPE'] == 'EM') {
  2186. $employee = DB::table('S002V01TPERS')
  2187. ->join('S002V01TUSUA', 'USUA_IDUS', '=', 'PERS_IDUS')->where([
  2188. ['PERS_NULI', '=', $line],
  2189. ['PERS_IDPE', '=', $item['ID']]
  2190. ])->first();
  2191. $name = $this->functionsController->joinName($employee->USUA_NOMB, $employee->USUA_APPA, $employee->USUA_APMA);
  2192. }
  2193. $staffFn[] = [
  2194. 'ID' => $this->encryptionController->encrypt($item['ID']),
  2195. 'TYPE' => $item['TYPE'],
  2196. 'NAME' => $name,
  2197. ];
  2198. }
  2199. $visit->PERSONAL = json_encode($staffFn);
  2200. $usrReg = DB::table('S002V01TUSUA')->where([
  2201. ['USUA_NULI', '=', $line],
  2202. ['USUA_IDUS', '=', $visit->USRREG],
  2203. ])->first();
  2204. $nameReg = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA) . " (" . $visit->USRREG . ")";
  2205. $visit->USRREG = $nameReg;
  2206. if (!is_null($visit->USAURE)) {
  2207. $usaure = DB::table('S002V01TUSUA')->where([
  2208. ['USUA_NULI', '=', $line],
  2209. ['USUA_IDUS', '=', $visit->USAURE],
  2210. ])->first();
  2211. $nameUre = $this->functionsController->joinName($usaure->USUA_NOMB, $usaure->USUA_APPA, $usaure->USUA_APMA) . " (" . $visit->USAURE . ")";
  2212. $visit->USAURE = $nameUre;
  2213. }
  2214. if (!is_null($visit->USRCAN)) {
  2215. $usrCan = DB::table('S002V01TUSUA')->where([
  2216. ['USUA_NULI', '=', $line],
  2217. ['USUA_IDUS', '=', $visit->USRCAN],
  2218. ])->first();
  2219. $nameCan = $this->functionsController->joinName($usrCan->USUA_NOMB, $usrCan->USUA_APPA, $usrCan->USUA_APMA) . " (" . $visit->USRCAN . ")";
  2220. $visit->USRCAN = $nameCan;
  2221. }
  2222. if (!is_null($visit->USUFIN)) {
  2223. $usrFin = DB::table('S002V01TUSUA')->where([
  2224. ['USUA_NULI', '=', $line],
  2225. ['USUA_IDUS', '=', $visit->USUFIN],
  2226. ])->first();
  2227. $nameFin = $this->functionsController->joinName($usrFin->USUA_NOMB, $usrFin->USUA_APPA, $usrFin->USUA_APMA) . " (" . $visit->USUFIN . ")";
  2228. $visit->USUFIN = $nameFin;
  2229. }
  2230. if (!is_null($visit->USRMOD)) {
  2231. $usrMod = DB::table('S002V01TUSUA')->where([
  2232. ['USUA_NULI', '=', $line],
  2233. ['USUA_IDUS', '=', $visit->USRMOD],
  2234. ])->first();
  2235. $nameMod = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA) . " (" . $visit->USRMOD . ")";
  2236. $visit->USRMOD = $nameMod;
  2237. }
  2238. $now = $this->functionsController->now();
  2239. $nowStr = $now->toDateTimeString();
  2240. $actions = DB::getQueryLog();
  2241. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2242. $idac = $this->functionsController->registerActivity(
  2243. $line,
  2244. 'S002V01M10GMPR',
  2245. 'S002V01F11RVTP',
  2246. 'S002V01P02COVI',
  2247. 'Consulta',
  2248. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la visita no programada #$idVisit.",
  2249. $idUser,
  2250. $nowStr,
  2251. 'S002V01S02AOTR'
  2252. );
  2253. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2254. return $this->responseController->makeResponse(false, 'EXITO.', $visit);
  2255. }
  2256. // Visitas técnicas no programadas (Preventivas)
  2257. public function updateVisitStatus(Request $request)
  2258. {
  2259. DB::enableQueryLog();
  2260. // RVTN_ESTA enum: 'VA','EP','CP','CE','P','C','R','A','F'
  2261. // VA, EP, CP, CE: Solo para RVTN_TIAC='A' (Automática)
  2262. // P, C, R, A, F: Solo para RVTN_TIAC='M' (Manual)
  2263. // EP y C son compartidos entre ambos tipos
  2264. $validator = Validator::make($request->all(), [
  2265. 'id_user' => 'required|string',
  2266. 'linea' => 'required|integer',
  2267. 'id_visit' => 'required|string',
  2268. 'comments' => 'nullable|string|min:15',
  2269. 'status' => 'required|string|in:VA,EP,CP,CE,P,C,R,A,F'
  2270. ]);
  2271. if ($validator->fails()) {
  2272. return $this->responseController->makeResponse(
  2273. true,
  2274. "Se encontraron uno o más errores.",
  2275. $this->responseController->makeErrors(
  2276. $validator->errors()->messages()
  2277. ),
  2278. 401
  2279. );
  2280. }
  2281. $form = $request->all();
  2282. $idUser = $this->encryptionController->decrypt($form['id_user']);
  2283. if (!$idUser) {
  2284. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  2285. }
  2286. $usr = DB::table('S002V01TUSUA')->where([
  2287. ['USUA_NULI', '=', $form['linea']],
  2288. ['USUA_IDUS', '=', $idUser],
  2289. ])->first();
  2290. if (is_null($usr)) {
  2291. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  2292. }
  2293. $idVisit = $this->encryptionController->decrypt($form['id_visit']);
  2294. if (!$idVisit) {
  2295. return $this->responseController->makeResponse(true, 'El ID de la visita solicitada no está encriptado correctamente.', [], 400);
  2296. }
  2297. $visit = DB::table('S002V01TRVTN')->where([
  2298. ['RVTN_NULI', '=', $form['linea']],
  2299. ['RVTN_IDVI', '=', $idVisit],
  2300. ])->first();
  2301. if (is_null($visit)) {
  2302. return $this->responseController->makeResponse(true, 'La visita solicitada no existe.', [], 404);
  2303. }
  2304. $currentStatus = $visit->RVTN_ESTA;
  2305. $newStatus = $form['status'];
  2306. $visitType = $visit->RVTN_TIAC; // 'A' = Automática, 'M' = Manual
  2307. // Validar que el estado solicitado corresponda al tipo de activación
  2308. $automaticStates = ['VA', 'EP', 'CP', 'CE'];
  2309. $manualStates = ['P', 'C', 'R', 'A', 'F'];
  2310. $isRequestedStateAutomatic = in_array($newStatus, $automaticStates);
  2311. $isRequestedStateManual = in_array($newStatus, $manualStates);
  2312. // EP y C son compartidos, ajustar validación
  2313. if ($newStatus === 'EP' || $newStatus === 'C') {
  2314. // Permitir EP y C para ambos tipos
  2315. } elseif (($visitType === 'A' && !$isRequestedStateAutomatic) ||
  2316. ($visitType === 'M' && !$isRequestedStateManual)
  2317. ) {
  2318. return $this->responseController->makeResponse(true, "El estado {$newStatus} no corresponde al tipo de activación de la visita.", [], 400);
  2319. }
  2320. // Matriz de transiciones permitidas
  2321. $allowedTransitions = [
  2322. 'A' => [ // Visitas Automáticas
  2323. 'VA' => ['EP', 'C'], // Validado -> En Proceso o Cancelar
  2324. 'EP' => ['CP', 'C'], // En Proceso -> Cerrado Pendiente o Cancelar
  2325. 'CP' => ['CE', 'C'], // Cerrado Pendiente -> Cerrado o Cancelar (aunque C desde CP está bloqueado)
  2326. 'CE' => [] // Cerrado - Estado final, no se puede cambiar
  2327. ],
  2328. 'M' => [ // Visitas Manuales
  2329. 'P' => ['A', 'R', 'C'], // Pendiente -> Aprobar, Rechazar o Cancelar
  2330. 'A' => ['EP', 'C'], // Aprobado -> En Proceso o Cancelar
  2331. 'EP' => ['F', 'C'], // En Proceso -> Finalizar o Cancelar
  2332. 'F' => [], // Finalizado - Estado final, no se puede cambiar
  2333. 'R' => [] // Rechazado - Estado final, no se puede cambiar
  2334. ]
  2335. ];
  2336. // Validar transición permitida
  2337. $currentStatusTransitions = $allowedTransitions[$visitType][$currentStatus] ?? [];
  2338. // Si el estado actual no está en la matriz, no permitir cambios
  2339. if (!array_key_exists($currentStatus, $allowedTransitions[$visitType])) {
  2340. return $this->responseController->makeResponse(true, "El estado actual {$currentStatus} no permite cambios.", [], 400);
  2341. }
  2342. // Validar si la transición está permitida
  2343. if (!in_array($newStatus, $currentStatusTransitions)) {
  2344. $statusNames = [
  2345. 'VA' => 'Validado',
  2346. 'EP' => 'En Proceso',
  2347. 'CP' => 'Cerrado Pendiente',
  2348. 'CE' => 'Cerrado',
  2349. 'P' => 'Pendiente',
  2350. 'A' => 'Aprobado',
  2351. 'R' => 'Rechazado',
  2352. 'F' => 'Finalizado',
  2353. 'C' => 'Cancelado'
  2354. ];
  2355. $currentStatusName = $statusNames[$currentStatus] ?? $currentStatus;
  2356. $newStatusName = $statusNames[$newStatus] ?? $newStatus;
  2357. return $this->responseController->makeResponse(true, "No se puede cambiar de {$currentStatusName} a {$newStatusName}. Transición no permitida.", [], 400);
  2358. }
  2359. // Validar cancelación desde estados finales
  2360. if ($newStatus === 'C') {
  2361. $finalStates = $visitType === 'A' ? ['CE'] : ['F', 'R'];
  2362. if (in_array($currentStatus, $finalStates)) {
  2363. return $this->responseController->makeResponse(true, "No se puede cancelar una visita que ya está en estado final.", [], 400);
  2364. }
  2365. // También bloquear desde CP según aclaración
  2366. if ($currentStatus === 'CP') {
  2367. return $this->responseController->makeResponse(true, "No se puede cancelar una visita en estado Cerrado Pendiente.", [], 400);
  2368. }
  2369. }
  2370. // Obtener y actualizar historial de estados
  2371. $statusHistoryArr = json_decode($visit->RVTN_HIES, true);
  2372. if (!is_array($statusHistoryArr)) {
  2373. $statusHistoryArr = [];
  2374. }
  2375. $now = $this->functionsController->now();
  2376. $nowStr = $now->toDateTimeString();
  2377. // Agregar nuevo estado al historial
  2378. $statusHistoryArr[] = [
  2379. 'FECHA' => $nowStr,
  2380. 'ESTADO' => $newStatus,
  2381. 'USUARIO' => $idUser
  2382. ];
  2383. $statusHistoryStr = json_encode($statusHistoryArr);
  2384. // Preparar actualización
  2385. $updateArr = [
  2386. 'RVTN_ESTA' => $newStatus,
  2387. 'RVTN_HIES' => $statusHistoryStr,
  2388. 'RVTN_USMO' => $idUser,
  2389. 'RVTN_FEMO' => $nowStr,
  2390. ];
  2391. // Estados automáticos: VA, EP, CP, CE
  2392. if (in_array($newStatus, ['VA', 'EP', 'CP', 'CE'])) {
  2393. $updateArr['RVTN_UARE'] = $idUser;
  2394. $updateArr['RVTN_FARE'] = $nowStr;
  2395. }
  2396. // Estados manuales
  2397. if ($newStatus == 'P') {
  2398. // Pendiente - Ya registrado en creación
  2399. } elseif ($newStatus == 'C') {
  2400. $updateArr['RVTN_USCA'] = $idUser;
  2401. $updateArr['RVTN_FECA'] = $nowStr;
  2402. } elseif ($newStatus == 'R') {
  2403. $updateArr['RVTN_UARE'] = $idUser;
  2404. $updateArr['RVTN_FARE'] = $nowStr;
  2405. // Registrar comentario de rechazo
  2406. if (!empty($form['comments'])) {
  2407. $commentsArr = json_decode($visit->RVTN_COME, true);
  2408. if (!is_array($commentsArr)) {
  2409. $commentsArr = [];
  2410. }
  2411. $commentsArr['COMENTARIO_RECHAZO'] = [
  2412. 'ID' => $idUser,
  2413. 'COMENTARIO' => $form['comments'],
  2414. 'FECHA' => $nowStr
  2415. ];
  2416. $updateArr['RVTN_COME'] = json_encode($commentsArr);
  2417. }
  2418. } elseif ($newStatus == 'A') {
  2419. $updateArr['RVTN_UARE'] = $idUser;
  2420. $updateArr['RVTN_FARE'] = $nowStr;
  2421. } elseif ($newStatus == 'F') {
  2422. $updateArr['RVTN_USFI'] = $idUser;
  2423. $updateArr['RVTN_FEFI'] = $nowStr;
  2424. }
  2425. DB::table('S002V01TRVTN')->where([
  2426. ['RVTN_NULI', '=', $form['linea']],
  2427. ['RVTN_IDVI', '=', $idVisit],
  2428. ])->update($updateArr);
  2429. // Obtener audiencia para notificaciones (operarios y regulador)
  2430. $audience = [];
  2431. // Obtener operarios que aceptaron la invitación (del último estado VA)
  2432. $statusHistoryForAudience = json_decode($visit->RVTN_HIES, true);
  2433. if (is_array($statusHistoryForAudience)) {
  2434. // Buscar el último estado VA
  2435. $lastVAStatus = null;
  2436. for ($i = count($statusHistoryForAudience) - 1; $i >= 0; $i--) {
  2437. if ($statusHistoryForAudience[$i]['ESTADO'] === 'VA') {
  2438. $lastVAStatus = $statusHistoryForAudience[$i];
  2439. break;
  2440. }
  2441. }
  2442. if ($lastVAStatus && isset($lastVAStatus['ATENCION']) && is_array($lastVAStatus['ATENCION'])) {
  2443. foreach ($lastVAStatus['ATENCION'] as $item) {
  2444. if (($item['RESPUESTA'] ?? '') === 'A') {
  2445. $audience[] = $item['ID'];
  2446. }
  2447. }
  2448. }
  2449. }
  2450. // Agregar regulador (quien registró la visita)
  2451. if (!empty($visit->RVTN_USRE) && !in_array($visit->RVTN_USRE, $audience)) {
  2452. $audience[] = $visit->RVTN_USRE;
  2453. }
  2454. // Preparar mensajes de notificación según el estado
  2455. $statusMessages = [
  2456. 'A' => 'Aprobación',
  2457. 'R' => 'Rechazo',
  2458. 'C' => 'Cancelación',
  2459. 'EP' => 'Inicio',
  2460. 'F' => 'Finalización',
  2461. 'VA' => 'Validación',
  2462. 'CP' => 'Cerrado Pendiente',
  2463. 'CE' => 'Cierre'
  2464. ];
  2465. $statusAction = $statusMessages[$newStatus] ?? 'Cambio de estado';
  2466. $notificationTitle = "Visita Técnica No Programada #$idVisit";
  2467. $notificationMessage = "La visita técnica no programada #$idVisit ha sido actulizada a estado:{$statusAction}.";
  2468. // Notificación general para todos los cambios de estado (A, R, C, EP, F, VA, CE, CP)
  2469. if (in_array($newStatus, ['A', 'R', 'C', 'EP', 'F', 'VA', 'CE', 'CP']) && !empty($audience)) {
  2470. $this->notificationsController->emitNotification(
  2471. 'S002V01M10GMPR',
  2472. $notificationTitle,
  2473. $notificationMessage,
  2474. [[
  2475. 'BOTON' => 'Ver detalles',
  2476. 'FUNCION' => 'openPreventiveWorkOrderDetails',
  2477. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit)])
  2478. ], [
  2479. 'BOTON' => 'Ir al módulo',
  2480. 'FUNCION' => 'openModule',
  2481. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMPR/AOTR/RVTP')])
  2482. ]],
  2483. $audience,
  2484. $idUser,
  2485. $form['linea'],
  2486. $this->getSocketClient(),
  2487. $idVisit,
  2488. 'Preventivo'
  2489. );
  2490. }
  2491. $actions = DB::getQueryLog();
  2492. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2493. $idac = $this->functionsController->registerActivity(
  2494. $form['linea'],
  2495. 'S002V01M10GMPR',
  2496. 'S002V01F11RVTP',
  2497. 'S002V01P01REVI',
  2498. 'Actualización',
  2499. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó la visita no programada #$idVisit.",
  2500. $idUser,
  2501. $nowStr,
  2502. 'S002V01S02AOTR'
  2503. );
  2504. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2505. return $this->responseController->makeResponse(false, 'EXITO.');
  2506. }
  2507. // Visitas técnicas no programadas (Preventivas)
  2508. // Registrar comentario de finalización de un operario en estado CP
  2509. public function registerOperatorClosingComment(Request $request)
  2510. {
  2511. DB::enableQueryLog();
  2512. $validator = Validator::make($request->all(), [
  2513. 'id_user' => 'required|string',
  2514. 'linea' => 'required|integer',
  2515. 'id_visit' => 'required|string',
  2516. 'comment' => 'required|string|min:15'
  2517. ]);
  2518. if ($validator->fails()) {
  2519. return $this->responseController->makeResponse(
  2520. true,
  2521. "Se encontraron uno o más errores.",
  2522. $this->responseController->makeErrors(
  2523. $validator->errors()->messages()
  2524. ),
  2525. 401
  2526. );
  2527. }
  2528. $form = $request->all();
  2529. $idUser = $this->encryptionController->decrypt($form['id_user']);
  2530. if (!$idUser) {
  2531. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  2532. }
  2533. $usr = DB::table('S002V01TUSUA')->where([
  2534. ['USUA_NULI', '=', $form['linea']],
  2535. ['USUA_IDUS', '=', $idUser],
  2536. ])->first();
  2537. if (is_null($usr)) {
  2538. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  2539. }
  2540. $idVisit = $this->encryptionController->decrypt($form['id_visit']);
  2541. if (!$idVisit) {
  2542. return $this->responseController->makeResponse(true, 'El ID de la visita solicitada no está encriptado correctamente.', [], 400);
  2543. }
  2544. $visit = DB::table('S002V01TRVTN')->where([
  2545. ['RVTN_NULI', '=', $form['linea']],
  2546. ['RVTN_IDVI', '=', $idVisit],
  2547. ])->first();
  2548. if (is_null($visit)) {
  2549. return $this->responseController->makeResponse(true, 'La visita solicitada no existe.', [], 404);
  2550. }
  2551. // Validar que la visita está en estado EP o CP
  2552. if (!in_array($visit->RVTN_ESTA, ['EP', 'CP'])) {
  2553. return $this->responseController->makeResponse(true, 'La visita no está en estado En Proceso o Cerrado Pendiente. Solo se pueden registrar comentarios de finalización en estos estados.', [], 400);
  2554. }
  2555. // Iniciar transacción
  2556. DB::beginTransaction();
  2557. try {
  2558. // Obtener historial de estados
  2559. $statusHistoryArr = json_decode($visit->RVTN_HIES, true);
  2560. if (!is_array($statusHistoryArr)) {
  2561. DB::rollBack();
  2562. return $this->responseController->makeResponse(true, 'No se encontró un historial de estados válido para la visita.', [], 500);
  2563. }
  2564. // Buscar el último estado VA para obtener la lista de operarios que aceptaron
  2565. $lastVAStatus = null;
  2566. $acceptedOperators = [];
  2567. for ($i = count($statusHistoryArr) - 1; $i >= 0; $i--) {
  2568. if ($statusHistoryArr[$i]['ESTADO'] === 'VA') {
  2569. $lastVAStatus = $statusHistoryArr[$i];
  2570. break;
  2571. }
  2572. }
  2573. if (!$lastVAStatus || !isset($lastVAStatus['ATENCION']) || !is_array($lastVAStatus['ATENCION'])) {
  2574. DB::rollBack();
  2575. return $this->responseController->makeResponse(true, 'No se encontró información de operarios asignados a la visita.', [], 404);
  2576. }
  2577. // Obtener lista de operarios que aceptaron
  2578. foreach ($lastVAStatus['ATENCION'] as $item) {
  2579. if (($item['RESPUESTA'] ?? '') === 'A') {
  2580. $acceptedOperators[] = $item['ID'];
  2581. }
  2582. }
  2583. // Validar que el usuario es uno de los operarios que aceptó
  2584. if (!in_array($idUser, $acceptedOperators)) {
  2585. DB::rollBack();
  2586. return $this->responseController->makeResponse(true, 'El usuario no está autorizado para registrar comentarios de finalización. Solo los operarios que aceptaron la invitación pueden registrar comentarios.', [], 401);
  2587. }
  2588. // Buscar el objeto CP en el historial o crearlo si no existe
  2589. $cpStatusIndex = null;
  2590. $cpStatus = null;
  2591. $isFirstComment = false;
  2592. for ($i = count($statusHistoryArr) - 1; $i >= 0; $i--) {
  2593. if ($statusHistoryArr[$i]['ESTADO'] === 'CP') {
  2594. $cpStatusIndex = $i;
  2595. $cpStatus = $statusHistoryArr[$i];
  2596. break;
  2597. }
  2598. }
  2599. // Si no existe objeto CP, crear uno nuevo y cambiar estado de EP a CP
  2600. if ($cpStatusIndex === null) {
  2601. $now = $this->functionsController->now();
  2602. $nowStr = $now->toDateTimeString();
  2603. $isFirstComment = true;
  2604. $cpStatus = [
  2605. 'FECHA' => $nowStr,
  2606. 'ESTADO' => 'CP',
  2607. 'USUARIO' => $idUser,
  2608. 'COMENTARIOS_OPERARIOS' => []
  2609. ];
  2610. $cpStatusIndex = count($statusHistoryArr);
  2611. $statusHistoryArr[] = $cpStatus;
  2612. } else {
  2613. // Asegurar que existe el array de comentarios
  2614. if (!isset($cpStatus['COMENTARIOS_OPERARIOS']) || !is_array($cpStatus['COMENTARIOS_OPERARIOS'])) {
  2615. $cpStatus['COMENTARIOS_OPERARIOS'] = [];
  2616. }
  2617. }
  2618. // Obtener comentarios existentes para validación
  2619. $existingCommentsArr = json_decode($visit->RVTN_COME, true);
  2620. if (!is_array($existingCommentsArr)) {
  2621. $existingCommentsArr = [];
  2622. }
  2623. // Validar que el operario no haya registrado comentario ya
  2624. if (isset($existingCommentsArr['COMENTARIOS_OPERADORES']) && is_array($existingCommentsArr['COMENTARIOS_OPERADORES'])) {
  2625. foreach ($existingCommentsArr['COMENTARIOS_OPERADORES'] as $existingComment) {
  2626. if (isset($existingComment['ID']) && $existingComment['ID'] === $idUser) {
  2627. DB::rollBack();
  2628. return $this->responseController->makeResponse(true, 'Ya ha registrado su comentario de finalización para esta visita.', [], 400);
  2629. }
  2630. }
  2631. }
  2632. // Registrar el comentario del operario
  2633. $now = $this->functionsController->now();
  2634. $nowStr = $now->toDateTimeString();
  2635. $operatorName = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2636. $cpStatus['COMENTARIOS_OPERARIOS'][] = [
  2637. 'ID' => $idUser,
  2638. 'COMENTARIO' => $form['comment'],
  2639. 'FECHA' => $nowStr
  2640. ];
  2641. // Actualizar el objeto en el historial
  2642. $statusHistoryArr[$cpStatusIndex] = $cpStatus;
  2643. $statusHistoryStr = json_encode($statusHistoryArr);
  2644. // Contar comentarios registrados vs total de operarios
  2645. $updatedCommentsArr = json_decode($visit->RVTN_COME, true);
  2646. $commentsRegistered = isset($updatedCommentsArr['COMENTARIOS_OPERADORES']) ? count($updatedCommentsArr['COMENTARIOS_OPERADORES']) : 0;
  2647. $totalOperators = count($acceptedOperators);
  2648. $isComplete = $commentsRegistered >= $totalOperators;
  2649. // Preparar actualización de la visita
  2650. $updateArr = [
  2651. 'RVTN_HIES' => $statusHistoryStr,
  2652. 'RVTN_USMO' => $idUser,
  2653. 'RVTN_FEMO' => $nowStr,
  2654. ];
  2655. // Si es el primer comentario, cambiar estado de EP a CP
  2656. if ($isFirstComment) {
  2657. $updateArr['RVTN_ESTA'] = 'CP';
  2658. $updateArr['RVTN_UARE'] = $idUser;
  2659. $updateArr['RVTN_FARE'] = $nowStr;
  2660. }
  2661. // Actualizar comentarios de operadores
  2662. $commentsArr = json_decode($visit->RVTN_COME, true);
  2663. if (!is_array($commentsArr)) {
  2664. $commentsArr = [];
  2665. }
  2666. // Inicializar array de comentarios de operadores si no existe
  2667. if (!isset($commentsArr['COMENTARIOS_OPERADORES']) || !is_array($commentsArr['COMENTARIOS_OPERADORES'])) {
  2668. $commentsArr['COMENTARIOS_OPERADORES'] = [];
  2669. }
  2670. // Agregar comentario del operario
  2671. $commentsArr['COMENTARIOS_OPERADORES'][] = [
  2672. 'ID' => $idUser,
  2673. 'COMENTARIO' => $form['comment'],
  2674. 'FECHA' => $nowStr
  2675. ];
  2676. $commentsStr = json_encode($commentsArr);
  2677. $updateArr['RVTN_COME'] = $commentsStr;
  2678. // Actualizar la visita
  2679. DB::table('S002V01TRVTN')->where([
  2680. ['RVTN_NULI', '=', $form['linea']],
  2681. ['RVTN_IDVI', '=', $idVisit],
  2682. ])->update($updateArr);
  2683. // Obtener regulador para notificaciones
  2684. $regulatorId = $visit->RVTN_USRE;
  2685. $regulatorAudience = !empty($regulatorId) ? [$regulatorId] : [];
  2686. // Obtener audiencia completa (operarios y regulador) para notificación de cambio de estado
  2687. $audience = [];
  2688. if (!empty($lastVAStatus['ATENCION']) && is_array($lastVAStatus['ATENCION'])) {
  2689. foreach ($lastVAStatus['ATENCION'] as $item) {
  2690. if (($item['RESPUESTA'] ?? '') === 'A') {
  2691. $audience[] = $item['ID'];
  2692. }
  2693. }
  2694. }
  2695. if (!empty($regulatorId) && !in_array($regulatorId, $audience)) {
  2696. $audience[] = $regulatorId;
  2697. }
  2698. // Si es el primer comentario (cambio EP -> CP), notificar a todos
  2699. if ($isFirstComment && !empty($audience)) {
  2700. $notificationTitle = "Visita Técnica No Programada #$idVisit";
  2701. $notificationMessage = "La visita técnica no programada #$idVisit ha pasado a estado Cerrado Pendiente.";
  2702. $this->notificationsController->emitNotification(
  2703. 'S002V01M10GMPR',
  2704. $notificationTitle,
  2705. $notificationMessage,
  2706. [[
  2707. 'BOTON' => 'Ver detalles',
  2708. 'FUNCION' => 'openPreventiveWorkOrderDetails',
  2709. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit)])
  2710. ], [
  2711. 'BOTON' => 'Ir al módulo',
  2712. 'FUNCION' => 'openModule',
  2713. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMPR/AOTR/RVTP')])
  2714. ]],
  2715. $audience,
  2716. $idUser,
  2717. $form['linea'],
  2718. $this->getSocketClient(),
  2719. $idVisit,
  2720. 'Preventivo'
  2721. );
  2722. }
  2723. // Notificar al regulador por cada comentario registrado
  2724. if (!empty($regulatorAudience)) {
  2725. $notificationTitle = "Visita Técnica No Programada #$idVisit";
  2726. $notificationMessage = "El usuario $operatorName ($idUser) registró comentario de finalización de la visita #$idVisit.";
  2727. $this->notificationsController->emitNotification(
  2728. 'S002V01M10GMPR',
  2729. $notificationTitle,
  2730. $notificationMessage,
  2731. [[
  2732. 'BOTON' => 'Ver detalles',
  2733. 'FUNCION' => 'openPreventiveWorkOrderDetails',
  2734. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit)])
  2735. ], [
  2736. 'BOTON' => 'Ir al módulo',
  2737. 'FUNCION' => 'openModule',
  2738. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMPR/AOTR/RVTP')])
  2739. ]],
  2740. $regulatorAudience,
  2741. $idUser,
  2742. $form['linea'],
  2743. $this->getSocketClient(),
  2744. $idVisit,
  2745. 'Preventivo'
  2746. );
  2747. }
  2748. // Si todos los comentarios están completos, enviar notificación final
  2749. if ($isComplete && !empty($regulatorAudience)) {
  2750. $finalNotificationTitle = "Visita Técnica No Programada #$idVisit";
  2751. $finalNotificationMessage = "La visita #$idVisit ya puede ser cerrada. Todos los operarios han registrado sus comentarios de finalización.";
  2752. $this->notificationsController->emitNotification(
  2753. 'S002V01M10GMPR',
  2754. $finalNotificationTitle,
  2755. $finalNotificationMessage,
  2756. [[
  2757. 'BOTON' => 'Cerrar visita',
  2758. 'FUNCION' => 'closePreventiveVisit',
  2759. 'PARAMETROS' => json_encode([
  2760. $this->encryptionController->encrypt($idVisit),
  2761. $this->encryptionController->encrypt($form['linea']),
  2762. $this->encryptionController->encrypt($regulatorId)
  2763. ])
  2764. ], [
  2765. 'BOTON' => 'Ver detalles',
  2766. 'FUNCION' => 'openPreventiveWorkOrderDetails',
  2767. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit)])
  2768. ], [
  2769. 'BOTON' => 'Ir al módulo',
  2770. 'FUNCION' => 'openModule',
  2771. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMPR/AOTR/RVTP')])
  2772. ]],
  2773. $regulatorAudience,
  2774. $idUser,
  2775. $form['linea'],
  2776. $this->getSocketClient(),
  2777. $idVisit,
  2778. 'Preventivo'
  2779. );
  2780. }
  2781. // Confirmar transacción
  2782. DB::commit();
  2783. $actions = DB::getQueryLog();
  2784. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2785. $idac = $this->functionsController->registerActivity(
  2786. $form['linea'],
  2787. 'S002V01M10GMPR',
  2788. 'S002V01F11RVTP',
  2789. 'S002V01P01REVI',
  2790. 'Actualización',
  2791. "El usuario $name (" . $usr->USUA_IDUS . ") registró comentario de finalización para la visita no programada #$idVisit.",
  2792. $idUser,
  2793. $nowStr,
  2794. 'S002V01S02AOTR'
  2795. );
  2796. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2797. return $this->responseController->makeResponse(false, 'EXITO.', [
  2798. 'comments_registered' => $commentsRegistered,
  2799. 'total_operators' => $totalOperators,
  2800. 'is_complete' => $isComplete
  2801. ]);
  2802. } catch (\Exception $e) {
  2803. // Revertir transacción en caso de error
  2804. DB::rollBack();
  2805. $actions = DB::getQueryLog();
  2806. $now = $this->functionsController->now();
  2807. $nowStr = $now->toDateTimeString();
  2808. $name = $this->functionsController->joinName($usr->USUA_NOMB ?? '', $usr->USUA_APPA ?? '', $usr->USUA_APMA ?? '');
  2809. $idac = $this->functionsController->registerActivity(
  2810. $form['linea'],
  2811. 'S002V01M10GMPR',
  2812. 'S002V01F11RVTP',
  2813. 'S002V01P01REVI',
  2814. 'Error',
  2815. "Error al registrar comentario de finalización para la visita no programada #$idVisit: " . $e->getMessage(),
  2816. $idUser,
  2817. $nowStr,
  2818. 'S002V01S02AOTR'
  2819. );
  2820. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2821. return $this->responseController->makeResponse(
  2822. true,
  2823. 'Ocurrió un error al registrar el comentario de finalización.',
  2824. ['error' => $e->getMessage()],
  2825. 500
  2826. );
  2827. }
  2828. }
  2829. // Visitas técnicas no programadas (Preventivas)
  2830. public function startPreventiveVisit(Request $request)
  2831. {
  2832. DB::enableQueryLog();
  2833. $validator = Validator::make($request->all(), [
  2834. 'id_user' => 'required|string',
  2835. 'linea' => 'required|integer',
  2836. 'id_visit' => 'required|string'
  2837. ]);
  2838. if ($validator->fails()) {
  2839. return $this->responseController->makeResponse(
  2840. true,
  2841. "Se encontraron uno o más errores.",
  2842. $this->responseController->makeErrors(
  2843. $validator->errors()->messages()
  2844. ),
  2845. 401
  2846. );
  2847. }
  2848. $form = $request->all();
  2849. $idUser = $this->encryptionController->decrypt($form['id_user']);
  2850. if (!$idUser) {
  2851. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  2852. }
  2853. $usr = DB::table('S002V01TUSUA')->where([
  2854. ['USUA_NULI', '=', $form['linea']],
  2855. ['USUA_IDUS', '=', $idUser]
  2856. ])->first();
  2857. if (is_null($usr)) {
  2858. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  2859. }
  2860. $idVisit = $this->encryptionController->decrypt($form['id_visit']);
  2861. if (!$idVisit) {
  2862. return $this->responseController->makeResponse(true, 'El ID de la visita no fue encriptado correctamente.', [], 400);
  2863. }
  2864. $visit = DB::table('S002V01TRVTN')->where([
  2865. ['RVTN_NULI', '=', $form['linea']],
  2866. ['RVTN_IDVI', '=', $idVisit]
  2867. ])->first();
  2868. if (is_null($visit)) {
  2869. return $this->responseController->makeResponse(true, 'La visita solicitada no existe.', [], 404);
  2870. }
  2871. $visitStates = [
  2872. 'VA' => 'Validada',
  2873. 'EP' => 'En progreso',
  2874. 'CP' => 'Cerrada pendiente',
  2875. 'CE' => 'Cerrada',
  2876. 'P' => 'Pendiente',
  2877. 'C' => 'Cancelada',
  2878. 'R' => 'Rechazada',
  2879. 'A' => 'Aprobada',
  2880. 'F' => 'Finalizada',
  2881. ];
  2882. if ($visit->RVTN_ESTA !== 'VA') {
  2883. $statusKey = $visit->RVTN_ESTA;
  2884. $statusName = array_key_exists($statusKey, $visitStates) ? $visitStates[$statusKey] : $statusKey;
  2885. return $this->responseController->makeResponse(true, "La visita está $statusName.", [], 401);
  2886. }
  2887. if ($visit->RVTN_USRE != $idUser) {
  2888. return $this->responseController->makeResponse(true, 'El usuario que solicitó la acción no tiene los permisos necesarios.', [], 401);
  2889. }
  2890. $statusHistoryArr = json_decode($visit->RVTN_HIES, true);
  2891. if (!is_array($statusHistoryArr)) {
  2892. $statusHistoryArr = [];
  2893. }
  2894. // Buscar objeto VA en el historial
  2895. // Si no existe pero RVTN_ESTA == 'VA', la visita está validada pero aún no tiene operarios asignados
  2896. $validatedHistoryFilt = array_filter($statusHistoryArr, function ($item) {
  2897. return array_key_exists('ESTADO', $item) && $item['ESTADO'] == 'VA';
  2898. });
  2899. $audience = [];
  2900. // Si existe el objeto VA en el historial, obtener los operarios aceptados
  2901. if (!empty($validatedHistoryFilt)) {
  2902. $validatedStatus = end($validatedHistoryFilt);
  2903. if (array_key_exists('ATENCION', $validatedStatus) && is_array($validatedStatus['ATENCION'])) {
  2904. foreach ($validatedStatus['ATENCION'] as $item) {
  2905. if (($item['RESPUESTA'] ?? '') === 'A') {
  2906. $audience[] = $item['ID'];
  2907. }
  2908. }
  2909. }
  2910. }
  2911. // Si no existe el objeto VA en el historial pero RVTN_ESTA == 'VA',
  2912. // la audiencia quedará vacía (no hay operarios asignados aún)
  2913. $now = $this->functionsController->now();
  2914. $nowStr = $now->toDateTimeString();
  2915. $statusHistoryArr[] = [
  2916. 'USUARIO' => $idUser,
  2917. 'ESTADO' => 'EP',
  2918. 'FECHA' => $nowStr
  2919. ];
  2920. $statusHistoryStr = json_encode($statusHistoryArr);
  2921. /*
  2922. $commentsArr = json_decode($visit->RVTN_COME, true);
  2923. if (!is_array($commentsArr)) {
  2924. $commentsArr = [];
  2925. }
  2926. $commentsArr['COMENTARIO_INICIO'] = $form['comments'];
  2927. $commentsStr = json_encode($commentsArr);
  2928. */
  2929. DB::table('S002V01TRVTN')->where([
  2930. ['RVTN_IDVI', '=', $idVisit],
  2931. ['RVTN_NULI', '=', $form['linea']]
  2932. ])->update([
  2933. 'RVTN_ESTA' => 'EP',
  2934. 'RVTN_HIES' => $statusHistoryStr,
  2935. 'RVTN_USMO' => $idUser,
  2936. 'RVTN_FEMO' => $nowStr,
  2937. ]);
  2938. $this->notificationsController->emitNotification(
  2939. 'S002V01M10GMPR',
  2940. "Visita Técnica No Programada #$idVisit",
  2941. "Inicio de la ejecución de la visita técnica no programada #$idVisit.",
  2942. [[
  2943. 'BOTON' => 'Ver detalles',
  2944. 'FUNCION' => 'openPreventiveWorkOrderDetails',
  2945. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit)])
  2946. ], [
  2947. 'BOTON' => 'Ir al módulo',
  2948. 'FUNCION' => 'openModule',
  2949. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMPR/AOTR/RVTP')])
  2950. ]],
  2951. $audience,
  2952. $idUser,
  2953. $form['linea'],
  2954. $this->getSocketClient(),
  2955. $idVisit,
  2956. 'Preventivo'
  2957. );
  2958. $actions = DB::getQueryLog();
  2959. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2960. $idac = $this->functionsController->registerActivity(
  2961. $form['linea'],
  2962. 'S002V01M10GMPR',
  2963. 'S002V01F11RVTP',
  2964. 'S002V01P01REVI',
  2965. 'Actualización',
  2966. "El usuario $name (" . $usr->USUA_IDUS . ") inició la visita técnica no programada #$idVisit.",
  2967. $idUser,
  2968. $nowStr,
  2969. 'S002V01S02AOTR'
  2970. );
  2971. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2972. return $this->responseController->makeResponse(false, 'EXITO.');
  2973. }
  2974. public function updateOrderStatus(Request $request)
  2975. {
  2976. DB::enableQueryLog();
  2977. $validator = Validator::make($request->all(), [
  2978. 'id_user' => 'required|string',
  2979. 'linea' => 'required|integer',
  2980. 'id_order' => 'required|string',
  2981. 'status' => 'required|string|in:B,R,A,E'
  2982. ]);
  2983. if ($validator->fails()) {
  2984. return $this->responseController->makeResponse(
  2985. true,
  2986. "Se encontraron uno o más errores.",
  2987. $this->responseController->makeErrors(
  2988. $validator->errors()->messages()
  2989. ),
  2990. 401
  2991. );
  2992. }
  2993. $form = $request->all();
  2994. $idUser = $this->encryptionController->decrypt($form['id_user']);
  2995. if (!$idUser) {
  2996. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  2997. }
  2998. $usr = DB::table('S002V01TUSUA')->where([
  2999. ['USUA_NULI', '=', $form['linea']],
  3000. ['USUA_IDUS', '=', $idUser],
  3001. ])->first();
  3002. if (is_null($usr)) {
  3003. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  3004. }
  3005. $idOrder = $this->encryptionController->decrypt($form['id_order']);
  3006. if (!$idOrder) {
  3007. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  3008. }
  3009. $order = DB::table('S002V01TOTPR')->where([
  3010. ['OTPR_NULI', '=', $form['linea']],
  3011. ['OTPR_IDOT', '=', $idOrder],
  3012. ])->first();
  3013. if (is_null($order)) {
  3014. return $this->responseController->makeResponse(true, 'La orden solicitada no existe.', [], 404);
  3015. }
  3016. if ($form['status'] == 'A' && $order->OTPR_SEAN == null) {
  3017. return $this->responseController->makeResponse(true, 'La orden no puede ser aprobada sin la asignación del análisis presupuestario.', [], 401);
  3018. }
  3019. $statusStr = '';
  3020. switch ($form['status']) {
  3021. case 'B':
  3022. $statusStr = 'Borrador';
  3023. break;
  3024. case 'R':
  3025. $statusStr = 'Revisión';
  3026. break;
  3027. case 'A':
  3028. $statusStr = 'Aprobado';
  3029. break;
  3030. case 'E':
  3031. $statusStr = 'Eliminado';
  3032. break;
  3033. }
  3034. $now = $this->functionsController->now();
  3035. $nowStr = $now->toDateTimeString();
  3036. DB::table('S002V01TOTPR')->where([
  3037. ['OTPR_NULI', '=', $form['linea']],
  3038. ['OTPR_IDOT', '=', $idOrder],
  3039. ])->update([
  3040. 'OTPR_ESTA' => $form['status'],
  3041. 'OTPR_USMO' => $idUser,
  3042. 'OTPR_FEMO' => $nowStr
  3043. ]);
  3044. $actions = DB::getQueryLog();
  3045. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3046. $idac = $this->functionsController->registerActivity(
  3047. $form['linea'],
  3048. 'S002V01M10GMPR',
  3049. 'S002V01F01COTP',
  3050. 'S002V01P01HOTP',
  3051. 'Actualización',
  3052. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó el estado de la orden #$idOrder a $statusStr.",
  3053. $idUser,
  3054. $nowStr,
  3055. 'S002V01S01ORTR'
  3056. );
  3057. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  3058. return $this->responseController->makeResponse(false, 'EXITO.');
  3059. }
  3060. public function setBudgetAnalysis(Request $request)
  3061. {
  3062. DB::enableQueryLog();
  3063. $validator = Validator::make($request->all(), [
  3064. 'id_user' => 'required|string',
  3065. 'linea' => 'required|integer',
  3066. 'id_order' => 'required|string',
  3067. 'analysis' => 'required|json'
  3068. ]);
  3069. if ($validator->fails()) {
  3070. return $this->responseController->makeResponse(
  3071. true,
  3072. "Se encontraron uno o más errores.",
  3073. $this->responseController->makeErrors(
  3074. $validator->errors()->messages()
  3075. ),
  3076. 401
  3077. );
  3078. }
  3079. $form = $request->all();
  3080. $idUser = $this->encryptionController->decrypt($form['id_user']);
  3081. if (!$idUser) {
  3082. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  3083. }
  3084. $usr = DB::table('S002V01TUSUA')->where([
  3085. ['USUA_NULI', '=', $form['linea']],
  3086. ['USUA_IDUS', '=', $idUser],
  3087. ])->first();
  3088. if (is_null($usr)) {
  3089. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  3090. }
  3091. $idOrder = $this->encryptionController->decrypt($form['id_order']);
  3092. if (!$idOrder) {
  3093. return $this->responseController->makeResponse(true, 'El ID de la orden no está encriptado correctamente.', [], 400);
  3094. }
  3095. $order = DB::table('S002V01TOTPR')->where([
  3096. ['OTPR_NULI', '=', $form['linea']],
  3097. ['OTPR_IDOT', '=', $idOrder]
  3098. ])->first();
  3099. if (is_null($order)) {
  3100. return $this->responseController->makeResponse(true, 'La orden consultada no está registrada.', [], 404);
  3101. }
  3102. $analysisArr = json_decode($form['analysis'], true);
  3103. if (empty($analysisArr)) {
  3104. return $this->responseController->makeResponse(true, 'El JSON de análisis no contiene información.', [], 400);
  3105. } else if (!array_key_exists('teamsConf', $analysisArr)) {
  3106. return $this->responseController->makeResponse(true, 'La información de la configuración de los equipos no se envió en el JSON.', [], 400);
  3107. } else if (!is_array($analysisArr['teamsConf'])) {
  3108. return $this->responseController->makeResponse(true, 'La configuración de los equipos tiene un formato inválido.', [], 400);
  3109. }
  3110. $teamsConf = $analysisArr['teamsConf'];
  3111. foreach ($teamsConf as $key => $conf) {
  3112. $idDec = $this->encryptionController->decrypt($conf['ID']);
  3113. if (!$idDec) {
  3114. return $this->responseController->makeResponse(true, "El ID del elemento en la posición $key del arreglo de especialidades no fue encriptado correctamente.", [], 400);
  3115. }
  3116. $conf['ID'] = $idDec;
  3117. $teamsConf[$key] = $conf;
  3118. }
  3119. $analysisArr['teamsConf'] = $teamsConf;
  3120. $now = $this->functionsController->now();
  3121. $nowStr = $now->toDateTimeString();
  3122. $sean = json_encode($analysisArr);
  3123. DB::table('S002V01TOTPR')->where([
  3124. ['OTPR_NULI', '=', $form['linea']],
  3125. ['OTPR_IDOT', '=', $idOrder],
  3126. ])->update([
  3127. 'OTPR_SEAN' => $sean,
  3128. 'OTPR_USMO' => $idUser,
  3129. 'OTPR_FEMO' => $nowStr
  3130. ]);
  3131. $actions = DB::getQueryLog();
  3132. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3133. $idac = $this->functionsController->registerActivity(
  3134. $form['linea'],
  3135. 'S002V01M10GMPR',
  3136. 'S002V01F01COTP',
  3137. 'S002V01P01HOTP',
  3138. 'Actualización',
  3139. "El usuario $name (" . $usr->USUA_IDUS . ") asignó el análisis presupuestario de la orden #$idOrder.",
  3140. $idUser,
  3141. $nowStr,
  3142. 'S002V01S01ORTR'
  3143. );
  3144. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  3145. return $this->responseController->makeResponse(false, 'EXITO.');
  3146. }
  3147. public function getMaintenanceSimulation($idOrder, $idUser, $line)
  3148. {
  3149. DB::enableQueryLog();
  3150. $idUser = $this->encryptionController->decrypt($idUser);
  3151. if (!$idUser) {
  3152. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  3153. }
  3154. $usr = DB::table('S002V01TUSUA')->where([
  3155. ['USUA_NULI', '=', $line],
  3156. ['USUA_IDUS', '=', $idUser],
  3157. ])->first();
  3158. if (is_null($usr)) {
  3159. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  3160. }
  3161. $idOrder = $this->encryptionController->decrypt($idOrder);
  3162. if (!$idOrder) {
  3163. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  3164. }
  3165. $order = DB::table('S002V01TOTPR')->select([
  3166. 'OTPR_IDOT AS IDORDER',
  3167. 'OTPR_DEIN AS DESCRIPCION',
  3168. 'OTPR_ININ AS INSTRUCCIONES',
  3169. 'OTPR_EQIN AS EQUIPAMIENTO',
  3170. 'OTPR_FIAP AS FECHAINICIO',
  3171. 'OTPR_FTAP AS FECHAFINAL',
  3172. 'OTPR_SEAN AS ANALISIS',
  3173. 'OTPR_TIIN AS TIEINMEST',
  3174. 'OTPR_OPPR AS OPERARIOS',
  3175. 'OTPR_DTIN AS TIETOTEST',
  3176. 'OTPR_RHRE AS RECURSOS',
  3177. 'OTPR_ACAS AS ACTIVADOR',
  3178. 'OTPR_ESTA AS ESTADO',
  3179. 'ACTI_PRIO AS PRIORIDAD',
  3180. 'ACTI_TIAC AS TIPOACTIVADOR',
  3181. 'ACTI_COAC AS CONFIGACTI'
  3182. ])->join('S002V01TACTI', 'ACTI_IDAC', '=', 'OTPR_ACAS')->where([
  3183. ['OTPR_NULI', '=', $line],
  3184. ['OTPR_IDOT', '=', $idOrder]
  3185. ])->first();
  3186. if (is_null($order)) {
  3187. return $this->responseController->makeResponse(true, 'La orden solicitada no existe.', [], 404);
  3188. } else if ($order->ESTADO == 'B') {
  3189. return $this->responseController->makeResponse(true, 'La orden solicitada no puede ejecutarse porque se encuentra en borradores.', [], 401);
  3190. } else if ($order->ESTADO == 'R') {
  3191. return $this->responseController->makeResponse(true, 'La orden solicitada no puede ejecutarse porque se encuentra en revisión.', [], 401);
  3192. } else if ($order->ESTADO == 'E') {
  3193. return $this->responseController->makeResponse(true, 'La orden solicitada no puede ejecutarse porque ha sido eliminado.', [], 401);
  3194. }
  3195. $analisisArr = json_decode($order->ANALISIS, true);
  3196. $teamsConf = $analisisArr['teamsConf'];
  3197. foreach ($teamsConf as $key => $val) {
  3198. $val['ID'] = $this->encryptionController->encrypt($val['ID']);
  3199. $teamsConf[$key] = $val;
  3200. }
  3201. $analisisArr['teamsConf'] = $teamsConf;
  3202. $order->ANALISIS = json_encode($analisisArr);
  3203. $actions = DB::getQueryLog();
  3204. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3205. $now = $this->functionsController->now();
  3206. $nowStr = $now->toDateTimeString();
  3207. $idac = $this->functionsController->registerActivity(
  3208. $line,
  3209. 'S002V01M10GMPR',
  3210. 'S002V01F06SPMA',
  3211. 'S002V01P02VSPM',
  3212. 'Consulta',
  3213. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la simulación de la orden #$idOrder.",
  3214. $idUser,
  3215. $nowStr,
  3216. 'S002V01S02AOTR'
  3217. );
  3218. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  3219. return $this->responseController->makeResponse(false, 'EXITO.', $order);
  3220. }
  3221. public function getOrderWithActivator($idOrder, $idUser, $line)
  3222. {
  3223. DB::enableQueryLog();
  3224. $idUser = $this->encryptionController->decrypt($idUser);
  3225. if (!$idUser) {
  3226. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  3227. }
  3228. $usr = DB::table('S002V01TUSUA')->where([
  3229. ['USUA_NULI', '=', $line],
  3230. ['USUA_IDUS', '=', $idUser],
  3231. ])->first();
  3232. if (is_null($usr)) {
  3233. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  3234. }
  3235. $idOrder = $this->encryptionController->decrypt($idOrder);
  3236. if (!$idOrder) {
  3237. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  3238. }
  3239. $order = DB::table('S002V01TOTPR')->select([
  3240. 'OTPR_IDOT AS IDORDEN',
  3241. 'OTPR_DEIN AS DESCRIPCION',
  3242. 'OTPR_ININ AS INSTRUCCIONES',
  3243. 'OTPR_EQIN AS EQUIPAMIENTO',
  3244. 'OTPR_FIAP AS FECHAINICIO',
  3245. 'OTPR_FTAP AS FECHAFINAL',
  3246. 'OTPR_TIIN AS TIEINMEST',
  3247. 'OTPR_DTIN AS TIETOTEST',
  3248. 'OTPR_ACAS AS ACTIVADOR',
  3249. 'OTPR_CLAS AS CLASIFICACION',
  3250. 'OTPR_ESTA AS ESTADO',
  3251. 'ACTI_PRIO AS PRIORIDAD',
  3252. 'ACTI_TIAC AS TIPOACTIVADOR',
  3253. 'ACTI_COAC AS CONFIGACTIVADOR',
  3254. ])->join('S002V01TACTI', 'ACTI_IDAC', '=', 'OTPR_ACAS')->where([
  3255. ['OTPR_NULI', '=', $line],
  3256. ['OTPR_IDOT', '=', $idOrder],
  3257. ])->first();
  3258. if (is_null($order)) {
  3259. return $this->responseController->makeResponse(true, 'La orden solicitada no está registrada.', [], 404);
  3260. }
  3261. $now = $this->functionsController->now();
  3262. $nowStr = $now->toDateTimeString();
  3263. $actions = DB::getQueryLog();
  3264. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3265. $idac = $this->functionsController->registerActivity(
  3266. $line,
  3267. 'S002V01M10GMPR',
  3268. 'S002V01F02AFDT',
  3269. 'S002V01P01OTAT',
  3270. 'Consulta',
  3271. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la información de la orden #$idOrder.",
  3272. $idUser,
  3273. $nowStr,
  3274. 'S002V01S02AOTR'
  3275. );
  3276. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  3277. return $this->responseController->makeResponse(false, 'EXITO.', $order);
  3278. }
  3279. public function updateOrderWithActivator(Request $request)
  3280. {
  3281. DB::enableQueryLog();
  3282. $validator = Validator::make($request->all(), [
  3283. 'id_user' => 'required|string',
  3284. 'linea' => 'required|integer',
  3285. 'id_order' => 'required|string',
  3286. 'id_activator' => 'required|string',
  3287. 'start_date' => 'required|date',
  3288. 'end_date' => 'required|date',
  3289. 'activator_type' => 'required|string|in:Calendario,Sintoma,Medida,Valor',
  3290. 'priority' => 'required|string|in:1,2,3,4',
  3291. 'config_activator' => 'required|json',
  3292. ]);
  3293. if ($validator->fails()) {
  3294. return $this->responseController->makeResponse(
  3295. true,
  3296. "Se encontraron uno o más errores.",
  3297. $this->responseController->makeErrors(
  3298. $validator->errors()->messages()
  3299. ),
  3300. 401
  3301. );
  3302. }
  3303. $form = $request->all();
  3304. $idUser = $this->encryptionController->decrypt($form['id_user']);
  3305. if (!$idUser) {
  3306. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  3307. }
  3308. $usr = DB::table('S002V01TUSUA')->where([
  3309. ['USUA_IDUS', '=', $idUser],
  3310. ['USUA_NULI', '=', $form['linea']],
  3311. ])->first();
  3312. if (is_null($usr)) {
  3313. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  3314. }
  3315. $idOrder = $this->encryptionController->decrypt($form['id_order']);
  3316. if (!$idOrder) {
  3317. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  3318. }
  3319. $order = DB::table('S002V01TOTPR')->where([
  3320. ['OTPR_NULI', '=', $form['linea']],
  3321. ['OTPR_IDOT', '=', $idOrder],
  3322. ])->first();
  3323. if (is_null($order)) {
  3324. return $this->responseController->makeResponse(true, 'La orden de trabajo solicitada no existe.', [], 404);
  3325. }
  3326. $idActivator = $this->encryptionController->decrypt($form['id_activator']);
  3327. if (!$idActivator) {
  3328. return $this->responseController->makeResponse(true, 'El ID del activador solicitado no está encriptado correctamente.', [], 400);
  3329. }
  3330. $activator = DB::table('S002V01TACTI')->where([
  3331. ['ACTI_IDAC', '=', $idActivator],
  3332. ['ACTI_NULI', '=', $form['linea']]
  3333. ])->first();
  3334. if (is_null($activator)) {
  3335. return $this->responseController->makeResponse(true, 'El activador solicitado no existe.', [], 404);
  3336. }
  3337. $now = $this->functionsController->now();
  3338. $nowStr = $now->toDateTimeString();
  3339. DB::table('S002V01TOTPR')->where([
  3340. ['OTPR_NULI', '=', $form['linea']],
  3341. ['OTPR_IDOT', '=', $idOrder],
  3342. ])->update([
  3343. 'OTPR_ACAS' => $idActivator,
  3344. 'OTPR_FIAP' => $form['start_date'],
  3345. 'OTPR_FTAP' => $form['end_date'],
  3346. 'OTPR_USMO' => $idUser,
  3347. 'OTPR_FEMO' => $nowStr,
  3348. ]);
  3349. DB::table('S002V01TACTI')->where([
  3350. ['ACTI_IDAC', '=', $idActivator],
  3351. ['ACTI_NULI', '=', $form['linea']]
  3352. ])->update([
  3353. 'ACTI_PRIO' => $form['priority'],
  3354. 'ACTI_TIAC' => $form['activator_type'],
  3355. 'ACTI_COAC' => $form['config_activator'],
  3356. 'ACTI_USMO' => $idUser,
  3357. 'ACTI_FEMO' => $nowStr,
  3358. ]);
  3359. $actions = DB::getQueryLog();
  3360. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3361. $idac = $this->functionsController->registerActivity(
  3362. $form['linea'],
  3363. 'S002V01M10GMPR',
  3364. 'S002V01F02AFDT',
  3365. 'S002V01P02ACAC',
  3366. 'Actualización',
  3367. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó la orden #$idOrder y el activador #$idActivator.",
  3368. $idUser,
  3369. $nowStr,
  3370. 'S002V01S02AOTR'
  3371. );
  3372. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  3373. return $this->responseController->makeResponse(false, 'EXITO.');
  3374. }
  3375. public function printOrderSimulation($idOrder, $idUser, $line)
  3376. {
  3377. DB::enableQueryLog();
  3378. $idUser = $this->encryptionController->decrypt($idUser);
  3379. if (!$idUser) {
  3380. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  3381. }
  3382. $usr = DB::table('S002V01TUSUA')->where([
  3383. ['USUA_NULI', '=', $line],
  3384. ['USUA_IDUS', '=', $idUser],
  3385. ])->first();
  3386. if (is_null($usr)) {
  3387. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  3388. }
  3389. $idOrder = $this->encryptionController->decrypt($idOrder);
  3390. if (!$idOrder) {
  3391. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  3392. }
  3393. $order = DB::table('S002V01TOTPR')->where([
  3394. ['OTPR_NULI', '=', $line],
  3395. ['OTPR_IDOT', '=', $idOrder]
  3396. ])->first();
  3397. if (is_null($order)) {
  3398. return $this->responseController->makeResponse(true, 'La orden solicitada no está registrada.', [], 404);
  3399. }
  3400. $simulation = DB::table('S002V01TRESI')->where([
  3401. ['RESI_NULI', '=', $line],
  3402. ['RESI_IDOT', '=', $idOrder]
  3403. ])->first();
  3404. if (is_null($simulation)) {
  3405. return $this->responseController->makeResponse(true, "La orden #$idOrder aún no ha sido simulada.", [], 401);
  3406. }
  3407. $content = json_decode($simulation->RESI_CUSI, true);
  3408. $html = "
  3409. <html>
  3410. <head>
  3411. <link href=\"https://fonts.googleapis.com/icon?family=Material+Icons\" rel=\"stylesheet\">
  3412. <style>
  3413. .main-icon-cell{
  3414. width: 50px;
  3415. height: 50px;
  3416. }
  3417. .main-icon-container{
  3418. width: 50px;
  3419. height: 50px;
  3420. border-radius: 64px;
  3421. border-style: solid;
  3422. padding-left: 8px;
  3423. padding-top: 8px;
  3424. border-color: rgba(0, 0, 0, 0.8);
  3425. }
  3426. .icon-container{
  3427. padding-left: 8px;
  3428. width: 42px;
  3429. height: 42px;
  3430. }
  3431. .instruction-font{
  3432. color: rgba(0, 0, 0, 0.8);
  3433. font-size: 24px;
  3434. padding-left: 8px;
  3435. font-weight: 600;
  3436. }
  3437. .space-cell{
  3438. width: 50px;
  3439. }
  3440. </style>
  3441. </head>
  3442. <body>
  3443. <table width=\"100%\">
  3444. ";
  3445. foreach ($content as $item) {
  3446. $imgContent = file_get_contents($this->functionsController->getBasePath() . "\storage\app\\files\\$item[icon].png");
  3447. $imgB64 = "data:image/png;base64, " . base64_encode($imgContent);
  3448. $html .= "
  3449. <tr>
  3450. ";
  3451. if (!$item['isMain']) {
  3452. $html .= '<td class="space-cell"></td>';
  3453. }
  3454. $html .= "
  3455. <td class=\"main-icon-cell\">
  3456. <div class=\"
  3457. ";
  3458. if ($item['isMain']) {
  3459. $html .= 'main-icon-container">';
  3460. } else {
  3461. $html .= 'icon-container ">';
  3462. }
  3463. $html .= "
  3464. <img src=\"$imgB64\" width=\"42\" height=\"42\">
  3465. </div>
  3466. </td>
  3467. <td class=\"instruction-font\" colspan=\"
  3468. ";
  3469. if ($item['isMain']) {
  3470. $html .= '2';
  3471. }
  3472. $html .= "
  3473. \">$item[label]</td>
  3474. </tr>
  3475. ";
  3476. }
  3477. $html .= "
  3478. </table>
  3479. </body>
  3480. </html>";
  3481. $filePath = $this->functionsController->getBasePath() . '\public_files\\';
  3482. $noar = "simulacion_orden_$idOrder";
  3483. $exte = "pdf";
  3484. $line = intval($line);
  3485. $line = $line < 10 ? "0$line" : "$line";
  3486. $como = "GMPR";
  3487. $cldo = "OR";
  3488. $now = $this->functionsController->now();
  3489. $nowStr = $now->toDateTimeString();
  3490. $nowArr = explode(" ", $nowStr);
  3491. $dateArr = explode("-", $nowArr[0]);
  3492. $year = substr($dateArr[0], 2);
  3493. $fecr = $year . $dateArr[1] . $dateArr[2];
  3494. $sec = DB::table('S002V01TAFAL')->where([
  3495. ['AFAL_NULI', '=', $line],
  3496. ['AFAL_COMO', '=', $como],
  3497. ['AFAL_CLDO', '=', $cldo],
  3498. ])->orderBy('AFAL_NUSE', 'desc')->first();
  3499. $nuse = is_null($sec) ? "1" : "" . intval($sec->AFAL_NUSE) + 1 . "";
  3500. for ($i = strlen($nuse); $i < 6; $i++) {
  3501. $nuse = "0$nuse";
  3502. }
  3503. $ver = DB::table('S002V01TAFAL')->where([
  3504. ['AFAL_NULI', '=', $line],
  3505. ['AFAL_COMO', '=', $como],
  3506. ['AFAL_CLDO', '=', $cldo],
  3507. ['AFAL_NOAR', '=', $noar],
  3508. ['AFAL_EXTE', '=', $exte],
  3509. ])->orderBy('AFAL_NUVE', 'desc')->first();
  3510. $nuve = is_null($ver) ? "1" : "" . intval($ver->AFAL_NUVE) + 1 . "";
  3511. for ($i = strlen($nuve); $i < 2; $i++) {
  3512. $nuve = "0$nuve";
  3513. }
  3514. $fileName = "$line-$como-$cldo-$fecr-$nuse=$nuve=$noar.$exte";
  3515. $dompdf = new Dompdf();
  3516. $dompdf->loadHtml($html);
  3517. $dompdf->setPaper('A4', 'portrait');
  3518. $dompdf->render();
  3519. $output = $dompdf->output();
  3520. $tempFile = $filePath . $fileName;
  3521. if (!file_exists($tempFile)) {
  3522. fopen($tempFile, 'w');
  3523. }
  3524. file_put_contents($tempFile, $output);
  3525. $ubic = Storage::putFile('files', new File($tempFile));
  3526. $ubic = str_replace("/", "\\", $ubic);
  3527. $ubic = $this->functionsController->getBasePath() . "\storage\app\\" . $ubic;
  3528. $tama = filesize($ubic);
  3529. $usac = json_encode([$idUser]);
  3530. unlink($tempFile);
  3531. DB::table('S002V01TAFAL')->insert([
  3532. 'AFAL_NULI' => $line,
  3533. 'AFAL_COMO' => $como,
  3534. 'AFAL_CLDO' => $cldo,
  3535. 'AFAL_FECR' => $fecr,
  3536. 'AFAL_NUSE' => $nuse,
  3537. 'AFAL_NUVE' => $nuve,
  3538. 'AFAL_NOAR' => $noar,
  3539. 'AFAL_EXTE' => $exte,
  3540. 'AFAL_TAMA' => $tama,
  3541. 'AFAL_UBIC' => $ubic,
  3542. 'AFAL_USAC' => $usac,
  3543. 'AFAL_USRE' => $idUser,
  3544. 'AFAL_FERE' => $nowStr,
  3545. ]);
  3546. $filesArr = json_decode($order->OTPR_DONE, true);
  3547. $filesArr[] = $fileName;
  3548. $filesStr = json_encode($filesArr);
  3549. DB::table('S002V01TOTPR')->where([
  3550. ['OTPR_NULI', '=', $line],
  3551. ['OTPR_IDOT', '=', $idOrder]
  3552. ])->update([
  3553. 'OTPR_DONE' => $filesStr,
  3554. 'OTPR_USMO' => $idUser,
  3555. 'OTPR_FEMO' => $nowStr
  3556. ]);
  3557. $actions = DB::getQueryLog();
  3558. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3559. $idac = $this->functionsController->registerActivity(
  3560. $line,
  3561. 'S002V01M10GMPR',
  3562. 'S002V01F06SPMA',
  3563. 'S002V01P02VSPM',
  3564. 'Registro',
  3565. "El usuario $name (" . $usr->USUA_IDUS . ") generó el archivo de simulación de la orden #$idOrder.",
  3566. $idUser,
  3567. $nowStr,
  3568. 'S002V01S02AOTR'
  3569. );
  3570. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  3571. return $this->responseController->makeResponse(false, 'EXITO.', ['fileID' => $fileName]);
  3572. }
  3573. public function saveOrderSimulation(Request $request)
  3574. {
  3575. DB::enableQueryLog();
  3576. $validator = Validator::make($request->all(), [
  3577. 'id_user' => 'required|string',
  3578. 'linea' => 'required|integer',
  3579. 'id_order' => 'required|string',
  3580. 'content' => 'required|json',
  3581. ]);
  3582. if ($validator->fails()) {
  3583. return $this->responseController->makeResponse(
  3584. true,
  3585. "Se encontraron uno o más errores.",
  3586. $this->responseController->makeErrors(
  3587. $validator->errors()->messages()
  3588. ),
  3589. 401
  3590. );
  3591. }
  3592. $form = $request->all();
  3593. $idUser = $this->encryptionController->decrypt($form['id_user']);
  3594. if (!$idUser) {
  3595. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  3596. }
  3597. $usr = DB::table('S002V01TUSUA')->where([
  3598. ['USUA_NULI', '=', $form['linea']],
  3599. ['USUA_IDUS', '=', $idUser],
  3600. ])->first();
  3601. if (is_null($usr)) {
  3602. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  3603. }
  3604. $idOrder = $this->encryptionController->decrypt($form['id_order']);
  3605. if (!$idOrder) {
  3606. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  3607. }
  3608. $order = DB::table('S002V01TOTPR')->where([
  3609. ['OTPR_NULI', '=', $form['linea']],
  3610. ['OTPR_IDOT', '=', $idOrder],
  3611. ])->first();
  3612. if (is_null($order)) {
  3613. return $this->responseController->makeResponse(true, 'La orden consultada no está registrada.', [], 404);
  3614. }
  3615. $simulation = DB::table('S002V01TRESI')->where([
  3616. ['RESI_NULI', '=', $form['linea']],
  3617. ['RESI_IDOT', '=', $idOrder],
  3618. ])->first();
  3619. $now = $this->functionsController->now();
  3620. $nowStr = $now->toDateTimeString();
  3621. if (is_null($simulation)) {
  3622. DB::table('S002V01TRESI')->insert([
  3623. 'RESI_NULI' => $form['linea'],
  3624. 'RESI_IDOT' => $idOrder,
  3625. 'RESI_CUSI' => $form['content'],
  3626. 'RESI_FUSI' => $nowStr,
  3627. 'RESI_UUSI' => $idUser,
  3628. ]);
  3629. } else {
  3630. $timestamp = $now->timestamp;
  3631. $history = $simulation->RESI_HISI == null ? [] : json_decode($simulation->RESI_HISI, true);
  3632. $history[$timestamp] = [
  3633. 'USUARIO' => $simulation->RESI_UUSI,
  3634. 'FECHA' => $simulation->RESI_FUSI,
  3635. 'CONTENIDO' => $simulation->RESI_CUSI,
  3636. ];
  3637. $historyStr = json_encode($history);
  3638. DB::table('S002V01TRESI')->where([
  3639. ['RESI_NULI', '=', $form['linea']],
  3640. ['RESI_IDOT', '=', $idOrder],
  3641. ])->update([
  3642. 'RESI_CUSI' => $form['content'],
  3643. 'RESI_FUSI' => $nowStr,
  3644. 'RESI_UUSI' => $idUser,
  3645. 'RESI_HISI' => $historyStr,
  3646. ]);
  3647. }
  3648. $actions = DB::getQueryLog();
  3649. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3650. $idac = $this->functionsController->registerActivity(
  3651. $form['linea'],
  3652. 'S002V01M10GMPR',
  3653. 'S002V01F06SPMA',
  3654. 'S002V01P02VSPM',
  3655. 'Registro',
  3656. "El usuario $name (" . $usr->USUA_IDUS . ") registró una simulación para la orden #$idOrder.",
  3657. $idUser,
  3658. $nowStr,
  3659. 'S002V01S02AOTR'
  3660. );
  3661. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  3662. return $this->responseController->makeResponse(false, 'EXITO.');
  3663. }
  3664. public function printOrderDetails($idOrder, $idUser, $line)
  3665. {
  3666. DB::enableQueryLog();
  3667. $idUser = $this->encryptionController->decrypt($idUser);
  3668. if (!$idUser) {
  3669. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  3670. }
  3671. $usr = DB::table('S002V01TUSUA')->where([
  3672. ['USUA_NULI', '=', $line],
  3673. ['USUA_IDUS', '=', $idUser]
  3674. ])->first();
  3675. if (is_null($usr)) {
  3676. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  3677. }
  3678. $idOrder = $this->encryptionController->decrypt($idOrder);
  3679. if (!$idOrder) {
  3680. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  3681. }
  3682. $order = DB::table('S002V01TOTPR')->join(
  3683. 'S002V01TACTI',
  3684. 'OTPR_ACAS',
  3685. '=',
  3686. 'ACTI_IDAC',
  3687. )->where([
  3688. ['OTPR_NULI', '=', $line],
  3689. ['OTPR_IDOT', '=', $idOrder]
  3690. ])->first();
  3691. if (is_null($order)) {
  3692. return $this->responseController->makeResponse(true, 'La orden solicitada no está registrada.', [], 404);
  3693. }
  3694. $html = "
  3695. <html>
  3696. <head>
  3697. <style>
  3698. table{
  3699. border-collapse: separate;
  3700. border-spacing: 0;
  3701. width: 100%;
  3702. }
  3703. .cell-title{
  3704. font-size: 12px;
  3705. font-weight: bold;
  3706. padding: 8px 8px 4px 8px;
  3707. }
  3708. .top-corners-radius{
  3709. border-radius: 4px 4px 0 0;
  3710. }
  3711. .left-bottom-corner-radius{
  3712. border-radius: 0 0 0 4px;
  3713. }
  3714. .right-bottom-corner-radius{
  3715. border-radius: 0 0 4px 0;
  3716. }
  3717. .cell-content{
  3718. padding: 4px 8px 8px 8px;
  3719. font-size: 16px;
  3720. color: rgba(0, 0, 0, 0.7);
  3721. }
  3722. .border-top{ border-top: 1px solid #dddddd; }
  3723. .border-left{ border-left: 1px solid #dddddd; }
  3724. .border-right{ border-right: 1px solid #dddddd; }
  3725. .border-bottom{ border-bottom: 1px solid #dddddd; }
  3726. </style>
  3727. </head>
  3728. <body>
  3729. <table>
  3730. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-title top-corners-radius border-top border-left border-right\">Descripción</td></tr>
  3731. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-content border-left border-right\">" . $order->OTPR_DEIN . "</td></tr>
  3732. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-title border-top border-left border-right\">Instrucciones</td></tr>
  3733. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-content border-left border-right\">
  3734. ";
  3735. $instructions = json_decode($order->OTPR_ININ, true);
  3736. $cont = 1;
  3737. foreach ($instructions as $instruction) {
  3738. $html .= $cont . ".- " . $instruction['INSTRUCCION'] . "<br>";
  3739. $cont++;
  3740. }
  3741. $startDate = $this->functionsController->formatDateTime($order->OTPR_FIAP);
  3742. $endDate = $this->functionsController->formatDateTime($order->OTPR_FTAP);
  3743. $html .= "
  3744. </td></tr>
  3745. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-title border-top border-left border-right\">Equipamiento relacionado</td></tr>
  3746. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-content border-left border-right\">" . $order->OTPR_EQIN . "</td></tr>
  3747. <tr>
  3748. <td width=\"50%\" colspan=\"3\" class=\"cell-title border-top border-left border-right\">Fecha y hora de inicio</td>
  3749. <td width=\"50%\" colspan=\"3\" class=\"cell-title border-top border-right\">Fecha y hora final</td>
  3750. </tr>
  3751. <tr>
  3752. <td width=\"50%\" colspan=\"3\" class=\"cell-content border-left border-right\">$startDate</td>
  3753. <td width=\"50%\" colspan=\"3\" class=\"cell-content border-right\">$endDate</td>
  3754. </tr>
  3755. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-title border-top border-left border-right\">Análisis presupuestario</td></tr>
  3756. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-content border-left border-right\">
  3757. ";
  3758. $analisis = $order->OTPR_SEAN == null ? 'Sin configuración' : json_decode($order->OTPR_SEAN, true);
  3759. if (gettype($analisis) == 'array') {
  3760. //PENDIENTE
  3761. $teamsConf = $analisis['teamsConf'];
  3762. foreach ($teamsConf as $key => $team) {
  3763. /*var_dump($key);
  3764. echo "<br>";*/
  3765. }
  3766. $html .= "
  3767. PENDIENTE
  3768. </td></tr>
  3769. ";
  3770. } else {
  3771. $html .= "
  3772. $analisis
  3773. </td></tr>
  3774. ";
  3775. }
  3776. $html .= "
  3777. <tr>
  3778. <td width=\"33%\" colspan=\"2\" class=\"cell-title border-top border-left border-right\">Clasificación</td>
  3779. <td width=\"34%\" colspan=\"2\" class=\"cell-title border-top border-right\">Duración total estimada</td>
  3780. <td width=\"33%\" colspan=\"2\" class=\"cell-title border-top border-right\">Tiempo de inmovilización estimado</td>
  3781. </tr>
  3782. <tr>
  3783. <td width=\"33%\" colspan=\"2\" class=\"cell-content border-left border-right\">" . $order->OTPR_CLAS . "</td>
  3784. <td width=\"34%\" colspan=\"2\" class=\"cell-content border-right\">" . $order->OTPR_DTIN . " hora(s)</td>
  3785. <td width=\"33%\" colspan=\"2\" class=\"cell-content border-right\">" . $order->OTPR_TIIN . " hora(s)</td>
  3786. </tr>
  3787. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-title border-top border-left border-right\">Personal involucrado</td></tr>
  3788. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-content border-left border-right\">PENDIENTE</td></tr>
  3789. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-title border-top border-left border-right\">Recursos requerido</td></tr>
  3790. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-content border-left border-right\">PENDIENTE</td></tr>
  3791. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-title border-top border-left border-right\">Documentos requeridos</td></tr>
  3792. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-content border-left border-right\">PENDIENTE</td></tr>
  3793. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-title border-top border-left border-right\">Contratos requeridos</td></tr>
  3794. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-content border-left border-right\">PENDIENTE</td></tr>
  3795. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-title border-top border-left border-right\">Información del activador</td></tr>
  3796. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-content border-left border-right\">PENDIENTE</td></tr>
  3797. <tr>
  3798. <td width=\"50%\" colspan=\"3\" class=\"cell-title border-top border-left border-right\">Usuario que registró</td>
  3799. <td width=\"50%\" colspan=\"3\" class=\"cell-title border-top border-right\">Fecha de registro</td>
  3800. </tr>
  3801. <tr>
  3802. <td width=\"50%\" colspan=\"3\" class=\"cell-content border-left border-right\">PENDIENTE</td>
  3803. <td width=\"50%\" colspan=\"3\" class=\"cell-content border-right\">PENDIENTE</td>
  3804. </tr>
  3805. <tr>
  3806. <td width=\"50%\" colspan=\"3\" class=\"cell-title border-top border-left border-right\">Usuario de la última modificación</td>
  3807. <td width=\"50%\" colspan=\"3\" class=\"cell-title border-top border-right\">Fecha de la última modificación</td>
  3808. </tr>
  3809. <tr>
  3810. <td width=\"50%\" colspan=\"3\" class=\"cell-content border-left border-right border-bottom left-bottom-corner-radius\">PENDIENTE</td>
  3811. <td width=\"50%\" colspan=\"3\" class=\"cell-content border-right border-bottom right-bottom-corner-radius\">PENDIENTE</td>
  3812. </tr>
  3813. </table>
  3814. </body>
  3815. </html>
  3816. ";
  3817. $filePath = $this->functionsController->getBasePath() . '\public_files\\';
  3818. $noar = "detalles_orden_$idOrder";
  3819. $exte = "pdf";
  3820. $line = intval($line);
  3821. $line = $line < 10 ? "0$line" : "$line";
  3822. $como = "GMPR";
  3823. $cldo = "OR";
  3824. $now = $this->functionsController->now();
  3825. $nowStr = $now->toDateTimeString();
  3826. $nowArr = explode(" ", $nowStr);
  3827. $dateArr = explode("-", $nowArr[0]);
  3828. $year = substr($dateArr[0], 2);
  3829. $fecr = $year . $dateArr[1] . $dateArr[2];
  3830. $sec = DB::table('S002V01TAFAL')->where([
  3831. ['AFAL_NULI', '=', $line],
  3832. ['AFAL_COMO', '=', $como],
  3833. ['AFAL_CLDO', '=', $cldo],
  3834. ])->orderBy('AFAL_NUSE', 'desc')->first();
  3835. $nuse = is_null($sec) ? "1" : "" . intval($sec->AFAL_NUSE) + 1 . "";
  3836. for ($i = strlen($nuse); $i < 6; $i++) {
  3837. $nuse = "0$nuse";
  3838. }
  3839. $ver = DB::table('S002V01TAFAL')->where([
  3840. ['AFAL_NULI', '=', $line],
  3841. ['AFAL_COMO', '=', $como],
  3842. ['AFAL_CLDO', '=', $cldo],
  3843. ['AFAL_NOAR', '=', $noar],
  3844. ['AFAL_EXTE', '=', $exte],
  3845. ])->orderBy('AFAL_NUVE', 'desc')->first();
  3846. $nuve = is_null($ver) ? "1" : "" . intval($ver->AFAL_NUVE) + 1 . "";
  3847. for ($i = strlen($nuve); $i < 2; $i++) {
  3848. $nuve = "0$nuve";
  3849. }
  3850. $fileName = "$line-$como-$cldo-$fecr-$nuse=$nuve=$noar.$exte";
  3851. $dompdf = new Dompdf();
  3852. $dompdf->loadHtml($html);
  3853. $dompdf->setPaper('A4', 'portrait');
  3854. $dompdf->render();
  3855. $output = $dompdf->output();
  3856. $tempFile = $filePath . $fileName;
  3857. if (!file_exists($tempFile)) {
  3858. fopen($tempFile, 'w');
  3859. }
  3860. file_put_contents($tempFile, $output);
  3861. $ubic = Storage::putFile('files', new File($tempFile));
  3862. $ubic = str_replace("/", "\\", $ubic);
  3863. $ubic = $this->functionsController->getBasePath() . "\storage\app\\" . $ubic;
  3864. $tama = filesize($ubic);
  3865. $usac = json_encode([$idUser]);
  3866. unlink($tempFile);
  3867. DB::table('S002V01TAFAL')->insert([
  3868. 'AFAL_NULI' => $line,
  3869. 'AFAL_COMO' => $como,
  3870. 'AFAL_CLDO' => $cldo,
  3871. 'AFAL_FECR' => $fecr,
  3872. 'AFAL_NUSE' => $nuse,
  3873. 'AFAL_NUVE' => $nuve,
  3874. 'AFAL_NOAR' => $noar,
  3875. 'AFAL_EXTE' => $exte,
  3876. 'AFAL_TAMA' => $tama,
  3877. 'AFAL_UBIC' => $ubic,
  3878. 'AFAL_USAC' => $usac,
  3879. 'AFAL_USRE' => $idUser,
  3880. 'AFAL_FERE' => $nowStr,
  3881. ]);
  3882. $filesArr = json_decode($order->OTPR_DONE, true);
  3883. $filesArr[] = $fileName;
  3884. $filesStr = json_encode($filesArr);
  3885. DB::table('S002V01TOTPR')->where([
  3886. ['OTPR_NULI', '=', $line],
  3887. ['OTPR_IDOT', '=', $idOrder]
  3888. ])->update([
  3889. 'OTPR_DONE' => $filesStr,
  3890. 'OTPR_USMO' => $idUser,
  3891. 'OTPR_FEMO' => $nowStr
  3892. ]);
  3893. $actions = DB::getQueryLog();
  3894. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3895. $idac = $this->functionsController->registerActivity(
  3896. $line,
  3897. 'S002V01M10GMPR',
  3898. 'S002V01F06SPMA',
  3899. 'S002V01P02VSPM',
  3900. 'Registro',
  3901. "El usuario $name (" . $usr->USUA_IDUS . ") generó el archivo de simulación de la orden #$idOrder.",
  3902. $idUser,
  3903. $nowStr,
  3904. 'S002V01S02AOTR'
  3905. );
  3906. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  3907. return $this->responseController->makeResponse(false, 'EXITO.', ['fileID' => $fileName]);
  3908. }
  3909. public function extractMaintenancePlan($idOrder, $idUser, $line)
  3910. {
  3911. DB::enableQueryLog();
  3912. $idUser = $this->encryptionController->decrypt($idUser);
  3913. if (!$idUser) {
  3914. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  3915. }
  3916. $usr = DB::table('S002V01TUSUA')->where([
  3917. ['USUA_NULI', '=', $line],
  3918. ['USUA_IDUS', '=', $idUser],
  3919. ])->first();
  3920. if (is_null($usr)) {
  3921. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  3922. }
  3923. $idOrder = $this->encryptionController->decrypt($idOrder);
  3924. if (!$idOrder) {
  3925. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  3926. }
  3927. $order = DB::table('S002V01TOTPR')->join(
  3928. 'S002V01TACTI',
  3929. 'OTPR_ACAS',
  3930. '=',
  3931. 'ACTI_IDAC',
  3932. )->where([
  3933. ['OTPR_NULI', '=', $line],
  3934. ['OTPR_IDOT', '=', $idOrder]
  3935. ])->first();
  3936. if (is_null($order)) {
  3937. return $this->responseController->makeResponse(true, 'La orden solicitada no está registrada.', [], 404);
  3938. }
  3939. $html = file_get_contents($this->templatesUbic . "01-GMPR-PL-010101-000003=01=PDF_PLAN_MANTENIMIENTO.html");
  3940. $logo = file_get_contents($this->functionsController->getBasePath() . "\storage\app\public\global_resources\sam-short-logo.png");
  3941. $logoStr = "data:image/svg+xml;base64," . base64_encode($logo);
  3942. $html = str_replace("%stcImage%", $logoStr, $html);
  3943. $now = $this->functionsController->now();
  3944. $nowStr = $now->toDateTimeString();
  3945. $currentDate = $this->functionsController->buildHumanCurrentDate($nowStr);
  3946. $html = str_replace("%currentDate%", $currentDate, $html);
  3947. $html = str_replace("%idOrder%", $order->OTPR_IDOT, $html);
  3948. $startDateTimeArr = explode(" ", $order->OTPR_FIAP);
  3949. $startTimeArr = explode(":", $startDateTimeArr[1]);
  3950. $startHour = intval($startTimeArr[0]);
  3951. $startPeriod = $startHour < 12 ? 'AM' : 'PM';
  3952. $startHour = $startHour > 12 ? $startHour - 12 : $startHour;
  3953. $startTimeStr = "$startHour:$startTimeArr[1] $startPeriod";
  3954. $html = str_replace("%startTime%", $startTimeStr, $html);
  3955. $startDateTimeObj = new Carbon($order->OTPR_FIAP);
  3956. $totalDuration = floatval($order->OTPR_DTIN);
  3957. $totalDurationHours = intval($totalDuration);
  3958. $startDateTimeObj->addHours($totalDuration);
  3959. $totalDurationFloatMinutes = $this->functionsController->floatSub($totalDuration, $totalDurationHours);
  3960. $totalDurationFloatMinutes = $this->functionsController->floatMul($totalDurationFloatMinutes, 60);
  3961. $totalDurationMinutes = ceil($totalDurationFloatMinutes);
  3962. $startDateTimeObj->addMinutes($totalDurationMinutes);
  3963. $endDateTimeArr = explode(" ", $startDateTimeObj->toDateTimeString());
  3964. $endTimeArr = explode(":", $endDateTimeArr[1]);
  3965. $endHour = intval($endTimeArr[0]);
  3966. $endPeriod = $startHour < 12 ? 'AM' : 'PM';
  3967. $endHour = $endHour > 12 ? $endHour - 12 : $endHour;
  3968. $endTimeStr = "$endHour:$endTimeArr[1] $endPeriod";
  3969. $html = str_replace("%endTime%", $endTimeStr, $html);
  3970. $html = str_replace("%inmTime%", $order->OTPR_TIIN . " h", $html);
  3971. $html = str_replace("%totalTime%", $order->OTPR_DTIN . " h", $html);
  3972. $html = str_replace("%equipment%", $order->OTPR_EQIN, $html);
  3973. $html = str_replace("%description%", $order->OTPR_DEIN, $html);
  3974. $instructions = json_decode($order->OTPR_ININ, true);
  3975. $activitiesTableBody = "";
  3976. foreach ($instructions as $k => $v) {
  3977. $key = $k + 1;
  3978. $activitiesTableBody .= "<tr>";
  3979. $activitiesTableBody .= "<td>$key</td>";
  3980. $activitiesTableBody .= "<td>$v[INSTRUCCION]</td>";
  3981. $activitiesTableBody .= "<td>";
  3982. /*foreach($v['ESPECIALIDADES'] as $v0){
  3983. /*$team = array_filter($teams, function($v1) use ($v0) {
  3984. return ($v1['ID'] == $v0);
  3985. });
  3986. $activitiesTableBody .= /*end($team)['ESP'] '--' . "<br>";
  3987. }*/
  3988. $activitiesTableBody .= "</td>";
  3989. $activitiesTableBody .= "</tr>";
  3990. }
  3991. $html = str_replace("%activitiesTableBody%", $activitiesTableBody, $html);
  3992. $personalArr = json_decode($order->OTPR_OPPR, true);
  3993. $constConf = json_decode($order->OTPR_SEAN, true);
  3994. $cont = 1;
  3995. $specialtiesTableBody = "";
  3996. foreach ($personalArr as $k => $v) {
  3997. /*$team = array_filter($teams, function($v1) use ($k) {
  3998. return ($v1['ID'] == $k);
  3999. });*/
  4000. $specialtiesTableBody .= "<tr>";
  4001. $specialtiesTableBody .= "<td>$cont</td>";
  4002. $specialtiesTableBody .= "<td>" . /*end($team)['ESP']*/ '--' . "</td>";
  4003. $specialtiesTableBody .= "<td>";
  4004. foreach ($constConf['teamsConf'][$k] as $v0) {
  4005. $usrID = '-'; //$v0['USER'];
  4006. $usrInv = DB::table('S002V01TUSUA')->where([
  4007. ['USUA_NULI', '=', $line],
  4008. ['USUA_IDUS', '=', $usrID],
  4009. ])->first();
  4010. $invName = /*$this->functionsController->joinName($usrInv->USUA_NOMB, $usrInv->USUA_APPA, $usrInv->USUA_APMA) . " ($usrID)"*/ '-';
  4011. $specialtiesTableBody .= $invName . "<br>";
  4012. }
  4013. $specialtiesTableBody .= "</td>";
  4014. $specialtiesTableBody .= "</tr>";
  4015. $cont++;
  4016. }
  4017. $html = str_replace("%specialtiesTableBody%", $specialtiesTableBody, $html);
  4018. $como = 'GMPR';
  4019. $cldo = 'OR';
  4020. $noar = "plan_de_mantenimiento_preventivo_orden_$idOrder";
  4021. $exte = "pdf";
  4022. $line = $line < 10 ? "0$line" : "$line";
  4023. $nowArr = explode(" ", $nowStr);
  4024. $dateArr = explode("-", $nowArr[0]);
  4025. $year = substr($dateArr[0], 2);
  4026. $fecr = $year . $dateArr[1] . $dateArr[2];
  4027. $sec = DB::table('S002V01TAFAL')->where([
  4028. ['AFAL_NULI', '=', $line],
  4029. ['AFAL_COMO', '=', $como],
  4030. ['AFAL_CLDO', '=', $cldo],
  4031. ])->orderBy('AFAL_NUSE', 'desc')->first();
  4032. $nuse = is_null($sec) ? "1" : "" . intval($sec->AFAL_NUSE) + 1 . "";
  4033. for ($i = strlen($nuse); $i < 6; $i++) {
  4034. $nuse = "0$nuse";
  4035. }
  4036. $ver = DB::table('S002V01TAFAL')->where([
  4037. ['AFAL_NULI', '=', $line],
  4038. ['AFAL_COMO', '=', $como],
  4039. ['AFAL_CLDO', '=', $cldo],
  4040. ['AFAL_NOAR', '=', $noar],
  4041. ['AFAL_EXTE', '=', $exte],
  4042. ])->orderBy('AFAL_NUVE', 'desc')->first();
  4043. $nuve = is_null($ver) ? "1" : "" . intval($ver->AFAL_NUVE) + 1 . "";
  4044. for ($i = strlen($nuve); $i < 2; $i++) {
  4045. $nuve = "0$nuve";
  4046. }
  4047. $filePath = $this->functionsController->getBasePath() . '\public_files\\';
  4048. $fileName = "$line-$como-$cldo-$fecr-$nuse=$nuve=$noar.$exte";
  4049. $dompdf = new Dompdf();
  4050. $dompdf->loadHtml($html);
  4051. $dompdf->setPaper('A4', 'portrait');
  4052. $dompdf->render();
  4053. $output = $dompdf->output();
  4054. $tempFile = $filePath . $fileName;
  4055. if (!file_exists($tempFile)) {
  4056. fopen($tempFile, 'w');
  4057. }
  4058. file_put_contents($tempFile, $output);
  4059. $ubic = Storage::putFile('files', new File($tempFile));
  4060. $ubic = str_replace("/", "\\", $ubic);
  4061. $ubic = $this->functionsController->getBasePath() . "\storage\app\\" . $ubic;
  4062. $tama = filesize($ubic);
  4063. $usac = json_encode([$idUser]);
  4064. unlink($tempFile);
  4065. DB::table('S002V01TAFAL')->insert([
  4066. 'AFAL_NULI' => $line,
  4067. 'AFAL_COMO' => $como,
  4068. 'AFAL_CLDO' => $cldo,
  4069. 'AFAL_FECR' => $fecr,
  4070. 'AFAL_NUSE' => $nuse,
  4071. 'AFAL_NUVE' => $nuve,
  4072. 'AFAL_NOAR' => $noar,
  4073. 'AFAL_EXTE' => $exte,
  4074. 'AFAL_TAMA' => $tama,
  4075. 'AFAL_UBIC' => $ubic,
  4076. 'AFAL_USAC' => $usac,
  4077. 'AFAL_USRE' => $idUser,
  4078. 'AFAL_FERE' => $nowStr,
  4079. ]);
  4080. $filesArr = json_decode($order->OTPR_DONE, true);
  4081. $filesArr[] = $fileName;
  4082. $filesStr = json_encode($filesArr);
  4083. DB::table('S002V01TOTPR')->where([
  4084. ['OTPR_NULI', '=', $line],
  4085. ['OTPR_IDOT', '=', $idOrder]
  4086. ])->update([
  4087. 'OTPR_DONE' => $filesStr,
  4088. 'OTPR_USMO' => $idUser,
  4089. 'OTPR_FEMO' => $nowStr
  4090. ]);
  4091. $actions = DB::getQueryLog();
  4092. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4093. $idac = $this->functionsController->registerActivity(
  4094. $line,
  4095. 'S002V01M10GMPR',
  4096. 'S002V01F07EPMA',
  4097. 'S002V01P02DPMA',
  4098. 'Registro',
  4099. "El usuario $name (" . $usr->USUA_IDUS . ") generó el archivo de plan de mantenimiento de la orden #$idOrder.",
  4100. $idUser,
  4101. $nowStr,
  4102. 'S002V01S02AOTR'
  4103. );
  4104. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4105. return $this->responseController->makeResponse(false, 'EXITO.', ['fileID' => $fileName]);
  4106. }
  4107. public function getMaintenancePlanAnalysis($idFile, $idUser, $line)
  4108. {
  4109. DB::enableQueryLog();
  4110. $idUser = $this->encryptionController->decrypt($idUser);
  4111. if (!$idUser) {
  4112. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  4113. }
  4114. $usr = DB::table('S002V01TUSUA')->where([
  4115. ['USUA_IDUS', '=', $idUser],
  4116. ['USUA_NULI', '=', $line]
  4117. ])->first();
  4118. if (is_null($usr)) {
  4119. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  4120. }
  4121. $idFile = $this->encryptionController->decrypt($idFile);
  4122. if (!$idFile) {
  4123. return $this->responseController->makeResponse(true, 'El ID del archivo solicitado no está encriptado correctamente.', [], 400);
  4124. }
  4125. $tempFile = DB::table('S002V01TARTE')->where([
  4126. ['ARTE_NULI', '=', $line],
  4127. ['ARTE_IDAR', '=', $idFile]
  4128. ])->first();
  4129. if (is_null($tempFile)) {
  4130. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  4131. }
  4132. $fileColumnsEsp = [
  4133. 'Id',
  4134. 'Activo',
  4135. 'Modo de tarea',
  4136. 'Nombre',
  4137. 'Duración',
  4138. 'Comienzo',
  4139. 'Fin',
  4140. 'Trabajo_Programado',
  4141. 'Costo',
  4142. 'Duración de línea base estimada',
  4143. 'Comienzo previsto',
  4144. 'Fin de línea base',
  4145. 'Trabajo previsto',
  4146. 'Costo de línea base',
  4147. 'Variación de duración',
  4148. 'Variación de trabajo',
  4149. 'Variación de costo'
  4150. ];
  4151. $fileColumnsIng = ['ID', 'Active', 'Task Mode', 'Name', 'Duration', 'Start', 'Finish', 'Predecessors', 'Outline Level', 'Notes'];
  4152. $MONTHS_ESP = [
  4153. 'enero' => '01',
  4154. 'febrero' => '02',
  4155. 'marzo' => '03',
  4156. 'abril' => '04',
  4157. 'mayo' => '05',
  4158. 'junio' => '06',
  4159. 'julio' => '07',
  4160. 'agosto' => '08',
  4161. 'septiembre' => '09',
  4162. 'octubre' => '10',
  4163. 'noviembre' => '11',
  4164. 'diciembre' => '12'
  4165. ];
  4166. $MONTHS_ING = [
  4167. '1' => '01',
  4168. '2' => '02',
  4169. '3' => '03',
  4170. '4' => '04',
  4171. '5' => '05',
  4172. '6' => '06',
  4173. '7' => '07',
  4174. '8' => '08',
  4175. '9' => '09',
  4176. '10' => '10',
  4177. '11' => '11',
  4178. '12' => '12'
  4179. ];
  4180. $spreadsheet = IOFactory::load($tempFile->ARTE_UBTE);
  4181. $workSheet = $spreadsheet->getActiveSheet();
  4182. $maxColStr = $workSheet->getHighestColumn();
  4183. $maxCol = Coordinate::columnIndexFromString($maxColStr);
  4184. for ($i = 1; $i <= $maxCol; $i++) {
  4185. $colStr = Coordinate::stringFromColumnIndex($i);
  4186. $cell = $workSheet->getCell($colStr . "1");
  4187. $val = $cell->getValue();
  4188. if (!in_array($val, $fileColumnsEsp) && !in_array($val, $fileColumnsIng)) {
  4189. return $this->responseController->makeResponse(true, 'El archivo tiene un formato inválido.', [], 400);
  4190. }
  4191. }
  4192. $instructions = [];
  4193. $maxRow = $workSheet->getHighestRow();
  4194. for ($row = 2; $row <= $maxRow; $row++) {
  4195. $activo = $workSheet->getCell("B$row")->getValue();
  4196. $language = $activo == 'Yes' ? 'I' : 'E';
  4197. if ($activo == 'Sí' || $activo == 'Si' || $activo == 'Yes') {
  4198. $durationStr = $workSheet->getCell("E$row")->getValue();
  4199. $durationArr = explode(" ", $durationStr);
  4200. $duration = 0;
  4201. if ($durationArr[1] == 'días' || $durationArr[1] == 'dias' || $durationArr[1] == 'día' || $durationArr[1] == 'dia' || $durationArr[1] == 'days' || $durationArr[1] == 'day') {
  4202. $daysFloat = floatval($durationArr[0]);
  4203. $daysInt = intval($daysFloat);
  4204. $duration += $daysInt * 24;
  4205. $hoursDec = $this->functionsController->floatSub($daysFloat, $daysInt);
  4206. $hoursFloat = $this->functionsController->floatMul($hoursDec, 24);
  4207. $duration = $this->functionsController->floatAdd($duration, $hoursFloat);
  4208. }
  4209. if ($durationArr[1] == 'mins' || $durationArr[1] == 'min') {
  4210. $minsFloat = floatval($durationArr[0]);
  4211. $hoursFloat = $this->functionsController->floatDiv($minsFloat, 60);
  4212. $duration = $this->functionsController->floatAdd($duration, $hoursFloat);
  4213. }
  4214. $duration = round($duration, 5);
  4215. $startDateStr = $workSheet->getCell("F$row")->getValue();
  4216. $startDateArr = explode(" ", $startDateStr);
  4217. if ($language == 'I') {
  4218. $americanDateArr = explode('/', $startDateArr[1]);
  4219. $startMonth = $MONTHS_ING[$americanDateArr[0]];
  4220. } else {
  4221. $startMonth = $MONTHS_ESP[$startDateArr[1]];
  4222. $startPeriod = $startDateArr[4] . $startDateArr[5];
  4223. $startPeriod = str_replace('.', '', $startPeriod);
  4224. $startHourArr = explode(":", $startDateArr[3]);
  4225. $startHour = intval($startHourArr[0]);
  4226. $startHour = $startPeriod == 'pm' && $startHour < 12 ? $startHour + 12 : $startHour;
  4227. $startHourStr = $startHour < 10 ? "0$startHour" : "$startHour";
  4228. $startDateTime = "$startDateArr[2]-$startMonth-$startDateArr[0] $startHourStr:$startHourArr[1]:00";
  4229. $startDateTimeObj = new Carbon($startDateTime);
  4230. $startTimestamp = $startDateTimeObj->timestamp;
  4231. $endDateStr = $workSheet->getCell("G$row")->getValue();
  4232. $endDateArr = explode(" ", $endDateStr);
  4233. $endMonth = $MONTHS_ESP[$endDateArr[1]];
  4234. $endPeriod = $endDateArr[4] . $endDateArr[5];
  4235. $endPeriod = str_replace('.', '', $endPeriod);
  4236. $endHourArr = explode(":", $endDateArr[3]);
  4237. $endHour = intval($endHourArr[0]);
  4238. $endHour = $endPeriod == 'pm' && $endHour < 12 ? $endHour + 12 : $endHour;
  4239. $endHourStr = $endHour < 10 ? "0$endHour" : "$endHour";
  4240. $endDateTime = "$endDateArr[2]-$endMonth-$endDateArr[0] $endHourStr:$endHourArr[1]:00";
  4241. $endDateTimeObj = new Carbon($endDateTime);
  4242. $endTimestamp = $endDateTimeObj->timestamp;
  4243. }
  4244. $difference = $endTimestamp - $startTimestamp;
  4245. $minutes = $this->functionsController->floatDiv($difference, 60);
  4246. $hours = $this->functionsController->floatDiv($minutes, 60);
  4247. $duration = round($hours, 5);
  4248. $instructions[] = [
  4249. "INSTRUCTION" => $workSheet->getCell("D$row")->getValue(),
  4250. "DURATION" => $duration,
  4251. "START" => $startDateTime,
  4252. "END" => $endDateTime,
  4253. "COST" => $workSheet->getCell("I$row")->getValue(),
  4254. ];
  4255. }
  4256. }
  4257. $now = $this->functionsController->now();
  4258. $nowStr = $now->toDateTimeString();
  4259. $actions = DB::getQueryLog();
  4260. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4261. $idac = $this->functionsController->registerActivity(
  4262. $line,
  4263. 'S002V01M10GMPR',
  4264. 'S002V01F10DIBI',
  4265. '-',
  4266. 'Consulta',
  4267. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el plan de mantenimiento para una nueva orden desde MS Project.",
  4268. $idUser,
  4269. $nowStr,
  4270. 'S002V01S02AOTR'
  4271. );
  4272. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4273. return $this->responseController->makeResponse(false, 'EXITO.', $instructions);
  4274. }
  4275. public function getFileToMSProject($idOrder, $idUser, $line)
  4276. {
  4277. DB::enableQueryLog();
  4278. $idUser = $this->encryptionController->decrypt($idUser);
  4279. if (!$idUser) {
  4280. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  4281. }
  4282. $usr = DB::table('S002V01TUSUA')->where([
  4283. ['USUA_NULI', '=', $line],
  4284. ['USUA_IDUS', '=', $idUser],
  4285. ])->first();
  4286. if (is_null($usr)) {
  4287. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  4288. }
  4289. $idOrder = $this->encryptionController->decrypt($idOrder);
  4290. if (!$idOrder) {
  4291. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  4292. }
  4293. $order = DB::table('S002V01TOTPR')->where([
  4294. ['OTPR_IDOT', '=', $idOrder],
  4295. ['OTPR_NULI', '=', $line]
  4296. ])->first();
  4297. if (is_null($order)) {
  4298. return $this->responseController->makeResponse(true, 'La orden consultada no está registrada.', [], 404);
  4299. }
  4300. $cols = [
  4301. 'Id',
  4302. 'Activo',
  4303. 'Modo de tarea',
  4304. 'Nombre',
  4305. 'Duración',
  4306. 'Comienzo',
  4307. 'Fin',
  4308. 'Trabajo_Programado',
  4309. 'Costo',
  4310. 'Duración de línea base estimada',
  4311. 'Comienzo previsto',
  4312. 'Fin de línea base',
  4313. 'Trabajo previsto',
  4314. 'Costo de línea base',
  4315. 'Variación de duración',
  4316. 'Variación de trabajo',
  4317. 'Variación de costo'
  4318. ];
  4319. $analysis = json_decode($order->OTPR_SEAN, true);
  4320. $instructionsConf = /*$analysis['instructionsConf']*/ '{}';
  4321. $instructions = json_decode($order->OTPR_ININ, true);
  4322. $arrCols = [];
  4323. $cont = 1;
  4324. foreach ($instructions as $instruction) {
  4325. $projectDuration = "";
  4326. if ($instruction['DURACION'] < 24) {
  4327. $duration = $instruction['DURACION'] * 60;
  4328. $projectDuration = "$duration min";
  4329. if ($duration > 1) {
  4330. $projectDuration .= "s";
  4331. }
  4332. } else {
  4333. $duration = $this->functionsController->floatDiv($instruction['DURACION'], 24);
  4334. $duration = round($duration, 2);
  4335. $projectDuration = "$duration día";
  4336. if ($duration > 1) {
  4337. $projectDuration .= "s";
  4338. }
  4339. }
  4340. $instructionStartDateTimeArr = explode(' ', $instruction['INICIO']);
  4341. $instructionStartTimeArr = explode(':', $instructionStartDateTimeArr[1]);
  4342. $instructionStartHour = intval($instructionStartTimeArr[0]);
  4343. if ($instructionStartDateTimeArr[2] == 'PM' && $instructionStartHour < 12) {
  4344. $instructionStartHour = $instructionStartHour + 12;
  4345. }
  4346. $instructionStartHourStr = $instructionStartHour < 10 ? "0$instructionStartHour" : "$instructionStartHour";
  4347. $instructionStartTimeStr = "$instructionStartHourStr:$instructionStartTimeArr[1]:00";
  4348. $instructionStartDateTimeStr = "$instructionStartDateTimeArr[0] $instructionStartTimeStr";
  4349. $instructionEndDateTimeArr = explode(' ', $instruction['FIN']);
  4350. $instructionEndTimeArr = explode(':', $instructionEndDateTimeArr[1]);
  4351. $instructionEndHour = intval($instructionEndTimeArr[0]);
  4352. if ($instructionEndDateTimeArr[2] == 'PM' && $instructionEndHour < 12) {
  4353. $instructionEndHour = $instructionEndHour + 12;
  4354. }
  4355. $instructionEndHourStr = $instructionEndHour < 10 ? "0$instructionEndHour" : "$instructionEndHour";
  4356. $instructionEndTimeStr = "$instructionEndHourStr:$instructionEndTimeArr[1]:00";
  4357. $instructionEndDateTimeStr = "$instructionEndDateTimeArr[0] $instructionEndTimeStr";
  4358. $projectStartDate = $this->functionsController->buildProjectDate($instructionStartDateTimeStr);
  4359. $projectEndDate = $this->functionsController->buildProjectDate($instructionEndDateTimeStr);
  4360. $cost = /*$instruction['ISFROMFILE'] ? $instruction['COSTO'] : $instructionsConf[$instruction['ID']]*/ '0';
  4361. $arrCol = [
  4362. "A" => $cont,
  4363. "B" => 'Sí',
  4364. "C" => 'Programada manualmente',
  4365. "D" => $instruction['INSTRUCCION'],
  4366. "E" => $projectDuration,
  4367. "F" => $projectStartDate,
  4368. "G" => $projectEndDate,
  4369. "H" => "0h",
  4370. "I" => $cost,
  4371. "J" => "0d",
  4372. "K" => "NOD",
  4373. "L" => "NOD",
  4374. "M" => "0h",
  4375. "N" => "0",
  4376. "O" => $projectDuration,
  4377. "P" => "0h",
  4378. "Q" => "0",
  4379. ];
  4380. $arrCols[] = $arrCol;
  4381. $cont++;
  4382. }
  4383. $now = $this->functionsController->now();
  4384. $nowStr = $now->toDateTimeString();
  4385. $dateTimeArr = explode(" ", $nowStr);
  4386. $dateArr = explode("-", $dateTimeArr[0]);
  4387. $year = substr($dateArr[0], 2);
  4388. $como = 'GMPR';
  4389. $cldo = 'OR';
  4390. $fecr = $year . $dateArr[1] . $dateArr[2];
  4391. $sec = DB::table('S002V01TAFAL')->where([
  4392. ['AFAL_NULI', '=', $line],
  4393. ['AFAL_COMO', '=', $como],
  4394. ['AFAL_CLDO', '=', $cldo],
  4395. ])->orderBy('AFAL_NUSE', 'desc')->first();
  4396. $nuse = "";
  4397. if (is_null($sec)) {
  4398. $nuse = '000001';
  4399. } else {
  4400. $secu = "" . intval($sec->AFAL_NUSE) + 1 . "";
  4401. $nuse = "";
  4402. for ($i = strlen($secu); $i < 6; $i++) {
  4403. $nuse .= "0";
  4404. }
  4405. $nuse = $nuse . $secu;
  4406. }
  4407. $noar = "plan_de_mantenimiento_preventivo_ms_project_orden_$idOrder";
  4408. $exte = "xlsx";
  4409. $ver = DB::table('S002V01TAFAL')->where([
  4410. ['AFAL_NULI', '=', $line],
  4411. ['AFAL_COMO', '=', $como],
  4412. ['AFAL_CLDO', '=', $cldo],
  4413. ['AFAL_NOAR', '=', $noar],
  4414. ['AFAL_EXTE', '=', $exte],
  4415. ])->orderBy('AFAL_NUVE', 'desc')->first();
  4416. $nuve = "";
  4417. if (is_null($ver)) {
  4418. $nuve = "01";
  4419. } else {
  4420. $vers = intval($ver->AFAL_NUVE) + 1;
  4421. $nuve = $vers < 10 ? "0$vers" : "$vers";
  4422. }
  4423. $line = $line < 10 ? "0$line" : "$line";
  4424. $filePath = $this->functionsController->getBasePath() . '\public_files\\';
  4425. $fileName = "$line-$como-$cldo-$fecr-$nuse=$nuve=$noar.$exte";
  4426. $tempFile = $filePath . $fileName;
  4427. if (file_exists($tempFile)) {
  4428. unlink($tempFile);
  4429. }
  4430. $spreadsheet = new Spreadsheet();
  4431. $workSheet = $spreadsheet->getActiveSheet();
  4432. $colInd = 1;
  4433. foreach ($cols as $colName) {
  4434. $colStr = Coordinate::stringFromColumnIndex($colInd);
  4435. $workSheet->setCellValue($colStr . "1", $colName);
  4436. $workSheet->getColumnDimension($colStr)->setAutoSize(true);
  4437. $colInd++;
  4438. }
  4439. $row = 2;
  4440. foreach ($arrCols as $col) {
  4441. foreach ($col as $k => $v) {
  4442. $workSheet->setCellValue($k . $row, $v);
  4443. }
  4444. $row++;
  4445. }
  4446. $writer = new Xlsx($spreadsheet);
  4447. $writer->save($tempFile);
  4448. $ubic = Storage::putFile('files', new File($tempFile));
  4449. $ubic = str_replace("/", "\\", $ubic);
  4450. $ubic = $this->functionsController->getBasePath() . "\storage\app\\" . $ubic;
  4451. $tama = filesize($ubic);
  4452. $usac = json_encode([$idUser]);
  4453. unlink($tempFile);
  4454. DB::table('S002V01TAFAL')->insert([
  4455. 'AFAL_NULI' => $line,
  4456. 'AFAL_COMO' => $como,
  4457. 'AFAL_CLDO' => $cldo,
  4458. 'AFAL_FECR' => $fecr,
  4459. 'AFAL_NUSE' => $nuse,
  4460. 'AFAL_NUVE' => $nuve,
  4461. 'AFAL_NOAR' => $noar,
  4462. 'AFAL_EXTE' => $exte,
  4463. 'AFAL_TAMA' => $tama,
  4464. 'AFAL_UBIC' => $ubic,
  4465. 'AFAL_USAC' => $usac,
  4466. 'AFAL_USRE' => $idUser,
  4467. 'AFAL_FERE' => $nowStr,
  4468. ]);
  4469. $filesArr = json_decode($order->OTPR_DONE, true);
  4470. $filesArr[] = $fileName;
  4471. $filesStr = json_encode($filesArr);
  4472. DB::table('S002V01TOTPR')->where([
  4473. ['OTPR_NULI', '=', $line],
  4474. ['OTPR_IDOT', '=', $idOrder]
  4475. ])->update([
  4476. 'OTPR_DONE' => $filesStr,
  4477. 'OTPR_USMO' => $idUser,
  4478. 'OTPR_FEMO' => $nowStr
  4479. ]);
  4480. $actions = DB::getQueryLog();
  4481. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4482. $idac = $this->functionsController->registerActivity(
  4483. $line,
  4484. 'S002V01M10GMPR',
  4485. 'S002V01F10DIBI',
  4486. '-',
  4487. 'Registro',
  4488. "El usuario $name (" . $usr->USUA_IDUS . ") generó el archivo de plan de mantenimiento para MS Project de la orden #$idOrder.",
  4489. $idUser,
  4490. $nowStr,
  4491. 'S002V01S02AOTR'
  4492. );
  4493. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4494. return $this->responseController->makeResponse(false, 'EXITO.', ['fileID' => $fileName]);
  4495. }
  4496. public function getOrderStaff($idOrder, $idUser, $line)
  4497. {
  4498. DB::enableQueryLog();
  4499. $idUser = $this->encryptionController->decrypt($idUser);
  4500. if (!$idUser) {
  4501. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  4502. }
  4503. $usr = DB::table('S002V01TUSUA')->where([
  4504. ['USUA_NULI', '=', $line],
  4505. ['USUA_IDUS', '=', $idUser],
  4506. ])->first();
  4507. if (is_null($usr)) {
  4508. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  4509. }
  4510. $idOrder = $this->encryptionController->decrypt($idOrder);
  4511. if (!$idOrder) {
  4512. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  4513. }
  4514. $order = DB::table('S002V01TOTPR')->where([
  4515. ['OTPR_IDOT', '=', $idOrder],
  4516. ['OTPR_NULI', '=', $line]
  4517. ])->first();
  4518. if (is_null($order)) {
  4519. return $this->responseController->makeResponse(true, 'La orden consultada no está registrada.', [], 404);
  4520. }
  4521. $staffArr = json_decode($order->OTPR_OPPR, true);
  4522. $staffTmp = [];
  4523. foreach ($staffArr as $val) {
  4524. if ($val['TYPE'] == 'EQ') {
  4525. $employees = DB::table('S002V01TPERS')->where([
  4526. ['PERS_NULI', '=', $line],
  4527. ['PERS_EQTR', '=', $val['ID']],
  4528. ])->get()->all();
  4529. foreach ($employees as $employee) {
  4530. if (!in_array($employee->PERS_IDPE, $staffTmp)) {
  4531. $staffTmp[] = $employee->PERS_IDPE;
  4532. }
  4533. }
  4534. } else if ($val['TYPE'] == 'SU') {
  4535. $employees = DB::table('S002V01TPERS')->where([
  4536. ['PERS_NULI', '=', $line],
  4537. ['PERS_IDPS', '=', $val['ID']],
  4538. ])->get()->all();
  4539. foreach ($employees as $employee) {
  4540. if (!in_array($employee->PERS_IDPE, $staffTmp)) {
  4541. $staffTmp[] = $employee->PERS_IDPE;
  4542. }
  4543. }
  4544. } else {
  4545. $id = intval($val['ID']);
  4546. if (!in_array($id, $staffTmp)) {
  4547. $staffTmp[] = $id;
  4548. }
  4549. }
  4550. }
  4551. $staffFn = [];
  4552. foreach ($staffTmp as $empID) {
  4553. $employee = DB::table('S002V01TPERS')->select([
  4554. 'PERS_IDPE AS IDEMPLEADO',
  4555. 'PERS_IDUS AS IDUSUARIO',
  4556. DB::raw('TRIM(CONCAT(USUA_NOMB, " " , USUA_APPA, " ", COALESCE(USUA_APMA,""))) AS NOMBREUSUARIO'),
  4557. 'PERS_TICO AS TIPOCONTRATO',
  4558. 'PERS_IDPS AS IDSUBCONTRATISTA',
  4559. 'PESU_RASO AS RAZONSOCIAL',
  4560. 'PESU_REFI AS REGIMENFISCAL',
  4561. 'PERS_EQTR AS IDEQUIPO',
  4562. 'EQMA_NOMB AS NOMBREEQUIPO'
  4563. ])->join('S002V01TUSUA', 'USUA_IDUS', '=', 'PERS_IDUS')
  4564. ->leftJoin('S002V01TPESU', 'PESU_IDPS', '=', 'PERS_IDPS')
  4565. ->leftJoin('S002V01TEQMA', 'EQMA_IDEQ', '=', 'PERS_EQTR')->where([
  4566. ['PERS_NULI', '=', $line],
  4567. ['PERS_IDPE', '=', $empID],
  4568. ])->first();
  4569. if (!is_null($employee)) {
  4570. $staffFn[] = $employee;
  4571. }
  4572. }
  4573. $now = $this->functionsController->now();
  4574. $nowStr = $now->toDateTimeString();
  4575. $actions = DB::getQueryLog();
  4576. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4577. $idac = $this->functionsController->registerActivity(
  4578. $line,
  4579. 'S002V01M10GMPR',
  4580. 'S002V01F01COTP',
  4581. 'S002V01P03COTP',
  4582. 'Consulta',
  4583. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los empleados de la orden #$idOrder.",
  4584. $idUser,
  4585. $nowStr,
  4586. 'S002V01S01ORTR'
  4587. );
  4588. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4589. return $this->responseController->makeResponse(false, 'EXITO.', $staffFn);
  4590. }
  4591. public function getOrderExecutionDetails($idExecution, $idUser, $line)
  4592. {
  4593. DB::enableQueryLog();
  4594. $idUser = $this->encryptionController->decrypt($idUser);
  4595. if (!$idUser) {
  4596. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  4597. }
  4598. $usr = DB::table('S002V01TUSUA')->where([
  4599. ['USUA_NULI', '=', $line],
  4600. ['USUA_IDUS', '=', $idUser],
  4601. ])->first();
  4602. if (is_null($usr)) {
  4603. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  4604. }
  4605. $idExecution = $this->encryptionController->decrypt($idExecution);
  4606. if (!$idExecution) {
  4607. return $this->responseController->makeResponse(true, 'El ID de la ejecución solicitada no está encriptado correctamente.', [], 400);
  4608. }
  4609. $idExecArr = explode('|', $idExecution);
  4610. if (count($idExecArr) < 4) {
  4611. return $this->responseController->makeResponse(true, 'El ID de la ejecución solicitada tiene un formato inválido.', [], 400);
  4612. }
  4613. $execution = DB::table('S002V01TBEOT')->select([
  4614. 'BEOT_IDRE AS IDREGISTRO',
  4615. 'BEOT_IDOT AS IDORDEN',
  4616. 'BEOT_FEPR AS FECHAPROGRAMACION',
  4617. 'BEOT_TIAC AS TIPOACCION',
  4618. 'BEOT_TIOR AS TIPOORDEN',
  4619. 'BEOT_DTEJ AS TIEMPOEJECUCION',
  4620. 'BEOT_OBSE AS OBSERVACIONES',
  4621. 'BEOT_FEEJ AS FECHAEJECUCION',
  4622. 'BEOT_USEJ AS USUARIOEJECUCION',
  4623. 'BEOT_FEFI AS FECHAFINALIZACION',
  4624. 'BEOT_USFI AS USUARIOFINALIZO'
  4625. ])->where([
  4626. ['BEOT_IDRE', '=', $idExecArr[0]],
  4627. ['BEOT_NULI', '=', $line],
  4628. ['BEOT_IDOT', '=', $idExecArr[2]],
  4629. ['BEOT_FEPR', '=', $idExecArr[3]],
  4630. ])->first();
  4631. if (is_null($execution)) {
  4632. return $this->responseController->makeResponse(true, 'El registro de la ejecución solicitada no existe.', [], 404);
  4633. }
  4634. $execution->IDREGISTRO = $this->encryptionController->encrypt($execution->IDREGISTRO);
  4635. $execution->IDORDEN = $this->encryptionController->encrypt($execution->IDORDEN);
  4636. $execUsr = DB::table('S002V01TUSUA')->where([
  4637. ['USUA_IDUS', '=', $execution->USUARIOEJECUCION],
  4638. ['USUA_NULI', '=', $line]
  4639. ])->first();
  4640. $execName = $this->functionsController->joinName($execUsr->USUA_NOMB, $execUsr->USUA_APPA, $execUsr->USUA_APMA);
  4641. $execution->USUARIOEJECUCION = $execName . " (" . $execution->USUARIOEJECUCION . ")";
  4642. if (!is_null($execution->USUARIOFINALIZO)) {
  4643. $finUsr = DB::table('S002V01TUSUA')->where([
  4644. ['USUA_IDUS', '=', $execution->USUARIOFINALIZO],
  4645. ['USUA_NULI', '=', $line]
  4646. ])->first();
  4647. $finName = $this->functionsController->joinName($finUsr->USUA_NOMB, $finUsr->USUA_APPA, $finUsr->USUA_APMA);
  4648. $execution->USUARIOFINALIZO = $finName . " (" . $execution->USUARIOFINALIZO . ")";
  4649. }
  4650. $now = $this->functionsController->now();
  4651. $nowStr = $now->toDateTimeString();
  4652. $actions = DB::getQueryLog();
  4653. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4654. $idac = $this->functionsController->registerActivity(
  4655. $line,
  4656. 'S002V01M10GMPR',
  4657. 'S002V01F01COTP',
  4658. 'S002V01P03COTP',
  4659. 'Consulta',
  4660. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los detalles de la ejecución del a orden de trabajo preventivo #$idExecArr[2] para la fecha $idExecArr[3].",
  4661. $idUser,
  4662. $nowStr,
  4663. 'S002V01S01ORTR'
  4664. );
  4665. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4666. return $this->responseController->makeResponse(false, 'EXITO.', $execution);
  4667. }
  4668. public function getWorkOrdersByEquipment($equipmentCode, $idUser, $line)
  4669. {
  4670. DB::enableQueryLog();
  4671. $idUser = $this->encryptionController->decrypt($idUser);
  4672. if (!$idUser) {
  4673. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  4674. }
  4675. $usr = DB::table('S002V01TUSUA')->where([
  4676. ['USUA_NULI', '=', $line],
  4677. ['USUA_IDUS', '=', $idUser],
  4678. ])->first();
  4679. if (is_null($usr)) {
  4680. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  4681. }
  4682. $equipmentCode = $this->encryptionController->decrypt($equipmentCode);
  4683. if (!$equipmentCode) {
  4684. return $this->responseController->makeResponse(true, 'El código del equipamiento relacionado no está encriptado correctamente.', [], 400);
  4685. }
  4686. $equipment = DB::table('S002V01TEQUI')->where([
  4687. ['EQUI_NULI', '=', $line],
  4688. ['EQUI_COEQ', '=', $equipmentCode]
  4689. ])->first();
  4690. if (is_null($equipment)) {
  4691. return $this->responseController->makeResponse(true, 'El equipamiento relacionado no está registrado.', [], 404);
  4692. }
  4693. $workOrders = DB::table('S002V01TOTPR')->select([
  4694. 'OTPR_IDOT AS IDORDEN',
  4695. 'OTPR_EQIN AS EQUIPO',
  4696. 'OTPR_FIAP AS FECHAINICIO',
  4697. 'OTPR_FTAP AS FECHAFINAL',
  4698. 'OTPR_ACAS AS ACTIVADOR',
  4699. 'ACTI_TIAC AS TIPOACTIVADOR',
  4700. 'ACTI_PRIO AS PRIORIDAD',
  4701. 'OTPR_ESTA AS ESTATUS'
  4702. ])->where([
  4703. ['OTPR_NULI', '=', $line],
  4704. ['OTPR_EQIN', '=', $equipmentCode],
  4705. ['OTPR_ESTA', '!=', 'E'],
  4706. ])->leftJoin('S002V01TACTI', 'ACTI_IDAC', '=', 'OTPR_ACAS')
  4707. ->orderBy('OTPR_IDOT', 'desc')->get()->all();
  4708. foreach ($workOrders as $key => $order) {
  4709. $order->IDORDEN = $this->encryptionController->encrypt($order->IDORDEN);
  4710. $order->EQUIPO = $this->encryptionController->encrypt($order->EQUIPO);
  4711. $order->ACTIVADOR = $this->encryptionController->encrypt($order->ACTIVADOR);
  4712. if (!is_null($order->PRIORIDAD)) {
  4713. $order->PRIORIDAD = $this->encryptionController->encrypt($order->PRIORIDAD);
  4714. }
  4715. if ($order->FECHAFINAL == '0001-01-01 00:00:00') $order->FECHAFINAL = null;
  4716. if ($order->FECHAINICIO == '0001-01-01 00:00:00') $order->FECHAINICIO = null;
  4717. $workOrders[$key] = $order;
  4718. }
  4719. $now = $this->functionsController->now();
  4720. $nowStr = $now->toDateTimeString();
  4721. $actions = DB::getQueryLog();
  4722. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4723. $idac = $this->functionsController->registerActivity(
  4724. $line,
  4725. 'S002V01M10GMPR',
  4726. 'S002V01F01COTP',
  4727. 'S002V01P01HOTP',
  4728. 'Consulta',
  4729. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las órdenes de trabajo relacionadas al equipamient $equipmentCode.",
  4730. $idUser,
  4731. $nowStr,
  4732. 'S002V01S01ORTR'
  4733. );
  4734. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4735. return $this->responseController->makeResponse(false, 'EXITO.', $workOrders);
  4736. }
  4737. // Visitas técnicas no programadas (Preventivas)
  4738. public function getVisit($idVisit, $idUser, $line)
  4739. {
  4740. DB::enableQueryLog();
  4741. $idUser = $this->encryptionController->decrypt($idUser);
  4742. if (!$idUser) {
  4743. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  4744. }
  4745. $usr = DB::table('S002V01TUSUA')->where([
  4746. ['USUA_NULI', '=', $line],
  4747. ['USUA_IDUS', '=', $idUser],
  4748. ])->first();
  4749. if (is_null($usr)) {
  4750. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  4751. }
  4752. $idVisit = $this->encryptionController->decrypt($idVisit);
  4753. if (!$idVisit) {
  4754. return $this->responseController->makeResponse(true, 'El ID de la visita no está encriptado correctamente.', [], 400);
  4755. }
  4756. $visit = DB::table('S002V01TRVTN')->select([
  4757. 'RVTN_IDVI AS ID_ORDEN',
  4758. 'RVTN_DEIN AS DESCRIPCION',
  4759. 'RVTN_EQRE AS CODIGO_EQUIPAMIENTO',
  4760. 'EQUI_TIPO AS TIPO_EQUIPAMIENTO',
  4761. 'EQUI_MODE AS MODELO_EQUIPAMIENTO',
  4762. 'EQUI_IDEQ AS ID_EQUIPAMIENTO',
  4763. 'RVTN_TESO AS TIEMPO_ESTIMADO',
  4764. 'RVTN_CORE AS CONTADOR',
  4765. 'RVTN_DRCO AS MEDIDAS',
  4766. 'RVTN_PRIO AS PRIORIDAD',
  4767. 'RVTN_MAUT AS RECURSOS',
  4768. 'RVTN_TIAC AS TIPO_ACTIVACION',
  4769. 'RVTN_DORE AS DOCUMENTOS_RELACIONADOS',
  4770. 'RVTN_CLAS AS CLASIFICACION',
  4771. 'RVTN_COME AS COMENTARIOS',
  4772. 'RVTN_ESTA AS ESTADO',
  4773. 'RVTN_USRE AS USUREG',
  4774. 'RVTN_FERE AS FECREG',
  4775. 'RVTN_HIES AS HISTORIAL',
  4776. 'RVTN_PEIN AS PERSONAL'
  4777. ])->leftJoin('S002V01TEQUI', 'EQUI_COEQ', '=', 'RVTN_EQRE')
  4778. ->where([
  4779. ['RVTN_IDVI', '=', $idVisit],
  4780. ['RVTN_NULI', '=', $line],
  4781. ])->first();
  4782. if (is_null($visit)) {
  4783. return $this->responseController->makeResponse(true, 'La visita consultada no existe.', [], 404);
  4784. }
  4785. $visit->ID_ORDEN = $this->encryptionController->encrypt($visit->ID_ORDEN);
  4786. $visit->CODIGO_EQUIPAMIENTO = $this->encryptionController->encrypt($visit->CODIGO_EQUIPAMIENTO);
  4787. if (!is_null($visit->ID_EQUIPAMIENTO)) {
  4788. $visit->ID_EQUIPAMIENTO = $this->encryptionController->encrypt($visit->ID_EQUIPAMIENTO);
  4789. }
  4790. $visit->CONTADOR = $this->encryptionController->encrypt($visit->CONTADOR);
  4791. $visit->PRIORIDAD = $this->encryptionController->encrypt($visit->PRIORIDAD);
  4792. // Procesar PERSONAL
  4793. $staffArr = json_decode($visit->PERSONAL, true);
  4794. foreach ($staffArr as $key => $val) {
  4795. $specialty = DB::table('S002V01TGEES')->where([
  4796. ['GEES_NULI', '=', $line],
  4797. ['GEES_COES', '=', $val['ID']]
  4798. ])->first();
  4799. $val['ID'] = $this->encryptionController->encrypt($val['ID']);
  4800. $val['NAME'] = $specialty->GEES_NOES;
  4801. $staffArr[$key] = $val;
  4802. }
  4803. $visit->PERSONAL = json_encode($staffArr);
  4804. // Procesar MEDIDAS_OBJ
  4805. $measureArr = json_decode($visit->MEDIDAS, true);
  4806. if (!empty($measureArr)) {
  4807. $measureArr['CONTADOR'] = $this->encryptionController->encrypt($measureArr['CONTADOR']);
  4808. $measureArr['ID_MEDIDA'] = $this->encryptionController->encrypt($measureArr['ID_MEDIDA']);
  4809. $measureArr['ID_SERVICIO_WEB'] = $this->encryptionController->encrypt($measureArr['ID_SERVICIO_WEB']);
  4810. }
  4811. $visit->MEDIDAS_OBJ = $measureArr;
  4812. unset($visit->MEDIDAS);
  4813. // Procesar RECURSOS_ARR
  4814. $resources = json_decode($visit->RECURSOS, true);
  4815. $resourcesFn = [];
  4816. foreach ($resources as $resource) {
  4817. if ($resource['ID'] != 'SH') {
  4818. $resourceObj = DB::table('S002V01TINST')->where([
  4819. ['INST_NULI', '=', $line],
  4820. ['INST_IDIS', '=', $resource['ID']],
  4821. ])->first();
  4822. if (!is_null($resourceObj)) {
  4823. $resourcesFn[] = $resourceObj->INST_MODE;
  4824. }
  4825. }
  4826. }
  4827. $visit->RECURSOS_ARR = $resourcesFn;
  4828. // Procesar DOCUMENTOS_RELACIONADOS_ARR
  4829. $documentsArr = json_decode($visit->DOCUMENTOS_RELACIONADOS, true);
  4830. $documentsFn = [];
  4831. foreach ($documentsArr as $document) {
  4832. $documentArr = explode('=', $document);
  4833. $codeArr = explode('-', $documentArr[0]);
  4834. $file = DB::table('S002V01TAFAL')->where([
  4835. ['AFAL_NULI', '=', $line],
  4836. ['AFAL_COMO', '=', $codeArr[1]],
  4837. ['AFAL_CLDO', '=', $codeArr[2]],
  4838. ['AFAL_FECR', '=', $codeArr[3]],
  4839. ['AFAL_NUSE', '=', $codeArr[4]],
  4840. ['AFAL_NUVE', '=', $documentArr[1]]
  4841. ])->first();
  4842. $documentsFn[] = [
  4843. 'name' => $file->AFAL_NOAR . '.' . $file->AFAL_EXTE,
  4844. 'size' => $file->AFAL_TAMA
  4845. ];
  4846. }
  4847. $visit->DOCUMENTOS_RELACIONADOS_ARR = $documentsFn;
  4848. // Mapear TIPO_ACTIVACION
  4849. $activationTypes = ['M' => 'Manual', 'A' => 'Automática'];
  4850. $visit->TIPO_ACTIVACION = $activationTypes[$visit->TIPO_ACTIVACION];
  4851. // Mapear ESTADO (RVTN_ESTA enum: 'VA','EP','CP','CE','P','C','R','A','F')
  4852. // VA, EP, CP, CE: Solo para RVTN_TIAC='A' (Automática)
  4853. // P, C, R, A, F: Solo para RVTN_TIAC='M' (Manual)
  4854. $orderStates = [
  4855. 'VA' => 'Validado', // Automática
  4856. 'EP' => 'En progreso', // Automática
  4857. 'CP' => 'Cerrado pendiente', // Automática
  4858. 'CE' => 'Cerrado', // Automática
  4859. 'P' => 'Pendiente', // Manual
  4860. 'C' => 'Cancelado', // Manual
  4861. 'R' => 'Rechazado', // Manual
  4862. 'A' => 'Aprobado', // Manual
  4863. 'F' => 'Finalizado', // Manual
  4864. ];
  4865. $visit->ESTADO = $orderStates[$visit->ESTADO];
  4866. // Procesar usuarios
  4867. $usrReg = DB::table('S002V01TUSUA')->where([
  4868. ['USUA_NULI', '=', $line],
  4869. ['USUA_IDUS', '=', $visit->USUREG],
  4870. ])->first();
  4871. $nameReg = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  4872. $visit->USUREG = $nameReg . " (" . $visit->USUREG . ")";
  4873. $now = $this->functionsController->now();
  4874. $nowStr = $now->toDateTimeString();
  4875. $actions = DB::getQueryLog();
  4876. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4877. $idac = $this->functionsController->registerActivity(
  4878. $line,
  4879. 'S002V01M10GMPR',
  4880. 'S002V01F11RVTP',
  4881. 'S002V01P02COVI',
  4882. 'Consulta',
  4883. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la visita técnica #$idVisit.",
  4884. $idUser,
  4885. $nowStr,
  4886. 'S002V01S02AOTR'
  4887. );
  4888. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4889. return $this->responseController->makeResponse(false, 'EXITO.', $visit);
  4890. }
  4891. // Visitas técnicas no programadas (Preventivas)
  4892. public function assignOperariosToPreventiveVisit(Request $request)
  4893. {
  4894. DB::enableQueryLog();
  4895. $validator = Validator::make($request->all(), [
  4896. 'id_user' => 'required|string',
  4897. 'linea' => 'required|integer',
  4898. 'id_order' => 'required|string',
  4899. 'config' => 'required|json',
  4900. ]);
  4901. if ($validator->fails()) {
  4902. return $this->responseController->makeResponse(
  4903. true,
  4904. "Se encontraron uno o más errores.",
  4905. $this->responseController->makeErrors(
  4906. $validator->errors()->messages()
  4907. ),
  4908. 401
  4909. );
  4910. }
  4911. $form = $request->all();
  4912. $idUser = $this->encryptionController->decrypt($form['id_user']);
  4913. if (!$idUser) {
  4914. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  4915. }
  4916. $usr = DB::table('S002V01TUSUA')->where([
  4917. ['USUA_NULI', '=', $form['linea']],
  4918. ['USUA_IDUS', '=', $idUser]
  4919. ])->first();
  4920. if (is_null($usr)) {
  4921. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  4922. }
  4923. $idVisit = $this->encryptionController->decrypt($form['id_order']);
  4924. if (!$idVisit) {
  4925. return $this->responseController->makeResponse(true, 'El ID de la visita no fue encriptado correctamente.', [], 400);
  4926. }
  4927. $visit = DB::table('S002V01TRVTN')->where([
  4928. ['RVTN_NULI', '=', $form['linea']],
  4929. ['RVTN_IDVI', '=', $idVisit]
  4930. ])->first();
  4931. if (is_null($visit)) {
  4932. return $this->responseController->makeResponse(true, 'La visita solicitada no existe.', [], 404);
  4933. } else if ($visit->RVTN_ESTA != 'P' && $visit->RVTN_ESTA != 'VA') {
  4934. return $this->responseController->makeResponse(true, 'La visita no está en un estado válido para asignar operarios.', [], 401);
  4935. }
  4936. $staffConfigArr = json_decode($form['config'], true);
  4937. $audience = [];
  4938. foreach ($staffConfigArr as $key => $val) {
  4939. if (!array_key_exists('SPECIALTY', $val) || !array_key_exists('STAFF', $val)) {
  4940. return $this->responseController->makeResponse(true, "El item $key del arreglo de configuración de operarios tiene un formato inválido.", [], 400);
  4941. }
  4942. $specialtyDec = $this->encryptionController->decrypt($val['SPECIALTY']);
  4943. if (!$specialtyDec) {
  4944. return $this->responseController->makeResponse(true, "El identificador de la especialidad en el item $key del arreglo de configuración de operarios no fue encriptado correctamente.", [], 400);
  4945. }
  4946. if (gettype($val['STAFF']) != 'array') {
  4947. return $this->responseController->makeResponse(true, "El contenedor de operarios en el item $key del arreglo de configuración de operarios es inválido.", [], 400);
  4948. }
  4949. foreach ($val['STAFF'] as $key0 => $val0) {
  4950. if (!array_key_exists('ID', $val0) || !array_key_exists('TYPE', $val0)) {
  4951. return $this->responseController->makeResponse(true, "El item $key del arreglo de configuración de operarios tiene un formato inválido.", [], 400);
  4952. }
  4953. $idDec = $this->encryptionController->decrypt($val0['ID']);
  4954. $employee = DB::table('S002V01TPERS')->where([
  4955. ['PERS_NULI', '=', $form['linea']],
  4956. ['PERS_IDPE', '=', $idDec]
  4957. ])->first();
  4958. if (is_null($employee)) {
  4959. return $this->responseController->makeResponse(true, "El operario $key0 de la especialidad $specialtyDec del arreglo de configuración de operarios no existe.", [], 404);
  4960. }
  4961. $audience[] = $employee->PERS_IDUS;
  4962. }
  4963. }
  4964. // Validación de cupo global (basado en RVTN_PEIN)
  4965. $planilla = json_decode($visit->RVTN_PEIN, true);
  4966. $cupoRequerido = 0;
  4967. if (!empty($planilla) && is_array($planilla)) {
  4968. foreach ($planilla as $pe) {
  4969. $cupoRequerido += intval($pe['CANT'] ?? 0);
  4970. }
  4971. }
  4972. // Obtener último VA
  4973. $statusHistoryArr = json_decode($visit->RVTN_HIES, true);
  4974. $vaObj = null;
  4975. if (!empty($statusHistoryArr)) {
  4976. for ($i = count($statusHistoryArr) - 1; $i >= 0; $i--) {
  4977. if ($statusHistoryArr[$i]['ESTADO'] == 'VA') {
  4978. $vaObj = $statusHistoryArr[$i];
  4979. break;
  4980. }
  4981. }
  4982. }
  4983. $atencionActual = $vaObj && array_key_exists('ATENCION', $vaObj) ? $vaObj['ATENCION'] : [];
  4984. $aceptadosActual = 0;
  4985. foreach ($atencionActual as $itm) {
  4986. if (($itm['RESPUESTA'] ?? '') === 'A') $aceptadosActual++;
  4987. }
  4988. if ($cupoRequerido > 0 && $aceptadosActual >= $cupoRequerido) {
  4989. return $this->responseController->makeResponse(true, 'La planilla ya está completa. No se pueden asignar más operarios.', [], 401);
  4990. }
  4991. // Evitar invitación duplicada al mismo operario (ya invitado o ya respondió)
  4992. $yaInvitados = [];
  4993. foreach ($atencionActual as $itm) {
  4994. $yaInvitados[$itm['ID']] = $itm['RESPUESTA'] ?? '';
  4995. }
  4996. foreach ($audience as $opId) {
  4997. if (array_key_exists($opId, $yaInvitados)) {
  4998. return $this->responseController->makeResponse(true, "El operario $opId ya fue invitado o respondió anteriormente.", [], 401);
  4999. }
  5000. }
  5001. // Iniciar transacción
  5002. DB::beginTransaction();
  5003. try {
  5004. // Emitir notificación
  5005. $this->notificationsController->emitNotification(
  5006. 'S002V01M10GMPR',
  5007. "Visita de mantenimiento preventivo #$idVisit",
  5008. "Se ha asignado la visita de mantenimiento preventivo #$idVisit y requiere su atención.",
  5009. [[
  5010. 'BOTON' => 'Ver detalles',
  5011. 'FUNCION' => 'openPreventiveWorkOrderDetails',
  5012. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit)])
  5013. ], [
  5014. 'BOTON' => 'Atender visita',
  5015. 'FUNCION' => 'attendPreventiveWorkOrder',
  5016. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit)])
  5017. ], [
  5018. 'BOTON' => 'Ir al módulo',
  5019. 'FUNCION' => 'openModule',
  5020. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMPR/AOTR/RVTP')])
  5021. ]],
  5022. $audience,
  5023. $idUser,
  5024. $form['linea'],
  5025. $this->getSocketClient(),
  5026. $idVisit,
  5027. 'Preventivo'
  5028. );
  5029. $now = $this->functionsController->now();
  5030. $nowStr = $now->toDateTimeString();
  5031. $statusHistoryArr = json_decode($visit->RVTN_HIES, true);
  5032. // Buscar si ya existe un objeto con estado VA
  5033. $vaStatusIndex = null;
  5034. foreach ($statusHistoryArr as $index => $status) {
  5035. if ($status['ESTADO'] == 'VA') {
  5036. $vaStatusIndex = $index;
  5037. break;
  5038. }
  5039. }
  5040. // Revalidar cupo justo antes de insertar (concurrencia)
  5041. $vaObjRef = null;
  5042. if (!empty($statusHistoryArr)) {
  5043. for ($i = count($statusHistoryArr) - 1; $i >= 0; $i--) {
  5044. if ($statusHistoryArr[$i]['ESTADO'] == 'VA') {
  5045. $vaObjRef = $statusHistoryArr[$i];
  5046. break;
  5047. }
  5048. }
  5049. }
  5050. $atencionRef = $vaObjRef && array_key_exists('ATENCION', $vaObjRef) ? $vaObjRef['ATENCION'] : [];
  5051. $aceptadosRef = 0;
  5052. foreach ($atencionRef as $itm) {
  5053. if (($itm['RESPUESTA'] ?? '') === 'A') $aceptadosRef++;
  5054. }
  5055. if ($cupoRequerido > 0 && $aceptadosRef >= $cupoRequerido) {
  5056. DB::rollBack();
  5057. return $this->responseController->makeResponse(true, 'La planilla ya está completa. No se pueden asignar más operarios.', [], 401);
  5058. }
  5059. // Crear nuevas invitaciones
  5060. $newInvitations = [];
  5061. foreach ($audience as $operarioId) {
  5062. $newInvitations[] = [
  5063. 'ID' => $operarioId,
  5064. 'FIRMA' => null,
  5065. 'RESPUESTA' => '',
  5066. 'COMENTARIOS' => ''
  5067. ];
  5068. }
  5069. if ($vaStatusIndex !== null) {
  5070. // Extender objeto VA existente
  5071. $statusHistoryArr[$vaStatusIndex]['ATENCION'] = array_merge(
  5072. $statusHistoryArr[$vaStatusIndex]['ATENCION'],
  5073. $newInvitations
  5074. );
  5075. // Agregar nuevos operarios a PERSONAL (sin duplicar)
  5076. $currentPersonal = $statusHistoryArr[$vaStatusIndex]['PERSONAL'] ?? [];
  5077. foreach ($audience as $operarioId) {
  5078. if (!in_array($operarioId, $currentPersonal)) {
  5079. $currentPersonal[] = $operarioId;
  5080. }
  5081. }
  5082. $statusHistoryArr[$vaStatusIndex]['PERSONAL'] = $currentPersonal;
  5083. } else {
  5084. // Crear nuevo objeto VA con todos los invitados en PERSONAL
  5085. $statusHistoryArr[] = [
  5086. 'USUARIO' => $idUser,
  5087. 'ESTADO' => 'VA',
  5088. 'FECHA' => $nowStr,
  5089. 'ATENCION' => $newInvitations,
  5090. 'PERSONAL' => $audience
  5091. ];
  5092. }
  5093. $statusHistoryStr = json_encode($statusHistoryArr);
  5094. // Actualizar la visita
  5095. DB::table('S002V01TRVTN')->where([
  5096. ['RVTN_IDVI', '=', $idVisit],
  5097. ['RVTN_NULI', '=', $form['linea']]
  5098. ])->update([
  5099. 'RVTN_ESTA' => 'VA',
  5100. 'RVTN_HIES' => $statusHistoryStr,
  5101. 'RVTN_USMO' => $idUser,
  5102. 'RVTN_FEMO' => $nowStr,
  5103. ]);
  5104. $actions = DB::getQueryLog();
  5105. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  5106. $idac = $this->functionsController->registerActivity(
  5107. $form['linea'],
  5108. 'S002V01M10GMPR',
  5109. 'S002V01F11RVTP',
  5110. 'S002V01P01REVI',
  5111. 'Actualización',
  5112. "El usuario $name (" . $usr->USUA_IDUS . ") asignó operarios a la visita preventiva #$idVisit.",
  5113. $idUser,
  5114. $nowStr,
  5115. 'S002V01S02AOTR'
  5116. );
  5117. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  5118. // Confirmar transacción
  5119. DB::commit();
  5120. return $this->responseController->makeResponse(false, 'EXITO.');
  5121. } catch (\Exception $e) {
  5122. // Revertir transacción en caso de error
  5123. DB::rollBack();
  5124. $actions = DB::getQueryLog();
  5125. $now = $this->functionsController->now();
  5126. $nowStr = $now->toDateTimeString();
  5127. $name = $this->functionsController->joinName($usr->USUA_NOMB ?? '', $usr->USUA_APPA ?? '', $usr->USUA_APMA ?? '');
  5128. $idac = $this->functionsController->registerActivity(
  5129. $form['linea'],
  5130. 'S002V01M10GMPR',
  5131. 'S002V01F11RVTP',
  5132. 'S002V01P01REVI',
  5133. 'Error',
  5134. "Error al asignar operarios a la visita preventiva #$idVisit: " . $e->getMessage(),
  5135. $idUser,
  5136. $nowStr,
  5137. 'S002V01S02AOTR'
  5138. );
  5139. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  5140. return $this->responseController->makeResponse(
  5141. true,
  5142. 'Ocurrió un error al asignar operarios a la visita preventiva.',
  5143. ['error' => $e->getMessage()],
  5144. 500
  5145. );
  5146. }
  5147. }
  5148. // Visitas técnicas no programadas (Preventivas)
  5149. public function getVisitAttendance($idVisit, $idUser, $line)
  5150. {
  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. $idVisit = $this->encryptionController->decrypt($idVisit);
  5164. if (!$idVisit) {
  5165. return $this->responseController->makeResponse(true, 'El ID de la visita relacionada no está encriptado correctamente', [], 400);
  5166. }
  5167. $visit = DB::table('S002V01TRVTN')->where([
  5168. ['RVTN_NULI', '=', $line],
  5169. ['RVTN_IDVI', '=', $idVisit]
  5170. ])->first();
  5171. if (is_null($visit)) {
  5172. return $this->responseController->makeResponse(true, 'La visita relacionada no está registrada.', [], 404);
  5173. }
  5174. // Validar que la visita esté en estado VA (validado)
  5175. // Puede estar en estado VA en RVTN_ESTA sin tener aún el objeto VA en RVTN_HIES
  5176. // (cuando se crea automáticamente pero aún no se han asignado operarios)
  5177. $visitStates = [
  5178. 'VA' => 'Validada',
  5179. 'EP' => 'En progreso',
  5180. 'CP' => 'Cerrada pendiente',
  5181. 'CE' => 'Cerrada',
  5182. 'PE' => 'Pendiente de validación',
  5183. 'P' => 'Pendiente',
  5184. 'C' => 'Cancelada',
  5185. 'R' => 'Rechazada',
  5186. 'A' => 'Aprobada',
  5187. 'F' => 'Finalizada',
  5188. ];
  5189. if ($visit->RVTN_ESTA !== 'VA') {
  5190. $statusKey = $visit->RVTN_ESTA;
  5191. $statusName = array_key_exists($statusKey, $visitStates) ? $visitStates[$statusKey] : $statusKey;
  5192. return $this->responseController->makeResponse(true, "La visita relacionada no está en el estado de validación (estado actual: $statusName)", [], 404);
  5193. }
  5194. $statusHistory = json_decode($visit->RVTN_HIES, true);
  5195. if (!is_array($statusHistory)) {
  5196. $statusHistory = [];
  5197. }
  5198. $validatedHistoryFilt = array_filter($statusHistory, function ($v, $k) {
  5199. return isset($v['ESTADO']) && $v['ESTADO'] == 'VA';
  5200. }, ARRAY_FILTER_USE_BOTH);
  5201. // Si no hay objeto VA en el historial pero el estado es VA, significa que aún no se han asignado operarios
  5202. if (empty($validatedHistoryFilt)) {
  5203. $statusKey = $visit->RVTN_ESTA;
  5204. $statusName = array_key_exists($statusKey, $visitStates) ? $visitStates[$statusKey] : $statusKey;
  5205. return $this->responseController->makeResponse(true, "La visita tiene estado $statusName pero aún no ha asignado operarios.", [], 404);
  5206. }
  5207. $validatedHistory = end($validatedHistoryFilt);
  5208. $attendance = array_key_exists('ATENCION', $validatedHistory) ? $validatedHistory['ATENCION'] : [];
  5209. $attendanceAux = [];
  5210. foreach ($attendance as $item) {
  5211. if ($item['RESPUESTA'] == 'A') {
  5212. $attendanceAux[] = $item;
  5213. }
  5214. }
  5215. return $this->responseController->makeResponse(false, 'EXITO', $attendanceAux);
  5216. }
  5217. // Visitas técnicas no programadas (Preventivas)
  5218. public function getVisitStaff($idVisit, $idUser, $line)
  5219. {
  5220. DB::enableQueryLog();
  5221. $idUser = $this->encryptionController->decrypt($idUser);
  5222. if (!$idUser) {
  5223. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  5224. }
  5225. $usr = DB::table('S002V01TUSUA')->where([
  5226. ['USUA_NULI', '=', $line],
  5227. ['USUA_IDUS', '=', $idUser],
  5228. ])->first();
  5229. if (is_null($usr)) {
  5230. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  5231. }
  5232. $idVisit = $this->encryptionController->decrypt($idVisit);
  5233. if (!$idVisit) {
  5234. return $this->responseController->makeResponse(true, 'El ID de la visita relacionada no está encriptado correctamente', [], 400);
  5235. }
  5236. $visit = DB::table('S002V01TRVTN')->where([
  5237. ['RVTN_NULI', '=', $line],
  5238. ['RVTN_IDVI', '=', $idVisit]
  5239. ])->first();
  5240. if (is_null($visit)) {
  5241. return $this->responseController->makeResponse(true, 'La visita relacionada no está registrada.', [], 404);
  5242. }
  5243. $staffArr = json_decode($visit->RVTN_PEIN, true);
  5244. $staffArrFn = [];
  5245. foreach ($staffArr as $val) {
  5246. if (array_key_exists('TYPE', $val)) {
  5247. if ($val['TYPE'] == 'EQ') {
  5248. $workTeamStaff = DB::table('S002V01TPERS')->join('S002V01TUSUA', 'USUA_IDUS', '=', 'PERS_IDUS')->where([
  5249. ['PERS_NULI', '=', $line],
  5250. ['PERS_EQTR', '=', $val['ID']]
  5251. ])->get()->all();
  5252. foreach ($workTeamStaff as $item) {
  5253. $itemID = $item->PERS_IDPE;
  5254. $itemFilt = array_filter($staffArrFn, function ($v, $k) use ($itemID) {
  5255. return $v['ID'] == $itemID;
  5256. }, ARRAY_FILTER_USE_BOTH);
  5257. if (count($itemFilt) <= 0) {
  5258. $staffArrFn[] = [
  5259. 'ID' => $itemID,
  5260. 'ID_USER' => $item->USUA_IDUS,
  5261. 'NAME' => $this->functionsController->joinName($item->USUA_NOMB, $item->USUA_APPA, $item->USUA_APMA),
  5262. 'TYPE' => $item->PERS_TICO,
  5263. ];
  5264. }
  5265. }
  5266. } else if ($val['TYPE'] == 'SU') {
  5267. $subcontratistStaff = DB::table('S002V01TPERS')->join('S002V01TUSUA', 'USUA_IDUS', '=', 'PERS_IDUS')->where([
  5268. ['PERS_NULI', '=', $line],
  5269. ['PERS_IDPS', '=', $val['ID']]
  5270. ])->get()->all();
  5271. foreach ($subcontratistStaff as $item) {
  5272. $itemID = $item->PERS_IDPE;
  5273. $itemFilt = array_filter($staffArrFn, function ($v, $k) use ($itemID) {
  5274. return $v['ID'] == $itemID;
  5275. }, ARRAY_FILTER_USE_BOTH);
  5276. if (count($itemFilt) <= 0) {
  5277. $staffArrFn[] = [
  5278. 'ID' => $itemID,
  5279. 'ID_USER' => $item->USUA_IDUS,
  5280. 'NAME' => $this->functionsController->joinName($item->USUA_NOMB, $item->USUA_APPA, $item->USUA_APMA),
  5281. 'TYPE' => $item->PERS_TICO,
  5282. ];
  5283. }
  5284. }
  5285. } else if ($val['TYPE'] == 'EM') {
  5286. $employee = DB::table('S002V01TPERS')->join('S002V01TUSUA', 'USUA_IDUS', '=', 'PERS_IDUS')->where([
  5287. ['PERS_NULI', '=', $line],
  5288. ['PERS_IDPE', '=', $val['ID']]
  5289. ])->first();
  5290. if (!is_null($employee)) {
  5291. $itemID = $employee->PERS_IDPE;
  5292. $itemFilt = array_filter($staffArrFn, function ($v, $k) use ($itemID) {
  5293. return $v['ID'] == $itemID;
  5294. }, ARRAY_FILTER_USE_BOTH);
  5295. if (count($itemFilt) <= 0) {
  5296. $staffArrFn[] = [
  5297. 'ID' => $itemID,
  5298. 'ID_USER' => $employee->USUA_IDUS,
  5299. 'NAME' => $this->functionsController->joinName($employee->USUA_NOMB, $employee->USUA_APPA, $employee->USUA_APMA),
  5300. 'TYPE' => $employee->PERS_TICO,
  5301. ];
  5302. }
  5303. }
  5304. }
  5305. }
  5306. }
  5307. return $this->responseController->makeResponse(false, 'EXITO', $staffArr);
  5308. }
  5309. // Visitas técnicas no programadas (Preventivas)
  5310. public function getVisitStatusHistory($idOrder, $idUser, $line)
  5311. {
  5312. DB::enableQueryLog();
  5313. $idUser = $this->encryptionController->decrypt($idUser);
  5314. if (!$idUser) {
  5315. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  5316. }
  5317. $usr = DB::table('S002V01TUSUA')->where([
  5318. ['USUA_NULI', '=', $line],
  5319. ['USUA_IDUS', '=', $idUser],
  5320. ])->first();
  5321. if (is_null($usr)) {
  5322. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  5323. }
  5324. $idOrder = $this->encryptionController->decrypt($idOrder);
  5325. if (!$idOrder) {
  5326. return $this->responseController->makeResponse(true, 'El ID de la visita solicitada no está encriptado correctamente', [], 400);
  5327. }
  5328. $visit = DB::table('S002V01TRVTN')->select([
  5329. 'RVTN_HIES AS HISTORIAL',
  5330. ])->where([
  5331. ['RVTN_IDVI', '=', $idOrder],
  5332. ['RVTN_NULI', '=', $line]
  5333. ])->first();
  5334. if (is_null($visit)) {
  5335. return $this->responseController->makeResponse(true, 'La visita solicitada no está registrada.', [], 404);
  5336. }
  5337. // RVTN_ESTA enum: 'VA','EP','CP','CE','P','C','R','A','F'
  5338. // VA, EP, CP, CE: Solo para RVTN_TIAC='A' (Automática)
  5339. // P, C, R, A, F: Solo para RVTN_TIAC='M' (Manual)
  5340. $visitStates = [
  5341. 'VA' => 'Validado', // Automática
  5342. 'EP' => 'En progreso', // Automática
  5343. 'CP' => 'Cerrado pendiente', // Automática
  5344. 'CE' => 'Cerrado', // Automática
  5345. 'PE' => 'Pendiente de validación', // Automática
  5346. 'P' => 'Pendiente', // Manual
  5347. 'C' => 'Cancelado', // Manual
  5348. 'R' => 'Rechazado', // Manual
  5349. 'A' => 'Aprobado', // Manual
  5350. 'F' => 'Finalizado', // Manual
  5351. ];
  5352. $statusHistoryArr = json_decode($visit->HISTORIAL, true);
  5353. foreach ($statusHistoryArr as $key => $item) {
  5354. $item['ESTADO'] = $visitStates[$item['ESTADO']];
  5355. $usrSta = DB::table('S002V01TUSUA')->where([
  5356. ['USUA_NULI', '=', $line],
  5357. ['USUA_IDUS', '=', $item['USUARIO']]
  5358. ])->first();
  5359. if (!is_null($usrSta)) {
  5360. $usrStaName = $this->functionsController->joinName($usrSta->USUA_NOMB, $usrSta->USUA_APPA, $usrSta->USUA_APMA);
  5361. $item['USUARIO'] = $usrStaName . " (" . $item['USUARIO'] . ")";
  5362. } else {
  5363. $item['USUARIO'] = "Usuario no encontrado (" . $item['USUARIO'] . ")";
  5364. }
  5365. $statusHistoryArr[$key] = $item;
  5366. }
  5367. $now = $this->functionsController->now();
  5368. $nowStr = $now->toDateTimeString();
  5369. $actions = DB::getQueryLog();
  5370. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  5371. $idac = $this->functionsController->registerActivity(
  5372. $line,
  5373. 'S002V01M10GMPR',
  5374. 'S002V01F11RVTP',
  5375. '-',
  5376. 'Consulta',
  5377. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el historial de estados de la visita #$idOrder de mantenimiento preventivo.",
  5378. $idUser,
  5379. $nowStr,
  5380. 'S002V01S02AOTR'
  5381. );
  5382. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  5383. return $this->responseController->makeResponse(false, 'EXITO.', $statusHistoryArr);
  5384. }
  5385. // Visitas técnicas no programadas (Preventivas)
  5386. public function attendPreventiveVisit(Request $request)
  5387. {
  5388. DB::enableQueryLog();
  5389. $validator = Validator::make($request->all(), [
  5390. 'id_user' => 'required|string',
  5391. 'linea' => 'required|integer',
  5392. 'id_order' => 'required|string',
  5393. 'attendance' => 'required|string|in:A,R',
  5394. 'data' => 'required|string'
  5395. ]);
  5396. if ($validator->fails()) {
  5397. return $this->responseController->makeResponse(
  5398. true,
  5399. "Se encontraron uno o más errores.",
  5400. $this->responseController->makeErrors(
  5401. $validator->errors()->messages()
  5402. ),
  5403. 401
  5404. );
  5405. }
  5406. $form = $request->all();
  5407. $idUser = $this->encryptionController->decrypt($form['id_user']);
  5408. if (!$idUser) {
  5409. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  5410. }
  5411. $usr = DB::table('S002V01TUSUA')->where([
  5412. ['USUA_NULI', '=', $form['linea']],
  5413. ['USUA_IDUS', '=', $idUser]
  5414. ])->first();
  5415. if (is_null($usr)) {
  5416. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  5417. }
  5418. $idVisit = $this->encryptionController->decrypt($form['id_order']);
  5419. if (!$idVisit) {
  5420. return $this->responseController->makeResponse(true, 'El ID de la visita no fue encriptado correctamente.', [], 400);
  5421. }
  5422. $visit = DB::table('S002V01TRVTN')->where([
  5423. ['RVTN_NULI', '=', $form['linea']],
  5424. ['RVTN_IDVI', '=', $idVisit]
  5425. ])->first();
  5426. if (is_null($visit)) {
  5427. return $this->responseController->makeResponse(true, 'La visita solicitada no existe.', [], 404);
  5428. } else if ($visit->RVTN_ESTA != 'VA') {
  5429. return $this->responseController->makeResponse(true, 'La visita no está validada para atender.', [], 401);
  5430. }
  5431. // Validar datos según tipo de respuesta
  5432. if ($form['attendance'] == 'A' && !str_contains($form['data'], 'data:image/png')) {
  5433. return $this->responseController->makeResponse(true, 'El archivo de la firma capturada es inválido.', [], 400);
  5434. } else if ($form['attendance'] == 'R' && strlen($form['data']) < 10) {
  5435. return $this->responseController->makeResponse(true, 'La cadena de comentarios tiene una longitud menor a 10 caracteres.', [], 400);
  5436. }
  5437. // Cupo global requerido (RVTN_PEIN)
  5438. $planilla = json_decode($visit->RVTN_PEIN, true);
  5439. $cupoRequerido = 0;
  5440. if (!empty($planilla) && is_array($planilla)) {
  5441. foreach ($planilla as $pe) {
  5442. $cupoRequerido += intval($pe['CANT'] ?? 0);
  5443. }
  5444. }
  5445. // Obtener historial de estados
  5446. $statusHistoryArr = json_decode($visit->RVTN_HIES, true);
  5447. // Buscar el último objeto con estado VA
  5448. $vaStatusIndex = null;
  5449. foreach ($statusHistoryArr as $index => $status) {
  5450. if ($status['ESTADO'] == 'VA') {
  5451. $vaStatusIndex = $index;
  5452. }
  5453. }
  5454. if ($vaStatusIndex === null) {
  5455. return $this->responseController->makeResponse(true, 'No se encontró un estado validado en el historial.', [], 404);
  5456. }
  5457. // Buscar el operario en el array ATENCION
  5458. $operatorIndex = null;
  5459. $atencionArr = $statusHistoryArr[$vaStatusIndex]['ATENCION'] ?? [];
  5460. foreach ($atencionArr as $index => $invitacion) {
  5461. if ($invitacion['ID'] == $idUser) {
  5462. $operatorIndex = $index;
  5463. break;
  5464. }
  5465. }
  5466. if ($operatorIndex === null) {
  5467. return $this->responseController->makeResponse(true, 'El usuario no está invitado a esta visita.', [], 401);
  5468. }
  5469. // Verificar si ya respondió
  5470. if (!empty($atencionArr[$operatorIndex]['RESPUESTA'])) {
  5471. $respuestaAnterior = $atencionArr[$operatorIndex]['RESPUESTA'] == 'A' ? 'aceptado' : 'rechazado';
  5472. return $this->responseController->makeResponse(true, "Ya ha $respuestaAnterior esta invitación.", [], 401);
  5473. }
  5474. // Bloquear aceptación si la planilla ya está completa (con base en aceptados actuales)
  5475. $aceptadosActual = 0;
  5476. foreach ($atencionArr as $itm) {
  5477. if (($itm['RESPUESTA'] ?? '') === 'A') $aceptadosActual++;
  5478. }
  5479. if ($form['attendance'] == 'A' && $cupoRequerido > 0 && $aceptadosActual >= $cupoRequerido) {
  5480. return $this->responseController->makeResponse(true, 'La planilla ya está completa. No es posible aceptar esta invitación.', [], 401);
  5481. }
  5482. // Iniciar transacción
  5483. DB::beginTransaction();
  5484. try {
  5485. $signature = null;
  5486. $comments = null;
  5487. if ($form['attendance'] == 'A') {
  5488. // Procesar firma
  5489. $filesPath = str_replace("app\\Http\\Controllers", "storage\\app\\files", __DIR__);
  5490. if (!file_exists($filesPath)) {
  5491. throw new \Exception('No se encontró la ubicación de almacenamiento de archivos.');
  5492. }
  5493. generateSignatureFileName:
  5494. $signatureFileName = $this->generateSignatureImageName();
  5495. $signatureFileDir = $filesPath . "\\" . $signatureFileName;
  5496. $signatureFileExists = file_exists($signatureFileDir);
  5497. if ($signatureFileExists) goto generateSignatureFileName;
  5498. $signatureFileData = str_replace("data:image/png;base64,", "", $form['data']);
  5499. file_put_contents($signatureFileDir, base64_decode($signatureFileData));
  5500. $now = $this->functionsController->now();
  5501. $year = $now->year;
  5502. $month = $now->month < 10 ? "0{$now->month}" : "{$now->month}";
  5503. $day = $now->day < 10 ? "0{$now->day}" : "{$now->day}";
  5504. $fecr = substr("$year", -2) . $month . $day;
  5505. $sec = DB::table('S002V01TAFAL')->where([
  5506. ['AFAL_COMO', '=', "GMPR"],
  5507. ['AFAL_CLDO', '=', "FO"],
  5508. ['AFAL_NULI', '=', $form['linea']],
  5509. ])->orderBy('AFAL_NUSE', 'desc')->first();
  5510. $nuse = 1;
  5511. if (!is_null($sec)) {
  5512. $nuse = intval($sec->AFAL_NUSE) + 1;
  5513. }
  5514. $noar = "firma_conformidad_" . $idUser;
  5515. $exte = "png";
  5516. $ver = DB::table('S002V01TAFAL')->where([
  5517. ['AFAL_NULI', '=', $form['linea']],
  5518. ['AFAL_COMO', '=', "GMPR"],
  5519. ['AFAL_CLDO', '=', "FO"],
  5520. ['AFAL_NOAR', '=', $noar],
  5521. ['AFAL_EXTE', '=', $exte],
  5522. ])->orderBy('AFAL_NUVE', 'desc')->first();
  5523. $nuve = 1;
  5524. if (!is_null($ver)) {
  5525. $nuve = intval($ver->AFAL_NUVE) + 1;
  5526. }
  5527. $tama = filesize($signatureFileDir);
  5528. $code = intval($form['linea']) < 10 ? "0{$form['linea']}" : "{$form['linea']}";
  5529. $code .= "-GMPR-FO-$fecr-";
  5530. for ($i = strlen((string)$nuse); $i < 6; $i++) {
  5531. $code .= "0";
  5532. }
  5533. $code .= "$nuse=";
  5534. $code .= $nuve < 10 ? "0$nuve=" : "$nuve=";
  5535. $code .= "$noar.$exte";
  5536. $usac = json_encode([$idUser]);
  5537. $nowStr = $now->toDateTimeString();
  5538. DB::table('S002V01TAFAL')->insert([
  5539. 'AFAL_NULI' => $form['linea'],
  5540. 'AFAL_COMO' => "GMPR",
  5541. 'AFAL_CLDO' => "FO",
  5542. 'AFAL_FECR' => $fecr,
  5543. 'AFAL_NUSE' => $nuse,
  5544. 'AFAL_NUVE' => $nuve,
  5545. 'AFAL_NOAR' => $noar,
  5546. 'AFAL_EXTE' => $exte,
  5547. 'AFAL_TAMA' => $tama,
  5548. 'AFAL_UBIC' => $signatureFileDir,
  5549. 'AFAL_USAC' => $usac,
  5550. 'AFAL_USRE' => $idUser,
  5551. 'AFAL_FERE' => $nowStr
  5552. ]);
  5553. $signature = $code;
  5554. } else if ($form['attendance'] == 'R') {
  5555. $comments = $form['data'];
  5556. }
  5557. // Última verificación de cupo (concurrencia) antes de confirmar respuesta
  5558. $statusHistoryCheck = json_decode(DB::table('S002V01TRVTN')->where([
  5559. ['RVTN_NULI', '=', $form['linea']],
  5560. ['RVTN_IDVI', '=', $idVisit]
  5561. ])->value('RVTN_HIES'), true);
  5562. $vaObjChk = null;
  5563. if (!empty($statusHistoryCheck)) {
  5564. for ($i = count($statusHistoryCheck) - 1; $i >= 0; $i--) {
  5565. if ($statusHistoryCheck[$i]['ESTADO'] == 'VA') {
  5566. $vaObjChk = $statusHistoryCheck[$i];
  5567. break;
  5568. }
  5569. }
  5570. }
  5571. $aceptadosChk = 0;
  5572. $atencionChk = $vaObjChk && array_key_exists('ATENCION', $vaObjChk) ? $vaObjChk['ATENCION'] : [];
  5573. foreach ($atencionChk as $itm) {
  5574. if (($itm['RESPUESTA'] ?? '') === 'A') $aceptadosChk++;
  5575. }
  5576. if ($form['attendance'] == 'A' && $cupoRequerido > 0 && $aceptadosChk >= $cupoRequerido) {
  5577. DB::rollBack();
  5578. return $this->responseController->makeResponse(true, 'La planilla ya está completa. No es posible aceptar esta invitación.', [], 401);
  5579. }
  5580. // Actualizar el objeto en ATENCION
  5581. $statusHistoryArr[$vaStatusIndex]['ATENCION'][$operatorIndex]['RESPUESTA'] = $form['attendance'];
  5582. $statusHistoryArr[$vaStatusIndex]['ATENCION'][$operatorIndex]['FIRMA'] = $signature;
  5583. $statusHistoryArr[$vaStatusIndex]['ATENCION'][$operatorIndex]['COMENTARIOS'] = $comments;
  5584. $statusHistoryStr = json_encode($statusHistoryArr);
  5585. $now = $this->functionsController->now();
  5586. $nowStr = $now->toDateTimeString();
  5587. // Actualizar la visita
  5588. DB::table('S002V01TRVTN')->where([
  5589. ['RVTN_NULI', '=', $form['linea']],
  5590. ['RVTN_IDVI', '=', $idVisit]
  5591. ])->update([
  5592. "RVTN_HIES" => $statusHistoryStr,
  5593. "RVTN_USMO" => $idUser,
  5594. "RVTN_FEMO" => $nowStr,
  5595. ]);
  5596. $userName = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  5597. $action = $form['attendance'] == 'A' ? 'aceptó' : 'rechazó';
  5598. // Preparar notificaciones
  5599. $notificationActions = [];
  5600. if ($form['attendance'] == 'A') {
  5601. $notificationActions[] = [
  5602. 'BOTON' => 'Revisar firma',
  5603. 'FUNCION' => 'reviewSignatureInPreventiveVisit',
  5604. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit), $this->encryptionController->encrypt($idUser)])
  5605. ];
  5606. } else if ($form['attendance'] == 'R') {
  5607. $notificationActions[] = [
  5608. 'BOTON' => 'Revisar comentarios',
  5609. 'FUNCION' => 'reviewCommentsInPreventiveVisit',
  5610. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit), $this->encryptionController->encrypt($idUser)])
  5611. ];
  5612. $notificationActions[] = [
  5613. 'BOTON' => 'Reasignar visita',
  5614. 'FUNCION' => 'revalidatePreventiveVisit',
  5615. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit)])
  5616. ];
  5617. }
  5618. $notificationActions[] = [
  5619. 'BOTON' => 'Ir al módulo',
  5620. 'FUNCION' => 'openModule',
  5621. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMPR/AOTR/RVTP')])
  5622. ];
  5623. // Notificar al regulador (usuario 0000000001)
  5624. $this->notificationsController->emitNotification(
  5625. 'S002V01M10GMPR',
  5626. "Visita de mantenimiento preventivo #$idVisit",
  5627. "El usuario $userName ($idUser) $action atender la visita de mantenimiento preventivo #$idVisit.",
  5628. $notificationActions,
  5629. ['0000000001'],
  5630. $idUser,
  5631. $form['linea'],
  5632. $this->getSocketClient(),
  5633. $idVisit,
  5634. 'Preventivo'
  5635. );
  5636. $actions = DB::getQueryLog();
  5637. $idac = $this->functionsController->registerActivity(
  5638. $form['linea'],
  5639. 'S002V01M10GMPR',
  5640. 'S002V01F11RVTP',
  5641. 'S002V01P01REVI',
  5642. 'Actualización',
  5643. "El usuario $userName (" . $usr->USUA_IDUS . ") $action atender la visita preventiva #$idVisit.",
  5644. $idUser,
  5645. $nowStr,
  5646. 'S002V01S02AOTR'
  5647. );
  5648. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  5649. // Confirmar transacción
  5650. DB::commit();
  5651. return $this->responseController->makeResponse(false, 'EXITO');
  5652. } catch (\Exception $e) {
  5653. // Revertir transacción en caso de error
  5654. DB::rollBack();
  5655. $actions = DB::getQueryLog();
  5656. $now = $this->functionsController->now();
  5657. $nowStr = $now->toDateTimeString();
  5658. $userName = $this->functionsController->joinName($usr->USUA_NOMB ?? '', $usr->USUA_APPA ?? '', $usr->USUA_APMA ?? '');
  5659. $idac = $this->functionsController->registerActivity(
  5660. $form['linea'],
  5661. 'S002V01M10GMPR',
  5662. 'S002V01F11RVTP',
  5663. 'S002V01P01REVI',
  5664. 'Error',
  5665. "Error al procesar la respuesta de la visita preventiva #$idVisit: " . $e->getMessage(),
  5666. $idUser,
  5667. $nowStr,
  5668. 'S002V01S02AOTR'
  5669. );
  5670. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  5671. return $this->responseController->makeResponse(
  5672. true,
  5673. 'Ocurrió un error al procesar la respuesta de la visita.',
  5674. ['error' => $e->getMessage()],
  5675. 500
  5676. );
  5677. }
  5678. }
  5679. private function generateSignatureImageName()
  5680. {
  5681. $upperLetters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
  5682. $lowerLetters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
  5683. $numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
  5684. $fileName = "";
  5685. for ($i = 0; $i < 40; $i++) {
  5686. $charTypeSelector = rand(0, 2);
  5687. $selectedChar = "";
  5688. switch ($charTypeSelector) {
  5689. case 0:
  5690. $charSelector = rand(0, count($upperLetters) - 1);
  5691. $selectedChar = $upperLetters[$charSelector];
  5692. break;
  5693. case 1:
  5694. $charSelector = rand(0, count($lowerLetters) - 1);
  5695. $selectedChar = $lowerLetters[$charSelector];
  5696. break;
  5697. case 2:
  5698. $charSelector = rand(0, count($numbers) - 1);
  5699. $selectedChar = $numbers[$charSelector];
  5700. break;
  5701. }
  5702. $selectedChar = empty($selectedChar) ? 'A' : $selectedChar;
  5703. $fileName .= $selectedChar;
  5704. }
  5705. return $fileName . '.png';
  5706. }
  5707. }