StockController.php 130 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862
  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. class StockController extends Controller
  19. {
  20. private $responseController;
  21. private $encController;
  22. private $resourcesController;
  23. private $documentManagementController;
  24. private $functionsController;
  25. public function __construct(){
  26. $this->responseController = new ResponseController();
  27. $this->encController = new EncryptionController();
  28. $this->resourcesController = new ResourcesController();
  29. $this->documentManagementController = new DocumentManagementController();
  30. $this->functionsController = new FunctionsController();
  31. }
  32. // Crear Artículo en Stock
  33. public function createArtitleWithoutOrder(Request $request) {
  34. $validator = Validator::make($request->all(), [
  35. 'ARTICULO' => 'required',
  36. 'MODELO' => 'required',
  37. 'CODIGO_MODELO' => 'required',
  38. 'FAMILIA' => 'required',
  39. 'SUBFAMILIA' => 'required',
  40. 'UNIDAD' => 'required',
  41. 'CODIGO_BARRAS' => 'required',
  42. 'CANTIDAD' => 'required',
  43. 'STOCK_MINIMO' => 'required',
  44. 'STOCK_MAXIMO' => 'required',
  45. 'REPARACION' => 'required|boolean',
  46. 'CONSUMIBLE' => 'required|boolean',
  47. 'PELIGROSO' => 'required|boolean',
  48. // 'FECHA_VENCIMIENTO' => '',
  49. // 'PROVEEDOR' => 'required',
  50. 'IMAGEN' => 'required',
  51. 'NUMERO_LINEA' => 'required',
  52. 'USUARIO' => 'required',
  53. ]);
  54. if ($validator->fails()) {
  55. return $this->responseController->makeResponse(
  56. true,
  57. "ERR_STOCK_REG000: Se encontraron uno o más errores.",
  58. $this->responseController->makeErrors($validator->errors()->messages()),
  59. 401
  60. );
  61. }
  62. DB::beginTransaction();
  63. $requestData = $request->all();
  64. try {
  65. $user = $this->encController->decrypt($requestData['USUARIO']);
  66. } catch (\Throwable $th) {
  67. DB::rollBack();
  68. return $this->responseController->makeResponse(true, "ERR_STOCK_REG001: Ocurrió un error al obtener el usuario.", $th->getMessage(), 500);
  69. }
  70. try {
  71. $validateFamily = DB::table('S002V01TFAMI')
  72. ->where('FAMI_COFA','=', $requestData['FAMILIA'])
  73. ->where('FAMI_NULI','=', $requestData['NUMERO_LINEA'])
  74. ->where('FAMI_ESTA','=','Activo')
  75. ->exists();
  76. } catch (\Throwable $th) {
  77. DB::rollBack();
  78. return $this->responseController->makeResponse(true, "ERR_STOCK_REG002: Ocurrió un error al validar la familia.", $th->getMessage(), 500);
  79. }
  80. if (!$validateFamily) {
  81. DB::rollBack();
  82. return $this->responseController->makeResponse(true, "ERR_STOCK_REG003: La familia no existe.", [], 500);
  83. }
  84. try {
  85. $validateSubfamily = DB::table('S002V01TSUBF')
  86. ->where('SUBF_COSU','=', $requestData['SUBFAMILIA'])
  87. ->where('SUBF_NULI','=', $requestData['NUMERO_LINEA'])
  88. ->where('SUBF_ESTA','=','Activo')
  89. ->exists();
  90. } catch (\Throwable $th) {
  91. DB::rollBack();
  92. return $this->responseController->makeResponse(true, "ERR_STOCK_REG004: Ocurrió un error al validar la subfamilia.", $th->getMessage(), 500);
  93. }
  94. if (!$validateSubfamily) {
  95. DB::rollBack();
  96. return $this->responseController->makeResponse(true, "ERR_STOCK_REG005: La subfamilia no existe.", [], 500);
  97. }
  98. try {
  99. $validateUnit = DB::table('S002V01TUNID')
  100. ->where('UNID_IDUN','=', $requestData['UNIDAD'])
  101. ->where('UNID_NULI','=', $requestData['NUMERO_LINEA'])
  102. ->where('UNID_ESTA','=','Activo')
  103. ->exists();
  104. } catch (\Throwable $th) {
  105. DB::rollBack();
  106. return $this->responseController->makeResponse(true, "ERR_STOCK_REG006: Ocurrió un error al validar la unidad.", $th->getMessage(), 500);
  107. }
  108. if (!$validateUnit) {
  109. DB::rollBack();
  110. return $this->responseController->makeResponse(true, "ERR_STOCK_REG007: La unidad no existe.", [], 500);
  111. }
  112. $requestData['PROVEEDOR'] = $requestData['PROVEEDOR'] === '' ? null : $requestData['PROVEEDOR'];
  113. if (!is_null($requestData['PROVEEDOR'])) {
  114. try {
  115. $validateUnit = DB::table('S002V01TPROV')
  116. ->where('PROV_NUPR','=', $requestData['PROVEEDOR'])
  117. ->where('PROV_NULI','=', $requestData['NUMERO_LINEA'])
  118. ->where('PROV_ESTA','=','Activo')
  119. ->exists();
  120. } catch (\Throwable $th) {
  121. DB::rollBack();
  122. return $this->responseController->makeResponse(true, "ERR_STOCK_REG008: Ocurrió un error al validar el proveedor.", $th->getMessage(), 500);
  123. }
  124. if (!$validateUnit) {
  125. DB::rollBack();
  126. return $this->responseController->makeResponse(true, "ERR_STOCK_REG009: El proveedor no existe.", [], 500);
  127. }
  128. }
  129. $arrCodeImages = array();
  130. foreach ($requestData['IMAGEN'] as $key => $encIdFile) {
  131. $idFile = $this->encController->decrypt($encIdFile);
  132. $tempFile = DB::table('S002V01TARTE')->where([
  133. ['ARTE_NULI', '=', $requestData['NUMERO_LINEA']],
  134. ['ARTE_IDAR', '=', $idFile],
  135. ])->first();
  136. if(is_null($tempFile)){
  137. return $this->responseController->makeResponse(true, 'ERR_ARTITLE_REG006: El archivo consultado no está registrado', [], 404);
  138. }else if($tempFile->ARTE_ESTA == 'Eliminado'){
  139. return $this->responseController->makeResponse(true, 'ERR_ARTITLE_REG007: El archivo consultado está eliminado', [], 404);
  140. }
  141. $fileResponse = $this->documentManagementController->moveFinalFile(
  142. intval($requestData['NUMERO_LINEA']),
  143. 'GIST',
  144. 'FO',
  145. $tempFile,
  146. $user,
  147. );
  148. if(!$fileResponse[0]){
  149. return $this->responseController->makeResponse(true, 'ERR_ARTITLE_REG008: '.$fileResponse[1], [], 400);
  150. }
  151. $arrCodeImages[] = $this->encController->encrypt($fileResponse[1]);
  152. }
  153. $jsonImages = json_encode($arrCodeImages);
  154. $now = $this->functionsController->now();
  155. $currentDate = $now->toDateTimeString();
  156. try {
  157. $validateRegister = DB::table('S002V01TSTAR')->insert([
  158. // 'STAR_CODI' => $requestData['CODIGO_STOCK'],
  159. 'STAR_ARTI' => $requestData['ARTICULO'],
  160. 'STAR_MODE' => $requestData['MODELO'],
  161. 'STAR_COMO' => $requestData['CODIGO_MODELO'],
  162. 'STAR_IDFA' => $requestData['FAMILIA'],
  163. 'STAR_IDSU' => $requestData['SUBFAMILIA'],
  164. 'STAR_NUPR' => $requestData['PROVEEDOR'],
  165. 'STAR_IDUN' => $requestData['UNIDAD'],
  166. 'STAR_COBA' => $requestData['CODIGO_BARRAS'],
  167. 'STAR_CANT' => $requestData['CANTIDAD'],
  168. 'STAR_STMI' => $requestData['STOCK_MINIMO'],
  169. 'STAR_STMA' => $requestData['STOCK_MAXIMO'],
  170. 'STAR_REPA' => $requestData['REPARACION'],
  171. 'STAR_CONS' => $requestData['CONSUMIBLE'],
  172. 'STAR_PELI' => $requestData['PELIGROSO'],
  173. 'STAR_FEVE' => $requestData['FECHA_VENCIMIENTO'],
  174. 'STAR_IMAG' => $jsonImages,
  175. 'STAR_TIAD' => 'Sin Pedido',
  176. 'STAR_NULI' => $requestData['NUMERO_LINEA'],
  177. 'STAR_USRE' => $user,
  178. 'STAR_FERE' => $currentDate,
  179. 'STAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  180. ]);
  181. } catch (\Throwable $th) {
  182. DB::rollBack();
  183. return $this->responseController->makeResponse(true, "ERR_STOCK_REG011: Ocurrió un error al registrar el artículo.", $th->getMessage(), 500);
  184. }
  185. if (!$validateRegister) {
  186. DB::rollBack();
  187. return $this->responseController->makeResponse(true, "ERR_STOCK_REG012: No se pudo registrar el artículo.", [], 500);
  188. }
  189. DB::commit();
  190. return $this->responseController->makeResponse(false, "EXITO: Registro Exitoso");
  191. }
  192. // FUNCIÓN GESTIÓN DE ALMACENES
  193. public function getWarehouse( $user, $line ) {
  194. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  195. if ($arrResponseCheckUser['error']) {
  196. DB::rollBack();
  197. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_GETBY000:'.$arrResponseCheckUser['msg'], [], 401);
  198. }
  199. try {
  200. $arrWarehouse = DB::table('S002V01TALMA')
  201. ->where('ALMA_NULI', '=', $line)
  202. ->get([
  203. 'ALMA_COAL AS CODIGO_ALMACEN',
  204. 'ALMA_NOAL AS NOMBRE_ALMACEN',
  205. 'ALMA_NORE AS NOMBRE_RESPONSABLE',
  206. 'ALMA_APRE AS APELLIDO_PATERNO_RESPONSABLE',
  207. 'ALMA_AMRE AS APELLIDO_MATERNO_RESPONSABLE',
  208. 'ALMA_COR1 AS CORREO1',
  209. 'ALMA_COR2 AS CORREO2',
  210. 'ALMA_LAD1 AS LADA1',
  211. 'ALMA_TEL1 AS TELEFONO1',
  212. 'ALMA_LAD2 AS LADA2',
  213. 'ALMA_TEL2 AS TELEFONO2',
  214. 'ALMA_CALL AS CALLE',
  215. 'ALMA_NUEX AS NUMERO_EXTERIOR',
  216. 'ALMA_NUIN AS NUMERO_INTERIOR',
  217. 'ALMA_COPO AS CODIGO_POSTAL',
  218. 'ALMA_COLO AS COLONIA',
  219. 'ALMA_LOCA AS LOCALIDAD',
  220. 'ALMA_MUNI AS MUNICIPIO',
  221. 'ALMA_ENTI AS ENTIDAD_FEDERATIVA',
  222. 'ALMA_PAIS AS PAIS',
  223. 'ALMA_ESTA AS ESTADO',
  224. 'ALMA_USRE AS USUARIO_REGISTRA',
  225. 'ALMA_FERE AS FECHA_REGISTRA',
  226. 'ALMA_USMO AS USUARIO_MODIFICA',
  227. 'ALMA_FEMO AS FECHA_MODIFICA',
  228. ]);
  229. $arrWarehouse = json_decode( json_encode($arrWarehouse), true );
  230. } catch (\Throwable $th) {
  231. DB::rollBack();
  232. return $this->responseController->makeResponse(
  233. true,
  234. "ERR_WAREHOUSE_GET001: Ocurrió un error al obtener los almacenes.",
  235. $th->getMessage(),
  236. 500
  237. );
  238. }
  239. foreach ($arrWarehouse as $keyWarehouse => $warehouse) {
  240. $arrResponseGetAddress = $this->resourcesController->getAddress(
  241. $warehouse['CODIGO_POSTAL'],
  242. $warehouse['COLONIA'],
  243. $warehouse['MUNICIPIO'],
  244. $warehouse['LOCALIDAD'],
  245. $warehouse['ENTIDAD_FEDERATIVA'],
  246. $warehouse['PAIS'],
  247. $line
  248. );
  249. if ($arrResponseGetAddress['error']) {
  250. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_GET002: '.$arrResponseGetAddress['msg'], $arrResponseGetAddress['response'], 401);
  251. }
  252. $warehouse['CODIGO_POSTAL'] = $arrResponseGetAddress['response']['CODIGO_POSTAL'];
  253. $warehouse['COLONIA'] = $arrResponseGetAddress['response']['COLONIA'];
  254. $warehouse['MUNICIPIO'] = $arrResponseGetAddress['response']['MUNICIPIO'];
  255. $warehouse['LOCALIDAD'] = $arrResponseGetAddress['response']['LOCALIDAD'];
  256. $warehouse['ENTIDAD_FEDERATIVA'] = $arrResponseGetAddress['response']['ENTIDAD_FEDERATIVA'];
  257. $warehouse['PAIS'] = $arrResponseGetAddress['response']['PAIS'];
  258. $arrWarehouse[$keyWarehouse] = $warehouse;
  259. }
  260. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrWarehouse);
  261. }
  262. public function getWarehouseById( $idWarehouse, $user, $line ) {
  263. try {
  264. $idWarehouse = $this->encController->decrypt($idWarehouse);
  265. } catch (\Throwable $th) {
  266. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_GETBY000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  267. }
  268. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  269. if ($arrResponseCheckUser['error']) {
  270. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_GETBY000:'.$arrResponseCheckUser['msg'], [], 401);
  271. }
  272. try {
  273. $arrWarehouse = (array) DB::table('S002V01TALMA')
  274. ->where('ALMA_COAL', '=', $idWarehouse)
  275. ->where('ALMA_NULI', '=', $line)
  276. ->first([
  277. 'ALMA_COAL AS CODIGO_ALMACEN',
  278. 'ALMA_NOAL AS NOMBRE_ALMACEN',
  279. 'ALMA_NORE AS NOMBRE_RESPONSABLE',
  280. 'ALMA_APRE AS APELLIDO_PATERNO_RESPONSABLE',
  281. 'ALMA_AMRE AS APELLIDO_MATERNO_RESPONSABLE',
  282. 'ALMA_COR1 AS CORREO1',
  283. 'ALMA_COR2 AS CORREO2',
  284. 'ALMA_LAD1 AS LADA1',
  285. 'ALMA_TEL1 AS TELEFONO1',
  286. 'ALMA_LAD2 AS LADA2',
  287. 'ALMA_TEL2 AS TELEFONO2',
  288. 'ALMA_CALL AS CALLE',
  289. 'ALMA_NUEX AS NUMERO_EXTERIOR',
  290. 'ALMA_NUIN AS NUMERO_INTERIOR',
  291. 'ALMA_COPO AS CODIGO_POSTAL',
  292. 'ALMA_COLO AS COLONIA',
  293. 'ALMA_LOCA AS LOCALIDAD',
  294. 'ALMA_MUNI AS MUNICIPIO',
  295. 'ALMA_ENTI AS ENTIDAD_FEDERATIVA',
  296. 'ALMA_PAIS AS PAIS',
  297. 'ALMA_ESTA AS ESTADO',
  298. 'ALMA_USRE AS USUARIO_REGISTRA',
  299. 'ALMA_FERE AS FECHA_REGISTRA',
  300. 'ALMA_USMO AS USUARIO_MODIFICA',
  301. 'ALMA_FEMO AS FECHA_MODIFICA',
  302. ]);
  303. $arrWarehouse = json_decode( json_encode($arrWarehouse), true );
  304. } catch (\Throwable $th) {
  305. DB::rollBack();
  306. return $this->responseController->makeResponse(
  307. true,
  308. "ERR_WAREHOUSE_GETBY001: Ocurrió un error al obtener los almacenes.",
  309. $th->getMessage(),
  310. 500
  311. );
  312. }
  313. if (!empty($arrWarehouse)) {
  314. $arrResponseGetAddress = $this->resourcesController->getAddress(
  315. $arrWarehouse['CODIGO_POSTAL'],
  316. $arrWarehouse['COLONIA'],
  317. $arrWarehouse['MUNICIPIO'],
  318. $arrWarehouse['LOCALIDAD'],
  319. $arrWarehouse['ENTIDAD_FEDERATIVA'],
  320. $arrWarehouse['PAIS'],
  321. $line
  322. );
  323. if ($arrResponseGetAddress['error']) {
  324. return $this->responseController->makeResponse(true, 'ERR_arrWarehouse_GETBY002: '.$arrResponseGetAddress['msg'], $arrResponseGetAddress['response'], 406);
  325. }
  326. $arrWarehouse['CODIGO_POSTAL'] = $arrResponseGetAddress['response']['CODIGO_POSTAL'];
  327. $arrWarehouse['COLONIA'] = $arrResponseGetAddress['response']['COLONIA'];
  328. $arrWarehouse['MUNICIPIO'] = $arrResponseGetAddress['response']['MUNICIPIO'];
  329. $arrWarehouse['LOCALIDAD'] = $arrResponseGetAddress['response']['LOCALIDAD'];
  330. $arrWarehouse['ENTIDAD_FEDERATIVA'] = $arrResponseGetAddress['response']['ENTIDAD_FEDERATIVA'];
  331. $arrWarehouse['PAIS'] = $arrResponseGetAddress['response']['PAIS'];
  332. }
  333. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrWarehouse);
  334. }
  335. public function getWarehouseActives($user, $line) {
  336. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  337. if ($arrResponseCheckUser['error']) {
  338. DB::rollBack();
  339. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_GETBY000:'.$arrResponseCheckUser['msg'], [], 401);
  340. }
  341. try {
  342. $arrWarehouse = DB::table('S002V01TALMA')
  343. ->where('ALMA_NULI', '=', $line)
  344. ->where('ALMA_ESTA', '=', 'Activo')
  345. ->get([
  346. 'ALMA_COAL AS CODIGO_ALMACEN',
  347. 'ALMA_NOAL AS NOMBRE_ALMACEN',
  348. 'ALMA_NORE AS NOMBRE_RESPONSABLE',
  349. 'ALMA_APRE AS APELLIDO_PATERNO_RESPONSABLE',
  350. 'ALMA_AMRE AS APELLIDO_MATERNO_RESPONSABLE',
  351. 'ALMA_COR1 AS CORREO1',
  352. 'ALMA_LAD1 AS LADA1',
  353. 'ALMA_TEL1 AS TELEFONO1',
  354. ]);
  355. $arrWarehouse = json_decode( json_encode($arrWarehouse), true );
  356. } catch (\Throwable $th) {
  357. DB::rollBack();
  358. return $this->responseController->makeResponse(
  359. true,
  360. "ERR_WAREHOUSE_GET001: Ocurrió un error al obtener los almacenes.",
  361. $th->getMessage(),
  362. 500
  363. );
  364. }
  365. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrWarehouse);
  366. }
  367. public function createWarehouse( Request $request ) {
  368. $validator = Validator::make($request->all(), [
  369. 'CODIGO_ALMACEN' => 'required|string',
  370. 'NOMBRE_ALMACEN' => 'required|string',
  371. 'NOMBRE_RESPONSABLE' => 'required|string',
  372. 'APELLIDO_PATERNO_RESPONSABLE' => 'required|string',
  373. 'APELLIDO_MATERNO_RESPONSABLE' => 'nullable|string',
  374. 'CORREO1' => 'required|string',
  375. 'CORREO2' => 'nullable|string',
  376. 'LADA1' => 'required|string',
  377. 'TELEFONO1' => 'required|string',
  378. 'LADA2' => 'nullable|string',
  379. 'TELEFONO2' => 'nullable|string',
  380. 'CALLE' => 'required|string',
  381. 'NUMERO_EXTERIOR' => 'required|string',
  382. 'NUMERO_INTERIOR' => 'nullable|string',
  383. 'CODIGO_POSTAL' => 'required|string',
  384. 'COLONIA' => 'required|string',
  385. 'LOCALIDAD' => 'nullable|string',
  386. 'MUNICIPIO' => 'required|string',
  387. 'ENTIDAD_FEDERATIVA' => 'required|string',
  388. 'PAIS' => 'required|string',
  389. 'USUARIO' => 'required|string',
  390. 'NUMERO_LINEA' => 'required|string',
  391. ]);
  392. if ($validator->fails()) {
  393. return $this->responseController->makeResponse(
  394. true,
  395. "ERR_WAREHOUSE_REG000: Se encontraron uno o más errores.",
  396. $this->responseController->makeErrors($validator->errors()->messages()),
  397. 401
  398. );
  399. }
  400. DB::beginTransaction();
  401. $requestData = $request->all();
  402. // Se valida y se obtiene el usuario
  403. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  404. if ($arrResponseCheckUser['error']) {
  405. DB::rollBack();
  406. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_REG001:'.$arrResponseCheckUser['msg'], [], 401);
  407. }
  408. $user = $arrResponseCheckUser['response'];
  409. // Se vallidan los campos de dirección
  410. $arrResponseCheckAddress = $this->resourcesController->validateAddress(
  411. $requestData['CODIGO_POSTAL'],
  412. $requestData['COLONIA'],
  413. $requestData['MUNICIPIO'],
  414. $requestData['LOCALIDAD'],
  415. $requestData['ENTIDAD_FEDERATIVA'],
  416. $requestData['PAIS'],
  417. $requestData['NUMERO_LINEA'],
  418. );
  419. if ($arrResponseCheckAddress['error']) {
  420. DB::rollBack();
  421. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_REG002: '.$arrResponseCheckAddress['msg'], $arrResponseCheckAddress['response'], 401);
  422. }
  423. $now = $this->functionsController->now();
  424. $currentDate = $now->toDateTimeString();
  425. try {
  426. $validateInsert = DB::table('S002V01TALMA')->insert([
  427. 'ALMA_NULI' => $requestData['NUMERO_LINEA'],
  428. 'ALMA_COAL' => $requestData['CODIGO_ALMACEN'],
  429. 'ALMA_NOAL' => $requestData['NOMBRE_ALMACEN'],
  430. 'ALMA_NORE' => $requestData['NOMBRE_RESPONSABLE'],
  431. 'ALMA_APRE' => $requestData['APELLIDO_PATERNO_RESPONSABLE'],
  432. 'ALMA_AMRE' => $requestData['APELLIDO_MATERNO_RESPONSABLE'],
  433. 'ALMA_COR1' => $requestData['CORREO1'],
  434. 'ALMA_COR2' => $requestData['CORREO2'],
  435. 'ALMA_LAD1' => $requestData['LADA1'],
  436. 'ALMA_TEL1' => $requestData['TELEFONO1'],
  437. 'ALMA_LAD2' => $requestData['LADA2'],
  438. 'ALMA_TEL2' => $requestData['TELEFONO2'],
  439. 'ALMA_CALL' => $requestData['CALLE'],
  440. 'ALMA_NUEX' => $requestData['NUMERO_EXTERIOR'],
  441. 'ALMA_NUIN' => $requestData['NUMERO_INTERIOR'],
  442. 'ALMA_COPO' => $requestData['CODIGO_POSTAL'],
  443. 'ALMA_COLO' => $requestData['COLONIA'],
  444. 'ALMA_LOCA' => $requestData['LOCALIDAD'],
  445. 'ALMA_MUNI' => $requestData['MUNICIPIO'],
  446. 'ALMA_ENTI' => $requestData['ENTIDAD_FEDERATIVA'],
  447. 'ALMA_PAIS' => $requestData['PAIS'],
  448. 'ALMA_USRE' => $user,
  449. 'ALMA_FERE' => $currentDate,
  450. 'ALMA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  451. ]);
  452. } catch (\Throwable $th) {
  453. DB::rollBack();
  454. return $this->responseController->makeResponse(
  455. true,
  456. "ERR_WAREHOUSE_REG003: Ocurrió un error al insertar el registro del almacen.",
  457. $th->getMessage(),
  458. 500
  459. );
  460. }
  461. if (!$validateInsert) {
  462. DB::rollBack();
  463. return $this->responseController->makeResponse(
  464. true,
  465. "ERR_WAREHOUSE_REG004: No se pudo insertar el registro del almacen.",
  466. [],
  467. 500
  468. );
  469. }
  470. DB::commit();
  471. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  472. }
  473. public function updateWarehouse( Request $request, $idWarehouse ) {
  474. $validator = Validator::make($request->all(), [
  475. 'CODIGO_ALMACEN' => 'required|string',
  476. 'NOMBRE_ALMACEN' => 'required|string',
  477. 'NOMBRE_RESPONSABLE' => 'required|string',
  478. 'APELLIDO_PATERNO_RESPONSABLE' => 'required|string',
  479. 'APELLIDO_MATERNO_RESPONSABLE' => 'nullable|string',
  480. 'CORREO1' => 'required|string',
  481. 'CORREO2' => 'nullable|string',
  482. 'LADA1' => 'required|string',
  483. 'TELEFONO1' => 'required|string',
  484. 'LADA2' => 'nullable|string',
  485. 'TELEFONO2' => 'nullable|string',
  486. 'CALLE' => 'required|string',
  487. 'NUMERO_EXTERIOR' => 'required|string',
  488. 'NUMERO_INTERIOR' => 'nullable|string',
  489. 'CODIGO_POSTAL' => 'required|string',
  490. 'COLONIA' => 'required|string',
  491. 'LOCALIDAD' => 'nullable|string',
  492. 'MUNICIPIO' => 'required|string',
  493. 'ENTIDAD_FEDERATIVA' => 'required|string',
  494. 'PAIS' => 'required|string',
  495. 'USUARIO' => 'required|string',
  496. 'NUMERO_LINEA' => 'required|string',
  497. ]);
  498. if ($validator->fails()) {
  499. return $this->responseController->makeResponse(
  500. true,
  501. "ERR_WAREHOUSE_UPD000: Se encontraron uno o más errores.",
  502. $this->responseController->makeErrors($validator->errors()->messages()),
  503. 401
  504. );
  505. }
  506. DB::beginTransaction();
  507. $requestData = $request->all();
  508. try {
  509. $idWarehouse = $this->encController->decrypt($idWarehouse);
  510. } catch (\Throwable $th) {
  511. return $this->responseController->makeResponse(true, "ERR_WAREHOUSE_UPD001: Ocurrió un error al desencriptar el ID del almacen.". $th->getMessage(), 406);
  512. }
  513. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  514. if ($arrResponseCheckUser['error']) {
  515. DB::rollBack();
  516. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_UPD002:'.$arrResponseCheckUser['msg'], [], 401);
  517. }
  518. $user = $arrResponseCheckUser['response'];
  519. $arrResponseCheckAddress = $this->resourcesController->validateAddress(
  520. $requestData['CODIGO_POSTAL'],
  521. $requestData['COLONIA'],
  522. $requestData['MUNICIPIO'],
  523. $requestData['LOCALIDAD'],
  524. $requestData['ENTIDAD_FEDERATIVA'],
  525. $requestData['PAIS'],
  526. $requestData['NUMERO_LINEA'],
  527. );
  528. if ($arrResponseCheckAddress['error']) {
  529. DB::rollBack();
  530. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_UPD003: '.$arrResponseCheckAddress['msg'], $arrResponseCheckAddress['response'], 401);
  531. }
  532. $now = $this->functionsController->now();
  533. $currentDate = $now->toDateTimeString();
  534. try {
  535. $validateUpdate = DB::table('S002V01TALMA')
  536. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  537. ->where('ALMA_COAL', '=', $idWarehouse)
  538. ->update([
  539. 'ALMA_COAL' => $requestData['CODIGO_ALMACEN'],
  540. 'ALMA_NOAL' => $requestData['NOMBRE_ALMACEN'],
  541. 'ALMA_NORE' => $requestData['NOMBRE_RESPONSABLE'],
  542. 'ALMA_APRE' => $requestData['APELLIDO_PATERNO_RESPONSABLE'],
  543. 'ALMA_AMRE' => $requestData['APELLIDO_MATERNO_RESPONSABLE'],
  544. 'ALMA_COR1' => $requestData['CORREO1'],
  545. 'ALMA_COR2' => $requestData['CORREO2'],
  546. 'ALMA_LAD1' => $requestData['LADA1'],
  547. 'ALMA_TEL1' => $requestData['TELEFONO1'],
  548. 'ALMA_LAD2' => $requestData['LADA2'],
  549. 'ALMA_TEL2' => $requestData['TELEFONO2'],
  550. 'ALMA_CALL' => $requestData['CALLE'],
  551. 'ALMA_NUEX' => $requestData['NUMERO_EXTERIOR'],
  552. 'ALMA_NUIN' => $requestData['NUMERO_INTERIOR'],
  553. 'ALMA_COPO' => $requestData['CODIGO_POSTAL'],
  554. 'ALMA_COLO' => $requestData['COLONIA'],
  555. 'ALMA_LOCA' => $requestData['LOCALIDAD'],
  556. 'ALMA_MUNI' => $requestData['MUNICIPIO'],
  557. 'ALMA_ENTI' => $requestData['ENTIDAD_FEDERATIVA'],
  558. 'ALMA_PAIS' => $requestData['PAIS'],
  559. 'ALMA_USMO' => $user,
  560. 'ALMA_FEMO' => $currentDate,
  561. 'ALMA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  562. ]);
  563. } catch (\Throwable $th) {
  564. DB::rollBack();
  565. return $this->responseController->makeResponse(
  566. true,
  567. "ERR_WAREHOUSE_UPD004: Ocurrió un error al insertar el registro del almacen.",
  568. $th->getMessage(),
  569. 500
  570. );
  571. }
  572. if (!$validateUpdate) {
  573. DB::rollBack();
  574. return $this->responseController->makeResponse(
  575. true,
  576. "ERR_WAREHOUSE_UPD005: No se pudo insertar el registro del almacen.",
  577. [],
  578. 500
  579. );
  580. }
  581. DB::commit();
  582. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  583. }
  584. public function deleteWarehouse( Request $request, $idWarehouse ) {
  585. $validator = Validator::make($request->all(), [
  586. 'USUARIO' => 'required|string',
  587. 'NUMERO_LINEA' => 'required|string',
  588. ]);
  589. if ($validator->fails()) {
  590. return $this->responseController->makeResponse(
  591. true,
  592. "ERR_WAREHOUSE_DEL000: Se encontraron uno o más errores.",
  593. $this->responseController->makeErrors($validator->errors()->messages()),
  594. 401
  595. );
  596. }
  597. DB::beginTransaction();
  598. $requestData = $request->all();
  599. try {
  600. $idWarehouse = $this->encController->decrypt($idWarehouse);
  601. } catch (\Throwable $th) {
  602. DB::rollBack();
  603. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL001: Ocurrió un error al desencriptar el nivel.', $th->getMessage(), 500);
  604. }
  605. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  606. if ($arrResponseCheckUser['error']) {
  607. DB::rollBack();
  608. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL002:'.$arrResponseCheckUser['msg'], [], 401);
  609. }
  610. $user = $arrResponseCheckUser['response'];
  611. try {
  612. $validateExists = DB::table('S002V01TALMA')
  613. ->where('ALMA_COAL', '=', $idWarehouse)
  614. ->where('ALMA_ESTA', '=', 'Activo')
  615. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  616. ->exists();
  617. } catch (\Throwable $th) {
  618. DB::rollBack();
  619. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL003: Ocurrió un error al obtener la información del área.', $th->getMessage(), 500);
  620. }
  621. if (!$validateExists) {
  622. DB::rollBack();
  623. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL004: El almacen no existe.', [], 406);
  624. }
  625. try {
  626. $validateExists = DB::table('S002V01TUBAR')
  627. ->where('UBAR_COAL', '=', $idWarehouse)
  628. ->where('UBAR_NULI', '=', $requestData['NUMERO_LINEA'])
  629. ->where('UBAR_ESTA', '=', 'Activo')
  630. ->exists();
  631. } catch (\Throwable $th) {
  632. DB::rollBack();
  633. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL005: Ocurrió un error al obtener las ubicaciones de los artículos.', $th->getMessage(), 500);
  634. }
  635. if($validateExists) {
  636. DB::rollBack();
  637. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL006: No se puede eliminar debido a que existen artículos ubicados en el almacen.', [], 406);
  638. }
  639. try {
  640. $countArea = DB::table('S002V01TAREA')
  641. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  642. ->where('AREA_COAL', '=', $idWarehouse)
  643. ->where('AREA_ESTA', '=', 'Activo')
  644. ->count();
  645. } catch (\Throwable $th) {
  646. DB::rollBack();
  647. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL007: Ocurrió un error al obtener la información del área.', $th->getMessage(), 500);
  648. }
  649. if ($countArea > 0) {
  650. DB::rollBack();
  651. $quantity = $countArea === 1 ? 'existe 1 área activa' : 'existen'.$countArea.' áreas activas';
  652. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL008: No se puede elminar debido a que '.$quantity.'.', [], 406);
  653. }
  654. try {
  655. $countLevel = DB::table('S002V01TNIVE')
  656. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  657. ->where('NIVE_COAL', '=', $idWarehouse)
  658. ->where('NIVE_ESTA', '=', 'Activo')
  659. ->count();
  660. } catch (\Throwable $th) {
  661. DB::rollBack();
  662. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL009: Ocurrió un error al obtener las zonas.', $th->getMessage(), 500);
  663. }
  664. if ($countLevel > 0) {
  665. DB::rollBack();
  666. $quantity = $countLevel === 1 ? 'existe 1 nivel activo' : 'existen '.$countLevel.' niveles activos';
  667. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL010: No se puede eliminar debido a que '.$quantity.'.', [], 406);
  668. }
  669. try {
  670. $countZones = DB::table('S002V01TZONA')
  671. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  672. ->where('ZONA_COAL', '=', $idWarehouse)
  673. ->where('ZONA_ESTA', '=', 'Activo')
  674. ->count();
  675. } catch (\Throwable $th) {
  676. DB::rollBack();
  677. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL011: Ocurrió un error al obtener las zonas.', $th->getMessage(), 500);
  678. }
  679. if ($countZones > 0) {
  680. DB::rollBack();
  681. $quantity = $countZones === 1 ? 'existe 1 zona activa' : 'existen '.$countZones.' zonas activas';
  682. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL012: No se puede eliminar debido a que '.$quantity.'.', [], 406);
  683. }
  684. $now = $this->functionsController->now();
  685. $currentDate = $now->toDateTimeString();
  686. try {
  687. $validateUpdate = DB::table('S002V01TALMA')
  688. ->where('ALMA_COAL', '=', $idWarehouse)
  689. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  690. ->update([
  691. 'ALMA_ESTA' => 'Eliminado',
  692. 'ALMA_USMO' => $user,
  693. 'ALMA_FEMO' => $currentDate,
  694. 'ALMA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  695. ]);
  696. } catch (\Throwable $th) {
  697. DB::rollBack();
  698. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL013: Ocurrió un error al eliminar el área.', $th->getMessage(), 500);
  699. }
  700. if (!$validateUpdate) {
  701. DB::rollBack();
  702. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_DEL014: No se pudo eliminar el área.', [], 406);
  703. }
  704. DB::commit();
  705. return $this->responseController->makeResponse(false, "ÉXITO: Eliminación Exitosa");
  706. }
  707. public function getAreaByWarehouse($idWarehouse, $user, $line) {
  708. try {
  709. $idWarehouse = $this->encController->decrypt($idWarehouse);
  710. } catch (\Throwable $th) {
  711. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBY000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  712. }
  713. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  714. if ($arrResponseCheckUser['error']) {
  715. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBY001:'.$arrResponseCheckUser['msg'], [], 401);
  716. }
  717. try {
  718. $getArea = DB::table('S002V01TAREA')
  719. ->where('AREA_COAL', '=', $idWarehouse)
  720. ->where('AREA_NULI', '=', $line)
  721. ->get([
  722. 'AREA_COAR AS CODIGO_AREA',
  723. 'AREA_NOAR AS NOMBRE_AREA',
  724. 'AREA_COME AS COMENTARIOS',
  725. 'AREA_ESTA AS ESTADO',
  726. 'AREA_USRE AS USUARIO_REGISTRA',
  727. 'AREA_FERE AS FECHA_REGISTRA',
  728. 'AREA_USMO AS USUARIO_MODIFICA',
  729. 'AREA_FEMO AS FECHA_MODIFICA',
  730. ]);
  731. $arrArea = json_decode(json_encode($getArea), true);
  732. } catch (\Throwable $th) {
  733. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBY002: Ocurrió un error al obtener los registros del área.', $th->getMessage(), 406);
  734. }
  735. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrArea);
  736. }
  737. public function getAreaById($idArea, $user, $line) {
  738. try {
  739. $idArea = $this->encController->decrypt($idArea);
  740. } catch (\Throwable $th) {
  741. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBY000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  742. }
  743. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  744. if ($arrResponseCheckUser['error']) {
  745. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBY001:'.$arrResponseCheckUser['msg'], [], 401);
  746. }
  747. try {
  748. $getArea = (array) DB::table('S002V01TAREA')
  749. ->where('AREA_COAR', '=', $idArea)
  750. ->where('AREA_NULI', '=', $line)
  751. ->first([
  752. 'AREA_NOAR AS NOMBRE_AREA',
  753. 'AREA_COME AS COMENTARIOS',
  754. 'AREA_ESTA AS ESTADO',
  755. 'AREA_USRE AS USUARIO_REGISTRA',
  756. 'AREA_FERE AS FECHA_REGISTRA',
  757. 'AREA_USMO AS USUARIO_MODIFICA',
  758. 'AREA_FEMO AS FECHA_MODIFICA',
  759. ]);
  760. $arrArea = json_decode(json_encode($getArea), true);
  761. } catch (\Throwable $th) {
  762. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBY002: Ocurrió un error al obtener los registros del área.', $th->getMessage(), 406);
  763. }
  764. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrArea);
  765. }
  766. public function getAreaByWarehouseActives($idWarehouse, $user, $line) {
  767. try {
  768. $idWarehouse = $this->encController->decrypt($idWarehouse);
  769. } catch (\Throwable $th) {
  770. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBYACTIVE000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  771. }
  772. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  773. if ($arrResponseCheckUser['error']) {
  774. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBYACTIVE001:'.$arrResponseCheckUser['msg'], [], 401);
  775. }
  776. try {
  777. $validateExists = DB::table('S002V01TALMA')
  778. ->where('ALMA_NULI', '=', $line)
  779. ->where('ALMA_COAL', '=', $idWarehouse)
  780. ->exists();
  781. } catch (\Throwable $th) {
  782. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBYACTIVE002: Ocurrió un error al obtener el almacen.', $th->getMessage(), 406);
  783. }
  784. if (!$validateExists) {
  785. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBYACTIVE003: El almacen no existe.', [], 401);
  786. }
  787. try {
  788. $getArea = DB::table('S002V01TAREA')
  789. ->where('AREA_COAL', '=', $idWarehouse)
  790. ->where('AREA_NULI', '=', $line)
  791. ->where('AREA_ESTA', '=', 'Activo')
  792. ->get([
  793. 'AREA_COAR AS CODIGO_AREA',
  794. 'AREA_NOAR AS NOMBRE_AREA',
  795. 'AREA_COME AS COMENTARIOS',
  796. ]);
  797. $arrArea = json_decode(json_encode($getArea), true);
  798. } catch (\Throwable $th) {
  799. return $this->responseController->makeResponse(true, 'ERR_AREA_GETBYACTIVE004: Ocurrió un error al obtener los registros del área.', $th->getMessage(), 406);
  800. }
  801. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrArea);
  802. }
  803. public function registerArea(Request $request) {
  804. $validator = Validator::make($request->all(), [
  805. 'CODIGO_ALMACEN' => 'required|string',
  806. 'CODIGO_AREA' => 'required|string',
  807. 'NOMBRE_AREA' => 'required|string',
  808. 'COMENTARIOS' => 'nullable|string',
  809. 'USUARIO' => 'required|string',
  810. 'NUMERO_LINEA' => 'required|string',
  811. ]);
  812. if ($validator->fails()) {
  813. return $this->responseController->makeResponse(
  814. true,
  815. "ERR_AREA_REG000: Se encontraron uno o más errores.",
  816. $this->responseController->makeErrors($validator->errors()->messages()),
  817. 401
  818. );
  819. }
  820. DB::beginTransaction();
  821. $requestData = $request->all();
  822. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  823. if ($arrResponseCheckUser['error']) {
  824. return $this->responseController->makeResponse(true, 'ERR_AREA_REG001:'.$arrResponseCheckUser['msg'], [], 401);
  825. }
  826. $user = $arrResponseCheckUser['response'];
  827. try {
  828. $validateExistWarehouse = DB::table('S002V01TALMA')
  829. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  830. ->where('ALMA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  831. ->where('ALMA_ESTA', '=', 'Activo')
  832. ->exists();
  833. } catch (\Throwable $th) {
  834. DB::rollBack();
  835. return $this->responseController->makeResponse(true, 'ERR_AREA_REG002: Ocurrió un error al verificar el almacen.', $th->getMessage(), 500);
  836. }
  837. if (!$validateExistWarehouse) {
  838. DB::rollBack();
  839. return $this->responseController->makeResponse(true, 'ERR_AREA_REG003: El almacen no existe.', [], 406);
  840. }
  841. try {
  842. $validateExistCode = DB::table('S002V01TAREA')
  843. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  844. ->where('AREA_COAR', '=', $requestData['CODIGO_AREA'])
  845. ->where('AREA_ESTA', '=', 'Activo')
  846. ->exists();
  847. } catch (\Throwable $th) {
  848. DB::rollBack();
  849. return $this->responseController->makeResponse(true, 'ERR_AREA_REG004: Ocurrió un error al verificar el código del área.', $th->getMessage(), 500);
  850. }
  851. if ($validateExistCode) {
  852. DB::rollBack();
  853. return $this->responseController->makeResponse(true, 'ERR_AREA_REG005: El código del área ya se encuentra registrado.', [], 406);
  854. }
  855. $now = $this->functionsController->now();
  856. $currentDate = $now->toDateTimeString();
  857. try {
  858. $validateInsert = DB::table('S002V01TAREA')->insert([
  859. 'AREA_COAL' => $requestData['CODIGO_ALMACEN'],
  860. 'AREA_COAR' => $requestData['CODIGO_AREA'],
  861. 'AREA_NOAR' => $requestData['NOMBRE_AREA'],
  862. 'AREA_COME' => $requestData['COMENTARIOS'],
  863. 'AREA_NULI' => $requestData['NUMERO_LINEA'],
  864. 'AREA_USRE' => $user,
  865. 'AREA_FERE' => $currentDate,
  866. 'AREA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  867. ]);
  868. } catch (\Throwable $th) {
  869. DB::rollBack();
  870. return $this->responseController->makeResponse(true, 'ERR_AREA_REG006: Ocurrió un error al ingresar la información a la base de datos.', $th->getMessage(), 500);
  871. }
  872. if (!$validateInsert) {
  873. DB::rollBack();
  874. return $this->responseController->makeResponse(true, 'ERR_AREA_REG007: No se pudo guardar la información en la base de datos.', [], 406);
  875. }
  876. DB::commit();
  877. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  878. }
  879. public function updateArea(Request $request, $idArea) {
  880. $validator = Validator::make($request->all(), [
  881. 'CODIGO_ALMACEN' => 'required|string',
  882. 'NOMBRE_AREA' => 'required|string',
  883. 'COMENTARIOS' => 'nullable|string',
  884. 'USUARIO' => 'required|string',
  885. 'NUMERO_LINEA' => 'required|string',
  886. ]);
  887. if ($validator->fails()) {
  888. return $this->responseController->makeResponse(
  889. true,
  890. "ERR_AREA_UPD000: Se encontraron uno o más errores.",
  891. $this->responseController->makeErrors($validator->errors()->messages()),
  892. 401
  893. );
  894. }
  895. DB::beginTransaction();
  896. $requestData = $request->all();
  897. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  898. if ($arrResponseCheckUser['error']) {
  899. DB::rollBack();
  900. return $this->responseController->makeResponse(true, 'ERR_AREA_UPD001:'.$arrResponseCheckUser['msg'], $arrResponseCheckUser['response'], 401);
  901. }
  902. $user = $arrResponseCheckUser['response'];
  903. try {
  904. $idArea = $this->encController->decrypt($idArea);
  905. } catch (\Throwable $th) {
  906. DB::rollBack();
  907. return $this->responseController->makeResponse(true, 'ERR_AREA_UPD002: Ocurrió un error al desencriptar el área.', $th->getMessage(), 500);
  908. }
  909. try {
  910. $validateExistArea = DB::table('S002V01TAREA')
  911. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  912. ->where('AREA_COAR', '=', $idArea)
  913. ->where('AREA_ESTA', '=', 'Activo')
  914. ->exists();
  915. } catch (\Throwable $th) {
  916. DB::rollBack();
  917. return $this->responseController->makeResponse(true, 'ERR_AREA_UPD003: Ocurrió un error al verificar el código del área.', $th->getMessage(), 500);
  918. }
  919. if (!$validateExistArea) {
  920. DB::rollBack();
  921. return $this->responseController->makeResponse(true, 'ERR_AREA_UPD004: El área no existe.', [], 406);
  922. }
  923. try {
  924. $validateExistWarehouse = DB::table('S002V01TALMA')
  925. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  926. ->where('ALMA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  927. ->where('ALMA_ESTA', '=', 'Activo')
  928. ->exists();
  929. } catch (\Throwable $th) {
  930. DB::rollBack();
  931. return $this->responseController->makeResponse(true, 'ERR_AREA_UPD005: Ocurrió un error al verificar el almacen.', $th->getMessage(), 500);
  932. }
  933. if (!$validateExistWarehouse) {
  934. DB::rollBack();
  935. return $this->responseController->makeResponse(true, 'ERR_AREA_UPD006: El almacen no existe.', [], 406);
  936. }
  937. $now = $this->functionsController->now();
  938. $currentDate = $now->toDateTimeString();
  939. try {
  940. $validateUpdate = DB::table('S002V01TAREA')
  941. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  942. ->where('AREA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  943. ->where('AREA_COAR', '=', $idArea)
  944. ->update([
  945. 'AREA_NOAR' => $requestData['NOMBRE_AREA'],
  946. 'AREA_COME' => $requestData['COMENTARIOS'],
  947. 'AREA_USMO' => $user,
  948. 'AREA_FEMO' => $currentDate,
  949. 'AREA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  950. ]);
  951. } catch (\Throwable $th) {
  952. DB::rollBack();
  953. return $this->responseController->makeResponse(true, 'ERR_AREA_UPD009: Ocurrió un error al ingresar la información a la base de datos.', $th->getMessage(), 500);
  954. }
  955. if (!$validateUpdate) {
  956. DB::rollBack();
  957. return $this->responseController->makeResponse(true, 'ERR_AREA_UPD010: No se pudo guardar la información en la base de datos.', [], 406);
  958. }
  959. DB::commit();
  960. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  961. }
  962. public function deleteArea(Request $request, $idArea) {
  963. $validator = Validator::make($request->all(), [
  964. 'USUARIO' => 'required|string',
  965. 'NUMERO_LINEA' => 'required|string',
  966. ]);
  967. if ($validator->fails()) {
  968. return $this->responseController->makeResponse(
  969. true,
  970. "ERR_AREA_DEL000: Se encontraron uno o más errores.",
  971. $this->responseController->makeErrors($validator->errors()->messages()),
  972. 401
  973. );
  974. }
  975. DB::beginTransaction();
  976. $requestData = $request->all();
  977. try {
  978. $idArea = $this->encController->decrypt($idArea);
  979. } catch (\Throwable $th) {
  980. DB::rollBack();
  981. return $this->responseController->makeResponse(true, 'ERR_AREA_DEL001: Ocurrió un error al desencriptar el nivel.', $th->getMessage(), 500);
  982. }
  983. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  984. if ($arrResponseCheckUser['error']) {
  985. DB::rollBack();
  986. return $this->responseController->makeResponse(true, 'ERR_AREA_DEL002:'.$arrResponseCheckUser['msg'], [], 401);
  987. }
  988. $user = $arrResponseCheckUser['response'];
  989. try {
  990. $arrArea = (array) DB::table('S002V01TAREA')
  991. ->where('AREA_COAR', '=', $idArea)
  992. ->where('AREA_ESTA', '=', 'Activo')
  993. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  994. ->first([
  995. 'AREA_COAL',
  996. ]);
  997. } catch (\Throwable $th) {
  998. DB::rollBack();
  999. return $this->responseController->makeResponse(true, 'ERR_AREA_DEL003: Ocurrió un error al obtener la información del área.', $th->getMessage(), 500);
  1000. }
  1001. if (empty($arrArea)) {
  1002. DB::rollBack();
  1003. return $this->responseController->makeResponse(true, 'ERR_AREA_DEL004: El área no existe.', [], 406);
  1004. }
  1005. try {
  1006. $validateExists = DB::table('S002V01TUBAR')
  1007. ->where('UBAR_COAL', '=', $arrArea['AREA_COAL'])
  1008. ->where('UBAR_COZO', '=', $idArea)
  1009. ->where('UBAR_NULI', '=', $requestData['NUMERO_LINEA'])
  1010. ->where('UBAR_ESTA', '=', 'Activo')
  1011. ->exists();
  1012. } catch (\Throwable $th) {
  1013. DB::rollBack();
  1014. return $this->responseController->makeResponse(true, 'ERR_AREA_DEL005: Ocurrió un error al obtener las ubicaciones de los artículos.', $th->getMessage(), 500);
  1015. }
  1016. if($validateExists) {
  1017. DB::rollBack();
  1018. return $this->responseController->makeResponse(true, 'ERR_AREA_DEL006: No se puede eliminar debido a que existen artículos ubicados en el área.', [], 406);
  1019. }
  1020. try {
  1021. $countLevel = DB::table('S002V01TNIVE')
  1022. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  1023. ->where('NIVE_COAL', '=', $arrArea['AREA_COAL'])
  1024. ->where('NIVE_COAR', '=', $idArea)
  1025. ->where('NIVE_ESTA', '=', 'Activo')
  1026. ->count();
  1027. } catch (\Throwable $th) {
  1028. DB::rollBack();
  1029. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL007: Ocurrió un error al obtener las zonas.', $th->getMessage(), 500);
  1030. }
  1031. if ($countLevel > 0) {
  1032. DB::rollBack();
  1033. $quantity = $countLevel === 1 ? 'existe 1 nivel activo' : 'existen '.$countLevel.' niveles activos';
  1034. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL008: No se puede eliminar debido a que '.$quantity.'.', [], 406);
  1035. }
  1036. try {
  1037. $countZones = DB::table('S002V01TZONA')
  1038. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  1039. ->where('ZONA_COAL', '=', $arrArea['AREA_COAL'])
  1040. ->where('ZONA_COAR', '=', $idArea)
  1041. ->where('ZONA_ESTA', '=', 'Activo')
  1042. ->count();
  1043. } catch (\Throwable $th) {
  1044. DB::rollBack();
  1045. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL009: Ocurrió un error al obtener las zonas.', $th->getMessage(), 500);
  1046. }
  1047. if ($countZones > 0) {
  1048. DB::rollBack();
  1049. $quantity = $countZones === 1 ? 'existe 1 zona activa' : 'existen '.$countZones.' zonas activas';
  1050. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL010: No se puede eliminar debido a que '.$quantity.'.', [], 406);
  1051. }
  1052. $now = $this->functionsController->now();
  1053. $currentDate = $now->toDateTimeString();
  1054. try {
  1055. $validateUpdate = DB::table('S002V01TAREA')
  1056. ->where('AREA_COAR', '=', $idArea)
  1057. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  1058. ->update([
  1059. 'AREA_ESTA' => 'Eliminado',
  1060. 'AREA_USMO' => $user,
  1061. 'AREA_FEMO' => $currentDate,
  1062. 'AREA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  1063. ]);
  1064. } catch (\Throwable $th) {
  1065. DB::rollBack();
  1066. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL011: Ocurrió un error al eliminar el área.', $th->getMessage(), 500);
  1067. }
  1068. if (!$validateUpdate) {
  1069. DB::rollBack();
  1070. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL012: No se pudo eliminar el área.', [], 406);
  1071. }
  1072. DB::commit();
  1073. return $this->responseController->makeResponse(false, "ÉXITO: Eliminación Exitosa");
  1074. }
  1075. public function getLevelByAreaWarehouse($idWarehouse, $idArea, $user, $line) {
  1076. try {
  1077. $idWarehouse = $this->encController->decrypt($idWarehouse);
  1078. } catch (\Throwable $th) {
  1079. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBY000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  1080. }
  1081. try {
  1082. $idArea = $this->encController->decrypt($idArea);
  1083. } catch (\Throwable $th) {
  1084. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBY001: Ocurrió un error al desencriptar el ID del área', $th->getMessage(), 406);
  1085. }
  1086. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  1087. if ($arrResponseCheckUser['error']) {
  1088. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBY002:'.$arrResponseCheckUser['msg'], [], 401);
  1089. }
  1090. try {
  1091. $getLevel = DB::table('S002V01TNIVE')
  1092. ->where('NIVE_COAL', '=', $idWarehouse)
  1093. ->where('NIVE_COAR', '=', $idArea)
  1094. ->where('NIVE_NULI', '=', $line)
  1095. ->get([
  1096. 'NIVE_CONI AS CODIGO_NIVEL',
  1097. 'NIVE_NONI AS NOMBRE_NIVEL',
  1098. 'NIVE_COME AS COMENTARIOS',
  1099. 'NIVE_ESTA AS ESTADO',
  1100. 'NIVE_USRE AS USUARIO_REGISTRA',
  1101. 'NIVE_FERE AS FECHA_REGISTRA',
  1102. 'NIVE_USMO AS USUARIO_MODIFICA',
  1103. 'NIVE_FEMO AS FECHA_MODIFICA',
  1104. ]);
  1105. $arrLevel = json_decode(json_encode($getLevel), true);
  1106. } catch (\Throwable $th) {
  1107. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBY003: Ocurrió un error al obtener los registros del área.', $th->getMessage(), 406);
  1108. }
  1109. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrLevel);
  1110. }
  1111. public function getLevelById( $idLevel, $user, $line ) {
  1112. try {
  1113. $idLevel = $this->encController->decrypt($idLevel);
  1114. } catch (\Throwable $th) {
  1115. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBY000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  1116. }
  1117. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  1118. if ($arrResponseCheckUser['error']) {
  1119. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBY001:'.$arrResponseCheckUser['msg'], [], 401);
  1120. }
  1121. try {
  1122. $getLevel = (array) DB::table('S002V01TNIVE')
  1123. ->where('NIVE_CONI', '=', $idLevel)
  1124. ->where('NIVE_NULI', '=', $line)
  1125. ->first([
  1126. 'NIVE_NONI AS NOMBRE_NIVEL',
  1127. 'NIVE_COME AS COMENTARIOS',
  1128. 'NIVE_ESTA AS ESTADO',
  1129. 'NIVE_USRE AS USUARIO_REGISTRA',
  1130. 'NIVE_FERE AS FECHA_REGISTRA',
  1131. 'NIVE_USMO AS USUARIO_MODIFICA',
  1132. 'NIVE_FEMO AS FECHA_MODIFICA',
  1133. ]);
  1134. $arrLevel = json_decode(json_encode($getLevel), true);
  1135. } catch (\Throwable $th) {
  1136. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBY002: Ocurrió un error al obtener los registros del área.', $th->getMessage(), 406);
  1137. }
  1138. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrLevel);
  1139. }
  1140. public function getLevelByAreaWarehouseActives($idWarehouse, $idArea, $user, $line) {
  1141. try {
  1142. $idWarehouse = $this->encController->decrypt($idWarehouse);
  1143. } catch (\Throwable $th) {
  1144. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBYACTIVE000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  1145. }
  1146. try {
  1147. $idArea = $this->encController->decrypt($idArea);
  1148. } catch (\Throwable $th) {
  1149. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBYACTIVE001: Ocurrió un error al desencriptar el ID del área', $th->getMessage(), 406);
  1150. }
  1151. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  1152. if ($arrResponseCheckUser['error']) {
  1153. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBYACTIVE002:'.$arrResponseCheckUser['msg'], [], 401);
  1154. }
  1155. try {
  1156. $validateExists = DB::table('S002V01TALMA')
  1157. ->where('ALMA_NULI', '=', $line)
  1158. ->where('ALMA_COAL', '=', $idWarehouse)
  1159. ->exists();
  1160. } catch (\Throwable $th) {
  1161. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBYACTIVE003: Ocurrió un error al obtener el almacen.', $th->getMessage(), 406);
  1162. }
  1163. if (!$validateExists) {
  1164. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBYACTIVE004: El almacen no existe.', [], 401);
  1165. }
  1166. try {
  1167. $validateExists = DB::table('S002V01TAREA')
  1168. ->where('AREA_NULI', '=', $line)
  1169. ->where('AREA_COAL', '=', $idWarehouse)
  1170. ->where('AREA_COAR', '=', $idArea)
  1171. ->exists();
  1172. } catch (\Throwable $th) {
  1173. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBYACTIVE005: Ocurrió un error al obtener el área.', $th->getMessage(), 406);
  1174. }
  1175. if (!$validateExists) {
  1176. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBYACTIVE006: El área no existe.', [], 401);
  1177. }
  1178. try {
  1179. $getLevel = DB::table('S002V01TNIVE')
  1180. ->where('NIVE_COAL', '=', $idWarehouse)
  1181. ->where('NIVE_COAR', '=', $idArea)
  1182. ->where('NIVE_NULI', '=', $line)
  1183. ->where('NIVE_ESTA', '=', 'Activo')
  1184. ->get([
  1185. 'NIVE_CONI AS CODIGO_NIVEL',
  1186. 'NIVE_NONI AS NOMBRE_NIVEL',
  1187. 'NIVE_COME AS COMENTARIOS',
  1188. ]);
  1189. $arrLevel = json_decode(json_encode($getLevel), true);
  1190. } catch (\Throwable $th) {
  1191. return $this->responseController->makeResponse(true, 'ERR_LEVEL_GETBYACTIVE007: Ocurrió un error al obtener los registros del área.', $th->getMessage(), 406);
  1192. }
  1193. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrLevel);
  1194. }
  1195. public function registerLevel(Request $request) {
  1196. $validator = Validator::make($request->all(), [
  1197. 'CODIGO_ALMACEN' => 'required|string',
  1198. 'CODIGO_AREA' => 'required|string',
  1199. 'CODIGO_NIVEL' => 'required|string',
  1200. 'NOMBRE_NIVEL' => 'required|string',
  1201. 'COMENTARIOS' => 'nullable|string',
  1202. 'USUARIO' => 'required|string',
  1203. 'NUMERO_LINEA' => 'required|string',
  1204. ]);
  1205. if ($validator->fails()) {
  1206. return $this->responseController->makeResponse(
  1207. true,
  1208. "ERR_LEVEL_REG000: Se encontraron uno o más errores.",
  1209. $this->responseController->makeErrors($validator->errors()->messages()),
  1210. 401
  1211. );
  1212. }
  1213. DB::beginTransaction();
  1214. $requestData = $request->all();
  1215. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  1216. if ($arrResponseCheckUser['error']) {
  1217. return $this->responseController->makeResponse(true, 'ERR_LEVEL_REG001:'.$arrResponseCheckUser['msg'], [], 401);
  1218. }
  1219. $user = $arrResponseCheckUser['response'];
  1220. try {
  1221. $validateExistWarehouse = DB::table('S002V01TALMA')
  1222. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  1223. ->where('ALMA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1224. ->where('ALMA_ESTA', '=', 'Activo')
  1225. ->exists();
  1226. } catch (\Throwable $th) {
  1227. DB::rollBack();
  1228. return $this->responseController->makeResponse(true, 'ERR_LEVEL_REG002: Ocurrió un error al verificar el almacen.', $th->getMessage(), 500);
  1229. }
  1230. if (!$validateExistWarehouse) {
  1231. DB::rollBack();
  1232. return $this->responseController->makeResponse(true, 'ERR_LEVEL_REG003: El almacen no existe.', [], 406);
  1233. }
  1234. try {
  1235. $validateExistArea = DB::table('S002V01TAREA')
  1236. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  1237. ->where('AREA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1238. ->where('AREA_COAR', '=', $requestData['CODIGO_AREA'])
  1239. ->where('AREA_ESTA', '=', 'Activo')
  1240. ->exists();
  1241. } catch (\Throwable $th) {
  1242. DB::rollBack();
  1243. return $this->responseController->makeResponse(true, 'ERR_LEVEL_REG004: Ocurrió un error al verificar el área.', $th->getMessage(), 500);
  1244. }
  1245. if (!$validateExistArea) {
  1246. DB::rollBack();
  1247. return $this->responseController->makeResponse(true, 'ERR_LEVEL_REG005: El área no existe.', [], 406);
  1248. }
  1249. try {
  1250. $validateExistLevel = DB::table('S002V01TNIVE')
  1251. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  1252. ->where('NIVE_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1253. ->where('NIVE_COAR', '=', $requestData['CODIGO_AREA'])
  1254. ->where('NIVE_CONI', '=', $requestData['CODIGO_NIVEL'])
  1255. ->exists();
  1256. } catch (\Throwable $th) {
  1257. DB::rollBack();
  1258. return $this->responseController->makeResponse(true, 'ERR_LEVEL_REG006: Ocurrió un error al verificar el código del nivel.', $th->getMessage(), 406);
  1259. }
  1260. if ($validateExistLevel) {
  1261. DB::rollBack();
  1262. return $this->responseController->makeResponse(true, 'ERR_LEVEL_REG007: El código del nivel ya existe.', [], 406);
  1263. }
  1264. $now = $this->functionsController->now();
  1265. $currentDate = $now->toDateTimeString();
  1266. try {
  1267. $validateInsert = DB::table('S002V01TNIVE')->insert([
  1268. 'NIVE_NULI' => $requestData['NUMERO_LINEA'],
  1269. 'NIVE_COAL' => $requestData['CODIGO_ALMACEN'],
  1270. 'NIVE_COAR' => $requestData['CODIGO_AREA'],
  1271. 'NIVE_CONI' => $requestData['CODIGO_NIVEL'],
  1272. 'NIVE_NONI' => $requestData['NOMBRE_NIVEL'],
  1273. 'NIVE_COME' => $requestData['COMENTARIOS'],
  1274. 'NIVE_USRE' => $user,
  1275. 'NIVE_FERE' => $currentDate,
  1276. 'NIVE_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  1277. ]);
  1278. } catch (\Throwable $th) {
  1279. DB::rollBack();
  1280. return $this->responseController->makeResponse(true, 'ERR_LEVEL_REG008: Ocurrió un error al ingresar la información a la base de datos.', $th->getMessage(), 500);
  1281. }
  1282. if (!$validateInsert) {
  1283. DB::rollBack();
  1284. return $this->responseController->makeResponse(true, 'ERR_LEVEL_REG009: No se pudo guardar la información en la base de datos.', [], 406);
  1285. }
  1286. DB::commit();
  1287. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  1288. }
  1289. public function updateLevel(Request $request, $idLevel) {
  1290. $validator = Validator::make($request->all(), [
  1291. 'CODIGO_ALMACEN' => 'required|string',
  1292. 'NOMBRE_NIVEL' => 'required|string',
  1293. 'COMENTARIOS' => 'nullable|string',
  1294. 'USUARIO' => 'required|string',
  1295. 'NUMERO_LINEA' => 'required|string',
  1296. ]);
  1297. if ($validator->fails()) {
  1298. return $this->responseController->makeResponse(
  1299. true,
  1300. "ERR_LEVEL_UPD000: Se encontraron uno o más errores.",
  1301. $this->responseController->makeErrors($validator->errors()->messages()),
  1302. 401
  1303. );
  1304. }
  1305. DB::beginTransaction();
  1306. $requestData = $request->all();
  1307. try {
  1308. $idLevel = $this->encController->decrypt($idLevel);
  1309. } catch (\Throwable $th) {
  1310. DB::rollBack();
  1311. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD001: Ocurrió un error al desencriptar el área.', $th->getMessage(), 500);
  1312. }
  1313. try {
  1314. $validateExistLevel = DB::table('S002V01TNIVE')
  1315. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  1316. ->where('NIVE_CONI', '=', $idLevel)
  1317. ->where('NIVE_ESTA', '=', 'Activo')
  1318. ->exists();
  1319. } catch (\Throwable $th) {
  1320. DB::rollBack();
  1321. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD002: Ocurrió un error al verificar el código del área.', $th->getMessage(), 500);
  1322. }
  1323. if (!$validateExistLevel) {
  1324. DB::rollBack();
  1325. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD003: El área no existe.', [], 406);
  1326. }
  1327. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  1328. if ($arrResponseCheckUser['error']) {
  1329. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD004:'.$arrResponseCheckUser['msg'], [], 401);
  1330. }
  1331. $user = $arrResponseCheckUser['response'];
  1332. try {
  1333. $validateExistWarehouse = DB::table('S002V01TALMA')
  1334. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  1335. ->where('ALMA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1336. ->where('ALMA_ESTA', '=', 'Activo')
  1337. ->exists();
  1338. } catch (\Throwable $th) {
  1339. DB::rollBack();
  1340. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD005: Ocurrió un error al verificar el almacen.', $th->getMessage(), 500);
  1341. }
  1342. if (!$validateExistWarehouse) {
  1343. DB::rollBack();
  1344. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD006: El almacen no existe.', [], 406);
  1345. }
  1346. try {
  1347. $validateExistArea = DB::table('S002V01TAREA')
  1348. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  1349. ->where('AREA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1350. ->where('AREA_COAR', '=', $requestData['CODIGO_AREA'])
  1351. ->where('AREA_ESTA', '=', 'Activo')
  1352. ->exists();
  1353. } catch (\Throwable $th) {
  1354. DB::rollBack();
  1355. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD007: Ocurrió un error al verificar el área.', $th->getMessage(), 500);
  1356. }
  1357. if (!$validateExistArea) {
  1358. DB::rollBack();
  1359. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD008: El área no existe.', [], 406);
  1360. }
  1361. try {
  1362. $validateExistLevel = DB::table('S002V01TNIVE')
  1363. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  1364. ->where('NIVE_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1365. ->where('NIVE_COAR', '=', $requestData['CODIGO_AREA'])
  1366. ->where('NIVE_CONI', '=', $requestData['CODIGO_NIVEL'])
  1367. ->exists();
  1368. } catch (\Throwable $th) {
  1369. DB::rollBack();
  1370. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD009: Ocurrió un error al verificar el código del nivel.', $th->getMessage(), 406);
  1371. }
  1372. if ($validateExistLevel) {
  1373. DB::rollBack();
  1374. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD010: El código del nivel ya existe.', [], 406);
  1375. }
  1376. $now = $this->functionsController->now();
  1377. $currentDate = $now->toDateTimeString();
  1378. try {
  1379. $validateUpdate = DB::table('S002V01TNIVE')
  1380. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  1381. ->where('NIVE_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1382. ->where('NIVE_COAR', '=', $requestData['CODIGO_AREA'])
  1383. ->where('NIVE_CONI', '=', $idLevel)
  1384. ->update([
  1385. 'NIVE_NONI' => $requestData['NOMBRE_NIVEL'],
  1386. 'NIVE_COME' => $requestData['COMENTARIOS'],
  1387. 'NIVE_USMO' => $user,
  1388. 'NIVE_FEMO' => $currentDate,
  1389. 'NIVE_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  1390. ]);
  1391. } catch (\Throwable $th) {
  1392. DB::rollBack();
  1393. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD011: Ocurrió un error al ingresar la información a la base de datos.', $th->getMessage(), 500);
  1394. }
  1395. if (!$validateUpdate) {
  1396. DB::rollBack();
  1397. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD012: No se pudo guardar la información en la base de datos.', [], 406);
  1398. }
  1399. DB::commit();
  1400. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  1401. }
  1402. public function deleteLevel(Request $request, $idLevel) {
  1403. $validator = Validator::make($request->all(), [
  1404. 'USUARIO' => 'required|string',
  1405. 'NUMERO_LINEA' => 'required|string',
  1406. ]);
  1407. if ($validator->fails()) {
  1408. return $this->responseController->makeResponse(
  1409. true,
  1410. "ERR_LEVEL_DEL000: Se encontraron uno o más errores.",
  1411. $this->responseController->makeErrors($validator->errors()->messages()),
  1412. 401
  1413. );
  1414. }
  1415. DB::beginTransaction();
  1416. $requestData = $request->all();
  1417. try {
  1418. $idLevel = $this->encController->decrypt($idLevel);
  1419. } catch (\Throwable $th) {
  1420. DB::rollBack();
  1421. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL001: Ocurrió un error al desencriptar el nivel.', $th->getMessage(), 500);
  1422. }
  1423. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  1424. if ($arrResponseCheckUser['error']) {
  1425. DB::rollBack();
  1426. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL002:'.$arrResponseCheckUser['msg'], [], 401);
  1427. }
  1428. $user = $arrResponseCheckUser['response'];
  1429. try {
  1430. $arrLevel = (array) DB::table('S002V01TNIVE')
  1431. ->where('NIVE_CONI', '=', $idLevel)
  1432. ->where('NIVE_ESTA', '=', 'Activo')
  1433. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  1434. ->first([
  1435. 'NIVE_COAL',
  1436. 'NIVE_COAR'
  1437. ]);
  1438. } catch (\Throwable $th) {
  1439. DB::rollBack();
  1440. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL003: Ocurrió un error al obtener la información del nivel.', $th->getMessage(), 500);
  1441. }
  1442. if (empty($arrLevel)) {
  1443. DB::rollBack();
  1444. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL004: El nivel no existe.', [], 406);
  1445. }
  1446. try {
  1447. $validateExists = DB::table('S002V01TUBAR')
  1448. ->where('UBAR_COAL', '=', $arrLevel['NIVE_COAL'])
  1449. ->where('UBAR_COAR', '=', $arrLevel['NIVE_COAR'])
  1450. ->where('UBAR_COZO', '=', $idLevel)
  1451. ->where('UBAR_NULI', '=', $requestData['NUMERO_LINEA'])
  1452. ->where('UBAR_ESTA', '=', 'Activo')
  1453. ->exists();
  1454. } catch (\Throwable $th) {
  1455. DB::rollBack();
  1456. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL005: Ocurrió un error al obtener las ubicaciones de los artículos.', $th->getMessage(), 500);
  1457. }
  1458. if($validateExists) {
  1459. DB::rollBack();
  1460. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL006: No se puede eliminar debido a que existen artículos ubicados en el nivel.', [], 406);
  1461. }
  1462. try {
  1463. $countZones = DB::table('S002V01TZONA')
  1464. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  1465. ->where('ZONA_COAL', '=', $arrLevel['NIVE_COAL'])
  1466. ->where('ZONA_COAR', '=', $arrLevel['NIVE_COAR'])
  1467. ->where('ZONA_CONI', '=', $idLevel)
  1468. ->where('ZONA_ESTA', '=', 'Activo')
  1469. ->count();
  1470. } catch (\Throwable $th) {
  1471. DB::rollBack();
  1472. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL007: Ocurrió un error al obtener las zonas.', $th->getMessage(), 500);
  1473. }
  1474. if ($countZones > 0) {
  1475. DB::rollBack();
  1476. $quantity = $countZones === 1 ? 'existe 1 zona activa' : 'existen '.$countZones.' zonas activas';
  1477. return $this->responseController->makeResponse(true, 'ERR_LEVEL_DEL008: No se puede eliminar debido a que '.$quantity.'.', [], 406);
  1478. }
  1479. $now = $this->functionsController->now();
  1480. $currentDate = $now->toDateTimeString();
  1481. try {
  1482. $validateUpdate = DB::table('S002V01TNIVE')
  1483. ->where('NIVE_CONI', '=', $idLevel)
  1484. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  1485. ->update([
  1486. 'NIVE_ESTA' => 'Eliminado',
  1487. 'NIVE_USMO' => $user,
  1488. 'NIVE_FEMO' => $currentDate,
  1489. 'NIVE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  1490. ]);
  1491. } catch (\Throwable $th) {
  1492. DB::rollBack();
  1493. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL009: Ocurrió un error al eliminar el nivel.', $th->getMessage(), 500);
  1494. }
  1495. if (!$validateUpdate) {
  1496. DB::rollBack();
  1497. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL010: No se pudo eliminar el nivel.', [], 406);
  1498. }
  1499. DB::commit();
  1500. return $this->responseController->makeResponse(false, "ÉXITO: Eliminación Exitosa");
  1501. }
  1502. public function getZoneByLevelAreaWarehouse($idWarehouse, $idArea, $idLevel, $user, $line) {
  1503. try {
  1504. $idWarehouse = $this->encController->decrypt($idWarehouse);
  1505. } catch (\Throwable $th) {
  1506. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBY000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  1507. }
  1508. try {
  1509. $idArea = $this->encController->decrypt($idArea);
  1510. } catch (\Throwable $th) {
  1511. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBY001: Ocurrió un error al desencriptar el ID del área', $th->getMessage(), 406);
  1512. }
  1513. try {
  1514. $idLevel = $this->encController->decrypt($idLevel);
  1515. } catch (\Throwable $th) {
  1516. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBY002: Ocurrió un error al desencriptar el ID del nivel', $th->getMessage(), 406);
  1517. }
  1518. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  1519. if ($arrResponseCheckUser['error']) {
  1520. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBY003:'.$arrResponseCheckUser['msg'], [], 401);
  1521. }
  1522. try {
  1523. $getZone = DB::table('S002V01TZONA')
  1524. ->where('ZONA_COAL', '=', $idWarehouse)
  1525. ->where('ZONA_COAR', '=', $idArea)
  1526. ->where('ZONA_CONI', '=', $idLevel)
  1527. ->where('ZONA_NULI', '=', $line)
  1528. ->get([
  1529. 'ZONA_COZO AS CODIGO_ZONA',
  1530. 'ZONA_NOZO AS NOMBRE_ZONA',
  1531. 'ZONA_COME AS COMENTARIOS',
  1532. 'ZONA_ESTA AS ESTADO',
  1533. 'ZONA_USRE AS USUARIO_REGISTRA',
  1534. 'ZONA_FERE AS FECHA_REGISTRA',
  1535. 'ZONA_USMO AS USUARIO_MODIFICA',
  1536. 'ZONA_FEMO AS FECHA_MODIFICA',
  1537. ]);
  1538. $arrZone = json_decode(json_encode($getZone), true);
  1539. } catch (\Throwable $th) {
  1540. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBY004: Ocurrió un error al obtener los registros del área.', $th->getMessage(), 406);
  1541. }
  1542. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrZone);
  1543. }
  1544. public function getZoneById($idZone, $user, $line) {
  1545. try {
  1546. $idZone = $this->encController->decrypt($idZone);
  1547. } catch (\Throwable $th) {
  1548. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBY000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  1549. }
  1550. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  1551. if ($arrResponseCheckUser['error']) {
  1552. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBY001:'.$arrResponseCheckUser['msg'], [], 401);
  1553. }
  1554. try {
  1555. $getZone = (array) DB::table('S002V01TZONA')
  1556. ->where('ZONA_COZO', '=', $idZone)
  1557. ->where('ZONA_NULI', '=', $line)
  1558. ->first([
  1559. 'ZONA_NOZO AS NOMBRE_ZONA',
  1560. 'ZONA_COME AS COMENTARIOS',
  1561. 'ZONA_ESTA AS ESTADO',
  1562. 'ZONA_USRE AS USUARIO_REGISTRA',
  1563. 'ZONA_FERE AS FECHA_REGISTRA',
  1564. 'ZONA_USMO AS USUARIO_MODIFICA',
  1565. 'ZONA_FEMO AS FECHA_MODIFICA',
  1566. ]);
  1567. $arrZone = json_decode(json_encode($getZone), true);
  1568. } catch (\Throwable $th) {
  1569. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBY002: Ocurrió un error al obtener los registros del área.', $th->getMessage(), 406);
  1570. }
  1571. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrZone);
  1572. }
  1573. public function getZoneByLevelAreaWarehouseActive($idWarehouse, $idArea, $idLevel, $user, $line) {
  1574. try {
  1575. $idWarehouse = $this->encController->decrypt($idWarehouse);
  1576. } catch (\Throwable $th) {
  1577. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  1578. }
  1579. try {
  1580. $idArea = $this->encController->decrypt($idArea);
  1581. } catch (\Throwable $th) {
  1582. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE001: Ocurrió un error al desencriptar el ID del área', $th->getMessage(), 406);
  1583. }
  1584. try {
  1585. $idLevel = $this->encController->decrypt($idLevel);
  1586. } catch (\Throwable $th) {
  1587. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE002: Ocurrió un error al desencriptar el ID del nivel', $th->getMessage(), 406);
  1588. }
  1589. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  1590. if ($arrResponseCheckUser['error']) {
  1591. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE003:'.$arrResponseCheckUser['msg'], [], 401);
  1592. }
  1593. try {
  1594. $validateExists = DB::table('S002V01TALMA')
  1595. ->where('ALMA_NULI', '=', $line)
  1596. ->where('ALMA_COAL', '=', $idWarehouse)
  1597. ->exists();
  1598. } catch (\Throwable $th) {
  1599. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE004: Ocurrió un error al obtener el almacen.', $th->getMessage(), 406);
  1600. }
  1601. if (!$validateExists) {
  1602. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE005: El almacen no existe.', [], 401);
  1603. }
  1604. try {
  1605. $validateExists = DB::table('S002V01TAREA')
  1606. ->where('AREA_NULI', '=', $line)
  1607. ->where('AREA_COAL', '=', $idWarehouse)
  1608. ->where('AREA_COAR', '=', $idArea)
  1609. ->exists();
  1610. } catch (\Throwable $th) {
  1611. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE006: Ocurrió un error al obtener el área.', $th->getMessage(), 406);
  1612. }
  1613. if (!$validateExists) {
  1614. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE007: El área no existe.', [], 401);
  1615. }
  1616. try {
  1617. $validateExists = DB::table('S002V01TNIVE')
  1618. ->where('NIVE_NULI', '=', $line)
  1619. ->where('NIVE_COAL', '=', $idWarehouse)
  1620. ->where('NIVE_COAR', '=', $idArea)
  1621. ->where('NIVE_CONI', '=', $idLevel)
  1622. ->exists();
  1623. } catch (\Throwable $th) {
  1624. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE008: Ocurrió un error al obtener el nivel.', $th->getMessage(), 406);
  1625. }
  1626. if (!$validateExists) {
  1627. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE009: El nivel no existe.', [], 401);
  1628. }
  1629. try {
  1630. $getZone = DB::table('S002V01TZONA')
  1631. ->where('ZONA_COAL', '=', $idWarehouse)
  1632. ->where('ZONA_COAR', '=', $idArea)
  1633. ->where('ZONA_CONI', '=', $idLevel)
  1634. ->where('ZONA_NULI', '=', $line)
  1635. ->where('ZONA_ESTA', '=', 'Activo')
  1636. ->get([
  1637. 'ZONA_COZO AS CODIGO_ZONA',
  1638. 'ZONA_NOZO AS NOMBRE_ZONA',
  1639. 'ZONA_COME AS COMENTARIOS',
  1640. ]);
  1641. $arrZone = json_decode(json_encode($getZone), true);
  1642. } catch (\Throwable $th) {
  1643. return $this->responseController->makeResponse(true, 'ERR_ZONE_GETBYACTIVE010: Ocurrió un error al obtener los registros del área.', $th->getMessage(), 406);
  1644. }
  1645. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrZone);
  1646. }
  1647. public function registerZone(Request $request) {
  1648. $validator = Validator::make($request->all(), [
  1649. 'CODIGO_ALMACEN' => 'required|string',
  1650. 'CODIGO_AREA' => 'required|string',
  1651. 'CODIGO_NIVEL' => 'required|string',
  1652. 'CODIGO_ZONA' => 'required|string',
  1653. 'NOMBRE_ZONA' => 'required|string',
  1654. 'COMENTARIOS' => 'nullable|string',
  1655. 'USUARIO' => 'required|string',
  1656. 'NUMERO_LINEA' => 'required|string',
  1657. ]);
  1658. if ($validator->fails()) {
  1659. return $this->responseController->makeResponse(
  1660. true,
  1661. "ERR_ZONE_REG000: Se encontraron uno o más errores.",
  1662. $this->responseController->makeErrors($validator->errors()->messages()),
  1663. 401
  1664. );
  1665. }
  1666. DB::beginTransaction();
  1667. $requestData = $request->all();
  1668. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  1669. if ($arrResponseCheckUser['error']) {
  1670. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG001:'.$arrResponseCheckUser['msg'], [], 401);
  1671. }
  1672. $user = $arrResponseCheckUser['response'];
  1673. try {
  1674. $validateExistWarehouse = DB::table('S002V01TALMA')
  1675. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  1676. ->where('ALMA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1677. ->where('ALMA_ESTA', '=', 'Activo')
  1678. ->exists();
  1679. } catch (\Throwable $th) {
  1680. DB::rollBack();
  1681. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG002: Ocurrió un error al verificar el almacen.', $th->getMessage(), 500);
  1682. }
  1683. if (!$validateExistWarehouse) {
  1684. DB::rollBack();
  1685. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG003: El almacen no existe.', [], 406);
  1686. }
  1687. try {
  1688. $validateExistArea = DB::table('S002V01TAREA')
  1689. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  1690. ->where('AREA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1691. ->where('AREA_COAR', '=', $requestData['CODIGO_AREA'])
  1692. ->where('AREA_ESTA', '=', 'Activo')
  1693. ->exists();
  1694. } catch (\Throwable $th) {
  1695. DB::rollBack();
  1696. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG004: Ocurrió un error al verificar el área.', $th->getMessage(), 500);
  1697. }
  1698. if (!$validateExistArea) {
  1699. DB::rollBack();
  1700. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG005: El área no existe.', [], 406);
  1701. }
  1702. try {
  1703. $validateExistNivel = DB::table('S002V01TNIVE')
  1704. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  1705. ->where('NIVE_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1706. ->where('NIVE_COAR', '=', $requestData['CODIGO_AREA'])
  1707. ->where('NIVE_CONI', '=', $requestData['CODIGO_NIVEL'])
  1708. ->where('NIVE_ESTA', '=', 'Activo')
  1709. ->exists();
  1710. } catch (\Throwable $th) {
  1711. DB::rollBack();
  1712. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG006: Ocurrió un error al verificar el nivel.', $th->getMessage(), 500);
  1713. }
  1714. if (!$validateExistNivel) {
  1715. DB::rollBack();
  1716. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG007: El nivel no existe.', [], 406);
  1717. }
  1718. try {
  1719. $validateExistZone = DB::table('S002V01TZONA')
  1720. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  1721. ->where('ZONA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1722. ->where('ZONA_COAR', '=', $requestData['CODIGO_AREA'])
  1723. ->where('ZONA_CONI', '=', $requestData['CODIGO_NIVEL'])
  1724. ->where('ZONA_COZO', '=', $requestData['CODIGO_ZONA'])
  1725. ->exists();
  1726. } catch (\Throwable $th) {
  1727. DB::rollBack();
  1728. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG008: Ocurrió un error al verificar el código de la zona.', $th->getMessage(), 406);
  1729. }
  1730. if ($validateExistZone) {
  1731. DB::rollBack();
  1732. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG009: El código de la zona ya existe.', [], 406);
  1733. }
  1734. $now = $this->functionsController->now();
  1735. $currentDate = $now->toDateTimeString();
  1736. try {
  1737. $validateInsert = DB::table('S002V01TZONA')->insert([
  1738. 'ZONA_NULI' => $requestData['NUMERO_LINEA'],
  1739. 'ZONA_COAL' => $requestData['CODIGO_ALMACEN'],
  1740. 'ZONA_COAR' => $requestData['CODIGO_AREA'],
  1741. 'ZONA_CONI' => $requestData['CODIGO_NIVEL'],
  1742. 'ZONA_COZO' => $requestData['CODIGO_ZONA'],
  1743. 'ZONA_NOZO' => $requestData['NOMBRE_ZONA'],
  1744. 'ZONA_COME' => $requestData['COMENTARIOS'],
  1745. 'ZONA_USRE' => $user,
  1746. 'ZONA_FERE' => $currentDate,
  1747. 'ZONA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  1748. ]);
  1749. } catch (\Throwable $th) {
  1750. DB::rollBack();
  1751. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG010: Ocurrió un error al ingresar la información a la base de datos.', $th->getMessage(), 500);
  1752. }
  1753. if (!$validateInsert) {
  1754. DB::rollBack();
  1755. return $this->responseController->makeResponse(true, 'ERR_ZONE_REG011: No se pudo guardar la información en la base de datos.', [], 406);
  1756. }
  1757. DB::commit();
  1758. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  1759. }
  1760. public function updateZone(Request $request, $idZone) {
  1761. $validator = Validator::make($request->all(), [
  1762. 'CODIGO_ALMACEN' => 'required|string',
  1763. 'CODIGO_AREA' => 'required|string',
  1764. 'CODIGO_NIVEL' => 'required|string',
  1765. 'NOMBRE_ZONA' => 'required|string',
  1766. 'COMENTARIOS' => 'nullable|string',
  1767. 'USUARIO' => 'required|string',
  1768. 'NUMERO_LINEA' => 'required|string',
  1769. ]);
  1770. if ($validator->fails()) {
  1771. return $this->responseController->makeResponse(
  1772. true,
  1773. "ERR_ZONE_UPD000: Se encontraron uno o más errores.",
  1774. $this->responseController->makeErrors($validator->errors()->messages()),
  1775. 401
  1776. );
  1777. }
  1778. DB::beginTransaction();
  1779. $requestData = $request->all();
  1780. try {
  1781. $idZone = $this->encController->decrypt($idZone);
  1782. } catch (\Throwable $th) {
  1783. DB::rollBack();
  1784. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD001: Ocurrió un error al desencriptar la zona.', $th->getMessage(), 500);
  1785. }
  1786. try {
  1787. $validateExistLevel = DB::table('S002V01TZONA')
  1788. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  1789. ->where('ZONA_COZO', '=', $idZone)
  1790. ->where('ZONA_ESTA', '=', 'Activo')
  1791. ->exists();
  1792. } catch (\Throwable $th) {
  1793. DB::rollBack();
  1794. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD002: Ocurrió un error al verificar el código de la zona.', $th->getMessage(), 500);
  1795. }
  1796. if (!$validateExistLevel) {
  1797. DB::rollBack();
  1798. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD003: El área no existe.', [], 406);
  1799. }
  1800. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  1801. if ($arrResponseCheckUser['error']) {
  1802. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD004:'.$arrResponseCheckUser['msg'], [], 401);
  1803. }
  1804. $user = $arrResponseCheckUser['response'];
  1805. try {
  1806. $validateExistWarehouse = DB::table('S002V01TALMA')
  1807. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  1808. ->where('ALMA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1809. ->where('ALMA_ESTA', '=', 'Activo')
  1810. ->exists();
  1811. } catch (\Throwable $th) {
  1812. DB::rollBack();
  1813. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD005: Ocurrió un error al verificar el almacen.', $th->getMessage(), 500);
  1814. }
  1815. if (!$validateExistWarehouse) {
  1816. DB::rollBack();
  1817. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD006: El almacen no existe.', [], 406);
  1818. }
  1819. try {
  1820. $validateExistArea = DB::table('S002V01TAREA')
  1821. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  1822. ->where('AREA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1823. ->where('AREA_COAR', '=', $requestData['CODIGO_AREA'])
  1824. ->where('AREA_ESTA', '=', 'Activo')
  1825. ->exists();
  1826. } catch (\Throwable $th) {
  1827. DB::rollBack();
  1828. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD007: Ocurrió un error al verificar el área.', $th->getMessage(), 500);
  1829. }
  1830. if (!$validateExistArea) {
  1831. DB::rollBack();
  1832. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD008: El área no existe.', [], 406);
  1833. }
  1834. try {
  1835. $validateExistNivel = DB::table('S002V01TNIVE')
  1836. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  1837. ->where('NIVE_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1838. ->where('NIVE_COAR', '=', $requestData['CODIGO_AREA'])
  1839. ->where('NIVE_CONI', '=', $requestData['CODIGO_NIVEL'])
  1840. ->where('NIVE_ESTA', '=', 'Activo')
  1841. ->exists();
  1842. } catch (\Throwable $th) {
  1843. DB::rollBack();
  1844. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD009: Ocurrió un error al verificar el nivel.', $th->getMessage(), 500);
  1845. }
  1846. if (!$validateExistNivel) {
  1847. DB::rollBack();
  1848. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD010: El nivel no existe.', [], 406);
  1849. }
  1850. try {
  1851. $validateExistZone = DB::table('S002V01TZONA')
  1852. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  1853. ->where('ZONA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1854. ->where('ZONA_COAR', '=', $requestData['CODIGO_AREA'])
  1855. ->where('ZONA_CONI', '=', $requestData['CODIGO_NIVEL'])
  1856. ->where('ZONA_COZO', '=', $requestData['CODIGO_ZONA'])
  1857. ->exists();
  1858. } catch (\Throwable $th) {
  1859. DB::rollBack();
  1860. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD011: Ocurrió un error al verificar el código de la zona.', $th->getMessage(), 406);
  1861. }
  1862. if ($validateExistZone) {
  1863. DB::rollBack();
  1864. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD012: El código de la zona ya existe.', [], 406);
  1865. }
  1866. $now = $this->functionsController->now();
  1867. $currentDate = $now->toDateTimeString();
  1868. try {
  1869. $validateUpdate = DB::table('S002V01TZONA')
  1870. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  1871. ->where('ZONA_COAL', '=', $requestData['CODIGO_ALMACEN'])
  1872. ->where('ZONA_COAR', '=', $requestData['CODIGO_AREA'])
  1873. ->where('ZONA_CONI', '=', $requestData['CODIGO_NIVEL'])
  1874. ->where('ZONA_COZO', '=', $idZone)
  1875. ->update([
  1876. 'ZONA_NOZO' => $requestData['NOMBRE_ZONA'],
  1877. 'ZONA_COME' => $requestData['COMENTARIOS'],
  1878. 'ZONA_USMO' => $user,
  1879. 'ZONA_FEMO' => $currentDate,
  1880. 'ZONA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  1881. ]);
  1882. } catch (\Throwable $th) {
  1883. DB::rollBack();
  1884. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD013: Ocurrió un error al ingresar la información a la base de datos.', $th->getMessage(), 500);
  1885. }
  1886. if (!$validateUpdate) {
  1887. DB::rollBack();
  1888. return $this->responseController->makeResponse(true, 'ERR_ZONE_UPD014: No se pudo guardar la información en la base de datos.', [], 406);
  1889. }
  1890. DB::commit();
  1891. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  1892. }
  1893. public function deleteZone(Request $request, $idZone) {
  1894. $validator = Validator::make($request->all(), [
  1895. 'USUARIO' => 'required|string',
  1896. 'NUMERO_LINEA' => 'required|string',
  1897. ]);
  1898. if ($validator->fails()) {
  1899. return $this->responseController->makeResponse(
  1900. true,
  1901. "ERR_ZONE_DEL000: Se encontraron uno o más errores.",
  1902. $this->responseController->makeErrors($validator->errors()->messages()),
  1903. 401
  1904. );
  1905. }
  1906. DB::beginTransaction();
  1907. $requestData = $request->all();
  1908. try {
  1909. $idZone = $this->encController->decrypt($idZone);
  1910. } catch (\Throwable $th) {
  1911. DB::rollBack();
  1912. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL001: Ocurrió un error al desencriptar la zona.', $th->getMessage(), 500);
  1913. }
  1914. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  1915. if ($arrResponseCheckUser['error']) {
  1916. DB::rollBack();
  1917. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL002:'.$arrResponseCheckUser['msg'], [], 401);
  1918. }
  1919. $user = $arrResponseCheckUser['response'];
  1920. try {
  1921. $arrZone = (array) DB::table('S002V01TZONA')
  1922. ->where('ZONA_COZO', '=', $idZone)
  1923. ->where('ZONA_ESTA', '=', 'Activo')
  1924. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  1925. ->first([
  1926. 'ZONA_COAL',
  1927. 'ZONA_COAR',
  1928. 'ZONA_CONI',
  1929. ]);
  1930. } catch (\Throwable $th) {
  1931. DB::rollBack();
  1932. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL003: Ocurrió un error al obtener la información de la zona.', $th->getMessage(), 500);
  1933. }
  1934. if (empty($arrZone)) {
  1935. DB::rollBack();
  1936. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL004: La zona no existe.', [], 406);
  1937. }
  1938. try {
  1939. $validateExists = DB::table('S002V01TUBAR')
  1940. ->where('UBAR_COAL', '=', $arrZone['ZONA_COAL'])
  1941. ->where('UBAR_COAR', '=', $arrZone['ZONA_COAR'])
  1942. ->where('UBAR_CONI', '=', $arrZone['ZONA_CONI'])
  1943. ->where('UBAR_COZO', '=', $idZone)
  1944. ->where('UBAR_NULI', '=', $requestData['NUMERO_LINEA'])
  1945. ->where('UBAR_ESTA', '=', 'Activo')
  1946. ->exists();
  1947. } catch (\Throwable $th) {
  1948. DB::rollBack();
  1949. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL005: Ocurrió un error al obtener las ubicaciones de los artículos.', $th->getMessage(), 500);
  1950. }
  1951. if($validateExists) {
  1952. DB::rollBack();
  1953. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL006: No se puede eliminar debido a que existen artículos ubicados en la zona.', [], 406);
  1954. }
  1955. $now = $this->functionsController->now();
  1956. $currentDate = $now->toDateTimeString();
  1957. try {
  1958. $validateUpdate = DB::table('S002V01TZONA')
  1959. ->where('ZONA_COZO', '=', $idZone)
  1960. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  1961. ->update([
  1962. 'ZONA_ESTA' => 'Eliminado',
  1963. 'ZONA_USMO' => $user,
  1964. 'ZONA_FEMO' => $currentDate,
  1965. 'ZONA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  1966. ]);
  1967. } catch (\Throwable $th) {
  1968. DB::rollBack();
  1969. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL007: Ocurrió un error al eliminar la zona.', $th->getMessage(), 500);
  1970. }
  1971. if (!$validateUpdate) {
  1972. DB::rollBack();
  1973. return $this->responseController->makeResponse(true, 'ERR_ZONE_DEL008: No se pudo eliminar la zona.', [], 406);
  1974. }
  1975. DB::commit();
  1976. return $this->responseController->makeResponse(false, "ÉXITO: Eliminación Exitosa");
  1977. }
  1978. // FUNCIÓN RECEPCIÓN DE ARTÍCULOS
  1979. public function registerToStock(Request $request) {
  1980. $validator = Validator::make($request->all(), [
  1981. 'WAREHOUSE' => 'required|string',
  1982. 'AREA' => 'required|string',
  1983. 'LEVEL' => 'required|string',
  1984. 'ZONE' => 'required|string',
  1985. 'ORDER' => 'required|string',
  1986. 'PRE_CODIFICATE' => 'required|array',
  1987. 'ID_INFORMATION' => 'required|integer',
  1988. 'FAMILIA' => 'required|string',
  1989. 'SUBFAMILIA' => 'required|string',
  1990. 'MODELO' => 'required|string',
  1991. 'CODIGO_MODELO' => 'required|string',
  1992. 'NUMBER_ITEMS' => 'required|integer',
  1993. 'CODIGO_BARRAS' => 'required|string',
  1994. 'FECHA_VENCIMIENTO' => 'nullable|string',
  1995. 'IMAGES' => 'required|json',
  1996. 'PRE_GENERATED_CODE' => 'required|string',
  1997. 'REPARABLE' => 'required|boolean',
  1998. 'CONSUMIBLE' => 'required|boolean',
  1999. 'USUARIO' => 'required|string',
  2000. 'NUMERO_LINEA' => 'required|string',
  2001. ]);
  2002. if($validator->fails()){
  2003. return $this->responseController->makeResponse(
  2004. true,
  2005. "Se encontraron uno o más errores.",
  2006. $this->responseController->makeErrors(
  2007. $validator->errors()->messages()
  2008. ),
  2009. 401
  2010. );
  2011. }
  2012. DB::beginTransaction();
  2013. $requestData = $request->all();
  2014. // Se verifica la precodificación
  2015. if ( count($requestData['PRE_CODIFICATE']) !== $requestData['NUMBER_ITEMS']) {
  2016. DB::rollBack();
  2017. return $this->responseController->makeResponse(true, 'La cantidad registrada de la precodificación no es igual la cantidad de artículos seleccionados.', [], 500);
  2018. }
  2019. // Se desencripta y se verifica si el usuario existe
  2020. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  2021. if ($arrResponseCheckUser['error']) {
  2022. DB::rollBack();
  2023. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  2024. }
  2025. $user = $arrResponseCheckUser['response'];
  2026. // Se obtiene la información encriptada
  2027. try {
  2028. $idWarehouse = $this->encController->decrypt($requestData['WAREHOUSE']);
  2029. } catch (\Throwable $th) {
  2030. DB::rollBack();
  2031. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el almacen.', $th->getMessage(), 500);
  2032. }
  2033. if ($idWarehouse === false) {
  2034. DB::rollBack();
  2035. return $this->responseController->makeResponse(true, 'La encriptación del almacen no es correcta.', [], 500);
  2036. }
  2037. try {
  2038. $idArea = $this->encController->decrypt($requestData['AREA']);
  2039. } catch (\Throwable $th) {
  2040. DB::rollBack();
  2041. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el área.', $th->getMessage(), 500);
  2042. }
  2043. if ($idArea === false) {
  2044. DB::rollBack();
  2045. return $this->responseController->makeResponse(true, 'La encriptacipon del área no es correcta.', [], 500);
  2046. }
  2047. try {
  2048. $idLevel = $this->encController->decrypt($requestData['LEVEL']);
  2049. } catch (\Throwable $th) {
  2050. DB::rollBack();
  2051. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener el nivel.', $th->getMessage(), 500);
  2052. }
  2053. if ($idLevel === false) {
  2054. DB::rollBack();
  2055. return $this->responseController->makeResponse(true, 'La encriptación del nivel no es correcta.', [], 500);
  2056. }
  2057. try {
  2058. $idZone = $this->encController->decrypt($requestData['ZONE']);
  2059. } catch (\Throwable $th) {
  2060. DB::rollBack();
  2061. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la zona.', $th->getMessage(), 500);
  2062. }
  2063. if ($idZone === false) {
  2064. DB::rollBack();
  2065. return $this->responseController->makeResponse(true, 'La encriptación de la zona no es correcta.', [], 500);
  2066. }
  2067. try {
  2068. $idFamily = $this->encController->decrypt($requestData['FAMILIA']);
  2069. } catch (\Throwable $th) {
  2070. DB::rollBack();
  2071. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la familia.', $th->getMessage(), 500);
  2072. }
  2073. if ($idFamily === false) {
  2074. DB::rollBack();
  2075. return $this->responseController->makeResponse(true, 'La encriptación de la familia no es correcta.', [], 500);
  2076. }
  2077. try {
  2078. $idSubfamily = $this->encController->decrypt($requestData['SUBFAMILIA']);
  2079. } catch (\Throwable $th) {
  2080. DB::rollBack();
  2081. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la subfamilia.', $th->getMessage(), 500);
  2082. }
  2083. if ($idSubfamily === false) {
  2084. DB::rollBack();
  2085. return $this->responseController->makeResponse(true, 'La encriptación de la subfamilia no es correcta.', [], 500);
  2086. }
  2087. // Se verifica que la información exista
  2088. try {
  2089. $validateWarehouse = DB::table('S002V01TALMA')
  2090. ->where('ALMA_COAL', '=', $idWarehouse)
  2091. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  2092. ->exists();
  2093. } catch (\Throwable $th) {
  2094. DB::rollBack();
  2095. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si el almacen existe.', $th->getMessage(), 500);
  2096. }
  2097. if ($validateWarehouse === false) {
  2098. DB::rollBack();
  2099. return $this->responseController->makeResponse(true, 'El almacen no existe.', [], 500);
  2100. }
  2101. try {
  2102. $validateArea = DB::table('S002V01TAREA')
  2103. ->where('AREA_COAL', '=', $idWarehouse)
  2104. ->where('AREA_COAR', '=', $idArea)
  2105. ->where('AREA_NULI', '=', $requestData['NUMERO_LINEA'])
  2106. ->exists();
  2107. } catch (\Throwable $th) {
  2108. DB::rollBack();
  2109. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si el área existe.', $th->getMessage(), 500);
  2110. }
  2111. if ($validateArea === false) {
  2112. DB::rollBack();
  2113. return $this->responseController->makeResponse(true, 'El área no existe.', [], 500);
  2114. }
  2115. try {
  2116. $validateLevel = DB::table('S002V01TNIVE')
  2117. ->where('NIVE_COAL', '=', $idWarehouse)
  2118. ->where('NIVE_COAR', '=', $idArea)
  2119. ->where('NIVE_CONI', '=', $idLevel)
  2120. ->where('NIVE_NULI', '=', $requestData['NUMERO_LINEA'])
  2121. ->exists();
  2122. } catch (\Throwable $th) {
  2123. DB::rollBack();
  2124. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si el nivel existe.', $th->getMessage(), 500);
  2125. }
  2126. if ($validateLevel === false) {
  2127. DB::rollBack();
  2128. return $this->responseController->makeResponse(true, 'El nivel no existe.', [], 500);
  2129. }
  2130. try {
  2131. $validateZone = DB::table('S002V01TZONA')
  2132. ->where('ZONA_COAL', '=', $idWarehouse)
  2133. ->where('ZONA_COAR', '=', $idArea)
  2134. ->where('ZONA_CONI', '=', $idLevel)
  2135. ->where('ZONA_COZO', '=', $idZone)
  2136. ->where('ZONA_NULI', '=', $requestData['NUMERO_LINEA'])
  2137. ->exists();
  2138. } catch (\Throwable $th) {
  2139. DB::rollBack();
  2140. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si la zona existe.', $th->getMessage(), 500);
  2141. }
  2142. if ($validateZone === false) {
  2143. return $this->responseController->makeResponse(true, 'La zona no existe.', [], 500);
  2144. }
  2145. try {
  2146. $validateFamily = DB::table('S002V01TFAMI')
  2147. ->where('FAMI_COFA', '=', $idFamily)
  2148. ->where('FAMI_NULI', '=', $requestData['NUMERO_LINEA'])
  2149. ->exists();
  2150. } catch (\Throwable $th) {
  2151. DB::rollBack();
  2152. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si la familia existe.', $th->getMessage(), 500);
  2153. }
  2154. if ($validateFamily === false) {
  2155. DB::rollBack();
  2156. return $this->responseController->makeResponse(true, 'La familia no existe.', [], 500);
  2157. }
  2158. try {
  2159. $validateSubfamily = DB::table('S002V01TSUBF')
  2160. ->where('SUBF_COFA', '=', $idFamily)
  2161. ->where('SUBF_COSU', '=', $idSubfamily)
  2162. ->where('SUBF_NULI', '=', $requestData['NUMERO_LINEA'])
  2163. ->exists();
  2164. } catch (\Throwable $th) {
  2165. DB::rollBack();
  2166. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar si la subfamilia existe.', $th->getMessage(), 500);
  2167. }
  2168. if ($validateSubfamily === false) {
  2169. DB::rollBack();
  2170. return $this->responseController->makeResponse(true, 'La subfamilia no existe.', [], 500);
  2171. }
  2172. try {
  2173. $orden = (array) DB::table('S002V01TORCO')
  2174. ->where('ORCO_NUOR', '=', $requestData['ORDER'])
  2175. ->where('ORCO_NULI', '=', $requestData['NUMERO_LINEA'])
  2176. ->where('ORCO_ESTA', '=', 'Recibido')
  2177. ->first([
  2178. 'ORCO_IDLI AS ID_LINEA_SOLICITUD',
  2179. 'ORCO_IDDE AS ID_DESCRIPCION',
  2180. ]);
  2181. } catch (\Throwable $th) {
  2182. DB::rollBack();
  2183. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información de la orden de compra.', $th->getMessage(), 500);
  2184. }
  2185. if (is_null($orden)) {
  2186. DB::rollBack();
  2187. return $this->responseController->makeResponse(true, 'La orden de compra no existe.', [], 500);
  2188. }
  2189. $now = $this->functionsController->now();
  2190. $currentDate = $now->toDateTimeString();
  2191. try {
  2192. $info = DB::table('S002V01TARSE')
  2193. ->where('ARSE_IDIN', '=', $requestData['ID_INFORMATION'])
  2194. ->where('ARSE_ESTA', '=', 'Activo')
  2195. ->where('ARSE_IDLI', '=', $orden['ID_LINEA_SOLICITUD'])
  2196. ->where('ARSE_NULI', '=', $requestData['NUMERO_LINEA'])
  2197. ->where('ARTI_NULI', '=', $requestData['NUMERO_LINEA'])
  2198. ->where('DEAR_NULI', '=', $requestData['NUMERO_LINEA'])
  2199. ->where('UNID_NULI', '=', $requestData['NUMERO_LINEA'])
  2200. ->where('INAR_NULI', '=', $requestData['NUMERO_LINEA'])
  2201. ->where('ARSE_ESTA', '=', 'Activo')
  2202. ->where('ARTI_ESTA', '=', 'Activo')
  2203. ->where('DEAR_ESTA', '=', 'Activo')
  2204. ->where('UNID_ESTA', '=', 'Activo')
  2205. ->where('INAR_ESTA', '=', 'Activo')
  2206. ->where('INAR_CODI', '=', $requestData['CODIGO_MODELO'])
  2207. ->where('INAR_MODE', '=', $requestData['MODELO'])
  2208. ->join('S002V01TARTI', 'ARTI_IDAR', '=', 'ARSE_IDAR')
  2209. ->join('S002V01TINAR', 'INAR_IDIN', '=', 'ARSE_IDIN')
  2210. ->join('S002V01TDEAR', 'DEAR_IDDE', '=', 'INAR_IDDE')
  2211. ->join('S002V01TUNID', 'UNID_IDUN', '=', 'DEAR_IDUN')
  2212. ->limit($requestData['NUMBER_ITEMS'])
  2213. ->get([
  2214. 'ARSE_IDAS AS NUMERO_SELECCIONADO',
  2215. 'ARTI_IDAR AS ID_ARTICULO',
  2216. 'ARTI_COFA AS CODIGO_FAMILIA',
  2217. 'ARTI_COSU AS CODIGO_SUBFAMILIA',
  2218. 'ARTI_CODI AS CODIGO_ARTICULO',
  2219. 'ARTI_NOMB AS NOMBRE_ARTICULO',
  2220. 'DEAR_IDDE AS ID_DESCRIPCION',
  2221. 'DEAR_IMAG AS IMAGENES',
  2222. 'DEAR_DESC AS DESCRIPCION',
  2223. 'DEAR_CARA AS CARACTERISTICAS',
  2224. 'DEAR_COWE AS COMPRA_WEB',
  2225. 'DEAR_NUPR AS NUMERO_PROVEEDOR',
  2226. 'UNID_IDUN AS ID_UNIDAD',
  2227. 'UNID_NOMB AS NOMBRE_UNIDAD',
  2228. 'UNID_ACRO AS ACRONIMO_UNIDAD',
  2229. 'INAR_IDIN AS ID_INFORMACION',
  2230. 'INAR_CODI AS CODIGO_INFORMACION',
  2231. 'INAR_MODE AS MODELO_INFORMACION',
  2232. 'INAR_COMO AS CODIGO_MONEDA',
  2233. 'INAR_PREC AS PRECIO',
  2234. 'INAR_MOMI AS MONTO_MINIMO',
  2235. 'INAR_CARA AS CARACTERISTICAS',
  2236. ]);
  2237. $info = json_decode(json_encode($info ), true);
  2238. } catch (\Throwable $th) {
  2239. DB::rollBack();
  2240. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información de los productos.', $th->getMessage(), 500);
  2241. }
  2242. if ( is_null($info) || empty($info) ) {
  2243. DB::rollBack();
  2244. return $this->responseController->makeResponse(true, 'No existen artículos asociados a la orden de compra.', [], 500);
  2245. }
  2246. // Se verifica que existan los datos suficientes para hacer el traslado.
  2247. if ( count($info) !== $requestData['NUMBER_ITEMS']) {
  2248. DB::rollBack();
  2249. 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);
  2250. }
  2251. // Se verifica los códigos de barras
  2252. $uniqueBarcode = false;
  2253. $arrBarcode = explode(',', $requestData['CODIGO_BARRAS']);
  2254. if ( count($arrBarcode) === 0) {
  2255. DB::rollBack();
  2256. return $this->responseController->makeResponse(true, 'No hay códigos de barras registrados.', [], 500);
  2257. } else if (count($arrBarcode) === $requestData['NUMBER_ITEMS']) {
  2258. $uniqueBarcode = false;
  2259. } else if (array_key_exists(0, $arrBarcode) && count($arrBarcode) === 1 && $arrBarcode[0] !== '' ) {
  2260. $uniqueBarcode = true;
  2261. } else {
  2262. DB::rollBack();
  2263. return $this->responseController->makeResponse(true, 'Ocurrió un error con los códigos de barras registrados.', [], 500);
  2264. }
  2265. // Se verifica que los códigos de barras del formulario no sean repetidos
  2266. $temp_array = array_unique($arrBarcode);
  2267. $duplicates = sizeof($temp_array) != sizeof($arrBarcode);
  2268. if ($duplicates) {
  2269. return $this->responseController->makeResponse(true, 'Los códigos de barras no se pueden repetir.', [], 400);
  2270. }
  2271. // Se verifican que los códigos de barras no se repitan
  2272. foreach ($arrBarcode as $key => $barcode) {
  2273. try {
  2274. $validateBarcode = DB::table('S002V01TSTAR')
  2275. ->where('STAR_COBA', '=', $barcode)
  2276. ->where('STAR_NULI', '=', $requestData['NUMERO_LINEA'])
  2277. ->exists();
  2278. } catch (\Throwable $th) {
  2279. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar los códigos de barras.', $th->getMessage(), 500);
  2280. }
  2281. if ($validateBarcode === true) {
  2282. return $this->responseController->makeResponse(true, 'El código '.$barcode.' ya se encuentra registrado', [], 500);
  2283. }
  2284. }
  2285. // Se verifica la fecha de vencimientos
  2286. $arrDueDate = array();
  2287. $uniqueDueDate = false;
  2288. if ($requestData['FECHA_VENCIMIENTO'] !== '' && !is_null($requestData['FECHA_VENCIMIENTO'])) {
  2289. $arrDueDate = explode(',', $requestData['FECHA_VENCIMIENTO']);
  2290. if ( count($arrDueDate) === 0) {
  2291. DB::rollBack();
  2292. return $this->responseController->makeResponse(true, 'No hay fechas de vencimientos registradas.', [], 500);
  2293. } else if (count($arrDueDate) === $requestData['NUMBER_ITEMS']) {
  2294. $uniqueDueDate = false;
  2295. } else if (array_key_exists(0, $arrDueDate) && count($arrDueDate) === 1 && $arrDueDate[0] !== '' ) {
  2296. $uniqueDueDate = true;
  2297. } else {
  2298. DB::rollBack();
  2299. return $this->responseController->makeResponse(true, 'Ocurrió un error con las fechas de vencimientos registrados.', [], 500);
  2300. }
  2301. }
  2302. // Se obtiene la fecha actual
  2303. $now = $this->functionsController->now();
  2304. $currentDate = $now->toDateTimeString();
  2305. $idInfoStock = null;
  2306. try {
  2307. $infoStock = (array) DB::table('S002V01TINST')
  2308. ->where('INST_COFA', '=', $idFamily)
  2309. ->where('INST_COSU', '=', $idSubfamily)
  2310. ->where('INST_ARTI', '=', $info[0]['ID_ARTICULO'])
  2311. ->where('INST_MODE', '=', $requestData['MODELO'])
  2312. ->where('INST_COMO', '=', $requestData['CODIGO_MODELO'])
  2313. ->where('INST_NULI', '=', $requestData['NUMERO_LINEA'])
  2314. ->where('INST_ESTA', '=', 'Activo')
  2315. ->first([
  2316. 'INST_IDIS AS ID_INFO_STOCK'
  2317. ]);
  2318. } catch (\Throwable $th) {
  2319. DB::rollBack();
  2320. return $this->responseController->makeResponse(true, 'Ocurrió un error al verificar la información del stock.', $th->getMessage(), 500);
  2321. }
  2322. if (empty($infoStock) || is_null($infoStock)) {
  2323. // Se obtiene el arreglo de las imagenes
  2324. $imagesArr = json_decode($requestData['IMAGES'], true);
  2325. // Se verifica que el arreglo contenga imagenes
  2326. if(count($imagesArr) < 1){
  2327. DB::rollBack();
  2328. return $this->responseController->makeResponse(true, 'El arreglo de la galería de imágenes está vacío.', [], 400);
  2329. }
  2330. $imagesGallery = [];
  2331. // Se iteran las imagenes
  2332. foreach($imagesArr as $imageFile){
  2333. // Se verifica que tenga un formato correcto
  2334. if(!array_key_exists('type', $imageFile)){
  2335. DB::rollBack();
  2336. return $this->responseController->makeResponse(true, 'El arreglo de la galería de imágenes tiene un formato inválido.', [], 400);
  2337. // Si la imagen es nueva, entonces...
  2338. }else if($imageFile['type'] == 'Nuevo'){
  2339. // Se obtiene el ID de la imagen
  2340. $tempFileID = $this->encController->decrypt($imageFile['id']);
  2341. // Se obtiene la información registrada en la tabla de archivos temporales
  2342. $tempFile = DB::table('S002V01TARTE')->where([
  2343. ['ARTE_IDAR', '=', $tempFileID],
  2344. ['ARTE_NULI', '=', $requestData['NUMERO_LINEA']]
  2345. ])->first();
  2346. // La imagen es colocada en su posición final
  2347. $finalFile = $this->documentManagementController->moveFinalFile($requestData['NUMERO_LINEA'], 'GEEQ', 'FO', $tempFile, $user);
  2348. // Si ocurrió un error, entonces se manda el error
  2349. if(!$finalFile[0]){
  2350. return $this->responseController->makeResponse(true, $finalFile[1], [], 400);
  2351. }
  2352. // La información es guardada en un arreglo
  2353. $imagesGallery[] = $finalFile[1];
  2354. }else{
  2355. // Se desencripta el id de la imagen
  2356. $fileID = $this->encController->decrypt($imageFile['id']);
  2357. // Es guardada en un arreglo
  2358. $imagesGallery[] = $fileID;
  2359. }
  2360. }
  2361. // El arreglo de las imagenes es pasada en formato string
  2362. $imagesGalleryStr = json_encode($imagesGallery);
  2363. try {
  2364. $idInsert = DB::table('S002V01TINST')->insertGetId([
  2365. 'INST_NULI' => $requestData['NUMERO_LINEA'],
  2366. 'INST_COFA' => $idFamily,
  2367. 'INST_COSU' => $idSubfamily,
  2368. 'INST_ARTI' => $info[0]['ID_ARTICULO'],
  2369. 'INST_MODE' => $requestData['MODELO'],
  2370. 'INST_COMO' => $requestData['CODIGO_MODELO'],
  2371. 'INST_IMAG' => $imagesGalleryStr,
  2372. 'INST_USRE' => $user,
  2373. 'INST_FERE' => $currentDate,
  2374. 'INST_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2375. ]);
  2376. } catch (\Throwable $th) {
  2377. DB::rollBack();
  2378. return $this->responseController->makeResponse(true, 'Ocurrió un error al insertar la información del stock.', $th->getMessage(), 500);
  2379. }
  2380. if ($idInsert === false || is_null($idInsert)) {
  2381. DB::rollBack();
  2382. return $this->responseController->makeResponse(true, 'No se pudo insertar la información del stock.', [], 500);
  2383. }
  2384. $idInfoStock = $idInsert;
  2385. } else {
  2386. $idInfoStock = $infoStock['ID_INFO_STOCK'];
  2387. }
  2388. if (is_null($idInfoStock)) {
  2389. DB::rollBack();
  2390. return $this->responseController->makeResponse(true, 'No se pudo obtener el ID de la información del Stock.', [], 500);
  2391. }
  2392. for ($i=0; $i < $requestData['NUMBER_ITEMS']; $i++) {
  2393. $dueDate = null;
  2394. if (count($arrDueDate) > 0 && $uniqueDueDate === true) {
  2395. $dueDate = $arrDueDate[0];
  2396. } else if (count($arrDueDate) > 0 && $uniqueDueDate === false) {
  2397. $dueDate = $arrDueDate[$i];
  2398. }
  2399. if (!is_null($dueDate) && str_contains($dueDate, 'T')) {
  2400. $dueDate = explode('T', $dueDate)[0];
  2401. }
  2402. try {
  2403. $preCodificate = $this->encController->decrypt($requestData['PRE_CODIFICATE'][$i]);
  2404. } catch (\Throwable $th) {
  2405. DB::rollBack();
  2406. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el identificador de la pre-codificación.', $th->getMessage(), 500);
  2407. }
  2408. if ($preCodificate === false) {
  2409. DB::rollBack();
  2410. return $this->responseController->makeResponse(true, 'No se pudo desencriptar el identificador de la pre-codificación.', [], 500);
  2411. }
  2412. try {
  2413. $idInsertStAr = DB::table('S002V01TSTAR')->insertGetId([
  2414. 'STAR_NULI' => $requestData['NUMERO_LINEA'],
  2415. 'STAR_IDIS' => $idInfoStock,
  2416. 'STAR_COBA' => $uniqueBarcode === true ? $arrBarcode[0] : $arrBarcode[$i],
  2417. 'STAR_FEVE' => $dueDate,
  2418. 'STAR_IDUN' => $info[$i]['ID_UNIDAD'],
  2419. 'STAR_NUPR' => $info[$i]['NUMERO_PROVEEDOR'],
  2420. 'STAR_TIAD' => 'Por pedido',
  2421. 'STAR_IDPC' => $preCodificate,
  2422. 'STAR_CONS' => $requestData['CONSUMIBLE'] ? 'Si' : 'No',
  2423. 'STAR_REPA' => $requestData['REPARABLE'] ? 'Si' : 'No',
  2424. 'STAR_ESTA' => 'Pendiente',
  2425. 'STAR_USRE' => $user,
  2426. 'STAR_FERE' => $currentDate,
  2427. 'STAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2428. ]);
  2429. } catch (\Throwable $th) {
  2430. DB::rollBack();
  2431. return $this->responseController->makeResponse(true, 'Ocurrió un error al registrar el equipamiento.', $th->getMessage(), 500);
  2432. }
  2433. if ($idInsertStAr === false || is_null($idInsertStAr)) {
  2434. DB::rollBack();
  2435. return $this->responseController->makeResponse(true, 'No se pudo registrar el equipamiento.', [], 500);
  2436. }
  2437. try {
  2438. $preCodeCodificate = $this->encController->decrypt($requestData['PRE_GENERATED_CODE']);
  2439. } catch (\Throwable $th) {
  2440. DB::rollBack();
  2441. return $this->responseController->makeResponse(true, 'Ocurrió un error al desencriptar el pre-código generado', $th->getMessage(), 500);
  2442. }
  2443. if ( $preCodeCodificate === false ) {
  2444. DB::rollBack();
  2445. return $this->responseController->makeResponse(true, 'No se pudo desencriptar el pre-código del equipamientos.', [], 500);
  2446. }
  2447. try {
  2448. $validateInsertUbAr = DB::table('S002V01TUBAR')->insert([
  2449. 'UBAR_NULI' => $requestData['NUMERO_LINEA'],
  2450. 'UBAR_COAL' => $idWarehouse,
  2451. 'UBAR_COAR' => $idArea,
  2452. 'UBAR_CONI' => $idLevel,
  2453. 'UBAR_COZO' => $idZone,
  2454. 'UBAR_IDST' => $idInsertStAr,
  2455. 'UBAR_COUB' => $preCodeCodificate,
  2456. 'UBAR_ESTA' => 'Pendiente',
  2457. 'UBAR_USRE' => $user,
  2458. 'UBAR_FERE' => $currentDate,
  2459. 'UBAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2460. ]);
  2461. } catch (\Throwable $th) {
  2462. DB::rollBack();
  2463. return $this->responseController->makeResponse(true, 'Ocurrió un error al registrar la ubicación del artículo.', $th->getMessage(), 500);
  2464. }
  2465. if ( $validateInsertUbAr === false ) {
  2466. DB::rollBack();
  2467. return $this->responseController->makeResponse(true, 'No se pudo registrar la ubicación del artículo.', [], 500);
  2468. }
  2469. try {
  2470. $validateUpdateArSe = DB::table('S002V01TARSE')
  2471. ->where('ARSE_IDAS', '=', $info[$i]['NUMERO_SELECCIONADO'])
  2472. ->where('ARSE_NULI', '=', $requestData['NUMERO_LINEA'])
  2473. ->where('ARSE_IDLI', '=', $orden['ID_LINEA_SOLICITUD'])
  2474. ->where('ARSE_IDAR', '=', $info[$i]['ID_ARTICULO'])
  2475. ->where('ARSE_NUPR', '=', $info[$i]['NUMERO_PROVEEDOR'])
  2476. ->where('ARSE_IDIN', '=', $requestData['ID_INFORMATION'])
  2477. ->where('ARSE_ESTA', '=', 'Activo')
  2478. ->update([
  2479. 'ARSE_ESTA' => 'Guardado',
  2480. 'ARSE_USMO' => $user,
  2481. 'ARSE_FEMO' => $currentDate,
  2482. 'ARSE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  2483. ]);
  2484. } catch (\Throwable $th) {
  2485. DB::rollBack();
  2486. return $this->responseController->makeResponse(true, 'Ocurrió un error al modificar el artículo seleccionado.', $th->getMessage(), 500);
  2487. }
  2488. if ($validateUpdateArSe === false) {
  2489. DB::rollBack();
  2490. return $this->responseController->makeResponse(true, 'No se pudo modificar el artículo seleccionado.', [], 500);
  2491. }
  2492. }
  2493. DB::commit();
  2494. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  2495. }
  2496. // FUNCIÓN INTERCAMBIO DE ARTICULOS ENTRE ALMACENES
  2497. public function getCurrentLocationArtitles($user, $line) {
  2498. try {
  2499. $arrStockArtitle = DB::table('S002V01TSTAR')
  2500. ->where('STAR_NULI', '=', $line)
  2501. ->where('STAR_ESTA', '=', 'Activo')
  2502. ->where('INST_NULI', '=', $line)
  2503. ->where('INST_ESTA', '=', 'Activo')
  2504. ->where('UBAR_ESTA', '=', 'Activo')
  2505. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  2506. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  2507. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'INST_COFA')
  2508. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'INST_COSU')
  2509. ->join('S002V01TALMA', 'ALMA_COAL', '=', 'UBAR_COAL')
  2510. ->join('S002V01TAREA', 'AREA_COAR', '=', 'UBAR_COAR')
  2511. ->join('S002V01TNIVE', 'NIVE_CONI', '=', 'UBAR_CONI')
  2512. ->join('S002V01TZONA', 'ZONA_COZO', '=', 'UBAR_COZO')
  2513. ->get([
  2514. 'STAR_IDST AS ID_STOCK',
  2515. 'INST_IDIS AS ID_INFORMACION_STOCK',
  2516. 'FAMI_NOFA AS NOMBRE_FAMILIA',
  2517. 'SUBF_NOSU AS NOMBRE_SUBFAMILIA',
  2518. 'INST_ARTI AS ARTITULO',
  2519. 'INST_MODE AS MODELO',
  2520. 'INST_COMO AS CODIGO_MODELO',
  2521. 'INST_IMAG AS IMAGENES',
  2522. 'UBAR_COUB AS CODIGO',
  2523. DB::raw('CONCAT(ALMA_NOAL, " (", ALMA_COAL, ")") AS NOMBRE_ALMACEN'),
  2524. DB::raw('CONCAT(AREA_NOAR, " (", AREA_COAR, ")") AS NOMBRE_AREA'),
  2525. DB::raw('CONCAT(NIVE_NONI, " (", NIVE_CONI, ")") AS NOMBRE_NIVEL'),
  2526. DB::raw('CONCAT(ZONA_NOZO, " (", ZONA_COZO, ")") AS NOMBRE_ZONA'),
  2527. ]);
  2528. $arrStockArtitle = json_decode(json_encode($arrStockArtitle), true);
  2529. } catch (\Throwable $th) {
  2530. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  2531. }
  2532. foreach ($arrStockArtitle as $key => $stockArtitle) {
  2533. $imagesGallery = [];
  2534. $arrImages = json_decode($stockArtitle['IMAGENES']);
  2535. foreach($arrImages as $image) {
  2536. $imageCodeEnc = $this->encController->encrypt($image);
  2537. $response = $this->documentManagementController->privateGetPublicDocumentURL(
  2538. $imageCodeEnc,
  2539. $user,
  2540. $line
  2541. );
  2542. if($response['error']){
  2543. return $this->responseController->makeresponse(true, $response['msg'], [], 500);
  2544. }
  2545. $imagesGallery[] = $response['response']['public_uri'];
  2546. }
  2547. $stockArtitle['IMAGENES'] = $imagesGallery;
  2548. $arrStockArtitle[$key] = $stockArtitle;
  2549. }
  2550. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrStockArtitle);
  2551. }
  2552. // FUNCIÓN PARA JOSÉ LUIS
  2553. public function getInfoStock($user, $line) {
  2554. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  2555. if ($arrResponseCheckUser['error']) {
  2556. return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
  2557. }
  2558. try {
  2559. $arrStockArtitle = DB::table('S002V01TSTAR')
  2560. ->where('STAR_NULI', '=', $line)
  2561. ->where('STAR_ESTA', '=', 'Activo')
  2562. ->where('INST_NULI', '=', $line)
  2563. ->where('INST_ESTA', '=', 'Activo')
  2564. ->where('UBAR_ESTA', '=', 'Activo')
  2565. ->join('S002V01TUBAR', 'UBAR_IDST', '=', 'STAR_IDST')
  2566. ->join('S002V01TINST', 'INST_IDIS', '=', 'STAR_IDIS')
  2567. ->get([
  2568. 'STAR_IDST AS ID_STOCK',
  2569. 'INST_MODE AS MODELO',
  2570. 'INST_COMO AS CODIGO_MODELO',
  2571. 'UBAR_COUB AS CODIGO_EQUIPAMIENTO',
  2572. 'STAR_CONS AS CONSUMIBLE',
  2573. 'STAR_REPA AS REPARABLE',
  2574. ]);
  2575. } catch (\Throwable $th) {
  2576. return $this->responseController->makeResponse(true, 'Ocurrió un error al obtener la información del stock.', $th->getMessage(), 500);
  2577. }
  2578. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrStockArtitle);
  2579. }
  2580. }