PreventiveMaintenanceController.php 272 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656
  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. Log::info($request->all());
  1920. DB::enableQueryLog();
  1921. $validator = Validator::make($request->all(), [
  1922. 'id_user' => 'required|string',
  1923. 'linea' => 'required|integer',
  1924. 'equipment' => 'required|string',
  1925. 'resources' => 'required|json',
  1926. 'comments' => 'required|string|min:35',
  1927. 'staff' => 'required|json',
  1928. 'priority' => 'required|integer',
  1929. 'description' => 'required|string',
  1930. 'estimated' => 'required|numeric',
  1931. 'clasification' => 'required|string'
  1932. ]);
  1933. if ($validator->fails()) {
  1934. return $this->responseController->makeResponse(
  1935. true,
  1936. "Se encontraron uno o más errores.",
  1937. $this->responseController->makeErrors(
  1938. $validator->errors()->messages()
  1939. ),
  1940. 401
  1941. );
  1942. }
  1943. $form = $request->all();
  1944. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1945. if (!$idUser) {
  1946. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1947. }
  1948. $usr = DB::table('S002V01TUSUA')->where([
  1949. ['USUA_NULI', '=', $form['linea']],
  1950. ['USUA_IDUS', '=', $idUser]
  1951. ])->first();
  1952. if (is_null($usr)) {
  1953. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1954. }
  1955. $equipmentCode = $this->encryptionController->decrypt($form['equipment']);
  1956. if (!$equipmentCode) {
  1957. return $this->responseController->makeResponse(true, 'El código del equipamiento relacionado no fue encriptado correctamente.', [], 400);
  1958. }
  1959. $equipment = DB::table('S002V01TEQUI')->where([
  1960. ['EQUI_NULI', '=', $form['linea']],
  1961. ['EQUI_COEQ', '=', $equipmentCode]
  1962. ])->first();
  1963. if (is_null($equipment)) {
  1964. return $this->responseController->makeResponse(true, 'El equipamiento seleccionado no existe.', [], 404);
  1965. }
  1966. $staffArr = json_decode($form['staff'], true);
  1967. if (empty($staffArr)) {
  1968. return $this->responseController->makeResponse(true, 'El JSON de personal está vacío.', [], 400);
  1969. }
  1970. $staff = [];
  1971. foreach ($staffArr as $val) {
  1972. if (!array_key_exists('TYPE', $val) && !array_key_exists('ID', $val)) {
  1973. return $this->responseController->makeResponse(true, "El arreglo del personal no tiene un formato válido.", [], 400);
  1974. }
  1975. $typeStr = '';
  1976. if ($val['TYPE'] == 'EQ') {
  1977. $typeStr = 'equipo de trabajo';
  1978. } else if ($val['TYPE'] == 'SU') {
  1979. $typeStr = 'subcontratista';
  1980. } else if ($val['TYPE'] == 'EM') {
  1981. $typeStr = 'empleado';
  1982. }
  1983. $key = $this->encryptionController->decrypt($val['ID']);
  1984. if (!$key) {
  1985. return $this->responseController->makeResponse(true, "El ID del $typeStr no fue encriptado correctamente.", [], 400);
  1986. }
  1987. $res = null;
  1988. if ($val['TYPE'] == 'EQ') {
  1989. $res = DB::table('S002V01TEQMA')->where([
  1990. ['EQMA_NULI', '=', $form['linea']],
  1991. ['EQMA_IDEQ', '=', $key],
  1992. ])->first();
  1993. } else if ($val['TYPE'] == 'SU') {
  1994. $res = DB::table('S002V01TPESU')->where([
  1995. ['PESU_NULI', '=', $form['linea']],
  1996. ['PESU_IDPS', '=', $key],
  1997. ])->first();
  1998. } else if ($val['TYPE'] == 'EM') {
  1999. $res = DB::table('S002V01TPERS')->where([
  2000. ['PERS_NULI', '=', $form['linea']],
  2001. ['PERS_IDPE', '=', $key],
  2002. ])->first();
  2003. }
  2004. if (is_null($res)) {
  2005. return $this->responseController->makeResponse(true, "El $typeStr no está registrado.", [], 400);
  2006. }
  2007. $staff[] = [
  2008. 'ID' => $key,
  2009. 'TYPE' => $val['TYPE']
  2010. ];
  2011. }
  2012. $resources = json_decode($form['resources'], true);
  2013. if (empty($resources)) {
  2014. return $this->responseController->makeResponse(true, 'El JSON de recursos tiene un formato inválido.', [], 400);
  2015. }
  2016. $commentsArr = [
  2017. 'COMENTARIO_REGISTRO' => $form['comments']
  2018. ];
  2019. $now = $this->functionsController->now();
  2020. $nowStr = $now->toDateTimeString();
  2021. $statusHistoryStr = json_encode([
  2022. [
  2023. 'FECHA' => $nowStr,
  2024. 'ESTADO' => 'PE',
  2025. 'USUARIO' => $idUser,
  2026. ]
  2027. ]);
  2028. $commentsStr = json_encode($commentsArr);
  2029. $staffStr = json_encode($staff);
  2030. $idVisit = DB::table('S002V01TRVTN')->insertGetId([
  2031. 'RVTN_NULI' => $form['linea'],
  2032. 'RVTN_EQRE' => $equipmentCode,
  2033. 'RVTN_DEIN' => $form['description'],
  2034. 'RVTN_TIAC' => 'M',
  2035. 'RVTN_PEIN' => $staffStr,
  2036. 'RVTN_MAUT' => $form['resources'],
  2037. 'RVTN_PRIO' => $form['priority'],
  2038. 'RVTN_TESO' => $form['estimated'],
  2039. 'RVTN_CLAS' => $form['clasification'],
  2040. 'RVTN_ESTA' => 'PE',
  2041. 'RVTN_HIES' => $statusHistoryStr,
  2042. 'RVTN_COME' => $commentsStr,
  2043. 'RVTN_USRE' => $idUser,
  2044. 'RVTN_FERE' => $nowStr,
  2045. ]);
  2046. $actions = DB::getQueryLog();
  2047. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2048. $idac = $this->functionsController->registerActivity(
  2049. $form['linea'],
  2050. 'S002V01M10GMPR',
  2051. 'S002V01F11RVTP',
  2052. 'S002V01P01REVI',
  2053. 'Registro',
  2054. "El usuario $name (" . $usr->USUA_IDUS . ") registró la visita no programada #$idVisit.",
  2055. $idUser,
  2056. $nowStr,
  2057. 'S002V01S02AOTR'
  2058. );
  2059. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2060. return $this->responseController->makeResponse(false, 'EXITO.');
  2061. }
  2062. // Visitas técnicas no programadas (Preventivas)
  2063. public function getUnprogrammedVisits($idUser, $line)
  2064. {
  2065. DB::enableQueryLog();
  2066. $idUser = $this->encryptionController->decrypt($idUser);
  2067. if (!$idUser) {
  2068. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  2069. }
  2070. $usr = DB::table('S002V01TUSUA')->where([
  2071. ['USUA_NULI', '=', $line],
  2072. ['USUA_IDUS', '=', $idUser],
  2073. ])->first();
  2074. if (is_null($usr)) {
  2075. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  2076. }
  2077. $visits = DB::table('S002V01TRVTN')->select([
  2078. 'RVTN_IDVI AS IDVISITA',
  2079. 'RVTN_EQRE AS EQUIPAMIENTO',
  2080. 'EQUI_TIPO AS TIPO_EQUIPAMIENTO',
  2081. 'EQUI_MODE AS MODELO_EQUIPAMIENTO',
  2082. 'EQUI_IDEQ AS ID_EQUIPAMIENTO',
  2083. 'RVTN_ESTA AS ESTADO',
  2084. 'RVTN_USRE AS USRREG',
  2085. 'RVTN_FERE AS FECREG',
  2086. 'RVTN_FEMO AS FECMOD',
  2087. 'RVTN_PRIO AS PRIORIDAD',
  2088. 'RVTN_DEIN AS DESCRIPCION',
  2089. 'RVTN_TIAC AS TIPO_ACTIVACION',
  2090. 'usrReg.USUA_NOMB AS USRREG_NOMB',
  2091. 'usrReg.USUA_APPA AS USRREG_APPA',
  2092. 'usrReg.USUA_APMA AS USRREG_APMA'
  2093. ])->where('RVTN_NULI', '=', $line)
  2094. ->join('S002V01TEQUI', 'EQUI_COEQ', '=', 'RVTN_EQRE')
  2095. ->leftJoin('S002V01TUSUA as usrReg', function ($join) {
  2096. $join->on('usrReg.USUA_IDUS', '=', 'S002V01TRVTN.RVTN_USRE')
  2097. ->on('usrReg.USUA_NULI', '=', 'S002V01TRVTN.RVTN_NULI');
  2098. })
  2099. ->orderBy('RVTN_IDVI', 'desc')->get()->all();
  2100. foreach ($visits as $visit) {
  2101. $visit->IDVISITA = $this->encryptionController->encrypt($visit->IDVISITA);
  2102. $visit->EQUIPAMIENTO = $this->encryptionController->encrypt($visit->EQUIPAMIENTO);
  2103. $visit->ID_EQUIPAMIENTO = $this->encryptionController->encrypt($visit->ID_EQUIPAMIENTO);
  2104. $nameReg = $this->functionsController->joinName($visit->USRREG_NOMB, $visit->USRREG_APPA, $visit->USRREG_APMA);
  2105. if (!empty($nameReg)) {
  2106. $visit->USRREG = trim((string) $nameReg) . " (" . $visit->USRREG . ")";
  2107. }
  2108. unset($visit->USRREG_NOMB, $visit->USRREG_APPA, $visit->USRREG_APMA);
  2109. }
  2110. $now = $this->functionsController->now();
  2111. $nowStr = $now->toDateTimeString();
  2112. $actions = DB::getQueryLog();
  2113. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2114. $idac = $this->functionsController->registerActivity(
  2115. $line,
  2116. 'S002V01M10GMPR',
  2117. 'S002V01F11RVTP',
  2118. 'S002V01P02COVI',
  2119. 'Consulta',
  2120. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la lista de visitas no programadas.",
  2121. $idUser,
  2122. $nowStr,
  2123. 'S002V01S02AOTR'
  2124. );
  2125. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2126. return $this->responseController->makeResponse(false, 'EXITO.', $visits);
  2127. }
  2128. // Visitas técnicas no programadas (Preventivas)
  2129. public function getUnprogrammedVisit($idVisit, $idUser, $line)
  2130. {
  2131. DB::enableQueryLog();
  2132. $idUser = $this->encryptionController->decrypt($idUser);
  2133. if (!$idUser) {
  2134. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  2135. }
  2136. $usr = DB::table('S002V01TUSUA')->where([
  2137. ['USUA_IDUS', '=', $idUser],
  2138. ['USUA_NULI', '=', $line]
  2139. ])->first();
  2140. if (is_null($usr)) {
  2141. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  2142. }
  2143. $idVisit = $this->encryptionController->decrypt($idVisit);
  2144. if (!$idVisit) {
  2145. return $this->responseController->makeResponse(true, 'El ID de la visita solicitada no está encriptado correctamente.', [], 400);
  2146. }
  2147. $visit = DB::table('S002V01TRVTN')->select([
  2148. 'RVTN_IDVI AS IDVISITA',
  2149. 'RVTN_EQRE AS EQUIPAMIENTO',
  2150. 'EQUI_TIPO AS TIPO_EQUIPAMIENTO',
  2151. 'EQUI_MODE AS MODELO_EQUIPAMIENTO',
  2152. 'EQUI_IDEQ AS ID_EQUIPAMIENTO',
  2153. 'RVTN_PEIN AS PERSONAL',
  2154. 'RVTN_MAUT AS MATERIAL',
  2155. 'RVTN_COME AS COMENTARIOS',
  2156. 'RVTN_ESTA AS ESTADO',
  2157. 'RVTN_USRE AS USRREG',
  2158. 'RVTN_FERE AS FECREG',
  2159. 'RVTN_UARE AS USAURE',
  2160. 'RVTN_FARE AS FEAURE',
  2161. 'RVTN_USCA AS USRCAN',
  2162. 'RVTN_FECA AS FECCAN',
  2163. 'RVTN_USFI AS USUFIN',
  2164. 'RVTN_FEFI AS FECFIN',
  2165. 'RVTN_USMO AS USRMOD',
  2166. 'RVTN_FEMO AS FECMOD',
  2167. ])->where([
  2168. ['RVTN_IDVI', '=', $idVisit],
  2169. ['RVTN_NULI', '=', $line]
  2170. ])->join('S002V01TEQUI', 'EQUI_COEQ', '=', 'RVTN_EQRE')->first();
  2171. if (is_null($visit)) {
  2172. return $this->responseController->makeResponse(true, 'La visita consultada no existe.', [], 404);
  2173. }
  2174. $visit->IDVISITA = $this->encryptionController->encrypt($visit->IDVISITA);
  2175. $visit->EQUIPAMIENTO = $this->encryptionController->encrypt($visit->EQUIPAMIENTO);
  2176. $visit->ID_EQUIPAMIENTO = $this->encryptionController->encrypt($visit->ID_EQUIPAMIENTO);
  2177. $staffFn = [];
  2178. $staffArr = json_decode($visit->PERSONAL, true);
  2179. foreach ($staffArr as $item) {
  2180. $name = '';
  2181. if ($item['TYPE'] == 'EQ') {
  2182. $workTeam = DB::table('S002V01TEQMA')->where([
  2183. ['EQMA_NULI', '=', $line],
  2184. ['EQMA_IDEQ', '=', $item['ID']]
  2185. ])->first();
  2186. $name = $workTeam->EQMA_NOMB;
  2187. } else if ($item['TYPE'] == 'SU') {
  2188. $subcontratist = DB::table('S002V01TPESU')->where([
  2189. ['PESU_NULI', '=', $line],
  2190. ['PESU_IDPS', '=', $item['ID']]
  2191. ])->first();
  2192. $name = $subcontratist->PESU_RASO;
  2193. } else if ($item['TYPE'] == 'EM') {
  2194. $employee = DB::table('S002V01TPERS')
  2195. ->join('S002V01TUSUA', 'USUA_IDUS', '=', 'PERS_IDUS')->where([
  2196. ['PERS_NULI', '=', $line],
  2197. ['PERS_IDPE', '=', $item['ID']]
  2198. ])->first();
  2199. $name = $this->functionsController->joinName($employee->USUA_NOMB, $employee->USUA_APPA, $employee->USUA_APMA);
  2200. }
  2201. $staffFn[] = [
  2202. 'ID' => $this->encryptionController->encrypt($item['ID']),
  2203. 'TYPE' => $item['TYPE'],
  2204. 'NAME' => $name,
  2205. ];
  2206. }
  2207. $visit->PERSONAL = json_encode($staffFn);
  2208. $usrReg = DB::table('S002V01TUSUA')->where([
  2209. ['USUA_NULI', '=', $line],
  2210. ['USUA_IDUS', '=', $visit->USRREG],
  2211. ])->first();
  2212. $nameReg = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA) . " (" . $visit->USRREG . ")";
  2213. $visit->USRREG = $nameReg;
  2214. if (!is_null($visit->USAURE)) {
  2215. $usaure = DB::table('S002V01TUSUA')->where([
  2216. ['USUA_NULI', '=', $line],
  2217. ['USUA_IDUS', '=', $visit->USAURE],
  2218. ])->first();
  2219. $nameUre = $this->functionsController->joinName($usaure->USUA_NOMB, $usaure->USUA_APPA, $usaure->USUA_APMA) . " (" . $visit->USAURE . ")";
  2220. $visit->USAURE = $nameUre;
  2221. }
  2222. if (!is_null($visit->USRCAN)) {
  2223. $usrCan = DB::table('S002V01TUSUA')->where([
  2224. ['USUA_NULI', '=', $line],
  2225. ['USUA_IDUS', '=', $visit->USRCAN],
  2226. ])->first();
  2227. $nameCan = $this->functionsController->joinName($usrCan->USUA_NOMB, $usrCan->USUA_APPA, $usrCan->USUA_APMA) . " (" . $visit->USRCAN . ")";
  2228. $visit->USRCAN = $nameCan;
  2229. }
  2230. if (!is_null($visit->USUFIN)) {
  2231. $usrFin = DB::table('S002V01TUSUA')->where([
  2232. ['USUA_NULI', '=', $line],
  2233. ['USUA_IDUS', '=', $visit->USUFIN],
  2234. ])->first();
  2235. $nameFin = $this->functionsController->joinName($usrFin->USUA_NOMB, $usrFin->USUA_APPA, $usrFin->USUA_APMA) . " (" . $visit->USUFIN . ")";
  2236. $visit->USUFIN = $nameFin;
  2237. }
  2238. if (!is_null($visit->USRMOD)) {
  2239. $usrMod = DB::table('S002V01TUSUA')->where([
  2240. ['USUA_NULI', '=', $line],
  2241. ['USUA_IDUS', '=', $visit->USRMOD],
  2242. ])->first();
  2243. $nameMod = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA) . " (" . $visit->USRMOD . ")";
  2244. $visit->USRMOD = $nameMod;
  2245. }
  2246. $now = $this->functionsController->now();
  2247. $nowStr = $now->toDateTimeString();
  2248. $actions = DB::getQueryLog();
  2249. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2250. $idac = $this->functionsController->registerActivity(
  2251. $line,
  2252. 'S002V01M10GMPR',
  2253. 'S002V01F11RVTP',
  2254. 'S002V01P02COVI',
  2255. 'Consulta',
  2256. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la visita no programada #$idVisit.",
  2257. $idUser,
  2258. $nowStr,
  2259. 'S002V01S02AOTR'
  2260. );
  2261. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  2262. return $this->responseController->makeResponse(false, 'EXITO.', $visit);
  2263. }
  2264. // Visitas técnicas no programadas (Preventivas)
  2265. public function updateVisitStatus(Request $request)
  2266. {
  2267. DB::enableQueryLog();
  2268. // RVTN_ESTA enum: 'PE','CA','RE','EL','VA','EP','CP','CE'
  2269. $validator = Validator::make($request->all(), [
  2270. 'id_user' => 'required|string',
  2271. 'linea' => 'required|integer',
  2272. 'id_visit' => 'required|string',
  2273. 'comments' => 'nullable|string|min:15',
  2274. 'status' => 'required|string|in:PE,CA,RE,EL,VA,EP,CP,CE'
  2275. ]);
  2276. if ($validator->fails()) {
  2277. return $this->responseController->makeResponse(
  2278. true,
  2279. "Se encontraron uno o más errores.",
  2280. $this->responseController->makeErrors(
  2281. $validator->errors()->messages()
  2282. ),
  2283. 401
  2284. );
  2285. }
  2286. $form = $request->all();
  2287. $idUser = $this->encryptionController->decrypt($form['id_user']);
  2288. if (!$idUser) {
  2289. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  2290. }
  2291. $usr = DB::table('S002V01TUSUA')->where([
  2292. ['USUA_NULI', '=', $form['linea']],
  2293. ['USUA_IDUS', '=', $idUser],
  2294. ])->first();
  2295. if (is_null($usr)) {
  2296. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  2297. }
  2298. $idVisit = $this->encryptionController->decrypt($form['id_visit']);
  2299. if (!$idVisit) {
  2300. return $this->responseController->makeResponse(true, 'El ID de la visita solicitada no está encriptado correctamente.', [], 400);
  2301. }
  2302. $visit = DB::table('S002V01TRVTN')->where([
  2303. ['RVTN_NULI', '=', $form['linea']],
  2304. ['RVTN_IDVI', '=', $idVisit],
  2305. ])->first();
  2306. if (is_null($visit)) {
  2307. return $this->responseController->makeResponse(true, 'La visita solicitada no existe.', [], 404);
  2308. }
  2309. $currentStatus = $visit->RVTN_ESTA;
  2310. $newStatus = $form['status'];
  2311. // Matriz de transiciones permitidas
  2312. $allowedTransitions = [
  2313. 'PE' => ['VA', 'RE', 'CA', 'EL'], // Pendiente
  2314. 'VA' => ['EP', 'CA', 'EL'], // Validado
  2315. 'EP' => ['CP', 'CA', 'EL'], // En proceso
  2316. 'CP' => ['CE'], // Cerrado pendiente
  2317. 'CE' => [], // Cerrado - Estado final
  2318. 'CA' => [], // Cancelado - Estado final
  2319. 'EL' => [], // Eliminado - Estado final
  2320. 'RE' => [] // Rechazado - Estado final
  2321. ];
  2322. // Validar transición permitida
  2323. $currentStatusTransitions = $allowedTransitions[$currentStatus] ?? [];
  2324. // Si el estado actual no está en la matriz, no permitir cambios
  2325. if (!array_key_exists($currentStatus, $allowedTransitions)) {
  2326. return $this->responseController->makeResponse(true, "El estado actual {$currentStatus} no permite cambios.", [], 400);
  2327. }
  2328. // Validar si la transición está permitida
  2329. if (!in_array($newStatus, $currentStatusTransitions)) {
  2330. $statusNames = [
  2331. 'PE' => 'Pendiente',
  2332. 'CA' => 'Cancelado',
  2333. 'RE' => 'Rechazado',
  2334. 'EL' => 'Eliminado',
  2335. 'VA' => 'Validado',
  2336. 'EP' => 'En Proceso',
  2337. 'CP' => 'Cerrado Pendiente',
  2338. 'CE' => 'Cerrado'
  2339. ];
  2340. $currentStatusName = $statusNames[$currentStatus] ?? $currentStatus;
  2341. $newStatusName = $statusNames[$newStatus] ?? $newStatus;
  2342. return $this->responseController->makeResponse(true, "No se puede cambiar de {$currentStatusName} a {$newStatusName}. Transición no permitida.", [], 400);
  2343. }
  2344. // Obtener y actualizar historial de estados
  2345. $statusHistoryArr = json_decode($visit->RVTN_HIES, true);
  2346. if (!is_array($statusHistoryArr)) {
  2347. $statusHistoryArr = [];
  2348. }
  2349. $now = $this->functionsController->now();
  2350. $nowStr = $now->toDateTimeString();
  2351. // Agregar nuevo estado al historial
  2352. $statusHistoryArr[] = [
  2353. 'FECHA' => $nowStr,
  2354. 'ESTADO' => $newStatus,
  2355. 'USUARIO' => $idUser
  2356. ];
  2357. $statusHistoryStr = json_encode($statusHistoryArr);
  2358. // Preparar actualización
  2359. $updateArr = [
  2360. 'RVTN_ESTA' => $newStatus,
  2361. 'RVTN_HIES' => $statusHistoryStr,
  2362. 'RVTN_USMO' => $idUser,
  2363. 'RVTN_FEMO' => $nowStr,
  2364. ];
  2365. // Asignar campos específicos según el estado
  2366. if ($newStatus == 'PE') {
  2367. // Pendiente - Ya registrado en creación
  2368. } elseif ($newStatus == 'CA') {
  2369. $updateArr['RVTN_USCA'] = $idUser;
  2370. $updateArr['RVTN_FECA'] = $nowStr;
  2371. // Registrar comentario de cancelación
  2372. if (!empty($form['comments'])) {
  2373. $commentsArr = json_decode($visit->RVTN_COME, true);
  2374. if (!is_array($commentsArr)) {
  2375. $commentsArr = [];
  2376. }
  2377. $commentsArr['COMENTARIO_CANCELACION'] = [
  2378. 'ID' => $idUser,
  2379. 'COMENTARIO' => $form['comments'],
  2380. 'FECHA' => $nowStr
  2381. ];
  2382. $updateArr['RVTN_COME'] = json_encode($commentsArr);
  2383. }
  2384. } elseif ($newStatus == 'RE') {
  2385. $updateArr['RVTN_UARE'] = $idUser;
  2386. $updateArr['RVTN_FARE'] = $nowStr;
  2387. // Registrar comentario de rechazo
  2388. if (!empty($form['comments'])) {
  2389. $commentsArr = json_decode($visit->RVTN_COME, true);
  2390. if (!is_array($commentsArr)) {
  2391. $commentsArr = [];
  2392. }
  2393. $commentsArr['COMENTARIO_RECHAZO'] = [
  2394. 'ID' => $idUser,
  2395. 'COMENTARIO' => $form['comments'],
  2396. 'FECHA' => $nowStr
  2397. ];
  2398. $updateArr['RVTN_COME'] = json_encode($commentsArr);
  2399. }
  2400. } elseif ($newStatus == 'EL') {
  2401. // Eliminado - podría necesitar campos específicos
  2402. } elseif (in_array($newStatus, ['VA', 'EP', 'CP', 'CE'])) {
  2403. $updateArr['RVTN_UARE'] = $idUser;
  2404. $updateArr['RVTN_FARE'] = $nowStr;
  2405. if ($newStatus == 'CE') {
  2406. $updateArr['RVTN_USFI'] = $idUser;
  2407. $updateArr['RVTN_FEFI'] = $nowStr;
  2408. // Calcular tiempo de duración de la intervención
  2409. $epDate = null;
  2410. $ceDate = $nowStr;
  2411. foreach ($statusHistoryArr as $historyItem) {
  2412. if ($historyItem['ESTADO'] === 'EP') {
  2413. $epDate = $historyItem['FECHA'];
  2414. }
  2415. }
  2416. if ($epDate) {
  2417. $startTime = new \Carbon\Carbon($epDate);
  2418. $endTime = new \Carbon\Carbon($ceDate);
  2419. $durationHours = $endTime->diffInMinutes($startTime) / 60.0;
  2420. $updateArr['RVTN_DTIN'] = $durationHours;
  2421. }
  2422. }
  2423. }
  2424. DB::table('S002V01TRVTN')->where([
  2425. ['RVTN_NULI', '=', $form['linea']],
  2426. ['RVTN_IDVI', '=', $idVisit],
  2427. ])->update($updateArr);
  2428. // Obtener audiencia para notificaciones (operarios y regulador)
  2429. $audience = [];
  2430. // Obtener operarios que aceptaron la invitación (del último estado VA)
  2431. $statusHistoryForAudience = json_decode($visit->RVTN_HIES, true);
  2432. if (is_array($statusHistoryForAudience)) {
  2433. // Buscar el último estado VA
  2434. $lastVAStatus = null;
  2435. for ($i = count($statusHistoryForAudience) - 1; $i >= 0; $i--) {
  2436. if ($statusHistoryForAudience[$i]['ESTADO'] === 'VA') {
  2437. $lastVAStatus = $statusHistoryForAudience[$i];
  2438. break;
  2439. }
  2440. }
  2441. if ($lastVAStatus && isset($lastVAStatus['ATENCION']) && is_array($lastVAStatus['ATENCION'])) {
  2442. foreach ($lastVAStatus['ATENCION'] as $item) {
  2443. if (($item['RESPUESTA'] ?? '') === 'A') {
  2444. $audience[] = $item['ID'];
  2445. }
  2446. }
  2447. }
  2448. }
  2449. // Agregar regulador (quien registró la visita)
  2450. if (!empty($visit->RVTN_USRE) && !in_array($visit->RVTN_USRE, $audience)) {
  2451. $audience[] = $visit->RVTN_USRE;
  2452. }
  2453. // Preparar mensajes de notificación según el estado
  2454. $statusMessages = [
  2455. 'PE' => 'Pendiente',
  2456. 'CA' => 'Cancelación',
  2457. 'RE' => 'Rechazo',
  2458. 'EL' => 'Eliminación',
  2459. 'VA' => 'Validación',
  2460. 'EP' => 'Inicio',
  2461. 'CP' => 'Cerrado Pendiente',
  2462. 'CE' => 'Cierre'
  2463. ];
  2464. $statusAction = $statusMessages[$newStatus] ?? 'Cambio de estado';
  2465. $notificationTitle = "Visita Técnica No Programada #$idVisit";
  2466. $notificationMessage = "La visita técnica no programada #$idVisit ha sido actualizada a estado: {$statusAction}.";
  2467. // Notificación general para todos los cambios de estado
  2468. if (in_array($newStatus, ['VA', 'RE', 'CA', 'EL', 'EP', 'CE', 'CP']) && !empty($audience)) {
  2469. $this->notificationsController->emitNotification(
  2470. 'S002V01M10GMPR',
  2471. $notificationTitle,
  2472. $notificationMessage,
  2473. [[
  2474. 'BOTON' => 'Ver detalles',
  2475. 'FUNCION' => 'openPreventiveWorkOrderDetails',
  2476. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit)])
  2477. ], [
  2478. 'BOTON' => 'Ir al módulo',
  2479. 'FUNCION' => 'openModule',
  2480. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMPR/AOTR/RVTP')])
  2481. ]],
  2482. $audience,
  2483. $idUser,
  2484. $form['linea'],
  2485. $this->getSocketClient(),
  2486. $idVisit,
  2487. 'Preventivo'
  2488. );
  2489. }
  2490. $actions = DB::getQueryLog();
  2491. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2492. $idac = $this->functionsController->registerActivity(
  2493. $form['linea'],
  2494. 'S002V01M10GMPR',
  2495. 'S002V01F11RVTP',
  2496. 'S002V01P01REVI',
  2497. 'Actualización',
  2498. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó la visita no programada #$idVisit a estado {$statusAction}.",
  2499. $idUser,
  2500. $nowStr,
  2501. 'S002V01S02AOTR'
  2502. );
  2503. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2504. return $this->responseController->makeResponse(false, 'EXITO.');
  2505. }
  2506. // Visitas técnicas no programadas (Preventivas)
  2507. // Registrar comentario de finalización de un operario en estado CP
  2508. public function registerOperatorClosingComment(Request $request)
  2509. {
  2510. DB::enableQueryLog();
  2511. $validator = Validator::make($request->all(), [
  2512. 'id_user' => 'required|string',
  2513. 'linea' => 'required|integer',
  2514. 'id_visit' => 'required|string',
  2515. 'comment' => 'required|string|min:15'
  2516. ]);
  2517. if ($validator->fails()) {
  2518. return $this->responseController->makeResponse(
  2519. true,
  2520. "Se encontraron uno o más errores.",
  2521. $this->responseController->makeErrors(
  2522. $validator->errors()->messages()
  2523. ),
  2524. 401
  2525. );
  2526. }
  2527. $form = $request->all();
  2528. $idUser = $this->encryptionController->decrypt($form['id_user']);
  2529. if (!$idUser) {
  2530. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  2531. }
  2532. $usr = DB::table('S002V01TUSUA')->where([
  2533. ['USUA_NULI', '=', $form['linea']],
  2534. ['USUA_IDUS', '=', $idUser],
  2535. ])->first();
  2536. if (is_null($usr)) {
  2537. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  2538. }
  2539. $idVisit = $this->encryptionController->decrypt($form['id_visit']);
  2540. if (!$idVisit) {
  2541. return $this->responseController->makeResponse(true, 'El ID de la visita solicitada no está encriptado correctamente.', [], 400);
  2542. }
  2543. $visit = DB::table('S002V01TRVTN')->where([
  2544. ['RVTN_NULI', '=', $form['linea']],
  2545. ['RVTN_IDVI', '=', $idVisit],
  2546. ])->first();
  2547. if (is_null($visit)) {
  2548. return $this->responseController->makeResponse(true, 'La visita solicitada no existe.', [], 404);
  2549. }
  2550. // Validar que la visita está en estado EP o CP
  2551. if (!in_array($visit->RVTN_ESTA, ['EP', 'CP'])) {
  2552. 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);
  2553. }
  2554. // Iniciar transacción
  2555. DB::beginTransaction();
  2556. try {
  2557. // Obtener historial de estados
  2558. $statusHistoryArr = json_decode($visit->RVTN_HIES, true);
  2559. if (!is_array($statusHistoryArr)) {
  2560. DB::rollBack();
  2561. return $this->responseController->makeResponse(true, 'No se encontró un historial de estados válido para la visita.', [], 500);
  2562. }
  2563. // Buscar el último estado VA para obtener la lista de operarios que aceptaron
  2564. $lastVAStatus = null;
  2565. $acceptedOperators = [];
  2566. for ($i = count($statusHistoryArr) - 1; $i >= 0; $i--) {
  2567. if ($statusHistoryArr[$i]['ESTADO'] === 'VA') {
  2568. $lastVAStatus = $statusHistoryArr[$i];
  2569. break;
  2570. }
  2571. }
  2572. if (!$lastVAStatus || !isset($lastVAStatus['ATENCION']) || !is_array($lastVAStatus['ATENCION'])) {
  2573. DB::rollBack();
  2574. return $this->responseController->makeResponse(true, 'No se encontró información de operarios asignados a la visita.', [], 404);
  2575. }
  2576. // Obtener lista de operarios que aceptaron
  2577. foreach ($lastVAStatus['ATENCION'] as $item) {
  2578. if (($item['RESPUESTA'] ?? '') === 'A') {
  2579. $acceptedOperators[] = $item['ID'];
  2580. }
  2581. }
  2582. // Validar que el usuario es uno de los operarios que aceptó
  2583. if (!in_array($idUser, $acceptedOperators)) {
  2584. DB::rollBack();
  2585. 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);
  2586. }
  2587. // Buscar el objeto CP en el historial o crearlo si no existe
  2588. $cpStatusIndex = null;
  2589. $cpStatus = null;
  2590. $isFirstComment = false;
  2591. for ($i = count($statusHistoryArr) - 1; $i >= 0; $i--) {
  2592. if ($statusHistoryArr[$i]['ESTADO'] === 'CP') {
  2593. $cpStatusIndex = $i;
  2594. $cpStatus = $statusHistoryArr[$i];
  2595. break;
  2596. }
  2597. }
  2598. // Si no existe objeto CP, crear uno nuevo y cambiar estado de EP a CP
  2599. if ($cpStatusIndex === null) {
  2600. $now = $this->functionsController->now();
  2601. $nowStr = $now->toDateTimeString();
  2602. $isFirstComment = true;
  2603. $cpStatus = [
  2604. 'FECHA' => $nowStr,
  2605. 'ESTADO' => 'CP',
  2606. 'USUARIO' => $idUser,
  2607. 'COMENTARIOS_OPERARIOS' => []
  2608. ];
  2609. $cpStatusIndex = count($statusHistoryArr);
  2610. $statusHistoryArr[] = $cpStatus;
  2611. } else {
  2612. // Asegurar que existe el array de comentarios
  2613. if (!isset($cpStatus['COMENTARIOS_OPERARIOS']) || !is_array($cpStatus['COMENTARIOS_OPERARIOS'])) {
  2614. $cpStatus['COMENTARIOS_OPERARIOS'] = [];
  2615. }
  2616. }
  2617. // Obtener comentarios existentes para validación
  2618. $existingCommentsArr = json_decode($visit->RVTN_COME, true);
  2619. if (!is_array($existingCommentsArr)) {
  2620. $existingCommentsArr = [];
  2621. }
  2622. // Validar que el operario no haya registrado comentario ya
  2623. if (isset($existingCommentsArr['COMENTARIOS_OPERADORES']) && is_array($existingCommentsArr['COMENTARIOS_OPERADORES'])) {
  2624. foreach ($existingCommentsArr['COMENTARIOS_OPERADORES'] as $existingComment) {
  2625. if (isset($existingComment['ID']) && $existingComment['ID'] === $idUser) {
  2626. DB::rollBack();
  2627. return $this->responseController->makeResponse(true, 'Ya ha registrado su comentario de finalización para esta visita.', [], 400);
  2628. }
  2629. }
  2630. }
  2631. // Registrar el comentario del operario
  2632. $now = $this->functionsController->now();
  2633. $nowStr = $now->toDateTimeString();
  2634. $operatorName = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2635. $cpStatus['COMENTARIOS_OPERARIOS'][] = [
  2636. 'ID' => $idUser,
  2637. 'COMENTARIO' => $form['comment'],
  2638. 'FECHA' => $nowStr
  2639. ];
  2640. // Actualizar el objeto en el historial
  2641. $statusHistoryArr[$cpStatusIndex] = $cpStatus;
  2642. $statusHistoryStr = json_encode($statusHistoryArr);
  2643. // Contar comentarios registrados vs total de operarios
  2644. $updatedCommentsArr = json_decode($visit->RVTN_COME, true);
  2645. $commentsRegistered = isset($updatedCommentsArr['COMENTARIOS_OPERADORES']) ? count($updatedCommentsArr['COMENTARIOS_OPERADORES']) : 0;
  2646. $totalOperators = count($acceptedOperators);
  2647. $isComplete = $commentsRegistered >= $totalOperators;
  2648. // Preparar actualización de la visita
  2649. $updateArr = [
  2650. 'RVTN_HIES' => $statusHistoryStr,
  2651. 'RVTN_USMO' => $idUser,
  2652. 'RVTN_FEMO' => $nowStr,
  2653. ];
  2654. // Si es el primer comentario, cambiar estado de EP a CP
  2655. if ($isFirstComment) {
  2656. $updateArr['RVTN_ESTA'] = 'CP';
  2657. $updateArr['RVTN_UARE'] = $idUser;
  2658. $updateArr['RVTN_FARE'] = $nowStr;
  2659. }
  2660. // Actualizar comentarios de operadores
  2661. $commentsArr = json_decode($visit->RVTN_COME, true);
  2662. if (!is_array($commentsArr)) {
  2663. $commentsArr = [];
  2664. }
  2665. // Inicializar array de comentarios de operadores si no existe
  2666. if (!isset($commentsArr['COMENTARIOS_OPERADORES']) || !is_array($commentsArr['COMENTARIOS_OPERADORES'])) {
  2667. $commentsArr['COMENTARIOS_OPERADORES'] = [];
  2668. }
  2669. // Agregar comentario del operario
  2670. $commentsArr['COMENTARIOS_OPERADORES'][] = [
  2671. 'ID' => $idUser,
  2672. 'COMENTARIO' => $form['comment'],
  2673. 'FECHA' => $nowStr
  2674. ];
  2675. $commentsStr = json_encode($commentsArr);
  2676. $updateArr['RVTN_COME'] = $commentsStr;
  2677. // Actualizar la visita
  2678. DB::table('S002V01TRVTN')->where([
  2679. ['RVTN_NULI', '=', $form['linea']],
  2680. ['RVTN_IDVI', '=', $idVisit],
  2681. ])->update($updateArr);
  2682. // Obtener regulador para notificaciones
  2683. $regulatorId = $visit->RVTN_USRE;
  2684. $regulatorAudience = !empty($regulatorId) ? [$regulatorId] : [];
  2685. // Obtener audiencia completa (operarios y regulador) para notificación de cambio de estado
  2686. $audience = [];
  2687. if (!empty($lastVAStatus['ATENCION']) && is_array($lastVAStatus['ATENCION'])) {
  2688. foreach ($lastVAStatus['ATENCION'] as $item) {
  2689. if (($item['RESPUESTA'] ?? '') === 'A') {
  2690. $audience[] = $item['ID'];
  2691. }
  2692. }
  2693. }
  2694. if (!empty($regulatorId) && !in_array($regulatorId, $audience)) {
  2695. $audience[] = $regulatorId;
  2696. }
  2697. // Si es el primer comentario (cambio EP -> CP), notificar a todos
  2698. if ($isFirstComment && !empty($audience)) {
  2699. $notificationTitle = "Visita Técnica No Programada #$idVisit";
  2700. $notificationMessage = "La visita técnica no programada #$idVisit ha pasado a estado Cerrado Pendiente.";
  2701. $this->notificationsController->emitNotification(
  2702. 'S002V01M10GMPR',
  2703. $notificationTitle,
  2704. $notificationMessage,
  2705. [[
  2706. 'BOTON' => 'Ver detalles',
  2707. 'FUNCION' => 'openPreventiveWorkOrderDetails',
  2708. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit)])
  2709. ], [
  2710. 'BOTON' => 'Ir al módulo',
  2711. 'FUNCION' => 'openModule',
  2712. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMPR/AOTR/RVTP')])
  2713. ]],
  2714. $audience,
  2715. $idUser,
  2716. $form['linea'],
  2717. $this->getSocketClient(),
  2718. $idVisit,
  2719. 'Preventivo'
  2720. );
  2721. }
  2722. // Notificar al regulador por cada comentario registrado
  2723. if (!empty($regulatorAudience)) {
  2724. $notificationTitle = "Visita Técnica No Programada #$idVisit";
  2725. $notificationMessage = "El usuario $operatorName ($idUser) registró comentario de finalización de la visita #$idVisit.";
  2726. $this->notificationsController->emitNotification(
  2727. 'S002V01M10GMPR',
  2728. $notificationTitle,
  2729. $notificationMessage,
  2730. [[
  2731. 'BOTON' => 'Ver detalles',
  2732. 'FUNCION' => 'openPreventiveWorkOrderDetails',
  2733. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit)])
  2734. ], [
  2735. 'BOTON' => 'Ir al módulo',
  2736. 'FUNCION' => 'openModule',
  2737. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMPR/AOTR/RVTP')])
  2738. ]],
  2739. $regulatorAudience,
  2740. $idUser,
  2741. $form['linea'],
  2742. $this->getSocketClient(),
  2743. $idVisit,
  2744. 'Preventivo'
  2745. );
  2746. }
  2747. // Si todos los comentarios están completos, enviar notificación final
  2748. if ($isComplete && !empty($regulatorAudience)) {
  2749. $finalNotificationTitle = "Visita Técnica No Programada #$idVisit";
  2750. $finalNotificationMessage = "La visita #$idVisit ya puede ser cerrada. Todos los operarios han registrado sus comentarios de finalización.";
  2751. $this->notificationsController->emitNotification(
  2752. 'S002V01M10GMPR',
  2753. $finalNotificationTitle,
  2754. $finalNotificationMessage,
  2755. [[
  2756. 'BOTON' => 'Cerrar visita',
  2757. 'FUNCION' => 'closePreventiveVisit',
  2758. 'PARAMETROS' => json_encode([
  2759. $this->encryptionController->encrypt($idVisit),
  2760. $this->encryptionController->encrypt($form['linea']),
  2761. $this->encryptionController->encrypt($regulatorId)
  2762. ])
  2763. ], [
  2764. 'BOTON' => 'Ver detalles',
  2765. 'FUNCION' => 'openPreventiveWorkOrderDetails',
  2766. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit)])
  2767. ], [
  2768. 'BOTON' => 'Ir al módulo',
  2769. 'FUNCION' => 'openModule',
  2770. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMPR/AOTR/RVTP')])
  2771. ]],
  2772. $regulatorAudience,
  2773. $idUser,
  2774. $form['linea'],
  2775. $this->getSocketClient(),
  2776. $idVisit,
  2777. 'Preventivo'
  2778. );
  2779. }
  2780. // Confirmar transacción
  2781. DB::commit();
  2782. $actions = DB::getQueryLog();
  2783. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2784. $idac = $this->functionsController->registerActivity(
  2785. $form['linea'],
  2786. 'S002V01M10GMPR',
  2787. 'S002V01F11RVTP',
  2788. 'S002V01P01REVI',
  2789. 'Actualización',
  2790. "El usuario $name (" . $usr->USUA_IDUS . ") registró comentario de finalización para la visita no programada #$idVisit.",
  2791. $idUser,
  2792. $nowStr,
  2793. 'S002V01S02AOTR'
  2794. );
  2795. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2796. return $this->responseController->makeResponse(false, 'EXITO.', [
  2797. 'comments_registered' => $commentsRegistered,
  2798. 'total_operators' => $totalOperators,
  2799. 'is_complete' => $isComplete
  2800. ]);
  2801. } catch (\Exception $e) {
  2802. // Revertir transacción en caso de error
  2803. DB::rollBack();
  2804. $actions = DB::getQueryLog();
  2805. $now = $this->functionsController->now();
  2806. $nowStr = $now->toDateTimeString();
  2807. $name = $this->functionsController->joinName($usr->USUA_NOMB ?? '', $usr->USUA_APPA ?? '', $usr->USUA_APMA ?? '');
  2808. $idac = $this->functionsController->registerActivity(
  2809. $form['linea'],
  2810. 'S002V01M10GMPR',
  2811. 'S002V01F11RVTP',
  2812. 'S002V01P01REVI',
  2813. 'Error',
  2814. "Error al registrar comentario de finalización para la visita no programada #$idVisit: " . $e->getMessage(),
  2815. $idUser,
  2816. $nowStr,
  2817. 'S002V01S02AOTR'
  2818. );
  2819. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2820. return $this->responseController->makeResponse(
  2821. true,
  2822. 'Ocurrió un error al registrar el comentario de finalización.',
  2823. ['error' => $e->getMessage()],
  2824. 500
  2825. );
  2826. }
  2827. }
  2828. // Visitas técnicas no programadas (Preventivas)
  2829. public function startPreventiveVisit(Request $request)
  2830. {
  2831. DB::enableQueryLog();
  2832. $validator = Validator::make($request->all(), [
  2833. 'id_user' => 'required|string',
  2834. 'linea' => 'required|integer',
  2835. 'id_visit' => 'required|string'
  2836. ]);
  2837. if ($validator->fails()) {
  2838. return $this->responseController->makeResponse(
  2839. true,
  2840. "Se encontraron uno o más errores.",
  2841. $this->responseController->makeErrors(
  2842. $validator->errors()->messages()
  2843. ),
  2844. 401
  2845. );
  2846. }
  2847. $form = $request->all();
  2848. $idUser = $this->encryptionController->decrypt($form['id_user']);
  2849. if (!$idUser) {
  2850. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  2851. }
  2852. $usr = DB::table('S002V01TUSUA')->where([
  2853. ['USUA_NULI', '=', $form['linea']],
  2854. ['USUA_IDUS', '=', $idUser]
  2855. ])->first();
  2856. if (is_null($usr)) {
  2857. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  2858. }
  2859. $idVisit = $this->encryptionController->decrypt($form['id_visit']);
  2860. if (!$idVisit) {
  2861. return $this->responseController->makeResponse(true, 'El ID de la visita no fue encriptado correctamente.', [], 400);
  2862. }
  2863. $visit = DB::table('S002V01TRVTN')->where([
  2864. ['RVTN_NULI', '=', $form['linea']],
  2865. ['RVTN_IDVI', '=', $idVisit]
  2866. ])->first();
  2867. if (is_null($visit)) {
  2868. return $this->responseController->makeResponse(true, 'La visita solicitada no existe.', [], 404);
  2869. }
  2870. $visitStates = [
  2871. 'PE' => 'Pendiente',
  2872. 'CA' => 'Cancelado',
  2873. 'RE' => 'Rechazado',
  2874. 'EL' => 'Eliminado',
  2875. 'VA' => 'Validado',
  2876. 'EP' => 'En Proceso',
  2877. 'CP' => 'Cerrado Pendiente',
  2878. 'CE' => 'Cerrado'
  2879. ];
  2880. if ($visit->RVTN_ESTA !== 'VA') {
  2881. $statusKey = $visit->RVTN_ESTA;
  2882. $statusName = array_key_exists($statusKey, $visitStates) ? $visitStates[$statusKey] : $statusKey;
  2883. return $this->responseController->makeResponse(true, "La visita está $statusName.", [], 401);
  2884. }
  2885. if ($visit->RVTN_USRE != $idUser) {
  2886. return $this->responseController->makeResponse(true, 'El usuario que solicitó la acción no tiene los permisos necesarios.', [], 401);
  2887. }
  2888. $statusHistoryArr = json_decode($visit->RVTN_HIES, true);
  2889. if (!is_array($statusHistoryArr)) {
  2890. $statusHistoryArr = [];
  2891. }
  2892. // Buscar objeto VA en el historial
  2893. // Si no existe pero RVTN_ESTA == 'VA', la visita está validada pero aún no tiene operarios asignados
  2894. $validatedHistoryFilt = array_filter($statusHistoryArr, function ($item) {
  2895. return array_key_exists('ESTADO', $item) && $item['ESTADO'] == 'VA';
  2896. });
  2897. $audience = [];
  2898. // Si existe el objeto VA en el historial, obtener los operarios aceptados
  2899. if (!empty($validatedHistoryFilt)) {
  2900. $validatedStatus = end($validatedHistoryFilt);
  2901. if (array_key_exists('ATENCION', $validatedStatus) && is_array($validatedStatus['ATENCION'])) {
  2902. foreach ($validatedStatus['ATENCION'] as $item) {
  2903. if (($item['RESPUESTA'] ?? '') === 'A') {
  2904. $audience[] = $item['ID'];
  2905. }
  2906. }
  2907. }
  2908. }
  2909. // Si no existe el objeto VA en el historial pero RVTN_ESTA == 'VA',
  2910. // la audiencia quedará vacía (no hay operarios asignados aún)
  2911. $now = $this->functionsController->now();
  2912. $nowStr = $now->toDateTimeString();
  2913. $statusHistoryArr[] = [
  2914. 'USUARIO' => $idUser,
  2915. 'ESTADO' => 'EP',
  2916. 'FECHA' => $nowStr
  2917. ];
  2918. $statusHistoryStr = json_encode($statusHistoryArr);
  2919. /*
  2920. $commentsArr = json_decode($visit->RVTN_COME, true);
  2921. if (!is_array($commentsArr)) {
  2922. $commentsArr = [];
  2923. }
  2924. $commentsArr['COMENTARIO_INICIO'] = $form['comments'];
  2925. $commentsStr = json_encode($commentsArr);
  2926. */
  2927. DB::table('S002V01TRVTN')->where([
  2928. ['RVTN_IDVI', '=', $idVisit],
  2929. ['RVTN_NULI', '=', $form['linea']]
  2930. ])->update([
  2931. 'RVTN_ESTA' => 'EP',
  2932. 'RVTN_HIES' => $statusHistoryStr,
  2933. 'RVTN_USMO' => $idUser,
  2934. 'RVTN_FEMO' => $nowStr,
  2935. ]);
  2936. $this->notificationsController->emitNotification(
  2937. 'S002V01M10GMPR',
  2938. "Visita Técnica No Programada #$idVisit",
  2939. "Inicio de la ejecución de la visita técnica no programada #$idVisit.",
  2940. [[
  2941. 'BOTON' => 'Ver detalles',
  2942. 'FUNCION' => 'openPreventiveWorkOrderDetails',
  2943. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit)])
  2944. ], [
  2945. 'BOTON' => 'Ir al módulo',
  2946. 'FUNCION' => 'openModule',
  2947. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMPR/AOTR/RVTP')])
  2948. ]],
  2949. $audience,
  2950. $idUser,
  2951. $form['linea'],
  2952. $this->getSocketClient(),
  2953. $idVisit,
  2954. 'Preventivo'
  2955. );
  2956. $actions = DB::getQueryLog();
  2957. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  2958. $idac = $this->functionsController->registerActivity(
  2959. $form['linea'],
  2960. 'S002V01M10GMPR',
  2961. 'S002V01F11RVTP',
  2962. 'S002V01P01REVI',
  2963. 'Actualización',
  2964. "El usuario $name (" . $usr->USUA_IDUS . ") inició la visita técnica no programada #$idVisit.",
  2965. $idUser,
  2966. $nowStr,
  2967. 'S002V01S02AOTR'
  2968. );
  2969. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  2970. return $this->responseController->makeResponse(false, 'EXITO.');
  2971. }
  2972. public function updateOrderStatus(Request $request)
  2973. {
  2974. DB::enableQueryLog();
  2975. $validator = Validator::make($request->all(), [
  2976. 'id_user' => 'required|string',
  2977. 'linea' => 'required|integer',
  2978. 'id_order' => 'required|string',
  2979. 'status' => 'required|string|in:B,R,A,E'
  2980. ]);
  2981. if ($validator->fails()) {
  2982. return $this->responseController->makeResponse(
  2983. true,
  2984. "Se encontraron uno o más errores.",
  2985. $this->responseController->makeErrors(
  2986. $validator->errors()->messages()
  2987. ),
  2988. 401
  2989. );
  2990. }
  2991. $form = $request->all();
  2992. $idUser = $this->encryptionController->decrypt($form['id_user']);
  2993. if (!$idUser) {
  2994. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  2995. }
  2996. $usr = DB::table('S002V01TUSUA')->where([
  2997. ['USUA_NULI', '=', $form['linea']],
  2998. ['USUA_IDUS', '=', $idUser],
  2999. ])->first();
  3000. if (is_null($usr)) {
  3001. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  3002. }
  3003. $idOrder = $this->encryptionController->decrypt($form['id_order']);
  3004. if (!$idOrder) {
  3005. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  3006. }
  3007. $order = DB::table('S002V01TOTPR')->where([
  3008. ['OTPR_NULI', '=', $form['linea']],
  3009. ['OTPR_IDOT', '=', $idOrder],
  3010. ])->first();
  3011. if (is_null($order)) {
  3012. return $this->responseController->makeResponse(true, 'La orden solicitada no existe.', [], 404);
  3013. }
  3014. if ($form['status'] == 'A' && $order->OTPR_SEAN == null) {
  3015. return $this->responseController->makeResponse(true, 'La orden no puede ser aprobada sin la asignación del análisis presupuestario.', [], 401);
  3016. }
  3017. $statusStr = '';
  3018. switch ($form['status']) {
  3019. case 'B':
  3020. $statusStr = 'Borrador';
  3021. break;
  3022. case 'R':
  3023. $statusStr = 'Revisión';
  3024. break;
  3025. case 'A':
  3026. $statusStr = 'Aprobado';
  3027. break;
  3028. case 'E':
  3029. $statusStr = 'Eliminado';
  3030. break;
  3031. }
  3032. $now = $this->functionsController->now();
  3033. $nowStr = $now->toDateTimeString();
  3034. DB::table('S002V01TOTPR')->where([
  3035. ['OTPR_NULI', '=', $form['linea']],
  3036. ['OTPR_IDOT', '=', $idOrder],
  3037. ])->update([
  3038. 'OTPR_ESTA' => $form['status'],
  3039. 'OTPR_USMO' => $idUser,
  3040. 'OTPR_FEMO' => $nowStr
  3041. ]);
  3042. $actions = DB::getQueryLog();
  3043. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3044. $idac = $this->functionsController->registerActivity(
  3045. $form['linea'],
  3046. 'S002V01M10GMPR',
  3047. 'S002V01F01COTP',
  3048. 'S002V01P01HOTP',
  3049. 'Actualización',
  3050. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó el estado de la orden #$idOrder a $statusStr.",
  3051. $idUser,
  3052. $nowStr,
  3053. 'S002V01S01ORTR'
  3054. );
  3055. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  3056. return $this->responseController->makeResponse(false, 'EXITO.');
  3057. }
  3058. public function setBudgetAnalysis(Request $request)
  3059. {
  3060. DB::enableQueryLog();
  3061. $validator = Validator::make($request->all(), [
  3062. 'id_user' => 'required|string',
  3063. 'linea' => 'required|integer',
  3064. 'id_order' => 'required|string',
  3065. 'analysis' => 'required|json'
  3066. ]);
  3067. if ($validator->fails()) {
  3068. return $this->responseController->makeResponse(
  3069. true,
  3070. "Se encontraron uno o más errores.",
  3071. $this->responseController->makeErrors(
  3072. $validator->errors()->messages()
  3073. ),
  3074. 401
  3075. );
  3076. }
  3077. $form = $request->all();
  3078. $idUser = $this->encryptionController->decrypt($form['id_user']);
  3079. if (!$idUser) {
  3080. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  3081. }
  3082. $usr = DB::table('S002V01TUSUA')->where([
  3083. ['USUA_NULI', '=', $form['linea']],
  3084. ['USUA_IDUS', '=', $idUser],
  3085. ])->first();
  3086. if (is_null($usr)) {
  3087. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  3088. }
  3089. $idOrder = $this->encryptionController->decrypt($form['id_order']);
  3090. if (!$idOrder) {
  3091. return $this->responseController->makeResponse(true, 'El ID de la orden no está encriptado correctamente.', [], 400);
  3092. }
  3093. $order = DB::table('S002V01TOTPR')->where([
  3094. ['OTPR_NULI', '=', $form['linea']],
  3095. ['OTPR_IDOT', '=', $idOrder]
  3096. ])->first();
  3097. if (is_null($order)) {
  3098. return $this->responseController->makeResponse(true, 'La orden consultada no está registrada.', [], 404);
  3099. }
  3100. $analysisArr = json_decode($form['analysis'], true);
  3101. if (empty($analysisArr)) {
  3102. return $this->responseController->makeResponse(true, 'El JSON de análisis no contiene información.', [], 400);
  3103. } else if (!array_key_exists('teamsConf', $analysisArr)) {
  3104. return $this->responseController->makeResponse(true, 'La información de la configuración de los equipos no se envió en el JSON.', [], 400);
  3105. } else if (!is_array($analysisArr['teamsConf'])) {
  3106. return $this->responseController->makeResponse(true, 'La configuración de los equipos tiene un formato inválido.', [], 400);
  3107. }
  3108. $teamsConf = $analysisArr['teamsConf'];
  3109. foreach ($teamsConf as $key => $conf) {
  3110. $idDec = $this->encryptionController->decrypt($conf['ID']);
  3111. if (!$idDec) {
  3112. return $this->responseController->makeResponse(true, "El ID del elemento en la posición $key del arreglo de especialidades no fue encriptado correctamente.", [], 400);
  3113. }
  3114. $conf['ID'] = $idDec;
  3115. $teamsConf[$key] = $conf;
  3116. }
  3117. $analysisArr['teamsConf'] = $teamsConf;
  3118. $now = $this->functionsController->now();
  3119. $nowStr = $now->toDateTimeString();
  3120. $sean = json_encode($analysisArr);
  3121. DB::table('S002V01TOTPR')->where([
  3122. ['OTPR_NULI', '=', $form['linea']],
  3123. ['OTPR_IDOT', '=', $idOrder],
  3124. ])->update([
  3125. 'OTPR_SEAN' => $sean,
  3126. 'OTPR_USMO' => $idUser,
  3127. 'OTPR_FEMO' => $nowStr
  3128. ]);
  3129. $actions = DB::getQueryLog();
  3130. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3131. $idac = $this->functionsController->registerActivity(
  3132. $form['linea'],
  3133. 'S002V01M10GMPR',
  3134. 'S002V01F01COTP',
  3135. 'S002V01P01HOTP',
  3136. 'Actualización',
  3137. "El usuario $name (" . $usr->USUA_IDUS . ") asignó el análisis presupuestario de la orden #$idOrder.",
  3138. $idUser,
  3139. $nowStr,
  3140. 'S002V01S01ORTR'
  3141. );
  3142. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  3143. return $this->responseController->makeResponse(false, 'EXITO.');
  3144. }
  3145. public function getMaintenanceSimulation($idOrder, $idUser, $line)
  3146. {
  3147. DB::enableQueryLog();
  3148. $idUser = $this->encryptionController->decrypt($idUser);
  3149. if (!$idUser) {
  3150. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  3151. }
  3152. $usr = DB::table('S002V01TUSUA')->where([
  3153. ['USUA_NULI', '=', $line],
  3154. ['USUA_IDUS', '=', $idUser],
  3155. ])->first();
  3156. if (is_null($usr)) {
  3157. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  3158. }
  3159. $idOrder = $this->encryptionController->decrypt($idOrder);
  3160. if (!$idOrder) {
  3161. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  3162. }
  3163. $order = DB::table('S002V01TOTPR')->select([
  3164. 'OTPR_IDOT AS IDORDER',
  3165. 'OTPR_DEIN AS DESCRIPCION',
  3166. 'OTPR_ININ AS INSTRUCCIONES',
  3167. 'OTPR_EQIN AS EQUIPAMIENTO',
  3168. 'OTPR_FIAP AS FECHAINICIO',
  3169. 'OTPR_FTAP AS FECHAFINAL',
  3170. 'OTPR_SEAN AS ANALISIS',
  3171. 'OTPR_TIIN AS TIEINMEST',
  3172. 'OTPR_OPPR AS OPERARIOS',
  3173. 'OTPR_DTIN AS TIETOTEST',
  3174. 'OTPR_RHRE AS RECURSOS',
  3175. 'OTPR_ACAS AS ACTIVADOR',
  3176. 'OTPR_ESTA AS ESTADO',
  3177. 'ACTI_PRIO AS PRIORIDAD',
  3178. 'ACTI_TIAC AS TIPOACTIVADOR',
  3179. 'ACTI_COAC AS CONFIGACTI'
  3180. ])->join('S002V01TACTI', 'ACTI_IDAC', '=', 'OTPR_ACAS')->where([
  3181. ['OTPR_NULI', '=', $line],
  3182. ['OTPR_IDOT', '=', $idOrder]
  3183. ])->first();
  3184. if (is_null($order)) {
  3185. return $this->responseController->makeResponse(true, 'La orden solicitada no existe.', [], 404);
  3186. } else if ($order->ESTADO == 'B') {
  3187. return $this->responseController->makeResponse(true, 'La orden solicitada no puede ejecutarse porque se encuentra en borradores.', [], 401);
  3188. } else if ($order->ESTADO == 'R') {
  3189. return $this->responseController->makeResponse(true, 'La orden solicitada no puede ejecutarse porque se encuentra en revisión.', [], 401);
  3190. } else if ($order->ESTADO == 'E') {
  3191. return $this->responseController->makeResponse(true, 'La orden solicitada no puede ejecutarse porque ha sido eliminado.', [], 401);
  3192. }
  3193. $analisisArr = json_decode($order->ANALISIS, true);
  3194. $teamsConf = $analisisArr['teamsConf'];
  3195. foreach ($teamsConf as $key => $val) {
  3196. $val['ID'] = $this->encryptionController->encrypt($val['ID']);
  3197. $teamsConf[$key] = $val;
  3198. }
  3199. $analisisArr['teamsConf'] = $teamsConf;
  3200. $order->ANALISIS = json_encode($analisisArr);
  3201. $actions = DB::getQueryLog();
  3202. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3203. $now = $this->functionsController->now();
  3204. $nowStr = $now->toDateTimeString();
  3205. $idac = $this->functionsController->registerActivity(
  3206. $line,
  3207. 'S002V01M10GMPR',
  3208. 'S002V01F06SPMA',
  3209. 'S002V01P02VSPM',
  3210. 'Consulta',
  3211. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la simulación de la orden #$idOrder.",
  3212. $idUser,
  3213. $nowStr,
  3214. 'S002V01S02AOTR'
  3215. );
  3216. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  3217. return $this->responseController->makeResponse(false, 'EXITO.', $order);
  3218. }
  3219. public function getOrderWithActivator($idOrder, $idUser, $line)
  3220. {
  3221. DB::enableQueryLog();
  3222. $idUser = $this->encryptionController->decrypt($idUser);
  3223. if (!$idUser) {
  3224. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  3225. }
  3226. $usr = DB::table('S002V01TUSUA')->where([
  3227. ['USUA_NULI', '=', $line],
  3228. ['USUA_IDUS', '=', $idUser],
  3229. ])->first();
  3230. if (is_null($usr)) {
  3231. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  3232. }
  3233. $idOrder = $this->encryptionController->decrypt($idOrder);
  3234. if (!$idOrder) {
  3235. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  3236. }
  3237. $order = DB::table('S002V01TOTPR')->select([
  3238. 'OTPR_IDOT AS IDORDEN',
  3239. 'OTPR_DEIN AS DESCRIPCION',
  3240. 'OTPR_ININ AS INSTRUCCIONES',
  3241. 'OTPR_EQIN AS EQUIPAMIENTO',
  3242. 'OTPR_FIAP AS FECHAINICIO',
  3243. 'OTPR_FTAP AS FECHAFINAL',
  3244. 'OTPR_TIIN AS TIEINMEST',
  3245. 'OTPR_DTIN AS TIETOTEST',
  3246. 'OTPR_ACAS AS ACTIVADOR',
  3247. 'OTPR_CLAS AS CLASIFICACION',
  3248. 'OTPR_ESTA AS ESTADO',
  3249. 'ACTI_PRIO AS PRIORIDAD',
  3250. 'ACTI_TIAC AS TIPOACTIVADOR',
  3251. 'ACTI_COAC AS CONFIGACTIVADOR',
  3252. ])->join('S002V01TACTI', 'ACTI_IDAC', '=', 'OTPR_ACAS')->where([
  3253. ['OTPR_NULI', '=', $line],
  3254. ['OTPR_IDOT', '=', $idOrder],
  3255. ])->first();
  3256. if (is_null($order)) {
  3257. return $this->responseController->makeResponse(true, 'La orden solicitada no está registrada.', [], 404);
  3258. }
  3259. $now = $this->functionsController->now();
  3260. $nowStr = $now->toDateTimeString();
  3261. $actions = DB::getQueryLog();
  3262. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3263. $idac = $this->functionsController->registerActivity(
  3264. $line,
  3265. 'S002V01M10GMPR',
  3266. 'S002V01F02AFDT',
  3267. 'S002V01P01OTAT',
  3268. 'Consulta',
  3269. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la información de la orden #$idOrder.",
  3270. $idUser,
  3271. $nowStr,
  3272. 'S002V01S02AOTR'
  3273. );
  3274. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  3275. return $this->responseController->makeResponse(false, 'EXITO.', $order);
  3276. }
  3277. public function updateOrderWithActivator(Request $request)
  3278. {
  3279. DB::enableQueryLog();
  3280. $validator = Validator::make($request->all(), [
  3281. 'id_user' => 'required|string',
  3282. 'linea' => 'required|integer',
  3283. 'id_order' => 'required|string',
  3284. 'id_activator' => 'required|string',
  3285. 'start_date' => 'required|date',
  3286. 'end_date' => 'required|date',
  3287. 'activator_type' => 'required|string|in:Calendario,Sintoma,Medida,Valor',
  3288. 'priority' => 'required|string|in:1,2,3,4',
  3289. 'config_activator' => 'required|json',
  3290. ]);
  3291. if ($validator->fails()) {
  3292. return $this->responseController->makeResponse(
  3293. true,
  3294. "Se encontraron uno o más errores.",
  3295. $this->responseController->makeErrors(
  3296. $validator->errors()->messages()
  3297. ),
  3298. 401
  3299. );
  3300. }
  3301. $form = $request->all();
  3302. $idUser = $this->encryptionController->decrypt($form['id_user']);
  3303. if (!$idUser) {
  3304. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  3305. }
  3306. $usr = DB::table('S002V01TUSUA')->where([
  3307. ['USUA_IDUS', '=', $idUser],
  3308. ['USUA_NULI', '=', $form['linea']],
  3309. ])->first();
  3310. if (is_null($usr)) {
  3311. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  3312. }
  3313. $idOrder = $this->encryptionController->decrypt($form['id_order']);
  3314. if (!$idOrder) {
  3315. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  3316. }
  3317. $order = DB::table('S002V01TOTPR')->where([
  3318. ['OTPR_NULI', '=', $form['linea']],
  3319. ['OTPR_IDOT', '=', $idOrder],
  3320. ])->first();
  3321. if (is_null($order)) {
  3322. return $this->responseController->makeResponse(true, 'La orden de trabajo solicitada no existe.', [], 404);
  3323. }
  3324. $idActivator = $this->encryptionController->decrypt($form['id_activator']);
  3325. if (!$idActivator) {
  3326. return $this->responseController->makeResponse(true, 'El ID del activador solicitado no está encriptado correctamente.', [], 400);
  3327. }
  3328. $activator = DB::table('S002V01TACTI')->where([
  3329. ['ACTI_IDAC', '=', $idActivator],
  3330. ['ACTI_NULI', '=', $form['linea']]
  3331. ])->first();
  3332. if (is_null($activator)) {
  3333. return $this->responseController->makeResponse(true, 'El activador solicitado no existe.', [], 404);
  3334. }
  3335. $now = $this->functionsController->now();
  3336. $nowStr = $now->toDateTimeString();
  3337. DB::table('S002V01TOTPR')->where([
  3338. ['OTPR_NULI', '=', $form['linea']],
  3339. ['OTPR_IDOT', '=', $idOrder],
  3340. ])->update([
  3341. 'OTPR_ACAS' => $idActivator,
  3342. 'OTPR_FIAP' => $form['start_date'],
  3343. 'OTPR_FTAP' => $form['end_date'],
  3344. 'OTPR_USMO' => $idUser,
  3345. 'OTPR_FEMO' => $nowStr,
  3346. ]);
  3347. DB::table('S002V01TACTI')->where([
  3348. ['ACTI_IDAC', '=', $idActivator],
  3349. ['ACTI_NULI', '=', $form['linea']]
  3350. ])->update([
  3351. 'ACTI_PRIO' => $form['priority'],
  3352. 'ACTI_TIAC' => $form['activator_type'],
  3353. 'ACTI_COAC' => $form['config_activator'],
  3354. 'ACTI_USMO' => $idUser,
  3355. 'ACTI_FEMO' => $nowStr,
  3356. ]);
  3357. $actions = DB::getQueryLog();
  3358. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3359. $idac = $this->functionsController->registerActivity(
  3360. $form['linea'],
  3361. 'S002V01M10GMPR',
  3362. 'S002V01F02AFDT',
  3363. 'S002V01P02ACAC',
  3364. 'Actualización',
  3365. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó la orden #$idOrder y el activador #$idActivator.",
  3366. $idUser,
  3367. $nowStr,
  3368. 'S002V01S02AOTR'
  3369. );
  3370. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  3371. return $this->responseController->makeResponse(false, 'EXITO.');
  3372. }
  3373. public function printOrderSimulation($idOrder, $idUser, $line)
  3374. {
  3375. DB::enableQueryLog();
  3376. $idUser = $this->encryptionController->decrypt($idUser);
  3377. if (!$idUser) {
  3378. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  3379. }
  3380. $usr = DB::table('S002V01TUSUA')->where([
  3381. ['USUA_NULI', '=', $line],
  3382. ['USUA_IDUS', '=', $idUser],
  3383. ])->first();
  3384. if (is_null($usr)) {
  3385. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  3386. }
  3387. $idOrder = $this->encryptionController->decrypt($idOrder);
  3388. if (!$idOrder) {
  3389. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  3390. }
  3391. $order = DB::table('S002V01TOTPR')->where([
  3392. ['OTPR_NULI', '=', $line],
  3393. ['OTPR_IDOT', '=', $idOrder]
  3394. ])->first();
  3395. if (is_null($order)) {
  3396. return $this->responseController->makeResponse(true, 'La orden solicitada no está registrada.', [], 404);
  3397. }
  3398. $simulation = DB::table('S002V01TRESI')->where([
  3399. ['RESI_NULI', '=', $line],
  3400. ['RESI_IDOT', '=', $idOrder]
  3401. ])->first();
  3402. if (is_null($simulation)) {
  3403. return $this->responseController->makeResponse(true, "La orden #$idOrder aún no ha sido simulada.", [], 401);
  3404. }
  3405. $content = json_decode($simulation->RESI_CUSI, true);
  3406. $html = "
  3407. <html>
  3408. <head>
  3409. <link href=\"https://fonts.googleapis.com/icon?family=Material+Icons\" rel=\"stylesheet\">
  3410. <style>
  3411. .main-icon-cell{
  3412. width: 50px;
  3413. height: 50px;
  3414. }
  3415. .main-icon-container{
  3416. width: 50px;
  3417. height: 50px;
  3418. border-radius: 64px;
  3419. border-style: solid;
  3420. padding-left: 8px;
  3421. padding-top: 8px;
  3422. border-color: rgba(0, 0, 0, 0.8);
  3423. }
  3424. .icon-container{
  3425. padding-left: 8px;
  3426. width: 42px;
  3427. height: 42px;
  3428. }
  3429. .instruction-font{
  3430. color: rgba(0, 0, 0, 0.8);
  3431. font-size: 24px;
  3432. padding-left: 8px;
  3433. font-weight: 600;
  3434. }
  3435. .space-cell{
  3436. width: 50px;
  3437. }
  3438. </style>
  3439. </head>
  3440. <body>
  3441. <table width=\"100%\">
  3442. ";
  3443. foreach ($content as $item) {
  3444. $imgContent = file_get_contents($this->functionsController->getBasePath() . "\storage\app\\files\\$item[icon].png");
  3445. $imgB64 = "data:image/png;base64, " . base64_encode($imgContent);
  3446. $html .= "
  3447. <tr>
  3448. ";
  3449. if (!$item['isMain']) {
  3450. $html .= '<td class="space-cell"></td>';
  3451. }
  3452. $html .= "
  3453. <td class=\"main-icon-cell\">
  3454. <div class=\"
  3455. ";
  3456. if ($item['isMain']) {
  3457. $html .= 'main-icon-container">';
  3458. } else {
  3459. $html .= 'icon-container ">';
  3460. }
  3461. $html .= "
  3462. <img src=\"$imgB64\" width=\"42\" height=\"42\">
  3463. </div>
  3464. </td>
  3465. <td class=\"instruction-font\" colspan=\"
  3466. ";
  3467. if ($item['isMain']) {
  3468. $html .= '2';
  3469. }
  3470. $html .= "
  3471. \">$item[label]</td>
  3472. </tr>
  3473. ";
  3474. }
  3475. $html .= "
  3476. </table>
  3477. </body>
  3478. </html>";
  3479. $filePath = $this->functionsController->getBasePath() . '\public_files\\';
  3480. $noar = "simulacion_orden_$idOrder";
  3481. $exte = "pdf";
  3482. $line = intval($line);
  3483. $line = $line < 10 ? "0$line" : "$line";
  3484. $como = "GMPR";
  3485. $cldo = "OR";
  3486. $now = $this->functionsController->now();
  3487. $nowStr = $now->toDateTimeString();
  3488. $nowArr = explode(" ", $nowStr);
  3489. $dateArr = explode("-", $nowArr[0]);
  3490. $year = substr($dateArr[0], 2);
  3491. $fecr = $year . $dateArr[1] . $dateArr[2];
  3492. $sec = DB::table('S002V01TAFAL')->where([
  3493. ['AFAL_NULI', '=', $line],
  3494. ['AFAL_COMO', '=', $como],
  3495. ['AFAL_CLDO', '=', $cldo],
  3496. ])->orderBy('AFAL_NUSE', 'desc')->first();
  3497. $nuse = is_null($sec) ? "1" : "" . intval($sec->AFAL_NUSE) + 1 . "";
  3498. for ($i = strlen($nuse); $i < 6; $i++) {
  3499. $nuse = "0$nuse";
  3500. }
  3501. $ver = DB::table('S002V01TAFAL')->where([
  3502. ['AFAL_NULI', '=', $line],
  3503. ['AFAL_COMO', '=', $como],
  3504. ['AFAL_CLDO', '=', $cldo],
  3505. ['AFAL_NOAR', '=', $noar],
  3506. ['AFAL_EXTE', '=', $exte],
  3507. ])->orderBy('AFAL_NUVE', 'desc')->first();
  3508. $nuve = is_null($ver) ? "1" : "" . intval($ver->AFAL_NUVE) + 1 . "";
  3509. for ($i = strlen($nuve); $i < 2; $i++) {
  3510. $nuve = "0$nuve";
  3511. }
  3512. $fileName = "$line-$como-$cldo-$fecr-$nuse=$nuve=$noar.$exte";
  3513. $dompdf = new Dompdf();
  3514. $dompdf->loadHtml($html);
  3515. $dompdf->setPaper('A4', 'portrait');
  3516. $dompdf->render();
  3517. $output = $dompdf->output();
  3518. $tempFile = $filePath . $fileName;
  3519. if (!file_exists($tempFile)) {
  3520. fopen($tempFile, 'w');
  3521. }
  3522. file_put_contents($tempFile, $output);
  3523. $ubic = Storage::putFile('files', new File($tempFile));
  3524. $ubic = str_replace("/", "\\", $ubic);
  3525. $ubic = $this->functionsController->getBasePath() . "\storage\app\\" . $ubic;
  3526. $tama = filesize($ubic);
  3527. $usac = json_encode([$idUser]);
  3528. unlink($tempFile);
  3529. DB::table('S002V01TAFAL')->insert([
  3530. 'AFAL_NULI' => $line,
  3531. 'AFAL_COMO' => $como,
  3532. 'AFAL_CLDO' => $cldo,
  3533. 'AFAL_FECR' => $fecr,
  3534. 'AFAL_NUSE' => $nuse,
  3535. 'AFAL_NUVE' => $nuve,
  3536. 'AFAL_NOAR' => $noar,
  3537. 'AFAL_EXTE' => $exte,
  3538. 'AFAL_TAMA' => $tama,
  3539. 'AFAL_UBIC' => $ubic,
  3540. 'AFAL_USAC' => $usac,
  3541. 'AFAL_USRE' => $idUser,
  3542. 'AFAL_FERE' => $nowStr,
  3543. ]);
  3544. $filesArr = json_decode($order->OTPR_DONE, true);
  3545. $filesArr[] = $fileName;
  3546. $filesStr = json_encode($filesArr);
  3547. DB::table('S002V01TOTPR')->where([
  3548. ['OTPR_NULI', '=', $line],
  3549. ['OTPR_IDOT', '=', $idOrder]
  3550. ])->update([
  3551. 'OTPR_DONE' => $filesStr,
  3552. 'OTPR_USMO' => $idUser,
  3553. 'OTPR_FEMO' => $nowStr
  3554. ]);
  3555. $actions = DB::getQueryLog();
  3556. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3557. $idac = $this->functionsController->registerActivity(
  3558. $line,
  3559. 'S002V01M10GMPR',
  3560. 'S002V01F06SPMA',
  3561. 'S002V01P02VSPM',
  3562. 'Registro',
  3563. "El usuario $name (" . $usr->USUA_IDUS . ") generó el archivo de simulación de la orden #$idOrder.",
  3564. $idUser,
  3565. $nowStr,
  3566. 'S002V01S02AOTR'
  3567. );
  3568. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  3569. return $this->responseController->makeResponse(false, 'EXITO.', ['fileID' => $fileName]);
  3570. }
  3571. public function saveOrderSimulation(Request $request)
  3572. {
  3573. DB::enableQueryLog();
  3574. $validator = Validator::make($request->all(), [
  3575. 'id_user' => 'required|string',
  3576. 'linea' => 'required|integer',
  3577. 'id_order' => 'required|string',
  3578. 'content' => 'required|json',
  3579. ]);
  3580. if ($validator->fails()) {
  3581. return $this->responseController->makeResponse(
  3582. true,
  3583. "Se encontraron uno o más errores.",
  3584. $this->responseController->makeErrors(
  3585. $validator->errors()->messages()
  3586. ),
  3587. 401
  3588. );
  3589. }
  3590. $form = $request->all();
  3591. $idUser = $this->encryptionController->decrypt($form['id_user']);
  3592. if (!$idUser) {
  3593. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  3594. }
  3595. $usr = DB::table('S002V01TUSUA')->where([
  3596. ['USUA_NULI', '=', $form['linea']],
  3597. ['USUA_IDUS', '=', $idUser],
  3598. ])->first();
  3599. if (is_null($usr)) {
  3600. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  3601. }
  3602. $idOrder = $this->encryptionController->decrypt($form['id_order']);
  3603. if (!$idOrder) {
  3604. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  3605. }
  3606. $order = DB::table('S002V01TOTPR')->where([
  3607. ['OTPR_NULI', '=', $form['linea']],
  3608. ['OTPR_IDOT', '=', $idOrder],
  3609. ])->first();
  3610. if (is_null($order)) {
  3611. return $this->responseController->makeResponse(true, 'La orden consultada no está registrada.', [], 404);
  3612. }
  3613. $simulation = DB::table('S002V01TRESI')->where([
  3614. ['RESI_NULI', '=', $form['linea']],
  3615. ['RESI_IDOT', '=', $idOrder],
  3616. ])->first();
  3617. $now = $this->functionsController->now();
  3618. $nowStr = $now->toDateTimeString();
  3619. if (is_null($simulation)) {
  3620. DB::table('S002V01TRESI')->insert([
  3621. 'RESI_NULI' => $form['linea'],
  3622. 'RESI_IDOT' => $idOrder,
  3623. 'RESI_CUSI' => $form['content'],
  3624. 'RESI_FUSI' => $nowStr,
  3625. 'RESI_UUSI' => $idUser,
  3626. ]);
  3627. } else {
  3628. $timestamp = $now->timestamp;
  3629. $history = $simulation->RESI_HISI == null ? [] : json_decode($simulation->RESI_HISI, true);
  3630. $history[$timestamp] = [
  3631. 'USUARIO' => $simulation->RESI_UUSI,
  3632. 'FECHA' => $simulation->RESI_FUSI,
  3633. 'CONTENIDO' => $simulation->RESI_CUSI,
  3634. ];
  3635. $historyStr = json_encode($history);
  3636. DB::table('S002V01TRESI')->where([
  3637. ['RESI_NULI', '=', $form['linea']],
  3638. ['RESI_IDOT', '=', $idOrder],
  3639. ])->update([
  3640. 'RESI_CUSI' => $form['content'],
  3641. 'RESI_FUSI' => $nowStr,
  3642. 'RESI_UUSI' => $idUser,
  3643. 'RESI_HISI' => $historyStr,
  3644. ]);
  3645. }
  3646. $actions = DB::getQueryLog();
  3647. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3648. $idac = $this->functionsController->registerActivity(
  3649. $form['linea'],
  3650. 'S002V01M10GMPR',
  3651. 'S002V01F06SPMA',
  3652. 'S002V01P02VSPM',
  3653. 'Registro',
  3654. "El usuario $name (" . $usr->USUA_IDUS . ") registró una simulación para la orden #$idOrder.",
  3655. $idUser,
  3656. $nowStr,
  3657. 'S002V01S02AOTR'
  3658. );
  3659. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  3660. return $this->responseController->makeResponse(false, 'EXITO.');
  3661. }
  3662. public function printOrderDetails($idOrder, $idUser, $line)
  3663. {
  3664. DB::enableQueryLog();
  3665. $idUser = $this->encryptionController->decrypt($idUser);
  3666. if (!$idUser) {
  3667. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  3668. }
  3669. $usr = DB::table('S002V01TUSUA')->where([
  3670. ['USUA_NULI', '=', $line],
  3671. ['USUA_IDUS', '=', $idUser]
  3672. ])->first();
  3673. if (is_null($usr)) {
  3674. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  3675. }
  3676. $idOrder = $this->encryptionController->decrypt($idOrder);
  3677. if (!$idOrder) {
  3678. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  3679. }
  3680. $order = DB::table('S002V01TOTPR')->join(
  3681. 'S002V01TACTI',
  3682. 'OTPR_ACAS',
  3683. '=',
  3684. 'ACTI_IDAC',
  3685. )->where([
  3686. ['OTPR_NULI', '=', $line],
  3687. ['OTPR_IDOT', '=', $idOrder]
  3688. ])->first();
  3689. if (is_null($order)) {
  3690. return $this->responseController->makeResponse(true, 'La orden solicitada no está registrada.', [], 404);
  3691. }
  3692. $html = "
  3693. <html>
  3694. <head>
  3695. <style>
  3696. table{
  3697. border-collapse: separate;
  3698. border-spacing: 0;
  3699. width: 100%;
  3700. }
  3701. .cell-title{
  3702. font-size: 12px;
  3703. font-weight: bold;
  3704. padding: 8px 8px 4px 8px;
  3705. }
  3706. .top-corners-radius{
  3707. border-radius: 4px 4px 0 0;
  3708. }
  3709. .left-bottom-corner-radius{
  3710. border-radius: 0 0 0 4px;
  3711. }
  3712. .right-bottom-corner-radius{
  3713. border-radius: 0 0 4px 0;
  3714. }
  3715. .cell-content{
  3716. padding: 4px 8px 8px 8px;
  3717. font-size: 16px;
  3718. color: rgba(0, 0, 0, 0.7);
  3719. }
  3720. .border-top{ border-top: 1px solid #dddddd; }
  3721. .border-left{ border-left: 1px solid #dddddd; }
  3722. .border-right{ border-right: 1px solid #dddddd; }
  3723. .border-bottom{ border-bottom: 1px solid #dddddd; }
  3724. </style>
  3725. </head>
  3726. <body>
  3727. <table>
  3728. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-title top-corners-radius border-top border-left border-right\">Descripción</td></tr>
  3729. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-content border-left border-right\">" . $order->OTPR_DEIN . "</td></tr>
  3730. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-title border-top border-left border-right\">Instrucciones</td></tr>
  3731. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-content border-left border-right\">
  3732. ";
  3733. $instructions = json_decode($order->OTPR_ININ, true);
  3734. $cont = 1;
  3735. foreach ($instructions as $instruction) {
  3736. $html .= $cont . ".- " . $instruction['INSTRUCCION'] . "<br>";
  3737. $cont++;
  3738. }
  3739. $startDate = $this->functionsController->formatDateTime($order->OTPR_FIAP);
  3740. $endDate = $this->functionsController->formatDateTime($order->OTPR_FTAP);
  3741. $html .= "
  3742. </td></tr>
  3743. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-title border-top border-left border-right\">Equipamiento relacionado</td></tr>
  3744. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-content border-left border-right\">" . $order->OTPR_EQIN . "</td></tr>
  3745. <tr>
  3746. <td width=\"50%\" colspan=\"3\" class=\"cell-title border-top border-left border-right\">Fecha y hora de inicio</td>
  3747. <td width=\"50%\" colspan=\"3\" class=\"cell-title border-top border-right\">Fecha y hora final</td>
  3748. </tr>
  3749. <tr>
  3750. <td width=\"50%\" colspan=\"3\" class=\"cell-content border-left border-right\">$startDate</td>
  3751. <td width=\"50%\" colspan=\"3\" class=\"cell-content border-right\">$endDate</td>
  3752. </tr>
  3753. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-title border-top border-left border-right\">Análisis presupuestario</td></tr>
  3754. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-content border-left border-right\">
  3755. ";
  3756. $analisis = $order->OTPR_SEAN == null ? 'Sin configuración' : json_decode($order->OTPR_SEAN, true);
  3757. if (gettype($analisis) == 'array') {
  3758. //PENDIENTE
  3759. $teamsConf = $analisis['teamsConf'];
  3760. foreach ($teamsConf as $key => $team) {
  3761. /*var_dump($key);
  3762. echo "<br>";*/
  3763. }
  3764. $html .= "
  3765. PENDIENTE
  3766. </td></tr>
  3767. ";
  3768. } else {
  3769. $html .= "
  3770. $analisis
  3771. </td></tr>
  3772. ";
  3773. }
  3774. $html .= "
  3775. <tr>
  3776. <td width=\"33%\" colspan=\"2\" class=\"cell-title border-top border-left border-right\">Clasificación</td>
  3777. <td width=\"34%\" colspan=\"2\" class=\"cell-title border-top border-right\">Duración total estimada</td>
  3778. <td width=\"33%\" colspan=\"2\" class=\"cell-title border-top border-right\">Tiempo de inmovilización estimado</td>
  3779. </tr>
  3780. <tr>
  3781. <td width=\"33%\" colspan=\"2\" class=\"cell-content border-left border-right\">" . $order->OTPR_CLAS . "</td>
  3782. <td width=\"34%\" colspan=\"2\" class=\"cell-content border-right\">" . $order->OTPR_DTIN . " hora(s)</td>
  3783. <td width=\"33%\" colspan=\"2\" class=\"cell-content border-right\">" . $order->OTPR_TIIN . " hora(s)</td>
  3784. </tr>
  3785. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-title border-top border-left border-right\">Personal involucrado</td></tr>
  3786. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-content border-left border-right\">PENDIENTE</td></tr>
  3787. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-title border-top border-left border-right\">Recursos requerido</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\">Documentos requeridos</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\">Contratos 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\">Información del activador</td></tr>
  3794. <tr><td width=\"100%\" colspan=\"6\" class=\"cell-content border-left border-right\">PENDIENTE</td></tr>
  3795. <tr>
  3796. <td width=\"50%\" colspan=\"3\" class=\"cell-title border-top border-left border-right\">Usuario que registró</td>
  3797. <td width=\"50%\" colspan=\"3\" class=\"cell-title border-top border-right\">Fecha de registro</td>
  3798. </tr>
  3799. <tr>
  3800. <td width=\"50%\" colspan=\"3\" class=\"cell-content border-left border-right\">PENDIENTE</td>
  3801. <td width=\"50%\" colspan=\"3\" class=\"cell-content border-right\">PENDIENTE</td>
  3802. </tr>
  3803. <tr>
  3804. <td width=\"50%\" colspan=\"3\" class=\"cell-title border-top border-left border-right\">Usuario de la última modificación</td>
  3805. <td width=\"50%\" colspan=\"3\" class=\"cell-title border-top border-right\">Fecha de la última modificación</td>
  3806. </tr>
  3807. <tr>
  3808. <td width=\"50%\" colspan=\"3\" class=\"cell-content border-left border-right border-bottom left-bottom-corner-radius\">PENDIENTE</td>
  3809. <td width=\"50%\" colspan=\"3\" class=\"cell-content border-right border-bottom right-bottom-corner-radius\">PENDIENTE</td>
  3810. </tr>
  3811. </table>
  3812. </body>
  3813. </html>
  3814. ";
  3815. $filePath = $this->functionsController->getBasePath() . '\public_files\\';
  3816. $noar = "detalles_orden_$idOrder";
  3817. $exte = "pdf";
  3818. $line = intval($line);
  3819. $line = $line < 10 ? "0$line" : "$line";
  3820. $como = "GMPR";
  3821. $cldo = "OR";
  3822. $now = $this->functionsController->now();
  3823. $nowStr = $now->toDateTimeString();
  3824. $nowArr = explode(" ", $nowStr);
  3825. $dateArr = explode("-", $nowArr[0]);
  3826. $year = substr($dateArr[0], 2);
  3827. $fecr = $year . $dateArr[1] . $dateArr[2];
  3828. $sec = DB::table('S002V01TAFAL')->where([
  3829. ['AFAL_NULI', '=', $line],
  3830. ['AFAL_COMO', '=', $como],
  3831. ['AFAL_CLDO', '=', $cldo],
  3832. ])->orderBy('AFAL_NUSE', 'desc')->first();
  3833. $nuse = is_null($sec) ? "1" : "" . intval($sec->AFAL_NUSE) + 1 . "";
  3834. for ($i = strlen($nuse); $i < 6; $i++) {
  3835. $nuse = "0$nuse";
  3836. }
  3837. $ver = DB::table('S002V01TAFAL')->where([
  3838. ['AFAL_NULI', '=', $line],
  3839. ['AFAL_COMO', '=', $como],
  3840. ['AFAL_CLDO', '=', $cldo],
  3841. ['AFAL_NOAR', '=', $noar],
  3842. ['AFAL_EXTE', '=', $exte],
  3843. ])->orderBy('AFAL_NUVE', 'desc')->first();
  3844. $nuve = is_null($ver) ? "1" : "" . intval($ver->AFAL_NUVE) + 1 . "";
  3845. for ($i = strlen($nuve); $i < 2; $i++) {
  3846. $nuve = "0$nuve";
  3847. }
  3848. $fileName = "$line-$como-$cldo-$fecr-$nuse=$nuve=$noar.$exte";
  3849. $dompdf = new Dompdf();
  3850. $dompdf->loadHtml($html);
  3851. $dompdf->setPaper('A4', 'portrait');
  3852. $dompdf->render();
  3853. $output = $dompdf->output();
  3854. $tempFile = $filePath . $fileName;
  3855. if (!file_exists($tempFile)) {
  3856. fopen($tempFile, 'w');
  3857. }
  3858. file_put_contents($tempFile, $output);
  3859. $ubic = Storage::putFile('files', new File($tempFile));
  3860. $ubic = str_replace("/", "\\", $ubic);
  3861. $ubic = $this->functionsController->getBasePath() . "\storage\app\\" . $ubic;
  3862. $tama = filesize($ubic);
  3863. $usac = json_encode([$idUser]);
  3864. unlink($tempFile);
  3865. DB::table('S002V01TAFAL')->insert([
  3866. 'AFAL_NULI' => $line,
  3867. 'AFAL_COMO' => $como,
  3868. 'AFAL_CLDO' => $cldo,
  3869. 'AFAL_FECR' => $fecr,
  3870. 'AFAL_NUSE' => $nuse,
  3871. 'AFAL_NUVE' => $nuve,
  3872. 'AFAL_NOAR' => $noar,
  3873. 'AFAL_EXTE' => $exte,
  3874. 'AFAL_TAMA' => $tama,
  3875. 'AFAL_UBIC' => $ubic,
  3876. 'AFAL_USAC' => $usac,
  3877. 'AFAL_USRE' => $idUser,
  3878. 'AFAL_FERE' => $nowStr,
  3879. ]);
  3880. $filesArr = json_decode($order->OTPR_DONE, true);
  3881. $filesArr[] = $fileName;
  3882. $filesStr = json_encode($filesArr);
  3883. DB::table('S002V01TOTPR')->where([
  3884. ['OTPR_NULI', '=', $line],
  3885. ['OTPR_IDOT', '=', $idOrder]
  3886. ])->update([
  3887. 'OTPR_DONE' => $filesStr,
  3888. 'OTPR_USMO' => $idUser,
  3889. 'OTPR_FEMO' => $nowStr
  3890. ]);
  3891. $actions = DB::getQueryLog();
  3892. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  3893. $idac = $this->functionsController->registerActivity(
  3894. $line,
  3895. 'S002V01M10GMPR',
  3896. 'S002V01F06SPMA',
  3897. 'S002V01P02VSPM',
  3898. 'Registro',
  3899. "El usuario $name (" . $usr->USUA_IDUS . ") generó el archivo de simulación de la orden #$idOrder.",
  3900. $idUser,
  3901. $nowStr,
  3902. 'S002V01S02AOTR'
  3903. );
  3904. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  3905. return $this->responseController->makeResponse(false, 'EXITO.', ['fileID' => $fileName]);
  3906. }
  3907. public function extractMaintenancePlan($idOrder, $idUser, $line)
  3908. {
  3909. DB::enableQueryLog();
  3910. $idUser = $this->encryptionController->decrypt($idUser);
  3911. if (!$idUser) {
  3912. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  3913. }
  3914. $usr = DB::table('S002V01TUSUA')->where([
  3915. ['USUA_NULI', '=', $line],
  3916. ['USUA_IDUS', '=', $idUser],
  3917. ])->first();
  3918. if (is_null($usr)) {
  3919. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  3920. }
  3921. $idOrder = $this->encryptionController->decrypt($idOrder);
  3922. if (!$idOrder) {
  3923. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  3924. }
  3925. $order = DB::table('S002V01TOTPR')->join(
  3926. 'S002V01TACTI',
  3927. 'OTPR_ACAS',
  3928. '=',
  3929. 'ACTI_IDAC',
  3930. )->where([
  3931. ['OTPR_NULI', '=', $line],
  3932. ['OTPR_IDOT', '=', $idOrder]
  3933. ])->first();
  3934. if (is_null($order)) {
  3935. return $this->responseController->makeResponse(true, 'La orden solicitada no está registrada.', [], 404);
  3936. }
  3937. $html = file_get_contents($this->templatesUbic . "01-GMPR-PL-010101-000003=01=PDF_PLAN_MANTENIMIENTO.html");
  3938. $logo = file_get_contents($this->functionsController->getBasePath() . "\storage\app\public\global_resources\sam-short-logo.png");
  3939. $logoStr = "data:image/svg+xml;base64," . base64_encode($logo);
  3940. $html = str_replace("%stcImage%", $logoStr, $html);
  3941. $now = $this->functionsController->now();
  3942. $nowStr = $now->toDateTimeString();
  3943. $currentDate = $this->functionsController->buildHumanCurrentDate($nowStr);
  3944. $html = str_replace("%currentDate%", $currentDate, $html);
  3945. $html = str_replace("%idOrder%", $order->OTPR_IDOT, $html);
  3946. $startDateTimeArr = explode(" ", $order->OTPR_FIAP);
  3947. $startTimeArr = explode(":", $startDateTimeArr[1]);
  3948. $startHour = intval($startTimeArr[0]);
  3949. $startPeriod = $startHour < 12 ? 'AM' : 'PM';
  3950. $startHour = $startHour > 12 ? $startHour - 12 : $startHour;
  3951. $startTimeStr = "$startHour:$startTimeArr[1] $startPeriod";
  3952. $html = str_replace("%startTime%", $startTimeStr, $html);
  3953. $startDateTimeObj = new Carbon($order->OTPR_FIAP);
  3954. $totalDuration = floatval($order->OTPR_DTIN);
  3955. $totalDurationHours = intval($totalDuration);
  3956. $startDateTimeObj->addHours($totalDuration);
  3957. $totalDurationFloatMinutes = $this->functionsController->floatSub($totalDuration, $totalDurationHours);
  3958. $totalDurationFloatMinutes = $this->functionsController->floatMul($totalDurationFloatMinutes, 60);
  3959. $totalDurationMinutes = ceil($totalDurationFloatMinutes);
  3960. $startDateTimeObj->addMinutes($totalDurationMinutes);
  3961. $endDateTimeArr = explode(" ", $startDateTimeObj->toDateTimeString());
  3962. $endTimeArr = explode(":", $endDateTimeArr[1]);
  3963. $endHour = intval($endTimeArr[0]);
  3964. $endPeriod = $startHour < 12 ? 'AM' : 'PM';
  3965. $endHour = $endHour > 12 ? $endHour - 12 : $endHour;
  3966. $endTimeStr = "$endHour:$endTimeArr[1] $endPeriod";
  3967. $html = str_replace("%endTime%", $endTimeStr, $html);
  3968. $html = str_replace("%inmTime%", $order->OTPR_TIIN . " h", $html);
  3969. $html = str_replace("%totalTime%", $order->OTPR_DTIN . " h", $html);
  3970. $html = str_replace("%equipment%", $order->OTPR_EQIN, $html);
  3971. $html = str_replace("%description%", $order->OTPR_DEIN, $html);
  3972. $instructions = json_decode($order->OTPR_ININ, true);
  3973. $activitiesTableBody = "";
  3974. foreach ($instructions as $k => $v) {
  3975. $key = $k + 1;
  3976. $activitiesTableBody .= "<tr>";
  3977. $activitiesTableBody .= "<td>$key</td>";
  3978. $activitiesTableBody .= "<td>$v[INSTRUCCION]</td>";
  3979. $activitiesTableBody .= "<td>";
  3980. /*foreach($v['ESPECIALIDADES'] as $v0){
  3981. /*$team = array_filter($teams, function($v1) use ($v0) {
  3982. return ($v1['ID'] == $v0);
  3983. });
  3984. $activitiesTableBody .= /*end($team)['ESP'] '--' . "<br>";
  3985. }*/
  3986. $activitiesTableBody .= "</td>";
  3987. $activitiesTableBody .= "</tr>";
  3988. }
  3989. $html = str_replace("%activitiesTableBody%", $activitiesTableBody, $html);
  3990. $personalArr = json_decode($order->OTPR_OPPR, true);
  3991. $constConf = json_decode($order->OTPR_SEAN, true);
  3992. $cont = 1;
  3993. $specialtiesTableBody = "";
  3994. foreach ($personalArr as $k => $v) {
  3995. /*$team = array_filter($teams, function($v1) use ($k) {
  3996. return ($v1['ID'] == $k);
  3997. });*/
  3998. $specialtiesTableBody .= "<tr>";
  3999. $specialtiesTableBody .= "<td>$cont</td>";
  4000. $specialtiesTableBody .= "<td>" . /*end($team)['ESP']*/ '--' . "</td>";
  4001. $specialtiesTableBody .= "<td>";
  4002. foreach ($constConf['teamsConf'][$k] as $v0) {
  4003. $usrID = '-'; //$v0['USER'];
  4004. $usrInv = DB::table('S002V01TUSUA')->where([
  4005. ['USUA_NULI', '=', $line],
  4006. ['USUA_IDUS', '=', $usrID],
  4007. ])->first();
  4008. $invName = /*$this->functionsController->joinName($usrInv->USUA_NOMB, $usrInv->USUA_APPA, $usrInv->USUA_APMA) . " ($usrID)"*/ '-';
  4009. $specialtiesTableBody .= $invName . "<br>";
  4010. }
  4011. $specialtiesTableBody .= "</td>";
  4012. $specialtiesTableBody .= "</tr>";
  4013. $cont++;
  4014. }
  4015. $html = str_replace("%specialtiesTableBody%", $specialtiesTableBody, $html);
  4016. $como = 'GMPR';
  4017. $cldo = 'OR';
  4018. $noar = "plan_de_mantenimiento_preventivo_orden_$idOrder";
  4019. $exte = "pdf";
  4020. $line = $line < 10 ? "0$line" : "$line";
  4021. $nowArr = explode(" ", $nowStr);
  4022. $dateArr = explode("-", $nowArr[0]);
  4023. $year = substr($dateArr[0], 2);
  4024. $fecr = $year . $dateArr[1] . $dateArr[2];
  4025. $sec = DB::table('S002V01TAFAL')->where([
  4026. ['AFAL_NULI', '=', $line],
  4027. ['AFAL_COMO', '=', $como],
  4028. ['AFAL_CLDO', '=', $cldo],
  4029. ])->orderBy('AFAL_NUSE', 'desc')->first();
  4030. $nuse = is_null($sec) ? "1" : "" . intval($sec->AFAL_NUSE) + 1 . "";
  4031. for ($i = strlen($nuse); $i < 6; $i++) {
  4032. $nuse = "0$nuse";
  4033. }
  4034. $ver = DB::table('S002V01TAFAL')->where([
  4035. ['AFAL_NULI', '=', $line],
  4036. ['AFAL_COMO', '=', $como],
  4037. ['AFAL_CLDO', '=', $cldo],
  4038. ['AFAL_NOAR', '=', $noar],
  4039. ['AFAL_EXTE', '=', $exte],
  4040. ])->orderBy('AFAL_NUVE', 'desc')->first();
  4041. $nuve = is_null($ver) ? "1" : "" . intval($ver->AFAL_NUVE) + 1 . "";
  4042. for ($i = strlen($nuve); $i < 2; $i++) {
  4043. $nuve = "0$nuve";
  4044. }
  4045. $filePath = $this->functionsController->getBasePath() . '\public_files\\';
  4046. $fileName = "$line-$como-$cldo-$fecr-$nuse=$nuve=$noar.$exte";
  4047. $dompdf = new Dompdf();
  4048. $dompdf->loadHtml($html);
  4049. $dompdf->setPaper('A4', 'portrait');
  4050. $dompdf->render();
  4051. $output = $dompdf->output();
  4052. $tempFile = $filePath . $fileName;
  4053. if (!file_exists($tempFile)) {
  4054. fopen($tempFile, 'w');
  4055. }
  4056. file_put_contents($tempFile, $output);
  4057. $ubic = Storage::putFile('files', new File($tempFile));
  4058. $ubic = str_replace("/", "\\", $ubic);
  4059. $ubic = $this->functionsController->getBasePath() . "\storage\app\\" . $ubic;
  4060. $tama = filesize($ubic);
  4061. $usac = json_encode([$idUser]);
  4062. unlink($tempFile);
  4063. DB::table('S002V01TAFAL')->insert([
  4064. 'AFAL_NULI' => $line,
  4065. 'AFAL_COMO' => $como,
  4066. 'AFAL_CLDO' => $cldo,
  4067. 'AFAL_FECR' => $fecr,
  4068. 'AFAL_NUSE' => $nuse,
  4069. 'AFAL_NUVE' => $nuve,
  4070. 'AFAL_NOAR' => $noar,
  4071. 'AFAL_EXTE' => $exte,
  4072. 'AFAL_TAMA' => $tama,
  4073. 'AFAL_UBIC' => $ubic,
  4074. 'AFAL_USAC' => $usac,
  4075. 'AFAL_USRE' => $idUser,
  4076. 'AFAL_FERE' => $nowStr,
  4077. ]);
  4078. $filesArr = json_decode($order->OTPR_DONE, true);
  4079. $filesArr[] = $fileName;
  4080. $filesStr = json_encode($filesArr);
  4081. DB::table('S002V01TOTPR')->where([
  4082. ['OTPR_NULI', '=', $line],
  4083. ['OTPR_IDOT', '=', $idOrder]
  4084. ])->update([
  4085. 'OTPR_DONE' => $filesStr,
  4086. 'OTPR_USMO' => $idUser,
  4087. 'OTPR_FEMO' => $nowStr
  4088. ]);
  4089. $actions = DB::getQueryLog();
  4090. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4091. $idac = $this->functionsController->registerActivity(
  4092. $line,
  4093. 'S002V01M10GMPR',
  4094. 'S002V01F07EPMA',
  4095. 'S002V01P02DPMA',
  4096. 'Registro',
  4097. "El usuario $name (" . $usr->USUA_IDUS . ") generó el archivo de plan de mantenimiento de la orden #$idOrder.",
  4098. $idUser,
  4099. $nowStr,
  4100. 'S002V01S02AOTR'
  4101. );
  4102. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4103. return $this->responseController->makeResponse(false, 'EXITO.', ['fileID' => $fileName]);
  4104. }
  4105. public function getMaintenancePlanAnalysis($idFile, $idUser, $line)
  4106. {
  4107. DB::enableQueryLog();
  4108. $idUser = $this->encryptionController->decrypt($idUser);
  4109. if (!$idUser) {
  4110. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  4111. }
  4112. $usr = DB::table('S002V01TUSUA')->where([
  4113. ['USUA_IDUS', '=', $idUser],
  4114. ['USUA_NULI', '=', $line]
  4115. ])->first();
  4116. if (is_null($usr)) {
  4117. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  4118. }
  4119. $idFile = $this->encryptionController->decrypt($idFile);
  4120. if (!$idFile) {
  4121. return $this->responseController->makeResponse(true, 'El ID del archivo solicitado no está encriptado correctamente.', [], 400);
  4122. }
  4123. $tempFile = DB::table('S002V01TARTE')->where([
  4124. ['ARTE_NULI', '=', $line],
  4125. ['ARTE_IDAR', '=', $idFile]
  4126. ])->first();
  4127. if (is_null($tempFile)) {
  4128. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  4129. }
  4130. $fileColumnsEsp = [
  4131. 'Id',
  4132. 'Activo',
  4133. 'Modo de tarea',
  4134. 'Nombre',
  4135. 'Duración',
  4136. 'Comienzo',
  4137. 'Fin',
  4138. 'Trabajo_Programado',
  4139. 'Costo',
  4140. 'Duración de línea base estimada',
  4141. 'Comienzo previsto',
  4142. 'Fin de línea base',
  4143. 'Trabajo previsto',
  4144. 'Costo de línea base',
  4145. 'Variación de duración',
  4146. 'Variación de trabajo',
  4147. 'Variación de costo'
  4148. ];
  4149. $fileColumnsIng = ['ID', 'Active', 'Task Mode', 'Name', 'Duration', 'Start', 'Finish', 'Predecessors', 'Outline Level', 'Notes'];
  4150. $MONTHS_ESP = [
  4151. 'enero' => '01',
  4152. 'febrero' => '02',
  4153. 'marzo' => '03',
  4154. 'abril' => '04',
  4155. 'mayo' => '05',
  4156. 'junio' => '06',
  4157. 'julio' => '07',
  4158. 'agosto' => '08',
  4159. 'septiembre' => '09',
  4160. 'octubre' => '10',
  4161. 'noviembre' => '11',
  4162. 'diciembre' => '12'
  4163. ];
  4164. $MONTHS_ING = [
  4165. '1' => '01',
  4166. '2' => '02',
  4167. '3' => '03',
  4168. '4' => '04',
  4169. '5' => '05',
  4170. '6' => '06',
  4171. '7' => '07',
  4172. '8' => '08',
  4173. '9' => '09',
  4174. '10' => '10',
  4175. '11' => '11',
  4176. '12' => '12'
  4177. ];
  4178. $spreadsheet = IOFactory::load($tempFile->ARTE_UBTE);
  4179. $workSheet = $spreadsheet->getActiveSheet();
  4180. $maxColStr = $workSheet->getHighestColumn();
  4181. $maxCol = Coordinate::columnIndexFromString($maxColStr);
  4182. for ($i = 1; $i <= $maxCol; $i++) {
  4183. $colStr = Coordinate::stringFromColumnIndex($i);
  4184. $cell = $workSheet->getCell($colStr . "1");
  4185. $val = $cell->getValue();
  4186. if (!in_array($val, $fileColumnsEsp) && !in_array($val, $fileColumnsIng)) {
  4187. return $this->responseController->makeResponse(true, 'El archivo tiene un formato inválido.', [], 400);
  4188. }
  4189. }
  4190. $instructions = [];
  4191. $maxRow = $workSheet->getHighestRow();
  4192. for ($row = 2; $row <= $maxRow; $row++) {
  4193. $activo = $workSheet->getCell("B$row")->getValue();
  4194. $language = $activo == 'Yes' ? 'I' : 'E';
  4195. if ($activo == 'Sí' || $activo == 'Si' || $activo == 'Yes') {
  4196. $durationStr = $workSheet->getCell("E$row")->getValue();
  4197. $durationArr = explode(" ", $durationStr);
  4198. $duration = 0;
  4199. if ($durationArr[1] == 'días' || $durationArr[1] == 'dias' || $durationArr[1] == 'día' || $durationArr[1] == 'dia' || $durationArr[1] == 'days' || $durationArr[1] == 'day') {
  4200. $daysFloat = floatval($durationArr[0]);
  4201. $daysInt = intval($daysFloat);
  4202. $duration += $daysInt * 24;
  4203. $hoursDec = $this->functionsController->floatSub($daysFloat, $daysInt);
  4204. $hoursFloat = $this->functionsController->floatMul($hoursDec, 24);
  4205. $duration = $this->functionsController->floatAdd($duration, $hoursFloat);
  4206. }
  4207. if ($durationArr[1] == 'mins' || $durationArr[1] == 'min') {
  4208. $minsFloat = floatval($durationArr[0]);
  4209. $hoursFloat = $this->functionsController->floatDiv($minsFloat, 60);
  4210. $duration = $this->functionsController->floatAdd($duration, $hoursFloat);
  4211. }
  4212. $duration = round($duration, 5);
  4213. $startDateStr = $workSheet->getCell("F$row")->getValue();
  4214. $startDateArr = explode(" ", $startDateStr);
  4215. if ($language == 'I') {
  4216. $americanDateArr = explode('/', $startDateArr[1]);
  4217. $startMonth = $MONTHS_ING[$americanDateArr[0]];
  4218. } else {
  4219. $startMonth = $MONTHS_ESP[$startDateArr[1]];
  4220. $startPeriod = $startDateArr[4] . $startDateArr[5];
  4221. $startPeriod = str_replace('.', '', $startPeriod);
  4222. $startHourArr = explode(":", $startDateArr[3]);
  4223. $startHour = intval($startHourArr[0]);
  4224. $startHour = $startPeriod == 'pm' && $startHour < 12 ? $startHour + 12 : $startHour;
  4225. $startHourStr = $startHour < 10 ? "0$startHour" : "$startHour";
  4226. $startDateTime = "$startDateArr[2]-$startMonth-$startDateArr[0] $startHourStr:$startHourArr[1]:00";
  4227. $startDateTimeObj = new Carbon($startDateTime);
  4228. $startTimestamp = $startDateTimeObj->timestamp;
  4229. $endDateStr = $workSheet->getCell("G$row")->getValue();
  4230. $endDateArr = explode(" ", $endDateStr);
  4231. $endMonth = $MONTHS_ESP[$endDateArr[1]];
  4232. $endPeriod = $endDateArr[4] . $endDateArr[5];
  4233. $endPeriod = str_replace('.', '', $endPeriod);
  4234. $endHourArr = explode(":", $endDateArr[3]);
  4235. $endHour = intval($endHourArr[0]);
  4236. $endHour = $endPeriod == 'pm' && $endHour < 12 ? $endHour + 12 : $endHour;
  4237. $endHourStr = $endHour < 10 ? "0$endHour" : "$endHour";
  4238. $endDateTime = "$endDateArr[2]-$endMonth-$endDateArr[0] $endHourStr:$endHourArr[1]:00";
  4239. $endDateTimeObj = new Carbon($endDateTime);
  4240. $endTimestamp = $endDateTimeObj->timestamp;
  4241. }
  4242. $difference = $endTimestamp - $startTimestamp;
  4243. $minutes = $this->functionsController->floatDiv($difference, 60);
  4244. $hours = $this->functionsController->floatDiv($minutes, 60);
  4245. $duration = round($hours, 5);
  4246. $instructions[] = [
  4247. "INSTRUCTION" => $workSheet->getCell("D$row")->getValue(),
  4248. "DURATION" => $duration,
  4249. "START" => $startDateTime,
  4250. "END" => $endDateTime,
  4251. "COST" => $workSheet->getCell("I$row")->getValue(),
  4252. ];
  4253. }
  4254. }
  4255. $now = $this->functionsController->now();
  4256. $nowStr = $now->toDateTimeString();
  4257. $actions = DB::getQueryLog();
  4258. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4259. $idac = $this->functionsController->registerActivity(
  4260. $line,
  4261. 'S002V01M10GMPR',
  4262. 'S002V01F10DIBI',
  4263. '-',
  4264. 'Consulta',
  4265. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el plan de mantenimiento para una nueva orden desde MS Project.",
  4266. $idUser,
  4267. $nowStr,
  4268. 'S002V01S02AOTR'
  4269. );
  4270. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4271. return $this->responseController->makeResponse(false, 'EXITO.', $instructions);
  4272. }
  4273. public function getFileToMSProject($idOrder, $idUser, $line)
  4274. {
  4275. DB::enableQueryLog();
  4276. $idUser = $this->encryptionController->decrypt($idUser);
  4277. if (!$idUser) {
  4278. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  4279. }
  4280. $usr = DB::table('S002V01TUSUA')->where([
  4281. ['USUA_NULI', '=', $line],
  4282. ['USUA_IDUS', '=', $idUser],
  4283. ])->first();
  4284. if (is_null($usr)) {
  4285. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  4286. }
  4287. $idOrder = $this->encryptionController->decrypt($idOrder);
  4288. if (!$idOrder) {
  4289. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  4290. }
  4291. $order = DB::table('S002V01TOTPR')->where([
  4292. ['OTPR_IDOT', '=', $idOrder],
  4293. ['OTPR_NULI', '=', $line]
  4294. ])->first();
  4295. if (is_null($order)) {
  4296. return $this->responseController->makeResponse(true, 'La orden consultada no está registrada.', [], 404);
  4297. }
  4298. $cols = [
  4299. 'Id',
  4300. 'Activo',
  4301. 'Modo de tarea',
  4302. 'Nombre',
  4303. 'Duración',
  4304. 'Comienzo',
  4305. 'Fin',
  4306. 'Trabajo_Programado',
  4307. 'Costo',
  4308. 'Duración de línea base estimada',
  4309. 'Comienzo previsto',
  4310. 'Fin de línea base',
  4311. 'Trabajo previsto',
  4312. 'Costo de línea base',
  4313. 'Variación de duración',
  4314. 'Variación de trabajo',
  4315. 'Variación de costo'
  4316. ];
  4317. $analysis = json_decode($order->OTPR_SEAN, true);
  4318. $instructionsConf = /*$analysis['instructionsConf']*/ '{}';
  4319. $instructions = json_decode($order->OTPR_ININ, true);
  4320. $arrCols = [];
  4321. $cont = 1;
  4322. foreach ($instructions as $instruction) {
  4323. $projectDuration = "";
  4324. if ($instruction['DURACION'] < 24) {
  4325. $duration = $instruction['DURACION'] * 60;
  4326. $projectDuration = "$duration min";
  4327. if ($duration > 1) {
  4328. $projectDuration .= "s";
  4329. }
  4330. } else {
  4331. $duration = $this->functionsController->floatDiv($instruction['DURACION'], 24);
  4332. $duration = round($duration, 2);
  4333. $projectDuration = "$duration día";
  4334. if ($duration > 1) {
  4335. $projectDuration .= "s";
  4336. }
  4337. }
  4338. $instructionStartDateTimeArr = explode(' ', $instruction['INICIO']);
  4339. $instructionStartTimeArr = explode(':', $instructionStartDateTimeArr[1]);
  4340. $instructionStartHour = intval($instructionStartTimeArr[0]);
  4341. if ($instructionStartDateTimeArr[2] == 'PM' && $instructionStartHour < 12) {
  4342. $instructionStartHour = $instructionStartHour + 12;
  4343. }
  4344. $instructionStartHourStr = $instructionStartHour < 10 ? "0$instructionStartHour" : "$instructionStartHour";
  4345. $instructionStartTimeStr = "$instructionStartHourStr:$instructionStartTimeArr[1]:00";
  4346. $instructionStartDateTimeStr = "$instructionStartDateTimeArr[0] $instructionStartTimeStr";
  4347. $instructionEndDateTimeArr = explode(' ', $instruction['FIN']);
  4348. $instructionEndTimeArr = explode(':', $instructionEndDateTimeArr[1]);
  4349. $instructionEndHour = intval($instructionEndTimeArr[0]);
  4350. if ($instructionEndDateTimeArr[2] == 'PM' && $instructionEndHour < 12) {
  4351. $instructionEndHour = $instructionEndHour + 12;
  4352. }
  4353. $instructionEndHourStr = $instructionEndHour < 10 ? "0$instructionEndHour" : "$instructionEndHour";
  4354. $instructionEndTimeStr = "$instructionEndHourStr:$instructionEndTimeArr[1]:00";
  4355. $instructionEndDateTimeStr = "$instructionEndDateTimeArr[0] $instructionEndTimeStr";
  4356. $projectStartDate = $this->functionsController->buildProjectDate($instructionStartDateTimeStr);
  4357. $projectEndDate = $this->functionsController->buildProjectDate($instructionEndDateTimeStr);
  4358. $cost = /*$instruction['ISFROMFILE'] ? $instruction['COSTO'] : $instructionsConf[$instruction['ID']]*/ '0';
  4359. $arrCol = [
  4360. "A" => $cont,
  4361. "B" => 'Sí',
  4362. "C" => 'Programada manualmente',
  4363. "D" => $instruction['INSTRUCCION'],
  4364. "E" => $projectDuration,
  4365. "F" => $projectStartDate,
  4366. "G" => $projectEndDate,
  4367. "H" => "0h",
  4368. "I" => $cost,
  4369. "J" => "0d",
  4370. "K" => "NOD",
  4371. "L" => "NOD",
  4372. "M" => "0h",
  4373. "N" => "0",
  4374. "O" => $projectDuration,
  4375. "P" => "0h",
  4376. "Q" => "0",
  4377. ];
  4378. $arrCols[] = $arrCol;
  4379. $cont++;
  4380. }
  4381. $now = $this->functionsController->now();
  4382. $nowStr = $now->toDateTimeString();
  4383. $dateTimeArr = explode(" ", $nowStr);
  4384. $dateArr = explode("-", $dateTimeArr[0]);
  4385. $year = substr($dateArr[0], 2);
  4386. $como = 'GMPR';
  4387. $cldo = 'OR';
  4388. $fecr = $year . $dateArr[1] . $dateArr[2];
  4389. $sec = DB::table('S002V01TAFAL')->where([
  4390. ['AFAL_NULI', '=', $line],
  4391. ['AFAL_COMO', '=', $como],
  4392. ['AFAL_CLDO', '=', $cldo],
  4393. ])->orderBy('AFAL_NUSE', 'desc')->first();
  4394. $nuse = "";
  4395. if (is_null($sec)) {
  4396. $nuse = '000001';
  4397. } else {
  4398. $secu = "" . intval($sec->AFAL_NUSE) + 1 . "";
  4399. $nuse = "";
  4400. for ($i = strlen($secu); $i < 6; $i++) {
  4401. $nuse .= "0";
  4402. }
  4403. $nuse = $nuse . $secu;
  4404. }
  4405. $noar = "plan_de_mantenimiento_preventivo_ms_project_orden_$idOrder";
  4406. $exte = "xlsx";
  4407. $ver = DB::table('S002V01TAFAL')->where([
  4408. ['AFAL_NULI', '=', $line],
  4409. ['AFAL_COMO', '=', $como],
  4410. ['AFAL_CLDO', '=', $cldo],
  4411. ['AFAL_NOAR', '=', $noar],
  4412. ['AFAL_EXTE', '=', $exte],
  4413. ])->orderBy('AFAL_NUVE', 'desc')->first();
  4414. $nuve = "";
  4415. if (is_null($ver)) {
  4416. $nuve = "01";
  4417. } else {
  4418. $vers = intval($ver->AFAL_NUVE) + 1;
  4419. $nuve = $vers < 10 ? "0$vers" : "$vers";
  4420. }
  4421. $line = $line < 10 ? "0$line" : "$line";
  4422. $filePath = $this->functionsController->getBasePath() . '\public_files\\';
  4423. $fileName = "$line-$como-$cldo-$fecr-$nuse=$nuve=$noar.$exte";
  4424. $tempFile = $filePath . $fileName;
  4425. if (file_exists($tempFile)) {
  4426. unlink($tempFile);
  4427. }
  4428. $spreadsheet = new Spreadsheet();
  4429. $workSheet = $spreadsheet->getActiveSheet();
  4430. $colInd = 1;
  4431. foreach ($cols as $colName) {
  4432. $colStr = Coordinate::stringFromColumnIndex($colInd);
  4433. $workSheet->setCellValue($colStr . "1", $colName);
  4434. $workSheet->getColumnDimension($colStr)->setAutoSize(true);
  4435. $colInd++;
  4436. }
  4437. $row = 2;
  4438. foreach ($arrCols as $col) {
  4439. foreach ($col as $k => $v) {
  4440. $workSheet->setCellValue($k . $row, $v);
  4441. }
  4442. $row++;
  4443. }
  4444. $writer = new Xlsx($spreadsheet);
  4445. $writer->save($tempFile);
  4446. $ubic = Storage::putFile('files', new File($tempFile));
  4447. $ubic = str_replace("/", "\\", $ubic);
  4448. $ubic = $this->functionsController->getBasePath() . "\storage\app\\" . $ubic;
  4449. $tama = filesize($ubic);
  4450. $usac = json_encode([$idUser]);
  4451. unlink($tempFile);
  4452. DB::table('S002V01TAFAL')->insert([
  4453. 'AFAL_NULI' => $line,
  4454. 'AFAL_COMO' => $como,
  4455. 'AFAL_CLDO' => $cldo,
  4456. 'AFAL_FECR' => $fecr,
  4457. 'AFAL_NUSE' => $nuse,
  4458. 'AFAL_NUVE' => $nuve,
  4459. 'AFAL_NOAR' => $noar,
  4460. 'AFAL_EXTE' => $exte,
  4461. 'AFAL_TAMA' => $tama,
  4462. 'AFAL_UBIC' => $ubic,
  4463. 'AFAL_USAC' => $usac,
  4464. 'AFAL_USRE' => $idUser,
  4465. 'AFAL_FERE' => $nowStr,
  4466. ]);
  4467. $filesArr = json_decode($order->OTPR_DONE, true);
  4468. $filesArr[] = $fileName;
  4469. $filesStr = json_encode($filesArr);
  4470. DB::table('S002V01TOTPR')->where([
  4471. ['OTPR_NULI', '=', $line],
  4472. ['OTPR_IDOT', '=', $idOrder]
  4473. ])->update([
  4474. 'OTPR_DONE' => $filesStr,
  4475. 'OTPR_USMO' => $idUser,
  4476. 'OTPR_FEMO' => $nowStr
  4477. ]);
  4478. $actions = DB::getQueryLog();
  4479. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4480. $idac = $this->functionsController->registerActivity(
  4481. $line,
  4482. 'S002V01M10GMPR',
  4483. 'S002V01F10DIBI',
  4484. '-',
  4485. 'Registro',
  4486. "El usuario $name (" . $usr->USUA_IDUS . ") generó el archivo de plan de mantenimiento para MS Project de la orden #$idOrder.",
  4487. $idUser,
  4488. $nowStr,
  4489. 'S002V01S02AOTR'
  4490. );
  4491. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4492. return $this->responseController->makeResponse(false, 'EXITO.', ['fileID' => $fileName]);
  4493. }
  4494. public function getOrderStaff($idOrder, $idUser, $line)
  4495. {
  4496. DB::enableQueryLog();
  4497. $idUser = $this->encryptionController->decrypt($idUser);
  4498. if (!$idUser) {
  4499. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  4500. }
  4501. $usr = DB::table('S002V01TUSUA')->where([
  4502. ['USUA_NULI', '=', $line],
  4503. ['USUA_IDUS', '=', $idUser],
  4504. ])->first();
  4505. if (is_null($usr)) {
  4506. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  4507. }
  4508. $idOrder = $this->encryptionController->decrypt($idOrder);
  4509. if (!$idOrder) {
  4510. return $this->responseController->makeResponse(true, 'El ID de la orden solicitada no está encriptado correctamente.', [], 400);
  4511. }
  4512. $order = DB::table('S002V01TOTPR')->where([
  4513. ['OTPR_IDOT', '=', $idOrder],
  4514. ['OTPR_NULI', '=', $line]
  4515. ])->first();
  4516. if (is_null($order)) {
  4517. return $this->responseController->makeResponse(true, 'La orden consultada no está registrada.', [], 404);
  4518. }
  4519. $staffArr = json_decode($order->OTPR_OPPR, true);
  4520. $staffTmp = [];
  4521. foreach ($staffArr as $val) {
  4522. if ($val['TYPE'] == 'EQ') {
  4523. $employees = DB::table('S002V01TPERS')->where([
  4524. ['PERS_NULI', '=', $line],
  4525. ['PERS_EQTR', '=', $val['ID']],
  4526. ])->get()->all();
  4527. foreach ($employees as $employee) {
  4528. if (!in_array($employee->PERS_IDPE, $staffTmp)) {
  4529. $staffTmp[] = $employee->PERS_IDPE;
  4530. }
  4531. }
  4532. } else if ($val['TYPE'] == 'SU') {
  4533. $employees = DB::table('S002V01TPERS')->where([
  4534. ['PERS_NULI', '=', $line],
  4535. ['PERS_IDPS', '=', $val['ID']],
  4536. ])->get()->all();
  4537. foreach ($employees as $employee) {
  4538. if (!in_array($employee->PERS_IDPE, $staffTmp)) {
  4539. $staffTmp[] = $employee->PERS_IDPE;
  4540. }
  4541. }
  4542. } else {
  4543. $id = intval($val['ID']);
  4544. if (!in_array($id, $staffTmp)) {
  4545. $staffTmp[] = $id;
  4546. }
  4547. }
  4548. }
  4549. $staffFn = [];
  4550. foreach ($staffTmp as $empID) {
  4551. $employee = DB::table('S002V01TPERS')->select([
  4552. 'PERS_IDPE AS IDEMPLEADO',
  4553. 'PERS_IDUS AS IDUSUARIO',
  4554. DB::raw('TRIM(CONCAT(USUA_NOMB, " " , USUA_APPA, " ", COALESCE(USUA_APMA,""))) AS NOMBREUSUARIO'),
  4555. 'PERS_TICO AS TIPOCONTRATO',
  4556. 'PERS_IDPS AS IDSUBCONTRATISTA',
  4557. 'PESU_RASO AS RAZONSOCIAL',
  4558. 'PESU_REFI AS REGIMENFISCAL',
  4559. 'PERS_EQTR AS IDEQUIPO',
  4560. 'EQMA_NOMB AS NOMBREEQUIPO'
  4561. ])->join('S002V01TUSUA', 'USUA_IDUS', '=', 'PERS_IDUS')
  4562. ->leftJoin('S002V01TPESU', 'PESU_IDPS', '=', 'PERS_IDPS')
  4563. ->leftJoin('S002V01TEQMA', 'EQMA_IDEQ', '=', 'PERS_EQTR')->where([
  4564. ['PERS_NULI', '=', $line],
  4565. ['PERS_IDPE', '=', $empID],
  4566. ])->first();
  4567. if (!is_null($employee)) {
  4568. $staffFn[] = $employee;
  4569. }
  4570. }
  4571. $now = $this->functionsController->now();
  4572. $nowStr = $now->toDateTimeString();
  4573. $actions = DB::getQueryLog();
  4574. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4575. $idac = $this->functionsController->registerActivity(
  4576. $line,
  4577. 'S002V01M10GMPR',
  4578. 'S002V01F01COTP',
  4579. 'S002V01P03COTP',
  4580. 'Consulta',
  4581. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los empleados de la orden #$idOrder.",
  4582. $idUser,
  4583. $nowStr,
  4584. 'S002V01S01ORTR'
  4585. );
  4586. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4587. return $this->responseController->makeResponse(false, 'EXITO.', $staffFn);
  4588. }
  4589. public function getOrderExecutionDetails($idExecution, $idUser, $line)
  4590. {
  4591. DB::enableQueryLog();
  4592. $idUser = $this->encryptionController->decrypt($idUser);
  4593. if (!$idUser) {
  4594. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  4595. }
  4596. $usr = DB::table('S002V01TUSUA')->where([
  4597. ['USUA_NULI', '=', $line],
  4598. ['USUA_IDUS', '=', $idUser],
  4599. ])->first();
  4600. if (is_null($usr)) {
  4601. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  4602. }
  4603. $idExecution = $this->encryptionController->decrypt($idExecution);
  4604. if (!$idExecution) {
  4605. return $this->responseController->makeResponse(true, 'El ID de la ejecución solicitada no está encriptado correctamente.', [], 400);
  4606. }
  4607. $idExecArr = explode('|', $idExecution);
  4608. if (count($idExecArr) < 4) {
  4609. return $this->responseController->makeResponse(true, 'El ID de la ejecución solicitada tiene un formato inválido.', [], 400);
  4610. }
  4611. $execution = DB::table('S002V01TBEOT')->select([
  4612. 'BEOT_IDRE AS IDREGISTRO',
  4613. 'BEOT_IDOT AS IDORDEN',
  4614. 'BEOT_FEPR AS FECHAPROGRAMACION',
  4615. 'BEOT_TIAC AS TIPOACCION',
  4616. 'BEOT_TIOR AS TIPOORDEN',
  4617. 'BEOT_DTEJ AS TIEMPOEJECUCION',
  4618. 'BEOT_OBSE AS OBSERVACIONES',
  4619. 'BEOT_FEEJ AS FECHAEJECUCION',
  4620. 'BEOT_USEJ AS USUARIOEJECUCION',
  4621. 'BEOT_FEFI AS FECHAFINALIZACION',
  4622. 'BEOT_USFI AS USUARIOFINALIZO'
  4623. ])->where([
  4624. ['BEOT_IDRE', '=', $idExecArr[0]],
  4625. ['BEOT_NULI', '=', $line],
  4626. ['BEOT_IDOT', '=', $idExecArr[2]],
  4627. ['BEOT_FEPR', '=', $idExecArr[3]],
  4628. ])->first();
  4629. if (is_null($execution)) {
  4630. return $this->responseController->makeResponse(true, 'El registro de la ejecución solicitada no existe.', [], 404);
  4631. }
  4632. $execution->IDREGISTRO = $this->encryptionController->encrypt($execution->IDREGISTRO);
  4633. $execution->IDORDEN = $this->encryptionController->encrypt($execution->IDORDEN);
  4634. $execUsr = DB::table('S002V01TUSUA')->where([
  4635. ['USUA_IDUS', '=', $execution->USUARIOEJECUCION],
  4636. ['USUA_NULI', '=', $line]
  4637. ])->first();
  4638. $execName = $this->functionsController->joinName($execUsr->USUA_NOMB, $execUsr->USUA_APPA, $execUsr->USUA_APMA);
  4639. $execution->USUARIOEJECUCION = $execName . " (" . $execution->USUARIOEJECUCION . ")";
  4640. if (!is_null($execution->USUARIOFINALIZO)) {
  4641. $finUsr = DB::table('S002V01TUSUA')->where([
  4642. ['USUA_IDUS', '=', $execution->USUARIOFINALIZO],
  4643. ['USUA_NULI', '=', $line]
  4644. ])->first();
  4645. $finName = $this->functionsController->joinName($finUsr->USUA_NOMB, $finUsr->USUA_APPA, $finUsr->USUA_APMA);
  4646. $execution->USUARIOFINALIZO = $finName . " (" . $execution->USUARIOFINALIZO . ")";
  4647. }
  4648. $now = $this->functionsController->now();
  4649. $nowStr = $now->toDateTimeString();
  4650. $actions = DB::getQueryLog();
  4651. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4652. $idac = $this->functionsController->registerActivity(
  4653. $line,
  4654. 'S002V01M10GMPR',
  4655. 'S002V01F01COTP',
  4656. 'S002V01P03COTP',
  4657. 'Consulta',
  4658. "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].",
  4659. $idUser,
  4660. $nowStr,
  4661. 'S002V01S01ORTR'
  4662. );
  4663. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4664. return $this->responseController->makeResponse(false, 'EXITO.', $execution);
  4665. }
  4666. public function getWorkOrdersByEquipment($equipmentCode, $idUser, $line)
  4667. {
  4668. DB::enableQueryLog();
  4669. $idUser = $this->encryptionController->decrypt($idUser);
  4670. if (!$idUser) {
  4671. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  4672. }
  4673. $usr = DB::table('S002V01TUSUA')->where([
  4674. ['USUA_NULI', '=', $line],
  4675. ['USUA_IDUS', '=', $idUser],
  4676. ])->first();
  4677. if (is_null($usr)) {
  4678. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  4679. }
  4680. $equipmentCode = $this->encryptionController->decrypt($equipmentCode);
  4681. if (!$equipmentCode) {
  4682. return $this->responseController->makeResponse(true, 'El código del equipamiento relacionado no está encriptado correctamente.', [], 400);
  4683. }
  4684. $equipment = DB::table('S002V01TEQUI')->where([
  4685. ['EQUI_NULI', '=', $line],
  4686. ['EQUI_COEQ', '=', $equipmentCode]
  4687. ])->first();
  4688. if (is_null($equipment)) {
  4689. return $this->responseController->makeResponse(true, 'El equipamiento relacionado no está registrado.', [], 404);
  4690. }
  4691. $workOrders = DB::table('S002V01TOTPR')->select([
  4692. 'OTPR_IDOT AS IDORDEN',
  4693. 'OTPR_EQIN AS EQUIPO',
  4694. 'OTPR_FIAP AS FECHAINICIO',
  4695. 'OTPR_FTAP AS FECHAFINAL',
  4696. 'OTPR_ACAS AS ACTIVADOR',
  4697. 'ACTI_TIAC AS TIPOACTIVADOR',
  4698. 'ACTI_PRIO AS PRIORIDAD',
  4699. 'OTPR_ESTA AS ESTATUS'
  4700. ])->where([
  4701. ['OTPR_NULI', '=', $line],
  4702. ['OTPR_EQIN', '=', $equipmentCode],
  4703. ['OTPR_ESTA', '!=', 'E'],
  4704. ])->leftJoin('S002V01TACTI', 'ACTI_IDAC', '=', 'OTPR_ACAS')
  4705. ->orderBy('OTPR_IDOT', 'desc')->get()->all();
  4706. foreach ($workOrders as $key => $order) {
  4707. $order->IDORDEN = $this->encryptionController->encrypt($order->IDORDEN);
  4708. $order->EQUIPO = $this->encryptionController->encrypt($order->EQUIPO);
  4709. $order->ACTIVADOR = $this->encryptionController->encrypt($order->ACTIVADOR);
  4710. if (!is_null($order->PRIORIDAD)) {
  4711. $order->PRIORIDAD = $this->encryptionController->encrypt($order->PRIORIDAD);
  4712. }
  4713. if ($order->FECHAFINAL == '0001-01-01 00:00:00') $order->FECHAFINAL = null;
  4714. if ($order->FECHAINICIO == '0001-01-01 00:00:00') $order->FECHAINICIO = null;
  4715. $workOrders[$key] = $order;
  4716. }
  4717. $now = $this->functionsController->now();
  4718. $nowStr = $now->toDateTimeString();
  4719. $actions = DB::getQueryLog();
  4720. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4721. $idac = $this->functionsController->registerActivity(
  4722. $line,
  4723. 'S002V01M10GMPR',
  4724. 'S002V01F01COTP',
  4725. 'S002V01P01HOTP',
  4726. 'Consulta',
  4727. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las órdenes de trabajo relacionadas al equipamient $equipmentCode.",
  4728. $idUser,
  4729. $nowStr,
  4730. 'S002V01S01ORTR'
  4731. );
  4732. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4733. return $this->responseController->makeResponse(false, 'EXITO.', $workOrders);
  4734. }
  4735. // Visitas técnicas no programadas (Preventivas)
  4736. public function getVisit($idVisit, $idUser, $line)
  4737. {
  4738. DB::enableQueryLog();
  4739. $idUser = $this->encryptionController->decrypt($idUser);
  4740. if (!$idUser) {
  4741. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  4742. }
  4743. $usr = DB::table('S002V01TUSUA')->where([
  4744. ['USUA_NULI', '=', $line],
  4745. ['USUA_IDUS', '=', $idUser],
  4746. ])->first();
  4747. if (is_null($usr)) {
  4748. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  4749. }
  4750. $idVisit = $this->encryptionController->decrypt($idVisit);
  4751. if (!$idVisit) {
  4752. return $this->responseController->makeResponse(true, 'El ID de la visita no está encriptado correctamente.', [], 400);
  4753. }
  4754. $visit = DB::table('S002V01TRVTN')->select([
  4755. 'RVTN_IDVI AS ID_ORDEN',
  4756. 'RVTN_DEIN AS DESCRIPCION',
  4757. 'RVTN_EQRE AS CODIGO_EQUIPAMIENTO',
  4758. 'EQUI_TIPO AS TIPO_EQUIPAMIENTO',
  4759. 'EQUI_MODE AS MODELO_EQUIPAMIENTO',
  4760. 'EQUI_IDEQ AS ID_EQUIPAMIENTO',
  4761. 'RVTN_TESO AS TIEMPO_ESTIMADO',
  4762. 'RVTN_CORE AS CONTADOR',
  4763. 'RVTN_DRCO AS MEDIDAS',
  4764. 'RVTN_PRIO AS PRIORIDAD',
  4765. 'RVTN_MAUT AS RECURSOS',
  4766. 'RVTN_TIAC AS TIPO_ACTIVACION',
  4767. 'RVTN_DORE AS DOCUMENTOS_RELACIONADOS',
  4768. 'RVTN_CLAS AS CLASIFICACION',
  4769. 'RVTN_COME AS COMENTARIOS',
  4770. 'RVTN_ESTA AS ESTADO',
  4771. 'RVTN_USRE AS USUREG',
  4772. 'RVTN_FERE AS FECREG',
  4773. 'RVTN_HIES AS HISTORIAL',
  4774. 'RVTN_PEIN AS PERSONAL'
  4775. ])->leftJoin('S002V01TEQUI', 'EQUI_COEQ', '=', 'RVTN_EQRE')
  4776. ->where([
  4777. ['RVTN_IDVI', '=', $idVisit],
  4778. ['RVTN_NULI', '=', $line],
  4779. ])->first();
  4780. if (is_null($visit)) {
  4781. return $this->responseController->makeResponse(true, 'La visita consultada no existe.', [], 404);
  4782. }
  4783. $visit->ID_ORDEN = $this->encryptionController->encrypt($visit->ID_ORDEN);
  4784. $visit->CODIGO_EQUIPAMIENTO = $this->encryptionController->encrypt($visit->CODIGO_EQUIPAMIENTO);
  4785. if (!is_null($visit->ID_EQUIPAMIENTO)) {
  4786. $visit->ID_EQUIPAMIENTO = $this->encryptionController->encrypt($visit->ID_EQUIPAMIENTO);
  4787. }
  4788. $visit->CONTADOR = $this->encryptionController->encrypt($visit->CONTADOR);
  4789. $visit->PRIORIDAD = $this->encryptionController->encrypt($visit->PRIORIDAD);
  4790. // Procesar PERSONAL
  4791. $staffArr = json_decode($visit->PERSONAL, true);
  4792. foreach ($staffArr as $key => $val) {
  4793. $specialty = DB::table('S002V01TGEES')->where([
  4794. ['GEES_NULI', '=', $line],
  4795. ['GEES_COES', '=', $val['ID']]
  4796. ])->first();
  4797. $val['ID'] = $this->encryptionController->encrypt($val['ID']);
  4798. $val['NAME'] = $specialty->GEES_NOES;
  4799. $staffArr[$key] = $val;
  4800. }
  4801. $visit->PERSONAL = json_encode($staffArr);
  4802. // Procesar MEDIDAS_OBJ
  4803. $measureArr = json_decode($visit->MEDIDAS, true);
  4804. if (!empty($measureArr)) {
  4805. $measureArr['CONTADOR'] = $this->encryptionController->encrypt($measureArr['CONTADOR']);
  4806. $measureArr['ID_MEDIDA'] = $this->encryptionController->encrypt($measureArr['ID_MEDIDA']);
  4807. $measureArr['ID_SERVICIO_WEB'] = $this->encryptionController->encrypt($measureArr['ID_SERVICIO_WEB']);
  4808. }
  4809. $visit->MEDIDAS_OBJ = $measureArr;
  4810. unset($visit->MEDIDAS);
  4811. // Procesar RECURSOS_ARR
  4812. $resources = json_decode($visit->RECURSOS, true);
  4813. $resourcesFn = [];
  4814. foreach ($resources as $resource) {
  4815. if ($resource['ID'] != 'SH') {
  4816. $resourceObj = DB::table('S002V01TINST')->where([
  4817. ['INST_NULI', '=', $line],
  4818. ['INST_IDIS', '=', $resource['ID']],
  4819. ])->first();
  4820. if (!is_null($resourceObj)) {
  4821. $resourcesFn[] = $resourceObj->INST_MODE;
  4822. }
  4823. }
  4824. }
  4825. $visit->RECURSOS_ARR = $resourcesFn;
  4826. // Procesar DOCUMENTOS_RELACIONADOS_ARR
  4827. $documentsArr = json_decode($visit->DOCUMENTOS_RELACIONADOS, true);
  4828. $documentsFn = [];
  4829. foreach ($documentsArr as $document) {
  4830. $documentArr = explode('=', $document);
  4831. $codeArr = explode('-', $documentArr[0]);
  4832. $file = DB::table('S002V01TAFAL')->where([
  4833. ['AFAL_NULI', '=', $line],
  4834. ['AFAL_COMO', '=', $codeArr[1]],
  4835. ['AFAL_CLDO', '=', $codeArr[2]],
  4836. ['AFAL_FECR', '=', $codeArr[3]],
  4837. ['AFAL_NUSE', '=', $codeArr[4]],
  4838. ['AFAL_NUVE', '=', $documentArr[1]]
  4839. ])->first();
  4840. $documentsFn[] = [
  4841. 'name' => $file->AFAL_NOAR . '.' . $file->AFAL_EXTE,
  4842. 'size' => $file->AFAL_TAMA
  4843. ];
  4844. }
  4845. $visit->DOCUMENTOS_RELACIONADOS_ARR = $documentsFn;
  4846. // Mapear TIPO_ACTIVACION
  4847. $activationTypes = ['M' => 'Manual', 'A' => 'Automática'];
  4848. $visit->TIPO_ACTIVACION = $activationTypes[$visit->TIPO_ACTIVACION];
  4849. // Mapear ESTADO (RVTN_ESTA enum: 'VA','EP','CP','CE','P','C','R','A','F')
  4850. // VA, EP, CP, CE: Solo para RVTN_TIAC='A' (Automática)
  4851. // P, C, R, A, F: Solo para RVTN_TIAC='M' (Manual)
  4852. $orderStates = [
  4853. 'VA' => 'Validado', // Automática
  4854. 'EP' => 'En progreso', // Automática
  4855. 'CP' => 'Cerrado pendiente', // Automática
  4856. 'CE' => 'Cerrado', // Automática
  4857. 'PE' => 'Pendiente', // Manual
  4858. 'CA' => 'Cancelado', // Manual
  4859. 'RE' => 'Rechazado', // Manual
  4860. 'EL' => 'Eliminado', // Manual
  4861. ];
  4862. $visit->ESTADO = $orderStates[$visit->ESTADO];
  4863. // Procesar usuarios
  4864. $usrReg = DB::table('S002V01TUSUA')->where([
  4865. ['USUA_NULI', '=', $line],
  4866. ['USUA_IDUS', '=', $visit->USUREG],
  4867. ])->first();
  4868. $nameReg = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  4869. $visit->USUREG = $nameReg . " (" . $visit->USUREG . ")";
  4870. $now = $this->functionsController->now();
  4871. $nowStr = $now->toDateTimeString();
  4872. $actions = DB::getQueryLog();
  4873. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  4874. $idac = $this->functionsController->registerActivity(
  4875. $line,
  4876. 'S002V01M10GMPR',
  4877. 'S002V01F11RVTP',
  4878. 'S002V01P02COVI',
  4879. 'Consulta',
  4880. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la visita técnica #$idVisit.",
  4881. $idUser,
  4882. $nowStr,
  4883. 'S002V01S02AOTR'
  4884. );
  4885. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  4886. return $this->responseController->makeResponse(false, 'EXITO.', $visit);
  4887. }
  4888. // Visitas técnicas no programadas (Preventivas)
  4889. public function assignOperariosToPreventiveVisit(Request $request)
  4890. {
  4891. DB::enableQueryLog();
  4892. $validator = Validator::make($request->all(), [
  4893. 'id_user' => 'required|string',
  4894. 'linea' => 'required|integer',
  4895. 'id_order' => 'required|string',
  4896. 'config' => 'required|json',
  4897. ]);
  4898. if ($validator->fails()) {
  4899. return $this->responseController->makeResponse(
  4900. true,
  4901. "Se encontraron uno o más errores.",
  4902. $this->responseController->makeErrors(
  4903. $validator->errors()->messages()
  4904. ),
  4905. 401
  4906. );
  4907. }
  4908. $form = $request->all();
  4909. $idUser = $this->encryptionController->decrypt($form['id_user']);
  4910. if (!$idUser) {
  4911. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  4912. }
  4913. $usr = DB::table('S002V01TUSUA')->where([
  4914. ['USUA_NULI', '=', $form['linea']],
  4915. ['USUA_IDUS', '=', $idUser]
  4916. ])->first();
  4917. if (is_null($usr)) {
  4918. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  4919. }
  4920. $idVisit = $this->encryptionController->decrypt($form['id_order']);
  4921. if (!$idVisit) {
  4922. return $this->responseController->makeResponse(true, 'El ID de la visita no fue encriptado correctamente.', [], 400);
  4923. }
  4924. $visit = DB::table('S002V01TRVTN')->where([
  4925. ['RVTN_NULI', '=', $form['linea']],
  4926. ['RVTN_IDVI', '=', $idVisit]
  4927. ])->first();
  4928. if (is_null($visit)) {
  4929. return $this->responseController->makeResponse(true, 'La visita solicitada no existe.', [], 404);
  4930. } else if ($visit->RVTN_ESTA != 'PE' && $visit->RVTN_ESTA != 'VA') {
  4931. return $this->responseController->makeResponse(true, 'La visita no está en un estado válido para asignar operarios.', [], 401);
  4932. }
  4933. $staffConfigArr = json_decode($form['config'], true);
  4934. $audience = [];
  4935. foreach ($staffConfigArr as $key => $val) {
  4936. if (!array_key_exists('SPECIALTY', $val) || !array_key_exists('STAFF', $val)) {
  4937. return $this->responseController->makeResponse(true, "El item $key del arreglo de configuración de operarios tiene un formato inválido.", [], 400);
  4938. }
  4939. $specialtyDec = $this->encryptionController->decrypt($val['SPECIALTY']);
  4940. if (!$specialtyDec) {
  4941. 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);
  4942. }
  4943. if (gettype($val['STAFF']) != 'array') {
  4944. return $this->responseController->makeResponse(true, "El contenedor de operarios en el item $key del arreglo de configuración de operarios es inválido.", [], 400);
  4945. }
  4946. foreach ($val['STAFF'] as $key0 => $val0) {
  4947. if (!array_key_exists('ID', $val0) || !array_key_exists('TYPE', $val0)) {
  4948. return $this->responseController->makeResponse(true, "El item $key del arreglo de configuración de operarios tiene un formato inválido.", [], 400);
  4949. }
  4950. $idDec = $this->encryptionController->decrypt($val0['ID']);
  4951. $employee = DB::table('S002V01TPERS')->where([
  4952. ['PERS_NULI', '=', $form['linea']],
  4953. ['PERS_IDPE', '=', $idDec]
  4954. ])->first();
  4955. if (is_null($employee)) {
  4956. return $this->responseController->makeResponse(true, "El operario $key0 de la especialidad $specialtyDec del arreglo de configuración de operarios no existe.", [], 404);
  4957. }
  4958. $audience[] = $employee->PERS_IDUS;
  4959. }
  4960. }
  4961. // Validación de cupo global (basado en RVTN_PEIN)
  4962. $planilla = json_decode($visit->RVTN_PEIN, true);
  4963. $cupoRequerido = 0;
  4964. if (!empty($planilla) && is_array($planilla)) {
  4965. foreach ($planilla as $pe) {
  4966. $cupoRequerido += intval($pe['CANT'] ?? 0);
  4967. }
  4968. }
  4969. // Obtener último VA
  4970. $statusHistoryArr = json_decode($visit->RVTN_HIES, true);
  4971. $vaObj = null;
  4972. if (!empty($statusHistoryArr)) {
  4973. for ($i = count($statusHistoryArr) - 1; $i >= 0; $i--) {
  4974. if ($statusHistoryArr[$i]['ESTADO'] == 'VA') {
  4975. $vaObj = $statusHistoryArr[$i];
  4976. break;
  4977. }
  4978. }
  4979. }
  4980. $atencionActual = $vaObj && array_key_exists('ATENCION', $vaObj) ? $vaObj['ATENCION'] : [];
  4981. $aceptadosActual = 0;
  4982. foreach ($atencionActual as $itm) {
  4983. if (($itm['RESPUESTA'] ?? '') === 'A') $aceptadosActual++;
  4984. }
  4985. if ($cupoRequerido > 0 && $aceptadosActual >= $cupoRequerido) {
  4986. return $this->responseController->makeResponse(true, 'La planilla ya está completa. No se pueden asignar más operarios.', [], 401);
  4987. }
  4988. // Evitar invitación duplicada al mismo operario (ya invitado o ya respondió)
  4989. $yaInvitados = [];
  4990. foreach ($atencionActual as $itm) {
  4991. $yaInvitados[$itm['ID']] = $itm['RESPUESTA'] ?? '';
  4992. }
  4993. foreach ($audience as $opId) {
  4994. if (array_key_exists($opId, $yaInvitados)) {
  4995. return $this->responseController->makeResponse(true, "El operario $opId ya fue invitado o respondió anteriormente.", [], 401);
  4996. }
  4997. }
  4998. // Iniciar transacción
  4999. DB::beginTransaction();
  5000. try {
  5001. // Emitir notificación
  5002. $this->notificationsController->emitNotification(
  5003. 'S002V01M10GMPR',
  5004. "Visita de mantenimiento preventivo #$idVisit",
  5005. "Se ha asignado la visita de mantenimiento preventivo #$idVisit y requiere su atención.",
  5006. [[
  5007. 'BOTON' => 'Ver detalles',
  5008. 'FUNCION' => 'openPreventiveWorkOrderDetails',
  5009. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit)])
  5010. ], [
  5011. 'BOTON' => 'Atender visita',
  5012. 'FUNCION' => 'attendPreventiveWorkOrder',
  5013. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit)])
  5014. ], [
  5015. 'BOTON' => 'Ir al módulo',
  5016. 'FUNCION' => 'openModule',
  5017. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMPR/AOTR/RVTP')])
  5018. ]],
  5019. $audience,
  5020. $idUser,
  5021. $form['linea'],
  5022. $this->getSocketClient(),
  5023. $idVisit,
  5024. 'Preventivo'
  5025. );
  5026. $now = $this->functionsController->now();
  5027. $nowStr = $now->toDateTimeString();
  5028. $statusHistoryArr = json_decode($visit->RVTN_HIES, true);
  5029. // Buscar si ya existe un objeto con estado VA
  5030. $vaStatusIndex = null;
  5031. foreach ($statusHistoryArr as $index => $status) {
  5032. if ($status['ESTADO'] == 'VA') {
  5033. $vaStatusIndex = $index;
  5034. break;
  5035. }
  5036. }
  5037. // Revalidar cupo justo antes de insertar (concurrencia)
  5038. $vaObjRef = null;
  5039. if (!empty($statusHistoryArr)) {
  5040. for ($i = count($statusHistoryArr) - 1; $i >= 0; $i--) {
  5041. if ($statusHistoryArr[$i]['ESTADO'] == 'VA') {
  5042. $vaObjRef = $statusHistoryArr[$i];
  5043. break;
  5044. }
  5045. }
  5046. }
  5047. $atencionRef = $vaObjRef && array_key_exists('ATENCION', $vaObjRef) ? $vaObjRef['ATENCION'] : [];
  5048. $aceptadosRef = 0;
  5049. foreach ($atencionRef as $itm) {
  5050. if (($itm['RESPUESTA'] ?? '') === 'A') $aceptadosRef++;
  5051. }
  5052. if ($cupoRequerido > 0 && $aceptadosRef >= $cupoRequerido) {
  5053. DB::rollBack();
  5054. return $this->responseController->makeResponse(true, 'La planilla ya está completa. No se pueden asignar más operarios.', [], 401);
  5055. }
  5056. // Crear nuevas invitaciones
  5057. $newInvitations = [];
  5058. foreach ($audience as $operarioId) {
  5059. $newInvitations[] = [
  5060. 'ID' => $operarioId,
  5061. 'FIRMA' => null,
  5062. 'RESPUESTA' => '',
  5063. 'COMENTARIOS' => ''
  5064. ];
  5065. }
  5066. if ($vaStatusIndex !== null) {
  5067. // Extender objeto VA existente
  5068. $statusHistoryArr[$vaStatusIndex]['ATENCION'] = array_merge(
  5069. $statusHistoryArr[$vaStatusIndex]['ATENCION'],
  5070. $newInvitations
  5071. );
  5072. // Agregar nuevos operarios a PERSONAL (sin duplicar)
  5073. $currentPersonal = $statusHistoryArr[$vaStatusIndex]['PERSONAL'] ?? [];
  5074. foreach ($audience as $operarioId) {
  5075. if (!in_array($operarioId, $currentPersonal)) {
  5076. $currentPersonal[] = $operarioId;
  5077. }
  5078. }
  5079. $statusHistoryArr[$vaStatusIndex]['PERSONAL'] = $currentPersonal;
  5080. } else {
  5081. // Crear nuevo objeto VA con todos los invitados en PERSONAL
  5082. $statusHistoryArr[] = [
  5083. 'USUARIO' => $idUser,
  5084. 'ESTADO' => 'VA',
  5085. 'FECHA' => $nowStr,
  5086. 'ATENCION' => $newInvitations,
  5087. 'PERSONAL' => $audience
  5088. ];
  5089. }
  5090. $statusHistoryStr = json_encode($statusHistoryArr);
  5091. // Actualizar la visita
  5092. DB::table('S002V01TRVTN')->where([
  5093. ['RVTN_IDVI', '=', $idVisit],
  5094. ['RVTN_NULI', '=', $form['linea']]
  5095. ])->update([
  5096. 'RVTN_ESTA' => 'VA',
  5097. 'RVTN_HIES' => $statusHistoryStr,
  5098. 'RVTN_USMO' => $idUser,
  5099. 'RVTN_FEMO' => $nowStr,
  5100. ]);
  5101. $actions = DB::getQueryLog();
  5102. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  5103. $idac = $this->functionsController->registerActivity(
  5104. $form['linea'],
  5105. 'S002V01M10GMPR',
  5106. 'S002V01F11RVTP',
  5107. 'S002V01P01REVI',
  5108. 'Actualización',
  5109. "El usuario $name (" . $usr->USUA_IDUS . ") asignó operarios a la visita preventiva #$idVisit.",
  5110. $idUser,
  5111. $nowStr,
  5112. 'S002V01S02AOTR'
  5113. );
  5114. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  5115. // Confirmar transacción
  5116. DB::commit();
  5117. return $this->responseController->makeResponse(false, 'EXITO.');
  5118. } catch (\Exception $e) {
  5119. // Revertir transacción en caso de error
  5120. DB::rollBack();
  5121. $actions = DB::getQueryLog();
  5122. $now = $this->functionsController->now();
  5123. $nowStr = $now->toDateTimeString();
  5124. $name = $this->functionsController->joinName($usr->USUA_NOMB ?? '', $usr->USUA_APPA ?? '', $usr->USUA_APMA ?? '');
  5125. $idac = $this->functionsController->registerActivity(
  5126. $form['linea'],
  5127. 'S002V01M10GMPR',
  5128. 'S002V01F11RVTP',
  5129. 'S002V01P01REVI',
  5130. 'Error',
  5131. "Error al asignar operarios a la visita preventiva #$idVisit: " . $e->getMessage(),
  5132. $idUser,
  5133. $nowStr,
  5134. 'S002V01S02AOTR'
  5135. );
  5136. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  5137. return $this->responseController->makeResponse(
  5138. true,
  5139. 'Ocurrió un error al asignar operarios a la visita preventiva.',
  5140. ['error' => $e->getMessage()],
  5141. 500
  5142. );
  5143. }
  5144. }
  5145. // Visitas técnicas no programadas (Preventivas)
  5146. public function getVisitAttendance($idVisit, $idUser, $line)
  5147. {
  5148. DB::enableQueryLog();
  5149. $idUser = $this->encryptionController->decrypt($idUser);
  5150. if (!$idUser) {
  5151. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  5152. }
  5153. $usr = DB::table('S002V01TUSUA')->where([
  5154. ['USUA_NULI', '=', $line],
  5155. ['USUA_IDUS', '=', $idUser],
  5156. ])->first();
  5157. if (is_null($usr)) {
  5158. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  5159. }
  5160. $idVisit = $this->encryptionController->decrypt($idVisit);
  5161. if (!$idVisit) {
  5162. return $this->responseController->makeResponse(true, 'El ID de la visita relacionada no está encriptado correctamente', [], 400);
  5163. }
  5164. $visit = DB::table('S002V01TRVTN')->where([
  5165. ['RVTN_NULI', '=', $line],
  5166. ['RVTN_IDVI', '=', $idVisit]
  5167. ])->first();
  5168. if (is_null($visit)) {
  5169. return $this->responseController->makeResponse(true, 'La visita relacionada no está registrada.', [], 404);
  5170. }
  5171. // Validar que la visita esté en estado VA (validado)
  5172. // Puede estar en estado VA en RVTN_ESTA sin tener aún el objeto VA en RVTN_HIES
  5173. // (cuando se crea automáticamente pero aún no se han asignado operarios)
  5174. $visitStates = [
  5175. 'PE' => 'Pendiente',
  5176. 'CA' => 'Cancelado',
  5177. 'RE' => 'Rechazado',
  5178. 'EL' => 'Eliminado',
  5179. 'VA' => 'Validado',
  5180. 'EP' => 'En Proceso',
  5181. 'CP' => 'Cerrado Pendiente',
  5182. 'CE' => 'Cerrado'
  5183. ];
  5184. if ($visit->RVTN_ESTA !== 'VA') {
  5185. $statusKey = $visit->RVTN_ESTA;
  5186. $statusName = array_key_exists($statusKey, $visitStates) ? $visitStates[$statusKey] : $statusKey;
  5187. return $this->responseController->makeResponse(true, "La visita relacionada no está en el estado de validación (estado actual: $statusName)", [], 404);
  5188. }
  5189. $statusHistory = json_decode($visit->RVTN_HIES, true);
  5190. if (!is_array($statusHistory)) {
  5191. $statusHistory = [];
  5192. }
  5193. $validatedHistoryFilt = array_filter($statusHistory, function ($v, $k) {
  5194. return isset($v['ESTADO']) && $v['ESTADO'] == 'VA';
  5195. }, ARRAY_FILTER_USE_BOTH);
  5196. // Si no hay objeto VA en el historial pero el estado es VA, significa que aún no se han asignado operarios
  5197. if (empty($validatedHistoryFilt)) {
  5198. $statusKey = $visit->RVTN_ESTA;
  5199. $statusName = array_key_exists($statusKey, $visitStates) ? $visitStates[$statusKey] : $statusKey;
  5200. return $this->responseController->makeResponse(true, "La visita tiene estado $statusName pero aún no ha asignado operarios.", [], 404);
  5201. }
  5202. $validatedHistory = end($validatedHistoryFilt);
  5203. $attendance = array_key_exists('ATENCION', $validatedHistory) ? $validatedHistory['ATENCION'] : [];
  5204. $attendanceAux = [];
  5205. foreach ($attendance as $item) {
  5206. if ($item['RESPUESTA'] == 'A') {
  5207. $attendanceAux[] = $item;
  5208. }
  5209. }
  5210. return $this->responseController->makeResponse(false, 'EXITO', $attendanceAux);
  5211. }
  5212. // Visitas técnicas no programadas (Preventivas)
  5213. public function getVisitStaff($idVisit, $idUser, $line)
  5214. {
  5215. DB::enableQueryLog();
  5216. $idUser = $this->encryptionController->decrypt($idUser);
  5217. if (!$idUser) {
  5218. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  5219. }
  5220. $usr = DB::table('S002V01TUSUA')->where([
  5221. ['USUA_NULI', '=', $line],
  5222. ['USUA_IDUS', '=', $idUser],
  5223. ])->first();
  5224. if (is_null($usr)) {
  5225. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  5226. }
  5227. $idVisit = $this->encryptionController->decrypt($idVisit);
  5228. if (!$idVisit) {
  5229. return $this->responseController->makeResponse(true, 'El ID de la visita relacionada no está encriptado correctamente', [], 400);
  5230. }
  5231. $visit = DB::table('S002V01TRVTN')->where([
  5232. ['RVTN_NULI', '=', $line],
  5233. ['RVTN_IDVI', '=', $idVisit]
  5234. ])->first();
  5235. if (is_null($visit)) {
  5236. return $this->responseController->makeResponse(true, 'La visita relacionada no está registrada.', [], 404);
  5237. }
  5238. $staffArr = json_decode($visit->RVTN_PEIN, true);
  5239. $staffArrFn = [];
  5240. foreach ($staffArr as $val) {
  5241. if (array_key_exists('TYPE', $val)) {
  5242. if ($val['TYPE'] == 'EQ') {
  5243. $workTeamStaff = DB::table('S002V01TPERS')->join('S002V01TUSUA', 'USUA_IDUS', '=', 'PERS_IDUS')->where([
  5244. ['PERS_NULI', '=', $line],
  5245. ['PERS_EQTR', '=', $val['ID']]
  5246. ])->get()->all();
  5247. foreach ($workTeamStaff as $item) {
  5248. $itemID = $item->PERS_IDPE;
  5249. $itemFilt = array_filter($staffArrFn, function ($v, $k) use ($itemID) {
  5250. return $v['ID'] == $itemID;
  5251. }, ARRAY_FILTER_USE_BOTH);
  5252. if (count($itemFilt) <= 0) {
  5253. $staffArrFn[] = [
  5254. 'ID' => $itemID,
  5255. 'ID_USER' => $item->USUA_IDUS,
  5256. 'NAME' => $this->functionsController->joinName($item->USUA_NOMB, $item->USUA_APPA, $item->USUA_APMA),
  5257. 'TYPE' => $item->PERS_TICO,
  5258. ];
  5259. }
  5260. }
  5261. } else if ($val['TYPE'] == 'SU') {
  5262. $subcontratistStaff = DB::table('S002V01TPERS')->join('S002V01TUSUA', 'USUA_IDUS', '=', 'PERS_IDUS')->where([
  5263. ['PERS_NULI', '=', $line],
  5264. ['PERS_IDPS', '=', $val['ID']]
  5265. ])->get()->all();
  5266. foreach ($subcontratistStaff as $item) {
  5267. $itemID = $item->PERS_IDPE;
  5268. $itemFilt = array_filter($staffArrFn, function ($v, $k) use ($itemID) {
  5269. return $v['ID'] == $itemID;
  5270. }, ARRAY_FILTER_USE_BOTH);
  5271. if (count($itemFilt) <= 0) {
  5272. $staffArrFn[] = [
  5273. 'ID' => $itemID,
  5274. 'ID_USER' => $item->USUA_IDUS,
  5275. 'NAME' => $this->functionsController->joinName($item->USUA_NOMB, $item->USUA_APPA, $item->USUA_APMA),
  5276. 'TYPE' => $item->PERS_TICO,
  5277. ];
  5278. }
  5279. }
  5280. } else if ($val['TYPE'] == 'EM') {
  5281. $employee = DB::table('S002V01TPERS')->join('S002V01TUSUA', 'USUA_IDUS', '=', 'PERS_IDUS')->where([
  5282. ['PERS_NULI', '=', $line],
  5283. ['PERS_IDPE', '=', $val['ID']]
  5284. ])->first();
  5285. if (!is_null($employee)) {
  5286. $itemID = $employee->PERS_IDPE;
  5287. $itemFilt = array_filter($staffArrFn, function ($v, $k) use ($itemID) {
  5288. return $v['ID'] == $itemID;
  5289. }, ARRAY_FILTER_USE_BOTH);
  5290. if (count($itemFilt) <= 0) {
  5291. $staffArrFn[] = [
  5292. 'ID' => $itemID,
  5293. 'ID_USER' => $employee->USUA_IDUS,
  5294. 'NAME' => $this->functionsController->joinName($employee->USUA_NOMB, $employee->USUA_APPA, $employee->USUA_APMA),
  5295. 'TYPE' => $employee->PERS_TICO,
  5296. ];
  5297. }
  5298. }
  5299. }
  5300. }
  5301. }
  5302. return $this->responseController->makeResponse(false, 'EXITO', $staffArr);
  5303. }
  5304. // Visitas técnicas no programadas (Preventivas)
  5305. public function getVisitStatusHistory($idOrder, $idUser, $line)
  5306. {
  5307. DB::enableQueryLog();
  5308. $idUser = $this->encryptionController->decrypt($idUser);
  5309. if (!$idUser) {
  5310. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  5311. }
  5312. $usr = DB::table('S002V01TUSUA')->where([
  5313. ['USUA_NULI', '=', $line],
  5314. ['USUA_IDUS', '=', $idUser],
  5315. ])->first();
  5316. if (is_null($usr)) {
  5317. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  5318. }
  5319. $idOrder = $this->encryptionController->decrypt($idOrder);
  5320. if (!$idOrder) {
  5321. return $this->responseController->makeResponse(true, 'El ID de la visita solicitada no está encriptado correctamente', [], 400);
  5322. }
  5323. $visit = DB::table('S002V01TRVTN')->select([
  5324. 'RVTN_HIES AS HISTORIAL',
  5325. ])->where([
  5326. ['RVTN_IDVI', '=', $idOrder],
  5327. ['RVTN_NULI', '=', $line]
  5328. ])->first();
  5329. if (is_null($visit)) {
  5330. return $this->responseController->makeResponse(true, 'La visita solicitada no está registrada.', [], 404);
  5331. }
  5332. $visitStates = [
  5333. 'PE' => 'Pendiente',
  5334. 'CA' => 'Cancelado',
  5335. 'RE' => 'Rechazado',
  5336. 'EL' => 'Eliminado',
  5337. 'VA' => 'Validado',
  5338. 'EP' => 'En Proceso',
  5339. 'CP' => 'Cerrado Pendiente',
  5340. 'CE' => 'Cerrado'
  5341. ];
  5342. $statusHistoryArr = json_decode($visit->HISTORIAL, true);
  5343. foreach ($statusHistoryArr as $key => $item) {
  5344. $item['ESTADO'] = $visitStates[$item['ESTADO']];
  5345. $usrSta = DB::table('S002V01TUSUA')->where([
  5346. ['USUA_NULI', '=', $line],
  5347. ['USUA_IDUS', '=', $item['USUARIO']]
  5348. ])->first();
  5349. if (!is_null($usrSta)) {
  5350. $usrStaName = $this->functionsController->joinName($usrSta->USUA_NOMB, $usrSta->USUA_APPA, $usrSta->USUA_APMA);
  5351. $item['USUARIO'] = $usrStaName . " (" . $item['USUARIO'] . ")";
  5352. } else {
  5353. $item['USUARIO'] = "Usuario no encontrado (" . $item['USUARIO'] . ")";
  5354. }
  5355. $statusHistoryArr[$key] = $item;
  5356. }
  5357. $now = $this->functionsController->now();
  5358. $nowStr = $now->toDateTimeString();
  5359. $actions = DB::getQueryLog();
  5360. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  5361. $idac = $this->functionsController->registerActivity(
  5362. $line,
  5363. 'S002V01M10GMPR',
  5364. 'S002V01F11RVTP',
  5365. '-',
  5366. 'Consulta',
  5367. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el historial de estados de la visita #$idOrder de mantenimiento preventivo.",
  5368. $idUser,
  5369. $nowStr,
  5370. 'S002V01S02AOTR'
  5371. );
  5372. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  5373. return $this->responseController->makeResponse(false, 'EXITO.', $statusHistoryArr);
  5374. }
  5375. // Visitas técnicas no programadas (Preventivas)
  5376. public function attendPreventiveVisit(Request $request)
  5377. {
  5378. DB::enableQueryLog();
  5379. $validator = Validator::make($request->all(), [
  5380. 'id_user' => 'required|string',
  5381. 'linea' => 'required|integer',
  5382. 'id_order' => 'required|string',
  5383. 'attendance' => 'required|string|in:A,R',
  5384. 'data' => 'required|string'
  5385. ]);
  5386. if ($validator->fails()) {
  5387. return $this->responseController->makeResponse(
  5388. true,
  5389. "Se encontraron uno o más errores.",
  5390. $this->responseController->makeErrors(
  5391. $validator->errors()->messages()
  5392. ),
  5393. 401
  5394. );
  5395. }
  5396. $form = $request->all();
  5397. $idUser = $this->encryptionController->decrypt($form['id_user']);
  5398. if (!$idUser) {
  5399. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  5400. }
  5401. $usr = DB::table('S002V01TUSUA')->where([
  5402. ['USUA_NULI', '=', $form['linea']],
  5403. ['USUA_IDUS', '=', $idUser]
  5404. ])->first();
  5405. if (is_null($usr)) {
  5406. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  5407. }
  5408. $idVisit = $this->encryptionController->decrypt($form['id_order']);
  5409. if (!$idVisit) {
  5410. return $this->responseController->makeResponse(true, 'El ID de la visita no fue encriptado correctamente.', [], 400);
  5411. }
  5412. $visit = DB::table('S002V01TRVTN')->where([
  5413. ['RVTN_NULI', '=', $form['linea']],
  5414. ['RVTN_IDVI', '=', $idVisit]
  5415. ])->first();
  5416. if (is_null($visit)) {
  5417. return $this->responseController->makeResponse(true, 'La visita solicitada no existe.', [], 404);
  5418. } else if ($visit->RVTN_ESTA != 'VA') {
  5419. return $this->responseController->makeResponse(true, 'La visita no está validada para atender.', [], 401);
  5420. }
  5421. // Validar datos según tipo de respuesta
  5422. if ($form['attendance'] == 'A' && !str_contains($form['data'], 'data:image/png')) {
  5423. return $this->responseController->makeResponse(true, 'El archivo de la firma capturada es inválido.', [], 400);
  5424. } else if ($form['attendance'] == 'R' && strlen($form['data']) < 10) {
  5425. return $this->responseController->makeResponse(true, 'La cadena de comentarios tiene una longitud menor a 10 caracteres.', [], 400);
  5426. }
  5427. // Cupo global requerido (RVTN_PEIN)
  5428. $planilla = json_decode($visit->RVTN_PEIN, true);
  5429. $cupoRequerido = 0;
  5430. if (!empty($planilla) && is_array($planilla)) {
  5431. foreach ($planilla as $pe) {
  5432. $cupoRequerido += intval($pe['CANT'] ?? 0);
  5433. }
  5434. }
  5435. // Obtener historial de estados
  5436. $statusHistoryArr = json_decode($visit->RVTN_HIES, true);
  5437. // Buscar el último objeto con estado VA
  5438. $vaStatusIndex = null;
  5439. foreach ($statusHistoryArr as $index => $status) {
  5440. if ($status['ESTADO'] == 'VA') {
  5441. $vaStatusIndex = $index;
  5442. }
  5443. }
  5444. if ($vaStatusIndex === null) {
  5445. return $this->responseController->makeResponse(true, 'No se encontró un estado validado en el historial.', [], 404);
  5446. }
  5447. // Buscar el operario en el array ATENCION
  5448. $operatorIndex = null;
  5449. $atencionArr = $statusHistoryArr[$vaStatusIndex]['ATENCION'] ?? [];
  5450. foreach ($atencionArr as $index => $invitacion) {
  5451. if ($invitacion['ID'] == $idUser) {
  5452. $operatorIndex = $index;
  5453. break;
  5454. }
  5455. }
  5456. if ($operatorIndex === null) {
  5457. return $this->responseController->makeResponse(true, 'El usuario no está invitado a esta visita.', [], 401);
  5458. }
  5459. // Verificar si ya respondió
  5460. if (!empty($atencionArr[$operatorIndex]['RESPUESTA'])) {
  5461. $respuestaAnterior = $atencionArr[$operatorIndex]['RESPUESTA'] == 'A' ? 'aceptado' : 'rechazado';
  5462. return $this->responseController->makeResponse(true, "Ya ha $respuestaAnterior esta invitación.", [], 401);
  5463. }
  5464. // Bloquear aceptación si la planilla ya está completa (con base en aceptados actuales)
  5465. $aceptadosActual = 0;
  5466. foreach ($atencionArr as $itm) {
  5467. if (($itm['RESPUESTA'] ?? '') === 'A') $aceptadosActual++;
  5468. }
  5469. if ($form['attendance'] == 'A' && $cupoRequerido > 0 && $aceptadosActual >= $cupoRequerido) {
  5470. return $this->responseController->makeResponse(true, 'La planilla ya está completa. No es posible aceptar esta invitación.', [], 401);
  5471. }
  5472. // Iniciar transacción
  5473. DB::beginTransaction();
  5474. try {
  5475. $signature = null;
  5476. $comments = null;
  5477. if ($form['attendance'] == 'A') {
  5478. // Procesar firma
  5479. $filesPath = str_replace("app\\Http\\Controllers", "storage\\app\\files", __DIR__);
  5480. if (!file_exists($filesPath)) {
  5481. throw new \Exception('No se encontró la ubicación de almacenamiento de archivos.');
  5482. }
  5483. generateSignatureFileName:
  5484. $signatureFileName = $this->generateSignatureImageName();
  5485. $signatureFileDir = $filesPath . "\\" . $signatureFileName;
  5486. $signatureFileExists = file_exists($signatureFileDir);
  5487. if ($signatureFileExists) goto generateSignatureFileName;
  5488. $signatureFileData = str_replace("data:image/png;base64,", "", $form['data']);
  5489. file_put_contents($signatureFileDir, base64_decode($signatureFileData));
  5490. $now = $this->functionsController->now();
  5491. $year = $now->year;
  5492. $month = $now->month < 10 ? "0{$now->month}" : "{$now->month}";
  5493. $day = $now->day < 10 ? "0{$now->day}" : "{$now->day}";
  5494. $fecr = substr("$year", -2) . $month . $day;
  5495. $sec = DB::table('S002V01TAFAL')->where([
  5496. ['AFAL_COMO', '=', "GMPR"],
  5497. ['AFAL_CLDO', '=', "FO"],
  5498. ['AFAL_NULI', '=', $form['linea']],
  5499. ])->orderBy('AFAL_NUSE', 'desc')->first();
  5500. $nuse = 1;
  5501. if (!is_null($sec)) {
  5502. $nuse = intval($sec->AFAL_NUSE) + 1;
  5503. }
  5504. $noar = "firma_conformidad_" . $idUser;
  5505. $exte = "png";
  5506. $ver = DB::table('S002V01TAFAL')->where([
  5507. ['AFAL_NULI', '=', $form['linea']],
  5508. ['AFAL_COMO', '=', "GMPR"],
  5509. ['AFAL_CLDO', '=', "FO"],
  5510. ['AFAL_NOAR', '=', $noar],
  5511. ['AFAL_EXTE', '=', $exte],
  5512. ])->orderBy('AFAL_NUVE', 'desc')->first();
  5513. $nuve = 1;
  5514. if (!is_null($ver)) {
  5515. $nuve = intval($ver->AFAL_NUVE) + 1;
  5516. }
  5517. $tama = filesize($signatureFileDir);
  5518. $code = intval($form['linea']) < 10 ? "0{$form['linea']}" : "{$form['linea']}";
  5519. $code .= "-GMPR-FO-$fecr-";
  5520. for ($i = strlen((string)$nuse); $i < 6; $i++) {
  5521. $code .= "0";
  5522. }
  5523. $code .= "$nuse=";
  5524. $code .= $nuve < 10 ? "0$nuve=" : "$nuve=";
  5525. $code .= "$noar.$exte";
  5526. $usac = json_encode([$idUser]);
  5527. $nowStr = $now->toDateTimeString();
  5528. DB::table('S002V01TAFAL')->insert([
  5529. 'AFAL_NULI' => $form['linea'],
  5530. 'AFAL_COMO' => "GMPR",
  5531. 'AFAL_CLDO' => "FO",
  5532. 'AFAL_FECR' => $fecr,
  5533. 'AFAL_NUSE' => $nuse,
  5534. 'AFAL_NUVE' => $nuve,
  5535. 'AFAL_NOAR' => $noar,
  5536. 'AFAL_EXTE' => $exte,
  5537. 'AFAL_TAMA' => $tama,
  5538. 'AFAL_UBIC' => $signatureFileDir,
  5539. 'AFAL_USAC' => $usac,
  5540. 'AFAL_USRE' => $idUser,
  5541. 'AFAL_FERE' => $nowStr
  5542. ]);
  5543. $signature = $code;
  5544. } else if ($form['attendance'] == 'R') {
  5545. $comments = $form['data'];
  5546. }
  5547. // Última verificación de cupo (concurrencia) antes de confirmar respuesta
  5548. $statusHistoryCheck = json_decode(DB::table('S002V01TRVTN')->where([
  5549. ['RVTN_NULI', '=', $form['linea']],
  5550. ['RVTN_IDVI', '=', $idVisit]
  5551. ])->value('RVTN_HIES'), true);
  5552. $vaObjChk = null;
  5553. if (!empty($statusHistoryCheck)) {
  5554. for ($i = count($statusHistoryCheck) - 1; $i >= 0; $i--) {
  5555. if ($statusHistoryCheck[$i]['ESTADO'] == 'VA') {
  5556. $vaObjChk = $statusHistoryCheck[$i];
  5557. break;
  5558. }
  5559. }
  5560. }
  5561. $aceptadosChk = 0;
  5562. $atencionChk = $vaObjChk && array_key_exists('ATENCION', $vaObjChk) ? $vaObjChk['ATENCION'] : [];
  5563. foreach ($atencionChk as $itm) {
  5564. if (($itm['RESPUESTA'] ?? '') === 'A') $aceptadosChk++;
  5565. }
  5566. if ($form['attendance'] == 'A' && $cupoRequerido > 0 && $aceptadosChk >= $cupoRequerido) {
  5567. DB::rollBack();
  5568. return $this->responseController->makeResponse(true, 'La planilla ya está completa. No es posible aceptar esta invitación.', [], 401);
  5569. }
  5570. // Actualizar el objeto en ATENCION
  5571. $statusHistoryArr[$vaStatusIndex]['ATENCION'][$operatorIndex]['RESPUESTA'] = $form['attendance'];
  5572. $statusHistoryArr[$vaStatusIndex]['ATENCION'][$operatorIndex]['FIRMA'] = $signature;
  5573. $statusHistoryArr[$vaStatusIndex]['ATENCION'][$operatorIndex]['COMENTARIOS'] = $comments;
  5574. $statusHistoryStr = json_encode($statusHistoryArr);
  5575. $now = $this->functionsController->now();
  5576. $nowStr = $now->toDateTimeString();
  5577. // Actualizar la visita
  5578. DB::table('S002V01TRVTN')->where([
  5579. ['RVTN_NULI', '=', $form['linea']],
  5580. ['RVTN_IDVI', '=', $idVisit]
  5581. ])->update([
  5582. "RVTN_HIES" => $statusHistoryStr,
  5583. "RVTN_USMO" => $idUser,
  5584. "RVTN_FEMO" => $nowStr,
  5585. ]);
  5586. $userName = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  5587. $action = $form['attendance'] == 'A' ? 'aceptó' : 'rechazó';
  5588. // Preparar notificaciones
  5589. $notificationActions = [];
  5590. if ($form['attendance'] == 'A') {
  5591. $notificationActions[] = [
  5592. 'BOTON' => 'Revisar firma',
  5593. 'FUNCION' => 'reviewSignatureInPreventiveVisit',
  5594. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit), $this->encryptionController->encrypt($idUser)])
  5595. ];
  5596. } else if ($form['attendance'] == 'R') {
  5597. $notificationActions[] = [
  5598. 'BOTON' => 'Revisar comentarios',
  5599. 'FUNCION' => 'reviewCommentsInPreventiveVisit',
  5600. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit), $this->encryptionController->encrypt($idUser)])
  5601. ];
  5602. $notificationActions[] = [
  5603. 'BOTON' => 'Reasignar visita',
  5604. 'FUNCION' => 'revalidatePreventiveVisit',
  5605. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt($idVisit)])
  5606. ];
  5607. }
  5608. $notificationActions[] = [
  5609. 'BOTON' => 'Ir al módulo',
  5610. 'FUNCION' => 'openModule',
  5611. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMPR/AOTR/RVTP')])
  5612. ];
  5613. // Notificar al regulador (usuario 0000000001)
  5614. $this->notificationsController->emitNotification(
  5615. 'S002V01M10GMPR',
  5616. "Visita de mantenimiento preventivo #$idVisit",
  5617. "El usuario $userName ($idUser) $action atender la visita de mantenimiento preventivo #$idVisit.",
  5618. $notificationActions,
  5619. ['0000000001'],
  5620. $idUser,
  5621. $form['linea'],
  5622. $this->getSocketClient(),
  5623. $idVisit,
  5624. 'Preventivo'
  5625. );
  5626. $actions = DB::getQueryLog();
  5627. $idac = $this->functionsController->registerActivity(
  5628. $form['linea'],
  5629. 'S002V01M10GMPR',
  5630. 'S002V01F11RVTP',
  5631. 'S002V01P01REVI',
  5632. 'Actualización',
  5633. "El usuario $userName (" . $usr->USUA_IDUS . ") $action atender la visita preventiva #$idVisit.",
  5634. $idUser,
  5635. $nowStr,
  5636. 'S002V01S02AOTR'
  5637. );
  5638. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  5639. // Confirmar transacción
  5640. DB::commit();
  5641. return $this->responseController->makeResponse(false, 'EXITO');
  5642. } catch (\Exception $e) {
  5643. // Revertir transacción en caso de error
  5644. DB::rollBack();
  5645. $actions = DB::getQueryLog();
  5646. $now = $this->functionsController->now();
  5647. $nowStr = $now->toDateTimeString();
  5648. $userName = $this->functionsController->joinName($usr->USUA_NOMB ?? '', $usr->USUA_APPA ?? '', $usr->USUA_APMA ?? '');
  5649. $idac = $this->functionsController->registerActivity(
  5650. $form['linea'],
  5651. 'S002V01M10GMPR',
  5652. 'S002V01F11RVTP',
  5653. 'S002V01P01REVI',
  5654. 'Error',
  5655. "Error al procesar la respuesta de la visita preventiva #$idVisit: " . $e->getMessage(),
  5656. $idUser,
  5657. $nowStr,
  5658. 'S002V01S02AOTR'
  5659. );
  5660. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  5661. return $this->responseController->makeResponse(
  5662. true,
  5663. 'Ocurrió un error al procesar la respuesta de la visita.',
  5664. ['error' => $e->getMessage()],
  5665. 500
  5666. );
  5667. }
  5668. }
  5669. private function generateSignatureImageName()
  5670. {
  5671. $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"];
  5672. $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"];
  5673. $numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
  5674. $fileName = "";
  5675. for ($i = 0; $i < 40; $i++) {
  5676. $charTypeSelector = rand(0, 2);
  5677. $selectedChar = "";
  5678. switch ($charTypeSelector) {
  5679. case 0:
  5680. $charSelector = rand(0, count($upperLetters) - 1);
  5681. $selectedChar = $upperLetters[$charSelector];
  5682. break;
  5683. case 1:
  5684. $charSelector = rand(0, count($lowerLetters) - 1);
  5685. $selectedChar = $lowerLetters[$charSelector];
  5686. break;
  5687. case 2:
  5688. $charSelector = rand(0, count($numbers) - 1);
  5689. $selectedChar = $numbers[$charSelector];
  5690. break;
  5691. }
  5692. $selectedChar = empty($selectedChar) ? 'A' : $selectedChar;
  5693. $fileName .= $selectedChar;
  5694. }
  5695. return $fileName . '.png';
  5696. }
  5697. }