StockController.php 253 KB

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