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 desencripta y se verifica si el usuario existe
  2135. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  2136. if ($arrResponseCheckUser['error']) {
  2137. DB::rollBack();
  2138. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  2139. }
  2140. $user = $arrResponseCheckUser['response'];
  2141. try {
  2142. $infoAdquisition = (array) DB::table('S002V01TINAR')
  2143. ->where([
  2144. ['INAR_IDIN', '=', $requestData['ID_INFORMATION']],
  2145. ['INAR_NULI', '=', $requestData['NUMERO_LINEA']],
  2146. ])
  2147. ->join('S002V01TDEAR', 'DEAR_IDDE', '=', 'INAR_IDDE')
  2148. ->join('S002V01TUNID', 'UNID_IDUN', '=', 'DEAR_IDUN')
  2149. ->first([
  2150. 'DEAR_CAAR AS CANTIDAD_ARTICULOS',
  2151. ]);
  2152. } catch (\Throwable $th) {
  2153. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información de la adquisición.', $th->getMessage(), 404);
  2154. }
  2155. if (empty($infoAdquisition)) {
  2156. return $this->responseController->makeResponse(true, 'No existe la información de la adquisición.', [], 404);
  2157. }
  2158. $infoAdquisition['CANTIDAD_ARTICULOS'] = intval($infoAdquisition['CANTIDAD_ARTICULOS']);
  2159. $requestData['NUMBER_ITEMS'] = intval($requestData['NUMBER_ITEMS']);
  2160. $amountArticle = $infoAdquisition['CANTIDAD_ARTICULOS'] * $requestData['NUMBER_ITEMS'];
  2161. // Se verifica la precodificación
  2162. if ( count($requestData['PRE_CODIFICATE']) !== $amountArticle) {
  2163. DB::rollBack();
  2164. return $this->responseController->makeResponse(true, 'La cantidad registrada de la precodificación no es igual la cantidad de artículos seleccionados.', [$requestData['PRE_CODIFICATE'], $requestData['NUMBER_ITEMS']], 500);
  2165. }
  2166. // Se obtiene la información encriptada
  2167. try {
  2168. $idWarehouse = $this->encController->decrypt($requestData['WAREHOUSE']);
  2169. } catch (\Throwable $th) {
  2170. DB::rollBack();
  2171. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el almacen.', $th->getMessage(), 500);
  2172. }
  2173. if ($idWarehouse === false) {
  2174. DB::rollBack();
  2175. return $this->responseController->makeResponse(true, 'La encriptación del almacen no es correcta.', [], 500);
  2176. }
  2177. try {
  2178. $idArea = $this->encController->decrypt($requestData['AREA']);
  2179. } catch (\Throwable $th) {
  2180. DB::rollBack();
  2181. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el área.', $th->getMessage(), 500);
  2182. }
  2183. if ($idArea === false) {
  2184. DB::rollBack();
  2185. return $this->responseController->makeResponse(true, 'La encriptacipon del área no es correcta.', [], 500);
  2186. }
  2187. try {
  2188. $idLevel = $this->encController->decrypt($requestData['LEVEL']);
  2189. } catch (\Throwable $th) {
  2190. DB::rollBack();
  2191. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el nivel.', $th->getMessage(), 500);
  2192. }
  2193. if ($idLevel === false) {
  2194. DB::rollBack();
  2195. return $this->responseController->makeResponse(true, 'La encriptación del nivel no es correcta.', [], 500);
  2196. }
  2197. try {
  2198. $idZone = $this->encController->decrypt($requestData['ZONE']);
  2199. } catch (\Throwable $th) {
  2200. DB::rollBack();
  2201. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la zona.', $th->getMessage(), 500);
  2202. }
  2203. if ($idZone === false) {
  2204. DB::rollBack();
  2205. return $this->responseController->makeResponse(true, 'La encriptación de la zona no es correcta.', [], 500);
  2206. }
  2207. try {
  2208. $idFamily = $this->encController->decrypt($requestData['FAMILIA']);
  2209. } catch (\Throwable $th) {
  2210. DB::rollBack();
  2211. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la familia.', $th->getMessage(), 500);
  2212. }
  2213. if ($idFamily === false) {
  2214. DB::rollBack();
  2215. return $this->responseController->makeResponse(true, 'La encriptación de la familia no es correcta.', [], 500);
  2216. }
  2217. try {
  2218. $idSubfamily = $this->encController->decrypt($requestData['SUBFAMILIA']);
  2219. } catch (\Throwable $th) {
  2220. DB::rollBack();
  2221. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la subfamilia.', $th->getMessage(), 500);
  2222. }
  2223. if ($idSubfamily === false) {
  2224. DB::rollBack();
  2225. return $this->responseController->makeResponse(true, 'La encriptación de la subfamilia no es correcta.', [], 500);
  2226. }
  2227. // Se verifica que la información exista
  2228. try {
  2229. $validateWarehouse = DB::table('S002V01TALMA')
  2230. ->where('ALMA_COAL', '=', $idWarehouse)
  2231. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  2232. ->exists();
  2233. } catch (\Throwable $th) {
  2234. DB::rollBack();
  2235. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si el almacen existe.', $th->getMessage(), 500);
  2236. }
  2237. if ($validateWarehouse === false) {
  2238. DB::rollBack();
  2239. return $this->responseController->makeResponse(true, 'El almacen no existe.', [], 500);
  2240. }
  2241. try {
  2242. $validateArea = DB::table('S002V01TAREA')
  2243. ->where('AREA_COAL', '=', $idWarehouse)
  2244. ->where('AREA_COAR', '=', $idArea)
  2245. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  2246. ->exists();
  2247. } catch (\Throwable $th) {
  2248. DB::rollBack();
  2249. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si el área existe.', $th->getMessage(), 500);
  2250. }
  2251. if ($validateArea === false) {
  2252. DB::rollBack();
  2253. return $this->responseController->makeResponse(true, 'El área no existe.', [], 500);
  2254. }
  2255. try {
  2256. $validateLevel = DB::table('S002V01TNIVE')
  2257. ->where('NIVE_COAL', '=', $idWarehouse)
  2258. ->where('NIVE_COAR', '=', $idArea)
  2259. ->where('NIVE_CONI', '=', $idLevel)
  2260. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  2261. ->exists();
  2262. } catch (\Throwable $th) {
  2263. DB::rollBack();
  2264. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si el nivel existe.', $th->getMessage(), 500);
  2265. }
  2266. if ($validateLevel === false) {
  2267. DB::rollBack();
  2268. return $this->responseController->makeResponse(true, 'El nivel no existe.', [], 500);
  2269. }
  2270. try {
  2271. $validateZone = DB::table('S002V01TZONA')
  2272. ->where('ZONA_COAL', '=', $idWarehouse)
  2273. ->where('ZONA_COAR', '=', $idArea)
  2274. ->where('ZONA_CONI', '=', $idLevel)
  2275. ->where('ZONA_COZO', '=', $idZone)
  2276. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  2277. ->exists();
  2278. } catch (\Throwable $th) {
  2279. DB::rollBack();
  2280. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si la zona existe.', $th->getMessage(), 500);
  2281. }
  2282. if ($validateZone === false) {
  2283. return $this->responseController->makeResponse(true, 'La zona no existe.', [], 500);
  2284. }
  2285. try {
  2286. $validateFamily = DB::table('S002V01TFAMI')
  2287. ->where('FAMI_COFA', '=', $idFamily)
  2288. ->where('FAMI_NULI', '=', $requestData['NUMERO_LINEA'])
  2289. ->exists();
  2290. } catch (\Throwable $th) {
  2291. DB::rollBack();
  2292. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si la familia existe.', $th->getMessage(), 500);
  2293. }
  2294. if ($validateFamily === false) {
  2295. DB::rollBack();
  2296. return $this->responseController->makeResponse(true, 'La familia no existe.', [], 500);
  2297. }
  2298. try {
  2299. $validateSubfamily = DB::table('S002V01TSUBF')
  2300. ->where('SUBF_COFA', '=', $idFamily)
  2301. ->where('SUBF_COSU', '=', $idSubfamily)
  2302. ->where('SUBF_NULI', '=', $requestData['NUMERO_LINEA'])
  2303. ->exists();
  2304. } catch (\Throwable $th) {
  2305. DB::rollBack();
  2306. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si la subfamilia existe.', $th->getMessage(), 500);
  2307. }
  2308. if ($validateSubfamily === false) {
  2309. DB::rollBack();
  2310. return $this->responseController->makeResponse(true, 'La subfamilia no existe.', [], 500);
  2311. }
  2312. try {
  2313. $orden = (array) DB::table('S002V01TORCO')
  2314. ->where('ORCO_NUOR', '=', $requestData['ORDER'])
  2315. ->where('ORCO_NULI', '=', $requestData['NUMERO_LINEA'])
  2316. ->where('ORCO_ESTA', '=', 'Recibido')
  2317. ->first([
  2318. 'ORCO_IDLI AS ID_LINEA_SOLICITUD',
  2319. 'ORCO_IDDE AS ID_DESCRIPCION',
  2320. ]);
  2321. } catch (\Throwable $th) {
  2322. DB::rollBack();
  2323. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información de la orden de compra.', $th->getMessage(), 500);
  2324. }
  2325. if (is_null($orden)) {
  2326. DB::rollBack();
  2327. return $this->responseController->makeResponse(true, 'La orden de compra no existe.', [], 500);
  2328. }
  2329. $now = $this->functionsController->now();
  2330. $currentDate = $now->toDateTimeString();
  2331. try {
  2332. $info = DB::table('S002V01TARSE')
  2333. ->where('ARSE_IDIN', '=', $requestData['ID_INFORMATION'])
  2334. ->where('ARSE_ESTA', '=', 'Activo')
  2335. ->where('ARSE_IDLI', '=', $orden['ID_LINEA_SOLICITUD'])
  2336. ->where('ARSE_NULI', '=', $requestData['NUMERO_LINEA'])
  2337. ->where('ARTI_NULI', '=', $requestData['NUMERO_LINEA'])
  2338. ->where('DEAR_NULI', '=', $requestData['NUMERO_LINEA'])
  2339. ->where('UNID_NULI', '=', $requestData['NUMERO_LINEA'])
  2340. ->where('INAR_NULI', '=', $requestData['NUMERO_LINEA'])
  2341. ->where('ARSE_ESTA', '=', 'Activo')
  2342. ->where('ARTI_ESTA', '=', 'Activo')
  2343. ->where('DEAR_ESTA', '=', 'Activo')
  2344. ->where('UNID_ESTA', '=', 'Activo')
  2345. ->where('INAR_ESTA', '=', 'Activo')
  2346. ->where('INAR_CODI', '=', $requestData['CODIGO_MODELO'])
  2347. ->where('INAR_MODE', '=', $requestData['MODELO'])
  2348. ->join('S002V01TARTI', 'ARTI_IDAR', '=', 'ARSE_IDAR')
  2349. ->join('S002V01TINAR', 'INAR_IDIN', '=', 'ARSE_IDIN')
  2350. ->join('S002V01TDEAR', 'DEAR_IDDE', '=', 'INAR_IDDE')
  2351. ->join('S002V01TUNID', 'UNID_IDUN', '=', 'DEAR_IDUN')
  2352. ->limit($requestData['NUMBER_ITEMS'])
  2353. ->get([
  2354. 'ARSE_IDAS AS NUMERO_SELECCIONADO',
  2355. 'ARTI_IDAR AS ID_ARTICULO',
  2356. 'ARTI_COFA AS CODIGO_FAMILIA',
  2357. 'ARTI_COSU AS CODIGO_SUBFAMILIA',
  2358. 'ARTI_CODI AS CODIGO_ARTICULO',
  2359. 'ARTI_NOMB AS NOMBRE_ARTICULO',
  2360. 'DEAR_IDDE AS ID_DESCRIPCION',
  2361. 'DEAR_IMAG AS IMAGENES',
  2362. 'DEAR_DESC AS DESCRIPCION',
  2363. 'DEAR_CARA AS CARACTERISTICAS',
  2364. 'DEAR_COWE AS COMPRA_WEB',
  2365. 'DEAR_NUPR AS NUMERO_PROVEEDOR',
  2366. 'UNID_IDUN AS ID_UNIDAD',
  2367. 'UNID_NOMB AS NOMBRE_UNIDAD',
  2368. 'UNID_ACRO AS ACRONIMO_UNIDAD',
  2369. 'INAR_IDIN AS ID_INFORMACION',
  2370. 'INAR_CODI AS CODIGO_INFORMACION',
  2371. 'INAR_MODE AS MODELO_INFORMACION',
  2372. 'INAR_COMO AS CODIGO_MONEDA',
  2373. 'INAR_PREC AS PRECIO',
  2374. 'INAR_MOMI AS MONTO_MINIMO',
  2375. 'INAR_CARA AS CARACTERISTICAS',
  2376. ]);
  2377. $info = json_decode(json_encode($info ), true);
  2378. } catch (\Throwable $th) {
  2379. DB::rollBack();
  2380. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información de los productos.', $th->getMessage(), 500);
  2381. }
  2382. if ( is_null($info) || empty($info) ) {
  2383. DB::rollBack();
  2384. return $this->responseController->makeResponse(true, 'No existen artículos asociados a la orden de compra.', [], 500);
  2385. }
  2386. // Se verifica que existan los datos suficientes para hacer el traslado.
  2387. if ( count($info) !== $requestData['NUMBER_ITEMS']) {
  2388. DB::rollBack();
  2389. 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.', [count($info), $requestData['NUMBER_ITEMS']], 500);
  2390. }
  2391. // Se verifica los códigos de barras
  2392. $uniqueBarcode = false;
  2393. $arrBarcode = explode(',', $requestData['CODIGO_BARRAS']);
  2394. if ( count($arrBarcode) === 0) {
  2395. DB::rollBack();
  2396. return $this->responseController->makeResponse(true, 'No hay códigos de barras registrados.', [], 500);
  2397. } else if (count($arrBarcode) === $requestData['NUMBER_ITEMS']) {
  2398. $uniqueBarcode = false;
  2399. } else if (array_key_exists(0, $arrBarcode) && count($arrBarcode) === 1 && $arrBarcode[0] !== '' ) {
  2400. $uniqueBarcode = true;
  2401. } else {
  2402. DB::rollBack();
  2403. return $this->responseController->makeResponse(true, 'Ocurrió un error con los códigos de barras registrados.', [], 500);
  2404. }
  2405. // Se verifica que los códigos de barras del formulario no sean repetidos
  2406. $temp_array = array_unique($arrBarcode);
  2407. $duplicates = sizeof($temp_array) != sizeof($arrBarcode);
  2408. if ($duplicates) {
  2409. return $this->responseController->makeResponse(true, 'Los códigos de barras no se pueden repetir.', [], 400);
  2410. }
  2411. // Se verifican que los códigos de barras no se repitan
  2412. foreach ($arrBarcode as $key => $barcode) {
  2413. try {
  2414. $validateBarcode = DB::table('S002V01TSTAR')
  2415. ->where('STAR_COBA', '=', $barcode)
  2416. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  2417. ->exists();
  2418. } catch (\Throwable $th) {
  2419. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar los códigos de barras.', $th->getMessage(), 500);
  2420. }
  2421. if ($validateBarcode === true) {
  2422. return $this->responseController->makeResponse(true, 'El código '.$barcode.' ya se encuentra registrado', [], 500);
  2423. }
  2424. }
  2425. // Se verifica la fecha de vencimientos
  2426. $arrDueDate = array();
  2427. $uniqueDueDate = false;
  2428. if ($requestData['FECHA_VENCIMIENTO'] !== '' && !is_null($requestData['FECHA_VENCIMIENTO'])) {
  2429. $arrDueDate = explode(',', $requestData['FECHA_VENCIMIENTO']);
  2430. if ( count($arrDueDate) === 0) {
  2431. DB::rollBack();
  2432. return $this->responseController->makeResponse(true, 'No hay fechas de vencimientos registradas.', [], 500);
  2433. } else if (count($arrDueDate) === $requestData['NUMBER_ITEMS']) {
  2434. $uniqueDueDate = false;
  2435. } else if (array_key_exists(0, $arrDueDate) && count($arrDueDate) === 1 && $arrDueDate[0] !== '' ) {
  2436. $uniqueDueDate = true;
  2437. } else {
  2438. DB::rollBack();
  2439. return $this->responseController->makeResponse(true, 'Ocurrió un error con las fechas de vencimientos registrados.', [], 500);
  2440. }
  2441. }
  2442. // Se obtiene la fecha actual
  2443. $now = $this->functionsController->now();
  2444. $currentDate = $now->toDateTimeString();
  2445. $idInfoStock = null;
  2446. try {
  2447. $infoStock = (array) DB::table('S002V01TINST')
  2448. ->where('INST_COFA', '=', $idFamily)
  2449. ->where('INST_COSU', '=', $idSubfamily)
  2450. ->where('INST_ARTI', '=', $info[0]['ID_ARTICULO'])
  2451. ->where('INST_MODE', '=', $requestData['MODELO'])
  2452. ->where('INST_COMO', '=', $requestData['CODIGO_MODELO'])
  2453. ->where('INST_NULI', '=', $requestData['NUMERO_LINEA'])
  2454. ->where('INST_ESTA', '=', 'Activo')
  2455. ->first([
  2456. 'INST_IDIS AS ID_INFO_STOCK'
  2457. ]);
  2458. } catch (\Throwable $th) {
  2459. DB::rollBack();
  2460. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar la información del stock.', $th->getMessage(), 500);
  2461. }
  2462. if (empty($infoStock) || is_null($infoStock)) {
  2463. // Se obtiene el arreglo de las imagenes
  2464. $imagesArr = json_decode($requestData['IMAGES'], true);
  2465. // Se verifica que el arreglo contenga imagenes
  2466. if(count($imagesArr) < 1){
  2467. DB::rollBack();
  2468. return $this->responseController->makeResponse(true, 'El arreglo de la galería de imágenes está vacío.', [], 400);
  2469. }
  2470. $imagesGallery = [];
  2471. // Se iteran las imagenes
  2472. foreach($imagesArr as $imageFile){
  2473. // Se verifica que tenga un formato correcto
  2474. if(!array_key_exists('type', $imageFile)){
  2475. DB::rollBack();
  2476. return $this->responseController->makeResponse(true, 'El arreglo de la galería de imágenes tiene un formato inválido.', [], 400);
  2477. // Si la imagen es nueva, entonces...
  2478. }else if($imageFile['type'] == 'Nuevo'){
  2479. // Se obtiene el ID de la imagen
  2480. $tempFileID = $this->encController->decrypt($imageFile['id']);
  2481. // Se obtiene la información registrada en la tabla de archivos temporales
  2482. $tempFile = DB::table('S002V01TARTE')->where([
  2483. ['ARTE_IDAR', '=', $tempFileID],
  2484. ['ARTE_NULI', '=', $requestData['NUMERO_LINEA']]
  2485. ])->first();
  2486. // La imagen es colocada en su posición final
  2487. $finalFile = $this->documentManagementController->moveFinalFile($requestData['NUMERO_LINEA'], 'GEEQ', 'FO', $tempFile, $user);
  2488. // Si ocurrió un error, entonces se manda el error
  2489. if(!$finalFile[0]){
  2490. return $this->responseController->makeResponse(true, $finalFile[1], [], 400);
  2491. }
  2492. // La información es guardada en un arreglo
  2493. $imagesGallery[] = $finalFile[1];
  2494. }else{
  2495. // Se desencripta el id de la imagen
  2496. $fileID = $this->encController->decrypt($imageFile['id']);
  2497. // Es guardada en un arreglo
  2498. $imagesGallery[] = $fileID;
  2499. }
  2500. }
  2501. // El arreglo de las imagenes es pasada en formato string
  2502. $imagesGalleryStr = json_encode($imagesGallery);
  2503. try {
  2504. $idInsert = DB::table('S002V01TINST')->insertGetId([
  2505. 'INST_NULI' => $requestData['NUMERO_LINEA'],
  2506. 'INST_COFA' => $idFamily,
  2507. 'INST_COSU' => $idSubfamily,
  2508. 'INST_ARTI' => $info[0]['ID_ARTICULO'],
  2509. 'INST_MODE' => $requestData['MODELO'],
  2510. 'INST_COMO' => $requestData['CODIGO_MODELO'],
  2511. 'INST_IMAG' => $imagesGalleryStr,
  2512. 'INST_USRE' => $user,
  2513. 'INST_FERE' => $currentDate,
  2514. 'INST_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2515. ]);
  2516. } catch (\Throwable $th) {
  2517. DB::rollBack();
  2518. return $this->responseController->makeResponse(true, 'Ocurrió un error al insertar la información del stock.', $th->getMessage(), 500);
  2519. }
  2520. if ($idInsert === false || is_null($idInsert)) {
  2521. DB::rollBack();
  2522. return $this->responseController->makeResponse(true, 'No se pudo insertar la información del stock.', [], 500);
  2523. }
  2524. $idInfoStock = $idInsert;
  2525. } else {
  2526. $idInfoStock = $infoStock['ID_INFO_STOCK'];
  2527. }
  2528. if (is_null($idInfoStock)) {
  2529. DB::rollBack();
  2530. return $this->responseController->makeResponse(true, 'No se pudo obtener el ID de la información del Stock.', [], 500);
  2531. }
  2532. for ($i=0; $i < $requestData['NUMBER_ITEMS']; $i++) {
  2533. $dueDate = null;
  2534. if (count($arrDueDate) > 0 && $uniqueDueDate === true) {
  2535. $dueDate = $arrDueDate[0];
  2536. } else if (count($arrDueDate) > 0 && $uniqueDueDate === false) {
  2537. $dueDate = $arrDueDate[$i];
  2538. }
  2539. if (!is_null($dueDate) && str_contains($dueDate, 'T')) {
  2540. $dueDate = explode('T', $dueDate)[0];
  2541. }
  2542. try {
  2543. $preCodificate = $this->encController->decrypt($requestData['PRE_CODIFICATE'][$i]);
  2544. } catch (\Throwable $th) {
  2545. DB::rollBack();
  2546. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el identificador de la pre-codificación.', $th->getMessage(), 500);
  2547. }
  2548. if ($preCodificate === false) {
  2549. DB::rollBack();
  2550. return $this->responseController->makeResponse(true, 'No se pudo desencriptar el identificador de la pre-codificación.', [], 500);
  2551. }
  2552. try {
  2553. $idInsertStAr = DB::table('S002V01TSTAR')->insertGetId([
  2554. 'STAR_NULI' => $requestData['NUMERO_LINEA'],
  2555. 'STAR_IDIS' => $idInfoStock,
  2556. 'STAR_COBA' => $uniqueBarcode === true ? $arrBarcode[0] : $arrBarcode[$i],
  2557. 'STAR_FEVE' => $dueDate,
  2558. 'STAR_IDUN' => $info[$i]['ID_UNIDAD'],
  2559. 'STAR_NUPR' => $info[$i]['NUMERO_PROVEEDOR'],
  2560. 'STAR_TIAD' => 'Por pedido',
  2561. 'STAR_IDPC' => $preCodificate,
  2562. 'STAR_CONS' => $requestData['CONSUMIBLE'] ? 'Si' : 'No',
  2563. 'STAR_REPA' => $requestData['REPARABLE'] ? 'Si' : 'No',
  2564. 'STAR_PRAD' => $info[$i]['PRECIO'],
  2565. 'STAR_COMO' => $info[$i]['CODIGO_MONEDA'],
  2566. 'STAR_ESTA' => 'Pendiente',
  2567. 'STAR_USRE' => $user,
  2568. 'STAR_FERE' => $currentDate,
  2569. 'STAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2570. ]);
  2571. } catch (\Throwable $th) {
  2572. DB::rollBack();
  2573. return $this->responseController->makeResponse(true, 'Ocurrió un error al registrar el equipamiento.', $th->getMessage(), 500);
  2574. }
  2575. if ($idInsertStAr === false || is_null($idInsertStAr)) {
  2576. DB::rollBack();
  2577. return $this->responseController->makeResponse(true, 'No se pudo registrar el equipamiento.', [], 500);
  2578. }
  2579. try {
  2580. $preCodeCodificate = $this->encController->decrypt($requestData['PRE_GENERATED_CODE']);
  2581. } catch (\Throwable $th) {
  2582. DB::rollBack();
  2583. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el pre-código generado', $th->getMessage(), 500);
  2584. }
  2585. if ( $preCodeCodificate === false ) {
  2586. DB::rollBack();
  2587. return $this->responseController->makeResponse(true, 'No se pudo desencriptar el pre-código del equipamientos.', [], 500);
  2588. }
  2589. try {
  2590. $validateInsertUbAr = DB::table('S002V01TUBAR')->insert([
  2591. 'UBAR_NULI' => $requestData['NUMERO_LINEA'],
  2592. 'UBAR_COAL' => $idWarehouse,
  2593. 'UBAR_COAR' => $idArea,
  2594. 'UBAR_CONI' => $idLevel,
  2595. 'UBAR_COZO' => $idZone,
  2596. 'UBAR_IDST' => $idInsertStAr,
  2597. 'UBAR_COUB' => $preCodeCodificate,
  2598. 'UBAR_ESTA' => 'Pendiente',
  2599. 'UBAR_USRE' => $user,
  2600. 'UBAR_FERE' => $currentDate,
  2601. 'UBAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2602. ]);
  2603. } catch (\Throwable $th) {
  2604. DB::rollBack();
  2605. return $this->responseController->makeResponse(true, 'Ocurrió un error al registrar la ubicación del artículo.', $th->getMessage(), 500);
  2606. }
  2607. if ( $validateInsertUbAr === false ) {
  2608. DB::rollBack();
  2609. return $this->responseController->makeResponse(true, 'No se pudo registrar la ubicación del artículo.', [], 500);
  2610. }
  2611. try {
  2612. $validateUpdateArSe = DB::table('S002V01TARSE')
  2613. ->where('ARSE_IDAS', '=', $info[$i]['NUMERO_SELECCIONADO'])
  2614. ->where('ARSE_NULI', '=', $requestData['NUMERO_LINEA'])
  2615. ->where('ARSE_IDLI', '=', $orden['ID_LINEA_SOLICITUD'])
  2616. ->where('ARSE_IDAR', '=', $info[$i]['ID_ARTICULO'])
  2617. ->where('ARSE_NUPR', '=', $info[$i]['NUMERO_PROVEEDOR'])
  2618. ->where('ARSE_IDIN', '=', $requestData['ID_INFORMATION'])
  2619. ->where('ARSE_ESTA', '=', 'Activo')
  2620. ->update([
  2621. 'ARSE_ESTA' => 'Guardado',
  2622. 'ARSE_USMO' => $user,
  2623. 'ARSE_FEMO' => $currentDate,
  2624. 'ARSE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2625. ]);
  2626. } catch (\Throwable $th) {
  2627. DB::rollBack();
  2628. return $this->responseController->makeResponse(true, 'Ocurrió un error al modificar el artículo seleccionado.', $th->getMessage(), 500);
  2629. }
  2630. if ($validateUpdateArSe === false) {
  2631. DB::rollBack();
  2632. return $this->responseController->makeResponse(true, 'No se pudo modificar el artículo seleccionado.', [], 500);
  2633. }
  2634. }
  2635. DB::commit();
  2636. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  2637. }
  2638. // FUNCIÓN INTERCAMBIO DE ARTICULOS ENTRE ALMACENES
  2639. public function getCurrentLocationArtitles($user, $line) {
  2640. try {
  2641. $arrStockArtitle = DB::table('S002V01TSTAR')
  2642. ->where('STAR_NULI', '=', $line)
  2643. ->where('STAR_ESTA', '=', 'Activo')
  2644. ->where('INST_NULI', '=', $line)
  2645. ->where('INST_ESTA', '=', 'Activo')
  2646. ->where('UBAR_ESTA', '=', 'Activo')
  2647. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  2648. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  2649. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  2650. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  2651. ->join('S002V01TALMA', 'ALMA_COAL', '=', 'UBAR_COAL')
  2652. ->join('S002V01TAREA', 'AREA_COAR', '=', 'UBAR_COAR')
  2653. ->join('S002V01TNIVE', 'NIVE_CONI', '=', 'UBAR_CONI')
  2654. ->join('S002V01TZONA', 'ZONA_COZO', '=', 'UBAR_COZO')
  2655. ->orderBy('ID_STOCK', 'DESC')
  2656. ->get([
  2657. 'STAR_IDST AS ID_STOCK',
  2658. 'INST_IDIS AS ID_INFORMACION_STOCK',
  2659. 'FAMI_COFA AS CODIGO_FAMILIA',
  2660. 'FAMI_NOFA AS NOMBRE_FAMILIA',
  2661. 'SUBF_COSU AS CODIGO_SUBFAMILIA',
  2662. 'SUBF_NOSU AS NOMBRE_SUBFAMILIA',
  2663. 'INST_ARTI AS ARTITULO',
  2664. 'INST_MODE AS MODELO',
  2665. 'INST_COMO AS CODIGO_MODELO',
  2666. 'INST_IMAG AS IMAGENES',
  2667. 'UBAR_COUB AS CODIGO',
  2668. DB::raw('CONCAT(ALMA_NOAL, " (", ALMA_COAL, ")") AS NOMBRE_ALMACEN'),
  2669. DB::raw('CONCAT(AREA_NOAR, " (", AREA_COAR, ")") AS NOMBRE_AREA'),
  2670. DB::raw('CONCAT(NIVE_NONI, " (", NIVE_CONI, ")") AS NOMBRE_NIVEL'),
  2671. DB::raw('CONCAT(ZONA_NOZO, " (", ZONA_COZO, ")") AS NOMBRE_ZONA'),
  2672. ]);
  2673. $arrStockArtitle = json_decode(json_encode($arrStockArtitle), true);
  2674. } catch (\Throwable $th) {
  2675. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  2676. }
  2677. foreach ($arrStockArtitle as $key => $stockArtitle) {
  2678. $imagesGallery = [];
  2679. $arrImages = json_decode($stockArtitle['IMAGENES']);
  2680. foreach($arrImages as $image) {
  2681. $imageCodeEnc = $this->encController->encrypt($image);
  2682. $response = $this->documentManagementController->privateGetPublicDocumentURL(
  2683. $imageCodeEnc,
  2684. $user,
  2685. $line
  2686. );
  2687. if($response['error']){
  2688. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  2689. }
  2690. $imagesGallery[] = $response['response']['public_uri'];
  2691. }
  2692. $stockArtitle['IMAGENES'] = $imagesGallery;
  2693. $arrStockArtitle[$key] = $stockArtitle;
  2694. }
  2695. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrStockArtitle);
  2696. }
  2697. // FUNCIÓN PARA JOSÉ LUIS
  2698. public function getInfoStock($user, $line) {
  2699. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  2700. if ($arrResponseCheckUser['error']) {
  2701. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  2702. }
  2703. try {
  2704. $arrStockArtitle = DB::table('S002V01TSTAR')
  2705. ->where('STAR_NULI', '=', $line)
  2706. ->where('STAR_ESTA', '=', 'Activo')
  2707. ->where('INST_NULI', '=', $line)
  2708. ->where('INST_ESTA', '=', 'Activo')
  2709. ->where('UBAR_ESTA', '=', 'Activo')
  2710. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  2711. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  2712. ->get([
  2713. 'STAR_IDST AS ID_STOCK',
  2714. 'INST_MODE AS MODELO',
  2715. 'INST_COMO AS CODIGO_MODELO',
  2716. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  2717. 'STAR_CONS AS CONSUMIBLE',
  2718. 'STAR_REPA AS REPARABLE',
  2719. ]);
  2720. } catch (\Throwable $th) {
  2721. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  2722. }
  2723. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrStockArtitle);
  2724. }
  2725. public function moveToStock(Request $request, string $idStock ) {
  2726. $validator = Validator::make($request->all(), [
  2727. 'ALMACEN' => 'required|string',
  2728. 'AREA' => 'required|string',
  2729. 'NIVEL' => 'required|string',
  2730. 'ZONA' => 'required|string',
  2731. 'FAMILIA' => 'required|string',
  2732. 'SUBFAMILIA' => 'required|string',
  2733. 'LINEA' => 'required|string',
  2734. 'TIPO' => 'required|string',
  2735. 'MODELO' => 'required|string',
  2736. 'ID_EQUIPMENTO' => 'required|string',
  2737. 'STATUS' => 'required|string',
  2738. 'CODIGO_EQUIPAMIENTO' => 'required|string',
  2739. 'USUARIO' => 'required|string',
  2740. 'NUMERO_LINEA' => 'required|integer',
  2741. ]);
  2742. if($validator->fails()){
  2743. return $this->responseController->makeResponse(
  2744. true,
  2745. "Se encontraron uno o más errores.",
  2746. $this->responseController->makeErrors(
  2747. $validator->errors()->messages()
  2748. ),
  2749. 401
  2750. );
  2751. }
  2752. DB::beginTransaction();
  2753. $requestData = $request->all();
  2754. $arrInfoRegistro = array();
  2755. $arrToWorkflow = array();
  2756. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  2757. if ($arrResponseCheckUser['error']) {
  2758. DB::rollBack();
  2759. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  2760. }
  2761. $user = $arrResponseCheckUser['response'];
  2762. // Se obtiene el identificador del stock
  2763. $idStock = $this->encController->decrypt($idStock);
  2764. if ($idStock === false) {
  2765. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el stock.', [], 406);
  2766. }
  2767. // Se obtiene el valor del almacen
  2768. $requestData['ALMACEN'] = $this->encController->decrypt($requestData['ALMACEN']);
  2769. if ($requestData['ALMACEN'] === false) {
  2770. DB::rollBack();
  2771. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el ID del almacen.', [], 406);
  2772. }
  2773. // Se obtiene el valor del area
  2774. $requestData['AREA'] = $this->encController->decrypt($requestData['AREA']);
  2775. if ($requestData['AREA'] === false) {
  2776. DB::rollBack();
  2777. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el ID del área.', [], 406);
  2778. }
  2779. // Se obtiene el valor del nivel
  2780. $requestData['NIVEL'] = $this->encController->decrypt($requestData['NIVEL']);
  2781. if ($requestData['NIVEL'] === false) {
  2782. DB::rollBack();
  2783. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el ID del nivel.', [], 406);
  2784. }
  2785. // Se obtiene el valor de la zona
  2786. $requestData['ZONA'] = $this->encController->decrypt($requestData['ZONA']);
  2787. if ($requestData['ZONA'] === false) {
  2788. DB::rollBack();
  2789. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el ID de la zona.', [], 406);
  2790. }
  2791. // Se verifica el código del artículo que sea válido
  2792. $codeVerified = $this->verifyPreCode($requestData);
  2793. if ($codeVerified !== $requestData['CODIGO_EQUIPAMIENTO']) {
  2794. DB::rollBack();
  2795. 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);
  2796. }
  2797. // Se verifica que el código no sea el mismo que el anterior
  2798. if ($requestData['ANTERIOR_CODIGO_EQUIPAMIENTO'] === $requestData['CODIGO_EQUIPAMIENTO']) {
  2799. DB::rollBack();
  2800. return $this->responseController->makeResponse(true, 'Los códigos no deben ser los mismos.', [], 406);
  2801. }
  2802. // Se verifica que la información del almacen exista
  2803. try {
  2804. $validateWarehouse = DB::table('S002V01TALMA')
  2805. ->where('ALMA_COAL', '=', $requestData['ALMACEN'])
  2806. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  2807. ->exists();
  2808. } catch (\Throwable $th) {
  2809. DB::rollBack();
  2810. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si el almacen existe.', $th->getMessage(), 500);
  2811. }
  2812. if ($validateWarehouse === false) {
  2813. DB::rollBack();
  2814. return $this->responseController->makeResponse(true, 'El almacen no existe.', [], 500);
  2815. }
  2816. // Se verifica que la informacion del área exista
  2817. try {
  2818. $validateArea = DB::table('S002V01TAREA')
  2819. ->where('AREA_COAL', '=', $requestData['ALMACEN'])
  2820. ->where('AREA_COAR', '=', $requestData['AREA'])
  2821. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  2822. ->exists();
  2823. } catch (\Throwable $th) {
  2824. DB::rollBack();
  2825. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si el área existe.', $th->getMessage(), 500);
  2826. }
  2827. if ($validateArea === false) {
  2828. DB::rollBack();
  2829. return $this->responseController->makeResponse(true, 'El área no existe.', [], 500);
  2830. }
  2831. // Se verifica que la información del nivel exista
  2832. try {
  2833. $validateLevel = DB::table('S002V01TNIVE')
  2834. ->where('NIVE_COAL', '=', $requestData['ALMACEN'])
  2835. ->where('NIVE_COAR', '=', $requestData['AREA'])
  2836. ->where('NIVE_CONI', '=', $requestData['NIVEL'])
  2837. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  2838. ->exists();
  2839. } catch (\Throwable $th) {
  2840. DB::rollBack();
  2841. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si el nivel existe.', $th->getMessage(), 500);
  2842. }
  2843. if ($validateLevel === false) {
  2844. DB::rollBack();
  2845. return $this->responseController->makeResponse(true, 'El nivel no existe.', [], 500);
  2846. }
  2847. // Se verifica que la información de la zona
  2848. try {
  2849. $validateZone = DB::table('S002V01TZONA')
  2850. ->where('ZONA_COAL', '=', $requestData['ALMACEN'])
  2851. ->where('ZONA_COAR', '=', $requestData['AREA'])
  2852. ->where('ZONA_CONI', '=', $requestData['NIVEL'])
  2853. ->where('ZONA_COZO', '=', $requestData['ZONA'])
  2854. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  2855. ->exists();
  2856. } catch (\Throwable $th) {
  2857. DB::rollBack();
  2858. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si la zona existe.', $th->getMessage(), 500);
  2859. }
  2860. if ($validateZone === false) {
  2861. return $this->responseController->makeResponse(true, 'La zona no existe.', [], 500);
  2862. }
  2863. $arrToWorkflow[] = [
  2864. 'ANTERIOR_CODIGO_EQUIPAMIENTO' => $requestData['ANTERIOR_CODIGO_EQUIPAMIENTO'],
  2865. 'NUEVO_CODIGO_EQUIPAMIENTO' => $requestData['CODIGO_EQUIPAMIENTO'],
  2866. 'ALMACEN' => $requestData['ALMACEN'],
  2867. 'AREA' => $requestData['AREA'],
  2868. 'NIVEL' => $requestData['NIVEL'],
  2869. 'ZONA' => $requestData['ZONA'],
  2870. ];
  2871. $strToWorkflow = json_encode($arrToWorkflow);
  2872. $encToWorkflow = $this->encController->encrypt($strToWorkflow);
  2873. $now = $this->functionsController->now();
  2874. $currentDate = $now->toDateTimeString();
  2875. try {
  2876. $validateUpdate = DB::table('S002V01TUBAR')
  2877. ->where('UBAR_IDST', '=', $idStock)
  2878. ->where('UBAR_COUB', '=', $requestData['ANTERIOR_CODIGO_EQUIPAMIENTO'])
  2879. ->where('UBAR_ESTA', '=', 'Activo')
  2880. ->where('UBAR_NULI', '=', $requestData['NUMERO_LINEA'])
  2881. ->update([
  2882. 'UBAR_ESTA' => 'Obsoleto',
  2883. 'UBAR_USMO' => $user,
  2884. 'UBAR_FEMO' => $currentDate,
  2885. 'UBAR_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  2886. ]);
  2887. } catch (\Throwable $th) {
  2888. DB::rollBack();
  2889. return $this->responseController->makeResponse(true, 'Ocurrió un error al modificar el estado actual del código del stock.', $th->getMessage(), 406);
  2890. }
  2891. if (!$validateUpdate) {
  2892. DB::rollBack();
  2893. return $this->responseController->makeResponse(true, 'No se pudo modificar el estado actual del código del stock.', [], 406);
  2894. }
  2895. try {
  2896. $idUbication = DB::table('S002V01TUBAR')->insertGetId([
  2897. 'UBAR_NULI' => $requestData['NUMERO_LINEA'],
  2898. 'UBAR_COAL' => $requestData['ALMACEN'],
  2899. 'UBAR_COAR' => $requestData['AREA'],
  2900. 'UBAR_CONI' => $requestData['NIVEL'],
  2901. 'UBAR_COZO' => $requestData['ZONA'],
  2902. 'UBAR_IDST' => $idStock,
  2903. 'UBAR_COUB' => $requestData['CODIGO_EQUIPAMIENTO'],
  2904. 'UBAR_ESTA' => 'Workflow',
  2905. 'UBAR_USRE' => $user,
  2906. 'UBAR_FERE' => $currentDate,
  2907. 'UBAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2908. ]);
  2909. } catch (\Throwable $th) {
  2910. DB::rollBack();
  2911. return $this->responseController->makeResponse(true, 'Ocurrió un error al insertar la nueva ubicación del artículo.', $th->getMessage(), 406);
  2912. }
  2913. if (!$idUbication) {
  2914. DB::rollBack();
  2915. return $this->responseController->makeResponse(true, 'No se pudo insertar la nueva ubicación del artículo.', [], 406);
  2916. }
  2917. $arrInfoRegistro['S002V01TUBAR'][] = [
  2918. 'UBAR_IDUB' => $idUbication,
  2919. 'UBAR_IDST' => $idStock,
  2920. 'UBAR_COUB' => $requestData['CODIGO_EQUIPAMIENTO'],
  2921. ];
  2922. try {
  2923. $equipment = (array) DB::table('S002V01TEQUI')
  2924. ->where('EQUI_COEQ', '=', $requestData['ANTERIOR_CODIGO_EQUIPAMIENTO'])
  2925. ->where('EQUI_NULI', '=', $requestData['NUMERO_LINEA'])
  2926. ->first();
  2927. } catch (\Throwable $th) {
  2928. DB::rollBack();
  2929. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del artículo.', $th->getMessage(), 406);
  2930. }
  2931. if (is_null($equipment) || empty($equipment)) {
  2932. DB::rollBack();
  2933. return $this->responseController->makeResponse(true, 'No se se pudo obtener la información del equipamiento.', [], 406);
  2934. }
  2935. $strHistorial = $equipment['EQUI_HICO'];
  2936. $arrHistorial = json_decode($strHistorial, true);
  2937. $arrHistorial[] = $requestData['CODIGO_EQUIPAMIENTO'];
  2938. try {
  2939. $validateUpdate = DB::table('S002V01TEQUI')
  2940. ->where('EQUI_COEQ', '=', $requestData['ANTERIOR_CODIGO_EQUIPAMIENTO'])
  2941. ->where('EQUI_NULI', '=', $requestData['NUMERO_LINEA'])
  2942. ->update([
  2943. 'EQUI_COEQ' => $requestData['CODIGO_EQUIPAMIENTO'],
  2944. 'EQUI_ALMA' => $requestData['ALMACEN'],
  2945. 'EQUI_AREA' => $requestData['AREA'],
  2946. 'EQUI_NIVE' => $requestData['NIVEL'],
  2947. 'EQUI_ZONA' => $requestData['ZONA'],
  2948. 'EQUI_HICO' => $arrHistorial,
  2949. 'EQUI_USMO' => $user,
  2950. 'EQUI_FEMO' => $currentDate,
  2951. 'EQUI_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2952. ]);
  2953. } catch (\Throwable $th) {
  2954. DB::rollBack();
  2955. return $this->responseController->makeResponse(true, 'Ocurrió un error al modificar el equipamiento.', $th->getMessage(), 406);
  2956. }
  2957. if (!$validateUpdate) {
  2958. DB::rollBack();
  2959. return $this->responseController->makeResponse(true, 'No se pudo modificar el equipamiento.', [], 406);
  2960. }
  2961. try {
  2962. $existsEquipmentWorkflow = DB::table('S002V01TEQWO')
  2963. ->where('EQWO_COEQ', '=', $requestData['CODIGO_EQUIPAMIENTO'])
  2964. ->where('EQWO_NULI', '=', $requestData['NUMERO_LINEA'])
  2965. ->where('EQWO_ESTA', '=', 'En Workflow')
  2966. ->exists();
  2967. } catch (\Throwable $th) {
  2968. DB::rollBack();
  2969. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar el estado del equipamiento.', $th->getMessage(), 406);
  2970. }
  2971. if ( !$existsEquipmentWorkflow ) {
  2972. try {
  2973. $idEquipmentWorkflow = DB::table('S002V01TEQWO')->insertGetId([
  2974. 'EQWO_NULI' => $requestData['NUMERO_LINEA'],
  2975. 'EQWO_COEQ' => $requestData['CODIGO_EQUIPAMIENTO'],
  2976. 'EQWO_ESTA' => 'En Workflow',
  2977. 'EQWO_USRE' => $user,
  2978. 'EQWO_FERE' => $currentDate,
  2979. 'EQWO_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2980. ]);
  2981. } catch (\Throwable $th) {
  2982. DB::rollBack();
  2983. return $this->responseController->makeResponse(true, 'Ocurrió un error al registrar el equipamiento en el workflow.', $th->getMessage(), 406);
  2984. }
  2985. if (!$idEquipmentWorkflow) {
  2986. DB::rollBack();
  2987. return $this->responseController->makeResponse(true, 'No se pudo registrar el equipamiento en el workflow.', [], 406);
  2988. }
  2989. $arrInfoRegistro['S002V01TEQWO'][] = [
  2990. 'EQWO_IDEQW' => $idEquipmentWorkflow,
  2991. 'EQWO_COEQ' => $requestData['CODIGO_EQUIPAMIENTO'],
  2992. ];
  2993. } else {
  2994. DB::rollBack();
  2995. return $this->responseController->makeResponse(true, 'No se pudo hacer el intercambio si el equipamiento se encuentra en proceso de validación.', [], 406);
  2996. }
  2997. $strInfoRegistro = json_encode($arrInfoRegistro);
  2998. $encInfoRegistro = $this->encController->encrypt($strInfoRegistro);
  2999. $arrResponseRequestWorkflow = $this->processManagementController->registerRequestWorkflow(5, $encToWorkflow, $encInfoRegistro, $user, $requestData['NUMERO_LINEA']);
  3000. if ($arrResponseRequestWorkflow['error']) {
  3001. DB::rollBack();
  3002. return $this->responseController->makeResponse(true, $arrResponseRequestWorkflow['msg'], $arrResponseRequestWorkflow['response'], 500);
  3003. }
  3004. DB::commit();
  3005. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  3006. }
  3007. private function verifyPreCode(array $parameters) {
  3008. $siteTag = '';
  3009. $site = ['LINEA', 'AREA', 'NIVEL'];
  3010. foreach($site as $ctrl){
  3011. $val = $parameters[$ctrl];
  3012. if($ctrl == 'LINE'){
  3013. $val = intval($val) < 10 ? "0$val" : "$val";
  3014. }
  3015. $siteTag .= $val.'.';
  3016. }
  3017. $siteTag = substr($siteTag, 0, -1);
  3018. $areaTag = '';
  3019. $area = ['ALMACEN', 'ZONA'];
  3020. foreach($area as $ctrl){
  3021. $val = $parameters[$ctrl];
  3022. $areaTag .= $val.'.';
  3023. }
  3024. $areaTag = substr($areaTag, 0, -1);
  3025. $technicalSpecificationTag = '';
  3026. $technicalSpecification = ['FAMILIA', 'SUBFAMILIA', 'STATUS'];
  3027. foreach($technicalSpecification as $ctrl){
  3028. $val = $parameters[$ctrl];
  3029. $technicalSpecificationTag .= $val.'.';
  3030. }
  3031. $technicalSpecificationTag = substr($technicalSpecificationTag, 0, -1);
  3032. $equipmentIdentifierTag = '';
  3033. $equipmentIdentifier = ['TIPO', 'MODELO', 'ID_EQUIPMENTO'];
  3034. foreach($equipmentIdentifier as $ctrl){
  3035. $val = $parameters[$ctrl];
  3036. if($ctrl == 'MODELO'){
  3037. $val = $this->processElement($val, 'model');
  3038. }else if($ctrl == 'TIPO'){
  3039. $val = $this->processElement($val, 'element');
  3040. }
  3041. $equipmentIdentifierTag .= $val.'-';
  3042. }
  3043. $equipmentIdentifierTag = substr($equipmentIdentifierTag, 0, -1);
  3044. $code = $siteTag.'-'.$areaTag.'_'.$technicalSpecificationTag.'.'.$equipmentIdentifierTag;
  3045. return $code;
  3046. }
  3047. private function processElement(string $value, string $type) : string {
  3048. $value = strtoupper($value);
  3049. $validVal0 = $this->functionsController->unaccent($value);
  3050. $CONNECTORS = ['DE', 'PARA', 'Y', 'O', 'DEL', 'EL', 'LA', 'LOS', 'POR', 'EN'];
  3051. $valueArr1 = explode(' ', $validVal0);
  3052. $validVal1 = [];
  3053. foreach($valueArr1 as $word){
  3054. if(!in_array($word, $CONNECTORS)){
  3055. $validVal1[] = $word;
  3056. }
  3057. }
  3058. $validLength = count($validVal1);
  3059. $validVal2 = "";
  3060. if($validLength < 1){
  3061. return "ERROR";
  3062. }else if($validLength == 1){
  3063. if(strlen($validVal1[0]) < 5){
  3064. $validVal2 = $validVal1[0];
  3065. for($i = strlen($validVal1[0]); $i < 5; $i++){
  3066. if($type == 'model'){
  3067. $validVal2 = "X$validVal2";
  3068. }else{
  3069. $validVal2 = $validVal2 . "X";
  3070. }
  3071. }
  3072. }else{
  3073. $validVal2 = substr($validVal1[0], 0, 5);
  3074. }
  3075. }else if($validLength == 2){
  3076. $word1 = "";
  3077. if(strlen($validVal1[0]) < 2){
  3078. if($type == 'model'){
  3079. $word1 = "X$validVal1[0]";
  3080. }else{
  3081. $word1 = $validVal1[0] . "X";
  3082. }
  3083. }else{
  3084. $word1 = substr($validVal1[0], 0, 2);
  3085. }
  3086. $word2 = "";
  3087. if(strlen($validVal1[1]) < 3){
  3088. $word2 = $validVal1[1];
  3089. for($i = strlen($validVal1[1]); $i < 3; $i++){
  3090. if($type == 'model'){
  3091. $word2 = "X$word2";
  3092. }else{
  3093. $word2 = $word2 . "X";
  3094. }
  3095. }
  3096. }else{
  3097. $word2 = substr($validVal1[1], 0, 3);
  3098. }
  3099. $validVal2 = $word1 . $word2;
  3100. }else if($validLength == 3){
  3101. $word1 = "";
  3102. if(strlen($validVal1[0]) < 2){
  3103. if($type == 'model'){
  3104. $word1 = "X$validVal1[0]";
  3105. }else{
  3106. $word1 = $validVal1[0] . "X";
  3107. }
  3108. }else{
  3109. $word1 = substr($validVal1[0], 0, 2);
  3110. }
  3111. $word2 = substr($validVal1[1], 0, 1);
  3112. $word3 = "";
  3113. if(strlen($validVal1[2]) < 2){
  3114. if($type == 'model'){
  3115. $word3 = "X$validVal1[2]";
  3116. }else{
  3117. $word3 = $validVal1[2] . "X";
  3118. }
  3119. }else{
  3120. $word3 = substr($validVal1[2], 0, 2);
  3121. }
  3122. $validVal2 = $word1 . $word2 . $word3;
  3123. }else if($validLength == 4){
  3124. $word1 = "";
  3125. if(strlen($validVal1[0]) < 2){
  3126. if($type == 'model'){
  3127. $word1 = "X$validVal1[0]";
  3128. }else{
  3129. $word1 = $validVal1[0] . "X";
  3130. }
  3131. }else{
  3132. $word1 = substr($validVal1[0], 0, 2);
  3133. }
  3134. $word2 = substr($validVal1[1], 0, 1);
  3135. $word3 = substr($validVal1[2], 0, 1);
  3136. $word4 = substr($validVal1[3], 0, 1);
  3137. $validVal2 = $word1 . $word2 . $word3 . $word4;
  3138. }else if($validLength >= 5){
  3139. $word1 = substr($validVal1[0], 0, 1);
  3140. $word2 = substr($validVal1[1], 0, 1);
  3141. $word3 = substr($validVal1[2], 0, 1);
  3142. $word4 = substr($validVal1[3], 0, 1);
  3143. $word5 = substr($validVal1[4], 0, 1);
  3144. $validVal2 = $word1 . $word2 . $word3 . $word4 . $word5;
  3145. }else{
  3146. return "ERROR";
  3147. }
  3148. return $validVal2;
  3149. }
  3150. public function getLocationStock($user, $line) {
  3151. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  3152. if ($arrResponseCheckUser['error']) {
  3153. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3154. }
  3155. try {
  3156. $arrLocation = DB::table('S002V01TUBAR')
  3157. ->where('UBAR_NULI', '=', $line)
  3158. ->where('UBAR_ESTA', '=', 'Activo')
  3159. ->whereOr('UBAR_ESTA', '=', 'Otro')
  3160. ->get([
  3161. DB::raw("CONCAT('#', UBAR_IDST) AS ID_STOCK"),
  3162. DB::raw("CONCAT('#', UBAR_IDUB) AS ID_UBICACION "),
  3163. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  3164. 'UBAR_ESTA AS ESTADO',
  3165. 'UBAR_USRE AS USUARIO_REGISTRA',
  3166. 'UBAR_FERE AS FECHA_REGISTRA',
  3167. 'UBAR_USMO AS USUARIO_MODIFICA',
  3168. 'UBAR_FEMO AS FECHA_MODIFICA',
  3169. ]);
  3170. $arrLocation = json_decode(json_encode($arrLocation), true);
  3171. } catch (\Throwable $th) {
  3172. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el emplazamiento del stock.', $th->getMessage(), 500);
  3173. }
  3174. $responseCheckLatestUpdate = $this->resourcesController->checkLatestUpdate($arrLocation, $line);
  3175. if ($responseCheckLatestUpdate['error']) {
  3176. return $this->responseController->makeResponse(true, $responseCheckLatestUpdate['msg'], [], 500);
  3177. }
  3178. $arrLocation = $responseCheckLatestUpdate['response'];
  3179. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrLocation);
  3180. }
  3181. public function getLocationByStock($idStock, $user, $line) {
  3182. $idStock = $this->encController->decrypt($idStock);
  3183. if (is_null($idStock)) {
  3184. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID del Stock.', [], 401);
  3185. }
  3186. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  3187. if ($arrResponseCheckUser['error']) {
  3188. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3189. }
  3190. try {
  3191. $arrLocation = DB::table('S002V01TUBAR')
  3192. ->where('UBAR_NULI', '=', $line)
  3193. ->where('UBAR_IDST', '=', $idStock)
  3194. ->orderBy('UBAR_IDUB', 'DESC')
  3195. ->get([
  3196. DB::raw("CONCAT('#', UBAR_IDUB) AS ID_UBICACION "),
  3197. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  3198. 'UBAR_ESTA AS ESTADO',
  3199. 'UBAR_USRE AS USUARIO_REGISTRA',
  3200. 'UBAR_FERE AS FECHA_REGISTRA',
  3201. 'UBAR_USMO AS USUARIO_MODIFICA',
  3202. 'UBAR_FEMO AS FECHA_MODIFICA',
  3203. ]);
  3204. $arrLocation = json_decode(json_encode($arrLocation), true);
  3205. } catch (\Throwable $th) {
  3206. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el emplazamiento del stock.', $th->getMessage(), 500);
  3207. }
  3208. $responseCheckLatestUpdate = $this->resourcesController->checkLatestUpdate($arrLocation, $line);
  3209. if ($responseCheckLatestUpdate['error']) {
  3210. return $this->responseController->makeResponse(true, $responseCheckLatestUpdate['msg'], [], 500);
  3211. }
  3212. $arrLocation = $responseCheckLatestUpdate['response'];
  3213. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrLocation);
  3214. }
  3215. public function getDetaisLocation($idStock, $idLocation, $user, $line) {
  3216. $idStock = $this->encController->decrypt($idStock);
  3217. if (is_null($idStock)) {
  3218. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID del Stock.', [], 401);
  3219. }
  3220. $idLocation = $this->encController->decrypt($idLocation);
  3221. if (is_null($idLocation)) {
  3222. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID de la ubicación.', [], 401);
  3223. }
  3224. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  3225. if ($arrResponseCheckUser['error']) {
  3226. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3227. }
  3228. try {
  3229. $arrLocation = (array) DB::table('S002V01TUBAR')
  3230. ->where('UBAR_NULI', '=', $line)
  3231. ->where('UBAR_IDST', '=', $idStock)
  3232. ->where('UBAR_IDUB', '=', $idLocation)
  3233. ->join('S002V01TALMA', 'ALMA_COAL', '=', 'UBAR_COAL')
  3234. ->join('S002V01TAREA', 'AREA_COAR', '=', 'UBAR_COAR')
  3235. ->join('S002V01TNIVE', 'NIVE_CONI', '=', 'UBAR_CONI')
  3236. ->join('S002V01TZONA', 'ZONA_COZO', '=', 'UBAR_COZO')
  3237. ->first([
  3238. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  3239. DB::raw('CONCAT(ALMA_NOAL, " (", ALMA_COAL, ")") AS ALMACEN'),
  3240. DB::raw('CONCAT(AREA_NOAR, " (", AREA_COAL, ")") AS AREA'),
  3241. DB::raw('CONCAT(NIVE_NONI, " (", NIVE_CONI, ")") AS NIVEL'),
  3242. DB::raw('CONCAT(ZONA_NOZO, " (", ZONA_COZO, ")") AS ZONA'),
  3243. 'UBAR_ESTA AS ESTADO',
  3244. ]);
  3245. } catch (\Throwable $th) {
  3246. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el emplazamiento del stock.', $th->getMessage(), 500);
  3247. }
  3248. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrLocation);
  3249. }
  3250. public function search(Request $request) {
  3251. $validator = Validator::make($request->all(), [
  3252. 'ID_STOCK' => 'nullable|string',
  3253. 'CODIGO_EQUIPAMIENTO' => 'nullable|string',
  3254. 'ID_UBICACION' => 'nullable|string',
  3255. 'FAMILIA' => 'nullable|string',
  3256. 'SUBFAMILIA' => 'nullable|string',
  3257. 'MODELO' => 'nullable|string',
  3258. 'CODIGO_MODELO' => 'nullable|string',
  3259. 'ALMACEN' => 'nullable|string',
  3260. 'AREA' => 'nullable|string',
  3261. 'NIVEL' => 'nullable|string',
  3262. 'ZONA' => 'nullable|string',
  3263. 'TIPO_ADQUISICION' => 'nullable|string',
  3264. 'NUMERO_PROVEEDOR' => 'nullable|string',
  3265. 'CODIGO_BARRAS' => 'nullable|string',
  3266. 'FECHA_VENCIMIENTO' => 'nullable|string',
  3267. 'TASA_ROTACION' => 'nullable|string',
  3268. 'STOCK_MINIMO' => 'nullable|string',
  3269. 'STOCK_MAXIMO' => 'nullable|string',
  3270. 'ESTADO' => 'nullable|string',
  3271. 'CONSUMIBLE' => 'nullable|string',
  3272. 'REPARABLE' => 'nullable|string',
  3273. 'USUARIO' => 'nullable|string',
  3274. 'NUMERO_LINEA' => 'nullable|string',
  3275. ]);
  3276. if ($validator->fails()) {
  3277. return $this->responseController->makeResponse(
  3278. true,
  3279. "Se encontraron uno o más errores.",
  3280. $this->responseController->makeErrors($validator->errors()->messages()),
  3281. 401
  3282. );
  3283. }
  3284. DB::beginTransaction();
  3285. $requestData = $request->all();
  3286. $arrClausules = [
  3287. 'FAMILIA' => 'INST_COFA',
  3288. 'SUBFAMILIA' => 'INST_COSU',
  3289. 'MODELO' => 'INST_MODE',
  3290. 'CODIGO_MODELO' => 'INST_COMO',
  3291. 'STOCK_MINIMO' => 'INST_STMI',
  3292. 'STOCK_MAXIMO' => 'INST_STMA',
  3293. 'ID_STOCK' => 'STAR_IDST',
  3294. 'CODIGO_BARRAS' => 'STAR_COBA',
  3295. 'FECHA_VENCIMIENTO' => 'STAR_FEVE',
  3296. 'NUMERO_PROVEEDOR' => 'STAR_NUPR',
  3297. 'TIPO_ADQUISICION' => 'STAR_TIAD',
  3298. 'CONSUMIBLE' => 'STAR_CONS',
  3299. 'REPARABLE' => 'STAR_REPA',
  3300. 'ESTADO' => 'STAR_ESTA',
  3301. 'ID_UBICACION' => 'UBAR_IDUB',
  3302. 'ALMACEN' => 'UBAR_COAL',
  3303. 'AREA' => 'UBAR_COAR',
  3304. 'NIVEL' => 'UBAR_CONI',
  3305. 'ZONA' => 'UBAR_COZO',
  3306. 'CODIGO_EQUIPAMIENTO' => 'UBAR_COUB',
  3307. ];
  3308. $arrWhere = array();
  3309. foreach ($requestData as $key => $value) {
  3310. if (!is_null($value) && $key !== 'USUARIO' && $key !== 'NUMERO_LINEA') {
  3311. $column = $arrClausules[$key];
  3312. $arrWhere[$column] = $value;
  3313. }
  3314. }
  3315. if (empty($arrWhere)) {
  3316. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", []);
  3317. }
  3318. try {
  3319. $arrInfo = DB::table('S002V01TSTAR')
  3320. ->where($arrWhere)
  3321. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  3322. ->where('INST_NULI', '=', $requestData['NUMERO_LINEA'])
  3323. ->where('UBAR_NULI', '=', $requestData['NUMERO_LINEA'])
  3324. ->where('INST_ESTA', '=', 'Activo')
  3325. ->where('UBAR_ESTA', '=', 'Activo')
  3326. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  3327. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  3328. ->get([
  3329. 'STAR_IDST AS ID_STOCK',
  3330. 'INST_MODE AS MODELO',
  3331. 'INST_COMO AS CODIGO_MODELO',
  3332. 'INST_IMAG AS IMAGENES',
  3333. 'UBAR_COUB AS CODIGO',
  3334. ]);
  3335. $arrInfo = json_decode(json_encode($arrInfo), true);
  3336. } catch (\Throwable $th) {
  3337. return $this->responseController->makeResponse(true, "Ocurrió un error al recuperar la información.", $th->getMessage(), 500);
  3338. }
  3339. foreach ($arrInfo as $key => $info) {
  3340. $arrImages = json_decode($info['IMAGENES']);
  3341. $arrUrlImage = array();
  3342. foreach ($arrImages as $keyImages => $images) {
  3343. $images = $this->encController->encrypt($images);
  3344. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  3345. if ($responseDocument['error']) {
  3346. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  3347. }
  3348. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  3349. }
  3350. $arrInfo[$key]['IMAGENES'] = $arrUrlImage;
  3351. }
  3352. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrInfo);
  3353. }
  3354. public function getInfoByStock($idStock, $user, $line) {
  3355. $idStock = $this->encController->decrypt($idStock);
  3356. if (is_null($idStock)) {
  3357. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID del Stock.', [], 401);
  3358. }
  3359. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  3360. if ($arrResponseCheckUser['error']) {
  3361. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3362. }
  3363. try {
  3364. $arrInfo = (array) DB::table('S002V01TSTAR')
  3365. ->where('STAR_IDST', '=', $idStock)
  3366. ->where('STAR_NULI', '=', $line)
  3367. ->where('INST_NULI', '=', $line)
  3368. ->where('UBAR_NULI', '=', $line)
  3369. ->where('INST_ESTA', '=', 'Activo')
  3370. ->where('UBAR_ESTA', '=', 'Activo')
  3371. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  3372. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  3373. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  3374. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  3375. ->join('S002V01TALMA', 'ALMA_COAL', '=', 'UBAR_COAL')
  3376. ->join('S002V01TAREA', 'AREA_COAR', '=', 'UBAR_COAR')
  3377. ->join('S002V01TNIVE', 'NIVE_CONI', '=', 'UBAR_CONI')
  3378. ->join('S002V01TZONA', 'ZONA_COZO', '=', 'UBAR_COZO')
  3379. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'STAR_NUPR')
  3380. ->first([
  3381. DB::raw('CONCAT(FAMI_NOFA, " (", FAMI_COFA, ")") AS FAMILIA'),
  3382. DB::raw('CONCAT(SUBF_NOSU, " (", SUBF_COSU, ")") AS SUBFAMILIA'),
  3383. 'INST_MODE AS MODELO',
  3384. 'INST_COMO AS CODIGO_MODELO',
  3385. 'INST_STMI AS STOCK_MINIMO',
  3386. 'INST_STMA AS STOCK_MAXIMO',
  3387. 'STAR_IDST AS ID_STOCK',
  3388. 'STAR_COBA AS CODIGO_BARRAS',
  3389. 'STAR_FEVE AS FECHA_VENCIMIENTO',
  3390. DB::raw('CONCAT(PROV_NOCO, " (", PROV_NUPR, ")") AS NUMERO_PROVEEDOR'),
  3391. 'STAR_TIAD AS TIPO_ADQUISICION',
  3392. 'STAR_CONS AS CONSUMIBLE',
  3393. 'STAR_REPA AS REPARABLE',
  3394. 'STAR_ESTA AS ESTADO',
  3395. 'UBAR_IDUB AS ID_UBICACION',
  3396. DB::raw('CONCAT(ALMA_NOAL, " (", ALMA_COAL, ")") AS ALMACEN'),
  3397. DB::raw('CONCAT(AREA_NOAR, " (", AREA_COAR, ")") AS AREA'),
  3398. DB::raw('CONCAT(NIVE_NONI, " (", NIVE_CONI, ")") AS NIVEL'),
  3399. DB::raw('CONCAT(ZONA_NOZO, " (", ZONA_COZO, ")") AS ZONA'),
  3400. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  3401. ]);
  3402. $arrInfo = json_decode(json_encode($arrInfo), true);
  3403. } catch (\Throwable $th) {
  3404. return $this->responseController->makeResponse(true, "Ocurrió un error al recuperar la información.", $th->getMessage(), 500);
  3405. }
  3406. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrInfo);
  3407. }
  3408. public function getMaxMinStock($user, $line) {
  3409. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  3410. if ($arrResponseCheckUser['error']) {
  3411. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3412. }
  3413. try {
  3414. $arrInfoStock = DB::table('S002V01TINST')
  3415. ->where('INST_NULI', '=', $line)
  3416. ->where('INST_ESTA', '=', 'Activo')
  3417. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  3418. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  3419. ->get([
  3420. 'INST_IDIS AS ID_INFORMACION_STOCK',
  3421. DB::raw('CONCAT(FAMI_NOFA, " (", FAMI_COFA, ")") AS FAMILIA'),
  3422. DB::raw('CONCAT(SUBF_NOSU, " (", SUBF_COSU, ")") AS SUBFAMILIA'),
  3423. 'INST_MODE AS MODELO',
  3424. 'INST_COMO AS CODIGO_MODELO',
  3425. 'INST_STMI AS STOCK_MINIMO',
  3426. 'INST_STMA AS STOCK_MAXIMO',
  3427. 'INST_IMAG AS IMAGENES',
  3428. ]);
  3429. $arrInfoStock = json_decode(json_encode($arrInfoStock), true);
  3430. } catch (\Throwable $th) {
  3431. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  3432. }
  3433. foreach ($arrInfoStock as $key => $info) {
  3434. $arrImages = json_decode($info['IMAGENES']);
  3435. $arrUrlImage = array();
  3436. foreach ($arrImages as $keyImages => $images) {
  3437. $images = $this->encController->encrypt($images);
  3438. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  3439. if ($responseDocument['error']) {
  3440. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  3441. }
  3442. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  3443. }
  3444. $arrInfoStock[$key]['IMAGENES'] = $arrUrlImage;
  3445. try {
  3446. $countStock = DB::table('S002V01TSTAR')
  3447. ->where('STAR_NULI', '=', $line)
  3448. ->where('STAR_IDIS', '=', $info['ID_INFORMACION_STOCK'])
  3449. ->where('STAR_ESTA', '=', 'Activo')
  3450. ->count();
  3451. } catch (\Throwable $th) {
  3452. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  3453. }
  3454. $arrInfoStock[$key]['CANTIDAD_ACTUAL'] = $countStock;
  3455. $stateStock = '';
  3456. if ($countStock < $info['STOCK_MINIMO'] && !is_null($info['STOCK_MINIMO'])) {
  3457. $stateStock = 'Menor al Stock Requerido';
  3458. } else if ($countStock > $info['STOCK_MAXIMO'] && !is_null($info['STOCK_MAXIMO'])) {
  3459. $stateStock = 'Exedente al Stock Requerido';
  3460. } else if (is_null($info['STOCK_MINIMO']) && is_null($info['STOCK_MAXIMO'])) {
  3461. $stateStock = 'No aplica';
  3462. } else {
  3463. $stateStock = 'Stock Normal';
  3464. }
  3465. if (is_null($info['STOCK_MINIMO'])) {
  3466. $arrInfoStock[$key]['STOCK_MINIMO'] = 'Sin Definir';
  3467. }
  3468. if (is_null($info['STOCK_MAXIMO'])) {
  3469. $arrInfoStock[$key]['STOCK_MAXIMO'] = 'Sin Definir';
  3470. }
  3471. $arrInfoStock[$key]['ESTADO'] = $stateStock;
  3472. }
  3473. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrInfoStock);
  3474. }
  3475. public function updateStockMaxMin(Request $request, $idInfo) {
  3476. $validator = Validator::make($request->all(), [
  3477. 'STOCK_MINIMO' => 'required|integer',
  3478. 'STOCK_MAXIMO' => 'required|integer',
  3479. 'USUARIO' => 'required|string',
  3480. 'NUMERO_LINEA' => 'required|integer',
  3481. ]);
  3482. if($validator->fails()){
  3483. return $this->responseController->makeResponse(
  3484. true,
  3485. "Se encontraron uno o más errores.",
  3486. $this->responseController->makeErrors(
  3487. $validator->errors()->messages()
  3488. ),
  3489. 401
  3490. );
  3491. }
  3492. DB::beginTransaction();
  3493. $requestData = $request->all();
  3494. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  3495. if ($arrResponseCheckUser['error']) {
  3496. DB::rollBack();
  3497. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3498. }
  3499. $user = $arrResponseCheckUser['response'];
  3500. $idInfo = $this->encController->decrypt($idInfo);
  3501. if (is_null($idInfo)) {
  3502. DB::rollBack();
  3503. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID de la información.', [], 401);
  3504. }
  3505. try {
  3506. $validateExist = DB::table('S002V01TINST')
  3507. ->where('INST_NULI', '=', $requestData['NUMERO_LINEA'])
  3508. ->where('INST_IDIS', '=', $idInfo)
  3509. ->where('INST_ESTA', '=', 'Activo')
  3510. ->exists();
  3511. } catch (\Throwable $th) {
  3512. DB::rollBack();
  3513. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si existe el artículo.', $th->getMessage(), 500);
  3514. }
  3515. if (!$validateExist) {
  3516. DB::rollBack();
  3517. return $this->responseController->makeResponse(true, 'El artículo no existe.', [], 401);
  3518. }
  3519. try {
  3520. $stockMin = intval($requestData['STOCK_MINIMO']);
  3521. } catch (\Throwable $th) {
  3522. DB::rollBack();
  3523. return $this->responseController->makeResponse(true, 'Ocurrió un error con el formato del stock mínimo.', $th->getMessage(), 500);
  3524. }
  3525. try {
  3526. $stockMax = intval($requestData['STOCK_MAXIMO']);
  3527. } catch (\Throwable $th) {
  3528. DB::rollBack();
  3529. return $this->responseController->makeResponse(true, 'Ocurrió un error con el formato del stock mínimo.', $th->getMessage(), 500);
  3530. }
  3531. if ($stockMin < 0) {
  3532. DB::rollBack();
  3533. return $this->responseController->makeResponse(true, 'La cantidad del stock mínimo no debe ser menor a 0.', [], 401);
  3534. }
  3535. if ($stockMax < 0) {
  3536. DB::rollBack();
  3537. return $this->responseController->makeResponse(true, 'La cantidad del stock máximo no debe ser menos a 0.', [], 401);
  3538. }
  3539. if ($stockMin > $stockMax) {
  3540. DB::rollBack();
  3541. return $this->responseController->makeResponse(true, 'La cantidad del stock mínimo no debe ser mayos al stock máximo.', [], 401);
  3542. }
  3543. $now = $this->functionsController->now();
  3544. $currentDate = $now->toDateTimeString();
  3545. try {
  3546. $validateUpdate = DB::table('S002V01TINST')
  3547. ->where('INST_NULI', '=', $requestData['NUMERO_LINEA'])
  3548. ->where('INST_IDIS', '=', $idInfo)
  3549. ->where('INST_ESTA', '=', 'Activo')
  3550. ->update([
  3551. 'INST_STMI' => $stockMin,
  3552. 'INST_STMA' => $stockMax,
  3553. 'INST_USMO' => $user,
  3554. 'INST_FEMO' => $currentDate,
  3555. 'INST_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  3556. ]);
  3557. } catch (\Throwable $th) {
  3558. DB::rollBack();
  3559. return $this->responseController->makeResponse(true, 'Ocurrió un error al modificar el stock mínimo y máximo.', $th->getMessage(), 500);
  3560. }
  3561. if (!$validateUpdate) {
  3562. DB::rollBack();
  3563. return $this->responseController->makeResponse(true, 'No se pudo modificar el stock mínimo y máximo.', [], 401);
  3564. }
  3565. DB::commit();
  3566. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  3567. }
  3568. public function getStockBarcode($user, $line) {
  3569. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  3570. if ($arrResponseCheckUser['error']) {
  3571. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3572. }
  3573. try {
  3574. $arrStock = DB::table('S002V01TSTAR')
  3575. ->where('STAR_NULI', '=', $line)
  3576. ->where('STAR_ESTA', '=', 'Activo')
  3577. ->where('INST_NULI', '=', $line)
  3578. ->where('INST_ESTA', '=', 'Activo')
  3579. ->where('UBAR_NULI', '=', $line)
  3580. ->where('UBAR_ESTA', '=', 'Activo')
  3581. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  3582. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  3583. ->get([
  3584. 'STAR_IDST AS ID_STOCK',
  3585. 'INST_MODE AS MODELO',
  3586. 'INST_COMO AS CODIGO_MODELO',
  3587. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  3588. 'STAR_COBA AS CODIGO_BARRA',
  3589. 'INST_IMAG AS IMAGENES',
  3590. ]);
  3591. $arrStock = json_decode(json_encode($arrStock), true);
  3592. } catch (\Throwable $th) {
  3593. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  3594. }
  3595. foreach ($arrStock as $key => $info) {
  3596. $arrImages = json_decode($info['IMAGENES']);
  3597. $arrUrlImage = array();
  3598. foreach ($arrImages as $keyImages => $images) {
  3599. $images = $this->encController->encrypt($images);
  3600. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  3601. if ($responseDocument['error']) {
  3602. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  3603. }
  3604. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  3605. }
  3606. $arrStock[$key]['IMAGENES'] = $arrUrlImage;
  3607. }
  3608. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrStock);
  3609. }
  3610. public function updateStockBarcode(Request $request, $idStock) {
  3611. $validator = Validator::make($request->all(), [
  3612. 'BARCODE' => 'required|string',
  3613. 'USUARIO' => 'required|string',
  3614. 'NUMERO_LINEA' => 'required|integer',
  3615. ]);
  3616. if($validator->fails()){
  3617. return $this->responseController->makeResponse(
  3618. true,
  3619. "Se encontraron uno o más errores.",
  3620. $this->responseController->makeErrors(
  3621. $validator->errors()->messages()
  3622. ),
  3623. 401
  3624. );
  3625. }
  3626. DB::beginTransaction();
  3627. $requestData = $request->all();
  3628. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  3629. if ($arrResponseCheckUser['error']) {
  3630. DB::rollBack();
  3631. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3632. }
  3633. $user = $arrResponseCheckUser['response'];
  3634. $idStock = $this->encController->decrypt($idStock);
  3635. if (is_null($idStock)) {
  3636. DB::rollBack();
  3637. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID de la información.', [], 401);
  3638. }
  3639. try {
  3640. $validateExists = DB::table('S002V01TSTAR')
  3641. ->where('STAR_IDST', '=', $idStock)
  3642. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  3643. ->where('STAR_ESTA', '=', 'Activo')
  3644. ->exists();
  3645. } catch (\Throwable $th) {
  3646. DB::rollBack();
  3647. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar el ID del stock.', $th->getMessage(), 500);
  3648. }
  3649. if (!$validateExists) {
  3650. DB::rollBack();
  3651. return $this->responseController->makeResponse(true, 'El número de stock no existe.', [], 500);
  3652. }
  3653. $now = $this->functionsController->now();
  3654. $currentDate = $now->toDateTimeString();
  3655. try {
  3656. $validateUpdate = DB::table('S002V01TSTAR')
  3657. ->where('STAR_IDST', '=', $idStock)
  3658. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  3659. ->where('STAR_ESTA', '=', 'Activo')
  3660. ->update([
  3661. 'STAR_COBA' => $requestData['BARCODE'],
  3662. 'STAR_USMO' => $user,
  3663. 'STAR_FEMO' => $currentDate,
  3664. 'STAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  3665. ]);
  3666. } catch (\Throwable $th) {
  3667. DB::rollBack();
  3668. return $this->responseController->makeResponse(true, 'Ocurrió un error modificar el código de barra.', $th->getMessage(), 500);
  3669. }
  3670. if (!$validateUpdate) {
  3671. DB::rollBack();
  3672. return $this->responseController->makeResponse(true, 'No se pudo modificar el código de barra.', [], 500);
  3673. }
  3674. DB::commit();
  3675. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  3676. }
  3677. public function getStockDueDate($user, $line) {
  3678. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  3679. if ($arrResponseCheckUser['error']) {
  3680. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3681. }
  3682. try {
  3683. $arrStock = DB::table('S002V01TSTAR')
  3684. ->where('STAR_NULI', '=', $line)
  3685. ->where('STAR_ESTA', '=', 'Activo')
  3686. ->where('INST_NULI', '=', $line)
  3687. ->where('INST_ESTA', '=', 'Activo')
  3688. ->where('UBAR_NULI', '=', $line)
  3689. ->where('UBAR_ESTA', '=', 'Activo')
  3690. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  3691. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  3692. ->get([
  3693. 'STAR_IDST AS ID_STOCK',
  3694. 'INST_MODE AS MODELO',
  3695. 'INST_COMO AS CODIGO_MODELO',
  3696. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  3697. 'STAR_FEVE AS FECHA_VENCIMIENTO',
  3698. 'INST_IMAG AS IMAGENES',
  3699. ]);
  3700. $arrStock = json_decode(json_encode($arrStock), true);
  3701. } catch (\Throwable $th) {
  3702. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  3703. }
  3704. foreach ($arrStock as $key => $info) {
  3705. $arrImages = json_decode($info['IMAGENES']);
  3706. $arrUrlImage = array();
  3707. foreach ($arrImages as $keyImages => $images) {
  3708. $images = $this->encController->encrypt($images);
  3709. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  3710. if ($responseDocument['error']) {
  3711. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  3712. }
  3713. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  3714. }
  3715. $arrStock[$key]['IMAGENES'] = $arrUrlImage;
  3716. if (is_null($info['FECHA_VENCIMIENTO'])) {
  3717. $arrStock[$key]['FECHA_VENCIMIENTO'] = 'Ninguno';
  3718. }
  3719. }
  3720. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrStock);
  3721. }
  3722. public function updateDueDateStock(Request $request, $idStock) {
  3723. $validator = Validator::make($request->all(), [
  3724. 'FECHA_VENCIMIENTO' => 'required|string',
  3725. 'USUARIO' => 'required|string',
  3726. 'NUMERO_LINEA' => 'required|integer',
  3727. ]);
  3728. if($validator->fails()){
  3729. return $this->responseController->makeResponse(
  3730. true,
  3731. "Se encontraron uno o más errores.",
  3732. $this->responseController->makeErrors(
  3733. $validator->errors()->messages()
  3734. ),
  3735. 401
  3736. );
  3737. }
  3738. DB::beginTransaction();
  3739. $requestData = $request->all();
  3740. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  3741. if ($arrResponseCheckUser['error']) {
  3742. DB::rollBack();
  3743. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3744. }
  3745. $user = $arrResponseCheckUser['response'];
  3746. $idStock = $this->encController->decrypt($idStock);
  3747. if (is_null($idStock)) {
  3748. DB::rollBack();
  3749. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID de la información.', [], 401);
  3750. }
  3751. try {
  3752. $validateExists = DB::table('S002V01TSTAR')
  3753. ->where('STAR_IDST', '=', $idStock)
  3754. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  3755. ->where('STAR_ESTA', '=', 'Activo')
  3756. ->exists();
  3757. } catch (\Throwable $th) {
  3758. DB::rollBack();
  3759. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar el ID del stock.', $th->getMessage(), 500);
  3760. }
  3761. if (!$validateExists) {
  3762. DB::rollBack();
  3763. return $this->responseController->makeResponse(true, 'El número de stock no existe.', [], 500);
  3764. }
  3765. $now = $this->functionsController->now();
  3766. $currentDate = $now->toDateTimeString();
  3767. try {
  3768. $validateUpdate = DB::table('S002V01TSTAR')
  3769. ->where('STAR_IDST', '=', $idStock)
  3770. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  3771. ->where('STAR_ESTA', '=', 'Activo')
  3772. ->update([
  3773. 'STAR_FEVE' => $requestData['FECHA_VENCIMIENTO'],
  3774. 'STAR_USMO' => $user,
  3775. 'STAR_FEMO' => $currentDate,
  3776. 'STAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  3777. ]);
  3778. } catch (\Throwable $th) {
  3779. DB::rollBack();
  3780. return $this->responseController->makeResponse(true, 'Ocurrió un error modificar la fecha de vencimiento.', $th->getMessage(), 500);
  3781. }
  3782. if (!$validateUpdate) {
  3783. DB::rollBack();
  3784. return $this->responseController->makeResponse(true, 'No se pudo modificar la fecha de vencimiento.', [], 500);
  3785. }
  3786. DB::commit();
  3787. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  3788. }
  3789. public function getDescriptionSheetStock($user, $line) {
  3790. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  3791. if ($arrResponseCheckUser['error']) {
  3792. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3793. }
  3794. try {
  3795. $arrStock = DB::table('S002V01TSTAR')
  3796. ->where('STAR_NULI', '=', $line)
  3797. ->where('STAR_ESTA', '=', 'Activo')
  3798. ->where('INST_NULI', '=', $line)
  3799. ->where('INST_ESTA', '=', 'Activo')
  3800. ->where('UBAR_NULI', '=', $line)
  3801. ->where('UBAR_ESTA', '=', 'Activo')
  3802. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  3803. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  3804. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  3805. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  3806. ->get([
  3807. 'STAR_IDST AS ID_STOCK',
  3808. 'INST_MODE AS MODELO',
  3809. 'INST_COMO AS CODIGO_MODELO',
  3810. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  3811. 'FAMI_NOFA AS FAMILIA',
  3812. 'SUBF_NOSU AS SUBFAMILIA',
  3813. 'INST_IMAG AS IMAGENES',
  3814. ]);
  3815. $arrStock = json_decode(json_encode($arrStock), true);
  3816. } catch (\Throwable $th) {
  3817. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  3818. }
  3819. foreach ($arrStock as $key => $info) {
  3820. $arrImages = json_decode($info['IMAGENES']);
  3821. $arrUrlImage = array();
  3822. foreach ($arrImages as $keyImages => $images) {
  3823. $images = $this->encController->encrypt($images);
  3824. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  3825. if ($responseDocument['error']) {
  3826. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  3827. }
  3828. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  3829. }
  3830. $arrStock[$key]['IMAGENES'] = $arrUrlImage;
  3831. }
  3832. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrStock);
  3833. }
  3834. public function createDescriptionSheetStockXLS($idStock, $user, $line) {
  3835. $idStock = $this->encController->decrypt($idStock);
  3836. if (is_null($idStock)) {
  3837. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID del Stock.', [], 401);
  3838. }
  3839. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  3840. if ($arrResponseCheckUser['error']) {
  3841. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  3842. }
  3843. $user = $arrResponseCheckUser['response'];
  3844. try {
  3845. $arrStock = (array) DB::table('S002V01TSTAR')
  3846. ->where('STAR_IDST', '=', $idStock)
  3847. ->where('STAR_NULI', '=', $line)
  3848. ->where('INST_NULI', '=', $line)
  3849. ->where('UBAR_NULI', '=', $line)
  3850. ->where('INST_ESTA', '=', 'Activo')
  3851. ->where('UBAR_ESTA', '=', 'Activo')
  3852. ->where('STAR_ESTA', '=', 'Activo')
  3853. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  3854. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  3855. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  3856. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  3857. ->join('S002V01TALMA', 'ALMA_COAL', '=', 'UBAR_COAL')
  3858. ->join('S002V01TAREA', 'AREA_COAR', '=', 'UBAR_COAR')
  3859. ->join('S002V01TNIVE', 'NIVE_CONI', '=', 'UBAR_CONI')
  3860. ->join('S002V01TZONA', 'ZONA_COZO', '=', 'UBAR_COZO')
  3861. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'STAR_NUPR')
  3862. ->first([
  3863. 'FAMI_COFA AS CODIGO_FAMILIA',
  3864. 'FAMI_NOFA AS FAMILIA',
  3865. 'SUBF_COSU AS CODIGO_SUBFAMILIA',
  3866. 'SUBF_NOSU AS SUBFAMILIA',
  3867. 'INST_MODE AS MODELO',
  3868. 'INST_COMO AS CODIGO_MODELO',
  3869. 'INST_STMI AS STOCK_MINIMO',
  3870. 'INST_STMA AS STOCK_MAXIMO',
  3871. 'STAR_IDST AS ID_STOCK',
  3872. 'STAR_COBA AS CODIGO_BARRAS',
  3873. 'STAR_FEVE AS FECHA_VENCIMIENTO',
  3874. 'STAR_NUPR AS NUMERO_PROVEEDOR',
  3875. 'PROV_NOCO AS PROVEEDOR',
  3876. 'STAR_TIAD AS TIPO_ADQUISICION',
  3877. 'STAR_CONS AS CONSUMIBLE',
  3878. 'STAR_REPA AS REPARABLE',
  3879. 'STAR_ESTA AS ESTADO',
  3880. 'UBAR_IDUB AS ID_UBICACION',
  3881. 'ALMA_COAL AS CODIGO_ALMACEN',
  3882. 'ALMA_NOAL AS NOMBRE_ALMACEN',
  3883. 'AREA_COAR AS CODIGO_AREA',
  3884. 'AREA_NOAR AS NOMBRE_AREA',
  3885. 'NIVE_CONI AS CODIGO_NIVEL',
  3886. 'NIVE_NONI AS NOMBRE_NIVEL',
  3887. 'ZONA_COZO AS CODIGO_ZONA',
  3888. 'ZONA_NOZO AS NOMBRE_ZONA',
  3889. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  3890. ]);
  3891. } catch (\Throwable $th) {
  3892. return $this->responseController->makeResponse(true, "Ocurrió un error al recuperar la información.", $th->getMessage(), 500);
  3893. }
  3894. if (empty($arrStock) === 0) {
  3895. return $this->responseController->makeResponse(true, "No se encontró información del stock.", [], 500);
  3896. }
  3897. $spreadsheet = new Spreadsheet();
  3898. $activeWorksheet = $spreadsheet->getActiveSheet()->setTitle($arrStock['ID_STOCK']);
  3899. $cell = 1;
  3900. $activeWorksheet->setCellValue("A$cell", 'Código del Equipamiento');
  3901. $activeWorksheet->setCellValue("B$cell", $arrStock['CODIGO_EQUIPAMIENTO']);
  3902. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3903. $cell++;
  3904. $activeWorksheet->setCellValue("A$cell", 'Familia');
  3905. $activeWorksheet->setCellValue("B$cell", $arrStock['FAMILIA'].' ('.$arrStock['CODIGO_FAMILIA'].')');
  3906. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3907. $cell++;
  3908. $activeWorksheet->setCellValue("A$cell", 'Subfamilia');
  3909. $activeWorksheet->setCellValue("B$cell", $arrStock['SUBFAMILIA'].' ('.$arrStock['CODIGO_SUBFAMILIA'].')');
  3910. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3911. $cell++;
  3912. $activeWorksheet->setCellValue("A$cell", 'Modelo');
  3913. $activeWorksheet->setCellValue("B$cell", $arrStock['MODELO']);
  3914. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3915. $cell++;
  3916. $activeWorksheet->setCellValue("A$cell", 'Código Modelo');
  3917. $activeWorksheet->setCellValue("B$cell", $arrStock['CODIGO_MODELO']);
  3918. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3919. $cell++;
  3920. $activeWorksheet->setCellValue("A$cell", 'Tipo de Adquisición');
  3921. $activeWorksheet->setCellValue("B$cell", $arrStock['TIPO_ADQUISICION']);
  3922. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3923. $cell++;
  3924. if (!is_null($arrStock['NUMERO_PROVEEDOR'])) {
  3925. $activeWorksheet->setCellValue("A$cell", 'Proveedor');
  3926. $activeWorksheet->setCellValue("B$cell", $arrStock['PROVEEDOR'].' ('.$arrStock['NUMERO_PROVEEDOR'].')');
  3927. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3928. $cell++;
  3929. }
  3930. if (!is_null($arrStock['FECHA_VENCIMIENTO'])) {
  3931. $activeWorksheet->setCellValue("A$cell", 'Fecha de Vencimiento');
  3932. $activeWorksheet->setCellValue("B$cell", $arrStock['FECHA_VENCIMIENTO']);
  3933. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3934. $cell++;
  3935. }
  3936. $activeWorksheet->setCellValue("A$cell", 'Código de Barras');
  3937. $activeWorksheet->setCellValue("B$cell", $arrStock['CODIGO_BARRAS']);
  3938. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3939. $cell++;
  3940. if ( array_key_exists('TASA_ROTACION', $arrStock) ) {
  3941. $activeWorksheet->setCellValue("A$cell", 'Tasa de Rotación');
  3942. $activeWorksheet->setCellValue("B$cell", $arrStock['TASA_ROTACION']);
  3943. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3944. $cell++;
  3945. }
  3946. $activeWorksheet->setCellValue("A$cell", 'Stock Mínimo');
  3947. $activeWorksheet->setCellValue("B$cell", is_null($arrStock['STOCK_MINIMO']) ? 'Sin Definir' : $arrStock['STOCK_MINIMO']);
  3948. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3949. $cell++;
  3950. $activeWorksheet->setCellValue("A$cell", 'Stock Máximo');
  3951. $activeWorksheet->setCellValue("B$cell", is_null($arrStock['STOCK_MAXIMO']) ? 'Sin Definir' : $arrStock['STOCK_MAXIMO']);
  3952. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3953. $cell++;
  3954. $activeWorksheet->setCellValue("A$cell", '¿Es consumible?');
  3955. $activeWorksheet->setCellValue("B$cell", $arrStock['CONSUMIBLE']);
  3956. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3957. $cell++;
  3958. $activeWorksheet->setCellValue("A$cell", '¿Es reparable?');
  3959. $activeWorksheet->setCellValue("B$cell", $arrStock['REPARABLE']);
  3960. $activeWorksheet->getStyle( "A$cell" )->getFont()->setBold(true);
  3961. $cell++;
  3962. $activeWorksheet->getColumnDimension("A")->setAutoSize(true);
  3963. $activeWorksheet->getColumnDimension("B")->setAutoSize(true);
  3964. $nuli = $this->resourcesController->formatSecuence($line, 2);
  3965. $como = 'GIST'; // Código del módulo
  3966. $cldo = 'IN'; // Código de la clasificación
  3967. $fecr = date('ymd'); // Fecha en la se carga el archivo
  3968. try {
  3969. $arrSecuence = (array) DB::table('S002V01TAFAL')
  3970. ->where('AFAL_COMO', '=', $como)
  3971. ->where('AFAL_CLDO', '=', $cldo)
  3972. ->where('AFAL_NULI', '=', $line)
  3973. ->orderBy('AFAL_NUSE', 'desc')
  3974. ->first([ 'AFAL_NUSE' ]);
  3975. } catch (\Throwable $th) {
  3976. return $this->responseController->makeResponse(
  3977. true,
  3978. "ERR_PROVIDER_SHEET020: Ocurrió un error al obtener la información de la secuencia.",
  3979. $th->getMessage(),
  3980. 500
  3981. );
  3982. }
  3983. $nuse = 1; // Secuencia del documento
  3984. if ( !empty( $arrSecuence ) && !is_null( $arrSecuence ) ) {
  3985. $nuse = intval( $arrSecuence['AFAL_NUSE'] ) + 1;
  3986. }
  3987. $nuse = $this->resourcesController->formatSecuence($nuse, 6);
  3988. $nuve = $this->resourcesController->formatSecuence('1', 2);
  3989. $noar = 'ficha_de_stock_'.$arrStock['ID_STOCK'];
  3990. $exte = 'xlsx';
  3991. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  3992. $filePath = $this->resourcesController->pathService.'\\public\\public_files\\'; // API JEAN
  3993. } else {
  3994. $filePath = $this->resourcesController->pathService.'\\public_files\\'; // API QA
  3995. }
  3996. $fileName = $nuli.'-'.$como.'-'.$cldo.'-'.$fecr.'-'.$nuse.'='.$nuve.'='.$noar.'.'.$exte;
  3997. $tempFile = $filePath.$fileName;
  3998. if ( file_exists( $tempFile ) ) {
  3999. if ( !unlink( $tempFile ) ) {
  4000. return $this->responseController->makeResponse(
  4001. true,
  4002. "ERR_PROVIDER_SHEET021: Ocurrió un error al eliminar el siguiente archivo: " . $tempFile,
  4003. [],
  4004. 500
  4005. );
  4006. }
  4007. }
  4008. try {
  4009. $writer = new Xlsx($spreadsheet);
  4010. ob_start();
  4011. $writer->save('php://output');
  4012. $base64 = base64_encode(ob_get_clean());
  4013. $validate = \File::put( $tempFile, base64_decode($base64));
  4014. } catch (\Throwable $th) {
  4015. return $this->responseController->makeResponse(
  4016. true,
  4017. "ERR_PROVIDER_SHEET022: Ocurrió un error al guardar el documento.",
  4018. $th->getMessage(),
  4019. 500
  4020. );
  4021. }
  4022. $ubic = Storage::putFile('files', new File($tempFile));
  4023. $ubic = str_replace("/", "\\", $ubic);
  4024. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  4025. $ubic = $this->resourcesController->pathService."\\storage\\app\\" . $ubic;
  4026. } else {
  4027. $ubic = $this->resourcesController->pathService."\\storage\\app\\" . $ubic;
  4028. }
  4029. $tama = filesize($ubic);
  4030. $usac = json_encode([$user]);
  4031. $now = $this->functionsController->now();
  4032. $currentDate = $now->toDateTimeString();
  4033. try {
  4034. $validateInsert = DB::table('S002V01TAFAL')->insert([
  4035. 'AFAL_NULI' => $line,
  4036. 'AFAL_COMO' => $como,
  4037. 'AFAL_CLDO' => $cldo,
  4038. 'AFAL_FECR' => $fecr,
  4039. 'AFAL_NUSE' => $nuse,
  4040. 'AFAL_NUVE' => $nuve,
  4041. 'AFAL_NOAR' => $noar,
  4042. 'AFAL_EXTE' => $exte,
  4043. 'AFAL_TAMA' => $tama,
  4044. 'AFAL_UBIC' => $ubic,
  4045. 'AFAL_USAC' => $usac,
  4046. 'AFAL_USRE' => $user,
  4047. 'AFAL_FERE' => $currentDate,
  4048. ]);
  4049. } catch (\Throwable $th) {
  4050. return $this->responseController->makeResponse(
  4051. true,
  4052. "ERR_PROVIDER_SHEET023: Ocurrió un error guardar los datos a la tabla final de archivos.",
  4053. $th->getMessage(),
  4054. 500
  4055. );
  4056. }
  4057. if ( !$validateInsert ) {
  4058. return $this->responseController->makeResponse(
  4059. true,
  4060. "ERR_PROVIDER_SHEET024: No se pudo guardar la ficha del proveedor en la base de datos.",
  4061. [],
  4062. 500
  4063. );
  4064. }
  4065. $encCode = $this->encController->encrypt($fileName);
  4066. try {
  4067. $validateInsert = DB::table('S002V01TDOST')->insert([
  4068. 'DOST_NULI' => $line,
  4069. 'DOST_IDST' => $idStock,
  4070. 'DOST_NODO' => 'Ficha de Stock #'.$arrStock['ID_STOCK'],
  4071. 'DOST_CODO' => $encCode,
  4072. 'DOST_EXTE' => $exte,
  4073. 'DOST_TAMA' => $tama,
  4074. 'DOST_CLAS' => 'IN',
  4075. 'DOST_VERS' => 1,
  4076. 'DOST_USRE' => $user,
  4077. 'DOST_FERE' => $currentDate,
  4078. 'DOST_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  4079. ]);
  4080. } catch (\Throwable $th) {
  4081. return $this->responseController->makeResponse(
  4082. true,
  4083. "Ocurrió un error guardar los datos a la tabla final de archivos.",
  4084. $th->getMessage(),
  4085. 500
  4086. );
  4087. }
  4088. if (!$validateInsert) {
  4089. return $this->responseController->makeResponse(
  4090. true,
  4091. "No se pudo guardar la ficha del proveedor en la base de datos.",
  4092. [],
  4093. 500
  4094. );
  4095. }
  4096. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  4097. $urlPublic = 'http://192.168.100.105:8000/public_files/' . $fileName;
  4098. } else {
  4099. $urlPublic = $this->functionsController->getApiURI().'sam/public_files/' . $fileName;
  4100. }
  4101. return $this->responseController->makeResponse(false, "EXITO: Modificación Exitosa", [ 'url' => $urlPublic ]);
  4102. }
  4103. public function createDescriptionSheetStockPDF($idStock, $user, $line) {
  4104. $idStock = $this->encController->decrypt($idStock);
  4105. if (is_null($idStock)) {
  4106. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID del Stock.', [], 401);
  4107. }
  4108. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  4109. if ($arrResponseCheckUser['error']) {
  4110. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  4111. }
  4112. $user = $arrResponseCheckUser['response'];
  4113. try {
  4114. $arrStock = (array) DB::table('S002V01TSTAR')
  4115. ->where('STAR_IDST', '=', $idStock)
  4116. ->where('STAR_NULI', '=', $line)
  4117. ->where('INST_NULI', '=', $line)
  4118. ->where('UBAR_NULI', '=', $line)
  4119. ->where('INST_ESTA', '=', 'Activo')
  4120. ->where('UBAR_ESTA', '=', 'Activo')
  4121. ->where('STAR_ESTA', '=', 'Activo')
  4122. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  4123. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  4124. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  4125. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  4126. ->join('S002V01TALMA', 'ALMA_COAL', '=', 'UBAR_COAL')
  4127. ->join('S002V01TAREA', 'AREA_COAR', '=', 'UBAR_COAR')
  4128. ->join('S002V01TNIVE', 'NIVE_CONI', '=', 'UBAR_CONI')
  4129. ->join('S002V01TZONA', 'ZONA_COZO', '=', 'UBAR_COZO')
  4130. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'STAR_NUPR')
  4131. ->first([
  4132. 'FAMI_COFA AS CODIGO_FAMILIA',
  4133. 'FAMI_NOFA AS FAMILIA',
  4134. 'SUBF_COSU AS CODIGO_SUBFAMILIA',
  4135. 'SUBF_NOSU AS SUBFAMILIA',
  4136. 'INST_MODE AS MODELO',
  4137. 'INST_COMO AS CODIGO_MODELO',
  4138. 'INST_STMI AS STOCK_MINIMO',
  4139. 'INST_STMA AS STOCK_MAXIMO',
  4140. 'STAR_IDST AS ID_STOCK',
  4141. 'STAR_COBA AS CODIGO_BARRAS',
  4142. 'STAR_FEVE AS FECHA_VENCIMIENTO',
  4143. 'PROV_NUPR AS NUMERO_PROVEEDOR',
  4144. 'PROV_NOCO AS PROVEEDOR',
  4145. 'STAR_TIAD AS TIPO_ADQUISICION',
  4146. 'STAR_CONS AS CONSUMIBLE',
  4147. 'STAR_REPA AS REPARABLE',
  4148. 'STAR_ESTA AS ESTADO',
  4149. 'UBAR_IDUB AS ID_UBICACION',
  4150. 'ALMA_COAL AS CODIGO_ALMACEN',
  4151. 'ALMA_NOAL AS NOMBRE_ALMACEN',
  4152. 'AREA_COAR AS CODIGO_AREA',
  4153. 'AREA_NOAR AS NOMBRE_AREA',
  4154. 'NIVE_CONI AS CODIGO_NIVEL',
  4155. 'NIVE_NONI AS NOMBRE_NIVEL',
  4156. 'ZONA_COZO AS CODIGO_ZONA',
  4157. 'ZONA_NOZO AS NOMBRE_ZONA',
  4158. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  4159. ]);
  4160. } catch (\Throwable $th) {
  4161. return $this->responseController->makeResponse(true, "Ocurrió un error al recuperar la información.", $th->getMessage(), 500);
  4162. }
  4163. if (empty($arrStock) === 0) {
  4164. return $this->responseController->makeResponse(true, "No se encontró información del stock.", [], 500);
  4165. }
  4166. $html = '<!DOCTYPE html>
  4167. <html lang="en">
  4168. <head>
  4169. <meta charset="UTF-8">
  4170. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  4171. <title>Document</title>
  4172. <style>
  4173. body {
  4174. font-family: Arial, Helvetica, sans-serif;
  4175. font-size: 8pt;
  4176. }
  4177. .section-title {
  4178. margin: 20px 0;
  4179. font-size: 12pt;
  4180. }
  4181. table {
  4182. width: 100%;
  4183. font-size: 8pt;
  4184. }
  4185. thead tr th {
  4186. text-align: start;
  4187. padding: 0 16px;
  4188. height: 52px;
  4189. border-bottom-width: 1px;
  4190. border-bottom-style: solid;
  4191. border-bottom-color: #CCC;
  4192. }
  4193. tbody tr td {
  4194. padding: 0 16px;
  4195. height: 52px;
  4196. border-bottom-width: 1px;
  4197. border-bottom-style: solid;
  4198. border-bottom-color: #CCC;
  4199. }
  4200. p {
  4201. text-align: justify;
  4202. }
  4203. </style>
  4204. </head>
  4205. <body>';
  4206. $html.='<div class="item">
  4207. <b>Código del Equipamiento</b>
  4208. <span>'.$arrStock['CODIGO_EQUIPAMIENTO'].'</span>
  4209. </div>';
  4210. $html.='<div class="item">
  4211. <b>Familia</b>
  4212. <span>'.$arrStock['FAMILIA'].' ('.$arrStock['CODIGO_FAMILIA'].')'.'</span>
  4213. </div>';
  4214. $html.='<div class="item">
  4215. <b>Subfamilia</b>
  4216. <span>'.$arrStock['SUBFAMILIA'].' ('.$arrStock['CODIGO_SUBFAMILIA'].')'.'</span>
  4217. </div>';
  4218. $html.='<div class="item">
  4219. <b>Modelo</b>
  4220. <span>'.$arrStock['MODELO'].'</span>
  4221. </div>';
  4222. $html.='<div class="item">
  4223. <b>Código Modelo</b>
  4224. <span>'.$arrStock['CODIGO_MODELO'].'</span>
  4225. </div>';
  4226. $html.='<div class="item">
  4227. <b>Tipo de Adquisición</b>
  4228. <span>'.$arrStock['TIPO_ADQUISICION'].'</span>
  4229. </div>';
  4230. $html.='<div class="item">
  4231. <b>Proveedor</b>
  4232. <span>'.$arrStock['PROVEEDOR'].' ('.$arrStock['NUMERO_PROVEEDOR'].')'.'</span>
  4233. </div>';
  4234. $html.='<div class="item">
  4235. <b>Fecha de Vencimiento</b>
  4236. <span>'.$arrStock['FECHA_VENCIMIENTO'].'</span>
  4237. </div>';
  4238. $html.='<div class="item">
  4239. <b>Código de Barras</b>
  4240. <span>'.$arrStock['CODIGO_BARRAS'].'</span>
  4241. </div>';
  4242. if ( array_key_exists('TASA_ROTACION', $arrStock) ) {
  4243. $html.='<div class="item">
  4244. <b>Tasa de Rotación</b>
  4245. <span>'.$arrStock['TASA_ROTACION'].'</span>
  4246. </div>';
  4247. }
  4248. $html.='<div class="item">
  4249. <b>Stock Mínimo</b>
  4250. <span>'.is_null($arrStock['STOCK_MINIMO']) ? 'Sin Definir' : $arrStock['STOCK_MINIMO'].'</span>
  4251. </div>';
  4252. $html.='<div class="item">
  4253. <b>Stock Máximo</b>
  4254. <span>'.is_null($arrStock['STOCK_MAXIMO']) ? 'Sin Definir' : $arrStock['STOCK_MAXIMO'].'</span>
  4255. </div>';
  4256. $html.='<div class="item">
  4257. <b>¿Es consumible?</b>
  4258. <span>'.$arrStock['CONSUMIBLE'].'</span>
  4259. </div>';
  4260. $html.='<div class="item">
  4261. <b>¿Es reparable?</b>
  4262. <span>'.$arrStock['REPARABLE'].'</span>
  4263. </div>';
  4264. $html.='</body></html>';
  4265. $nuli = $this->resourcesController->formatSecuence($line, 2);
  4266. $como = 'GIST'; // Código del módulo
  4267. $cldo = 'IN'; // Código de la clasificación
  4268. $fecr = date('ymd'); // Fecha en la se carga el archivo
  4269. try {
  4270. $arrSecuence = (array) DB::table('S002V01TAFAL')
  4271. ->where('AFAL_COMO', '=', $como)
  4272. ->where('AFAL_CLDO', '=', $cldo)
  4273. ->where('AFAL_NULI', '=', $line)
  4274. ->orderBy('AFAL_NUSE', 'desc')
  4275. ->first([ 'AFAL_NUSE' ]);
  4276. } catch (\Throwable $th) {
  4277. return $this->responseController->makeResponse(
  4278. true,
  4279. "ERR_PROVIDER_SHEET020: Ocurrió un error al obtener la información de la secuencia.",
  4280. $th->getMessage(),
  4281. 500
  4282. );
  4283. }
  4284. $nuse = 1; // Secuencia del documento
  4285. if ( !empty( $arrSecuence ) && !is_null( $arrSecuence ) ) {
  4286. $nuse = intval( $arrSecuence['AFAL_NUSE'] ) + 1;
  4287. }
  4288. $nuse = $this->resourcesController->formatSecuence($nuse, 6);
  4289. $nuve = $this->resourcesController->formatSecuence('1', 2);
  4290. $noar = 'ficha_de_adquisición_' . $arrStock['ID_STOCK'];
  4291. $nuli = $this->resourcesController->formatSecuence($line, 2);
  4292. $como = 'GIST'; // Código del módulo
  4293. $cldo = 'IN'; // Código de la clasificación
  4294. $fecr = date('ymd'); // Fecha en la se carga el archivo
  4295. try {
  4296. $arrSecuence = (array) DB::table('S002V01TAFAL')
  4297. ->where('AFAL_COMO', '=', $como)
  4298. ->where('AFAL_CLDO', '=', $cldo)
  4299. ->where('AFAL_NULI', '=', $line)
  4300. ->orderBy('AFAL_NUSE', 'desc')
  4301. ->first([ 'AFAL_NUSE' ]);
  4302. } catch (\Throwable $th) {
  4303. return $this->responseController->makeResponse(
  4304. true,
  4305. "ERR_PROVIDER_SHEET020: Ocurrió un error al obtener la información de la secuencia.",
  4306. $th->getMessage(),
  4307. 500
  4308. );
  4309. }
  4310. $nuse = 1; // Secuencia del documento
  4311. if ( !empty( $arrSecuence ) && !is_null( $arrSecuence ) ) {
  4312. $nuse = intval( $arrSecuence['AFAL_NUSE'] ) + 1;
  4313. }
  4314. $nuse = $this->resourcesController->formatSecuence($nuse, 6);
  4315. $nuve = $this->resourcesController->formatSecuence('1', 2);
  4316. $noar = 'ficha_de_stock_' . $arrStock['ID_STOCK'];
  4317. $exte = 'pdf';
  4318. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  4319. $filePath = $this->resourcesController->pathService.'\\public\\public_files\\'; // API JEAN
  4320. } else {
  4321. $filePath = $this->resourcesController->pathService.'\\public_files\\'; // API QA
  4322. }
  4323. $fileName = $nuli.'-'.$como.'-'.$cldo.'-'.$fecr.'-'.$nuse.'='.$nuve.'='.$noar.'.'.$exte;
  4324. $tempFile = $filePath . $fileName;
  4325. $dompdf = new Dompdf();
  4326. $dompdf ->loadHtml($html);
  4327. $dompdf->setPaper('A4', 'landscape');
  4328. $dompdf->render();
  4329. $output = $dompdf->output();
  4330. file_put_contents($tempFile, $output);
  4331. $ubic = Storage::putFile('files', new File($tempFile));
  4332. $ubic = str_replace("/", "\\", $ubic);
  4333. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  4334. $ubic = $this->resourcesController->pathService."\\storage\\app\\" . $ubic;
  4335. } else {
  4336. $ubic = $this->resourcesController->pathService."\\storage\\app\\" . $ubic;
  4337. }
  4338. $tama = filesize($ubic);
  4339. $usac = json_encode([$user]);
  4340. $now = $this->functionsController->now();
  4341. $currentDate = $now->toDateTimeString();
  4342. try {
  4343. $validateInsert = DB::table('S002V01TAFAL')->insert([
  4344. 'AFAL_NULI' => $line,
  4345. 'AFAL_COMO' => $como,
  4346. 'AFAL_CLDO' => $cldo,
  4347. 'AFAL_FECR' => $fecr,
  4348. 'AFAL_NUSE' => $nuse,
  4349. 'AFAL_NUVE' => $nuve,
  4350. 'AFAL_NOAR' => $noar,
  4351. 'AFAL_EXTE' => $exte,
  4352. 'AFAL_TAMA' => $tama,
  4353. 'AFAL_UBIC' => $ubic,
  4354. 'AFAL_USAC' => $usac,
  4355. 'AFAL_USRE' => $user,
  4356. 'AFAL_FERE' => $currentDate,
  4357. ]);
  4358. } catch (\Throwable $th) {
  4359. return $this->responseController->makeResponse(
  4360. true,
  4361. "ERR_PROVIDER_SHEET023: Ocurrió un error guardar los datos a la tabla final de archivos.",
  4362. $th->getMessage(),
  4363. 500
  4364. );
  4365. }
  4366. if ( !$validateInsert ) {
  4367. return $this->responseController->makeResponse(
  4368. true,
  4369. "ERR_PROVIDER_SHEET024: No se pudo guardar la ficha del proveedor en la base de datos.",
  4370. [],
  4371. 500
  4372. );
  4373. }
  4374. $encCode = $this->encController->encrypt($fileName);
  4375. try {
  4376. $validateInsert = DB::table('S002V01TDOST')->insert([
  4377. 'DOST_NULI' => $line,
  4378. 'DOST_IDST' => $idStock,
  4379. 'DOST_NODO' => 'Ficha de Stock #'.$arrStock['ID_STOCK'],
  4380. 'DOST_CODO' => $encCode,
  4381. 'DOST_EXTE' => $exte,
  4382. 'DOST_TAMA' => $tama,
  4383. 'DOST_CLAS' => 'IN',
  4384. 'DOST_VERS' => 1,
  4385. 'DOST_USRE' => $user,
  4386. 'DOST_FERE' => $currentDate,
  4387. 'DOST_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  4388. ]);
  4389. } catch (\Throwable $th) {
  4390. return $this->responseController->makeResponse(
  4391. true,
  4392. "Ocurrió un error guardar los datos a la tabla final de archivos.",
  4393. $th->getMessage(),
  4394. 500
  4395. );
  4396. }
  4397. if (!$validateInsert) {
  4398. return $this->responseController->makeResponse(
  4399. true,
  4400. "No se pudo guardar la ficha del proveedor en la base de datos.",
  4401. [],
  4402. 500
  4403. );
  4404. }
  4405. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  4406. $urlPublic = 'http://192.168.100.105:8000/public_files/' . $fileName;
  4407. } else {
  4408. $urlPublic = $this->functionsController->getApiURI().'sam/public_files/' . $fileName;
  4409. }
  4410. return $this->responseController->makeResponse(false, "EXITO: Modificación Exitosa", [ 'url' => $urlPublic ]);
  4411. }
  4412. public function getDocumentAssociation($user, $line) {
  4413. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  4414. if ($arrResponseCheckUser['error']) {
  4415. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  4416. }
  4417. try {
  4418. $arrStock = DB::table('S002V01TSTAR')
  4419. ->where('STAR_NULI', '=', $line)
  4420. ->where('STAR_ESTA', '=', 'Activo')
  4421. ->where('INST_NULI', '=', $line)
  4422. ->where('INST_ESTA', '=', 'Activo')
  4423. ->where('UBAR_NULI', '=', $line)
  4424. ->where('UBAR_ESTA', '=', 'Activo')
  4425. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  4426. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  4427. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  4428. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  4429. ->get([
  4430. 'STAR_IDST AS ID_STOCK',
  4431. 'INST_MODE AS MODELO',
  4432. 'INST_COMO AS CODIGO_MODELO',
  4433. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  4434. 'FAMI_NOFA AS FAMILIA',
  4435. 'SUBF_NOSU AS SUBFAMILIA',
  4436. 'INST_IMAG AS IMAGENES',
  4437. 'STAR_NUPR AS NUMERO_PROVEEDOR'
  4438. ]);
  4439. $arrStock = json_decode(json_encode($arrStock), true);
  4440. } catch (\Throwable $th) {
  4441. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  4442. }
  4443. foreach ($arrStock as $key => $stock) {
  4444. $arrImages = json_decode($stock['IMAGENES']);
  4445. $arrUrlImage = array();
  4446. foreach ($arrImages as $keyImages => $images) {
  4447. $images = $this->encController->encrypt($images);
  4448. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  4449. if ($responseDocument['error']) {
  4450. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  4451. }
  4452. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  4453. }
  4454. $arrStock[$key]['IMAGENES'] = $arrUrlImage;
  4455. try {
  4456. $count = DB::table('S002V01TDOST')
  4457. ->where('DOST_IDST', '=', $stock['ID_STOCK'])
  4458. ->where('DOST_NULI', '=', $line)
  4459. ->where('DOST_ESTA', '=', 'Activo')
  4460. ->count();
  4461. } catch (\Throwable $th) {
  4462. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la cantidad de documentos.', $th->getMessage(), 500);
  4463. }
  4464. $arrStock[$key]['CANTIDAD_DOCUMENTOS'] = $count;
  4465. }
  4466. return $this->responseController->makeResponse(false, "ÉXITO: Consulta ExitosaA", $arrStock);
  4467. }
  4468. public function getDocumentsByStock($idStock, $user, $line) {
  4469. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  4470. if ($arrResponseCheckUser['error']) {
  4471. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  4472. }
  4473. $idStock = $this->encController->decrypt($idStock);
  4474. if (is_null($idStock)) {
  4475. DB::rollBack();
  4476. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID de la información.', [], 401);
  4477. }
  4478. try {
  4479. $validateExists = DB::table('S002V01TSTAR')
  4480. ->where('STAR_IDST', '=', $idStock)
  4481. ->where('STAR_NULI', '=', $line)
  4482. ->where('STAR_ESTA', '=', 'Activo')
  4483. ->exists();
  4484. } catch (\Throwable $th) {
  4485. DB::rollBack();
  4486. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar el ID del stock.', $th->getMessage(), 500);
  4487. }
  4488. if (!$validateExists) {
  4489. DB::rollBack();
  4490. return $this->responseController->makeResponse(true, 'El número de stock no existe.', [], 500);
  4491. }
  4492. try {
  4493. $arrDocuments = DB::table('S002V01TDOST')
  4494. ->where('DOST_IDST', '=', $idStock)
  4495. ->where('DOST_NULI', '=', $line)
  4496. ->get([
  4497. 'DOST_IDDO AS ID_DOCUMENTO',
  4498. 'DOST_CODO AS CODIGO_DOCUMENTO',
  4499. 'DOST_NODO AS NOMBRE_DOCUMENTO',
  4500. 'DOST_CLAS AS CLASIFICACION',
  4501. 'DOST_EXTE AS EXTENSION',
  4502. 'DOST_VERS AS VERSION',
  4503. 'DOST_TAMA AS TAMANO',
  4504. 'DOST_ESTA AS ESTADO',
  4505. 'DOST_USRE AS USUARIO_REGISTRA',
  4506. 'DOST_FERE AS FECHA_REGISTRA',
  4507. 'DOST_USMO AS USUARIO_MODIFICA',
  4508. 'DOST_FEMO AS FECHA_MODIFICA',
  4509. ]);
  4510. $arrDocuments = json_decode(json_encode($arrDocuments), true);
  4511. } catch (\Throwable $th) {
  4512. DB::rollBack();
  4513. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  4514. }
  4515. $responseCheckLatestUpdate = $this->resourcesController->checkLatestUpdate($arrDocuments, $line);
  4516. if ($responseCheckLatestUpdate['error']) {
  4517. return $this->responseController->makeResponse(true, $responseCheckLatestUpdate['msg'], [], 500);
  4518. }
  4519. $arrDocuments = $responseCheckLatestUpdate['response'];
  4520. foreach ($arrDocuments as $keyDocument => $document) {
  4521. foreach ($document as $key => $value) {
  4522. if ($key === 'ID_DOCUMENTO') {
  4523. $document[$key] = '#'.$value;
  4524. }
  4525. if ($key === 'CLASIFICACION') {
  4526. $document[$key] = $this->resourcesController->arrClasificateDocument[$value];
  4527. }
  4528. if ($key === 'TAMANO') {
  4529. $document[$key] = ($value / 1000).' KB';
  4530. }
  4531. }
  4532. $arrDocuments[$keyDocument] = $document;
  4533. }
  4534. return $this->responseController->makeResponse(false, "EXITO: Consulta Exitosa", $arrDocuments);
  4535. }
  4536. public function registerDocumentStock(Request $request) {
  4537. $valitador = Validator::make($request->all(), [
  4538. 'ID_STOCK' => 'required|string',
  4539. 'NOMBRE_ARCHIVO' => 'required|string',
  4540. 'TIPO_ARCHIVO' => 'required|string',
  4541. 'ARCHIVO' => 'required|string',
  4542. 'USUARIO' => 'required|string',
  4543. 'NUMERO_LINEA' => 'required|integer',
  4544. ]);
  4545. if ($valitador->fails()) {
  4546. return $this->responseController->makeResponse(
  4547. true,
  4548. "ERR_DOCUMENT_ORDER_REG000: Se encontraron uno o más errores.",
  4549. $this->responseController->makeErrors($valitador->errors()->messages()),
  4550. 401
  4551. );
  4552. }
  4553. DB::beginTransaction();
  4554. $requestData = $request->all();
  4555. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  4556. if ($arrResponseCheckUser['error']) {
  4557. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  4558. }
  4559. $user = $arrResponseCheckUser['response'];
  4560. try {
  4561. $idFile = $this->encController->decrypt($requestData['ARCHIVO']);
  4562. } catch (\Throwable $th) {
  4563. DB::rollBack();
  4564. return $this->responseController->makeResponse(true, "ERR_DOCUMENT_ORDER_REG002: Ocurrió un error al desencriptar el documento.", $th->getMessage(), 401);
  4565. }
  4566. $tempFile = DB::table('S002V01TARTE')->where([
  4567. ['ARTE_NULI', '=', $requestData['NUMERO_LINEA']],
  4568. ['ARTE_IDAR', '=', $idFile],
  4569. ])->first();
  4570. if(is_null($tempFile)){
  4571. return $this->responseController->makeResponse(true, 'ERR_DOCUMENT_ORDER_REG003: El archivo consultado no está registrado', [], 404);
  4572. }else if($tempFile->ARTE_ESTA == 'Eliminado'){
  4573. return $this->responseController->makeResponse(true, 'ERR_DOCUMENT_ORDER_REG004: El archivo consultado está eliminado', [], 404);
  4574. }
  4575. $fileResponse = $this->documentManagementController->moveFinalFile(
  4576. intval($requestData['NUMERO_LINEA']),
  4577. 'GEAD',
  4578. $requestData['TIPO_ARCHIVO'],
  4579. $tempFile,
  4580. $user,
  4581. );
  4582. if(!$fileResponse[0]){
  4583. return $this->responseController->makeResponse(true, 'ERR_DOCUMENT_ORDER_REG005: '.$fileResponse[1], [], 400);
  4584. }
  4585. $encCode = $this->encController->encrypt($fileResponse[1]);
  4586. $now = $this->functionsController->now();
  4587. $currentDate = $now->toDateTimeString();
  4588. try {
  4589. $validateInsert = DB::table('S002V01TDOST')->insert([
  4590. 'DOST_NULI' => $requestData['NUMERO_LINEA'],
  4591. 'DOST_IDST' => $requestData['ID_STOCK'],
  4592. 'DOST_NODO' => $requestData['NOMBRE_ARCHIVO'],
  4593. 'DOST_CODO' => $encCode,
  4594. 'DOST_EXTE' => $tempFile->ARTE_EXTE,
  4595. 'DOST_TAMA' => $tempFile->ARTE_TAMA,
  4596. 'DOST_CLAS' => $requestData['TIPO_ARCHIVO'],
  4597. 'DOST_VERS' => 1,
  4598. 'DOST_USRE' => $user,
  4599. 'DOST_FERE' => $currentDate,
  4600. 'DOST_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  4601. ]);
  4602. } catch (\Throwable $th) {
  4603. return $this->responseController->makeResponse(
  4604. true,
  4605. "Ocurrió un error guardar los datos a la tabla final de archivos.",
  4606. $th->getMessage(),
  4607. 500
  4608. );
  4609. }
  4610. if (!$validateInsert) {
  4611. return $this->responseController->makeResponse(
  4612. true,
  4613. "No se pudo guardar la ficha del proveedor en la base de datos.",
  4614. [],
  4615. 500
  4616. );
  4617. }
  4618. DB::commit();
  4619. return $this->responseController->makeResponse(false, "ÉXITO: Registro de Documento Exitoso");
  4620. }
  4621. public function getReplenishmentQuantitiesService($user, $line) {
  4622. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  4623. if ($arrResponseCheckUser['error']) {
  4624. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  4625. }
  4626. try {
  4627. $arrStock = DB::table('S002V01TSTAR')
  4628. ->where('STAR_TIAD', '=', 'Por Pedido')
  4629. ->where('STAR_NULI', '=', $line)
  4630. ->where('STAR_ESTA', '=', 'Activo')
  4631. ->where('INST_NULI', '=', $line)
  4632. ->where('INST_ESTA', '=', 'Activo')
  4633. ->where('UBAR_NULI', '=', $line)
  4634. ->where('UBAR_ESTA', '=', 'Activo')
  4635. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  4636. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  4637. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  4638. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  4639. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'STAR_NUPR')
  4640. ->get([
  4641. 'STAR_IDST AS ID_STOCK',
  4642. 'INST_MODE AS MODELO',
  4643. 'INST_COMO AS CODIGO_MODELO',
  4644. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  4645. 'FAMI_NOFA AS FAMILIA',
  4646. 'SUBF_NOSU AS SUBFAMILIA',
  4647. 'INST_IMAG AS IMAGENES',
  4648. 'PROV_NUPR AS NUMERO_PROVEEDOR',
  4649. 'PROV_NOCO AS NOMBRE_PROVEEDOR',
  4650. ]);
  4651. $arrStock = json_decode(json_encode($arrStock), true);
  4652. } catch (\Throwable $th) {
  4653. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  4654. }
  4655. foreach ($arrStock as $key => $info) {
  4656. $arrImages = json_decode($info['IMAGENES']);
  4657. $arrUrlImage = array();
  4658. foreach ($arrImages as $keyImages => $images) {
  4659. $images = $this->encController->encrypt($images);
  4660. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  4661. if ($responseDocument['error']) {
  4662. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  4663. }
  4664. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  4665. }
  4666. $arrStock[$key]['IMAGENES'] = $arrUrlImage;
  4667. $arrStock[$key]['CANTIDAD'] = 0;
  4668. }
  4669. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrStock);
  4670. }
  4671. public function createRequestLine(Request $request) {
  4672. $valitador = Validator::make($request->all(), [
  4673. 'REQUEST_LINE' => 'required|array',
  4674. 'USUARIO' => 'required|string',
  4675. 'NUMERO_LINEA' => 'required|integer',
  4676. ]);
  4677. if ($valitador->fails()) {
  4678. return $this->responseController->makeResponse(
  4679. true,
  4680. "ERR_DOCUMENT_ORDER_REG000: Se encontraron uno o más errores.",
  4681. $this->responseController->makeErrors($valitador->errors()->messages()),
  4682. 401
  4683. );
  4684. }
  4685. DB::beginTransaction();
  4686. $requestData = $request->all();
  4687. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  4688. if ($arrResponseCheckUser['error']) {
  4689. DB::rollBack();
  4690. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  4691. }
  4692. $user = $arrResponseCheckUser['response'];
  4693. $now = $this->functionsController->now();
  4694. $currentDate = $now->toDateTimeString();
  4695. $arrProviders = array();
  4696. $arrRequestData = array();
  4697. foreach ($requestData['REQUEST_LINE'] as $keyRequest => $request) {
  4698. try {
  4699. $validateInsert = DB::table('S002V01TCARE')->insert([
  4700. 'CARE_NULI' => $requestData['NUMERO_LINEA'],
  4701. 'CARE_IDST' => $request['ID_STOCK'],
  4702. 'CARE_CAAD' => $request['CANTIDAD'],
  4703. 'CARE_USRE' => $user,
  4704. 'CARE_FERE' => $currentDate,
  4705. 'CARE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  4706. ]);
  4707. } catch (\Throwable $th) {
  4708. DB::rollBack();
  4709. return $this->responseController->makeResponse(true, 'Ocurrió un error al guar la información del reabastecimiento del stock.', $th->getMessage(), 500);
  4710. }
  4711. if (!$validateInsert) {
  4712. DB::rollBack();
  4713. return $this->responseController->makeResponse(true, 'No se pudo guardar los datos del reabastecimiento dentro de la base de datos.', [], 500);
  4714. }
  4715. try {
  4716. $arrStock = (array) DB::table('S002V01TSTAR')
  4717. ->where('STAR_IDST', '=', $request['ID_STOCK'])
  4718. ->where('STAR_TIAD', '=', 'Por Pedido')
  4719. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  4720. ->where('STAR_ESTA', '=', 'Activo')
  4721. ->where('INST_NULI', '=', $requestData['NUMERO_LINEA'])
  4722. ->where('INST_ESTA', '=', 'Activo')
  4723. ->where('UBAR_NULI', '=', $requestData['NUMERO_LINEA'])
  4724. ->where('UBAR_ESTA', '=', 'Activo')
  4725. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  4726. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  4727. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  4728. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  4729. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'STAR_NUPR')
  4730. ->first([
  4731. 'INST_MODE AS MODELO',
  4732. 'INST_COMO AS CODIGO_MODELO',
  4733. 'FAMI_COFA AS FAMILIA',
  4734. 'SUBF_COSU AS SUBFAMILIA',
  4735. 'PROV_NUPR AS NUMERO_PROVEEDOR',
  4736. ]);
  4737. } catch (\Throwable $th) {
  4738. DB::rollBack();
  4739. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  4740. }
  4741. if (empty($arrStock)) {
  4742. DB::rollBack();
  4743. return $this->responseController->makeResponse(true, 'No se encontró la información del Stock', [], 500);
  4744. }
  4745. if ( !in_array($arrStock['NUMERO_PROVEEDOR'], $arrProviders) ) {
  4746. $arrProviders[] = $arrStock['NUMERO_PROVEEDOR'];
  4747. }
  4748. $request['MODELO'] = $arrStock['MODELO'];
  4749. $request['CODIGO_MODELO'] = $arrStock['CODIGO_MODELO'];
  4750. $request['FAMILIA'] = $arrStock['FAMILIA'];
  4751. $request['SUBFAMILIA'] = $arrStock['SUBFAMILIA'];
  4752. $request['NUMERO_PROVEEDOR'] = $arrStock['NUMERO_PROVEEDOR'];
  4753. try {
  4754. $infoAdquisition = (array) DB::table('S002V01TINAR')
  4755. ->where('INAR_NULI', '=', $requestData['NUMERO_LINEA'])
  4756. ->where('INAR_CODI', '=', $arrStock['CODIGO_MODELO'])
  4757. ->where('INAR_MODE', '=', $arrStock['MODELO'])
  4758. ->where('INAR_ESTA', '=', 'Activo')
  4759. ->where('DEAR_NULI', '=', $requestData['NUMERO_LINEA'])
  4760. ->where('DEAR_NUPR', '=', $arrStock['NUMERO_PROVEEDOR'])
  4761. ->where('DEAR_ESTA', '=', 'Activo')
  4762. ->where('ARTI_NULI', '=', $requestData['NUMERO_LINEA'])
  4763. ->where('ARTI_COFA', '=', $arrStock['FAMILIA'])
  4764. ->where('ARTI_COSU', '=', $arrStock['SUBFAMILIA'])
  4765. ->where('ARTI_ESTA', '=', 'Activo')
  4766. ->join('S002V01TDEAR', 'DEAR_IDDE', '=', 'INAR_IDDE')
  4767. ->join('S002V01TARTI', 'ARTI_IDAR', '=', 'DEAR_IDAR')
  4768. ->first([
  4769. 'ARTI_IDAR AS ID_ARTICULO',
  4770. 'INAR_IDIN AS ID_INFORMACION',
  4771. ]);
  4772. } catch (\Throwable $th) {
  4773. DB::rollBack();
  4774. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información de la adquisición.', $th->getMessage(), 500);
  4775. }
  4776. if (empty($infoAdquisition)) {
  4777. DB::rollBack();
  4778. return $this->responseController->makeResponse(true, 'No se encontró la información de la adquisición.', [], 500);
  4779. }
  4780. $request['ID_ARTICULO'] = $infoAdquisition['ID_ARTICULO'];
  4781. $request['ID_INFORMACION'] = $infoAdquisition['ID_INFORMACION'];
  4782. $arrRequestData[$keyRequest] = $request;
  4783. }
  4784. foreach ($arrProviders as $provider) {
  4785. $contItemRequest = 1;
  4786. foreach ($arrRequestData as $key => $value) {
  4787. if ($provider === $value['NUMERO_PROVEEDOR']) {
  4788. try {
  4789. $idRequest = DB::table('S002V01TLINE')->insertGetId([
  4790. 'LINE_NULI' => $requestData['NUMERO_LINEA'],
  4791. 'LINE_NUPR' => $value['NUMERO_PROVEEDOR'],
  4792. 'LINE_ESTA' => 'En espera',
  4793. 'LINE_USRE' => $user,
  4794. 'LINE_FERE' => $currentDate,
  4795. 'LINE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  4796. ]);
  4797. } catch (\Throwable $th) {
  4798. DB::rollBack();
  4799. return $this->responseController->makeResponse(true, 'Ocurrió un error al insertar la información en la línea de solicitud.', $th->getMessage(), 500);
  4800. }
  4801. if ($idRequest <= 0) {
  4802. DB::rollBack();
  4803. return $this->responseController->makeResponse(true, 'No se pudo insertar la información de la línea de solicitud.', [], 500);
  4804. }
  4805. for ($i=0; $i < $value['CANTIDAD']; $i++) {
  4806. try {
  4807. $validateInsert = DB::table('S002V01TARSE')->insert([
  4808. 'ARSE_IDAS' => $contItemRequest,
  4809. 'ARSE_IDLI' => $idRequest,
  4810. 'ARSE_ESTA' => 'Activo',
  4811. 'ARSE_NULI' => $requestData['NUMERO_LINEA'],
  4812. 'ARSE_IDAR' => $value['ID_ARTICULO'],
  4813. 'ARSE_NUPR' => $value['NUMERO_PROVEEDOR'],
  4814. 'ARSE_IDIN' => $value['ID_INFORMACION'],
  4815. 'ARSE_USRE' => $user,
  4816. 'ARSE_FERE' => $currentDate,
  4817. 'ARSE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  4818. ]);
  4819. } catch (\Throwable $th) {
  4820. DB::rollBack();
  4821. return $this->responseController->makeResponse(true, 'Ocurrió un error al insertar los artículos en la línea de solicitud.', $th->getMessage(), 500);
  4822. }
  4823. if (!$validateInsert) {
  4824. DB::rollBack();
  4825. return $this->responseController->makeResponse(true, 'No se pudo insertar los artículos en la línea de solicitud.', [], 500);
  4826. }
  4827. $contItemRequest++;
  4828. }
  4829. }
  4830. }
  4831. }
  4832. DB::commit();
  4833. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  4834. }
  4835. public function getReplenishmentSuggestion($user, $line) {
  4836. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  4837. if ($arrResponseCheckUser['error']) {
  4838. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  4839. }
  4840. try {
  4841. $arrInfoStock = DB::table('S002V01TINST')
  4842. ->where('INST_NULI', '=', $line)
  4843. ->where('INST_ESTA', '=', 'Activo')
  4844. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  4845. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  4846. ->join('S002V01TSTAR', 'STAR_IDIS', '=', 'INST_IDIS')
  4847. ->get([
  4848. 'INST_IDIS AS ID_INFORMACION_STOCK',
  4849. 'STAR_IDST AS ID_STOCK',
  4850. DB::raw('CONCAT(FAMI_NOFA, " (", FAMI_COFA, ")") AS FAMILIA'),
  4851. DB::raw('CONCAT(SUBF_NOSU, " (", SUBF_COSU, ")") AS SUBFAMILIA'),
  4852. 'INST_MODE AS MODELO',
  4853. 'INST_COMO AS CODIGO_MODELO',
  4854. 'STAR_NUPR AS NUMERO_PROVEEDOR',
  4855. 'INST_STMI AS STOCK_MINIMO',
  4856. 'INST_STMA AS STOCK_MAXIMO',
  4857. 'INST_IMAG AS IMAGENES',
  4858. ]);
  4859. $arrInfoStock = json_decode(json_encode($arrInfoStock), true);
  4860. } catch (\Throwable $th) {
  4861. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  4862. }
  4863. $arrDataStock = array();
  4864. foreach ($arrInfoStock as $key => $info) {
  4865. $arrImages = json_decode($info['IMAGENES']);
  4866. $arrUrlImage = array();
  4867. foreach ($arrImages as $images) {
  4868. $images = $this->encController->encrypt($images);
  4869. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  4870. if ($responseDocument['error']) {
  4871. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  4872. }
  4873. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  4874. }
  4875. $arrInfoStock[$key]['IMAGENES'] = $arrUrlImage;
  4876. try {
  4877. $countStock = DB::table('S002V01TSTAR')
  4878. ->where('STAR_NULI', '=', $line)
  4879. ->where('STAR_IDIS', '=', $info['ID_INFORMACION_STOCK'])
  4880. ->where('STAR_ESTA', '=', 'Activo')
  4881. ->count();
  4882. } catch (\Throwable $th) {
  4883. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el stock.', $th->getMessage(), 500);
  4884. }
  4885. $arrInfoStock[$key]['CANTIDAD_ACTUAL'] = $countStock;
  4886. if ($countStock < $info['STOCK_MINIMO'] && !is_null($info['STOCK_MINIMO'])) {
  4887. $arrInfoStock[$key]['ESTADO'] = 'Menor al Stock Requerido';
  4888. $arrInfoStock[$key]['CANTIDAD_SUGERIDA'] = $info['STOCK_MINIMO'] - $countStock;
  4889. $arrDataStock[] = $arrInfoStock[$key];
  4890. }
  4891. }
  4892. try {
  4893. $arrReplanishment = DB::table('S002V01TCARE')
  4894. ->where('CARE_NULI', '=', $line)
  4895. ->where('CARE_ESTA', '=', 'Activo')
  4896. ->join('S002V01TSTAR', 'STAR_IDST', '=', 'CARE_IDST')
  4897. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  4898. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  4899. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  4900. ->orderBy('CARE_CAAD', 'DESC')
  4901. ->limit(5)
  4902. ->get([
  4903. 'INST_IDIS AS ID_INFORMACION_STOCK',
  4904. 'STAR_IDST AS ID_STOCK',
  4905. DB::raw('CONCAT(FAMI_NOFA, " (", FAMI_COFA, ")") AS FAMILIA'),
  4906. DB::raw('CONCAT(SUBF_NOSU, " (", SUBF_COSU, ")") AS SUBFAMILIA'),
  4907. 'INST_MODE AS MODELO',
  4908. 'INST_COMO AS CODIGO_MODELO',
  4909. 'STAR_NUPR AS NUMERO_PROVEEDOR',
  4910. 'INST_STMI AS STOCK_MINIMO',
  4911. 'INST_STMA AS STOCK_MAXIMO',
  4912. 'INST_IMAG AS IMAGENES',
  4913. 'CARE_CAAD AS CANTIDAD_SUGERIDA',
  4914. ]);
  4915. $arrReplanishment = json_decode(json_encode($arrReplanishment), true);
  4916. } catch (\Throwable $th) {
  4917. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el reabastecimiento.', $th->getMessage(), 500);
  4918. }
  4919. foreach ($arrReplanishment as $key => $replanishment) {
  4920. $arrImages = json_decode($replanishment['IMAGENES']);
  4921. $arrUrlImage = array();
  4922. foreach ($arrImages as $images) {
  4923. $images = $this->encController->encrypt($images);
  4924. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  4925. if ($responseDocument['error']) {
  4926. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  4927. }
  4928. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  4929. }
  4930. $arrReplanishment[$key]['IMAGENES'] = $arrUrlImage;
  4931. $arrReplanishment[$key]['ESTADO'] = 'Stock habitualmente pedido';
  4932. }
  4933. // Se verifica los campos repetidos
  4934. $arrIdDataStock = array_column($arrDataStock, 'ID_INFORMACION_STOCK');
  4935. $arrIdReplanishment = array_column($arrReplanishment, 'ID_INFORMACION_STOCK');
  4936. $arrRepeated = array_merge($arrIdDataStock, $arrIdReplanishment);
  4937. $arrUnique = array_unique($arrRepeated);
  4938. $arrValues = array_values($arrUnique);
  4939. // Se unen con los estados para el resultado final
  4940. $arrFinal = array();
  4941. foreach ($arrValues as $key => $value) {
  4942. foreach ($arrDataStock as $dataStock) {
  4943. if ($dataStock['ID_INFORMACION_STOCK'] === $value) {
  4944. $arrFinal[$key] = $dataStock;
  4945. }
  4946. }
  4947. foreach ($arrReplanishment as $replanishment) {
  4948. if ($replanishment['ID_INFORMACION_STOCK'] === $value) {
  4949. if (array_key_exists($key, $arrFinal)) {
  4950. $arrFinal[$key]['ESTADO'] .= ' | '.$replanishment['ESTADO'];
  4951. } else {
  4952. $arrFinal[$key] = $replanishment;
  4953. }
  4954. }
  4955. }
  4956. }
  4957. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrFinal);
  4958. }
  4959. public function getNomenclatureManagement($user, $line) {
  4960. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  4961. if ($arrResponseCheckUser['error']) {
  4962. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  4963. }
  4964. try {
  4965. $arrStock = DB::table('S002V01TSTAR')
  4966. ->where('STAR_TIAD', '=', 'Por Pedido')
  4967. ->where('STAR_NULI', '=', $line)
  4968. ->where('STAR_ESTA', '=', 'Activo')
  4969. ->where('INST_NULI', '=', $line)
  4970. ->where('INST_ESTA', '=', 'Activo')
  4971. ->where('UBAR_NULI', '=', $line)
  4972. ->where('UBAR_ESTA', '=', 'Activo')
  4973. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  4974. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  4975. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  4976. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  4977. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'STAR_NUPR')
  4978. ->get([
  4979. 'STAR_IDST AS ID_STOCK',
  4980. 'INST_MODE AS MODELO',
  4981. 'INST_COMO AS CODIGO_MODELO',
  4982. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  4983. 'FAMI_NOFA AS FAMILIA',
  4984. 'SUBF_NOSU AS SUBFAMILIA',
  4985. 'INST_IMAG AS IMAGENES',
  4986. ]);
  4987. $arrStock = json_decode(json_encode($arrStock), true);
  4988. } catch (\Throwable $th) {
  4989. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  4990. }
  4991. foreach ($arrStock as $key => $info) {
  4992. $arrImages = json_decode($info['IMAGENES']);
  4993. $arrUrlImage = array();
  4994. foreach ($arrImages as $keyImages => $images) {
  4995. $images = $this->encController->encrypt($images);
  4996. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  4997. if ($responseDocument['error']) {
  4998. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  4999. }
  5000. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  5001. }
  5002. $arrStock[$key]['IMAGENES'] = $arrUrlImage;
  5003. }
  5004. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrStock);
  5005. }
  5006. public function getDetailsNomenclature($idStock, $user, $line) {
  5007. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  5008. if ($arrResponseCheckUser['error']) {
  5009. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  5010. }
  5011. $idStock = $this->encController->decrypt($idStock);
  5012. if (is_null($idStock)) {
  5013. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID de la información.', [], 401);
  5014. }
  5015. try {
  5016. $validateExists = DB::table('S002V01TSTAR')
  5017. ->where('STAR_IDST', '=', $idStock)
  5018. ->where('STAR_NULI', '=', $line)
  5019. ->where('STAR_ESTA', '=', 'Activo')
  5020. ->exists();
  5021. } catch (\Throwable $th) {
  5022. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar el ID del stock.', $th->getMessage(), 500);
  5023. }
  5024. if (!$validateExists) {
  5025. return $this->responseController->makeResponse(true, 'El número de stock no existe.', [], 500);
  5026. }
  5027. try {
  5028. $info = (array) DB::table('S002V01TSTAR')
  5029. ->where('STAR_IDST', '=', $idStock)
  5030. ->where('STAR_TIAD', '=', 'Por Pedido')
  5031. ->where('STAR_NULI', '=', $line)
  5032. ->where('STAR_ESTA', '=', 'Activo')
  5033. ->where('INST_NULI', '=', $line)
  5034. ->where('INST_ESTA', '=', 'Activo')
  5035. ->where('UBAR_NULI', '=', $line)
  5036. ->where('UBAR_ESTA', '=', 'Activo')
  5037. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  5038. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  5039. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  5040. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  5041. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'STAR_NUPR')
  5042. ->join('S002V01TALMA', 'ALMA_COAL', '=', 'UBAR_COAL')
  5043. ->join('S002V01TAREA', 'AREA_COAR', '=', 'UBAR_COAR')
  5044. ->join('S002V01TNIVE', 'NIVE_CONI', '=', 'UBAR_CONI')
  5045. ->join('S002V01TZONA', 'ZONA_COZO', '=', 'UBAR_COZO')
  5046. ->first([
  5047. 'INST_MODE AS MODELO',
  5048. 'INST_COMO AS CODIGO_MODELO',
  5049. 'FAMI_COFA AS CODIGO_FAMILIA',
  5050. 'FAMI_NOFA AS FAMILIA',
  5051. 'SUBF_COSU AS CODIGO_SUBFAMILIA',
  5052. 'SUBF_NOSU AS SUBFAMILIA',
  5053. 'ALMA_COAL AS CODIGO_ALMACEN',
  5054. 'ALMA_NOAL AS NOMBRE_ALMACEN',
  5055. 'AREA_COAR AS CODIGO_AREA',
  5056. 'AREA_NOAR AS NOMBRE_AREA',
  5057. 'NIVE_CONI AS CODIGO_NIVEL',
  5058. 'NIVE_NONI AS NOMBRE_NIVEL',
  5059. 'ZONA_COZO AS CODIGO_ZONA',
  5060. 'ZONA_NOZO AS NOMBRE_ZONA',
  5061. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  5062. ]);
  5063. } catch (\Throwable $th) {
  5064. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar el ID del stock.', $th->getMessage(), 500);
  5065. }
  5066. if (empty($info)) {
  5067. return $this->responseController->makeResponse(true, 'No se encontró la información del código.', [], 500);
  5068. }
  5069. $codeEquipment = $info['CODIGO_EQUIPAMIENTO'];
  5070. $arrStructureCode = explode('_', $codeEquipment);
  5071. if ( count($arrStructureCode) !== 2 ) {
  5072. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la estructura del código.' [], 500);
  5073. }
  5074. $strLBS = $arrStructureCode[0];
  5075. $arrStructureLBS = explode('.', $strLBS);
  5076. if (count($arrStructureLBS) !== 4) {
  5077. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la estructura del LBS.' [], 500);
  5078. }
  5079. $levelOccupation = $arrStructureLBS[2];
  5080. $arrStructureLevelOccupation = explode('-', $levelOccupation);
  5081. if (count($arrStructureLevelOccupation) !== 2) {
  5082. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el nivel y la ocupación.', [], 500);
  5083. }
  5084. $codeSite = $arrStructureLBS[0];
  5085. $codeLocation = $arrStructureLBS[1];
  5086. $codeElement = $arrStructureLBS[3];
  5087. $codeLevel = $arrStructureLevelOccupation[0];
  5088. $codeOccupation = $arrStructureLevelOccupation[1];
  5089. $strPBS = $arrStructureCode[1];
  5090. $arrStructurePBS = explode('.', $strPBS);
  5091. if ( count($arrStructurePBS) !== 4 ) {
  5092. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la estructura del PBS.', [], 500);
  5093. }
  5094. $typeModel = $arrStructurePBS[3];
  5095. $arrStructureTypeModel = explode('-', $typeModel);
  5096. if (count($arrStructureTypeModel) !== 3) {
  5097. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener tipo y modelo.', [], 500);
  5098. }
  5099. $codeFamily = $arrStructurePBS[0];
  5100. $codeSubfamily = $arrStructurePBS[1];
  5101. $codeState = $arrStructurePBS[2];
  5102. $codeType = $arrStructureTypeModel[0];
  5103. $codeModel = $arrStructureTypeModel[1];
  5104. $arrClaves = array(
  5105. 'CLAVE_SITIO' => $codeSite,
  5106. 'SITIO' => 'Número de Línea 01',
  5107. 'CLAVE_UBICACION' => $codeLocation,
  5108. 'UBICACION' => $info['NOMBRE_AREA'],
  5109. 'CLAVE_POSICION' => $codeElement,
  5110. 'POSICION' => $info['NOMBRE_NIVEL'],
  5111. 'CLAVE_OCUPACION' => $codeLevel,
  5112. 'OCUPACION' => $info['NOMBRE_ALMACEN'],
  5113. 'CLAVE_ELEMENTO' => $codeOccupation,
  5114. 'ELEMENTO' => $info['NOMBRE_ZONA'],
  5115. 'CLAVE_FAMILIA' => $codeFamily,
  5116. 'FAMILIA' => $info['FAMILIA'],
  5117. 'CLAVE_SUBFAMILIA' => $codeSubfamily,
  5118. 'SUBFAMILIA' => $info['SUBFAMILIA'],
  5119. 'CLAVE_ESTADO' => $codeState,
  5120. 'ESTADO' => $this->resourcesController->arrStatesEquipment[$codeState],
  5121. 'CLAVE_TIPO' => $codeType,
  5122. 'TIPO' => $info['MODELO'],
  5123. 'CLAVE_MODELO' => $codeModel,
  5124. 'MODELO' => $info['CODIGO_MODELO'],
  5125. );
  5126. return $this->responseController->makeResponse(false, 'ÉXITO: Consulta Exitosa.', $arrClaves);
  5127. }
  5128. public function getObjectProperties($user, $line) {
  5129. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  5130. if ($arrResponseCheckUser['error']) {
  5131. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  5132. }
  5133. try {
  5134. $arrStock = DB::table('S002V01TSTAR')
  5135. ->where('STAR_NULI', '=', $line)
  5136. ->where('STAR_ESTA', '=', 'Activo')
  5137. ->where('INST_NULI', '=', $line)
  5138. ->where('INST_ESTA', '=', 'Activo')
  5139. ->where('UBAR_NULI', '=', $line)
  5140. ->where('UBAR_ESTA', '=', 'Activo')
  5141. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  5142. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  5143. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  5144. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  5145. ->get([
  5146. 'STAR_IDST AS ID_STOCK',
  5147. 'INST_MODE AS MODELO',
  5148. 'INST_COMO AS CODIGO_MODELO',
  5149. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  5150. 'INST_IMAG AS IMAGENES',
  5151. 'FAMI_COFA AS CODIGO_FAMILIA',
  5152. 'FAMI_NOFA AS FAMILIA',
  5153. 'SUBF_COSU AS CODIGO_SUBFAMILIA',
  5154. 'SUBF_NOSU AS SUBFAMILIA',
  5155. ]);
  5156. $arrStock = json_decode(json_encode($arrStock), true);
  5157. } catch (\Throwable $th) {
  5158. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  5159. }
  5160. foreach ($arrStock as $key => $info) {
  5161. $arrImages = json_decode($info['IMAGENES']);
  5162. $arrUrlImage = array();
  5163. foreach ($arrImages as $keyImages => $images) {
  5164. $images = $this->encController->encrypt($images);
  5165. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  5166. if ($responseDocument['error']) {
  5167. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  5168. }
  5169. $arrUrlImage[] = $responseDocument['response']['public_uri'];
  5170. }
  5171. $arrStock[$key]['IMAGENES'] = $arrUrlImage;
  5172. }
  5173. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrStock);
  5174. }
  5175. public function getDetailsObjectProperties($idStock, $user, $line) {
  5176. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  5177. if ($arrResponseCheckUser['error']) {
  5178. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  5179. }
  5180. $idStock = $this->encController->decrypt($idStock);
  5181. if (is_null($idStock)) {
  5182. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID de la información.', [], 401);
  5183. }
  5184. try {
  5185. $validateExists = DB::table('S002V01TSTAR')
  5186. ->where('STAR_IDST', '=', $idStock)
  5187. ->where('STAR_NULI', '=', $line)
  5188. ->where('STAR_ESTA', '=', 'Activo')
  5189. ->exists();
  5190. } catch (\Throwable $th) {
  5191. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar el ID del stock.', $th->getMessage(), 500);
  5192. }
  5193. if (!$validateExists) {
  5194. return $this->responseController->makeResponse(true, 'El número de stock no existe.', [], 500);
  5195. }
  5196. try {
  5197. $arrStock = (array) DB::table('S002V01TSTAR')
  5198. ->where('STAR_IDST', '=', $idStock)
  5199. ->where('STAR_NULI', '=', $line)
  5200. ->where('STAR_ESTA', '=', 'Activo')
  5201. ->where('INST_NULI', '=', $line)
  5202. ->where('INST_ESTA', '=', 'Activo')
  5203. ->where('UBAR_NULI', '=', $line)
  5204. ->where('UBAR_ESTA', '=', 'Activo')
  5205. ->where('FAMI_NULI', '=', $line)
  5206. ->where('FAMI_ESTA', '=', 'Activo')
  5207. ->where('SUBF_NULI', '=', $line)
  5208. ->where('SUBF_ESTA', '=', 'Activo')
  5209. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  5210. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  5211. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  5212. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  5213. ->join('S002V01TALMA', 'ALMA_COAL', '=', 'UBAR_COAL')
  5214. ->join('S002V01TAREA', 'AREA_COAR', '=', 'UBAR_COAR')
  5215. ->join('S002V01TNIVE', 'NIVE_CONI', '=', 'UBAR_CONI')
  5216. ->join('S002V01TZONA', 'ZONA_COZO', '=', 'UBAR_COZO')
  5217. ->first([
  5218. 'STAR_COBA AS CODIGO_BARRA',
  5219. 'STAR_FEVE AS FECHA_VENCIMIENTO',
  5220. 'STAR_NUPR AS NUMERO_PROVEEDOR',
  5221. 'STAR_TIAD AS TIPO_ADQUISICION',
  5222. 'STAR_CONS AS CONSUMIBLE',
  5223. 'STAR_REPA AS REPARABLE',
  5224. 'STAR_ALTO AS ALTO',
  5225. 'STAR_ANCH AS ANCHO',
  5226. 'STAR_LARG AS LARGO',
  5227. 'STAR_COLO AS COLOR',
  5228. 'STAR_PRAD AS PRECIO_ADQUIRIDO',
  5229. 'STAR_COMO AS CODIGO_MONEDA',
  5230. 'INST_MODE AS MODELO',
  5231. 'INST_COMO AS CODIGO_MODELO',
  5232. 'INST_STMI AS STOCK_MINIMO',
  5233. 'INST_STMA AS STOCK_MAXIMO',
  5234. 'STAR_NIPE AS NIVEL_PELIGROSIDAD',
  5235. 'INST_IMAG AS IMAGENES',
  5236. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  5237. 'FAMI_COFA AS CODIGO_FAMILIA',
  5238. 'FAMI_NOFA AS NOMBRE_FAMILIA',
  5239. 'SUBF_COSU AS CODIGO_SUBFAMILIA',
  5240. 'SUBF_NOSU AS NOMBRE_SUBFAMILIA',
  5241. 'ALMA_COAL AS CODIGO_ALMACEN',
  5242. 'ALMA_NOAL AS NOMBRE_ALMACEN',
  5243. 'AREA_COAR AS CODIGO_AREA',
  5244. 'AREA_NOAR AS NOMBRE_AREA',
  5245. 'NIVE_CONI AS CODIGO_NIVEL',
  5246. 'NIVE_NONI AS NOMBRE_NIVEL',
  5247. 'ZONA_COZO AS CODIGO_ZONA',
  5248. 'ZONA_NOZO AS NOMBRE_ZONA',
  5249. ]);
  5250. } catch (\Throwable $th) {
  5251. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener los datos del stock.', $th->getMessage(), 500);
  5252. }
  5253. if (empty($arrStock)) {
  5254. return $this->responseController->makeResponse(true, 'No existe información de stock.', [], 500);
  5255. }
  5256. $arrImages = array();
  5257. $arrImagesTemp = json_decode($arrStock['IMAGENES']);
  5258. foreach ($arrImagesTemp as $key => $value) {
  5259. $images = $this->encController->encrypt($value);
  5260. $responseDocument = $this->documentManagementController->privateGetPublicDocumentURL($images, $user, $line);
  5261. if ($responseDocument['error']) {
  5262. return $this->responseController->makeResponse(true, $responseDocument['msg'], [], 500);
  5263. }
  5264. $arrImages[] = $responseDocument['response']['public_uri'];
  5265. }
  5266. $arrStock['IMAGENES'] = $arrImages;
  5267. try {
  5268. $infoAdquisition = (array) DB::table('S002V01TINAR')
  5269. ->where('INAR_NULI', '=', $line)
  5270. ->where('INAR_CODI', '=', $arrStock['CODIGO_MODELO'])
  5271. ->where('INAR_MODE', '=', $arrStock['MODELO'])
  5272. ->where('INAR_ESTA', '=', 'Activo')
  5273. ->where('DEAR_NULI', '=', $line)
  5274. ->where('DEAR_NUPR', '=', $arrStock['NUMERO_PROVEEDOR'])
  5275. ->where('DEAR_ESTA', '=', 'Activo')
  5276. ->where('ARTI_NULI', '=', $line)
  5277. ->where('ARTI_COFA', '=', $arrStock['CODIGO_FAMILIA'])
  5278. ->where('ARTI_COSU', '=', $arrStock['CODIGO_SUBFAMILIA'])
  5279. ->where('ARTI_ESTA', '=', 'Activo')
  5280. ->join('S002V01TDEAR', 'DEAR_IDDE', '=', 'INAR_IDDE')
  5281. ->join('S002V01TARTI', 'ARTI_IDAR', '=', 'DEAR_IDAR')
  5282. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'DEAR_NUPR')
  5283. ->first([
  5284. 'DEAR_DESC AS DESCRIPCION_PROVEEDOR',
  5285. 'DEAR_CARA AS CARACTERISTICA_PROVEEDOR',
  5286. 'PROV_NOCO AS NOMBRE_PROVEEDOR',
  5287. ]);
  5288. } catch (\Throwable $th) {
  5289. DB::rollBack();
  5290. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información de la adquisición.', $th->getMessage(), 500);
  5291. }
  5292. if (empty($infoAdquisition)) {
  5293. DB::rollBack();
  5294. return $this->responseController->makeResponse(true, 'No se encontró la información de la adquisición.', [], 500);
  5295. }
  5296. $arrGeneralInfo = array_merge($arrStock, $infoAdquisition);
  5297. return $this->responseController->makeResponse(false, 'ÉXITO: Consulta Exitosa', $arrGeneralInfo);
  5298. }
  5299. public function saveInfoStock(Request $request, $idStock) {
  5300. $valitador = Validator::make($request->all(), [
  5301. 'ALTO' => 'string|nullable',
  5302. 'ANCHO' => 'string|nullable',
  5303. 'LARGO' => 'string|nullable',
  5304. 'COLOR' => 'string|nullable',
  5305. 'NIVEL_PELIGROSIDAD' => 'string|nullable',
  5306. 'TASA_ROTACION' => 'string|nullable',
  5307. 'USUARIO' => 'required|string',
  5308. 'NUMERO_LINEA' => 'required|integer',
  5309. ]);
  5310. if ($valitador->fails()) {
  5311. return $this->responseController->makeResponse(
  5312. true,
  5313. "ERR_DOCUMENT_ORDER_REG000: Se encontraron uno o más errores.",
  5314. $this->responseController->makeErrors($valitador->errors()->messages()),
  5315. 401
  5316. );
  5317. }
  5318. DB::beginTransaction();
  5319. $requestData = $request->all();
  5320. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  5321. if ($arrResponseCheckUser['error']) {
  5322. DB::rollBack();
  5323. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  5324. }
  5325. $user = $arrResponseCheckUser['response'];
  5326. $idStock = $this->encController->decrypt($idStock);
  5327. if (is_null($idStock)) {
  5328. DB::rollBack();
  5329. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el ID de la información.', [], 401);
  5330. }
  5331. try {
  5332. $validateExists = DB::table('S002V01TSTAR')
  5333. ->where('STAR_IDST', '=', $idStock)
  5334. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  5335. ->where('STAR_ESTA', '=', 'Activo')
  5336. ->exists();
  5337. } catch (\Throwable $th) {
  5338. DB::rollBack();
  5339. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar el ID del stock.', $th->getMessage(), 500);
  5340. }
  5341. if (!$validateExists) {
  5342. DB::rollBack();
  5343. return $this->responseController->makeResponse(true, 'El número de stock no existe.', [], 500);
  5344. }
  5345. $now = $this->functionsController->now();
  5346. $currentDate = $now->toDateTimeString();
  5347. try {
  5348. $validateInsert = DB::table('S002V01TSTAR')
  5349. ->where('STAR_IDST', '=', $idStock)
  5350. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  5351. ->where('STAR_ESTA', '=', 'Activo')
  5352. ->update([
  5353. 'STAR_ALTO' => $requestData['ALTO'],
  5354. 'STAR_ANCH' => $requestData['ANCHO'],
  5355. 'STAR_LARG' => $requestData['LARGO'],
  5356. 'STAR_COLO' => $requestData['COLOR'],
  5357. 'STAR_NIPE' => $requestData['NIVEL_PELIGROSIDAD'],
  5358. 'STAR_USMO' => $user,
  5359. 'STAR_FEMO' => $currentDate,
  5360. 'STAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  5361. ]);
  5362. } catch (\Throwable $th) {
  5363. DB::rollBack();
  5364. return $this->responseController->makeResponse(true, 'Ocurrió un error al insertar los datos del stock.', $th->getMessage(), 500);
  5365. }
  5366. if (!$validateInsert) {
  5367. DB::rollBack();
  5368. return $this->responseController->makeResponse(true, 'No se pudo insertar los datos del stock.', [], 500);
  5369. }
  5370. DB::commit();
  5371. return $this->responseController->makeResponse(false, 'ÉXITO: Registro Exitoso');
  5372. }
  5373. public function getToolsAndSpare ($user, $line) {
  5374. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  5375. if ($arrResponseCheckUser['error']) {
  5376. DB::rollBack();
  5377. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  5378. }
  5379. try {
  5380. $arrStock = DB::table('S002V01TINST')
  5381. ->where([
  5382. ['INST_NULI', '=', $line],
  5383. ])->get([
  5384. 'INST_IDIS AS ID',
  5385. 'INST_MODE AS NOMB',
  5386. 'INST_COMO AS MODE',
  5387. ]);
  5388. $arrStock = json_decode(json_encode($arrStock), true);
  5389. } catch (\Throwable $th) {
  5390. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener los artículos en stock.', $th->getMessage(), 500);
  5391. }
  5392. $arrTools = [];
  5393. $arrSpares = [];
  5394. foreach ($arrStock as $stock) {
  5395. try {
  5396. $items = DB::table('S002V01TSTAR')
  5397. ->where([
  5398. ['STAR_NULI', '=', $line],
  5399. ['STAR_IDIS', '=', $stock['ID']],
  5400. ])
  5401. ->join('S002V01TUNID', 'UNID_IDUN', '=', 'STAR_IDUN')
  5402. ->get([
  5403. 'UNID_NOMB AS UNIT',
  5404. 'STAR_CONS AS TYPE',
  5405. ]);
  5406. $items = json_decode(json_encode($items), true);
  5407. } catch (\Throwable $th) {
  5408. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener los datos de las herramientas y refacciones.', $th->getMessage(), 500);
  5409. }
  5410. $stock['CANTDISP'] = count($items);
  5411. foreach ($items as $item) {
  5412. if ($item['TYPE'] === 'Si') {
  5413. $stock['TYPE'] = 'Refaccción';
  5414. } else {
  5415. $stock['TYPE'] = 'Herramienta';
  5416. }
  5417. $stock['UNIT'] = $item['UNIT'];
  5418. }
  5419. $stock['ID'] = $this->encController->encrypt("{$stock['ID']}");
  5420. if ($stock['TYPE'] === 'Refaccción') {
  5421. $arrSpares[] = $stock;
  5422. } else {
  5423. $arrTools[] = $stock;
  5424. }
  5425. }
  5426. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", [ 'HERRAMIENTAS' => $arrTools, 'REFACCIONES' => $arrSpares ]);
  5427. }
  5428. }