StockController.php 270 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068
  1. <?php
  2. /*
  3. Desarrollador: Ing. Jean Jairo Benitez Meza
  4. Ultima Modificación: 29/02/2023
  5. Módulo: Gestión de Inventario y/o Stock
  6. */
  7. namespace App\Http\Controllers;
  8. use App\Http\Controllers\Controller;
  9. use App\Http\Controllers\ResourcesController;
  10. use App\Http\Controllers\ResponseController;
  11. use App\Http\Controllers\EncryptionController;
  12. use App\Http\Controllers\DocumentManagementController;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Validator;
  16. use App\Http\Controllers\FunctionsController;
  17. use Illuminate\Support\Facades\Storage;
  18. use Illuminate\Http\File;
  19. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  20. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  21. use Dompdf\Dompdf;
  22. class StockController extends Controller
  23. {
  24. private $responseController;
  25. private $encController;
  26. private $resourcesController;
  27. private $documentManagementController;
  28. private $functionsController;
  29. private $processManagementController;
  30. public function __construct(){
  31. $this->responseController = new ResponseController();
  32. $this->encController = new EncryptionController();
  33. $this->resourcesController = new ResourcesController();
  34. $this->documentManagementController = new DocumentManagementController();
  35. $this->functionsController = new FunctionsController();
  36. $this->processManagementController = new ProcessManagementController();
  37. }
  38. // Crear Artículo en Stock
  39. public function createArtitleWithoutOrder(Request $request) {
  40. $validator = Validator::make($request->all(), [
  41. 'ARTICULO' => 'required',
  42. 'MODELO' => 'required',
  43. 'CODIGO_MODELO' => 'required',
  44. 'FAMILIA' => 'required',
  45. 'SUBFAMILIA' => 'required',
  46. 'UNIDAD' => 'required',
  47. 'CODIGO_BARRAS' => 'required',
  48. 'CANTIDAD' => 'required',
  49. 'STOCK_MINIMO' => 'required',
  50. 'STOCK_MAXIMO' => 'required',
  51. 'REPARACION' => 'required|boolean',
  52. 'CONSUMIBLE' => 'required|boolean',
  53. 'PELIGROSO' => 'required|boolean',
  54. // 'FECHA_VENCIMIENTO' => '',
  55. // 'PROVEEDOR' => 'required',
  56. 'IMAGEN' => 'required',
  57. 'NUMERO_LINEA' => 'required',
  58. 'USUARIO' => 'required',
  59. ]);
  60. if ($validator->fails()) {
  61. return $this->responseController->makeResponse(
  62. true,
  63. "ERR_STOCK_REG000: Se encontraron uno o más errores.",
  64. $this->responseController->makeErrors($validator->errors()->messages()),
  65. 401
  66. );
  67. }
  68. DB::beginTransaction();
  69. $requestData = $request->all();
  70. try {
  71. $user = $this->encController->decrypt($requestData['USUARIO']);
  72. } catch (\Throwable $th) {
  73. DB::rollBack();
  74. return $this->responseController->makeResponse(true, "ERR_STOCK_REG001: Ocurrió un error al obtener el usuario.", $th->getMessage(), 500);
  75. }
  76. try {
  77. $validateFamily = DB::table('S002V01TFAMI')
  78. ->where('FAMI_COFA','=', $requestData['FAMILIA'])
  79. ->where('FAMI_NULI','=', $requestData['NUMERO_LINEA'])
  80. ->where('FAMI_ESTA','=','Activo')
  81. ->exists();
  82. } catch (\Throwable $th) {
  83. DB::rollBack();
  84. return $this->responseController->makeResponse(true, "ERR_STOCK_REG002: Ocurrió un error al validar la familia.", $th->getMessage(), 500);
  85. }
  86. if (!$validateFamily) {
  87. DB::rollBack();
  88. return $this->responseController->makeResponse(true, "ERR_STOCK_REG003: La familia no existe.", [], 500);
  89. }
  90. try {
  91. $validateSubfamily = DB::table('S002V01TSUBF')
  92. ->where('SUBF_COSU','=', $requestData['SUBFAMILIA'])
  93. ->where('SUBF_NULI','=', $requestData['NUMERO_LINEA'])
  94. ->where('SUBF_ESTA','=','Activo')
  95. ->exists();
  96. } catch (\Throwable $th) {
  97. DB::rollBack();
  98. return $this->responseController->makeResponse(true, "ERR_STOCK_REG004: Ocurrió un error al validar la subfamilia.", $th->getMessage(), 500);
  99. }
  100. if (!$validateSubfamily) {
  101. DB::rollBack();
  102. return $this->responseController->makeResponse(true, "ERR_STOCK_REG005: La subfamilia no existe.", [], 500);
  103. }
  104. try {
  105. $validateUnit = DB::table('S002V01TUNID')
  106. ->where('UNID_IDUN','=', $requestData['UNIDAD'])
  107. ->where('UNID_NULI','=', $requestData['NUMERO_LINEA'])
  108. ->where('UNID_ESTA','=','Activo')
  109. ->exists();
  110. } catch (\Throwable $th) {
  111. DB::rollBack();
  112. return $this->responseController->makeResponse(true, "ERR_STOCK_REG006: Ocurrió un error al validar la unidad.", $th->getMessage(), 500);
  113. }
  114. if (!$validateUnit) {
  115. DB::rollBack();
  116. return $this->responseController->makeResponse(true, "ERR_STOCK_REG007: La unidad no existe.", [], 500);
  117. }
  118. $requestData['PROVEEDOR'] = $requestData['PROVEEDOR'] === '' ? null : $requestData['PROVEEDOR'];
  119. if (!is_null($requestData['PROVEEDOR'])) {
  120. try {
  121. $validateUnit = DB::table('S002V01TPROV')
  122. ->where('PROV_NUPR','=', $requestData['PROVEEDOR'])
  123. ->where('PROV_NULI','=', $requestData['NUMERO_LINEA'])
  124. ->where('PROV_ESTA','=','Activo')
  125. ->exists();
  126. } catch (\Throwable $th) {
  127. DB::rollBack();
  128. return $this->responseController->makeResponse(true, "ERR_STOCK_REG008: Ocurrió un error al validar el proveedor.", $th->getMessage(), 500);
  129. }
  130. if (!$validateUnit) {
  131. DB::rollBack();
  132. return $this->responseController->makeResponse(true, "ERR_STOCK_REG009: El proveedor no existe.", [], 500);
  133. }
  134. }
  135. $arrCodeImages = array();
  136. foreach ($requestData['IMAGEN'] as $key => $encIdFile) {
  137. $idFile = $this->encController->decrypt($encIdFile);
  138. $tempFile = DB::table('S002V01TARTE')->where([
  139. ['ARTE_NULI', '=', $requestData['NUMERO_LINEA']],
  140. ['ARTE_IDAR', '=', $idFile],
  141. ])->first();
  142. if(is_null($tempFile)){
  143. return $this->responseController->makeResponse(true, 'ERR_ARTITLE_REG006: El archivo consultado no está registrado', [], 404);
  144. }else if($tempFile->ARTE_ESTA == 'Eliminado'){
  145. return $this->responseController->makeResponse(true, 'ERR_ARTITLE_REG007: El archivo consultado está eliminado', [], 404);
  146. }
  147. $fileResponse = $this->documentManagementController->moveFinalFile(
  148. intval($requestData['NUMERO_LINEA']),
  149. 'GIST',
  150. 'FO',
  151. $tempFile,
  152. $user,
  153. );
  154. if(!$fileResponse[0]){
  155. return $this->responseController->makeResponse(true, 'ERR_ARTITLE_REG008: '.$fileResponse[1], [], 400);
  156. }
  157. $arrCodeImages[] = $this->encController->encrypt($fileResponse[1]);
  158. }
  159. $jsonImages = json_encode($arrCodeImages);
  160. $now = $this->functionsController->now();
  161. $currentDate = $now->toDateTimeString();
  162. try {
  163. $validateRegister = DB::table('S002V01TSTAR')->insert([
  164. // 'STAR_CODI' => $requestData['CODIGO_STOCK'],
  165. 'STAR_ARTI' => $requestData['ARTICULO'],
  166. 'STAR_MODE' => $requestData['MODELO'],
  167. 'STAR_COMO' => $requestData['CODIGO_MODELO'],
  168. 'STAR_IDFA' => $requestData['FAMILIA'],
  169. 'STAR_IDSU' => $requestData['SUBFAMILIA'],
  170. 'STAR_NUPR' => $requestData['PROVEEDOR'],
  171. 'STAR_IDUN' => $requestData['UNIDAD'],
  172. 'STAR_COBA' => $requestData['CODIGO_BARRAS'],
  173. 'STAR_CANT' => $requestData['CANTIDAD'],
  174. 'STAR_STMI' => $requestData['STOCK_MINIMO'],
  175. 'STAR_STMA' => $requestData['STOCK_MAXIMO'],
  176. 'STAR_REPA' => $requestData['REPARACION'],
  177. 'STAR_CONS' => $requestData['CONSUMIBLE'],
  178. 'STAR_PELI' => $requestData['PELIGROSO'],
  179. 'STAR_FEVE' => $requestData['FECHA_VENCIMIENTO'],
  180. 'STAR_IMAG' => $jsonImages,
  181. 'STAR_TIAD' => 'Sin Pedido',
  182. 'STAR_NULI' => $requestData['NUMERO_LINEA'],
  183. 'STAR_USRE' => $user,
  184. 'STAR_FERE' => $currentDate,
  185. 'STAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  186. ]);
  187. } catch (\Throwable $th) {
  188. DB::rollBack();
  189. return $this->responseController->makeResponse(true, "ERR_STOCK_REG011: Ocurrió un error al registrar el artículo.", $th->getMessage(), 500);
  190. }
  191. if (!$validateRegister) {
  192. DB::rollBack();
  193. return $this->responseController->makeResponse(true, "ERR_STOCK_REG012: No se pudo registrar el artículo.", [], 500);
  194. }
  195. DB::commit();
  196. return $this->responseController->makeResponse(false, "EXITO: Registro Exitoso");
  197. }
  198. // FUNCIÓN GESTIÓN DE ALMACENES
  199. public function getWarehouse( $user, $line ) {
  200. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  201. if ($arrResponseCheckUser['error']) {
  202. DB::rollBack();
  203. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_GETBY000:'.$arrResponseCheckUser['msg'], [], 401);
  204. }
  205. try {
  206. $arrWarehouse = DB::table('S002V01TALMA')
  207. ->where('ALMA_NULI', '=', $line)
  208. ->get([
  209. 'ALMA_COAL AS CODIGO_ALMACEN',
  210. 'ALMA_NOAL AS NOMBRE_ALMACEN',
  211. 'ALMA_NORE AS NOMBRE_RESPONSABLE',
  212. 'ALMA_APRE AS APELLIDO_PATERNO_RESPONSABLE',
  213. 'ALMA_AMRE AS APELLIDO_MATERNO_RESPONSABLE',
  214. 'ALMA_COR1 AS CORREO1',
  215. 'ALMA_COR2 AS CORREO2',
  216. 'ALMA_LAD1 AS LADA1',
  217. 'ALMA_TEL1 AS TELEFONO1',
  218. 'ALMA_LAD2 AS LADA2',
  219. 'ALMA_TEL2 AS TELEFONO2',
  220. 'ALMA_CALL AS CALLE',
  221. 'ALMA_NUEX AS NUMERO_EXTERIOR',
  222. 'ALMA_NUIN AS NUMERO_INTERIOR',
  223. 'ALMA_COPO AS CODIGO_POSTAL',
  224. 'ALMA_COLO AS COLONIA',
  225. 'ALMA_LOCA AS LOCALIDAD',
  226. 'ALMA_MUNI AS MUNICIPIO',
  227. 'ALMA_ENTI AS ENTIDAD_FEDERATIVA',
  228. 'ALMA_PAIS AS PAIS',
  229. 'ALMA_ESTA AS ESTADO',
  230. 'ALMA_USRE AS USUARIO_REGISTRA',
  231. 'ALMA_FERE AS FECHA_REGISTRA',
  232. 'ALMA_USMO AS USUARIO_MODIFICA',
  233. 'ALMA_FEMO AS FECHA_MODIFICA',
  234. ]);
  235. $arrWarehouse = json_decode( json_encode($arrWarehouse), true );
  236. } catch (\Throwable $th) {
  237. DB::rollBack();
  238. return $this->responseController->makeResponse(
  239. true,
  240. "ERR_WAREHOUSE_GET001: Ocurrió un error al obtener los almacenes.",
  241. $th->getMessage(),
  242. 500
  243. );
  244. }
  245. foreach ($arrWarehouse as $keyWarehouse => $warehouse) {
  246. $arrResponseGetAddress = $this->resourcesController->getAddress(
  247. $warehouse['CODIGO_POSTAL'],
  248. $warehouse['COLONIA'],
  249. $warehouse['MUNICIPIO'],
  250. $warehouse['LOCALIDAD'],
  251. $warehouse['ENTIDAD_FEDERATIVA'],
  252. $warehouse['PAIS'],
  253. $line
  254. );
  255. if ($arrResponseGetAddress['error']) {
  256. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_GET002: '.$arrResponseGetAddress['msg'], $arrResponseGetAddress['response'], 401);
  257. }
  258. $warehouse['CODIGO_POSTAL'] = $arrResponseGetAddress['response']['CODIGO_POSTAL'];
  259. $warehouse['COLONIA'] = $arrResponseGetAddress['response']['COLONIA'];
  260. $warehouse['MUNICIPIO'] = $arrResponseGetAddress['response']['MUNICIPIO'];
  261. $warehouse['LOCALIDAD'] = $arrResponseGetAddress['response']['LOCALIDAD'];
  262. $warehouse['ENTIDAD_FEDERATIVA'] = $arrResponseGetAddress['response']['ENTIDAD_FEDERATIVA'];
  263. $warehouse['PAIS'] = $arrResponseGetAddress['response']['PAIS'];
  264. try {
  265. $countStock = DB::table('S002V01TUBAR')
  266. ->where('UBAR_NULI', '=', $line)
  267. ->where('UBAR_ESTA', '=', 'Activo')
  268. ->where('UBAR_COAL', '=', $warehouse['CODIGO_ALMACEN'])
  269. ->count();
  270. } catch (\Throwable $th) {
  271. DB::rollBack();
  272. return $this->responseController->makeResponse(
  273. true,
  274. "ERR_WAREHOUSE_GET003: Ocurrió un error al obtener los almacenes.",
  275. $th->getMessage(),
  276. 500
  277. );
  278. }
  279. $warehouse['CANTIDAD_STOCK'] = $countStock;
  280. $arrWarehouse[$keyWarehouse] = $warehouse;
  281. }
  282. $responseCheckLatestUpdate = $this->resourcesController->checkLatestUpdate($arrWarehouse, $line);
  283. if ($responseCheckLatestUpdate['error']) {
  284. return $this->responseController->makeResponse(true, $responseCheckLatestUpdate['msg'], [], 500);
  285. }
  286. $arrWarehouse = $responseCheckLatestUpdate['response'];
  287. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrWarehouse);
  288. }
  289. public function getWarehouseById( $idWarehouse, $user, $line ) {
  290. try {
  291. $idWarehouse = $this->encController->decrypt($idWarehouse);
  292. } catch (\Throwable $th) {
  293. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_GETBY000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  294. }
  295. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  296. if ($arrResponseCheckUser['error']) {
  297. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_GETBY000:'.$arrResponseCheckUser['msg'], [], 401);
  298. }
  299. try {
  300. $arrWarehouse = (array) DB::table('S002V01TALMA')
  301. ->where('ALMA_COAL', '=', $idWarehouse)
  302. ->where('ALMA_NULI', '=', $line)
  303. ->first([
  304. 'ALMA_COAL AS CODIGO_ALMACEN',
  305. 'ALMA_NOAL AS NOMBRE_ALMACEN',
  306. 'ALMA_NORE AS NOMBRE_RESPONSABLE',
  307. 'ALMA_APRE AS APELLIDO_PATERNO_RESPONSABLE',
  308. 'ALMA_AMRE AS APELLIDO_MATERNO_RESPONSABLE',
  309. 'ALMA_COR1 AS CORREO1',
  310. 'ALMA_COR2 AS CORREO2',
  311. 'ALMA_LAD1 AS LADA1',
  312. 'ALMA_TEL1 AS TELEFONO1',
  313. 'ALMA_LAD2 AS LADA2',
  314. 'ALMA_TEL2 AS TELEFONO2',
  315. 'ALMA_CALL AS CALLE',
  316. 'ALMA_NUEX AS NUMERO_EXTERIOR',
  317. 'ALMA_NUIN AS NUMERO_INTERIOR',
  318. 'ALMA_COPO AS CODIGO_POSTAL',
  319. 'ALMA_COLO AS COLONIA',
  320. 'ALMA_LOCA AS LOCALIDAD',
  321. 'ALMA_MUNI AS MUNICIPIO',
  322. 'ALMA_ENTI AS ENTIDAD_FEDERATIVA',
  323. 'ALMA_PAIS AS PAIS',
  324. 'ALMA_ESTA AS ESTADO',
  325. 'ALMA_USRE AS USUARIO_REGISTRA',
  326. 'ALMA_FERE AS FECHA_REGISTRA',
  327. 'ALMA_USMO AS USUARIO_MODIFICA',
  328. 'ALMA_FEMO AS FECHA_MODIFICA',
  329. ]);
  330. $arrWarehouse = json_decode( json_encode($arrWarehouse), true );
  331. } catch (\Throwable $th) {
  332. DB::rollBack();
  333. return $this->responseController->makeResponse(
  334. true,
  335. "ERR_WAREHOUSE_GETBY001: Ocurrió un error al obtener los almacenes.",
  336. $th->getMessage(),
  337. 500
  338. );
  339. }
  340. if (!empty($arrWarehouse)) {
  341. $arrResponseGetAddress = $this->resourcesController->getAddress(
  342. $arrWarehouse['CODIGO_POSTAL'],
  343. $arrWarehouse['COLONIA'],
  344. $arrWarehouse['MUNICIPIO'],
  345. $arrWarehouse['LOCALIDAD'],
  346. $arrWarehouse['ENTIDAD_FEDERATIVA'],
  347. $arrWarehouse['PAIS'],
  348. $line
  349. );
  350. if ($arrResponseGetAddress['error']) {
  351. return $this->responseController->makeResponse(true, 'ERR_arrWarehouse_GETBY002: '.$arrResponseGetAddress['msg'], $arrResponseGetAddress['response'], 406);
  352. }
  353. $arrWarehouse['CODIGO_POSTAL'] = $arrResponseGetAddress['response']['CODIGO_POSTAL'];
  354. $arrWarehouse['COLONIA'] = $arrResponseGetAddress['response']['COLONIA'];
  355. $arrWarehouse['MUNICIPIO'] = $arrResponseGetAddress['response']['MUNICIPIO'];
  356. $arrWarehouse['LOCALIDAD'] = $arrResponseGetAddress['response']['LOCALIDAD'];
  357. $arrWarehouse['ENTIDAD_FEDERATIVA'] = $arrResponseGetAddress['response']['ENTIDAD_FEDERATIVA'];
  358. $arrWarehouse['PAIS'] = $arrResponseGetAddress['response']['PAIS'];
  359. }
  360. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrWarehouse);
  361. }
  362. public function getWarehouseActives($user, $line) {
  363. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  364. if ($arrResponseCheckUser['error']) {
  365. DB::rollBack();
  366. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_GETBY000:'.$arrResponseCheckUser['msg'], [], 401);
  367. }
  368. try {
  369. $arrWarehouse = DB::table('S002V01TALMA')
  370. ->where('ALMA_NULI', '=', $line)
  371. ->where('ALMA_ESTA', '=', 'Activo')
  372. ->get([
  373. 'ALMA_COAL AS CODIGO_ALMACEN',
  374. 'ALMA_NOAL AS NOMBRE_ALMACEN',
  375. 'ALMA_NORE AS NOMBRE_RESPONSABLE',
  376. 'ALMA_APRE AS APELLIDO_PATERNO_RESPONSABLE',
  377. 'ALMA_AMRE AS APELLIDO_MATERNO_RESPONSABLE',
  378. 'ALMA_COR1 AS CORREO1',
  379. 'ALMA_LAD1 AS LADA1',
  380. 'ALMA_TEL1 AS TELEFONO1',
  381. ]);
  382. $arrWarehouse = json_decode( json_encode($arrWarehouse), true );
  383. } catch (\Throwable $th) {
  384. DB::rollBack();
  385. return $this->responseController->makeResponse(
  386. true,
  387. "ERR_WAREHOUSE_GET001: Ocurrió un error al obtener los almacenes.",
  388. $th->getMessage(),
  389. 500
  390. );
  391. }
  392. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrWarehouse);
  393. }
  394. public function createWarehouse( Request $request ) {
  395. $validator = Validator::make($request->all(), [
  396. 'CODIGO_ALMACEN' => 'required|string',
  397. 'NOMBRE_ALMACEN' => 'required|string',
  398. 'NOMBRE_RESPONSABLE' => 'required|string',
  399. 'APELLIDO_PATERNO_RESPONSABLE' => 'required|string',
  400. 'APELLIDO_MATERNO_RESPONSABLE' => 'nullable|string',
  401. 'CORREO1' => 'required|string',
  402. 'CORREO2' => 'nullable|string',
  403. 'LADA1' => 'required|string',
  404. 'TELEFONO1' => 'required|string',
  405. 'LADA2' => 'nullable|string',
  406. 'TELEFONO2' => 'nullable|string',
  407. 'CALLE' => 'required|string',
  408. 'NUMERO_EXTERIOR' => 'required|string',
  409. 'NUMERO_INTERIOR' => 'nullable|string',
  410. 'CODIGO_POSTAL' => 'required|string',
  411. 'COLONIA' => 'required|string',
  412. 'LOCALIDAD' => 'nullable|string',
  413. 'MUNICIPIO' => 'required|string',
  414. 'ENTIDAD_FEDERATIVA' => 'required|string',
  415. 'PAIS' => 'required|string',
  416. 'USUARIO' => 'required|string',
  417. 'NUMERO_LINEA' => 'required|integer',
  418. ]);
  419. if ($validator->fails()) {
  420. return $this->responseController->makeResponse(
  421. true,
  422. "ERR_WAREHOUSE_REG000: Se encontraron uno o más errores.",
  423. $this->responseController->makeErrors($validator->errors()->messages()),
  424. 401
  425. );
  426. }
  427. DB::beginTransaction();
  428. $requestData = $request->all();
  429. // Se valida y se obtiene el usuario
  430. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  431. if ($arrResponseCheckUser['error']) {
  432. DB::rollBack();
  433. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_REG001:'.$arrResponseCheckUser['msg'], [], 401);
  434. }
  435. $user = $arrResponseCheckUser['response'];
  436. // Se vallidan los campos de dirección
  437. $arrResponseCheckAddress = $this->resourcesController->validateAddress(
  438. $requestData['CODIGO_POSTAL'],
  439. $requestData['COLONIA'],
  440. $requestData['MUNICIPIO'],
  441. $requestData['LOCALIDAD'],
  442. $requestData['ENTIDAD_FEDERATIVA'],
  443. $requestData['PAIS'],
  444. $requestData['NUMERO_LINEA'],
  445. );
  446. if ($arrResponseCheckAddress['error']) {
  447. DB::rollBack();
  448. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_REG002: '.$arrResponseCheckAddress['msg'], $arrResponseCheckAddress['response'], 401);
  449. }
  450. $now = $this->functionsController->now();
  451. $currentDate = $now->toDateTimeString();
  452. try {
  453. $validateInsert = DB::table('S002V01TALMA')->insert([
  454. 'ALMA_NULI' => $requestData['NUMERO_LINEA'],
  455. 'ALMA_COAL' => $requestData['CODIGO_ALMACEN'],
  456. 'ALMA_NOAL' => $requestData['NOMBRE_ALMACEN'],
  457. 'ALMA_NORE' => $requestData['NOMBRE_RESPONSABLE'],
  458. 'ALMA_APRE' => $requestData['APELLIDO_PATERNO_RESPONSABLE'],
  459. 'ALMA_AMRE' => $requestData['APELLIDO_MATERNO_RESPONSABLE'],
  460. 'ALMA_COR1' => $requestData['CORREO1'],
  461. 'ALMA_COR2' => $requestData['CORREO2'],
  462. 'ALMA_LAD1' => $requestData['LADA1'],
  463. 'ALMA_TEL1' => $requestData['TELEFONO1'],
  464. 'ALMA_LAD2' => $requestData['LADA2'],
  465. 'ALMA_TEL2' => $requestData['TELEFONO2'],
  466. 'ALMA_CALL' => $requestData['CALLE'],
  467. 'ALMA_NUEX' => $requestData['NUMERO_EXTERIOR'],
  468. 'ALMA_NUIN' => $requestData['NUMERO_INTERIOR'],
  469. 'ALMA_COPO' => $requestData['CODIGO_POSTAL'],
  470. 'ALMA_COLO' => $requestData['COLONIA'],
  471. 'ALMA_LOCA' => $requestData['LOCALIDAD'],
  472. 'ALMA_MUNI' => $requestData['MUNICIPIO'],
  473. 'ALMA_ENTI' => $requestData['ENTIDAD_FEDERATIVA'],
  474. 'ALMA_PAIS' => $requestData['PAIS'],
  475. 'ALMA_USRE' => $user,
  476. 'ALMA_FERE' => $currentDate,
  477. 'ALMA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  478. ]);
  479. } catch (\Throwable $th) {
  480. DB::rollBack();
  481. return $this->responseController->makeResponse(
  482. true,
  483. "ERR_WAREHOUSE_REG003: Ocurrió un error al insertar el registro del almacen.",
  484. $th->getMessage(),
  485. 500
  486. );
  487. }
  488. if (!$validateInsert) {
  489. DB::rollBack();
  490. return $this->responseController->makeResponse(
  491. true,
  492. "ERR_WAREHOUSE_REG004: No se pudo insertar el registro del almacen.",
  493. [],
  494. 500
  495. );
  496. }
  497. DB::commit();
  498. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  499. }
  500. public function updateWarehouse( Request $request, $idWarehouse ) {
  501. $validator = Validator::make($request->all(), [
  502. 'CODIGO_ALMACEN' => 'required|string',
  503. 'NOMBRE_ALMACEN' => 'required|string',
  504. 'NOMBRE_RESPONSABLE' => 'required|string',
  505. 'APELLIDO_PATERNO_RESPONSABLE' => 'required|string',
  506. 'APELLIDO_MATERNO_RESPONSABLE' => 'nullable|string',
  507. 'CORREO1' => 'required|string',
  508. 'CORREO2' => 'nullable|string',
  509. 'LADA1' => 'required|string',
  510. 'TELEFONO1' => 'required|string',
  511. 'LADA2' => 'nullable|string',
  512. 'TELEFONO2' => 'nullable|string',
  513. 'CALLE' => 'required|string',
  514. 'NUMERO_EXTERIOR' => 'required|string',
  515. 'NUMERO_INTERIOR' => 'nullable|string',
  516. 'CODIGO_POSTAL' => 'required|string',
  517. 'COLONIA' => 'required|string',
  518. 'LOCALIDAD' => 'nullable|string',
  519. 'MUNICIPIO' => 'required|string',
  520. 'ENTIDAD_FEDERATIVA' => 'required|string',
  521. 'PAIS' => 'required|string',
  522. 'USUARIO' => 'required|string',
  523. 'NUMERO_LINEA' => 'required|integer',
  524. ]);
  525. if ($validator->fails()) {
  526. return $this->responseController->makeResponse(
  527. true,
  528. "ERR_WAREHOUSE_UPD000: Se encontraron uno o más errores.",
  529. $this->responseController->makeErrors($validator->errors()->messages()),
  530. 401
  531. );
  532. }
  533. DB::beginTransaction();
  534. $requestData = $request->all();
  535. try {
  536. $idWarehouse = $this->encController->decrypt($idWarehouse);
  537. } catch (\Throwable $th) {
  538. return $this->responseController->makeResponse(true, "ERR_WAREHOUSE_UPD001: Ocurrió un error al desencriptar el ID del almacen.". $th->getMessage(), 406);
  539. }
  540. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  541. if ($arrResponseCheckUser['error']) {
  542. DB::rollBack();
  543. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_UPD002:'.$arrResponseCheckUser['msg'], [], 401);
  544. }
  545. $user = $arrResponseCheckUser['response'];
  546. $arrResponseCheckAddress = $this->resourcesController->validateAddress(
  547. $requestData['CODIGO_POSTAL'],
  548. $requestData['COLONIA'],
  549. $requestData['MUNICIPIO'],
  550. $requestData['LOCALIDAD'],
  551. $requestData['ENTIDAD_FEDERATIVA'],
  552. $requestData['PAIS'],
  553. $requestData['NUMERO_LINEA'],
  554. );
  555. if ($arrResponseCheckAddress['error']) {
  556. DB::rollBack();
  557. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_UPD003: '.$arrResponseCheckAddress['msg'], $arrResponseCheckAddress['response'], 401);
  558. }
  559. $now = $this->functionsController->now();
  560. $currentDate = $now->toDateTimeString();
  561. try {
  562. $validateUpdate = DB::table('S002V01TALMA')
  563. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  564. ->where('ALMA_COAL', '=', $idWarehouse)
  565. ->update([
  566. 'ALMA_COAL' => $requestData['CODIGO_ALMACEN'],
  567. 'ALMA_NOAL' => $requestData['NOMBRE_ALMACEN'],
  568. 'ALMA_NORE' => $requestData['NOMBRE_RESPONSABLE'],
  569. 'ALMA_APRE' => $requestData['APELLIDO_PATERNO_RESPONSABLE'],
  570. 'ALMA_AMRE' => $requestData['APELLIDO_MATERNO_RESPONSABLE'],
  571. 'ALMA_COR1' => $requestData['CORREO1'],
  572. 'ALMA_COR2' => $requestData['CORREO2'],
  573. 'ALMA_LAD1' => $requestData['LADA1'],
  574. 'ALMA_TEL1' => $requestData['TELEFONO1'],
  575. 'ALMA_LAD2' => $requestData['LADA2'],
  576. 'ALMA_TEL2' => $requestData['TELEFONO2'],
  577. 'ALMA_CALL' => $requestData['CALLE'],
  578. 'ALMA_NUEX' => $requestData['NUMERO_EXTERIOR'],
  579. 'ALMA_NUIN' => $requestData['NUMERO_INTERIOR'],
  580. 'ALMA_COPO' => $requestData['CODIGO_POSTAL'],
  581. 'ALMA_COLO' => $requestData['COLONIA'],
  582. 'ALMA_LOCA' => $requestData['LOCALIDAD'],
  583. 'ALMA_MUNI' => $requestData['MUNICIPIO'],
  584. 'ALMA_ENTI' => $requestData['ENTIDAD_FEDERATIVA'],
  585. 'ALMA_PAIS' => $requestData['PAIS'],
  586. 'ALMA_USMO' => $user,
  587. 'ALMA_FEMO' => $currentDate,
  588. 'ALMA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  589. ]);
  590. } catch (\Throwable $th) {
  591. DB::rollBack();
  592. return $this->responseController->makeResponse(
  593. true,
  594. "ERR_WAREHOUSE_UPD004: Ocurrió un error al insertar el registro del almacen.",
  595. $th->getMessage(),
  596. 500
  597. );
  598. }
  599. if (!$validateUpdate) {
  600. DB::rollBack();
  601. return $this->responseController->makeResponse(
  602. true,
  603. "ERR_WAREHOUSE_UPD005: No se pudo insertar el registro del almacen.",
  604. [],
  605. 500
  606. );
  607. }
  608. DB::commit();
  609. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  610. }
  611. public function deleteWarehouse( Request $request, $idWarehouse ) {
  612. $validator = Validator::make($request->all(), [
  613. 'USUARIO' => 'required|string',
  614. 'NUMERO_LINEA' => 'required|integer',
  615. ]);
  616. if ($validator->fails()) {
  617. return $this->responseController->makeResponse(
  618. true,
  619. "ERR_WAREHOUSE_DEL000: Se encontraron uno o más errores.",
  620. $this->responseController->makeErrors($validator->errors()->messages()),
  621. 401
  622. );
  623. }
  624. DB::beginTransaction();
  625. $requestData = $request->all();
  626. try {
  627. $idWarehouse = $this->encController->decrypt($idWarehouse);
  628. } catch (\Throwable $th) {
  629. DB::rollBack();
  630. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL001: Ocurrió un error al desencriptar el nivel.', $th->getMessage(), 500);
  631. }
  632. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  633. if ($arrResponseCheckUser['error']) {
  634. DB::rollBack();
  635. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL002:'.$arrResponseCheckUser['msg'], [], 401);
  636. }
  637. $user = $arrResponseCheckUser['response'];
  638. try {
  639. $validateExists = DB::table('S002V01TALMA')
  640. ->where('ALMA_COAL', '=', $idWarehouse)
  641. ->where('ALMA_ESTA', '=', 'Activo')
  642. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  643. ->exists();
  644. } catch (\Throwable $th) {
  645. DB::rollBack();
  646. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL003: Ocurrió un error al obtener la información del área.', $th->getMessage(), 500);
  647. }
  648. if (!$validateExists) {
  649. DB::rollBack();
  650. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL004: El almacen no existe.', [], 406);
  651. }
  652. try {
  653. $validateExists = DB::table('S002V01TUBAR')
  654. ->where('UBAR_COAL', '=', $idWarehouse)
  655. ->where('UBAR_NULI', '=', $requestData['NUMERO_LINEA'])
  656. ->where('UBAR_ESTA', '=', 'Activo')
  657. ->exists();
  658. } catch (\Throwable $th) {
  659. DB::rollBack();
  660. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL005: Ocurrió un error al obtener las ubicaciones de los artículos.', $th->getMessage(), 500);
  661. }
  662. if($validateExists) {
  663. DB::rollBack();
  664. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL006: No se puede eliminar debido a que existen artículos ubicados en el almacen.', [], 406);
  665. }
  666. try {
  667. $countArea = DB::table('S002V01TAREA')
  668. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  669. ->where('AREA_COAL', '=', $idWarehouse)
  670. ->where('AREA_ESTA', '=', 'Activo')
  671. ->count();
  672. } catch (\Throwable $th) {
  673. DB::rollBack();
  674. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL007: Ocurrió un error al obtener la información del área.', $th->getMessage(), 500);
  675. }
  676. if ($countArea > 0) {
  677. DB::rollBack();
  678. $quantity = $countArea === 1 ? 'existe 1 área activa' : 'existen'.$countArea.' áreas activas';
  679. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL008: No se puede elminar debido a que '.$quantity.'.', [], 406);
  680. }
  681. try {
  682. $countLevel = DB::table('S002V01TNIVE')
  683. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  684. ->where('NIVE_COAL', '=', $idWarehouse)
  685. ->where('NIVE_ESTA', '=', 'Activo')
  686. ->count();
  687. } catch (\Throwable $th) {
  688. DB::rollBack();
  689. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL009: Ocurrió un error al obtener las zonas.', $th->getMessage(), 500);
  690. }
  691. if ($countLevel > 0) {
  692. DB::rollBack();
  693. $quantity = $countLevel === 1 ? 'existe 1 nivel activo' : 'existen '.$countLevel.' niveles activos';
  694. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL010: No se puede eliminar debido a que '.$quantity.'.', [], 406);
  695. }
  696. try {
  697. $countZones = DB::table('S002V01TZONA')
  698. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  699. ->where('ZONA_COAL', '=', $idWarehouse)
  700. ->where('ZONA_ESTA', '=', 'Activo')
  701. ->count();
  702. } catch (\Throwable $th) {
  703. DB::rollBack();
  704. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL011: Ocurrió un error al obtener las zonas.', $th->getMessage(), 500);
  705. }
  706. if ($countZones > 0) {
  707. DB::rollBack();
  708. $quantity = $countZones === 1 ? 'existe 1 zona activa' : 'existen '.$countZones.' zonas activas';
  709. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL012: No se puede eliminar debido a que '.$quantity.'.', [], 406);
  710. }
  711. $now = $this->functionsController->now();
  712. $currentDate = $now->toDateTimeString();
  713. try {
  714. $validateUpdate = DB::table('S002V01TALMA')
  715. ->where('ALMA_COAL', '=', $idWarehouse)
  716. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  717. ->update([
  718. 'ALMA_ESTA' => 'Eliminado',
  719. 'ALMA_USMO' => $user,
  720. 'ALMA_FEMO' => $currentDate,
  721. 'ALMA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  722. ]);
  723. } catch (\Throwable $th) {
  724. DB::rollBack();
  725. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL013: Ocurrió un error al eliminar el área.', $th->getMessage(), 500);
  726. }
  727. if (!$validateUpdate) {
  728. DB::rollBack();
  729. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL014: No se pudo eliminar el área.', [], 406);
  730. }
  731. DB::commit();
  732. return $this->responseController->makeResponse(false, "ÉXITO: Eliminación Exitosa");
  733. }
  734. public function getAreaByWarehouse($idWarehouse, $user, $line) {
  735. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  736. if ($arrResponseCheckUser['error']) {
  737. DB::rollBack();
  738. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_GETBY000:'.$arrResponseCheckUser['msg'], [], 401);
  739. }
  740. try {
  741. $idWarehouse = $this->encController->decrypt($idWarehouse);
  742. } catch (\Throwable $th) {
  743. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBY000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  744. }
  745. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  746. if ($arrResponseCheckUser['error']) {
  747. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBY001:'.$arrResponseCheckUser['msg'], [], 401);
  748. }
  749. try {
  750. $getArea = DB::table('S002V01TAREA')
  751. ->where('AREA_COAL', '=', $idWarehouse)
  752. ->where('AREA_NULI', '=', $line)
  753. ->get([
  754. 'AREA_COAR AS CODIGO_AREA',
  755. 'AREA_NOAR AS NOMBRE_AREA',
  756. 'AREA_COME AS COMENTARIOS',
  757. 'AREA_ESTA AS ESTADO',
  758. 'AREA_USRE AS USUARIO_REGISTRA',
  759. 'AREA_FERE AS FECHA_REGISTRA',
  760. 'AREA_USMO AS USUARIO_MODIFICA',
  761. 'AREA_FEMO AS FECHA_MODIFICA',
  762. ]);
  763. $arrArea = json_decode(json_encode($getArea), true);
  764. } catch (\Throwable $th) {
  765. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBY002: Ocurrió un error al obtener los registros del área.', $th->getMessage(), 406);
  766. }
  767. foreach ($arrArea as $key => $value) {
  768. try {
  769. $countStock = DB::table('S002V01TUBAR')
  770. ->where('UBAR_NULI', '=', $line)
  771. ->where('UBAR_ESTA', '=', 'Activo')
  772. ->where('UBAR_COAL', '=', $idWarehouse)
  773. ->where('UBAR_COAR', '=', $value['CODIGO_AREA'])
  774. ->count();
  775. } catch (\Throwable $th) {
  776. DB::rollBack();
  777. return $this->responseController->makeResponse(
  778. true,
  779. "ERR_WAREHOUSE_GET003: Ocurrió un error al obtener los almacenes.",
  780. $th->getMessage(),
  781. 500
  782. );
  783. }
  784. $value['CANTIDAD_STOCK'] = $countStock;
  785. $arrArea[$key] = $value;
  786. }
  787. $responseCheckLatestUpdate = $this->resourcesController->checkLatestUpdate($arrArea, $line);
  788. if ($responseCheckLatestUpdate['error']) {
  789. return $this->responseController->makeResponse(true, $responseCheckLatestUpdate['msg'], [], 500);
  790. }
  791. $arrArea = $responseCheckLatestUpdate['response'];
  792. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrArea);
  793. }
  794. public function getAreaById($idArea, $user, $line) {
  795. try {
  796. $idArea = $this->encController->decrypt($idArea);
  797. } catch (\Throwable $th) {
  798. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBY000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  799. }
  800. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  801. if ($arrResponseCheckUser['error']) {
  802. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBY001:'.$arrResponseCheckUser['msg'], [], 401);
  803. }
  804. try {
  805. $getArea = (array) DB::table('S002V01TAREA')
  806. ->where('AREA_COAR', '=', $idArea)
  807. ->where('AREA_NULI', '=', $line)
  808. ->first([
  809. 'AREA_NOAR AS NOMBRE_AREA',
  810. 'AREA_COME AS COMENTARIOS',
  811. 'AREA_ESTA AS ESTADO',
  812. 'AREA_USRE AS USUARIO_REGISTRA',
  813. 'AREA_FERE AS FECHA_REGISTRA',
  814. 'AREA_USMO AS USUARIO_MODIFICA',
  815. 'AREA_FEMO AS FECHA_MODIFICA',
  816. ]);
  817. $arrArea = json_decode(json_encode($getArea), true);
  818. } catch (\Throwable $th) {
  819. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBY002: Ocurrió un error al obtener los registros del área.', $th->getMessage(), 406);
  820. }
  821. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrArea);
  822. }
  823. public function getAreaByWarehouseActives($idWarehouse, $user, $line) {
  824. try {
  825. $idWarehouse = $this->encController->decrypt($idWarehouse);
  826. } catch (\Throwable $th) {
  827. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBYACTIVE000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  828. }
  829. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  830. if ($arrResponseCheckUser['error']) {
  831. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBYACTIVE001:'.$arrResponseCheckUser['msg'], [], 401);
  832. }
  833. try {
  834. $validateExists = DB::table('S002V01TALMA')
  835. ->where('ALMA_NULI', '=', $line)
  836. ->where('ALMA_COAL', '=', $idWarehouse)
  837. ->exists();
  838. } catch (\Throwable $th) {
  839. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBYACTIVE002: Ocurrió un error al obtener el almacen.', $th->getMessage(), 406);
  840. }
  841. if (!$validateExists) {
  842. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBYACTIVE003: El almacen no existe.', [], 401);
  843. }
  844. try {
  845. $getArea = DB::table('S002V01TAREA')
  846. ->where('AREA_COAL', '=', $idWarehouse)
  847. ->where('AREA_NULI', '=', $line)
  848. ->where('AREA_ESTA', '=', 'Activo')
  849. ->get([
  850. 'AREA_COAR AS CODIGO_AREA',
  851. 'AREA_NOAR AS NOMBRE_AREA',
  852. 'AREA_COME AS COMENTARIOS',
  853. ]);
  854. $arrArea = json_decode(json_encode($getArea), true);
  855. } catch (\Throwable $th) {
  856. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBYACTIVE004: Ocurrió un error al obtener los registros del área.', $th->getMessage(), 406);
  857. }
  858. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrArea);
  859. }
  860. public function registerArea(Request $request) {
  861. $validator = Validator::make($request->all(), [
  862. 'CODIGO_ALMACEN' => 'required|string',
  863. 'CODIGO_AREA' => 'required|string',
  864. 'NOMBRE_AREA' => 'required|string',
  865. 'COMENTARIOS' => 'nullable|string',
  866. 'USUARIO' => 'required|string',
  867. 'NUMERO_LINEA' => 'required|integer',
  868. ]);
  869. if ($validator->fails()) {
  870. return $this->responseController->makeResponse(
  871. true,
  872. "ERR_AREA_REG000: Se encontraron uno o más errores.",
  873. $this->responseController->makeErrors($validator->errors()->messages()),
  874. 401
  875. );
  876. }
  877. DB::beginTransaction();
  878. $requestData = $request->all();
  879. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  880. if ($arrResponseCheckUser['error']) {
  881. return $this->responseController->makeResponse(true, 'ERR_AREA_REG001:'.$arrResponseCheckUser['msg'], [], 401);
  882. }
  883. $user = $arrResponseCheckUser['response'];
  884. try {
  885. $validateExistWarehouse = DB::table('S002V01TALMA')
  886. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  887. ->where('ALMA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  888. ->where('ALMA_ESTA', '=', 'Activo')
  889. ->exists();
  890. } catch (\Throwable $th) {
  891. DB::rollBack();
  892. return $this->responseController->makeResponse(true, 'ERR_AREA_REG002: Ocurrió un error al verificar el almacen.', $th->getMessage(), 500);
  893. }
  894. if (!$validateExistWarehouse) {
  895. DB::rollBack();
  896. return $this->responseController->makeResponse(true, 'ERR_AREA_REG003: El almacen no existe.', [], 406);
  897. }
  898. try {
  899. $validateExistCode = DB::table('S002V01TAREA')
  900. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  901. ->where('AREA_COAR', '=', $requestData['CODIGO_AREA'])
  902. ->where('AREA_ESTA', '=', 'Activo')
  903. ->exists();
  904. } catch (\Throwable $th) {
  905. DB::rollBack();
  906. return $this->responseController->makeResponse(true, 'ERR_AREA_REG004: Ocurrió un error al verificar el código del área.', $th->getMessage(), 500);
  907. }
  908. if ($validateExistCode) {
  909. DB::rollBack();
  910. return $this->responseController->makeResponse(true, 'ERR_AREA_REG005: El código del área ya se encuentra registrado.', [], 406);
  911. }
  912. $now = $this->functionsController->now();
  913. $currentDate = $now->toDateTimeString();
  914. try {
  915. $validateInsert = DB::table('S002V01TAREA')->insert([
  916. 'AREA_COAL' => $requestData['CODIGO_ALMACEN'],
  917. 'AREA_COAR' => $requestData['CODIGO_AREA'],
  918. 'AREA_NOAR' => $requestData['NOMBRE_AREA'],
  919. 'AREA_COME' => $requestData['COMENTARIOS'],
  920. 'AREA_NULI' => $requestData['NUMERO_LINEA'],
  921. 'AREA_USRE' => $user,
  922. 'AREA_FERE' => $currentDate,
  923. 'AREA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  924. ]);
  925. } catch (\Throwable $th) {
  926. DB::rollBack();
  927. return $this->responseController->makeResponse(true, 'ERR_AREA_REG006: Ocurrió un error al ingresar la información a la base de datos.', $th->getMessage(), 500);
  928. }
  929. if (!$validateInsert) {
  930. DB::rollBack();
  931. return $this->responseController->makeResponse(true, 'ERR_AREA_REG007: No se pudo guardar la información en la base de datos.', [], 406);
  932. }
  933. DB::commit();
  934. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  935. }
  936. public function updateArea(Request $request, $idArea) {
  937. $validator = Validator::make($request->all(), [
  938. 'CODIGO_ALMACEN' => 'required|string',
  939. 'NOMBRE_AREA' => 'required|string',
  940. 'COMENTARIOS' => 'nullable|string',
  941. 'USUARIO' => 'required|string',
  942. 'NUMERO_LINEA' => 'required|integer',
  943. ]);
  944. if ($validator->fails()) {
  945. return $this->responseController->makeResponse(
  946. true,
  947. "ERR_AREA_UPD000: Se encontraron uno o más errores.",
  948. $this->responseController->makeErrors($validator->errors()->messages()),
  949. 401
  950. );
  951. }
  952. DB::beginTransaction();
  953. $requestData = $request->all();
  954. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  955. if ($arrResponseCheckUser['error']) {
  956. DB::rollBack();
  957. return $this->responseController->makeResponse(true, 'ERR_AREA_UPD001:'.$arrResponseCheckUser['msg'], $arrResponseCheckUser['response'], 401);
  958. }
  959. $user = $arrResponseCheckUser['response'];
  960. try {
  961. $idArea = $this->encController->decrypt($idArea);
  962. } catch (\Throwable $th) {
  963. DB::rollBack();
  964. return $this->responseController->makeResponse(true, 'ERR_AREA_UPD002: Ocurrió un error al desencriptar el área.', $th->getMessage(), 500);
  965. }
  966. try {
  967. $validateExistArea = DB::table('S002V01TAREA')
  968. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  969. ->where('AREA_COAR', '=', $idArea)
  970. ->where('AREA_ESTA', '=', 'Activo')
  971. ->exists();
  972. } catch (\Throwable $th) {
  973. DB::rollBack();
  974. return $this->responseController->makeResponse(true, 'ERR_AREA_UPD003: Ocurrió un error al verificar el código del área.', $th->getMessage(), 500);
  975. }
  976. if (!$validateExistArea) {
  977. DB::rollBack();
  978. return $this->responseController->makeResponse(true, 'ERR_AREA_UPD004: El área no existe.', [], 406);
  979. }
  980. try {
  981. $validateExistWarehouse = DB::table('S002V01TALMA')
  982. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  983. ->where('ALMA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  984. ->where('ALMA_ESTA', '=', 'Activo')
  985. ->exists();
  986. } catch (\Throwable $th) {
  987. DB::rollBack();
  988. return $this->responseController->makeResponse(true, 'ERR_AREA_UPD005: Ocurrió un error al verificar el almacen.', $th->getMessage(), 500);
  989. }
  990. if (!$validateExistWarehouse) {
  991. DB::rollBack();
  992. return $this->responseController->makeResponse(true, 'ERR_AREA_UPD006: El almacen no existe.', [], 406);
  993. }
  994. $now = $this->functionsController->now();
  995. $currentDate = $now->toDateTimeString();
  996. try {
  997. $validateUpdate = DB::table('S002V01TAREA')
  998. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  999. ->where('AREA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1000. ->where('AREA_COAR', '=', $idArea)
  1001. ->update([
  1002. 'AREA_NOAR' => $requestData['NOMBRE_AREA'],
  1003. 'AREA_COME' => $requestData['COMENTARIOS'],
  1004. 'AREA_USMO' => $user,
  1005. 'AREA_FEMO' => $currentDate,
  1006. 'AREA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  1007. ]);
  1008. } catch (\Throwable $th) {
  1009. DB::rollBack();
  1010. return $this->responseController->makeResponse(true, 'ERR_AREA_UPD009: Ocurrió un error al ingresar la información a la base de datos.', $th->getMessage(), 500);
  1011. }
  1012. if (!$validateUpdate) {
  1013. DB::rollBack();
  1014. return $this->responseController->makeResponse(true, 'ERR_AREA_UPD010: No se pudo guardar la información en la base de datos.', [], 406);
  1015. }
  1016. DB::commit();
  1017. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  1018. }
  1019. public function deleteArea(Request $request, $idArea) {
  1020. $validator = Validator::make($request->all(), [
  1021. 'USUARIO' => 'required|string',
  1022. 'NUMERO_LINEA' => 'required|integer',
  1023. ]);
  1024. if ($validator->fails()) {
  1025. return $this->responseController->makeResponse(
  1026. true,
  1027. "ERR_AREA_DEL000: Se encontraron uno o más errores.",
  1028. $this->responseController->makeErrors($validator->errors()->messages()),
  1029. 401
  1030. );
  1031. }
  1032. DB::beginTransaction();
  1033. $requestData = $request->all();
  1034. try {
  1035. $idArea = $this->encController->decrypt($idArea);
  1036. } catch (\Throwable $th) {
  1037. DB::rollBack();
  1038. return $this->responseController->makeResponse(true, 'ERR_AREA_DEL001: Ocurrió un error al desencriptar el nivel.', $th->getMessage(), 500);
  1039. }
  1040. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  1041. if ($arrResponseCheckUser['error']) {
  1042. DB::rollBack();
  1043. return $this->responseController->makeResponse(true, 'ERR_AREA_DEL002:'.$arrResponseCheckUser['msg'], [], 401);
  1044. }
  1045. $user = $arrResponseCheckUser['response'];
  1046. try {
  1047. $arrArea = (array) DB::table('S002V01TAREA')
  1048. ->where('AREA_COAR', '=', $idArea)
  1049. ->where('AREA_ESTA', '=', 'Activo')
  1050. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  1051. ->first([
  1052. 'AREA_COAL',
  1053. ]);
  1054. } catch (\Throwable $th) {
  1055. DB::rollBack();
  1056. return $this->responseController->makeResponse(true, 'ERR_AREA_DEL003: Ocurrió un error al obtener la información del área.', $th->getMessage(), 500);
  1057. }
  1058. if (empty($arrArea)) {
  1059. DB::rollBack();
  1060. return $this->responseController->makeResponse(true, 'ERR_AREA_DEL004: El área no existe.', [], 406);
  1061. }
  1062. try {
  1063. $validateExists = DB::table('S002V01TUBAR')
  1064. ->where('UBAR_COAL', '=', $arrArea['AREA_COAL'])
  1065. ->where('UBAR_COZO', '=', $idArea)
  1066. ->where('UBAR_NULI', '=', $requestData['NUMERO_LINEA'])
  1067. ->where('UBAR_ESTA', '=', 'Activo')
  1068. ->exists();
  1069. } catch (\Throwable $th) {
  1070. DB::rollBack();
  1071. return $this->responseController->makeResponse(true, 'ERR_AREA_DEL005: Ocurrió un error al obtener las ubicaciones de los artículos.', $th->getMessage(), 500);
  1072. }
  1073. if($validateExists) {
  1074. DB::rollBack();
  1075. return $this->responseController->makeResponse(true, 'ERR_AREA_DEL006: No se puede eliminar debido a que existen artículos ubicados en el área.', [], 406);
  1076. }
  1077. try {
  1078. $countLevel = DB::table('S002V01TNIVE')
  1079. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  1080. ->where('NIVE_COAL', '=', $arrArea['AREA_COAL'])
  1081. ->where('NIVE_COAR', '=', $idArea)
  1082. ->where('NIVE_ESTA', '=', 'Activo')
  1083. ->count();
  1084. } catch (\Throwable $th) {
  1085. DB::rollBack();
  1086. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL007: Ocurrió un error al obtener las zonas.', $th->getMessage(), 500);
  1087. }
  1088. if ($countLevel > 0) {
  1089. DB::rollBack();
  1090. $quantity = $countLevel === 1 ? 'existe 1 nivel activo' : 'existen '.$countLevel.' niveles activos';
  1091. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL008: No se puede eliminar debido a que '.$quantity.'.', [], 406);
  1092. }
  1093. try {
  1094. $countZones = DB::table('S002V01TZONA')
  1095. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  1096. ->where('ZONA_COAL', '=', $arrArea['AREA_COAL'])
  1097. ->where('ZONA_COAR', '=', $idArea)
  1098. ->where('ZONA_ESTA', '=', 'Activo')
  1099. ->count();
  1100. } catch (\Throwable $th) {
  1101. DB::rollBack();
  1102. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL009: Ocurrió un error al obtener las zonas.', $th->getMessage(), 500);
  1103. }
  1104. if ($countZones > 0) {
  1105. DB::rollBack();
  1106. $quantity = $countZones === 1 ? 'existe 1 zona activa' : 'existen '.$countZones.' zonas activas';
  1107. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL010: No se puede eliminar debido a que '.$quantity.'.', [], 406);
  1108. }
  1109. $now = $this->functionsController->now();
  1110. $currentDate = $now->toDateTimeString();
  1111. try {
  1112. $validateUpdate = DB::table('S002V01TAREA')
  1113. ->where('AREA_COAR', '=', $idArea)
  1114. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  1115. ->update([
  1116. 'AREA_ESTA' => 'Eliminado',
  1117. 'AREA_USMO' => $user,
  1118. 'AREA_FEMO' => $currentDate,
  1119. 'AREA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  1120. ]);
  1121. } catch (\Throwable $th) {
  1122. DB::rollBack();
  1123. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL011: Ocurrió un error al eliminar el área.', $th->getMessage(), 500);
  1124. }
  1125. if (!$validateUpdate) {
  1126. DB::rollBack();
  1127. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL012: No se pudo eliminar el área.', [], 406);
  1128. }
  1129. DB::commit();
  1130. return $this->responseController->makeResponse(false, "ÉXITO: Eliminación Exitosa");
  1131. }
  1132. public function getLevelByAreaWarehouse($idWarehouse, $idArea, $user, $line) {
  1133. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  1134. if ($arrResponseCheckUser['error']) {
  1135. DB::rollBack();
  1136. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_GETBY000:'.$arrResponseCheckUser['msg'], [], 401);
  1137. }
  1138. try {
  1139. $idWarehouse = $this->encController->decrypt($idWarehouse);
  1140. } catch (\Throwable $th) {
  1141. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBY000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  1142. }
  1143. try {
  1144. $idArea = $this->encController->decrypt($idArea);
  1145. } catch (\Throwable $th) {
  1146. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBY001: Ocurrió un error al desencriptar el ID del área', $th->getMessage(), 406);
  1147. }
  1148. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  1149. if ($arrResponseCheckUser['error']) {
  1150. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBY002:'.$arrResponseCheckUser['msg'], [], 401);
  1151. }
  1152. try {
  1153. $getLevel = DB::table('S002V01TNIVE')
  1154. ->where('NIVE_COAL', '=', $idWarehouse)
  1155. ->where('NIVE_COAR', '=', $idArea)
  1156. ->where('NIVE_NULI', '=', $line)
  1157. ->get([
  1158. 'NIVE_CONI AS CODIGO_NIVEL',
  1159. 'NIVE_NONI AS NOMBRE_NIVEL',
  1160. 'NIVE_COME AS COMENTARIOS',
  1161. 'NIVE_ESTA AS ESTADO',
  1162. 'NIVE_USRE AS USUARIO_REGISTRA',
  1163. 'NIVE_FERE AS FECHA_REGISTRA',
  1164. 'NIVE_USMO AS USUARIO_MODIFICA',
  1165. 'NIVE_FEMO AS FECHA_MODIFICA',
  1166. ]);
  1167. $arrLevel = json_decode(json_encode($getLevel), true);
  1168. } catch (\Throwable $th) {
  1169. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBY003: Ocurrió un error al obtener los registros del área.', $th->getMessage(), 406);
  1170. }
  1171. foreach ($arrLevel as $key => $value) {
  1172. try {
  1173. $countStock = DB::table('S002V01TUBAR')
  1174. ->where('UBAR_NULI', '=', $line)
  1175. ->where('UBAR_ESTA', '=', 'Activo')
  1176. ->where('UBAR_COAL', '=', $idWarehouse)
  1177. ->where('UBAR_COAR', '=', $idArea)
  1178. ->where('UBAR_CONI', '=', $value['CODIGO_NIVEL'])
  1179. ->count();
  1180. } catch (\Throwable $th) {
  1181. DB::rollBack();
  1182. return $this->responseController->makeResponse(
  1183. true,
  1184. "ERR_WAREHOUSE_GET003: Ocurrió un error al obtener los almacenes.",
  1185. $th->getMessage(),
  1186. 500
  1187. );
  1188. }
  1189. $value['CANTIDAD_STOCK'] = $countStock;
  1190. $arrLevel[$key] = $value;
  1191. }
  1192. $responseCheckLatestUpdate = $this->resourcesController->checkLatestUpdate($arrLevel, $line);
  1193. if ($responseCheckLatestUpdate['error']) {
  1194. return $this->responseController->makeResponse(true, $responseCheckLatestUpdate['msg'], [], 500);
  1195. }
  1196. $arrLevel = $responseCheckLatestUpdate['response'];
  1197. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrLevel);
  1198. }
  1199. public function getLevelById( $idLevel, $user, $line ) {
  1200. try {
  1201. $idLevel = $this->encController->decrypt($idLevel);
  1202. } catch (\Throwable $th) {
  1203. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBY000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  1204. }
  1205. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  1206. if ($arrResponseCheckUser['error']) {
  1207. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBY001:'.$arrResponseCheckUser['msg'], [], 401);
  1208. }
  1209. try {
  1210. $getLevel = (array) DB::table('S002V01TNIVE')
  1211. ->where('NIVE_CONI', '=', $idLevel)
  1212. ->where('NIVE_NULI', '=', $line)
  1213. ->first([
  1214. 'NIVE_NONI AS NOMBRE_NIVEL',
  1215. 'NIVE_COME AS COMENTARIOS',
  1216. 'NIVE_ESTA AS ESTADO',
  1217. 'NIVE_USRE AS USUARIO_REGISTRA',
  1218. 'NIVE_FERE AS FECHA_REGISTRA',
  1219. 'NIVE_USMO AS USUARIO_MODIFICA',
  1220. 'NIVE_FEMO AS FECHA_MODIFICA',
  1221. ]);
  1222. $arrLevel = json_decode(json_encode($getLevel), true);
  1223. } catch (\Throwable $th) {
  1224. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBY002: Ocurrió un error al obtener los registros del área.', $th->getMessage(), 406);
  1225. }
  1226. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrLevel);
  1227. }
  1228. public function getLevelByAreaWarehouseActives($idWarehouse, $idArea, $user, $line) {
  1229. try {
  1230. $idWarehouse = $this->encController->decrypt($idWarehouse);
  1231. } catch (\Throwable $th) {
  1232. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBYACTIVE000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  1233. }
  1234. try {
  1235. $idArea = $this->encController->decrypt($idArea);
  1236. } catch (\Throwable $th) {
  1237. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBYACTIVE001: Ocurrió un error al desencriptar el ID del área', $th->getMessage(), 406);
  1238. }
  1239. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  1240. if ($arrResponseCheckUser['error']) {
  1241. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBYACTIVE002:'.$arrResponseCheckUser['msg'], [], 401);
  1242. }
  1243. try {
  1244. $validateExists = DB::table('S002V01TALMA')
  1245. ->where('ALMA_NULI', '=', $line)
  1246. ->where('ALMA_COAL', '=', $idWarehouse)
  1247. ->exists();
  1248. } catch (\Throwable $th) {
  1249. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBYACTIVE003: Ocurrió un error al obtener el almacen.', $th->getMessage(), 406);
  1250. }
  1251. if (!$validateExists) {
  1252. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBYACTIVE004: El almacen no existe.', [], 401);
  1253. }
  1254. try {
  1255. $validateExists = DB::table('S002V01TAREA')
  1256. ->where('AREA_NULI', '=', $line)
  1257. ->where('AREA_COAL', '=', $idWarehouse)
  1258. ->where('AREA_COAR', '=', $idArea)
  1259. ->exists();
  1260. } catch (\Throwable $th) {
  1261. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBYACTIVE005: Ocurrió un error al obtener el área.', $th->getMessage(), 406);
  1262. }
  1263. if (!$validateExists) {
  1264. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBYACTIVE006: El área no existe.', [], 401);
  1265. }
  1266. try {
  1267. $getLevel = DB::table('S002V01TNIVE')
  1268. ->where('NIVE_COAL', '=', $idWarehouse)
  1269. ->where('NIVE_COAR', '=', $idArea)
  1270. ->where('NIVE_NULI', '=', $line)
  1271. ->where('NIVE_ESTA', '=', 'Activo')
  1272. ->get([
  1273. 'NIVE_CONI AS CODIGO_NIVEL',
  1274. 'NIVE_NONI AS NOMBRE_NIVEL',
  1275. 'NIVE_COME AS COMENTARIOS',
  1276. ]);
  1277. $arrLevel = json_decode(json_encode($getLevel), true);
  1278. } catch (\Throwable $th) {
  1279. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBYACTIVE007: Ocurrió un error al obtener los registros del área.', $th->getMessage(), 406);
  1280. }
  1281. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrLevel);
  1282. }
  1283. public function registerLevel(Request $request) {
  1284. $validator = Validator::make($request->all(), [
  1285. 'CODIGO_ALMACEN' => 'required|string',
  1286. 'CODIGO_AREA' => 'required|string',
  1287. 'CODIGO_NIVEL' => 'required|string',
  1288. 'NOMBRE_NIVEL' => 'required|string',
  1289. 'COMENTARIOS' => 'nullable|string',
  1290. 'USUARIO' => 'required|string',
  1291. 'NUMERO_LINEA' => 'required|integer',
  1292. ]);
  1293. if ($validator->fails()) {
  1294. return $this->responseController->makeResponse(
  1295. true,
  1296. "ERR_LEVEL_REG000: Se encontraron uno o más errores.",
  1297. $this->responseController->makeErrors($validator->errors()->messages()),
  1298. 401
  1299. );
  1300. }
  1301. DB::beginTransaction();
  1302. $requestData = $request->all();
  1303. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  1304. if ($arrResponseCheckUser['error']) {
  1305. return $this->responseController->makeResponse(true, 'ERR_LEVEL_REG001:'.$arrResponseCheckUser['msg'], [], 401);
  1306. }
  1307. $user = $arrResponseCheckUser['response'];
  1308. try {
  1309. $validateExistWarehouse = DB::table('S002V01TALMA')
  1310. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  1311. ->where('ALMA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1312. ->where('ALMA_ESTA', '=', 'Activo')
  1313. ->exists();
  1314. } catch (\Throwable $th) {
  1315. DB::rollBack();
  1316. return $this->responseController->makeResponse(true, 'ERR_LEVEL_REG002: Ocurrió un error al verificar el almacen.', $th->getMessage(), 500);
  1317. }
  1318. if (!$validateExistWarehouse) {
  1319. DB::rollBack();
  1320. return $this->responseController->makeResponse(true, 'ERR_LEVEL_REG003: El almacen no existe.', [], 406);
  1321. }
  1322. try {
  1323. $validateExistArea = DB::table('S002V01TAREA')
  1324. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  1325. ->where('AREA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1326. ->where('AREA_COAR', '=', $requestData['CODIGO_AREA'])
  1327. ->where('AREA_ESTA', '=', 'Activo')
  1328. ->exists();
  1329. } catch (\Throwable $th) {
  1330. DB::rollBack();
  1331. return $this->responseController->makeResponse(true, 'ERR_LEVEL_REG004: Ocurrió un error al verificar el área.', $th->getMessage(), 500);
  1332. }
  1333. if (!$validateExistArea) {
  1334. DB::rollBack();
  1335. return $this->responseController->makeResponse(true, 'ERR_LEVEL_REG005: El área no existe.', [], 406);
  1336. }
  1337. try {
  1338. $validateExistLevel = DB::table('S002V01TNIVE')
  1339. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  1340. ->where('NIVE_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1341. ->where('NIVE_COAR', '=', $requestData['CODIGO_AREA'])
  1342. ->where('NIVE_CONI', '=', $requestData['CODIGO_NIVEL'])
  1343. ->exists();
  1344. } catch (\Throwable $th) {
  1345. DB::rollBack();
  1346. return $this->responseController->makeResponse(true, 'ERR_LEVEL_REG006: Ocurrió un error al verificar el código del nivel.', $th->getMessage(), 406);
  1347. }
  1348. if ($validateExistLevel) {
  1349. DB::rollBack();
  1350. return $this->responseController->makeResponse(true, 'ERR_LEVEL_REG007: El código del nivel ya existe.', [], 406);
  1351. }
  1352. $now = $this->functionsController->now();
  1353. $currentDate = $now->toDateTimeString();
  1354. try {
  1355. $validateInsert = DB::table('S002V01TNIVE')->insert([
  1356. 'NIVE_NULI' => $requestData['NUMERO_LINEA'],
  1357. 'NIVE_COAL' => $requestData['CODIGO_ALMACEN'],
  1358. 'NIVE_COAR' => $requestData['CODIGO_AREA'],
  1359. 'NIVE_CONI' => $requestData['CODIGO_NIVEL'],
  1360. 'NIVE_NONI' => $requestData['NOMBRE_NIVEL'],
  1361. 'NIVE_COME' => $requestData['COMENTARIOS'],
  1362. 'NIVE_USRE' => $user,
  1363. 'NIVE_FERE' => $currentDate,
  1364. 'NIVE_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  1365. ]);
  1366. } catch (\Throwable $th) {
  1367. DB::rollBack();
  1368. return $this->responseController->makeResponse(true, 'ERR_LEVEL_REG008: Ocurrió un error al ingresar la información a la base de datos.', $th->getMessage(), 500);
  1369. }
  1370. if (!$validateInsert) {
  1371. DB::rollBack();
  1372. return $this->responseController->makeResponse(true, 'ERR_LEVEL_REG009: No se pudo guardar la información en la base de datos.', [], 406);
  1373. }
  1374. DB::commit();
  1375. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  1376. }
  1377. public function updateLevel(Request $request, $idLevel) {
  1378. $validator = Validator::make($request->all(), [
  1379. 'CODIGO_ALMACEN' => 'required|string',
  1380. 'NOMBRE_NIVEL' => 'required|string',
  1381. 'COMENTARIOS' => 'nullable|string',
  1382. 'USUARIO' => 'required|string',
  1383. 'NUMERO_LINEA' => 'required|integer',
  1384. ]);
  1385. if ($validator->fails()) {
  1386. return $this->responseController->makeResponse(
  1387. true,
  1388. "ERR_LEVEL_UPD000: Se encontraron uno o más errores.",
  1389. $this->responseController->makeErrors($validator->errors()->messages()),
  1390. 401
  1391. );
  1392. }
  1393. DB::beginTransaction();
  1394. $requestData = $request->all();
  1395. try {
  1396. $idLevel = $this->encController->decrypt($idLevel);
  1397. } catch (\Throwable $th) {
  1398. DB::rollBack();
  1399. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD001: Ocurrió un error al desencriptar el área.', $th->getMessage(), 500);
  1400. }
  1401. try {
  1402. $validateExistLevel = DB::table('S002V01TNIVE')
  1403. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  1404. ->where('NIVE_CONI', '=', $idLevel)
  1405. ->where('NIVE_ESTA', '=', 'Activo')
  1406. ->exists();
  1407. } catch (\Throwable $th) {
  1408. DB::rollBack();
  1409. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD002: Ocurrió un error al verificar el código del área.', $th->getMessage(), 500);
  1410. }
  1411. if (!$validateExistLevel) {
  1412. DB::rollBack();
  1413. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD003: El área no existe.', [], 406);
  1414. }
  1415. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  1416. if ($arrResponseCheckUser['error']) {
  1417. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD004:'.$arrResponseCheckUser['msg'], [], 401);
  1418. }
  1419. $user = $arrResponseCheckUser['response'];
  1420. try {
  1421. $validateExistWarehouse = DB::table('S002V01TALMA')
  1422. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  1423. ->where('ALMA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1424. ->where('ALMA_ESTA', '=', 'Activo')
  1425. ->exists();
  1426. } catch (\Throwable $th) {
  1427. DB::rollBack();
  1428. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD005: Ocurrió un error al verificar el almacen.', $th->getMessage(), 500);
  1429. }
  1430. if (!$validateExistWarehouse) {
  1431. DB::rollBack();
  1432. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD006: El almacen no existe.', [], 406);
  1433. }
  1434. try {
  1435. $validateExistArea = DB::table('S002V01TAREA')
  1436. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  1437. ->where('AREA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1438. ->where('AREA_COAR', '=', $requestData['CODIGO_AREA'])
  1439. ->where('AREA_ESTA', '=', 'Activo')
  1440. ->exists();
  1441. } catch (\Throwable $th) {
  1442. DB::rollBack();
  1443. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD007: Ocurrió un error al verificar el área.', $th->getMessage(), 500);
  1444. }
  1445. if (!$validateExistArea) {
  1446. DB::rollBack();
  1447. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD008: El área no existe.', [], 406);
  1448. }
  1449. try {
  1450. $validateExistLevel = DB::table('S002V01TNIVE')
  1451. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  1452. ->where('NIVE_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1453. ->where('NIVE_COAR', '=', $requestData['CODIGO_AREA'])
  1454. ->where('NIVE_CONI', '=', $requestData['CODIGO_NIVEL'])
  1455. ->exists();
  1456. } catch (\Throwable $th) {
  1457. DB::rollBack();
  1458. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD009: Ocurrió un error al verificar el código del nivel.', $th->getMessage(), 406);
  1459. }
  1460. if ($validateExistLevel) {
  1461. DB::rollBack();
  1462. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD010: El código del nivel ya existe.', [], 406);
  1463. }
  1464. $now = $this->functionsController->now();
  1465. $currentDate = $now->toDateTimeString();
  1466. try {
  1467. $validateUpdate = DB::table('S002V01TNIVE')
  1468. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  1469. ->where('NIVE_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1470. ->where('NIVE_COAR', '=', $requestData['CODIGO_AREA'])
  1471. ->where('NIVE_CONI', '=', $idLevel)
  1472. ->update([
  1473. 'NIVE_NONI' => $requestData['NOMBRE_NIVEL'],
  1474. 'NIVE_COME' => $requestData['COMENTARIOS'],
  1475. 'NIVE_USMO' => $user,
  1476. 'NIVE_FEMO' => $currentDate,
  1477. 'NIVE_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  1478. ]);
  1479. } catch (\Throwable $th) {
  1480. DB::rollBack();
  1481. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD011: Ocurrió un error al ingresar la información a la base de datos.', $th->getMessage(), 500);
  1482. }
  1483. if (!$validateUpdate) {
  1484. DB::rollBack();
  1485. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD012: No se pudo guardar la información en la base de datos.', [], 406);
  1486. }
  1487. DB::commit();
  1488. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  1489. }
  1490. public function deleteLevel(Request $request, $idLevel) {
  1491. $validator = Validator::make($request->all(), [
  1492. 'USUARIO' => 'required|string',
  1493. 'NUMERO_LINEA' => 'required|integer',
  1494. ]);
  1495. if ($validator->fails()) {
  1496. return $this->responseController->makeResponse(
  1497. true,
  1498. "ERR_LEVEL_DEL000: Se encontraron uno o más errores.",
  1499. $this->responseController->makeErrors($validator->errors()->messages()),
  1500. 401
  1501. );
  1502. }
  1503. DB::beginTransaction();
  1504. $requestData = $request->all();
  1505. try {
  1506. $idLevel = $this->encController->decrypt($idLevel);
  1507. } catch (\Throwable $th) {
  1508. DB::rollBack();
  1509. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL001: Ocurrió un error al desencriptar el nivel.', $th->getMessage(), 500);
  1510. }
  1511. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  1512. if ($arrResponseCheckUser['error']) {
  1513. DB::rollBack();
  1514. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL002:'.$arrResponseCheckUser['msg'], [], 401);
  1515. }
  1516. $user = $arrResponseCheckUser['response'];
  1517. try {
  1518. $arrLevel = (array) DB::table('S002V01TNIVE')
  1519. ->where('NIVE_CONI', '=', $idLevel)
  1520. ->where('NIVE_ESTA', '=', 'Activo')
  1521. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  1522. ->first([
  1523. 'NIVE_COAL',
  1524. 'NIVE_COAR'
  1525. ]);
  1526. } catch (\Throwable $th) {
  1527. DB::rollBack();
  1528. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL003: Ocurrió un error al obtener la información del nivel.', $th->getMessage(), 500);
  1529. }
  1530. if (empty($arrLevel)) {
  1531. DB::rollBack();
  1532. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL004: El nivel no existe.', [], 406);
  1533. }
  1534. try {
  1535. $validateExists = DB::table('S002V01TUBAR')
  1536. ->where('UBAR_COAL', '=', $arrLevel['NIVE_COAL'])
  1537. ->where('UBAR_COAR', '=', $arrLevel['NIVE_COAR'])
  1538. ->where('UBAR_COZO', '=', $idLevel)
  1539. ->where('UBAR_NULI', '=', $requestData['NUMERO_LINEA'])
  1540. ->where('UBAR_ESTA', '=', 'Activo')
  1541. ->exists();
  1542. } catch (\Throwable $th) {
  1543. DB::rollBack();
  1544. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL005: Ocurrió un error al obtener las ubicaciones de los artículos.', $th->getMessage(), 500);
  1545. }
  1546. if($validateExists) {
  1547. DB::rollBack();
  1548. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL006: No se puede eliminar debido a que existen artículos ubicados en el nivel.', [], 406);
  1549. }
  1550. try {
  1551. $countZones = DB::table('S002V01TZONA')
  1552. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  1553. ->where('ZONA_COAL', '=', $arrLevel['NIVE_COAL'])
  1554. ->where('ZONA_COAR', '=', $arrLevel['NIVE_COAR'])
  1555. ->where('ZONA_CONI', '=', $idLevel)
  1556. ->where('ZONA_ESTA', '=', 'Activo')
  1557. ->count();
  1558. } catch (\Throwable $th) {
  1559. DB::rollBack();
  1560. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL007: Ocurrió un error al obtener las zonas.', $th->getMessage(), 500);
  1561. }
  1562. if ($countZones > 0) {
  1563. DB::rollBack();
  1564. $quantity = $countZones === 1 ? 'existe 1 zona activa' : 'existen '.$countZones.' zonas activas';
  1565. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL008: No se puede eliminar debido a que '.$quantity.'.', [], 406);
  1566. }
  1567. $now = $this->functionsController->now();
  1568. $currentDate = $now->toDateTimeString();
  1569. try {
  1570. $validateUpdate = DB::table('S002V01TNIVE')
  1571. ->where('NIVE_CONI', '=', $idLevel)
  1572. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  1573. ->update([
  1574. 'NIVE_ESTA' => 'Eliminado',
  1575. 'NIVE_USMO' => $user,
  1576. 'NIVE_FEMO' => $currentDate,
  1577. 'NIVE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  1578. ]);
  1579. } catch (\Throwable $th) {
  1580. DB::rollBack();
  1581. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL009: Ocurrió un error al eliminar el nivel.', $th->getMessage(), 500);
  1582. }
  1583. if (!$validateUpdate) {
  1584. DB::rollBack();
  1585. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL010: No se pudo eliminar el nivel.', [], 406);
  1586. }
  1587. DB::commit();
  1588. return $this->responseController->makeResponse(false, "ÉXITO: Eliminación Exitosa");
  1589. }
  1590. public function getZoneByLevelAreaWarehouse($idWarehouse, $idArea, $idLevel, $user, $line) {
  1591. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  1592. if ($arrResponseCheckUser['error']) {
  1593. DB::rollBack();
  1594. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_GETBY000:'.$arrResponseCheckUser['msg'], [], 401);
  1595. }
  1596. try {
  1597. $idWarehouse = $this->encController->decrypt($idWarehouse);
  1598. } catch (\Throwable $th) {
  1599. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBY000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  1600. }
  1601. try {
  1602. $idArea = $this->encController->decrypt($idArea);
  1603. } catch (\Throwable $th) {
  1604. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBY001: Ocurrió un error al desencriptar el ID del área', $th->getMessage(), 406);
  1605. }
  1606. try {
  1607. $idLevel = $this->encController->decrypt($idLevel);
  1608. } catch (\Throwable $th) {
  1609. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBY002: Ocurrió un error al desencriptar el ID del nivel', $th->getMessage(), 406);
  1610. }
  1611. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  1612. if ($arrResponseCheckUser['error']) {
  1613. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBY003:'.$arrResponseCheckUser['msg'], [], 401);
  1614. }
  1615. try {
  1616. $getZone = DB::table('S002V01TZONA')
  1617. ->where('ZONA_COAL', '=', $idWarehouse)
  1618. ->where('ZONA_COAR', '=', $idArea)
  1619. ->where('ZONA_CONI', '=', $idLevel)
  1620. ->where('ZONA_NULI', '=', $line)
  1621. ->get([
  1622. 'ZONA_COZO AS CODIGO_ZONA',
  1623. 'ZONA_NOZO AS NOMBRE_ZONA',
  1624. 'ZONA_COME AS COMENTARIOS',
  1625. 'ZONA_ESTA AS ESTADO',
  1626. 'ZONA_USRE AS USUARIO_REGISTRA',
  1627. 'ZONA_FERE AS FECHA_REGISTRA',
  1628. 'ZONA_USMO AS USUARIO_MODIFICA',
  1629. 'ZONA_FEMO AS FECHA_MODIFICA',
  1630. ]);
  1631. $arrZone = json_decode(json_encode($getZone), true);
  1632. } catch (\Throwable $th) {
  1633. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBY004: Ocurrió un error al obtener los registros del área.', $th->getMessage(), 406);
  1634. }
  1635. foreach ($arrZone as $key => $value) {
  1636. try {
  1637. $countStock = DB::table('S002V01TUBAR')
  1638. ->where('UBAR_NULI', '=', $line)
  1639. ->where('UBAR_ESTA', '=', 'Activo')
  1640. ->where('UBAR_COAL', '=', $idWarehouse)
  1641. ->where('UBAR_COAR', '=', $idArea)
  1642. ->where('UBAR_CONI', '=', $idLevel)
  1643. ->where('UBAR_COZO', '=', $value['CODIGO_ZONA'])
  1644. ->count();
  1645. } catch (\Throwable $th) {
  1646. DB::rollBack();
  1647. return $this->responseController->makeResponse(
  1648. true,
  1649. "ERR_WAREHOUSE_GET003: Ocurrió un error al obtener los almacenes.",
  1650. $th->getMessage(),
  1651. 500
  1652. );
  1653. }
  1654. $value['CANTIDAD_STOCK'] = $countStock;
  1655. $arrZone[$key] = $value;
  1656. }
  1657. $responseCheckLatestUpdate = $this->resourcesController->checkLatestUpdate($arrZone, $line);
  1658. if ($responseCheckLatestUpdate['error']) {
  1659. return $this->responseController->makeResponse(true, $responseCheckLatestUpdate['msg'], [], 500);
  1660. }
  1661. $arrZone = $responseCheckLatestUpdate['response'];
  1662. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrZone);
  1663. }
  1664. public function getZoneById($idZone, $user, $line) {
  1665. try {
  1666. $idZone = $this->encController->decrypt($idZone);
  1667. } catch (\Throwable $th) {
  1668. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBY000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  1669. }
  1670. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  1671. if ($arrResponseCheckUser['error']) {
  1672. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBY001:'.$arrResponseCheckUser['msg'], [], 401);
  1673. }
  1674. try {
  1675. $getZone = (array) DB::table('S002V01TZONA')
  1676. ->where('ZONA_COZO', '=', $idZone)
  1677. ->where('ZONA_NULI', '=', $line)
  1678. ->first([
  1679. 'ZONA_NOZO AS NOMBRE_ZONA',
  1680. 'ZONA_COME AS COMENTARIOS',
  1681. 'ZONA_ESTA AS ESTADO',
  1682. 'ZONA_USRE AS USUARIO_REGISTRA',
  1683. 'ZONA_FERE AS FECHA_REGISTRA',
  1684. 'ZONA_USMO AS USUARIO_MODIFICA',
  1685. 'ZONA_FEMO AS FECHA_MODIFICA',
  1686. ]);
  1687. $arrZone = json_decode(json_encode($getZone), true);
  1688. } catch (\Throwable $th) {
  1689. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBY002: Ocurrió un error al obtener los registros del área.', $th->getMessage(), 406);
  1690. }
  1691. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrZone);
  1692. }
  1693. public function getZoneByLevelAreaWarehouseActive($idWarehouse, $idArea, $idLevel, $user, $line) {
  1694. try {
  1695. $idWarehouse = $this->encController->decrypt($idWarehouse);
  1696. } catch (\Throwable $th) {
  1697. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  1698. }
  1699. try {
  1700. $idArea = $this->encController->decrypt($idArea);
  1701. } catch (\Throwable $th) {
  1702. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE001: Ocurrió un error al desencriptar el ID del área', $th->getMessage(), 406);
  1703. }
  1704. try {
  1705. $idLevel = $this->encController->decrypt($idLevel);
  1706. } catch (\Throwable $th) {
  1707. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE002: Ocurrió un error al desencriptar el ID del nivel', $th->getMessage(), 406);
  1708. }
  1709. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  1710. if ($arrResponseCheckUser['error']) {
  1711. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE003:'.$arrResponseCheckUser['msg'], [], 401);
  1712. }
  1713. try {
  1714. $validateExists = DB::table('S002V01TALMA')
  1715. ->where('ALMA_NULI', '=', $line)
  1716. ->where('ALMA_COAL', '=', $idWarehouse)
  1717. ->exists();
  1718. } catch (\Throwable $th) {
  1719. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE004: Ocurrió un error al obtener el almacen.', $th->getMessage(), 406);
  1720. }
  1721. if (!$validateExists) {
  1722. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE005: El almacen no existe.', [], 401);
  1723. }
  1724. try {
  1725. $validateExists = DB::table('S002V01TAREA')
  1726. ->where('AREA_NULI', '=', $line)
  1727. ->where('AREA_COAL', '=', $idWarehouse)
  1728. ->where('AREA_COAR', '=', $idArea)
  1729. ->exists();
  1730. } catch (\Throwable $th) {
  1731. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE006: Ocurrió un error al obtener el área.', $th->getMessage(), 406);
  1732. }
  1733. if (!$validateExists) {
  1734. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE007: El área no existe.', [], 401);
  1735. }
  1736. try {
  1737. $validateExists = DB::table('S002V01TNIVE')
  1738. ->where('NIVE_NULI', '=', $line)
  1739. ->where('NIVE_COAL', '=', $idWarehouse)
  1740. ->where('NIVE_COAR', '=', $idArea)
  1741. ->where('NIVE_CONI', '=', $idLevel)
  1742. ->exists();
  1743. } catch (\Throwable $th) {
  1744. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE008: Ocurrió un error al obtener el nivel.', $th->getMessage(), 406);
  1745. }
  1746. if (!$validateExists) {
  1747. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE009: El nivel no existe.', [], 401);
  1748. }
  1749. try {
  1750. $getZone = DB::table('S002V01TZONA')
  1751. ->where('ZONA_COAL', '=', $idWarehouse)
  1752. ->where('ZONA_COAR', '=', $idArea)
  1753. ->where('ZONA_CONI', '=', $idLevel)
  1754. ->where('ZONA_NULI', '=', $line)
  1755. ->where('ZONA_ESTA', '=', 'Activo')
  1756. ->get([
  1757. 'ZONA_COZO AS CODIGO_ZONA',
  1758. 'ZONA_NOZO AS NOMBRE_ZONA',
  1759. 'ZONA_COME AS COMENTARIOS',
  1760. ]);
  1761. $arrZone = json_decode(json_encode($getZone), true);
  1762. } catch (\Throwable $th) {
  1763. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE010: Ocurrió un error al obtener los registros del área.', $th->getMessage(), 406);
  1764. }
  1765. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrZone);
  1766. }
  1767. public function registerZone(Request $request) {
  1768. $validator = Validator::make($request->all(), [
  1769. 'CODIGO_ALMACEN' => 'required|string',
  1770. 'CODIGO_AREA' => 'required|string',
  1771. 'CODIGO_NIVEL' => 'required|string',
  1772. 'CODIGO_ZONA' => 'required|string',
  1773. 'NOMBRE_ZONA' => 'required|string',
  1774. 'COMENTARIOS' => 'nullable|string',
  1775. 'USUARIO' => 'required|string',
  1776. 'NUMERO_LINEA' => 'required|integer',
  1777. ]);
  1778. if ($validator->fails()) {
  1779. return $this->responseController->makeResponse(
  1780. true,
  1781. "ERR_ZONE_REG000: Se encontraron uno o más errores.",
  1782. $this->responseController->makeErrors($validator->errors()->messages()),
  1783. 401
  1784. );
  1785. }
  1786. DB::beginTransaction();
  1787. $requestData = $request->all();
  1788. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  1789. if ($arrResponseCheckUser['error']) {
  1790. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG001:'.$arrResponseCheckUser['msg'], [], 401);
  1791. }
  1792. $user = $arrResponseCheckUser['response'];
  1793. try {
  1794. $validateExistWarehouse = DB::table('S002V01TALMA')
  1795. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  1796. ->where('ALMA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1797. ->where('ALMA_ESTA', '=', 'Activo')
  1798. ->exists();
  1799. } catch (\Throwable $th) {
  1800. DB::rollBack();
  1801. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG002: Ocurrió un error al verificar el almacen.', $th->getMessage(), 500);
  1802. }
  1803. if (!$validateExistWarehouse) {
  1804. DB::rollBack();
  1805. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG003: El almacen no existe.', [], 406);
  1806. }
  1807. try {
  1808. $validateExistArea = DB::table('S002V01TAREA')
  1809. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  1810. ->where('AREA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1811. ->where('AREA_COAR', '=', $requestData['CODIGO_AREA'])
  1812. ->where('AREA_ESTA', '=', 'Activo')
  1813. ->exists();
  1814. } catch (\Throwable $th) {
  1815. DB::rollBack();
  1816. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG004: Ocurrió un error al verificar el área.', $th->getMessage(), 500);
  1817. }
  1818. if (!$validateExistArea) {
  1819. DB::rollBack();
  1820. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG005: El área no existe.', [], 406);
  1821. }
  1822. try {
  1823. $validateExistNivel = DB::table('S002V01TNIVE')
  1824. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  1825. ->where('NIVE_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1826. ->where('NIVE_COAR', '=', $requestData['CODIGO_AREA'])
  1827. ->where('NIVE_CONI', '=', $requestData['CODIGO_NIVEL'])
  1828. ->where('NIVE_ESTA', '=', 'Activo')
  1829. ->exists();
  1830. } catch (\Throwable $th) {
  1831. DB::rollBack();
  1832. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG006: Ocurrió un error al verificar el nivel.', $th->getMessage(), 500);
  1833. }
  1834. if (!$validateExistNivel) {
  1835. DB::rollBack();
  1836. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG007: El nivel no existe.', [], 406);
  1837. }
  1838. try {
  1839. $validateExistZone = DB::table('S002V01TZONA')
  1840. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  1841. ->where('ZONA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1842. ->where('ZONA_COAR', '=', $requestData['CODIGO_AREA'])
  1843. ->where('ZONA_CONI', '=', $requestData['CODIGO_NIVEL'])
  1844. ->where('ZONA_COZO', '=', $requestData['CODIGO_ZONA'])
  1845. ->exists();
  1846. } catch (\Throwable $th) {
  1847. DB::rollBack();
  1848. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG008: Ocurrió un error al verificar el código de la zona.', $th->getMessage(), 406);
  1849. }
  1850. if ($validateExistZone) {
  1851. DB::rollBack();
  1852. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG009: El código de la zona ya existe.', [], 406);
  1853. }
  1854. $now = $this->functionsController->now();
  1855. $currentDate = $now->toDateTimeString();
  1856. try {
  1857. $validateInsert = DB::table('S002V01TZONA')->insert([
  1858. 'ZONA_NULI' => $requestData['NUMERO_LINEA'],
  1859. 'ZONA_COAL' => $requestData['CODIGO_ALMACEN'],
  1860. 'ZONA_COAR' => $requestData['CODIGO_AREA'],
  1861. 'ZONA_CONI' => $requestData['CODIGO_NIVEL'],
  1862. 'ZONA_COZO' => $requestData['CODIGO_ZONA'],
  1863. 'ZONA_NOZO' => $requestData['NOMBRE_ZONA'],
  1864. 'ZONA_COME' => $requestData['COMENTARIOS'],
  1865. 'ZONA_USRE' => $user,
  1866. 'ZONA_FERE' => $currentDate,
  1867. 'ZONA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  1868. ]);
  1869. } catch (\Throwable $th) {
  1870. DB::rollBack();
  1871. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG010: Ocurrió un error al ingresar la información a la base de datos.', $th->getMessage(), 500);
  1872. }
  1873. if (!$validateInsert) {
  1874. DB::rollBack();
  1875. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG011: No se pudo guardar la información en la base de datos.', [], 406);
  1876. }
  1877. DB::commit();
  1878. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  1879. }
  1880. public function updateZone(Request $request, $idZone) {
  1881. $validator = Validator::make($request->all(), [
  1882. 'CODIGO_ALMACEN' => 'required|string',
  1883. 'CODIGO_AREA' => 'required|string',
  1884. 'CODIGO_NIVEL' => 'required|string',
  1885. 'NOMBRE_ZONA' => 'required|string',
  1886. 'COMENTARIOS' => 'nullable|string',
  1887. 'USUARIO' => 'required|string',
  1888. 'NUMERO_LINEA' => 'required|integer',
  1889. ]);
  1890. if ($validator->fails()) {
  1891. return $this->responseController->makeResponse(
  1892. true,
  1893. "ERR_ZONE_UPD000: Se encontraron uno o más errores.",
  1894. $this->responseController->makeErrors($validator->errors()->messages()),
  1895. 401
  1896. );
  1897. }
  1898. DB::beginTransaction();
  1899. $requestData = $request->all();
  1900. try {
  1901. $idZone = $this->encController->decrypt($idZone);
  1902. } catch (\Throwable $th) {
  1903. DB::rollBack();
  1904. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD001: Ocurrió un error al desencriptar la zona.', $th->getMessage(), 500);
  1905. }
  1906. try {
  1907. $validateExistLevel = DB::table('S002V01TZONA')
  1908. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  1909. ->where('ZONA_COZO', '=', $idZone)
  1910. ->where('ZONA_ESTA', '=', 'Activo')
  1911. ->exists();
  1912. } catch (\Throwable $th) {
  1913. DB::rollBack();
  1914. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD002: Ocurrió un error al verificar el código de la zona.', $th->getMessage(), 500);
  1915. }
  1916. if (!$validateExistLevel) {
  1917. DB::rollBack();
  1918. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD003: El área no existe.', [], 406);
  1919. }
  1920. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  1921. if ($arrResponseCheckUser['error']) {
  1922. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD004:'.$arrResponseCheckUser['msg'], [], 401);
  1923. }
  1924. $user = $arrResponseCheckUser['response'];
  1925. try {
  1926. $validateExistWarehouse = DB::table('S002V01TALMA')
  1927. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  1928. ->where('ALMA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1929. ->where('ALMA_ESTA', '=', 'Activo')
  1930. ->exists();
  1931. } catch (\Throwable $th) {
  1932. DB::rollBack();
  1933. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD005: Ocurrió un error al verificar el almacen.', $th->getMessage(), 500);
  1934. }
  1935. if (!$validateExistWarehouse) {
  1936. DB::rollBack();
  1937. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD006: El almacen no existe.', [], 406);
  1938. }
  1939. try {
  1940. $validateExistArea = DB::table('S002V01TAREA')
  1941. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  1942. ->where('AREA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1943. ->where('AREA_COAR', '=', $requestData['CODIGO_AREA'])
  1944. ->where('AREA_ESTA', '=', 'Activo')
  1945. ->exists();
  1946. } catch (\Throwable $th) {
  1947. DB::rollBack();
  1948. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD007: Ocurrió un error al verificar el área.', $th->getMessage(), 500);
  1949. }
  1950. if (!$validateExistArea) {
  1951. DB::rollBack();
  1952. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD008: El área no existe.', [], 406);
  1953. }
  1954. try {
  1955. $validateExistNivel = DB::table('S002V01TNIVE')
  1956. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  1957. ->where('NIVE_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1958. ->where('NIVE_COAR', '=', $requestData['CODIGO_AREA'])
  1959. ->where('NIVE_CONI', '=', $requestData['CODIGO_NIVEL'])
  1960. ->where('NIVE_ESTA', '=', 'Activo')
  1961. ->exists();
  1962. } catch (\Throwable $th) {
  1963. DB::rollBack();
  1964. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD009: Ocurrió un error al verificar el nivel.', $th->getMessage(), 500);
  1965. }
  1966. if (!$validateExistNivel) {
  1967. DB::rollBack();
  1968. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD010: El nivel no existe.', [], 406);
  1969. }
  1970. try {
  1971. $validateExistZone = DB::table('S002V01TZONA')
  1972. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  1973. ->where('ZONA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1974. ->where('ZONA_COAR', '=', $requestData['CODIGO_AREA'])
  1975. ->where('ZONA_CONI', '=', $requestData['CODIGO_NIVEL'])
  1976. ->where('ZONA_COZO', '=', $requestData['CODIGO_ZONA'])
  1977. ->exists();
  1978. } catch (\Throwable $th) {
  1979. DB::rollBack();
  1980. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD011: Ocurrió un error al verificar el código de la zona.', $th->getMessage(), 406);
  1981. }
  1982. if ($validateExistZone) {
  1983. DB::rollBack();
  1984. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD012: El código de la zona ya existe.', [], 406);
  1985. }
  1986. $now = $this->functionsController->now();
  1987. $currentDate = $now->toDateTimeString();
  1988. try {
  1989. $validateUpdate = DB::table('S002V01TZONA')
  1990. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  1991. ->where('ZONA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1992. ->where('ZONA_COAR', '=', $requestData['CODIGO_AREA'])
  1993. ->where('ZONA_CONI', '=', $requestData['CODIGO_NIVEL'])
  1994. ->where('ZONA_COZO', '=', $idZone)
  1995. ->update([
  1996. 'ZONA_NOZO' => $requestData['NOMBRE_ZONA'],
  1997. 'ZONA_COME' => $requestData['COMENTARIOS'],
  1998. 'ZONA_USMO' => $user,
  1999. 'ZONA_FEMO' => $currentDate,
  2000. 'ZONA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2001. ]);
  2002. } catch (\Throwable $th) {
  2003. DB::rollBack();
  2004. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD013: Ocurrió un error al ingresar la información a la base de datos.', $th->getMessage(), 500);
  2005. }
  2006. if (!$validateUpdate) {
  2007. DB::rollBack();
  2008. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD014: No se pudo guardar la información en la base de datos.', [], 406);
  2009. }
  2010. DB::commit();
  2011. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  2012. }
  2013. public function deleteZone(Request $request, $idZone) {
  2014. $validator = Validator::make($request->all(), [
  2015. 'USUARIO' => 'required|string',
  2016. 'NUMERO_LINEA' => 'required|integer',
  2017. ]);
  2018. if ($validator->fails()) {
  2019. return $this->responseController->makeResponse(
  2020. true,
  2021. "ERR_ZONE_DEL000: Se encontraron uno o más errores.",
  2022. $this->responseController->makeErrors($validator->errors()->messages()),
  2023. 401
  2024. );
  2025. }
  2026. DB::beginTransaction();
  2027. $requestData = $request->all();
  2028. try {
  2029. $idZone = $this->encController->decrypt($idZone);
  2030. } catch (\Throwable $th) {
  2031. DB::rollBack();
  2032. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL001: Ocurrió un error al desencriptar la zona.', $th->getMessage(), 500);
  2033. }
  2034. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  2035. if ($arrResponseCheckUser['error']) {
  2036. DB::rollBack();
  2037. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL002:'.$arrResponseCheckUser['msg'], [], 401);
  2038. }
  2039. $user = $arrResponseCheckUser['response'];
  2040. try {
  2041. $arrZone = (array) DB::table('S002V01TZONA')
  2042. ->where('ZONA_COZO', '=', $idZone)
  2043. ->where('ZONA_ESTA', '=', 'Activo')
  2044. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  2045. ->first([
  2046. 'ZONA_COAL',
  2047. 'ZONA_COAR',
  2048. 'ZONA_CONI',
  2049. ]);
  2050. } catch (\Throwable $th) {
  2051. DB::rollBack();
  2052. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL003: Ocurrió un error al obtener la información de la zona.', $th->getMessage(), 500);
  2053. }
  2054. if (empty($arrZone)) {
  2055. DB::rollBack();
  2056. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL004: La zona no existe.', [], 406);
  2057. }
  2058. try {
  2059. $validateExists = DB::table('S002V01TUBAR')
  2060. ->where('UBAR_COAL', '=', $arrZone['ZONA_COAL'])
  2061. ->where('UBAR_COAR', '=', $arrZone['ZONA_COAR'])
  2062. ->where('UBAR_CONI', '=', $arrZone['ZONA_CONI'])
  2063. ->where('UBAR_COZO', '=', $idZone)
  2064. ->where('UBAR_NULI', '=', $requestData['NUMERO_LINEA'])
  2065. ->where('UBAR_ESTA', '=', 'Activo')
  2066. ->exists();
  2067. } catch (\Throwable $th) {
  2068. DB::rollBack();
  2069. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL005: Ocurrió un error al obtener las ubicaciones de los artículos.', $th->getMessage(), 500);
  2070. }
  2071. if($validateExists) {
  2072. DB::rollBack();
  2073. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL006: No se puede eliminar debido a que existen artículos ubicados en la zona.', [], 406);
  2074. }
  2075. $now = $this->functionsController->now();
  2076. $currentDate = $now->toDateTimeString();
  2077. try {
  2078. $validateUpdate = DB::table('S002V01TZONA')
  2079. ->where('ZONA_COZO', '=', $idZone)
  2080. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  2081. ->update([
  2082. 'ZONA_ESTA' => 'Eliminado',
  2083. 'ZONA_USMO' => $user,
  2084. 'ZONA_FEMO' => $currentDate,
  2085. 'ZONA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2086. ]);
  2087. } catch (\Throwable $th) {
  2088. DB::rollBack();
  2089. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL007: Ocurrió un error al eliminar la zona.', $th->getMessage(), 500);
  2090. }
  2091. if (!$validateUpdate) {
  2092. DB::rollBack();
  2093. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL008: No se pudo eliminar la zona.', [], 406);
  2094. }
  2095. DB::commit();
  2096. return $this->responseController->makeResponse(false, "ÉXITO: Eliminación Exitosa");
  2097. }
  2098. // FUNCIÓN RECEPCIÓN DE ARTÍCULOS
  2099. public function registerToStock(Request $request) {
  2100. $validator = Validator::make($request->all(), [
  2101. 'WAREHOUSE' => 'required|string',
  2102. 'AREA' => 'required|string',
  2103. 'LEVEL' => 'required|string',
  2104. 'ZONE' => 'required|string',
  2105. 'ORDER' => 'required|string',
  2106. 'PRE_CODIFICATE' => 'required|array',
  2107. 'ID_INFORMATION' => 'required|integer',
  2108. 'FAMILIA' => 'required|string',
  2109. 'SUBFAMILIA' => 'required|string',
  2110. 'MODELO' => 'required|string',
  2111. 'CODIGO_MODELO' => 'required|string',
  2112. 'NUMBER_ITEMS' => 'required|integer',
  2113. 'CODIGO_BARRAS' => 'required|string',
  2114. 'FECHA_VENCIMIENTO' => 'nullable|string',
  2115. 'IMAGES' => 'required|json',
  2116. 'PRE_GENERATED_CODE' => 'required|string',
  2117. 'REPARABLE' => 'required|boolean',
  2118. 'CONSUMIBLE' => 'required|boolean',
  2119. 'USUARIO' => 'required|string',
  2120. 'NUMERO_LINEA' => 'required|integer',
  2121. ]);
  2122. if($validator->fails()){
  2123. return $this->responseController->makeResponse(
  2124. true,
  2125. "Se encontraron uno o más errores.",
  2126. $this->responseController->makeErrors(
  2127. $validator->errors()->messages()
  2128. ),
  2129. 401
  2130. );
  2131. }
  2132. DB::beginTransaction();
  2133. $requestData = $request->all();
  2134. // Se verifica la precodificación
  2135. if ( count($requestData['PRE_CODIFICATE']) !== $requestData['NUMBER_ITEMS']) {
  2136. DB::rollBack();
  2137. return $this->responseController->makeResponse(true, 'La cantidad registrada de la precodificación no es igual la cantidad de artículos seleccionados.', [], 500);
  2138. }
  2139. // Se desencripta y se verifica si el usuario existe
  2140. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  2141. if ($arrResponseCheckUser['error']) {
  2142. DB::rollBack();
  2143. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  2144. }
  2145. $user = $arrResponseCheckUser['response'];
  2146. // Se obtiene la información encriptada
  2147. try {
  2148. $idWarehouse = $this->encController->decrypt($requestData['WAREHOUSE']);
  2149. } catch (\Throwable $th) {
  2150. DB::rollBack();
  2151. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el almacen.', $th->getMessage(), 500);
  2152. }
  2153. if ($idWarehouse === false) {
  2154. DB::rollBack();
  2155. return $this->responseController->makeResponse(true, 'La encriptación del almacen no es correcta.', [], 500);
  2156. }
  2157. try {
  2158. $idArea = $this->encController->decrypt($requestData['AREA']);
  2159. } catch (\Throwable $th) {
  2160. DB::rollBack();
  2161. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el área.', $th->getMessage(), 500);
  2162. }
  2163. if ($idArea === false) {
  2164. DB::rollBack();
  2165. return $this->responseController->makeResponse(true, 'La encriptacipon del área no es correcta.', [], 500);
  2166. }
  2167. try {
  2168. $idLevel = $this->encController->decrypt($requestData['LEVEL']);
  2169. } catch (\Throwable $th) {
  2170. DB::rollBack();
  2171. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el nivel.', $th->getMessage(), 500);
  2172. }
  2173. if ($idLevel === false) {
  2174. DB::rollBack();
  2175. return $this->responseController->makeResponse(true, 'La encriptación del nivel no es correcta.', [], 500);
  2176. }
  2177. try {
  2178. $idZone = $this->encController->decrypt($requestData['ZONE']);
  2179. } catch (\Throwable $th) {
  2180. DB::rollBack();
  2181. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la zona.', $th->getMessage(), 500);
  2182. }
  2183. if ($idZone === false) {
  2184. DB::rollBack();
  2185. return $this->responseController->makeResponse(true, 'La encriptación de la zona no es correcta.', [], 500);
  2186. }
  2187. try {
  2188. $idFamily = $this->encController->decrypt($requestData['FAMILIA']);
  2189. } catch (\Throwable $th) {
  2190. DB::rollBack();
  2191. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la familia.', $th->getMessage(), 500);
  2192. }
  2193. if ($idFamily === false) {
  2194. DB::rollBack();
  2195. return $this->responseController->makeResponse(true, 'La encriptación de la familia no es correcta.', [], 500);
  2196. }
  2197. try {
  2198. $idSubfamily = $this->encController->decrypt($requestData['SUBFAMILIA']);
  2199. } catch (\Throwable $th) {
  2200. DB::rollBack();
  2201. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la subfamilia.', $th->getMessage(), 500);
  2202. }
  2203. if ($idSubfamily === false) {
  2204. DB::rollBack();
  2205. return $this->responseController->makeResponse(true, 'La encriptación de la subfamilia no es correcta.', [], 500);
  2206. }
  2207. // Se verifica que la información exista
  2208. try {
  2209. $validateWarehouse = DB::table('S002V01TALMA')
  2210. ->where('ALMA_COAL', '=', $idWarehouse)
  2211. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  2212. ->exists();
  2213. } catch (\Throwable $th) {
  2214. DB::rollBack();
  2215. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si el almacen existe.', $th->getMessage(), 500);
  2216. }
  2217. if ($validateWarehouse === false) {
  2218. DB::rollBack();
  2219. return $this->responseController->makeResponse(true, 'El almacen no existe.', [], 500);
  2220. }
  2221. try {
  2222. $validateArea = DB::table('S002V01TAREA')
  2223. ->where('AREA_COAL', '=', $idWarehouse)
  2224. ->where('AREA_COAR', '=', $idArea)
  2225. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  2226. ->exists();
  2227. } catch (\Throwable $th) {
  2228. DB::rollBack();
  2229. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si el área existe.', $th->getMessage(), 500);
  2230. }
  2231. if ($validateArea === false) {
  2232. DB::rollBack();
  2233. return $this->responseController->makeResponse(true, 'El área no existe.', [], 500);
  2234. }
  2235. try {
  2236. $validateLevel = DB::table('S002V01TNIVE')
  2237. ->where('NIVE_COAL', '=', $idWarehouse)
  2238. ->where('NIVE_COAR', '=', $idArea)
  2239. ->where('NIVE_CONI', '=', $idLevel)
  2240. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  2241. ->exists();
  2242. } catch (\Throwable $th) {
  2243. DB::rollBack();
  2244. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si el nivel existe.', $th->getMessage(), 500);
  2245. }
  2246. if ($validateLevel === false) {
  2247. DB::rollBack();
  2248. return $this->responseController->makeResponse(true, 'El nivel no existe.', [], 500);
  2249. }
  2250. try {
  2251. $validateZone = DB::table('S002V01TZONA')
  2252. ->where('ZONA_COAL', '=', $idWarehouse)
  2253. ->where('ZONA_COAR', '=', $idArea)
  2254. ->where('ZONA_CONI', '=', $idLevel)
  2255. ->where('ZONA_COZO', '=', $idZone)
  2256. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  2257. ->exists();
  2258. } catch (\Throwable $th) {
  2259. DB::rollBack();
  2260. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si la zona existe.', $th->getMessage(), 500);
  2261. }
  2262. if ($validateZone === false) {
  2263. return $this->responseController->makeResponse(true, 'La zona no existe.', [], 500);
  2264. }
  2265. try {
  2266. $validateFamily = DB::table('S002V01TFAMI')
  2267. ->where('FAMI_COFA', '=', $idFamily)
  2268. ->where('FAMI_NULI', '=', $requestData['NUMERO_LINEA'])
  2269. ->exists();
  2270. } catch (\Throwable $th) {
  2271. DB::rollBack();
  2272. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si la familia existe.', $th->getMessage(), 500);
  2273. }
  2274. if ($validateFamily === false) {
  2275. DB::rollBack();
  2276. return $this->responseController->makeResponse(true, 'La familia no existe.', [], 500);
  2277. }
  2278. try {
  2279. $validateSubfamily = DB::table('S002V01TSUBF')
  2280. ->where('SUBF_COFA', '=', $idFamily)
  2281. ->where('SUBF_COSU', '=', $idSubfamily)
  2282. ->where('SUBF_NULI', '=', $requestData['NUMERO_LINEA'])
  2283. ->exists();
  2284. } catch (\Throwable $th) {
  2285. DB::rollBack();
  2286. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si la subfamilia existe.', $th->getMessage(), 500);
  2287. }
  2288. if ($validateSubfamily === false) {
  2289. DB::rollBack();
  2290. return $this->responseController->makeResponse(true, 'La subfamilia no existe.', [], 500);
  2291. }
  2292. try {
  2293. $orden = (array) DB::table('S002V01TORCO')
  2294. ->where('ORCO_NUOR', '=', $requestData['ORDER'])
  2295. ->where('ORCO_NULI', '=', $requestData['NUMERO_LINEA'])
  2296. ->where('ORCO_ESTA', '=', 'Recibido')
  2297. ->first([
  2298. 'ORCO_IDLI AS ID_LINEA_SOLICITUD',
  2299. 'ORCO_IDDE AS ID_DESCRIPCION',
  2300. ]);
  2301. } catch (\Throwable $th) {
  2302. DB::rollBack();
  2303. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información de la orden de compra.', $th->getMessage(), 500);
  2304. }
  2305. if (is_null($orden)) {
  2306. DB::rollBack();
  2307. return $this->responseController->makeResponse(true, 'La orden de compra no existe.', [], 500);
  2308. }
  2309. $now = $this->functionsController->now();
  2310. $currentDate = $now->toDateTimeString();
  2311. try {
  2312. $info = DB::table('S002V01TARSE')
  2313. ->where('ARSE_IDIN', '=', $requestData['ID_INFORMATION'])
  2314. ->where('ARSE_ESTA', '=', 'Activo')
  2315. ->where('ARSE_IDLI', '=', $orden['ID_LINEA_SOLICITUD'])
  2316. ->where('ARSE_NULI', '=', $requestData['NUMERO_LINEA'])
  2317. ->where('ARTI_NULI', '=', $requestData['NUMERO_LINEA'])
  2318. ->where('DEAR_NULI', '=', $requestData['NUMERO_LINEA'])
  2319. ->where('UNID_NULI', '=', $requestData['NUMERO_LINEA'])
  2320. ->where('INAR_NULI', '=', $requestData['NUMERO_LINEA'])
  2321. ->where('ARSE_ESTA', '=', 'Activo')
  2322. ->where('ARTI_ESTA', '=', 'Activo')
  2323. ->where('DEAR_ESTA', '=', 'Activo')
  2324. ->where('UNID_ESTA', '=', 'Activo')
  2325. ->where('INAR_ESTA', '=', 'Activo')
  2326. ->where('INAR_CODI', '=', $requestData['CODIGO_MODELO'])
  2327. ->where('INAR_MODE', '=', $requestData['MODELO'])
  2328. ->join('S002V01TARTI', 'ARTI_IDAR', '=', 'ARSE_IDAR')
  2329. ->join('S002V01TINAR', 'INAR_IDIN', '=', 'ARSE_IDIN')
  2330. ->join('S002V01TDEAR', 'DEAR_IDDE', '=', 'INAR_IDDE')
  2331. ->join('S002V01TUNID', 'UNID_IDUN', '=', 'DEAR_IDUN')
  2332. ->limit($requestData['NUMBER_ITEMS'])
  2333. ->get([
  2334. 'ARSE_IDAS AS NUMERO_SELECCIONADO',
  2335. 'ARTI_IDAR AS ID_ARTICULO',
  2336. 'ARTI_COFA AS CODIGO_FAMILIA',
  2337. 'ARTI_COSU AS CODIGO_SUBFAMILIA',
  2338. 'ARTI_CODI AS CODIGO_ARTICULO',
  2339. 'ARTI_NOMB AS NOMBRE_ARTICULO',
  2340. 'DEAR_IDDE AS ID_DESCRIPCION',
  2341. 'DEAR_IMAG AS IMAGENES',
  2342. 'DEAR_DESC AS DESCRIPCION',
  2343. 'DEAR_CARA AS CARACTERISTICAS',
  2344. 'DEAR_COWE AS COMPRA_WEB',
  2345. 'DEAR_NUPR AS NUMERO_PROVEEDOR',
  2346. 'UNID_IDUN AS ID_UNIDAD',
  2347. 'UNID_NOMB AS NOMBRE_UNIDAD',
  2348. 'UNID_ACRO AS ACRONIMO_UNIDAD',
  2349. 'INAR_IDIN AS ID_INFORMACION',
  2350. 'INAR_CODI AS CODIGO_INFORMACION',
  2351. 'INAR_MODE AS MODELO_INFORMACION',
  2352. 'INAR_COMO AS CODIGO_MONEDA',
  2353. 'INAR_PREC AS PRECIO',
  2354. 'INAR_MOMI AS MONTO_MINIMO',
  2355. 'INAR_CARA AS CARACTERISTICAS',
  2356. ]);
  2357. $info = json_decode(json_encode($info ), true);
  2358. } catch (\Throwable $th) {
  2359. DB::rollBack();
  2360. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información de los productos.', $th->getMessage(), 500);
  2361. }
  2362. if ( is_null($info) || empty($info) ) {
  2363. DB::rollBack();
  2364. return $this->responseController->makeResponse(true, 'No existen artículos asociados a la orden de compra.', [], 500);
  2365. }
  2366. // Se verifica que existan los datos suficientes para hacer el traslado.
  2367. if ( count($info) !== $requestData['NUMBER_ITEMS']) {
  2368. DB::rollBack();
  2369. return $this->responseController->makeResponse(true, 'La cantidad de artículos en la orden de compra no corresponde a la cantidad de artículos seleccionados a Stock.', [], 500);
  2370. }
  2371. // Se verifica los códigos de barras
  2372. $uniqueBarcode = false;
  2373. $arrBarcode = explode(',', $requestData['CODIGO_BARRAS']);
  2374. if ( count($arrBarcode) === 0) {
  2375. DB::rollBack();
  2376. return $this->responseController->makeResponse(true, 'No hay códigos de barras registrados.', [], 500);
  2377. } else if (count($arrBarcode) === $requestData['NUMBER_ITEMS']) {
  2378. $uniqueBarcode = false;
  2379. } else if (array_key_exists(0, $arrBarcode) && count($arrBarcode) === 1 && $arrBarcode[0] !== '' ) {
  2380. $uniqueBarcode = true;
  2381. } else {
  2382. DB::rollBack();
  2383. return $this->responseController->makeResponse(true, 'Ocurrió un error con los códigos de barras registrados.', [], 500);
  2384. }
  2385. // Se verifica que los códigos de barras del formulario no sean repetidos
  2386. $temp_array = array_unique($arrBarcode);
  2387. $duplicates = sizeof($temp_array) != sizeof($arrBarcode);
  2388. if ($duplicates) {
  2389. return $this->responseController->makeResponse(true, 'Los códigos de barras no se pueden repetir.', [], 400);
  2390. }
  2391. // Se verifican que los códigos de barras no se repitan
  2392. foreach ($arrBarcode as $key => $barcode) {
  2393. try {
  2394. $validateBarcode = DB::table('S002V01TSTAR')
  2395. ->where('STAR_COBA', '=', $barcode)
  2396. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  2397. ->exists();
  2398. } catch (\Throwable $th) {
  2399. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar los códigos de barras.', $th->getMessage(), 500);
  2400. }
  2401. if ($validateBarcode === true) {
  2402. return $this->responseController->makeResponse(true, 'El código '.$barcode.' ya se encuentra registrado', [], 500);
  2403. }
  2404. }
  2405. // Se verifica la fecha de vencimientos
  2406. $arrDueDate = array();
  2407. $uniqueDueDate = false;
  2408. if ($requestData['FECHA_VENCIMIENTO'] !== '' && !is_null($requestData['FECHA_VENCIMIENTO'])) {
  2409. $arrDueDate = explode(',', $requestData['FECHA_VENCIMIENTO']);
  2410. if ( count($arrDueDate) === 0) {
  2411. DB::rollBack();
  2412. return $this->responseController->makeResponse(true, 'No hay fechas de vencimientos registradas.', [], 500);
  2413. } else if (count($arrDueDate) === $requestData['NUMBER_ITEMS']) {
  2414. $uniqueDueDate = false;
  2415. } else if (array_key_exists(0, $arrDueDate) && count($arrDueDate) === 1 && $arrDueDate[0] !== '' ) {
  2416. $uniqueDueDate = true;
  2417. } else {
  2418. DB::rollBack();
  2419. return $this->responseController->makeResponse(true, 'Ocurrió un error con las fechas de vencimientos registrados.', [], 500);
  2420. }
  2421. }
  2422. // Se obtiene la fecha actual
  2423. $now = $this->functionsController->now();
  2424. $currentDate = $now->toDateTimeString();
  2425. $idInfoStock = null;
  2426. try {
  2427. $infoStock = (array) DB::table('S002V01TINST')
  2428. ->where('INST_COFA', '=', $idFamily)
  2429. ->where('INST_COSU', '=', $idSubfamily)
  2430. ->where('INST_ARTI', '=', $info[0]['ID_ARTICULO'])
  2431. ->where('INST_MODE', '=', $requestData['MODELO'])
  2432. ->where('INST_COMO', '=', $requestData['CODIGO_MODELO'])
  2433. ->where('INST_NULI', '=', $requestData['NUMERO_LINEA'])
  2434. ->where('INST_ESTA', '=', 'Activo')
  2435. ->first([
  2436. 'INST_IDIS AS ID_INFO_STOCK'
  2437. ]);
  2438. } catch (\Throwable $th) {
  2439. DB::rollBack();
  2440. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar la información del stock.', $th->getMessage(), 500);
  2441. }
  2442. if (empty($infoStock) || is_null($infoStock)) {
  2443. // Se obtiene el arreglo de las imagenes
  2444. $imagesArr = json_decode($requestData['IMAGES'], true);
  2445. // Se verifica que el arreglo contenga imagenes
  2446. if(count($imagesArr) < 1){
  2447. DB::rollBack();
  2448. return $this->responseController->makeResponse(true, 'El arreglo de la galería de imágenes está vacío.', [], 400);
  2449. }
  2450. $imagesGallery = [];
  2451. // Se iteran las imagenes
  2452. foreach($imagesArr as $imageFile){
  2453. // Se verifica que tenga un formato correcto
  2454. if(!array_key_exists('type', $imageFile)){
  2455. DB::rollBack();
  2456. return $this->responseController->makeResponse(true, 'El arreglo de la galería de imágenes tiene un formato inválido.', [], 400);
  2457. // Si la imagen es nueva, entonces...
  2458. }else if($imageFile['type'] == 'Nuevo'){
  2459. // Se obtiene el ID de la imagen
  2460. $tempFileID = $this->encController->decrypt($imageFile['id']);
  2461. // Se obtiene la información registrada en la tabla de archivos temporales
  2462. $tempFile = DB::table('S002V01TARTE')->where([
  2463. ['ARTE_IDAR', '=', $tempFileID],
  2464. ['ARTE_NULI', '=', $requestData['NUMERO_LINEA']]
  2465. ])->first();
  2466. // La imagen es colocada en su posición final
  2467. $finalFile = $this->documentManagementController->moveFinalFile($requestData['NUMERO_LINEA'], 'GEEQ', 'FO', $tempFile, $user);
  2468. // Si ocurrió un error, entonces se manda el error
  2469. if(!$finalFile[0]){
  2470. return $this->responseController->makeResponse(true, $finalFile[1], [], 400);
  2471. }
  2472. // La información es guardada en un arreglo
  2473. $imagesGallery[] = $finalFile[1];
  2474. }else{
  2475. // Se desencripta el id de la imagen
  2476. $fileID = $this->encController->decrypt($imageFile['id']);
  2477. // Es guardada en un arreglo
  2478. $imagesGallery[] = $fileID;
  2479. }
  2480. }
  2481. // El arreglo de las imagenes es pasada en formato string
  2482. $imagesGalleryStr = json_encode($imagesGallery);
  2483. try {
  2484. $idInsert = DB::table('S002V01TINST')->insertGetId([
  2485. 'INST_NULI' => $requestData['NUMERO_LINEA'],
  2486. 'INST_COFA' => $idFamily,
  2487. 'INST_COSU' => $idSubfamily,
  2488. 'INST_ARTI' => $info[0]['ID_ARTICULO'],
  2489. 'INST_MODE' => $requestData['MODELO'],
  2490. 'INST_COMO' => $requestData['CODIGO_MODELO'],
  2491. 'INST_IMAG' => $imagesGalleryStr,
  2492. 'INST_USRE' => $user,
  2493. 'INST_FERE' => $currentDate,
  2494. 'INST_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2495. ]);
  2496. } catch (\Throwable $th) {
  2497. DB::rollBack();
  2498. return $this->responseController->makeResponse(true, 'Ocurrió un error al insertar la información del stock.', $th->getMessage(), 500);
  2499. }
  2500. if ($idInsert === false || is_null($idInsert)) {
  2501. DB::rollBack();
  2502. return $this->responseController->makeResponse(true, 'No se pudo insertar la información del stock.', [], 500);
  2503. }
  2504. $idInfoStock = $idInsert;
  2505. } else {
  2506. $idInfoStock = $infoStock['ID_INFO_STOCK'];
  2507. }
  2508. if (is_null($idInfoStock)) {
  2509. DB::rollBack();
  2510. return $this->responseController->makeResponse(true, 'No se pudo obtener el ID de la información del Stock.', [], 500);
  2511. }
  2512. for ($i=0; $i < $requestData['NUMBER_ITEMS']; $i++) {
  2513. $dueDate = null;
  2514. if (count($arrDueDate) > 0 && $uniqueDueDate === true) {
  2515. $dueDate = $arrDueDate[0];
  2516. } else if (count($arrDueDate) > 0 && $uniqueDueDate === false) {
  2517. $dueDate = $arrDueDate[$i];
  2518. }
  2519. if (!is_null($dueDate) && str_contains($dueDate, 'T')) {
  2520. $dueDate = explode('T', $dueDate)[0];
  2521. }
  2522. try {
  2523. $preCodificate = $this->encController->decrypt($requestData['PRE_CODIFICATE'][$i]);
  2524. } catch (\Throwable $th) {
  2525. DB::rollBack();
  2526. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el identificador de la pre-codificación.', $th->getMessage(), 500);
  2527. }
  2528. if ($preCodificate === false) {
  2529. DB::rollBack();
  2530. return $this->responseController->makeResponse(true, 'No se pudo desencriptar el identificador de la pre-codificación.', [], 500);
  2531. }
  2532. try {
  2533. $idInsertStAr = DB::table('S002V01TSTAR')->insertGetId([
  2534. 'STAR_NULI' => $requestData['NUMERO_LINEA'],
  2535. 'STAR_IDIS' => $idInfoStock,
  2536. 'STAR_COBA' => $uniqueBarcode === true ? $arrBarcode[0] : $arrBarcode[$i],
  2537. 'STAR_FEVE' => $dueDate,
  2538. 'STAR_IDUN' => $info[$i]['ID_UNIDAD'],
  2539. 'STAR_NUPR' => $info[$i]['NUMERO_PROVEEDOR'],
  2540. 'STAR_TIAD' => 'Por pedido',
  2541. 'STAR_IDPC' => $preCodificate,
  2542. 'STAR_CONS' => $requestData['CONSUMIBLE'] ? 'Si' : 'No',
  2543. 'STAR_REPA' => $requestData['REPARABLE'] ? 'Si' : 'No',
  2544. 'STAR_PRAD' => $info[$i]['PRECIO'],
  2545. 'STAR_COMO' => $info[$i]['CODIGO_MONEDA'],
  2546. 'STAR_ESTA' => 'Pendiente',
  2547. 'STAR_USRE' => $user,
  2548. 'STAR_FERE' => $currentDate,
  2549. 'STAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2550. ]);
  2551. } catch (\Throwable $th) {
  2552. DB::rollBack();
  2553. return $this->responseController->makeResponse(true, 'Ocurrió un error al registrar el equipamiento.', $th->getMessage(), 500);
  2554. }
  2555. if ($idInsertStAr === false || is_null($idInsertStAr)) {
  2556. DB::rollBack();
  2557. return $this->responseController->makeResponse(true, 'No se pudo registrar el equipamiento.', [], 500);
  2558. }
  2559. try {
  2560. $preCodeCodificate = $this->encController->decrypt($requestData['PRE_GENERATED_CODE']);
  2561. } catch (\Throwable $th) {
  2562. DB::rollBack();
  2563. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el pre-código generado', $th->getMessage(), 500);
  2564. }
  2565. if ( $preCodeCodificate === false ) {
  2566. DB::rollBack();
  2567. return $this->responseController->makeResponse(true, 'No se pudo desencriptar el pre-código del equipamientos.', [], 500);
  2568. }
  2569. try {
  2570. $validateInsertUbAr = DB::table('S002V01TUBAR')->insert([
  2571. 'UBAR_NULI' => $requestData['NUMERO_LINEA'],
  2572. 'UBAR_COAL' => $idWarehouse,
  2573. 'UBAR_COAR' => $idArea,
  2574. 'UBAR_CONI' => $idLevel,
  2575. 'UBAR_COZO' => $idZone,
  2576. 'UBAR_IDST' => $idInsertStAr,
  2577. 'UBAR_COUB' => $preCodeCodificate,
  2578. 'UBAR_ESTA' => 'Pendiente',
  2579. 'UBAR_USRE' => $user,
  2580. 'UBAR_FERE' => $currentDate,
  2581. 'UBAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2582. ]);
  2583. } catch (\Throwable $th) {
  2584. DB::rollBack();
  2585. return $this->responseController->makeResponse(true, 'Ocurrió un error al registrar la ubicación del artículo.', $th->getMessage(), 500);
  2586. }
  2587. if ( $validateInsertUbAr === false ) {
  2588. DB::rollBack();
  2589. return $this->responseController->makeResponse(true, 'No se pudo registrar la ubicación del artículo.', [], 500);
  2590. }
  2591. try {
  2592. $validateUpdateArSe = DB::table('S002V01TARSE')
  2593. ->where('ARSE_IDAS', '=', $info[$i]['NUMERO_SELECCIONADO'])
  2594. ->where('ARSE_NULI', '=', $requestData['NUMERO_LINEA'])
  2595. ->where('ARSE_IDLI', '=', $orden['ID_LINEA_SOLICITUD'])
  2596. ->where('ARSE_IDAR', '=', $info[$i]['ID_ARTICULO'])
  2597. ->where('ARSE_NUPR', '=', $info[$i]['NUMERO_PROVEEDOR'])
  2598. ->where('ARSE_IDIN', '=', $requestData['ID_INFORMATION'])
  2599. ->where('ARSE_ESTA', '=', 'Activo')
  2600. ->update([
  2601. 'ARSE_ESTA' => 'Guardado',
  2602. 'ARSE_USMO' => $user,
  2603. 'ARSE_FEMO' => $currentDate,
  2604. 'ARSE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2605. ]);
  2606. } catch (\Throwable $th) {
  2607. DB::rollBack();
  2608. return $this->responseController->makeResponse(true, 'Ocurrió un error al modificar el artículo seleccionado.', $th->getMessage(), 500);
  2609. }
  2610. if ($validateUpdateArSe === false) {
  2611. DB::rollBack();
  2612. return $this->responseController->makeResponse(true, 'No se pudo modificar el artículo seleccionado.', [], 500);
  2613. }
  2614. }
  2615. DB::commit();
  2616. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  2617. }
  2618. // FUNCIÓN INTERCAMBIO DE ARTICULOS ENTRE ALMACENES
  2619. public function getCurrentLocationArtitles($user, $line) {
  2620. try {
  2621. $arrStockArtitle = DB::table('S002V01TSTAR')
  2622. ->where('STAR_NULI', '=', $line)
  2623. ->where('STAR_ESTA', '=', 'Activo')
  2624. ->where('INST_NULI', '=', $line)
  2625. ->where('INST_ESTA', '=', 'Activo')
  2626. ->where('UBAR_ESTA', '=', 'Activo')
  2627. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  2628. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  2629. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  2630. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  2631. ->join('S002V01TALMA', 'ALMA_COAL', '=', 'UBAR_COAL')
  2632. ->join('S002V01TAREA', 'AREA_COAR', '=', 'UBAR_COAR')
  2633. ->join('S002V01TNIVE', 'NIVE_CONI', '=', 'UBAR_CONI')
  2634. ->join('S002V01TZONA', 'ZONA_COZO', '=', 'UBAR_COZO')
  2635. ->orderBy('ID_STOCK', 'DESC')
  2636. ->get([
  2637. 'STAR_IDST AS ID_STOCK',
  2638. 'INST_IDIS AS ID_INFORMACION_STOCK',
  2639. 'FAMI_COFA AS CODIGO_FAMILIA',
  2640. 'FAMI_NOFA AS NOMBRE_FAMILIA',
  2641. 'SUBF_COSU AS CODIGO_SUBFAMILIA',
  2642. 'SUBF_NOSU AS NOMBRE_SUBFAMILIA',
  2643. 'INST_ARTI AS ARTITULO',
  2644. 'INST_MODE AS MODELO',
  2645. 'INST_COMO AS CODIGO_MODELO',
  2646. 'INST_IMAG AS IMAGENES',
  2647. 'UBAR_COUB AS CODIGO',
  2648. DB::raw('CONCAT(ALMA_NOAL, " (", ALMA_COAL, ")") AS NOMBRE_ALMACEN'),
  2649. DB::raw('CONCAT(AREA_NOAR, " (", AREA_COAR, ")") AS NOMBRE_AREA'),
  2650. DB::raw('CONCAT(NIVE_NONI, " (", NIVE_CONI, ")") AS NOMBRE_NIVEL'),
  2651. DB::raw('CONCAT(ZONA_NOZO, " (", ZONA_COZO, ")") AS NOMBRE_ZONA'),
  2652. ]);
  2653. $arrStockArtitle = json_decode(json_encode($arrStockArtitle), true);
  2654. } catch (\Throwable $th) {
  2655. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  2656. }
  2657. foreach ($arrStockArtitle as $key => $stockArtitle) {
  2658. $imagesGallery = [];
  2659. $arrImages = json_decode($stockArtitle['IMAGENES']);
  2660. foreach($arrImages as $image) {
  2661. $imageCodeEnc = $this->encController->encrypt($image);
  2662. $response = $this->documentManagementController->privateGetPublicDocumentURL(
  2663. $imageCodeEnc,
  2664. $user,
  2665. $line
  2666. );
  2667. if($response['error']){
  2668. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  2669. }
  2670. $imagesGallery[] = $response['response']['public_uri'];
  2671. }
  2672. $stockArtitle['IMAGENES'] = $imagesGallery;
  2673. $arrStockArtitle[$key] = $stockArtitle;
  2674. }
  2675. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrStockArtitle);
  2676. }
  2677. // FUNCIÓN PARA JOSÉ LUIS
  2678. public function getInfoStock($user, $line) {
  2679. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  2680. if ($arrResponseCheckUser['error']) {
  2681. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  2682. }
  2683. try {
  2684. $arrStockArtitle = DB::table('S002V01TSTAR')
  2685. ->where('STAR_NULI', '=', $line)
  2686. ->where('STAR_ESTA', '=', 'Activo')
  2687. ->where('INST_NULI', '=', $line)
  2688. ->where('INST_ESTA', '=', 'Activo')
  2689. ->where('UBAR_ESTA', '=', 'Activo')
  2690. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  2691. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  2692. ->get([
  2693. 'STAR_IDST AS ID_STOCK',
  2694. 'INST_MODE AS MODELO',
  2695. 'INST_COMO AS CODIGO_MODELO',
  2696. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  2697. 'STAR_CONS AS CONSUMIBLE',
  2698. 'STAR_REPA AS REPARABLE',
  2699. ]);
  2700. } catch (\Throwable $th) {
  2701. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  2702. }
  2703. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrStockArtitle);
  2704. }
  2705. public function moveToStock(Request $request, string $idStock ) {
  2706. $validator = Validator::make($request->all(), [
  2707. 'ALMACEN' => 'required|string',
  2708. 'AREA' => 'required|string',
  2709. 'NIVEL' => 'required|string',
  2710. 'ZONA' => 'required|string',
  2711. 'FAMILIA' => 'required|string',
  2712. 'SUBFAMILIA' => 'required|string',
  2713. 'LINEA' => 'required|string',
  2714. 'TIPO' => 'required|string',
  2715. 'MODELO' => 'required|string',
  2716. 'ID_EQUIPMENTO' => 'required|string',
  2717. 'STATUS' => 'required|string',
  2718. 'CODIGO_EQUIPAMIENTO' => 'required|string',
  2719. 'USUARIO' => 'required|string',
  2720. 'NUMERO_LINEA' => 'required|integer',
  2721. ]);
  2722. if($validator->fails()){
  2723. return $this->responseController->makeResponse(
  2724. true,
  2725. "Se encontraron uno o más errores.",
  2726. $this->responseController->makeErrors(
  2727. $validator->errors()->messages()
  2728. ),
  2729. 401
  2730. );
  2731. }
  2732. DB::beginTransaction();
  2733. $requestData = $request->all();
  2734. $arrInfoRegistro = array();
  2735. $arrToWorkflow = array();
  2736. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  2737. if ($arrResponseCheckUser['error']) {
  2738. DB::rollBack();
  2739. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  2740. }
  2741. $user = $arrResponseCheckUser['response'];
  2742. // Se obtiene el identificador del stock
  2743. $idStock = $this->encController->decrypt($idStock);
  2744. if ($idStock === false) {
  2745. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el stock.', [], 406);
  2746. }
  2747. // Se obtiene el valor del almacen
  2748. $requestData['ALMACEN'] = $this->encController->decrypt($requestData['ALMACEN']);
  2749. if ($requestData['ALMACEN'] === false) {
  2750. DB::rollBack();
  2751. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el ID del almacen.', [], 406);
  2752. }
  2753. // Se obtiene el valor del area
  2754. $requestData['AREA'] = $this->encController->decrypt($requestData['AREA']);
  2755. if ($requestData['AREA'] === false) {
  2756. DB::rollBack();
  2757. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el ID del área.', [], 406);
  2758. }
  2759. // Se obtiene el valor del nivel
  2760. $requestData['NIVEL'] = $this->encController->decrypt($requestData['NIVEL']);
  2761. if ($requestData['NIVEL'] === false) {
  2762. DB::rollBack();
  2763. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el ID del nivel.', [], 406);
  2764. }
  2765. // Se obtiene el valor de la zona
  2766. $requestData['ZONA'] = $this->encController->decrypt($requestData['ZONA']);
  2767. if ($requestData['ZONA'] === false) {
  2768. DB::rollBack();
  2769. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el ID de la zona.', [], 406);
  2770. }
  2771. // Se verifica el código del artículo que sea válido
  2772. $codeVerified = $this->verifyPreCode($requestData);
  2773. if ($codeVerified !== $requestData['CODIGO_EQUIPAMIENTO']) {
  2774. DB::rollBack();
  2775. return $this->responseController->makeResponse(true, 'El código generado no coincide con el código verificado a partir de la información del formulario.', [], 406);
  2776. }
  2777. // Se verifica que el código no sea el mismo que el anterior
  2778. if ($requestData['ANTERIOR_CODIGO_EQUIPAMIENTO'] === $requestData['CODIGO_EQUIPAMIENTO']) {
  2779. DB::rollBack();
  2780. return $this->responseController->makeResponse(true, 'Los códigos no deben ser los mismos.', [], 406);
  2781. }
  2782. // Se verifica que la información del almacen exista
  2783. try {
  2784. $validateWarehouse = DB::table('S002V01TALMA')
  2785. ->where('ALMA_COAL', '=', $requestData['ALMACEN'])
  2786. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  2787. ->exists();
  2788. } catch (\Throwable $th) {
  2789. DB::rollBack();
  2790. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si el almacen existe.', $th->getMessage(), 500);
  2791. }
  2792. if ($validateWarehouse === false) {
  2793. DB::rollBack();
  2794. return $this->responseController->makeResponse(true, 'El almacen no existe.', [], 500);
  2795. }
  2796. // Se verifica que la informacion del área exista
  2797. try {
  2798. $validateArea = DB::table('S002V01TAREA')
  2799. ->where('AREA_COAL', '=', $requestData['ALMACEN'])
  2800. ->where('AREA_COAR', '=', $requestData['AREA'])
  2801. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  2802. ->exists();
  2803. } catch (\Throwable $th) {
  2804. DB::rollBack();
  2805. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si el área existe.', $th->getMessage(), 500);
  2806. }
  2807. if ($validateArea === false) {
  2808. DB::rollBack();
  2809. return $this->responseController->makeResponse(true, 'El área no existe.', [], 500);
  2810. }
  2811. // Se verifica que la información del nivel exista
  2812. try {
  2813. $validateLevel = DB::table('S002V01TNIVE')
  2814. ->where('NIVE_COAL', '=', $requestData['ALMACEN'])
  2815. ->where('NIVE_COAR', '=', $requestData['AREA'])
  2816. ->where('NIVE_CONI', '=', $requestData['NIVEL'])
  2817. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  2818. ->exists();
  2819. } catch (\Throwable $th) {
  2820. DB::rollBack();
  2821. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si el nivel existe.', $th->getMessage(), 500);
  2822. }
  2823. if ($validateLevel === false) {
  2824. DB::rollBack();
  2825. return $this->responseController->makeResponse(true, 'El nivel no existe.', [], 500);
  2826. }
  2827. // Se verifica que la información de la zona
  2828. try {
  2829. $validateZone = DB::table('S002V01TZONA')
  2830. ->where('ZONA_COAL', '=', $requestData['ALMACEN'])
  2831. ->where('ZONA_COAR', '=', $requestData['AREA'])
  2832. ->where('ZONA_CONI', '=', $requestData['NIVEL'])
  2833. ->where('ZONA_COZO', '=', $requestData['ZONA'])
  2834. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  2835. ->exists();
  2836. } catch (\Throwable $th) {
  2837. DB::rollBack();
  2838. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si la zona existe.', $th->getMessage(), 500);
  2839. }
  2840. if ($validateZone === false) {
  2841. return $this->responseController->makeResponse(true, 'La zona no existe.', [], 500);
  2842. }
  2843. $arrToWorkflow[] = [
  2844. 'ANTERIOR_CODIGO_EQUIPAMIENTO' => $requestData['ANTERIOR_CODIGO_EQUIPAMIENTO'],
  2845. 'NUEVO_CODIGO_EQUIPAMIENTO' => $requestData['CODIGO_EQUIPAMIENTO'],
  2846. 'ALMACEN' => $requestData['ALMACEN'],
  2847. 'AREA' => $requestData['AREA'],
  2848. 'NIVEL' => $requestData['NIVEL'],
  2849. 'ZONA' => $requestData['ZONA'],
  2850. ];
  2851. $strToWorkflow = json_encode($arrToWorkflow);
  2852. $encToWorkflow = $this->encController->encrypt($strToWorkflow);
  2853. $now = $this->functionsController->now();
  2854. $currentDate = $now->toDateTimeString();
  2855. try {
  2856. $validateUpdate = DB::table('S002V01TUBAR')
  2857. ->where('UBAR_IDST', '=', $idStock)
  2858. ->where('UBAR_COUB', '=', $requestData['ANTERIOR_CODIGO_EQUIPAMIENTO'])
  2859. ->where('UBAR_ESTA', '=', 'Activo')
  2860. ->where('UBAR_NULI', '=', $requestData['NUMERO_LINEA'])
  2861. ->update([
  2862. 'UBAR_ESTA' => 'Obsoleto',
  2863. 'UBAR_USMO' => $user,
  2864. 'UBAR_FEMO' => $currentDate,
  2865. 'UBAR_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  2866. ]);
  2867. } catch (\Throwable $th) {
  2868. DB::rollBack();
  2869. return $this->responseController->makeResponse(true, 'Ocurrió un error al modificar el estado actual del código del stock.', $th->getMessage(), 406);
  2870. }
  2871. if (!$validateUpdate) {
  2872. DB::rollBack();
  2873. return $this->responseController->makeResponse(true, 'No se pudo modificar el estado actual del código del stock.', [], 406);
  2874. }
  2875. try {
  2876. $idUbication = DB::table('S002V01TUBAR')->insertGetId([
  2877. 'UBAR_NULI' => $requestData['NUMERO_LINEA'],
  2878. 'UBAR_COAL' => $requestData['ALMACEN'],
  2879. 'UBAR_COAR' => $requestData['AREA'],
  2880. 'UBAR_CONI' => $requestData['NIVEL'],
  2881. 'UBAR_COZO' => $requestData['ZONA'],
  2882. 'UBAR_IDST' => $idStock,
  2883. 'UBAR_COUB' => $requestData['CODIGO_EQUIPAMIENTO'],
  2884. 'UBAR_ESTA' => 'Workflow',
  2885. 'UBAR_USRE' => $user,
  2886. 'UBAR_FERE' => $currentDate,
  2887. 'UBAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2888. ]);
  2889. } catch (\Throwable $th) {
  2890. DB::rollBack();
  2891. return $this->responseController->makeResponse(true, 'Ocurrió un error al insertar la nueva ubicación del artículo.', $th->getMessage(), 406);
  2892. }
  2893. if (!$idUbication) {
  2894. DB::rollBack();
  2895. return $this->responseController->makeResponse(true, 'No se pudo insertar la nueva ubicación del artículo.', [], 406);
  2896. }
  2897. $arrInfoRegistro['S002V01TUBAR'][] = [
  2898. 'UBAR_IDUB' => $idUbication,
  2899. 'UBAR_IDST' => $idStock,
  2900. 'UBAR_COUB' => $requestData['CODIGO_EQUIPAMIENTO'],
  2901. ];
  2902. try {
  2903. $equipment = (array) DB::table('S002V01TEQUI')
  2904. ->where('EQUI_COEQ', '=', $requestData['ANTERIOR_CODIGO_EQUIPAMIENTO'])
  2905. ->where('EQUI_NULI', '=', $requestData['NUMERO_LINEA'])
  2906. ->first();
  2907. } catch (\Throwable $th) {
  2908. DB::rollBack();
  2909. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del artículo.', $th->getMessage(), 406);
  2910. }
  2911. if (is_null($equipment) || empty($equipment)) {
  2912. DB::rollBack();
  2913. return $this->responseController->makeResponse(true, 'No se se pudo obtener la información del equipamiento.', [], 406);
  2914. }
  2915. $strHistorial = $equipment['EQUI_HICO'];
  2916. $arrHistorial = json_decode($strHistorial, true);
  2917. $arrHistorial[] = $requestData['CODIGO_EQUIPAMIENTO'];
  2918. try {
  2919. $validateUpdate = DB::table('S002V01TEQUI')
  2920. ->where('EQUI_COEQ', '=', $requestData['ANTERIOR_CODIGO_EQUIPAMIENTO'])
  2921. ->where('EQUI_NULI', '=', $requestData['NUMERO_LINEA'])
  2922. ->update([
  2923. 'EQUI_COEQ' => $requestData['CODIGO_EQUIPAMIENTO'],
  2924. 'EQUI_ALMA' => $requestData['ALMACEN'],
  2925. 'EQUI_AREA' => $requestData['AREA'],
  2926. 'EQUI_NIVE' => $requestData['NIVEL'],
  2927. 'EQUI_ZONA' => $requestData['ZONA'],
  2928. 'EQUI_HICO' => $arrHistorial,
  2929. 'EQUI_USMO' => $user,
  2930. 'EQUI_FEMO' => $currentDate,
  2931. 'EQUI_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2932. ]);
  2933. } catch (\Throwable $th) {
  2934. DB::rollBack();
  2935. return $this->responseController->makeResponse(true, 'Ocurrió un error al modificar el equipamiento.', $th->getMessage(), 406);
  2936. }
  2937. if (!$validateUpdate) {
  2938. DB::rollBack();
  2939. return $this->responseController->makeResponse(true, 'No se pudo modificar el equipamiento.', [], 406);
  2940. }
  2941. try {
  2942. $existsEquipmentWorkflow = DB::table('S002V01TEQWO')
  2943. ->where('EQWO_COEQ', '=', $requestData['CODIGO_EQUIPAMIENTO'])
  2944. ->where('EQWO_NULI', '=', $requestData['NUMERO_LINEA'])
  2945. ->where('EQWO_ESTA', '=', 'En Workflow')
  2946. ->exists();
  2947. } catch (\Throwable $th) {
  2948. DB::rollBack();
  2949. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar el estado del equipamiento.', $th->getMessage(), 406);
  2950. }
  2951. if ( !$existsEquipmentWorkflow ) {
  2952. try {
  2953. $idEquipmentWorkflow = DB::table('S002V01TEQWO')->insertGetId([
  2954. 'EQWO_NULI' => $requestData['NUMERO_LINEA'],
  2955. 'EQWO_COEQ' => $requestData['CODIGO_EQUIPAMIENTO'],
  2956. 'EQWO_ESTA' => 'En Workflow',
  2957. 'EQWO_USRE' => $user,
  2958. 'EQWO_FERE' => $currentDate,
  2959. 'EQWO_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2960. ]);
  2961. } catch (\Throwable $th) {
  2962. DB::rollBack();
  2963. return $this->responseController->makeResponse(true, 'Ocurrió un error al registrar el equipamiento en el workflow.', $th->getMessage(), 406);
  2964. }
  2965. if (!$idEquipmentWorkflow) {
  2966. DB::rollBack();
  2967. return $this->responseController->makeResponse(true, 'No se pudo registrar el equipamiento en el workflow.', [], 406);
  2968. }
  2969. $arrInfoRegistro['S002V01TEQWO'][] = [
  2970. 'EQWO_IDEQW' => $idEquipmentWorkflow,
  2971. 'EQWO_COEQ' => $requestData['CODIGO_EQUIPAMIENTO'],
  2972. ];
  2973. } else {
  2974. DB::rollBack();
  2975. return $this->responseController->makeResponse(true, 'No se pudo hacer el intercambio si el equipamiento se encuentra en proceso de validación.', [], 406);
  2976. }
  2977. $strInfoRegistro = json_encode($arrInfoRegistro);
  2978. $encInfoRegistro = $this->encController->encrypt($strInfoRegistro);
  2979. $arrResponseRequestWorkflow = $this->processManagementController->registerRequestWorkflow(5, $encToWorkflow, $encInfoRegistro, $user, $requestData['NUMERO_LINEA']);
  2980. if ($arrResponseRequestWorkflow['error']) {
  2981. DB::rollBack();
  2982. return $this->responseController->makeResponse(true, $arrResponseRequestWorkflow['msg'], $arrResponseRequestWorkflow['response'], 500);
  2983. }
  2984. DB::commit();
  2985. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  2986. }
  2987. private function verifyPreCode(array $parameters) {
  2988. $siteTag = '';
  2989. $site = ['LINEA', 'AREA', 'NIVEL'];
  2990. foreach($site as $ctrl){
  2991. $val = $parameters[$ctrl];
  2992. if($ctrl == 'LINE'){
  2993. $val = intval($val) < 10 ? "0$val" : "$val";
  2994. }
  2995. $siteTag .= $val.'.';
  2996. }
  2997. $siteTag = substr($siteTag, 0, -1);
  2998. $areaTag = '';
  2999. $area = ['ALMACEN', 'ZONA'];
  3000. foreach($area as $ctrl){
  3001. $val = $parameters[$ctrl];
  3002. $areaTag .= $val.'.';
  3003. }
  3004. $areaTag = substr($areaTag, 0, -1);
  3005. $technicalSpecificationTag = '';
  3006. $technicalSpecification = ['FAMILIA', 'SUBFAMILIA', 'STATUS'];
  3007. foreach($technicalSpecification as $ctrl){
  3008. $val = $parameters[$ctrl];
  3009. $technicalSpecificationTag .= $val.'.';
  3010. }
  3011. $technicalSpecificationTag = substr($technicalSpecificationTag, 0, -1);
  3012. $equipmentIdentifierTag = '';
  3013. $equipmentIdentifier = ['TIPO', 'MODELO', 'ID_EQUIPMENTO'];
  3014. foreach($equipmentIdentifier as $ctrl){
  3015. $val = $parameters[$ctrl];
  3016. if($ctrl == 'MODELO'){
  3017. $val = $this->processElement($val, 'model');
  3018. }else if($ctrl == 'TIPO'){
  3019. $val = $this->processElement($val, 'element');
  3020. }
  3021. $equipmentIdentifierTag .= $val.'-';
  3022. }
  3023. $equipmentIdentifierTag = substr($equipmentIdentifierTag, 0, -1);
  3024. $code = $siteTag.'-'.$areaTag.'_'.$technicalSpecificationTag.'.'.$equipmentIdentifierTag;
  3025. return $code;
  3026. }
  3027. private function processElement(string $value, string $type) : string {
  3028. $value = strtoupper($value);
  3029. $validVal0 = $this->functionsController->unaccent($value);
  3030. $CONNECTORS = ['DE', 'PARA', 'Y', 'O', 'DEL', 'EL', 'LA', 'LOS', 'POR', 'EN'];
  3031. $valueArr1 = explode(' ', $validVal0);
  3032. $validVal1 = [];
  3033. foreach($valueArr1 as $word){
  3034. if(!in_array($word, $CONNECTORS)){
  3035. $validVal1[] = $word;
  3036. }
  3037. }
  3038. $validLength = count($validVal1);
  3039. $validVal2 = "";
  3040. if($validLength < 1){
  3041. return "ERROR";
  3042. }else if($validLength == 1){
  3043. if(strlen($validVal1[0]) < 5){
  3044. $validVal2 = $validVal1[0];
  3045. for($i = strlen($validVal1[0]); $i < 5; $i++){
  3046. if($type == 'model'){
  3047. $validVal2 = "X$validVal2";
  3048. }else{
  3049. $validVal2 = $validVal2 . "X";
  3050. }
  3051. }
  3052. }else{
  3053. $validVal2 = substr($validVal1[0], 0, 5);
  3054. }
  3055. }else if($validLength == 2){
  3056. $word1 = "";
  3057. if(strlen($validVal1[0]) < 2){
  3058. if($type == 'model'){
  3059. $word1 = "X$validVal1[0]";
  3060. }else{
  3061. $word1 = $validVal1[0] . "X";
  3062. }
  3063. }else{
  3064. $word1 = substr($validVal1[0], 0, 2);
  3065. }
  3066. $word2 = "";
  3067. if(strlen($validVal1[1]) < 3){
  3068. $word2 = $validVal1[1];
  3069. for($i = strlen($validVal1[1]); $i < 3; $i++){
  3070. if($type == 'model'){
  3071. $word2 = "X$word2";
  3072. }else{
  3073. $word2 = $word2 . "X";
  3074. }
  3075. }
  3076. }else{
  3077. $word2 = substr($validVal1[1], 0, 3);
  3078. }
  3079. $validVal2 = $word1 . $word2;
  3080. }else if($validLength == 3){
  3081. $word1 = "";
  3082. if(strlen($validVal1[0]) < 2){
  3083. if($type == 'model'){
  3084. $word1 = "X$validVal1[0]";
  3085. }else{
  3086. $word1 = $validVal1[0] . "X";
  3087. }
  3088. }else{
  3089. $word1 = substr($validVal1[0], 0, 2);
  3090. }
  3091. $word2 = substr($validVal1[1], 0, 1);
  3092. $word3 = "";
  3093. if(strlen($validVal1[2]) < 2){
  3094. if($type == 'model'){
  3095. $word3 = "X$validVal1[2]";
  3096. }else{
  3097. $word3 = $validVal1[2] . "X";
  3098. }
  3099. }else{
  3100. $word3 = substr($validVal1[2], 0, 2);
  3101. }
  3102. $validVal2 = $word1 . $word2 . $word3;
  3103. }else if($validLength == 4){
  3104. $word1 = "";
  3105. if(strlen($validVal1[0]) < 2){
  3106. if($type == 'model'){
  3107. $word1 = "X$validVal1[0]";
  3108. }else{
  3109. $word1 = $validVal1[0] . "X";
  3110. }
  3111. }else{
  3112. $word1 = substr($validVal1[0], 0, 2);
  3113. }
  3114. $word2 = substr($validVal1[1], 0, 1);
  3115. $word3 = substr($validVal1[2], 0, 1);
  3116. $word4 = substr($validVal1[3], 0, 1);
  3117. $validVal2 = $word1 . $word2 . $word3 . $word4;
  3118. }else if($validLength >= 5){
  3119. $word1 = substr($validVal1[0], 0, 1);
  3120. $word2 = substr($validVal1[1], 0, 1);
  3121. $word3 = substr($validVal1[2], 0, 1);
  3122. $word4 = substr($validVal1[3], 0, 1);
  3123. $word5 = substr($validVal1[4], 0, 1);
  3124. $validVal2 = $word1 . $word2 . $word3 . $word4 . $word5;
  3125. }else{
  3126. return "ERROR";
  3127. }
  3128. return $validVal2;
  3129. }
  3130. public function getLocationStock($user, $line) {
  3131. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  3132. if ($arrResponseCheckUser['error']) {
  3133. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3134. }
  3135. try {
  3136. $arrLocation = DB::table('S002V01TUBAR')
  3137. ->where('UBAR_NULI', '=', $line)
  3138. ->where('UBAR_ESTA', '=', 'Activo')
  3139. ->whereOr('UBAR_ESTA', '=', 'Otro')
  3140. ->get([
  3141. DB::raw("CONCAT('#', UBAR_IDST) AS ID_STOCK"),
  3142. DB::raw("CONCAT('#', UBAR_IDUB) AS ID_UBICACION "),
  3143. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  3144. 'UBAR_ESTA AS ESTADO',
  3145. 'UBAR_USRE AS USUARIO_REGISTRA',
  3146. 'UBAR_FERE AS FECHA_REGISTRA',
  3147. 'UBAR_USMO AS USUARIO_MODIFICA',
  3148. 'UBAR_FEMO AS FECHA_MODIFICA',
  3149. ]);
  3150. $arrLocation = json_decode(json_encode($arrLocation), true);
  3151. } catch (\Throwable $th) {
  3152. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el emplazamiento del stock.', $th->getMessage(), 500);
  3153. }
  3154. $responseCheckLatestUpdate = $this->resourcesController->checkLatestUpdate($arrLocation, $line);
  3155. if ($responseCheckLatestUpdate['error']) {
  3156. return $this->responseController->makeResponse(true, $responseCheckLatestUpdate['msg'], [], 500);
  3157. }
  3158. $arrLocation = $responseCheckLatestUpdate['response'];
  3159. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrLocation);
  3160. }
  3161. public function getLocationByStock($idStock, $user, $line) {
  3162. $idStock = $this->encController->decrypt($idStock);
  3163. if (is_null($idStock)) {
  3164. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID del Stock.', [], 401);
  3165. }
  3166. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  3167. if ($arrResponseCheckUser['error']) {
  3168. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3169. }
  3170. try {
  3171. $arrLocation = DB::table('S002V01TUBAR')
  3172. ->where('UBAR_NULI', '=', $line)
  3173. ->where('UBAR_IDST', '=', $idStock)
  3174. ->orderBy('UBAR_IDUB', 'DESC')
  3175. ->get([
  3176. DB::raw("CONCAT('#', UBAR_IDUB) AS ID_UBICACION "),
  3177. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  3178. 'UBAR_ESTA AS ESTADO',
  3179. 'UBAR_USRE AS USUARIO_REGISTRA',
  3180. 'UBAR_FERE AS FECHA_REGISTRA',
  3181. 'UBAR_USMO AS USUARIO_MODIFICA',
  3182. 'UBAR_FEMO AS FECHA_MODIFICA',
  3183. ]);
  3184. $arrLocation = json_decode(json_encode($arrLocation), true);
  3185. } catch (\Throwable $th) {
  3186. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el emplazamiento del stock.', $th->getMessage(), 500);
  3187. }
  3188. $responseCheckLatestUpdate = $this->resourcesController->checkLatestUpdate($arrLocation, $line);
  3189. if ($responseCheckLatestUpdate['error']) {
  3190. return $this->responseController->makeResponse(true, $responseCheckLatestUpdate['msg'], [], 500);
  3191. }
  3192. $arrLocation = $responseCheckLatestUpdate['response'];
  3193. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrLocation);
  3194. }
  3195. public function getDetaisLocation($idStock, $idLocation, $user, $line) {
  3196. $idStock = $this->encController->decrypt($idStock);
  3197. if (is_null($idStock)) {
  3198. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID del Stock.', [], 401);
  3199. }
  3200. $idLocation = $this->encController->decrypt($idLocation);
  3201. if (is_null($idLocation)) {
  3202. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID de la ubicación.', [], 401);
  3203. }
  3204. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  3205. if ($arrResponseCheckUser['error']) {
  3206. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3207. }
  3208. try {
  3209. $arrLocation = (array) DB::table('S002V01TUBAR')
  3210. ->where('UBAR_NULI', '=', $line)
  3211. ->where('UBAR_IDST', '=', $idStock)
  3212. ->where('UBAR_IDUB', '=', $idLocation)
  3213. ->join('S002V01TALMA', 'ALMA_COAL', '=', 'UBAR_COAL')
  3214. ->join('S002V01TAREA', 'AREA_COAR', '=', 'UBAR_COAR')
  3215. ->join('S002V01TNIVE', 'NIVE_CONI', '=', 'UBAR_CONI')
  3216. ->join('S002V01TZONA', 'ZONA_COZO', '=', 'UBAR_COZO')
  3217. ->first([
  3218. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  3219. DB::raw('CONCAT(ALMA_NOAL, " (", ALMA_COAL, ")") AS ALMACEN'),
  3220. DB::raw('CONCAT(AREA_NOAR, " (", AREA_COAL, ")") AS AREA'),
  3221. DB::raw('CONCAT(NIVE_NONI, " (", NIVE_CONI, ")") AS NIVEL'),
  3222. DB::raw('CONCAT(ZONA_NOZO, " (", ZONA_COZO, ")") AS ZONA'),
  3223. 'UBAR_ESTA AS ESTADO',
  3224. ]);
  3225. } catch (\Throwable $th) {
  3226. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el emplazamiento del stock.', $th->getMessage(), 500);
  3227. }
  3228. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrLocation);
  3229. }
  3230. public function search(Request $request) {
  3231. $validator = Validator::make($request->all(), [
  3232. 'ID_STOCK' => 'nullable|string',
  3233. 'CODIGO_EQUIPAMIENTO' => 'nullable|string',
  3234. 'ID_UBICACION' => 'nullable|string',
  3235. 'FAMILIA' => 'nullable|string',
  3236. 'SUBFAMILIA' => 'nullable|string',
  3237. 'MODELO' => 'nullable|string',
  3238. 'CODIGO_MODELO' => 'nullable|string',
  3239. 'ALMACEN' => 'nullable|string',
  3240. 'AREA' => 'nullable|string',
  3241. 'NIVEL' => 'nullable|string',
  3242. 'ZONA' => 'nullable|string',
  3243. 'TIPO_ADQUISICION' => 'nullable|string',
  3244. 'NUMERO_PROVEEDOR' => 'nullable|string',
  3245. 'CODIGO_BARRAS' => 'nullable|string',
  3246. 'FECHA_VENCIMIENTO' => 'nullable|string',
  3247. 'TASA_ROTACION' => 'nullable|string',
  3248. 'STOCK_MINIMO' => 'nullable|string',
  3249. 'STOCK_MAXIMO' => 'nullable|string',
  3250. 'ESTADO' => 'nullable|string',
  3251. 'CONSUMIBLE' => 'nullable|string',
  3252. 'REPARABLE' => 'nullable|string',
  3253. 'USUARIO' => 'nullable|string',
  3254. 'NUMERO_LINEA' => 'nullable|string',
  3255. ]);
  3256. if ($validator->fails()) {
  3257. return $this->responseController->makeResponse(
  3258. true,
  3259. "Se encontraron uno o más errores.",
  3260. $this->responseController->makeErrors($validator->errors()->messages()),
  3261. 401
  3262. );
  3263. }
  3264. DB::beginTransaction();
  3265. $requestData = $request->all();
  3266. $arrClausules = [
  3267. 'FAMILIA' => 'INST_COFA',
  3268. 'SUBFAMILIA' => 'INST_COSU',
  3269. 'MODELO' => 'INST_MODE',
  3270. 'CODIGO_MODELO' => 'INST_COMO',
  3271. 'STOCK_MINIMO' => 'INST_STMI',
  3272. 'STOCK_MAXIMO' => 'INST_STMA',
  3273. 'ID_STOCK' => 'STAR_IDST',
  3274. 'CODIGO_BARRAS' => 'STAR_COBA',
  3275. 'FECHA_VENCIMIENTO' => 'STAR_FEVE',
  3276. 'NUMERO_PROVEEDOR' => 'STAR_NUPR',
  3277. 'TIPO_ADQUISICION' => 'STAR_TIAD',
  3278. 'CONSUMIBLE' => 'STAR_CONS',
  3279. 'REPARABLE' => 'STAR_REPA',
  3280. 'ESTADO' => 'STAR_ESTA',
  3281. 'ID_UBICACION' => 'UBAR_IDUB',
  3282. 'ALMACEN' => 'UBAR_COAL',
  3283. 'AREA' => 'UBAR_COAR',
  3284. 'NIVEL' => 'UBAR_CONI',
  3285. 'ZONA' => 'UBAR_COZO',
  3286. 'CODIGO_EQUIPAMIENTO' => 'UBAR_COUB',
  3287. ];
  3288. $arrWhere = array();
  3289. foreach ($requestData as $key => $value) {
  3290. if (!is_null($value) && $key !== 'USUARIO' && $key !== 'NUMERO_LINEA') {
  3291. $column = $arrClausules[$key];
  3292. $arrWhere[$column] = $value;
  3293. }
  3294. }
  3295. if (empty($arrWhere)) {
  3296. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", []);
  3297. }
  3298. try {
  3299. $arrInfo = DB::table('S002V01TSTAR')
  3300. ->where($arrWhere)
  3301. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  3302. ->where('INST_NULI', '=', $requestData['NUMERO_LINEA'])
  3303. ->where('UBAR_NULI', '=', $requestData['NUMERO_LINEA'])
  3304. ->where('INST_ESTA', '=', 'Activo')
  3305. ->where('UBAR_ESTA', '=', 'Activo')
  3306. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  3307. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  3308. ->get([
  3309. 'STAR_IDST AS ID_STOCK',
  3310. 'INST_MODE AS MODELO',
  3311. 'INST_COMO AS CODIGO_MODELO',
  3312. 'INST_IMAG AS IMAGENES',
  3313. 'UBAR_COUB AS CODIGO',
  3314. ]);
  3315. $arrInfo = json_decode(json_encode($arrInfo), true);
  3316. } catch (\Throwable $th) {
  3317. return $this->responseController->makeResponse(true, "Ocurrió un error al recuperar la información.", $th->getMessage(), 500);
  3318. }
  3319. foreach ($arrInfo as $key => $info) {
  3320. $arrImages = json_decode($info['IMAGENES']);
  3321. $arrUrlImage = array();
  3322. foreach ($arrImages as $keyImages => $images) {
  3323. $images = $this->encController->encrypt($images);
  3324. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  3325. if ($responseDocument['error']) {
  3326. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  3327. }
  3328. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  3329. }
  3330. $arrInfo[$key]['IMAGENES'] = $arrUrlImage;
  3331. }
  3332. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrInfo);
  3333. }
  3334. public function getInfoByStock($idStock, $user, $line) {
  3335. $idStock = $this->encController->decrypt($idStock);
  3336. if (is_null($idStock)) {
  3337. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID del Stock.', [], 401);
  3338. }
  3339. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  3340. if ($arrResponseCheckUser['error']) {
  3341. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3342. }
  3343. try {
  3344. $arrInfo = (array) DB::table('S002V01TSTAR')
  3345. ->where('STAR_IDST', '=', $idStock)
  3346. ->where('STAR_NULI', '=', $line)
  3347. ->where('INST_NULI', '=', $line)
  3348. ->where('UBAR_NULI', '=', $line)
  3349. ->where('INST_ESTA', '=', 'Activo')
  3350. ->where('UBAR_ESTA', '=', 'Activo')
  3351. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  3352. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  3353. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  3354. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  3355. ->join('S002V01TALMA', 'ALMA_COAL', '=', 'UBAR_COAL')
  3356. ->join('S002V01TAREA', 'AREA_COAR', '=', 'UBAR_COAR')
  3357. ->join('S002V01TNIVE', 'NIVE_CONI', '=', 'UBAR_CONI')
  3358. ->join('S002V01TZONA', 'ZONA_COZO', '=', 'UBAR_COZO')
  3359. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'STAR_NUPR')
  3360. ->first([
  3361. DB::raw('CONCAT(FAMI_NOFA, " (", FAMI_COFA, ")") AS FAMILIA'),
  3362. DB::raw('CONCAT(SUBF_NOSU, " (", SUBF_COSU, ")") AS SUBFAMILIA'),
  3363. 'INST_MODE AS MODELO',
  3364. 'INST_COMO AS CODIGO_MODELO',
  3365. 'INST_STMI AS STOCK_MINIMO',
  3366. 'INST_STMA AS STOCK_MAXIMO',
  3367. 'STAR_IDST AS ID_STOCK',
  3368. 'STAR_COBA AS CODIGO_BARRAS',
  3369. 'STAR_FEVE AS FECHA_VENCIMIENTO',
  3370. DB::raw('CONCAT(PROV_NOCO, " (", PROV_NUPR, ")") AS NUMERO_PROVEEDOR'),
  3371. 'STAR_TIAD AS TIPO_ADQUISICION',
  3372. 'STAR_CONS AS CONSUMIBLE',
  3373. 'STAR_REPA AS REPARABLE',
  3374. 'STAR_ESTA AS ESTADO',
  3375. 'UBAR_IDUB AS ID_UBICACION',
  3376. DB::raw('CONCAT(ALMA_NOAL, " (", ALMA_COAL, ")") AS ALMACEN'),
  3377. DB::raw('CONCAT(AREA_NOAR, " (", AREA_COAR, ")") AS AREA'),
  3378. DB::raw('CONCAT(NIVE_NONI, " (", NIVE_CONI, ")") AS NIVEL'),
  3379. DB::raw('CONCAT(ZONA_NOZO, " (", ZONA_COZO, ")") AS ZONA'),
  3380. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  3381. ]);
  3382. $arrInfo = json_decode(json_encode($arrInfo), true);
  3383. } catch (\Throwable $th) {
  3384. return $this->responseController->makeResponse(true, "Ocurrió un error al recuperar la información.", $th->getMessage(), 500);
  3385. }
  3386. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrInfo);
  3387. }
  3388. public function getMaxMinStock($user, $line) {
  3389. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  3390. if ($arrResponseCheckUser['error']) {
  3391. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3392. }
  3393. try {
  3394. $arrInfoStock = DB::table('S002V01TINST')
  3395. ->where('INST_NULI', '=', $line)
  3396. ->where('INST_ESTA', '=', 'Activo')
  3397. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  3398. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  3399. ->get([
  3400. 'INST_IDIS AS ID_INFORMACION_STOCK',
  3401. DB::raw('CONCAT(FAMI_NOFA, " (", FAMI_COFA, ")") AS FAMILIA'),
  3402. DB::raw('CONCAT(SUBF_NOSU, " (", SUBF_COSU, ")") AS SUBFAMILIA'),
  3403. 'INST_MODE AS MODELO',
  3404. 'INST_COMO AS CODIGO_MODELO',
  3405. 'INST_STMI AS STOCK_MINIMO',
  3406. 'INST_STMA AS STOCK_MAXIMO',
  3407. 'INST_IMAG AS IMAGENES',
  3408. ]);
  3409. $arrInfoStock = json_decode(json_encode($arrInfoStock), true);
  3410. } catch (\Throwable $th) {
  3411. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  3412. }
  3413. foreach ($arrInfoStock as $key => $info) {
  3414. $arrImages = json_decode($info['IMAGENES']);
  3415. $arrUrlImage = array();
  3416. foreach ($arrImages as $keyImages => $images) {
  3417. $images = $this->encController->encrypt($images);
  3418. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  3419. if ($responseDocument['error']) {
  3420. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  3421. }
  3422. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  3423. }
  3424. $arrInfoStock[$key]['IMAGENES'] = $arrUrlImage;
  3425. try {
  3426. $countStock = DB::table('S002V01TSTAR')
  3427. ->where('STAR_NULI', '=', $line)
  3428. ->where('STAR_IDIS', '=', $info['ID_INFORMACION_STOCK'])
  3429. ->where('STAR_ESTA', '=', 'Activo')
  3430. ->count();
  3431. } catch (\Throwable $th) {
  3432. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  3433. }
  3434. $arrInfoStock[$key]['CANTIDAD_ACTUAL'] = $countStock;
  3435. $stateStock = '';
  3436. if ($countStock < $info['STOCK_MINIMO'] && !is_null($info['STOCK_MINIMO'])) {
  3437. $stateStock = 'Menor al Stock Requerido';
  3438. } else if ($countStock > $info['STOCK_MAXIMO'] && !is_null($info['STOCK_MAXIMO'])) {
  3439. $stateStock = 'Exedente al Stock Requerido';
  3440. } else if (is_null($info['STOCK_MINIMO']) && is_null($info['STOCK_MAXIMO'])) {
  3441. $stateStock = 'No aplica';
  3442. } else {
  3443. $stateStock = 'Stock Normal';
  3444. }
  3445. if (is_null($info['STOCK_MINIMO'])) {
  3446. $arrInfoStock[$key]['STOCK_MINIMO'] = 'Sin Definir';
  3447. }
  3448. if (is_null($info['STOCK_MAXIMO'])) {
  3449. $arrInfoStock[$key]['STOCK_MAXIMO'] = 'Sin Definir';
  3450. }
  3451. $arrInfoStock[$key]['ESTADO'] = $stateStock;
  3452. }
  3453. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrInfoStock);
  3454. }
  3455. public function updateStockMaxMin(Request $request, $idInfo) {
  3456. $validator = Validator::make($request->all(), [
  3457. 'STOCK_MINIMO' => 'required|integer',
  3458. 'STOCK_MAXIMO' => 'required|integer',
  3459. 'USUARIO' => 'required|string',
  3460. 'NUMERO_LINEA' => 'required|integer',
  3461. ]);
  3462. if($validator->fails()){
  3463. return $this->responseController->makeResponse(
  3464. true,
  3465. "Se encontraron uno o más errores.",
  3466. $this->responseController->makeErrors(
  3467. $validator->errors()->messages()
  3468. ),
  3469. 401
  3470. );
  3471. }
  3472. DB::beginTransaction();
  3473. $requestData = $request->all();
  3474. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  3475. if ($arrResponseCheckUser['error']) {
  3476. DB::rollBack();
  3477. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3478. }
  3479. $user = $arrResponseCheckUser['response'];
  3480. $idInfo = $this->encController->decrypt($idInfo);
  3481. if (is_null($idInfo)) {
  3482. DB::rollBack();
  3483. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID de la información.', [], 401);
  3484. }
  3485. try {
  3486. $validateExist = DB::table('S002V01TINST')
  3487. ->where('INST_NULI', '=', $requestData['NUMERO_LINEA'])
  3488. ->where('INST_IDIS', '=', $idInfo)
  3489. ->where('INST_ESTA', '=', 'Activo')
  3490. ->exists();
  3491. } catch (\Throwable $th) {
  3492. DB::rollBack();
  3493. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si existe el artículo.', $th->getMessage(), 500);
  3494. }
  3495. if (!$validateExist) {
  3496. DB::rollBack();
  3497. return $this->responseController->makeResponse(true, 'El artículo no existe.', [], 401);
  3498. }
  3499. try {
  3500. $stockMin = intval($requestData['STOCK_MINIMO']);
  3501. } catch (\Throwable $th) {
  3502. DB::rollBack();
  3503. return $this->responseController->makeResponse(true, 'Ocurrió un error con el formato del stock mínimo.', $th->getMessage(), 500);
  3504. }
  3505. try {
  3506. $stockMax = intval($requestData['STOCK_MAXIMO']);
  3507. } catch (\Throwable $th) {
  3508. DB::rollBack();
  3509. return $this->responseController->makeResponse(true, 'Ocurrió un error con el formato del stock mínimo.', $th->getMessage(), 500);
  3510. }
  3511. if ($stockMin < 0) {
  3512. DB::rollBack();
  3513. return $this->responseController->makeResponse(true, 'La cantidad del stock mínimo no debe ser menor a 0.', [], 401);
  3514. }
  3515. if ($stockMax < 0) {
  3516. DB::rollBack();
  3517. return $this->responseController->makeResponse(true, 'La cantidad del stock máximo no debe ser menos a 0.', [], 401);
  3518. }
  3519. if ($stockMin > $stockMax) {
  3520. DB::rollBack();
  3521. return $this->responseController->makeResponse(true, 'La cantidad del stock mínimo no debe ser mayos al stock máximo.', [], 401);
  3522. }
  3523. $now = $this->functionsController->now();
  3524. $currentDate = $now->toDateTimeString();
  3525. try {
  3526. $validateUpdate = DB::table('S002V01TINST')
  3527. ->where('INST_NULI', '=', $requestData['NUMERO_LINEA'])
  3528. ->where('INST_IDIS', '=', $idInfo)
  3529. ->where('INST_ESTA', '=', 'Activo')
  3530. ->update([
  3531. 'INST_STMI' => $stockMin,
  3532. 'INST_STMA' => $stockMax,
  3533. 'INST_USMO' => $user,
  3534. 'INST_FEMO' => $currentDate,
  3535. 'INST_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  3536. ]);
  3537. } catch (\Throwable $th) {
  3538. DB::rollBack();
  3539. return $this->responseController->makeResponse(true, 'Ocurrió un error al modificar el stock mínimo y máximo.', $th->getMessage(), 500);
  3540. }
  3541. if (!$validateUpdate) {
  3542. DB::rollBack();
  3543. return $this->responseController->makeResponse(true, 'No se pudo modificar el stock mínimo y máximo.', [], 401);
  3544. }
  3545. DB::commit();
  3546. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  3547. }
  3548. public function getStockBarcode($user, $line) {
  3549. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  3550. if ($arrResponseCheckUser['error']) {
  3551. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3552. }
  3553. try {
  3554. $arrStock = DB::table('S002V01TSTAR')
  3555. ->where('STAR_NULI', '=', $line)
  3556. ->where('STAR_ESTA', '=', 'Activo')
  3557. ->where('INST_NULI', '=', $line)
  3558. ->where('INST_ESTA', '=', 'Activo')
  3559. ->where('UBAR_NULI', '=', $line)
  3560. ->where('UBAR_ESTA', '=', 'Activo')
  3561. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  3562. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  3563. ->get([
  3564. 'STAR_IDST AS ID_STOCK',
  3565. 'INST_MODE AS MODELO',
  3566. 'INST_COMO AS CODIGO_MODELO',
  3567. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  3568. 'STAR_COBA AS CODIGO_BARRA',
  3569. 'INST_IMAG AS IMAGENES',
  3570. ]);
  3571. $arrStock = json_decode(json_encode($arrStock), true);
  3572. } catch (\Throwable $th) {
  3573. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  3574. }
  3575. foreach ($arrStock as $key => $info) {
  3576. $arrImages = json_decode($info['IMAGENES']);
  3577. $arrUrlImage = array();
  3578. foreach ($arrImages as $keyImages => $images) {
  3579. $images = $this->encController->encrypt($images);
  3580. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  3581. if ($responseDocument['error']) {
  3582. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  3583. }
  3584. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  3585. }
  3586. $arrStock[$key]['IMAGENES'] = $arrUrlImage;
  3587. }
  3588. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrStock);
  3589. }
  3590. public function updateStockBarcode(Request $request, $idStock) {
  3591. $validator = Validator::make($request->all(), [
  3592. 'BARCODE' => 'required|string',
  3593. 'USUARIO' => 'required|string',
  3594. 'NUMERO_LINEA' => 'required|integer',
  3595. ]);
  3596. if($validator->fails()){
  3597. return $this->responseController->makeResponse(
  3598. true,
  3599. "Se encontraron uno o más errores.",
  3600. $this->responseController->makeErrors(
  3601. $validator->errors()->messages()
  3602. ),
  3603. 401
  3604. );
  3605. }
  3606. DB::beginTransaction();
  3607. $requestData = $request->all();
  3608. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  3609. if ($arrResponseCheckUser['error']) {
  3610. DB::rollBack();
  3611. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3612. }
  3613. $user = $arrResponseCheckUser['response'];
  3614. $idStock = $this->encController->decrypt($idStock);
  3615. if (is_null($idStock)) {
  3616. DB::rollBack();
  3617. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID de la información.', [], 401);
  3618. }
  3619. try {
  3620. $validateExists = DB::table('S002V01TSTAR')
  3621. ->where('STAR_IDST', '=', $idStock)
  3622. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  3623. ->where('STAR_ESTA', '=', 'Activo')
  3624. ->exists();
  3625. } catch (\Throwable $th) {
  3626. DB::rollBack();
  3627. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar el ID del stock.', $th->getMessage(), 500);
  3628. }
  3629. if (!$validateExists) {
  3630. DB::rollBack();
  3631. return $this->responseController->makeResponse(true, 'El número de stock no existe.', [], 500);
  3632. }
  3633. $now = $this->functionsController->now();
  3634. $currentDate = $now->toDateTimeString();
  3635. try {
  3636. $validateUpdate = DB::table('S002V01TSTAR')
  3637. ->where('STAR_IDST', '=', $idStock)
  3638. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  3639. ->where('STAR_ESTA', '=', 'Activo')
  3640. ->update([
  3641. 'STAR_COBA' => $requestData['BARCODE'],
  3642. 'STAR_USMO' => $user,
  3643. 'STAR_FEMO' => $currentDate,
  3644. 'STAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  3645. ]);
  3646. } catch (\Throwable $th) {
  3647. DB::rollBack();
  3648. return $this->responseController->makeResponse(true, 'Ocurrió un error modificar el código de barra.', $th->getMessage(), 500);
  3649. }
  3650. if (!$validateUpdate) {
  3651. DB::rollBack();
  3652. return $this->responseController->makeResponse(true, 'No se pudo modificar el código de barra.', [], 500);
  3653. }
  3654. DB::commit();
  3655. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  3656. }
  3657. public function getStockDueDate($user, $line) {
  3658. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  3659. if ($arrResponseCheckUser['error']) {
  3660. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3661. }
  3662. try {
  3663. $arrStock = DB::table('S002V01TSTAR')
  3664. ->where('STAR_NULI', '=', $line)
  3665. ->where('STAR_ESTA', '=', 'Activo')
  3666. ->where('INST_NULI', '=', $line)
  3667. ->where('INST_ESTA', '=', 'Activo')
  3668. ->where('UBAR_NULI', '=', $line)
  3669. ->where('UBAR_ESTA', '=', 'Activo')
  3670. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  3671. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  3672. ->get([
  3673. 'STAR_IDST AS ID_STOCK',
  3674. 'INST_MODE AS MODELO',
  3675. 'INST_COMO AS CODIGO_MODELO',
  3676. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  3677. 'STAR_FEVE AS FECHA_VENCIMIENTO',
  3678. 'INST_IMAG AS IMAGENES',
  3679. ]);
  3680. $arrStock = json_decode(json_encode($arrStock), true);
  3681. } catch (\Throwable $th) {
  3682. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  3683. }
  3684. foreach ($arrStock as $key => $info) {
  3685. $arrImages = json_decode($info['IMAGENES']);
  3686. $arrUrlImage = array();
  3687. foreach ($arrImages as $keyImages => $images) {
  3688. $images = $this->encController->encrypt($images);
  3689. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  3690. if ($responseDocument['error']) {
  3691. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  3692. }
  3693. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  3694. }
  3695. $arrStock[$key]['IMAGENES'] = $arrUrlImage;
  3696. if (is_null($info['FECHA_VENCIMIENTO'])) {
  3697. $arrStock[$key]['FECHA_VENCIMIENTO'] = 'Ninguno';
  3698. }
  3699. }
  3700. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrStock);
  3701. }
  3702. public function updateDueDateStock(Request $request, $idStock) {
  3703. $validator = Validator::make($request->all(), [
  3704. 'FECHA_VENCIMIENTO' => 'required|string',
  3705. 'USUARIO' => 'required|string',
  3706. 'NUMERO_LINEA' => 'required|integer',
  3707. ]);
  3708. if($validator->fails()){
  3709. return $this->responseController->makeResponse(
  3710. true,
  3711. "Se encontraron uno o más errores.",
  3712. $this->responseController->makeErrors(
  3713. $validator->errors()->messages()
  3714. ),
  3715. 401
  3716. );
  3717. }
  3718. DB::beginTransaction();
  3719. $requestData = $request->all();
  3720. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  3721. if ($arrResponseCheckUser['error']) {
  3722. DB::rollBack();
  3723. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3724. }
  3725. $user = $arrResponseCheckUser['response'];
  3726. $idStock = $this->encController->decrypt($idStock);
  3727. if (is_null($idStock)) {
  3728. DB::rollBack();
  3729. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID de la información.', [], 401);
  3730. }
  3731. try {
  3732. $validateExists = DB::table('S002V01TSTAR')
  3733. ->where('STAR_IDST', '=', $idStock)
  3734. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  3735. ->where('STAR_ESTA', '=', 'Activo')
  3736. ->exists();
  3737. } catch (\Throwable $th) {
  3738. DB::rollBack();
  3739. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar el ID del stock.', $th->getMessage(), 500);
  3740. }
  3741. if (!$validateExists) {
  3742. DB::rollBack();
  3743. return $this->responseController->makeResponse(true, 'El número de stock no existe.', [], 500);
  3744. }
  3745. $now = $this->functionsController->now();
  3746. $currentDate = $now->toDateTimeString();
  3747. try {
  3748. $validateUpdate = DB::table('S002V01TSTAR')
  3749. ->where('STAR_IDST', '=', $idStock)
  3750. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  3751. ->where('STAR_ESTA', '=', 'Activo')
  3752. ->update([
  3753. 'STAR_FEVE' => $requestData['FECHA_VENCIMIENTO'],
  3754. 'STAR_USMO' => $user,
  3755. 'STAR_FEMO' => $currentDate,
  3756. 'STAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  3757. ]);
  3758. } catch (\Throwable $th) {
  3759. DB::rollBack();
  3760. return $this->responseController->makeResponse(true, 'Ocurrió un error modificar la fecha de vencimiento.', $th->getMessage(), 500);
  3761. }
  3762. if (!$validateUpdate) {
  3763. DB::rollBack();
  3764. return $this->responseController->makeResponse(true, 'No se pudo modificar la fecha de vencimiento.', [], 500);
  3765. }
  3766. DB::commit();
  3767. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  3768. }
  3769. public function getDescriptionSheetStock($user, $line) {
  3770. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  3771. if ($arrResponseCheckUser['error']) {
  3772. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3773. }
  3774. try {
  3775. $arrStock = DB::table('S002V01TSTAR')
  3776. ->where('STAR_NULI', '=', $line)
  3777. ->where('STAR_ESTA', '=', 'Activo')
  3778. ->where('INST_NULI', '=', $line)
  3779. ->where('INST_ESTA', '=', 'Activo')
  3780. ->where('UBAR_NULI', '=', $line)
  3781. ->where('UBAR_ESTA', '=', 'Activo')
  3782. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  3783. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  3784. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  3785. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  3786. ->get([
  3787. 'STAR_IDST AS ID_STOCK',
  3788. 'INST_MODE AS MODELO',
  3789. 'INST_COMO AS CODIGO_MODELO',
  3790. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  3791. 'FAMI_NOFA AS FAMILIA',
  3792. 'SUBF_NOSU AS SUBFAMILIA',
  3793. 'INST_IMAG AS IMAGENES',
  3794. ]);
  3795. $arrStock = json_decode(json_encode($arrStock), true);
  3796. } catch (\Throwable $th) {
  3797. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  3798. }
  3799. foreach ($arrStock as $key => $info) {
  3800. $arrImages = json_decode($info['IMAGENES']);
  3801. $arrUrlImage = array();
  3802. foreach ($arrImages as $keyImages => $images) {
  3803. $images = $this->encController->encrypt($images);
  3804. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  3805. if ($responseDocument['error']) {
  3806. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  3807. }
  3808. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  3809. }
  3810. $arrStock[$key]['IMAGENES'] = $arrUrlImage;
  3811. }
  3812. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrStock);
  3813. }
  3814. public function createDescriptionSheetStockXLS($idStock, $user, $line) {
  3815. $idStock = $this->encController->decrypt($idStock);
  3816. if (is_null($idStock)) {
  3817. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID del Stock.', [], 401);
  3818. }
  3819. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  3820. if ($arrResponseCheckUser['error']) {
  3821. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3822. }
  3823. $user = $arrResponseCheckUser['response'];
  3824. try {
  3825. $arrStock = (array) DB::table('S002V01TSTAR')
  3826. ->where('STAR_IDST', '=', $idStock)
  3827. ->where('STAR_NULI', '=', $line)
  3828. ->where('INST_NULI', '=', $line)
  3829. ->where('UBAR_NULI', '=', $line)
  3830. ->where('INST_ESTA', '=', 'Activo')
  3831. ->where('UBAR_ESTA', '=', 'Activo')
  3832. ->where('STAR_ESTA', '=', 'Activo')
  3833. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  3834. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  3835. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  3836. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  3837. ->join('S002V01TALMA', 'ALMA_COAL', '=', 'UBAR_COAL')
  3838. ->join('S002V01TAREA', 'AREA_COAR', '=', 'UBAR_COAR')
  3839. ->join('S002V01TNIVE', 'NIVE_CONI', '=', 'UBAR_CONI')
  3840. ->join('S002V01TZONA', 'ZONA_COZO', '=', 'UBAR_COZO')
  3841. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'STAR_NUPR')
  3842. ->first([
  3843. 'FAMI_COFA AS CODIGO_FAMILIA',
  3844. 'FAMI_NOFA AS FAMILIA',
  3845. 'SUBF_COSU AS CODIGO_SUBFAMILIA',
  3846. 'SUBF_NOSU AS SUBFAMILIA',
  3847. 'INST_MODE AS MODELO',
  3848. 'INST_COMO AS CODIGO_MODELO',
  3849. 'INST_STMI AS STOCK_MINIMO',
  3850. 'INST_STMA AS STOCK_MAXIMO',
  3851. 'STAR_IDST AS ID_STOCK',
  3852. 'STAR_COBA AS CODIGO_BARRAS',
  3853. 'STAR_FEVE AS FECHA_VENCIMIENTO',
  3854. 'STAR_NUPR AS NUMERO_PROVEEDOR',
  3855. 'PROV_NOCO AS PROVEEDOR',
  3856. 'STAR_TIAD AS TIPO_ADQUISICION',
  3857. 'STAR_CONS AS CONSUMIBLE',
  3858. 'STAR_REPA AS REPARABLE',
  3859. 'STAR_ESTA AS ESTADO',
  3860. 'UBAR_IDUB AS ID_UBICACION',
  3861. 'ALMA_COAL AS CODIGO_ALMACEN',
  3862. 'ALMA_NOAL AS NOMBRE_ALMACEN',
  3863. 'AREA_COAR AS CODIGO_AREA',
  3864. 'AREA_NOAR AS NOMBRE_AREA',
  3865. 'NIVE_CONI AS CODIGO_NIVEL',
  3866. 'NIVE_NONI AS NOMBRE_NIVEL',
  3867. 'ZONA_COZO AS CODIGO_ZONA',
  3868. 'ZONA_NOZO AS NOMBRE_ZONA',
  3869. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  3870. ]);
  3871. } catch (\Throwable $th) {
  3872. return $this->responseController->makeResponse(true, "Ocurrió un error al recuperar la información.", $th->getMessage(), 500);
  3873. }
  3874. if (empty($arrStock) === 0) {
  3875. return $this->responseController->makeResponse(true, "No se encontró información del stock.", [], 500);
  3876. }
  3877. $spreadsheet = new Spreadsheet();
  3878. $activeWorksheet = $spreadsheet->getActiveSheet()->setTitle($arrStock['ID_STOCK']);
  3879. $cell = 1;
  3880. $activeWorksheet->setCellValue("A$cell", 'Código del Equipamiento');
  3881. $activeWorksheet->setCellValue("B$cell", $arrStock['CODIGO_EQUIPAMIENTO']);
  3882. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3883. $cell++;
  3884. $activeWorksheet->setCellValue("A$cell", 'Familia');
  3885. $activeWorksheet->setCellValue("B$cell", $arrStock['FAMILIA'].' ('.$arrStock['CODIGO_FAMILIA'].')');
  3886. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3887. $cell++;
  3888. $activeWorksheet->setCellValue("A$cell", 'Subfamilia');
  3889. $activeWorksheet->setCellValue("B$cell", $arrStock['SUBFAMILIA'].' ('.$arrStock['CODIGO_SUBFAMILIA'].')');
  3890. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3891. $cell++;
  3892. $activeWorksheet->setCellValue("A$cell", 'Modelo');
  3893. $activeWorksheet->setCellValue("B$cell", $arrStock['MODELO']);
  3894. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3895. $cell++;
  3896. $activeWorksheet->setCellValue("A$cell", 'Código Modelo');
  3897. $activeWorksheet->setCellValue("B$cell", $arrStock['CODIGO_MODELO']);
  3898. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3899. $cell++;
  3900. $activeWorksheet->setCellValue("A$cell", 'Tipo de Adquisición');
  3901. $activeWorksheet->setCellValue("B$cell", $arrStock['TIPO_ADQUISICION']);
  3902. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3903. $cell++;
  3904. if (!is_null($arrStock['NUMERO_PROVEEDOR'])) {
  3905. $activeWorksheet->setCellValue("A$cell", 'Proveedor');
  3906. $activeWorksheet->setCellValue("B$cell", $arrStock['PROVEEDOR'].' ('.$arrStock['NUMERO_PROVEEDOR'].')');
  3907. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3908. $cell++;
  3909. }
  3910. if (!is_null($arrStock['FECHA_VENCIMIENTO'])) {
  3911. $activeWorksheet->setCellValue("A$cell", 'Fecha de Vencimiento');
  3912. $activeWorksheet->setCellValue("B$cell", $arrStock['FECHA_VENCIMIENTO']);
  3913. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3914. $cell++;
  3915. }
  3916. $activeWorksheet->setCellValue("A$cell", 'Código de Barras');
  3917. $activeWorksheet->setCellValue("B$cell", $arrStock['CODIGO_BARRAS']);
  3918. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3919. $cell++;
  3920. if ( array_key_exists('TASA_ROTACION', $arrStock) ) {
  3921. $activeWorksheet->setCellValue("A$cell", 'Tasa de Rotación');
  3922. $activeWorksheet->setCellValue("B$cell", $arrStock['TASA_ROTACION']);
  3923. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3924. $cell++;
  3925. }
  3926. $activeWorksheet->setCellValue("A$cell", 'Stock Mínimo');
  3927. $activeWorksheet->setCellValue("B$cell", is_null($arrStock['STOCK_MINIMO']) ? 'Sin Definir' : $arrStock['STOCK_MINIMO']);
  3928. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3929. $cell++;
  3930. $activeWorksheet->setCellValue("A$cell", 'Stock Máximo');
  3931. $activeWorksheet->setCellValue("B$cell", is_null($arrStock['STOCK_MAXIMO']) ? 'Sin Definir' : $arrStock['STOCK_MAXIMO']);
  3932. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3933. $cell++;
  3934. $activeWorksheet->setCellValue("A$cell", '¿Es consumible?');
  3935. $activeWorksheet->setCellValue("B$cell", $arrStock['CONSUMIBLE']);
  3936. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3937. $cell++;
  3938. $activeWorksheet->setCellValue("A$cell", '¿Es reparable?');
  3939. $activeWorksheet->setCellValue("B$cell", $arrStock['REPARABLE']);
  3940. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3941. $cell++;
  3942. $activeWorksheet->getColumnDimension("A")->setAutoSize(true);
  3943. $activeWorksheet->getColumnDimension("B")->setAutoSize(true);
  3944. $nuli = $this->resourcesController->formatSecuence($line, 2);
  3945. $como = 'GIST'; // Código del módulo
  3946. $cldo = 'IN'; // Código de la clasificación
  3947. $fecr = date('ymd'); // Fecha en la se carga el archivo
  3948. try {
  3949. $arrSecuence = (array) DB::table('S002V01TAFAL')
  3950. ->where('AFAL_COMO', '=', $como)
  3951. ->where('AFAL_CLDO', '=', $cldo)
  3952. ->where('AFAL_NULI', '=', $line)
  3953. ->orderBy('AFAL_NUSE', 'desc')
  3954. ->first([ 'AFAL_NUSE' ]);
  3955. } catch (\Throwable $th) {
  3956. return $this->responseController->makeResponse(
  3957. true,
  3958. "ERR_PROVIDER_SHEET020: Ocurrió un error al obtener la información de la secuencia.",
  3959. $th->getMessage(),
  3960. 500
  3961. );
  3962. }
  3963. $nuse = 1; // Secuencia del documento
  3964. if ( !empty( $arrSecuence ) && !is_null( $arrSecuence ) ) {
  3965. $nuse = intval( $arrSecuence['AFAL_NUSE'] ) + 1;
  3966. }
  3967. $nuse = $this->resourcesController->formatSecuence($nuse, 6);
  3968. $nuve = $this->resourcesController->formatSecuence('1', 2);
  3969. $noar = 'ficha_de_stock_'.$arrStock['ID_STOCK'];
  3970. $exte = 'xlsx';
  3971. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  3972. $filePath = $this->resourcesController->pathService.'\\public\\public_files\\'; // API JEAN
  3973. } else {
  3974. $filePath = $this->resourcesController->pathService.'\\public_files\\'; // API QA
  3975. }
  3976. $fileName = $nuli.'-'.$como.'-'.$cldo.'-'.$fecr.'-'.$nuse.'='.$nuve.'='.$noar.'.'.$exte;
  3977. $tempFile = $filePath.$fileName;
  3978. if ( file_exists( $tempFile ) ) {
  3979. if ( !unlink( $tempFile ) ) {
  3980. return $this->responseController->makeResponse(
  3981. true,
  3982. "ERR_PROVIDER_SHEET021: Ocurrió un error al eliminar el siguiente archivo: " . $tempFile,
  3983. [],
  3984. 500
  3985. );
  3986. }
  3987. }
  3988. try {
  3989. $writer = new Xlsx($spreadsheet);
  3990. ob_start();
  3991. $writer->save('php://output');
  3992. $base64 = base64_encode(ob_get_clean());
  3993. $validate = \File::put( $tempFile, base64_decode($base64));
  3994. } catch (\Throwable $th) {
  3995. return $this->responseController->makeResponse(
  3996. true,
  3997. "ERR_PROVIDER_SHEET022: Ocurrió un error al guardar el documento.",
  3998. $th->getMessage(),
  3999. 500
  4000. );
  4001. }
  4002. $ubic = Storage::putFile('files', new File($tempFile));
  4003. $ubic = str_replace("/", "\\", $ubic);
  4004. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  4005. $ubic = $this->resourcesController->pathService."\\storage\\app\\" . $ubic;
  4006. } else {
  4007. $ubic = $this->resourcesController->pathService."\\storage\\app\\" . $ubic;
  4008. }
  4009. $tama = filesize($ubic);
  4010. $usac = json_encode([$user]);
  4011. $now = $this->functionsController->now();
  4012. $currentDate = $now->toDateTimeString();
  4013. try {
  4014. $validateInsert = DB::table('S002V01TAFAL')->insert([
  4015. 'AFAL_NULI' => $line,
  4016. 'AFAL_COMO' => $como,
  4017. 'AFAL_CLDO' => $cldo,
  4018. 'AFAL_FECR' => $fecr,
  4019. 'AFAL_NUSE' => $nuse,
  4020. 'AFAL_NUVE' => $nuve,
  4021. 'AFAL_NOAR' => $noar,
  4022. 'AFAL_EXTE' => $exte,
  4023. 'AFAL_TAMA' => $tama,
  4024. 'AFAL_UBIC' => $ubic,
  4025. 'AFAL_USAC' => $usac,
  4026. 'AFAL_USRE' => $user,
  4027. 'AFAL_FERE' => $currentDate,
  4028. ]);
  4029. } catch (\Throwable $th) {
  4030. return $this->responseController->makeResponse(
  4031. true,
  4032. "ERR_PROVIDER_SHEET023: Ocurrió un error guardar los datos a la tabla final de archivos.",
  4033. $th->getMessage(),
  4034. 500
  4035. );
  4036. }
  4037. if ( !$validateInsert ) {
  4038. return $this->responseController->makeResponse(
  4039. true,
  4040. "ERR_PROVIDER_SHEET024: No se pudo guardar la ficha del proveedor en la base de datos.",
  4041. [],
  4042. 500
  4043. );
  4044. }
  4045. $encCode = $this->encController->encrypt($fileName);
  4046. try {
  4047. $validateInsert = DB::table('S002V01TDOST')->insert([
  4048. 'DOST_NULI' => $line,
  4049. 'DOST_IDST' => $idStock,
  4050. 'DOST_NODO' => 'Ficha de Stock #'.$arrStock['ID_STOCK'],
  4051. 'DOST_CODO' => $encCode,
  4052. 'DOST_EXTE' => $exte,
  4053. 'DOST_TAMA' => $tama,
  4054. 'DOST_CLAS' => 'IN',
  4055. 'DOST_VERS' => 1,
  4056. 'DOST_USRE' => $user,
  4057. 'DOST_FERE' => $currentDate,
  4058. 'DOST_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  4059. ]);
  4060. } catch (\Throwable $th) {
  4061. return $this->responseController->makeResponse(
  4062. true,
  4063. "Ocurrió un error guardar los datos a la tabla final de archivos.",
  4064. $th->getMessage(),
  4065. 500
  4066. );
  4067. }
  4068. if (!$validateInsert) {
  4069. return $this->responseController->makeResponse(
  4070. true,
  4071. "No se pudo guardar la ficha del proveedor en la base de datos.",
  4072. [],
  4073. 500
  4074. );
  4075. }
  4076. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  4077. $urlPublic = 'http://192.168.100.105:8000/public_files/' . $fileName;
  4078. } else {
  4079. $urlPublic = $this->functionsController->getApiURI().'sam/public_files/' . $fileName;
  4080. }
  4081. return $this->responseController->makeResponse(false, "EXITO: Modificación Exitosa", [ 'url' => $urlPublic ]);
  4082. }
  4083. public function createDescriptionSheetStockPDF($idStock, $user, $line) {
  4084. $idStock = $this->encController->decrypt($idStock);
  4085. if (is_null($idStock)) {
  4086. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID del Stock.', [], 401);
  4087. }
  4088. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  4089. if ($arrResponseCheckUser['error']) {
  4090. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  4091. }
  4092. $user = $arrResponseCheckUser['response'];
  4093. try {
  4094. $arrStock = (array) DB::table('S002V01TSTAR')
  4095. ->where('STAR_IDST', '=', $idStock)
  4096. ->where('STAR_NULI', '=', $line)
  4097. ->where('INST_NULI', '=', $line)
  4098. ->where('UBAR_NULI', '=', $line)
  4099. ->where('INST_ESTA', '=', 'Activo')
  4100. ->where('UBAR_ESTA', '=', 'Activo')
  4101. ->where('STAR_ESTA', '=', 'Activo')
  4102. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  4103. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  4104. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  4105. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  4106. ->join('S002V01TALMA', 'ALMA_COAL', '=', 'UBAR_COAL')
  4107. ->join('S002V01TAREA', 'AREA_COAR', '=', 'UBAR_COAR')
  4108. ->join('S002V01TNIVE', 'NIVE_CONI', '=', 'UBAR_CONI')
  4109. ->join('S002V01TZONA', 'ZONA_COZO', '=', 'UBAR_COZO')
  4110. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'STAR_NUPR')
  4111. ->first([
  4112. 'FAMI_COFA AS CODIGO_FAMILIA',
  4113. 'FAMI_NOFA AS FAMILIA',
  4114. 'SUBF_COSU AS CODIGO_SUBFAMILIA',
  4115. 'SUBF_NOSU AS SUBFAMILIA',
  4116. 'INST_MODE AS MODELO',
  4117. 'INST_COMO AS CODIGO_MODELO',
  4118. 'INST_STMI AS STOCK_MINIMO',
  4119. 'INST_STMA AS STOCK_MAXIMO',
  4120. 'STAR_IDST AS ID_STOCK',
  4121. 'STAR_COBA AS CODIGO_BARRAS',
  4122. 'STAR_FEVE AS FECHA_VENCIMIENTO',
  4123. 'PROV_NUPR AS NUMERO_PROVEEDOR',
  4124. 'PROV_NOCO AS PROVEEDOR',
  4125. 'STAR_TIAD AS TIPO_ADQUISICION',
  4126. 'STAR_CONS AS CONSUMIBLE',
  4127. 'STAR_REPA AS REPARABLE',
  4128. 'STAR_ESTA AS ESTADO',
  4129. 'UBAR_IDUB AS ID_UBICACION',
  4130. 'ALMA_COAL AS CODIGO_ALMACEN',
  4131. 'ALMA_NOAL AS NOMBRE_ALMACEN',
  4132. 'AREA_COAR AS CODIGO_AREA',
  4133. 'AREA_NOAR AS NOMBRE_AREA',
  4134. 'NIVE_CONI AS CODIGO_NIVEL',
  4135. 'NIVE_NONI AS NOMBRE_NIVEL',
  4136. 'ZONA_COZO AS CODIGO_ZONA',
  4137. 'ZONA_NOZO AS NOMBRE_ZONA',
  4138. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  4139. ]);
  4140. } catch (\Throwable $th) {
  4141. return $this->responseController->makeResponse(true, "Ocurrió un error al recuperar la información.", $th->getMessage(), 500);
  4142. }
  4143. if (empty($arrStock) === 0) {
  4144. return $this->responseController->makeResponse(true, "No se encontró información del stock.", [], 500);
  4145. }
  4146. $html = '<!DOCTYPE html>
  4147. <html lang="en">
  4148. <head>
  4149. <meta charset="UTF-8">
  4150. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  4151. <title>Document</title>
  4152. <style>
  4153. body {
  4154. font-family: Arial, Helvetica, sans-serif;
  4155. font-size: 8pt;
  4156. }
  4157. .section-title {
  4158. margin: 20px 0;
  4159. font-size: 12pt;
  4160. }
  4161. table {
  4162. width: 100%;
  4163. font-size: 8pt;
  4164. }
  4165. thead tr th {
  4166. text-align: start;
  4167. padding: 0 16px;
  4168. height: 52px;
  4169. border-bottom-width: 1px;
  4170. border-bottom-style: solid;
  4171. border-bottom-color: #CCC;
  4172. }
  4173. tbody tr td {
  4174. padding: 0 16px;
  4175. height: 52px;
  4176. border-bottom-width: 1px;
  4177. border-bottom-style: solid;
  4178. border-bottom-color: #CCC;
  4179. }
  4180. p {
  4181. text-align: justify;
  4182. }
  4183. </style>
  4184. </head>
  4185. <body>';
  4186. $html.='<div class="item">
  4187. <b>Código del Equipamiento</b>
  4188. <span>'.$arrStock['CODIGO_EQUIPAMIENTO'].'</span>
  4189. </div>';
  4190. $html.='<div class="item">
  4191. <b>Familia</b>
  4192. <span>'.$arrStock['FAMILIA'].' ('.$arrStock['CODIGO_FAMILIA'].')'.'</span>
  4193. </div>';
  4194. $html.='<div class="item">
  4195. <b>Subfamilia</b>
  4196. <span>'.$arrStock['SUBFAMILIA'].' ('.$arrStock['CODIGO_SUBFAMILIA'].')'.'</span>
  4197. </div>';
  4198. $html.='<div class="item">
  4199. <b>Modelo</b>
  4200. <span>'.$arrStock['MODELO'].'</span>
  4201. </div>';
  4202. $html.='<div class="item">
  4203. <b>Código Modelo</b>
  4204. <span>'.$arrStock['CODIGO_MODELO'].'</span>
  4205. </div>';
  4206. $html.='<div class="item">
  4207. <b>Tipo de Adquisición</b>
  4208. <span>'.$arrStock['TIPO_ADQUISICION'].'</span>
  4209. </div>';
  4210. $html.='<div class="item">
  4211. <b>Proveedor</b>
  4212. <span>'.$arrStock['PROVEEDOR'].' ('.$arrStock['NUMERO_PROVEEDOR'].')'.'</span>
  4213. </div>';
  4214. $html.='<div class="item">
  4215. <b>Fecha de Vencimiento</b>
  4216. <span>'.$arrStock['FECHA_VENCIMIENTO'].'</span>
  4217. </div>';
  4218. $html.='<div class="item">
  4219. <b>Código de Barras</b>
  4220. <span>'.$arrStock['CODIGO_BARRAS'].'</span>
  4221. </div>';
  4222. if ( array_key_exists('TASA_ROTACION', $arrStock) ) {
  4223. $html.='<div class="item">
  4224. <b>Tasa de Rotación</b>
  4225. <span>'.$arrStock['TASA_ROTACION'].'</span>
  4226. </div>';
  4227. }
  4228. $html.='<div class="item">
  4229. <b>Stock Mínimo</b>
  4230. <span>'.is_null($arrStock['STOCK_MINIMO']) ? 'Sin Definir' : $arrStock['STOCK_MINIMO'].'</span>
  4231. </div>';
  4232. $html.='<div class="item">
  4233. <b>Stock Máximo</b>
  4234. <span>'.is_null($arrStock['STOCK_MAXIMO']) ? 'Sin Definir' : $arrStock['STOCK_MAXIMO'].'</span>
  4235. </div>';
  4236. $html.='<div class="item">
  4237. <b>¿Es consumible?</b>
  4238. <span>'.$arrStock['CONSUMIBLE'].'</span>
  4239. </div>';
  4240. $html.='<div class="item">
  4241. <b>¿Es reparable?</b>
  4242. <span>'.$arrStock['REPARABLE'].'</span>
  4243. </div>';
  4244. $html.='</body></html>';
  4245. $nuli = $this->resourcesController->formatSecuence($line, 2);
  4246. $como = 'GIST'; // Código del módulo
  4247. $cldo = 'IN'; // Código de la clasificación
  4248. $fecr = date('ymd'); // Fecha en la se carga el archivo
  4249. try {
  4250. $arrSecuence = (array) DB::table('S002V01TAFAL')
  4251. ->where('AFAL_COMO', '=', $como)
  4252. ->where('AFAL_CLDO', '=', $cldo)
  4253. ->where('AFAL_NULI', '=', $line)
  4254. ->orderBy('AFAL_NUSE', 'desc')
  4255. ->first([ 'AFAL_NUSE' ]);
  4256. } catch (\Throwable $th) {
  4257. return $this->responseController->makeResponse(
  4258. true,
  4259. "ERR_PROVIDER_SHEET020: Ocurrió un error al obtener la información de la secuencia.",
  4260. $th->getMessage(),
  4261. 500
  4262. );
  4263. }
  4264. $nuse = 1; // Secuencia del documento
  4265. if ( !empty( $arrSecuence ) && !is_null( $arrSecuence ) ) {
  4266. $nuse = intval( $arrSecuence['AFAL_NUSE'] ) + 1;
  4267. }
  4268. $nuse = $this->resourcesController->formatSecuence($nuse, 6);
  4269. $nuve = $this->resourcesController->formatSecuence('1', 2);
  4270. $noar = 'ficha_de_adquisición_' . $arrStock['ID_STOCK'];
  4271. $nuli = $this->resourcesController->formatSecuence($line, 2);
  4272. $como = 'GIST'; // Código del módulo
  4273. $cldo = 'IN'; // Código de la clasificación
  4274. $fecr = date('ymd'); // Fecha en la se carga el archivo
  4275. try {
  4276. $arrSecuence = (array) DB::table('S002V01TAFAL')
  4277. ->where('AFAL_COMO', '=', $como)
  4278. ->where('AFAL_CLDO', '=', $cldo)
  4279. ->where('AFAL_NULI', '=', $line)
  4280. ->orderBy('AFAL_NUSE', 'desc')
  4281. ->first([ 'AFAL_NUSE' ]);
  4282. } catch (\Throwable $th) {
  4283. return $this->responseController->makeResponse(
  4284. true,
  4285. "ERR_PROVIDER_SHEET020: Ocurrió un error al obtener la información de la secuencia.",
  4286. $th->getMessage(),
  4287. 500
  4288. );
  4289. }
  4290. $nuse = 1; // Secuencia del documento
  4291. if ( !empty( $arrSecuence ) && !is_null( $arrSecuence ) ) {
  4292. $nuse = intval( $arrSecuence['AFAL_NUSE'] ) + 1;
  4293. }
  4294. $nuse = $this->resourcesController->formatSecuence($nuse, 6);
  4295. $nuve = $this->resourcesController->formatSecuence('1', 2);
  4296. $noar = 'ficha_de_stock_' . $arrStock['ID_STOCK'];
  4297. $exte = 'pdf';
  4298. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  4299. $filePath = $this->resourcesController->pathService.'\\public\\public_files\\'; // API JEAN
  4300. } else {
  4301. $filePath = $this->resourcesController->pathService.'\\public_files\\'; // API QA
  4302. }
  4303. $fileName = $nuli.'-'.$como.'-'.$cldo.'-'.$fecr.'-'.$nuse.'='.$nuve.'='.$noar.'.'.$exte;
  4304. $tempFile = $filePath . $fileName;
  4305. $dompdf = new Dompdf();
  4306. $dompdf ->loadHtml($html);
  4307. $dompdf->setPaper('A4', 'landscape');
  4308. $dompdf->render();
  4309. $output = $dompdf->output();
  4310. file_put_contents($tempFile, $output);
  4311. $ubic = Storage::putFile('files', new File($tempFile));
  4312. $ubic = str_replace("/", "\\", $ubic);
  4313. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  4314. $ubic = $this->resourcesController->pathService."\\storage\\app\\" . $ubic;
  4315. } else {
  4316. $ubic = $this->resourcesController->pathService."\\storage\\app\\" . $ubic;
  4317. }
  4318. $tama = filesize($ubic);
  4319. $usac = json_encode([$user]);
  4320. $now = $this->functionsController->now();
  4321. $currentDate = $now->toDateTimeString();
  4322. try {
  4323. $validateInsert = DB::table('S002V01TAFAL')->insert([
  4324. 'AFAL_NULI' => $line,
  4325. 'AFAL_COMO' => $como,
  4326. 'AFAL_CLDO' => $cldo,
  4327. 'AFAL_FECR' => $fecr,
  4328. 'AFAL_NUSE' => $nuse,
  4329. 'AFAL_NUVE' => $nuve,
  4330. 'AFAL_NOAR' => $noar,
  4331. 'AFAL_EXTE' => $exte,
  4332. 'AFAL_TAMA' => $tama,
  4333. 'AFAL_UBIC' => $ubic,
  4334. 'AFAL_USAC' => $usac,
  4335. 'AFAL_USRE' => $user,
  4336. 'AFAL_FERE' => $currentDate,
  4337. ]);
  4338. } catch (\Throwable $th) {
  4339. return $this->responseController->makeResponse(
  4340. true,
  4341. "ERR_PROVIDER_SHEET023: Ocurrió un error guardar los datos a la tabla final de archivos.",
  4342. $th->getMessage(),
  4343. 500
  4344. );
  4345. }
  4346. if ( !$validateInsert ) {
  4347. return $this->responseController->makeResponse(
  4348. true,
  4349. "ERR_PROVIDER_SHEET024: No se pudo guardar la ficha del proveedor en la base de datos.",
  4350. [],
  4351. 500
  4352. );
  4353. }
  4354. $encCode = $this->encController->encrypt($fileName);
  4355. try {
  4356. $validateInsert = DB::table('S002V01TDOST')->insert([
  4357. 'DOST_NULI' => $line,
  4358. 'DOST_IDST' => $idStock,
  4359. 'DOST_NODO' => 'Ficha de Stock #'.$arrStock['ID_STOCK'],
  4360. 'DOST_CODO' => $encCode,
  4361. 'DOST_EXTE' => $exte,
  4362. 'DOST_TAMA' => $tama,
  4363. 'DOST_CLAS' => 'IN',
  4364. 'DOST_VERS' => 1,
  4365. 'DOST_USRE' => $user,
  4366. 'DOST_FERE' => $currentDate,
  4367. 'DOST_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  4368. ]);
  4369. } catch (\Throwable $th) {
  4370. return $this->responseController->makeResponse(
  4371. true,
  4372. "Ocurrió un error guardar los datos a la tabla final de archivos.",
  4373. $th->getMessage(),
  4374. 500
  4375. );
  4376. }
  4377. if (!$validateInsert) {
  4378. return $this->responseController->makeResponse(
  4379. true,
  4380. "No se pudo guardar la ficha del proveedor en la base de datos.",
  4381. [],
  4382. 500
  4383. );
  4384. }
  4385. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  4386. $urlPublic = 'http://192.168.100.105:8000/public_files/' . $fileName;
  4387. } else {
  4388. $urlPublic = $this->functionsController->getApiURI().'sam/public_files/' . $fileName;
  4389. }
  4390. return $this->responseController->makeResponse(false, "EXITO: Modificación Exitosa", [ 'url' => $urlPublic ]);
  4391. }
  4392. public function getDocumentAssociation($user, $line) {
  4393. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  4394. if ($arrResponseCheckUser['error']) {
  4395. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  4396. }
  4397. try {
  4398. $arrStock = DB::table('S002V01TSTAR')
  4399. ->where('STAR_NULI', '=', $line)
  4400. ->where('STAR_ESTA', '=', 'Activo')
  4401. ->where('INST_NULI', '=', $line)
  4402. ->where('INST_ESTA', '=', 'Activo')
  4403. ->where('UBAR_NULI', '=', $line)
  4404. ->where('UBAR_ESTA', '=', 'Activo')
  4405. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  4406. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  4407. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  4408. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  4409. ->get([
  4410. 'STAR_IDST AS ID_STOCK',
  4411. 'INST_MODE AS MODELO',
  4412. 'INST_COMO AS CODIGO_MODELO',
  4413. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  4414. 'FAMI_NOFA AS FAMILIA',
  4415. 'SUBF_NOSU AS SUBFAMILIA',
  4416. 'INST_IMAG AS IMAGENES',
  4417. 'STAR_NUPR AS NUMERO_PROVEEDOR'
  4418. ]);
  4419. $arrStock = json_decode(json_encode($arrStock), true);
  4420. } catch (\Throwable $th) {
  4421. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  4422. }
  4423. foreach ($arrStock as $key => $stock) {
  4424. $arrImages = json_decode($stock['IMAGENES']);
  4425. $arrUrlImage = array();
  4426. foreach ($arrImages as $keyImages => $images) {
  4427. $images = $this->encController->encrypt($images);
  4428. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  4429. if ($responseDocument['error']) {
  4430. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  4431. }
  4432. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  4433. }
  4434. $arrStock[$key]['IMAGENES'] = $arrUrlImage;
  4435. try {
  4436. $count = DB::table('S002V01TDOST')
  4437. ->where('DOST_IDST', '=', $stock['ID_STOCK'])
  4438. ->where('DOST_NULI', '=', $line)
  4439. ->where('DOST_ESTA', '=', 'Activo')
  4440. ->count();
  4441. } catch (\Throwable $th) {
  4442. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la cantidad de documentos.', $th->getMessage(), 500);
  4443. }
  4444. $arrStock[$key]['CANTIDAD_DOCUMENTOS'] = $count;
  4445. }
  4446. return $this->responseController->makeResponse(false, "ÉXITO: Consulta ExitosaA", $arrStock);
  4447. }
  4448. public function getDocumentsByStock($idStock, $user, $line) {
  4449. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  4450. if ($arrResponseCheckUser['error']) {
  4451. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  4452. }
  4453. $idStock = $this->encController->decrypt($idStock);
  4454. if (is_null($idStock)) {
  4455. DB::rollBack();
  4456. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID de la información.', [], 401);
  4457. }
  4458. try {
  4459. $validateExists = DB::table('S002V01TSTAR')
  4460. ->where('STAR_IDST', '=', $idStock)
  4461. ->where('STAR_NULI', '=', $line)
  4462. ->where('STAR_ESTA', '=', 'Activo')
  4463. ->exists();
  4464. } catch (\Throwable $th) {
  4465. DB::rollBack();
  4466. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar el ID del stock.', $th->getMessage(), 500);
  4467. }
  4468. if (!$validateExists) {
  4469. DB::rollBack();
  4470. return $this->responseController->makeResponse(true, 'El número de stock no existe.', [], 500);
  4471. }
  4472. try {
  4473. $arrDocuments = DB::table('S002V01TDOST')
  4474. ->where('DOST_IDST', '=', $idStock)
  4475. ->where('DOST_NULI', '=', $line)
  4476. ->get([
  4477. 'DOST_IDDO AS ID_DOCUMENTO',
  4478. 'DOST_CODO AS CODIGO_DOCUMENTO',
  4479. 'DOST_NODO AS NOMBRE_DOCUMENTO',
  4480. 'DOST_CLAS AS CLASIFICACION',
  4481. 'DOST_EXTE AS EXTENSION',
  4482. 'DOST_VERS AS VERSION',
  4483. 'DOST_TAMA AS TAMANO',
  4484. 'DOST_ESTA AS ESTADO',
  4485. 'DOST_USRE AS USUARIO_REGISTRA',
  4486. 'DOST_FERE AS FECHA_REGISTRA',
  4487. 'DOST_USMO AS USUARIO_MODIFICA',
  4488. 'DOST_FEMO AS FECHA_MODIFICA',
  4489. ]);
  4490. $arrDocuments = json_decode(json_encode($arrDocuments), true);
  4491. } catch (\Throwable $th) {
  4492. DB::rollBack();
  4493. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  4494. }
  4495. $responseCheckLatestUpdate = $this->resourcesController->checkLatestUpdate($arrDocuments, $line);
  4496. if ($responseCheckLatestUpdate['error']) {
  4497. return $this->responseController->makeResponse(true, $responseCheckLatestUpdate['msg'], [], 500);
  4498. }
  4499. $arrDocuments = $responseCheckLatestUpdate['response'];
  4500. foreach ($arrDocuments as $keyDocument => $document) {
  4501. foreach ($document as $key => $value) {
  4502. if ($key === 'ID_DOCUMENTO') {
  4503. $document[$key] = '#'.$value;
  4504. }
  4505. if ($key === 'CLASIFICACION') {
  4506. $document[$key] = $this->resourcesController->arrClasificateDocument[$value];
  4507. }
  4508. if ($key === 'TAMANO') {
  4509. $document[$key] = ($value / 1000).' KB';
  4510. }
  4511. }
  4512. $arrDocuments[$keyDocument] = $document;
  4513. }
  4514. return $this->responseController->makeResponse(false, "EXITO: Consulta Exitosa", $arrDocuments);
  4515. }
  4516. public function registerDocumentStock(Request $request) {
  4517. $valitador = Validator::make($request->all(), [
  4518. 'ID_STOCK' => 'required|string',
  4519. 'NOMBRE_ARCHIVO' => 'required|string',
  4520. 'TIPO_ARCHIVO' => 'required|string',
  4521. 'ARCHIVO' => 'required|string',
  4522. 'USUARIO' => 'required|string',
  4523. 'NUMERO_LINEA' => 'required|integer',
  4524. ]);
  4525. if ($valitador->fails()) {
  4526. return $this->responseController->makeResponse(
  4527. true,
  4528. "ERR_DOCUMENT_ORDER_REG000: Se encontraron uno o más errores.",
  4529. $this->responseController->makeErrors($valitador->errors()->messages()),
  4530. 401
  4531. );
  4532. }
  4533. DB::beginTransaction();
  4534. $requestData = $request->all();
  4535. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  4536. if ($arrResponseCheckUser['error']) {
  4537. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  4538. }
  4539. $user = $arrResponseCheckUser['response'];
  4540. try {
  4541. $idFile = $this->encController->decrypt($requestData['ARCHIVO']);
  4542. } catch (\Throwable $th) {
  4543. DB::rollBack();
  4544. return $this->responseController->makeResponse(true, "ERR_DOCUMENT_ORDER_REG002: Ocurrió un error al desencriptar el documento.", $th->getMessage(), 401);
  4545. }
  4546. $tempFile = DB::table('S002V01TARTE')->where([
  4547. ['ARTE_NULI', '=', $requestData['NUMERO_LINEA']],
  4548. ['ARTE_IDAR', '=', $idFile],
  4549. ])->first();
  4550. if(is_null($tempFile)){
  4551. return $this->responseController->makeResponse(true, 'ERR_DOCUMENT_ORDER_REG003: El archivo consultado no está registrado', [], 404);
  4552. }else if($tempFile->ARTE_ESTA == 'Eliminado'){
  4553. return $this->responseController->makeResponse(true, 'ERR_DOCUMENT_ORDER_REG004: El archivo consultado está eliminado', [], 404);
  4554. }
  4555. $fileResponse = $this->documentManagementController->moveFinalFile(
  4556. intval($requestData['NUMERO_LINEA']),
  4557. 'GEAD',
  4558. $requestData['TIPO_ARCHIVO'],
  4559. $tempFile,
  4560. $user,
  4561. );
  4562. if(!$fileResponse[0]){
  4563. return $this->responseController->makeResponse(true, 'ERR_DOCUMENT_ORDER_REG005: '.$fileResponse[1], [], 400);
  4564. }
  4565. $encCode = $this->encController->encrypt($fileResponse[1]);
  4566. $now = $this->functionsController->now();
  4567. $currentDate = $now->toDateTimeString();
  4568. try {
  4569. $validateInsert = DB::table('S002V01TDOST')->insert([
  4570. 'DOST_NULI' => $requestData['NUMERO_LINEA'],
  4571. 'DOST_IDST' => $requestData['ID_STOCK'],
  4572. 'DOST_NODO' => $requestData['NOMBRE_ARCHIVO'],
  4573. 'DOST_CODO' => $encCode,
  4574. 'DOST_EXTE' => $tempFile->ARTE_EXTE,
  4575. 'DOST_TAMA' => $tempFile->ARTE_TAMA,
  4576. 'DOST_CLAS' => $requestData['TIPO_ARCHIVO'],
  4577. 'DOST_VERS' => 1,
  4578. 'DOST_USRE' => $user,
  4579. 'DOST_FERE' => $currentDate,
  4580. 'DOST_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  4581. ]);
  4582. } catch (\Throwable $th) {
  4583. return $this->responseController->makeResponse(
  4584. true,
  4585. "Ocurrió un error guardar los datos a la tabla final de archivos.",
  4586. $th->getMessage(),
  4587. 500
  4588. );
  4589. }
  4590. if (!$validateInsert) {
  4591. return $this->responseController->makeResponse(
  4592. true,
  4593. "No se pudo guardar la ficha del proveedor en la base de datos.",
  4594. [],
  4595. 500
  4596. );
  4597. }
  4598. DB::commit();
  4599. return $this->responseController->makeResponse(false, "ÉXITO: Registro de Documento Exitoso");
  4600. }
  4601. public function getReplenishmentQuantitiesService($user, $line) {
  4602. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  4603. if ($arrResponseCheckUser['error']) {
  4604. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  4605. }
  4606. try {
  4607. $arrStock = DB::table('S002V01TSTAR')
  4608. ->where('STAR_TIAD', '=', 'Por Pedido')
  4609. ->where('STAR_NULI', '=', $line)
  4610. ->where('STAR_ESTA', '=', 'Activo')
  4611. ->where('INST_NULI', '=', $line)
  4612. ->where('INST_ESTA', '=', 'Activo')
  4613. ->where('UBAR_NULI', '=', $line)
  4614. ->where('UBAR_ESTA', '=', 'Activo')
  4615. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  4616. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  4617. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  4618. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  4619. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'STAR_NUPR')
  4620. ->get([
  4621. 'STAR_IDST AS ID_STOCK',
  4622. 'INST_MODE AS MODELO',
  4623. 'INST_COMO AS CODIGO_MODELO',
  4624. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  4625. 'FAMI_NOFA AS FAMILIA',
  4626. 'SUBF_NOSU AS SUBFAMILIA',
  4627. 'INST_IMAG AS IMAGENES',
  4628. 'PROV_NUPR AS NUMERO_PROVEEDOR',
  4629. 'PROV_NOCO AS NOMBRE_PROVEEDOR',
  4630. ]);
  4631. $arrStock = json_decode(json_encode($arrStock), true);
  4632. } catch (\Throwable $th) {
  4633. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  4634. }
  4635. foreach ($arrStock as $key => $info) {
  4636. $arrImages = json_decode($info['IMAGENES']);
  4637. $arrUrlImage = array();
  4638. foreach ($arrImages as $keyImages => $images) {
  4639. $images = $this->encController->encrypt($images);
  4640. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  4641. if ($responseDocument['error']) {
  4642. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  4643. }
  4644. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  4645. }
  4646. $arrStock[$key]['IMAGENES'] = $arrUrlImage;
  4647. $arrStock[$key]['CANTIDAD'] = 0;
  4648. }
  4649. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrStock);
  4650. }
  4651. public function createRequestLine(Request $request) {
  4652. $valitador = Validator::make($request->all(), [
  4653. 'REQUEST_LINE' => 'required|array',
  4654. 'USUARIO' => 'required|string',
  4655. 'NUMERO_LINEA' => 'required|integer',
  4656. ]);
  4657. if ($valitador->fails()) {
  4658. return $this->responseController->makeResponse(
  4659. true,
  4660. "ERR_DOCUMENT_ORDER_REG000: Se encontraron uno o más errores.",
  4661. $this->responseController->makeErrors($valitador->errors()->messages()),
  4662. 401
  4663. );
  4664. }
  4665. DB::beginTransaction();
  4666. $requestData = $request->all();
  4667. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  4668. if ($arrResponseCheckUser['error']) {
  4669. DB::rollBack();
  4670. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  4671. }
  4672. $user = $arrResponseCheckUser['response'];
  4673. $now = $this->functionsController->now();
  4674. $currentDate = $now->toDateTimeString();
  4675. $arrProviders = array();
  4676. $arrRequestData = array();
  4677. foreach ($requestData['REQUEST_LINE'] as $keyRequest => $request) {
  4678. try {
  4679. $validateInsert = DB::table('S002V01TCARE')->insert([
  4680. 'CARE_NULI' => $requestData['NUMERO_LINEA'],
  4681. 'CARE_IDST' => $request['ID_STOCK'],
  4682. 'CARE_CAAD' => $request['CANTIDAD'],
  4683. 'CARE_USRE' => $user,
  4684. 'CARE_FERE' => $currentDate,
  4685. 'CARE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  4686. ]);
  4687. } catch (\Throwable $th) {
  4688. DB::rollBack();
  4689. return $this->responseController->makeResponse(true, 'Ocurrió un error al guar la información del reabastecimiento del stock.', $th->getMessage(), 500);
  4690. }
  4691. if (!$validateInsert) {
  4692. DB::rollBack();
  4693. return $this->responseController->makeResponse(true, 'No se pudo guardar los datos del reabastecimiento dentro de la base de datos.', [], 500);
  4694. }
  4695. try {
  4696. $arrStock = (array) DB::table('S002V01TSTAR')
  4697. ->where('STAR_IDST', '=', $request['ID_STOCK'])
  4698. ->where('STAR_TIAD', '=', 'Por Pedido')
  4699. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  4700. ->where('STAR_ESTA', '=', 'Activo')
  4701. ->where('INST_NULI', '=', $requestData['NUMERO_LINEA'])
  4702. ->where('INST_ESTA', '=', 'Activo')
  4703. ->where('UBAR_NULI', '=', $requestData['NUMERO_LINEA'])
  4704. ->where('UBAR_ESTA', '=', 'Activo')
  4705. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  4706. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  4707. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  4708. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  4709. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'STAR_NUPR')
  4710. ->first([
  4711. 'INST_MODE AS MODELO',
  4712. 'INST_COMO AS CODIGO_MODELO',
  4713. 'FAMI_COFA AS FAMILIA',
  4714. 'SUBF_COSU AS SUBFAMILIA',
  4715. 'PROV_NUPR AS NUMERO_PROVEEDOR',
  4716. ]);
  4717. } catch (\Throwable $th) {
  4718. DB::rollBack();
  4719. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  4720. }
  4721. if (empty($arrStock)) {
  4722. DB::rollBack();
  4723. return $this->responseController->makeResponse(true, 'No se encontró la información del Stock', [], 500);
  4724. }
  4725. if ( !in_array($arrStock['NUMERO_PROVEEDOR'], $arrProviders) ) {
  4726. $arrProviders[] = $arrStock['NUMERO_PROVEEDOR'];
  4727. }
  4728. $request['MODELO'] = $arrStock['MODELO'];
  4729. $request['CODIGO_MODELO'] = $arrStock['CODIGO_MODELO'];
  4730. $request['FAMILIA'] = $arrStock['FAMILIA'];
  4731. $request['SUBFAMILIA'] = $arrStock['SUBFAMILIA'];
  4732. $request['NUMERO_PROVEEDOR'] = $arrStock['NUMERO_PROVEEDOR'];
  4733. try {
  4734. $infoAdquisition = (array) DB::table('S002V01TINAR')
  4735. ->where('INAR_NULI', '=', $requestData['NUMERO_LINEA'])
  4736. ->where('INAR_CODI', '=', $arrStock['CODIGO_MODELO'])
  4737. ->where('INAR_MODE', '=', $arrStock['MODELO'])
  4738. ->where('INAR_ESTA', '=', 'Activo')
  4739. ->where('DEAR_NULI', '=', $requestData['NUMERO_LINEA'])
  4740. ->where('DEAR_NUPR', '=', $arrStock['NUMERO_PROVEEDOR'])
  4741. ->where('DEAR_ESTA', '=', 'Activo')
  4742. ->where('ARTI_NULI', '=', $requestData['NUMERO_LINEA'])
  4743. ->where('ARTI_COFA', '=', $arrStock['FAMILIA'])
  4744. ->where('ARTI_COSU', '=', $arrStock['SUBFAMILIA'])
  4745. ->where('ARTI_ESTA', '=', 'Activo')
  4746. ->join('S002V01TDEAR', 'DEAR_IDDE', '=', 'INAR_IDDE')
  4747. ->join('S002V01TARTI', 'ARTI_IDAR', '=', 'DEAR_IDAR')
  4748. ->first([
  4749. 'ARTI_IDAR AS ID_ARTICULO',
  4750. 'INAR_IDIN AS ID_INFORMACION',
  4751. ]);
  4752. } catch (\Throwable $th) {
  4753. DB::rollBack();
  4754. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información de la adquisición.', $th->getMessage(), 500);
  4755. }
  4756. if (empty($infoAdquisition)) {
  4757. DB::rollBack();
  4758. return $this->responseController->makeResponse(true, 'No se encontró la información de la adquisición.', [], 500);
  4759. }
  4760. $request['ID_ARTICULO'] = $infoAdquisition['ID_ARTICULO'];
  4761. $request['ID_INFORMACION'] = $infoAdquisition['ID_INFORMACION'];
  4762. $arrRequestData[$keyRequest] = $request;
  4763. }
  4764. foreach ($arrProviders as $provider) {
  4765. $contItemRequest = 1;
  4766. foreach ($arrRequestData as $key => $value) {
  4767. if ($provider === $value['NUMERO_PROVEEDOR']) {
  4768. try {
  4769. $idRequest = DB::table('S002V01TLINE')->insertGetId([
  4770. 'LINE_NULI' => $requestData['NUMERO_LINEA'],
  4771. 'LINE_NUPR' => $value['NUMERO_PROVEEDOR'],
  4772. 'LINE_ESTA' => 'En espera',
  4773. 'LINE_USRE' => $user,
  4774. 'LINE_FERE' => $currentDate,
  4775. 'LINE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  4776. ]);
  4777. } catch (\Throwable $th) {
  4778. DB::rollBack();
  4779. return $this->responseController->makeResponse(true, 'Ocurrió un error al insertar la información en la línea de solicitud.', $th->getMessage(), 500);
  4780. }
  4781. if ($idRequest <= 0) {
  4782. DB::rollBack();
  4783. return $this->responseController->makeResponse(true, 'No se pudo insertar la información de la línea de solicitud.', [], 500);
  4784. }
  4785. for ($i=0; $i < $value['CANTIDAD']; $i++) {
  4786. try {
  4787. $validateInsert = DB::table('S002V01TARSE')->insert([
  4788. 'ARSE_IDAS' => $contItemRequest,
  4789. 'ARSE_IDLI' => $idRequest,
  4790. 'ARSE_ESTA' => 'Activo',
  4791. 'ARSE_NULI' => $requestData['NUMERO_LINEA'],
  4792. 'ARSE_IDAR' => $value['ID_ARTICULO'],
  4793. 'ARSE_NUPR' => $value['NUMERO_PROVEEDOR'],
  4794. 'ARSE_IDIN' => $value['ID_INFORMACION'],
  4795. 'ARSE_USRE' => $user,
  4796. 'ARSE_FERE' => $currentDate,
  4797. 'ARSE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  4798. ]);
  4799. } catch (\Throwable $th) {
  4800. DB::rollBack();
  4801. return $this->responseController->makeResponse(true, 'Ocurrió un error al insertar los artículos en la línea de solicitud.', $th->getMessage(), 500);
  4802. }
  4803. if (!$validateInsert) {
  4804. DB::rollBack();
  4805. return $this->responseController->makeResponse(true, 'No se pudo insertar los artículos en la línea de solicitud.', [], 500);
  4806. }
  4807. $contItemRequest++;
  4808. }
  4809. }
  4810. }
  4811. }
  4812. DB::commit();
  4813. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  4814. }
  4815. public function getReplenishmentSuggestion($user, $line) {
  4816. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  4817. if ($arrResponseCheckUser['error']) {
  4818. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  4819. }
  4820. try {
  4821. $arrInfoStock = DB::table('S002V01TINST')
  4822. ->where('INST_NULI', '=', $line)
  4823. ->where('INST_ESTA', '=', 'Activo')
  4824. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  4825. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  4826. ->join('S002V01TSTAR', 'STAR_IDIS', '=', 'INST_IDIS')
  4827. ->get([
  4828. 'INST_IDIS AS ID_INFORMACION_STOCK',
  4829. 'STAR_IDST AS ID_STOCK',
  4830. DB::raw('CONCAT(FAMI_NOFA, " (", FAMI_COFA, ")") AS FAMILIA'),
  4831. DB::raw('CONCAT(SUBF_NOSU, " (", SUBF_COSU, ")") AS SUBFAMILIA'),
  4832. 'INST_MODE AS MODELO',
  4833. 'INST_COMO AS CODIGO_MODELO',
  4834. 'STAR_NUPR AS NUMERO_PROVEEDOR',
  4835. 'INST_STMI AS STOCK_MINIMO',
  4836. 'INST_STMA AS STOCK_MAXIMO',
  4837. 'INST_IMAG AS IMAGENES',
  4838. ]);
  4839. $arrInfoStock = json_decode(json_encode($arrInfoStock), true);
  4840. } catch (\Throwable $th) {
  4841. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  4842. }
  4843. $arrDataStock = array();
  4844. foreach ($arrInfoStock as $key => $info) {
  4845. $arrImages = json_decode($info['IMAGENES']);
  4846. $arrUrlImage = array();
  4847. foreach ($arrImages as $images) {
  4848. $images = $this->encController->encrypt($images);
  4849. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  4850. if ($responseDocument['error']) {
  4851. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  4852. }
  4853. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  4854. }
  4855. $arrInfoStock[$key]['IMAGENES'] = $arrUrlImage;
  4856. try {
  4857. $countStock = DB::table('S002V01TSTAR')
  4858. ->where('STAR_NULI', '=', $line)
  4859. ->where('STAR_IDIS', '=', $info['ID_INFORMACION_STOCK'])
  4860. ->where('STAR_ESTA', '=', 'Activo')
  4861. ->count();
  4862. } catch (\Throwable $th) {
  4863. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el stock.', $th->getMessage(), 500);
  4864. }
  4865. $arrInfoStock[$key]['CANTIDAD_ACTUAL'] = $countStock;
  4866. if ($countStock < $info['STOCK_MINIMO'] && !is_null($info['STOCK_MINIMO'])) {
  4867. $arrInfoStock[$key]['ESTADO'] = 'Menor al Stock Requerido';
  4868. $arrInfoStock[$key]['CANTIDAD_SUGERIDA'] = $info['STOCK_MINIMO'] - $countStock;
  4869. $arrDataStock[] = $arrInfoStock[$key];
  4870. }
  4871. }
  4872. try {
  4873. $arrReplanishment = DB::table('S002V01TCARE')
  4874. ->where('CARE_NULI', '=', $line)
  4875. ->where('CARE_ESTA', '=', 'Activo')
  4876. ->join('S002V01TSTAR', 'STAR_IDST', '=', 'CARE_IDST')
  4877. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  4878. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  4879. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  4880. ->orderBy('CARE_CAAD', 'DESC')
  4881. ->limit(5)
  4882. ->get([
  4883. 'INST_IDIS AS ID_INFORMACION_STOCK',
  4884. 'STAR_IDST AS ID_STOCK',
  4885. DB::raw('CONCAT(FAMI_NOFA, " (", FAMI_COFA, ")") AS FAMILIA'),
  4886. DB::raw('CONCAT(SUBF_NOSU, " (", SUBF_COSU, ")") AS SUBFAMILIA'),
  4887. 'INST_MODE AS MODELO',
  4888. 'INST_COMO AS CODIGO_MODELO',
  4889. 'STAR_NUPR AS NUMERO_PROVEEDOR',
  4890. 'INST_STMI AS STOCK_MINIMO',
  4891. 'INST_STMA AS STOCK_MAXIMO',
  4892. 'INST_IMAG AS IMAGENES',
  4893. 'CARE_CAAD AS CANTIDAD_SUGERIDA',
  4894. ]);
  4895. $arrReplanishment = json_decode(json_encode($arrReplanishment), true);
  4896. } catch (\Throwable $th) {
  4897. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el reabastecimiento.', $th->getMessage(), 500);
  4898. }
  4899. foreach ($arrReplanishment as $key => $replanishment) {
  4900. $arrImages = json_decode($replanishment['IMAGENES']);
  4901. $arrUrlImage = array();
  4902. foreach ($arrImages as $images) {
  4903. $images = $this->encController->encrypt($images);
  4904. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  4905. if ($responseDocument['error']) {
  4906. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  4907. }
  4908. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  4909. }
  4910. $arrReplanishment[$key]['IMAGENES'] = $arrUrlImage;
  4911. $arrReplanishment[$key]['ESTADO'] = 'Stock habitualmente pedido';
  4912. }
  4913. // Se verifica los campos repetidos
  4914. $arrIdDataStock = array_column($arrDataStock, 'ID_INFORMACION_STOCK');
  4915. $arrIdReplanishment = array_column($arrReplanishment, 'ID_INFORMACION_STOCK');
  4916. $arrRepeated = array_merge($arrIdDataStock, $arrIdReplanishment);
  4917. $arrUnique = array_unique($arrRepeated);
  4918. $arrValues = array_values($arrUnique);
  4919. // Se unen con los estados para el resultado final
  4920. $arrFinal = array();
  4921. foreach ($arrValues as $key => $value) {
  4922. foreach ($arrDataStock as $dataStock) {
  4923. if ($dataStock['ID_INFORMACION_STOCK'] === $value) {
  4924. $arrFinal[$key] = $dataStock;
  4925. }
  4926. }
  4927. foreach ($arrReplanishment as $replanishment) {
  4928. if ($replanishment['ID_INFORMACION_STOCK'] === $value) {
  4929. if (array_key_exists($key, $arrFinal)) {
  4930. $arrFinal[$key]['ESTADO'] .= ' | '.$replanishment['ESTADO'];
  4931. } else {
  4932. $arrFinal[$key] = $replanishment;
  4933. }
  4934. }
  4935. }
  4936. }
  4937. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrFinal);
  4938. }
  4939. public function getNomenclatureManagement($user, $line) {
  4940. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  4941. if ($arrResponseCheckUser['error']) {
  4942. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  4943. }
  4944. try {
  4945. $arrStock = DB::table('S002V01TSTAR')
  4946. ->where('STAR_TIAD', '=', 'Por Pedido')
  4947. ->where('STAR_NULI', '=', $line)
  4948. ->where('STAR_ESTA', '=', 'Activo')
  4949. ->where('INST_NULI', '=', $line)
  4950. ->where('INST_ESTA', '=', 'Activo')
  4951. ->where('UBAR_NULI', '=', $line)
  4952. ->where('UBAR_ESTA', '=', 'Activo')
  4953. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  4954. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  4955. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  4956. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  4957. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'STAR_NUPR')
  4958. ->get([
  4959. 'STAR_IDST AS ID_STOCK',
  4960. 'INST_MODE AS MODELO',
  4961. 'INST_COMO AS CODIGO_MODELO',
  4962. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  4963. 'FAMI_NOFA AS FAMILIA',
  4964. 'SUBF_NOSU AS SUBFAMILIA',
  4965. 'INST_IMAG AS IMAGENES',
  4966. ]);
  4967. $arrStock = json_decode(json_encode($arrStock), true);
  4968. } catch (\Throwable $th) {
  4969. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  4970. }
  4971. foreach ($arrStock as $key => $info) {
  4972. $arrImages = json_decode($info['IMAGENES']);
  4973. $arrUrlImage = array();
  4974. foreach ($arrImages as $keyImages => $images) {
  4975. $images = $this->encController->encrypt($images);
  4976. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  4977. if ($responseDocument['error']) {
  4978. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  4979. }
  4980. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  4981. }
  4982. $arrStock[$key]['IMAGENES'] = $arrUrlImage;
  4983. }
  4984. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrStock);
  4985. }
  4986. public function getDetailsNomenclature($idStock, $user, $line) {
  4987. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  4988. if ($arrResponseCheckUser['error']) {
  4989. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  4990. }
  4991. $idStock = $this->encController->decrypt($idStock);
  4992. if (is_null($idStock)) {
  4993. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID de la información.', [], 401);
  4994. }
  4995. try {
  4996. $validateExists = DB::table('S002V01TSTAR')
  4997. ->where('STAR_IDST', '=', $idStock)
  4998. ->where('STAR_NULI', '=', $line)
  4999. ->where('STAR_ESTA', '=', 'Activo')
  5000. ->exists();
  5001. } catch (\Throwable $th) {
  5002. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar el ID del stock.', $th->getMessage(), 500);
  5003. }
  5004. if (!$validateExists) {
  5005. return $this->responseController->makeResponse(true, 'El número de stock no existe.', [], 500);
  5006. }
  5007. try {
  5008. $info = (array) DB::table('S002V01TSTAR')
  5009. ->where('STAR_IDST', '=', $idStock)
  5010. ->where('STAR_TIAD', '=', 'Por Pedido')
  5011. ->where('STAR_NULI', '=', $line)
  5012. ->where('STAR_ESTA', '=', 'Activo')
  5013. ->where('INST_NULI', '=', $line)
  5014. ->where('INST_ESTA', '=', 'Activo')
  5015. ->where('UBAR_NULI', '=', $line)
  5016. ->where('UBAR_ESTA', '=', 'Activo')
  5017. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  5018. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  5019. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  5020. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  5021. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'STAR_NUPR')
  5022. ->join('S002V01TALMA', 'ALMA_COAL', '=', 'UBAR_COAL')
  5023. ->join('S002V01TAREA', 'AREA_COAR', '=', 'UBAR_COAR')
  5024. ->join('S002V01TNIVE', 'NIVE_CONI', '=', 'UBAR_CONI')
  5025. ->join('S002V01TZONA', 'ZONA_COZO', '=', 'UBAR_COZO')
  5026. ->first([
  5027. 'INST_MODE AS MODELO',
  5028. 'INST_COMO AS CODIGO_MODELO',
  5029. 'FAMI_COFA AS CODIGO_FAMILIA',
  5030. 'FAMI_NOFA AS FAMILIA',
  5031. 'SUBF_COSU AS CODIGO_SUBFAMILIA',
  5032. 'SUBF_NOSU AS SUBFAMILIA',
  5033. 'ALMA_COAL AS CODIGO_ALMACEN',
  5034. 'ALMA_NOAL AS NOMBRE_ALMACEN',
  5035. 'AREA_COAR AS CODIGO_AREA',
  5036. 'AREA_NOAR AS NOMBRE_AREA',
  5037. 'NIVE_CONI AS CODIGO_NIVEL',
  5038. 'NIVE_NONI AS NOMBRE_NIVEL',
  5039. 'ZONA_COZO AS CODIGO_ZONA',
  5040. 'ZONA_NOZO AS NOMBRE_ZONA',
  5041. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  5042. ]);
  5043. } catch (\Throwable $th) {
  5044. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar el ID del stock.', $th->getMessage(), 500);
  5045. }
  5046. if (empty($info)) {
  5047. return $this->responseController->makeResponse(true, 'No se encontró la información del código.', [], 500);
  5048. }
  5049. $codeEquipment = $info['CODIGO_EQUIPAMIENTO'];
  5050. $arrStructureCode = explode('_', $codeEquipment);
  5051. if ( count($arrStructureCode) !== 2 ) {
  5052. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la estructura del código.' [], 500);
  5053. }
  5054. $strLBS = $arrStructureCode[0];
  5055. $arrStructureLBS = explode('.', $strLBS);
  5056. if (count($arrStructureLBS) !== 4) {
  5057. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la estructura del LBS.' [], 500);
  5058. }
  5059. $levelOccupation = $arrStructureLBS[2];
  5060. $arrStructureLevelOccupation = explode('-', $levelOccupation);
  5061. if (count($arrStructureLevelOccupation) !== 2) {
  5062. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el nivel y la ocupación.', [], 500);
  5063. }
  5064. $codeSite = $arrStructureLBS[0];
  5065. $codeLocation = $arrStructureLBS[1];
  5066. $codeElement = $arrStructureLBS[3];
  5067. $codeLevel = $arrStructureLevelOccupation[0];
  5068. $codeOccupation = $arrStructureLevelOccupation[1];
  5069. $strPBS = $arrStructureCode[1];
  5070. $arrStructurePBS = explode('.', $strPBS);
  5071. if ( count($arrStructurePBS) !== 4 ) {
  5072. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la estructura del PBS.', [], 500);
  5073. }
  5074. $typeModel = $arrStructurePBS[3];
  5075. $arrStructureTypeModel = explode('-', $typeModel);
  5076. if (count($arrStructureTypeModel) !== 3) {
  5077. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener tipo y modelo.', [], 500);
  5078. }
  5079. $codeFamily = $arrStructurePBS[0];
  5080. $codeSubfamily = $arrStructurePBS[1];
  5081. $codeState = $arrStructurePBS[2];
  5082. $codeType = $arrStructureTypeModel[0];
  5083. $codeModel = $arrStructureTypeModel[1];
  5084. $arrClaves = array(
  5085. 'CLAVE_SITIO' => $codeSite,
  5086. 'SITIO' => 'Número de Línea 01',
  5087. 'CLAVE_UBICACION' => $codeLocation,
  5088. 'UBICACION' => $info['NOMBRE_AREA'],
  5089. 'CLAVE_POSICION' => $codeElement,
  5090. 'POSICION' => $info['NOMBRE_NIVEL'],
  5091. 'CLAVE_OCUPACION' => $codeLevel,
  5092. 'OCUPACION' => $info['NOMBRE_ALMACEN'],
  5093. 'CLAVE_ELEMENTO' => $codeOccupation,
  5094. 'ELEMENTO' => $info['NOMBRE_ZONA'],
  5095. 'CLAVE_FAMILIA' => $codeFamily,
  5096. 'FAMILIA' => $info['FAMILIA'],
  5097. 'CLAVE_SUBFAMILIA' => $codeSubfamily,
  5098. 'SUBFAMILIA' => $info['SUBFAMILIA'],
  5099. 'CLAVE_ESTADO' => $codeState,
  5100. 'ESTADO' => $this->resourcesController->arrStatesEquipment[$codeState],
  5101. 'CLAVE_TIPO' => $codeType,
  5102. 'TIPO' => $info['MODELO'],
  5103. 'CLAVE_MODELO' => $codeModel,
  5104. 'MODELO' => $info['CODIGO_MODELO'],
  5105. );
  5106. return $this->responseController->makeResponse(false, 'ÉXITO: Consulta Exitosa.', $arrClaves);
  5107. }
  5108. public function getObjectProperties($user, $line) {
  5109. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  5110. if ($arrResponseCheckUser['error']) {
  5111. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  5112. }
  5113. try {
  5114. $arrStock = DB::table('S002V01TSTAR')
  5115. ->where('STAR_NULI', '=', $line)
  5116. ->where('STAR_ESTA', '=', 'Activo')
  5117. ->where('INST_NULI', '=', $line)
  5118. ->where('INST_ESTA', '=', 'Activo')
  5119. ->where('UBAR_NULI', '=', $line)
  5120. ->where('UBAR_ESTA', '=', 'Activo')
  5121. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  5122. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  5123. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  5124. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  5125. ->get([
  5126. 'STAR_IDST AS ID_STOCK',
  5127. 'INST_MODE AS MODELO',
  5128. 'INST_COMO AS CODIGO_MODELO',
  5129. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  5130. 'INST_IMAG AS IMAGENES',
  5131. 'FAMI_COFA AS CODIGO_FAMILIA',
  5132. 'FAMI_NOFA AS FAMILIA',
  5133. 'SUBF_COSU AS CODIGO_SUBFAMILIA',
  5134. 'SUBF_NOSU AS SUBFAMILIA',
  5135. ]);
  5136. $arrStock = json_decode(json_encode($arrStock), true);
  5137. } catch (\Throwable $th) {
  5138. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  5139. }
  5140. foreach ($arrStock as $key => $info) {
  5141. $arrImages = json_decode($info['IMAGENES']);
  5142. $arrUrlImage = array();
  5143. foreach ($arrImages as $keyImages => $images) {
  5144. $images = $this->encController->encrypt($images);
  5145. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  5146. if ($responseDocument['error']) {
  5147. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  5148. }
  5149. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  5150. }
  5151. $arrStock[$key]['IMAGENES'] = $arrUrlImage;
  5152. }
  5153. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrStock);
  5154. }
  5155. public function getDetailsObjectProperties($idStock, $user, $line) {
  5156. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  5157. if ($arrResponseCheckUser['error']) {
  5158. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  5159. }
  5160. $idStock = $this->encController->decrypt($idStock);
  5161. if (is_null($idStock)) {
  5162. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID de la información.', [], 401);
  5163. }
  5164. try {
  5165. $validateExists = DB::table('S002V01TSTAR')
  5166. ->where('STAR_IDST', '=', $idStock)
  5167. ->where('STAR_NULI', '=', $line)
  5168. ->where('STAR_ESTA', '=', 'Activo')
  5169. ->exists();
  5170. } catch (\Throwable $th) {
  5171. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar el ID del stock.', $th->getMessage(), 500);
  5172. }
  5173. if (!$validateExists) {
  5174. return $this->responseController->makeResponse(true, 'El número de stock no existe.', [], 500);
  5175. }
  5176. try {
  5177. $arrStock = (array) DB::table('S002V01TSTAR')
  5178. ->where('STAR_IDST', '=', $idStock)
  5179. ->where('STAR_NULI', '=', $line)
  5180. ->where('STAR_ESTA', '=', 'Activo')
  5181. ->where('INST_NULI', '=', $line)
  5182. ->where('INST_ESTA', '=', 'Activo')
  5183. ->where('UBAR_NULI', '=', $line)
  5184. ->where('UBAR_ESTA', '=', 'Activo')
  5185. ->where('FAMI_NULI', '=', $line)
  5186. ->where('FAMI_ESTA', '=', 'Activo')
  5187. ->where('SUBF_NULI', '=', $line)
  5188. ->where('SUBF_ESTA', '=', 'Activo')
  5189. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  5190. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  5191. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  5192. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  5193. ->join('S002V01TALMA', 'ALMA_COAL', '=', 'UBAR_COAL')
  5194. ->join('S002V01TAREA', 'AREA_COAR', '=', 'UBAR_COAR')
  5195. ->join('S002V01TNIVE', 'NIVE_CONI', '=', 'UBAR_CONI')
  5196. ->join('S002V01TZONA', 'ZONA_COZO', '=', 'UBAR_COZO')
  5197. ->first([
  5198. 'STAR_COBA AS CODIGO_BARRA',
  5199. 'STAR_FEVE AS FECHA_VENCIMIENTO',
  5200. 'STAR_NUPR AS NUMERO_PROVEEDOR',
  5201. 'STAR_TIAD AS TIPO_ADQUISICION',
  5202. 'STAR_CONS AS CONSUMIBLE',
  5203. 'STAR_REPA AS REPARABLE',
  5204. 'STAR_ALTO AS ALTO',
  5205. 'STAR_ANCH AS ANCHO',
  5206. 'STAR_LARG AS LARGO',
  5207. 'STAR_COLO AS COLOR',
  5208. 'STAR_PRAD AS PRECIO_ADQUIRIDO',
  5209. 'STAR_COMO AS CODIGO_MONEDA',
  5210. 'INST_MODE AS MODELO',
  5211. 'INST_COMO AS CODIGO_MODELO',
  5212. 'INST_STMI AS STOCK_MINIMO',
  5213. 'INST_STMA AS STOCK_MAXIMO',
  5214. 'STAR_NIPE AS NIVEL_PELIGROSIDAD',
  5215. 'INST_IMAG AS IMAGENES',
  5216. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  5217. 'FAMI_COFA AS CODIGO_FAMILIA',
  5218. 'FAMI_NOFA AS NOMBRE_FAMILIA',
  5219. 'SUBF_COSU AS CODIGO_SUBFAMILIA',
  5220. 'SUBF_NOSU AS NOMBRE_SUBFAMILIA',
  5221. 'ALMA_COAL AS CODIGO_ALMACEN',
  5222. 'ALMA_NOAL AS NOMBRE_ALMACEN',
  5223. 'AREA_COAR AS CODIGO_AREA',
  5224. 'AREA_NOAR AS NOMBRE_AREA',
  5225. 'NIVE_CONI AS CODIGO_NIVEL',
  5226. 'NIVE_NONI AS NOMBRE_NIVEL',
  5227. 'ZONA_COZO AS CODIGO_ZONA',
  5228. 'ZONA_NOZO AS NOMBRE_ZONA',
  5229. ]);
  5230. } catch (\Throwable $th) {
  5231. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener los datos del stock.', $th->getMessage(), 500);
  5232. }
  5233. if (empty($arrStock)) {
  5234. return $this->responseController->makeResponse(true, 'No existe información de stock.', [], 500);
  5235. }
  5236. $arrImages = array();
  5237. $arrImagesTemp = json_decode($arrStock['IMAGENES']);
  5238. foreach ($arrImagesTemp as $key => $value) {
  5239. $images = $this->encController->encrypt($value);
  5240. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  5241. if ($responseDocument['error']) {
  5242. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  5243. }
  5244. $arrImages[] = $responseDocument['response']['public_uri'];
  5245. }
  5246. $arrStock['IMAGENES'] = $arrImages;
  5247. try {
  5248. $infoAdquisition = (array) DB::table('S002V01TINAR')
  5249. ->where('INAR_NULI', '=', $line)
  5250. ->where('INAR_CODI', '=', $arrStock['CODIGO_MODELO'])
  5251. ->where('INAR_MODE', '=', $arrStock['MODELO'])
  5252. ->where('INAR_ESTA', '=', 'Activo')
  5253. ->where('DEAR_NULI', '=', $line)
  5254. ->where('DEAR_NUPR', '=', $arrStock['NUMERO_PROVEEDOR'])
  5255. ->where('DEAR_ESTA', '=', 'Activo')
  5256. ->where('ARTI_NULI', '=', $line)
  5257. ->where('ARTI_COFA', '=', $arrStock['CODIGO_FAMILIA'])
  5258. ->where('ARTI_COSU', '=', $arrStock['CODIGO_SUBFAMILIA'])
  5259. ->where('ARTI_ESTA', '=', 'Activo')
  5260. ->join('S002V01TDEAR', 'DEAR_IDDE', '=', 'INAR_IDDE')
  5261. ->join('S002V01TARTI', 'ARTI_IDAR', '=', 'DEAR_IDAR')
  5262. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'DEAR_NUPR')
  5263. ->first([
  5264. 'DEAR_DESC AS DESCRIPCION_PROVEEDOR',
  5265. 'DEAR_CARA AS CARACTERISTICA_PROVEEDOR',
  5266. 'PROV_NOCO AS NOMBRE_PROVEEDOR',
  5267. ]);
  5268. } catch (\Throwable $th) {
  5269. DB::rollBack();
  5270. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información de la adquisición.', $th->getMessage(), 500);
  5271. }
  5272. if (empty($infoAdquisition)) {
  5273. DB::rollBack();
  5274. return $this->responseController->makeResponse(true, 'No se encontró la información de la adquisición.', [], 500);
  5275. }
  5276. $arrGeneralInfo = array_merge($arrStock, $infoAdquisition);
  5277. return $this->responseController->makeResponse(false, 'ÉXITO: Consulta Exitosa', $arrGeneralInfo);
  5278. }
  5279. public function saveInfoStock(Request $request, $idStock) {
  5280. $valitador = Validator::make($request->all(), [
  5281. 'ALTO' => 'string|nullable',
  5282. 'ANCHO' => 'string|nullable',
  5283. 'LARGO' => 'string|nullable',
  5284. 'COLOR' => 'string|nullable',
  5285. 'NIVEL_PELIGROSIDAD' => 'string|nullable',
  5286. 'TASA_ROTACION' => 'string|nullable',
  5287. 'USUARIO' => 'required|string',
  5288. 'NUMERO_LINEA' => 'required|integer',
  5289. ]);
  5290. if ($valitador->fails()) {
  5291. return $this->responseController->makeResponse(
  5292. true,
  5293. "ERR_DOCUMENT_ORDER_REG000: Se encontraron uno o más errores.",
  5294. $this->responseController->makeErrors($valitador->errors()->messages()),
  5295. 401
  5296. );
  5297. }
  5298. DB::beginTransaction();
  5299. $requestData = $request->all();
  5300. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  5301. if ($arrResponseCheckUser['error']) {
  5302. DB::rollBack();
  5303. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  5304. }
  5305. $user = $arrResponseCheckUser['response'];
  5306. $idStock = $this->encController->decrypt($idStock);
  5307. if (is_null($idStock)) {
  5308. DB::rollBack();
  5309. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID de la información.', [], 401);
  5310. }
  5311. try {
  5312. $validateExists = DB::table('S002V01TSTAR')
  5313. ->where('STAR_IDST', '=', $idStock)
  5314. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  5315. ->where('STAR_ESTA', '=', 'Activo')
  5316. ->exists();
  5317. } catch (\Throwable $th) {
  5318. DB::rollBack();
  5319. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar el ID del stock.', $th->getMessage(), 500);
  5320. }
  5321. if (!$validateExists) {
  5322. DB::rollBack();
  5323. return $this->responseController->makeResponse(true, 'El número de stock no existe.', [], 500);
  5324. }
  5325. $now = $this->functionsController->now();
  5326. $currentDate = $now->toDateTimeString();
  5327. try {
  5328. $validateInsert = DB::table('S002V01TSTAR')
  5329. ->where('STAR_IDST', '=', $idStock)
  5330. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  5331. ->where('STAR_ESTA', '=', 'Activo')
  5332. ->update([
  5333. 'STAR_ALTO' => $requestData['ALTO'],
  5334. 'STAR_ANCH' => $requestData['ANCHO'],
  5335. 'STAR_LARG' => $requestData['LARGO'],
  5336. 'STAR_COLO' => $requestData['COLOR'],
  5337. 'STAR_NIPE' => $requestData['NIVEL_PELIGROSIDAD'],
  5338. 'STAR_USMO' => $user,
  5339. 'STAR_FEMO' => $currentDate,
  5340. 'STAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  5341. ]);
  5342. } catch (\Throwable $th) {
  5343. DB::rollBack();
  5344. return $this->responseController->makeResponse(true, 'Ocurrió un error al insertar los datos del stock.', $th->getMessage(), 500);
  5345. }
  5346. if (!$validateInsert) {
  5347. DB::rollBack();
  5348. return $this->responseController->makeResponse(true, 'No se pudo insertar los datos del stock.', [], 500);
  5349. }
  5350. DB::commit();
  5351. return $this->responseController->makeResponse(false, 'ÉXITO: Registro Exitoso');
  5352. }
  5353. public function getTools ($user, $line) {
  5354. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  5355. if ($arrResponseCheckUser['error']) {
  5356. DB::rollBack();
  5357. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  5358. }
  5359. try {
  5360. $arrStock = DB::table('S002V01TINST')
  5361. ->where([
  5362. ['INST_NULI', '=', $line],
  5363. ])->get([
  5364. 'INST_IDIS AS ID',
  5365. 'INST_MODE AS NOMB',
  5366. 'INST_COMO AS MODE',
  5367. // ' AS CANTDISP',
  5368. // ' AS TYPE',
  5369. // ' AS UNIT',
  5370. ]);
  5371. $arrStock = json_decode(json_encode($arrStock), true);
  5372. } catch (\Throwable $th) {
  5373. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener los artículos en stock.', $th->getMessage(), 500);
  5374. }
  5375. $arrTools = [];
  5376. $arrSpares = [];
  5377. foreach ($arrStock as $stock) {
  5378. try {
  5379. $items = DB::table('S002V01TSTAR')
  5380. ->where([
  5381. ['STAR_NULI', '=', $line],
  5382. ['STAR_IDIS', '=', $stock['ID']],
  5383. ])
  5384. ->join('S002V01TUNID', 'UNID_IDUN', '=', 'STAR_IDUN')
  5385. ->get([
  5386. 'UNID_NOMB AS UNIT',
  5387. 'STAR_CONS AS TYPE',
  5388. ]);
  5389. $items = json_decode(json_encode($items), true);
  5390. } catch (\Throwable $th) {
  5391. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener los datos de las herramientas y refacciones.', $th->getMessage(), 500);
  5392. }
  5393. $stock['CANTDISP'] = count($items);
  5394. foreach ($items as $item) {
  5395. if ($item['TYPE'] === 'Si') {
  5396. $stock['TYPE'] = 'Refaccción';
  5397. } else {
  5398. $stock['TYPE'] = 'Herramienta';
  5399. }
  5400. $stock['UNIT'] = $item['UNIT'];
  5401. }
  5402. if ($stock['TYPE'] === 'Refaccción') {
  5403. $arrTools[] = $stock;
  5404. } else {
  5405. $arrSpares[] = $stock;
  5406. }
  5407. }
  5408. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", [ 'HERRAMIENTAS' => $arrTools, 'REFACCIONES' => $arrSpares ]);
  5409. }
  5410. public function getSpares ($user, $line) {
  5411. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  5412. if ($arrResponseCheckUser['error']) {
  5413. DB::rollBack();
  5414. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  5415. }
  5416. /* try {
  5417. $arrSpares = DB::table('S002V01TINST')
  5418. ->where([
  5419. 'STAR_NULI', '', $line,
  5420. ])
  5421. } catch (\Throwable $th) {
  5422. //throw $th;
  5423. } */
  5424. }
  5425. }