StockController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. public function getWarehouse( $user, $line ) {
  33. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  34. if ($arrResponseCheckUser['error']) {
  35. DB::rollBack();
  36. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_GETBY000:'.$arrResponseCheckUser['msg'], [], 401);
  37. }
  38. try {
  39. $arrWarehouse = DB::table('S002V01TALMA')
  40. ->where('ALMA_NULI', '=', $line)
  41. ->get([
  42. 'ALMA_IDAL AS ID_ALMACEN',
  43. 'ALMA_COAL AS CODIGO_ALMACEN',
  44. 'ALMA_COAL AS NOMBRE_ALMACEN',
  45. 'ALMA_NORE AS NOMBRE_RESPONSABLE',
  46. 'ALMA_APRE AS APELLIDO_PATERNO_RESPONSABLE',
  47. 'ALMA_AMRE AS APELLIDO_MATERNO_RESPONSABLE',
  48. 'ALMA_COR1 AS CORREO1',
  49. 'ALMA_COR2 AS CORREO2',
  50. 'ALMA_LAD1 AS LADA1',
  51. 'ALMA_TEL1 AS TELEFONO1',
  52. 'ALMA_LAD2 AS LADA2',
  53. 'ALMA_TEL2 AS TELEFONO2',
  54. 'ALMA_CALL AS CALLE',
  55. 'ALMA_NUEX AS NUMERO_EXTERIOR',
  56. 'ALMA_NUIN AS NUMERO_INTERIOR',
  57. 'ALMA_COPO AS CODIGO_POSTAL',
  58. 'ALMA_COLO AS COLONIA',
  59. 'ALMA_LOCA AS LOCALIDAD',
  60. 'ALMA_MUNI AS MUNICIPIO',
  61. 'ALMA_ENTI AS ENTIDAD_FEDERATIVA',
  62. 'ALMA_PAIS AS PAIS',
  63. 'ALMA_ESTA AS ESTADO',
  64. 'ALMA_USRE AS USUARIO_REGISTRA',
  65. 'ALMA_FERE AS FECHA_REGISTRA',
  66. 'ALMA_USMO AS USUARIO_MODIFICA',
  67. 'ALMA_FEMO AS FECHA_MODIFICA',
  68. ]);
  69. $arrWarehouse = json_decode( json_encode($arrWarehouse), true );
  70. } catch (\Throwable $th) {
  71. DB::rollBack();
  72. return $this->responseController->makeResponse(
  73. true,
  74. "ERR_WAREHOUSE_GET001: Ocurrió un error al obtener los almacenes.",
  75. $th->getMessage(),
  76. 500
  77. );
  78. }
  79. foreach ($arrWarehouse as $keyWarehouse => $warehouse) {
  80. $arrResponseGetAddress = $this->resourcesController->getAddress(
  81. $warehouse['CODIGO_POSTAL'],
  82. $warehouse['COLONIA'],
  83. $warehouse['MUNICIPIO'],
  84. $warehouse['LOCALIDAD'],
  85. $warehouse['ENTIDAD_FEDERATIVA'],
  86. $warehouse['PAIS'],
  87. $line
  88. );
  89. if ($arrResponseGetAddress['error']) {
  90. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_GET002: '.$arrResponseGetAddress['msg'], $arrResponseGetAddress['response'], 401);
  91. }
  92. $warehouse['CODIGO_POSTAL'] = $arrResponseGetAddress['response']['CODIGO_POSTAL'];
  93. $warehouse['COLONIA'] = $arrResponseGetAddress['response']['COLONIA'];
  94. $warehouse['MUNICIPIO'] = $arrResponseGetAddress['response']['MUNICIPIO'];
  95. $warehouse['LOCALIDAD'] = $arrResponseGetAddress['response']['LOCALIDAD'];
  96. $warehouse['ENTIDAD_FEDERATIVA'] = $arrResponseGetAddress['response']['ENTIDAD_FEDERATIVA'];
  97. $warehouse['PAIS'] = $arrResponseGetAddress['response']['PAIS'];
  98. $arrWarehouse[$keyWarehouse] = $warehouse;
  99. }
  100. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrWarehouse);
  101. }
  102. public function getWarehouseById( $idWarehouse, $user, $line ) {
  103. try {
  104. $idWarehouse = $this->encController->decrypt($idWarehouse);
  105. } catch (\Throwable $th) {
  106. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_GETBY000: Ocurrió un error al desencriptar el ID del almacen', $th->getMessage(), 406);
  107. }
  108. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  109. if ($arrResponseCheckUser['error']) {
  110. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_GETBY000:'.$arrResponseCheckUser['msg'], [], 401);
  111. }
  112. try {
  113. $arrWarehouse = (array) DB::table('S002V01TALMA')
  114. ->where('ALMA_IDAL', '=', $idWarehouse)
  115. ->where('ALMA_NULI', '=', $line)
  116. ->first([
  117. 'ALMA_COAL AS CODIGO_ALMACEN',
  118. 'ALMA_COAL AS NOMBRE_ALMACEN',
  119. 'ALMA_NORE AS NOMBRE_RESPONSABLE',
  120. 'ALMA_APRE AS APELLIDO_PATERNO_RESPONSABLE',
  121. 'ALMA_AMRE AS APELLIDO_MATERNO_RESPONSABLE',
  122. 'ALMA_COR1 AS CORREO1',
  123. 'ALMA_COR2 AS CORREO2',
  124. 'ALMA_LAD1 AS LADA1',
  125. 'ALMA_TEL1 AS TELEFONO1',
  126. 'ALMA_LAD2 AS LADA2',
  127. 'ALMA_TEL2 AS TELEFONO2',
  128. 'ALMA_CALL AS CALLE',
  129. 'ALMA_NUEX AS NUMERO_EXTERIOR',
  130. 'ALMA_NUIN AS NUMERO_INTERIOR',
  131. 'ALMA_COPO AS CODIGO_POSTAL',
  132. 'ALMA_COLO AS COLONIA',
  133. 'ALMA_LOCA AS LOCALIDAD',
  134. 'ALMA_MUNI AS MUNICIPIO',
  135. 'ALMA_ENTI AS ENTIDAD_FEDERATIVA',
  136. 'ALMA_PAIS AS PAIS',
  137. 'ALMA_ESTA AS ESTADO',
  138. 'ALMA_USRE AS USUARIO_REGISTRA',
  139. 'ALMA_FERE AS FECHA_REGISTRA',
  140. 'ALMA_USMO AS USUARIO_MODIFICA',
  141. 'ALMA_FEMO AS FECHA_MODIFICA',
  142. ]);
  143. $arrWarehouse = json_decode( json_encode($arrWarehouse), true );
  144. } catch (\Throwable $th) {
  145. DB::rollBack();
  146. return $this->responseController->makeResponse(
  147. true,
  148. "ERR_WAREHOUSE_GETBY001: Ocurrió un error al obtener los almacenes.",
  149. $th->getMessage(),
  150. 500
  151. );
  152. }
  153. if (!empty($arrWarehouse)) {
  154. $arrResponseGetAddress = $this->resourcesController->getAddress(
  155. $arrWarehouse['CODIGO_POSTAL'],
  156. $arrWarehouse['COLONIA'],
  157. $arrWarehouse['MUNICIPIO'],
  158. $arrWarehouse['LOCALIDAD'],
  159. $arrWarehouse['ENTIDAD_FEDERATIVA'],
  160. $arrWarehouse['PAIS'],
  161. $line
  162. );
  163. if ($arrResponseGetAddress['error']) {
  164. return $this->responseController->makeResponse(true, 'ERR_arrWarehouse_GETBY002: '.$arrResponseGetAddress['msg'], $arrResponseGetAddress['response'], 406);
  165. }
  166. $arrWarehouse['CODIGO_POSTAL'] = $arrResponseGetAddress['response']['CODIGO_POSTAL'];
  167. $arrWarehouse['COLONIA'] = $arrResponseGetAddress['response']['COLONIA'];
  168. $arrWarehouse['MUNICIPIO'] = $arrResponseGetAddress['response']['MUNICIPIO'];
  169. $arrWarehouse['LOCALIDAD'] = $arrResponseGetAddress['response']['LOCALIDAD'];
  170. $arrWarehouse['ENTIDAD_FEDERATIVA'] = $arrResponseGetAddress['response']['ENTIDAD_FEDERATIVA'];
  171. $arrWarehouse['PAIS'] = $arrResponseGetAddress['response']['PAIS'];
  172. }
  173. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrWarehouse);
  174. }
  175. public function createWarehouse( Request $request ) {
  176. $validator = Validator::make($request->all(), [
  177. 'CODIGO_ALMACEN' => 'required|string',
  178. 'NOMBRE_ALMACEN' => 'required|string',
  179. 'NOMBRE_RESPONSABLE' => 'required|string',
  180. 'APELLIDO_PATERNO_RESPONSABLE' => 'required|string',
  181. 'APELLIDO_MATERNO_RESPONSABLE' => 'nullable|string',
  182. 'CORREO1' => 'required|string',
  183. 'CORREO2' => 'nullable|string',
  184. 'LADA1' => 'required|string',
  185. 'TELEFONO1' => 'required|string',
  186. 'LADA2' => 'nullable|string',
  187. 'TELEFONO2' => 'nullable|string',
  188. 'CALLE' => 'required|string',
  189. 'NUMERO_EXTERIOR' => 'required|string',
  190. 'NUMERO_INTERIOR' => 'nullable|string',
  191. 'CODIGO_POSTAL' => 'required|string',
  192. 'COLONIA' => 'required|string',
  193. 'LOCALIDAD' => 'nullable|string',
  194. 'MUNICIPIO' => 'required|string',
  195. 'ENTIDAD_FEDERATIVA' => 'required|string',
  196. 'PAIS' => 'required|string',
  197. 'USUARIO' => 'required|string',
  198. 'NUMERO_LINEA' => 'required|string',
  199. ]);
  200. if ($validator->fails()) {
  201. return $this->responseController->makeResponse(
  202. true,
  203. "ERR_WAREHOUSE_REG000: Se encontraron uno o más errores.",
  204. $this->responseController->makeErrors($validator->errors()->messages()),
  205. 401
  206. );
  207. }
  208. DB::beginTransaction();
  209. $requestData = $request->all();
  210. // Se valida y se obtiene el usuario
  211. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  212. if ($arrResponseCheckUser['error']) {
  213. DB::rollBack();
  214. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_REG001:'.$arrResponseCheckUser['msg'], [], 401);
  215. }
  216. $user = $arrResponseCheckUser['response'];
  217. // Se vallidan los campos de dirección
  218. $arrResponseCheckAddress = $this->resourcesController->validateAddress(
  219. $requestData['CODIGO_POSTAL'],
  220. $requestData['COLONIA'],
  221. $requestData['MUNICIPIO'],
  222. $requestData['LOCALIDAD'],
  223. $requestData['ENTIDAD_FEDERATIVA'],
  224. $requestData['PAIS'],
  225. $requestData['NUMERO_LINEA'],
  226. );
  227. if ($arrResponseCheckAddress['error']) {
  228. DB::rollBack();
  229. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_REG002: '.$arrResponseCheckAddress['msg'], $arrResponseCheckAddress['response'], 401);
  230. }
  231. $now = $this->functionsController->now();
  232. $currentDate = $now->toDateTimeString();
  233. try {
  234. $validateInsert = DB::table('S002V01TALMA')->insert([
  235. 'ALMA_NULI' => $requestData['NUMERO_LINEA'],
  236. 'ALMA_COAL' => $requestData['CODIGO_ALMACEN'],
  237. 'ALMA_NOAL' => $requestData['NOMBRE_ALMACEN'],
  238. 'ALMA_NORE' => $requestData['NOMBRE_RESPONSABLE'],
  239. 'ALMA_APRE' => $requestData['APELLIDO_PATERNO_RESPONSABLE'],
  240. 'ALMA_AMRE' => $requestData['APELLIDO_MATERNO_RESPONSABLE'],
  241. 'ALMA_COR1' => $requestData['CORREO1'],
  242. 'ALMA_COR2' => $requestData['CORREO2'],
  243. 'ALMA_LAD1' => $requestData['LADA1'],
  244. 'ALMA_TEL1' => $requestData['TELEFONO1'],
  245. 'ALMA_LAD2' => $requestData['LADA2'],
  246. 'ALMA_TEL2' => $requestData['TELEFONO2'],
  247. 'ALMA_CALL' => $requestData['CALLE'],
  248. 'ALMA_NUEX' => $requestData['NUMERO_EXTERIOR'],
  249. 'ALMA_NUIN' => $requestData['NUMERO_INTERIOR'],
  250. 'ALMA_COPO' => $requestData['CODIGO_POSTAL'],
  251. 'ALMA_COLO' => $requestData['COLONIA'],
  252. 'ALMA_LOCA' => $requestData['LOCALIDAD'],
  253. 'ALMA_MUNI' => $requestData['MUNICIPIO'],
  254. 'ALMA_ENTI' => $requestData['ENTIDAD_FEDERATIVA'],
  255. 'ALMA_PAIS' => $requestData['PAIS'],
  256. 'ALMA_USRE' => $user,
  257. 'ALMA_FERE' => $currentDate,
  258. 'ALMA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  259. ]);
  260. } catch (\Throwable $th) {
  261. DB::rollBack();
  262. return $this->responseController->makeResponse(
  263. true,
  264. "ERR_WAREHOUSE_REG003: Ocurrió un error al insertar el registro del almacen.",
  265. $th->getMessage(),
  266. 500
  267. );
  268. }
  269. if (!$validateInsert) {
  270. DB::rollBack();
  271. return $this->responseController->makeResponse(
  272. true,
  273. "ERR_WAREHOUSE_REG004: No se pudo insertar el registro del almacen.",
  274. [],
  275. 500
  276. );
  277. }
  278. DB::commit();
  279. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  280. }
  281. public function updateWarehouse( Request $request, $idWarehouse ) {
  282. $validator = Validator::make($request->all(), [
  283. 'CODIGO_ALMACEN' => 'required|string',
  284. 'NOMBRE_ALMACEN' => 'required|string',
  285. 'NOMBRE_RESPONSABLE' => 'required|string',
  286. 'APELLIDO_PATERNO_RESPONSABLE' => 'required|string',
  287. 'APELLIDO_MATERNO_RESPONSABLE' => 'nullable|string',
  288. 'CORREO1' => 'required|string',
  289. 'CORREO2' => 'nullable|string',
  290. 'LADA1' => 'required|string',
  291. 'TELEFONO1' => 'required|string',
  292. 'LADA2' => 'nullable|string',
  293. 'TELEFONO2' => 'nullable|string',
  294. 'CALLE' => 'required|string',
  295. 'NUMERO_EXTERIOR' => 'required|string',
  296. 'NUMERO_INTERIOR' => 'nullable|string',
  297. 'CODIGO_POSTAL' => 'required|string',
  298. 'COLONIA' => 'required|string',
  299. 'LOCALIDAD' => 'nullable|string',
  300. 'MUNICIPIO' => 'required|string',
  301. 'ENTIDAD_FEDERATIVA' => 'required|string',
  302. 'PAIS' => 'required|string',
  303. 'USUARIO' => 'required|string',
  304. 'NUMERO_LINEA' => 'required|string',
  305. ]);
  306. if ($validator->fails()) {
  307. return $this->responseController->makeResponse(
  308. true,
  309. "ERR_WAREHOUSE_UPD000: Se encontraron uno o más errores.",
  310. $this->responseController->makeErrors($validator->errors()->messages()),
  311. 401
  312. );
  313. }
  314. DB::beginTransaction();
  315. $requestData = $request->all();
  316. try {
  317. $idWarehouse = $this->encController->decrypt($idWarehouse);
  318. } catch (\Throwable $th) {
  319. return $this->responseController->makeResponse(true, "ERR_WAREHOUSE_UPD001: Ocurrió un error al desencriptar el ID del almacen.". $th->getMessage(), 406);
  320. }
  321. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  322. if ($arrResponseCheckUser['error']) {
  323. DB::rollBack();
  324. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_UPD002:'.$arrResponseCheckUser['msg'], [], 401);
  325. }
  326. $user = $arrResponseCheckUser['response'];
  327. $arrResponseCheckAddress = $this->resourcesController->validateAddress(
  328. $requestData['CODIGO_POSTAL'],
  329. $requestData['COLONIA'],
  330. $requestData['MUNICIPIO'],
  331. $requestData['LOCALIDAD'],
  332. $requestData['ENTIDAD_FEDERATIVA'],
  333. $requestData['PAIS'],
  334. $requestData['NUMERO_LINEA'],
  335. );
  336. if ($arrResponseCheckAddress['error']) {
  337. DB::rollBack();
  338. return $this->responseController->makeResponse(true, 'ERR_WAREHOUSE_UPD003: '.$arrResponseCheckAddress['msg'], $arrResponseCheckAddress['response'], 401);
  339. }
  340. $now = $this->functionsController->now();
  341. $currentDate = $now->toDateTimeString();
  342. try {
  343. $validateUpdate = DB::table('S002V01TALMA')
  344. ->where('ALMA_NULI', '=', $requestData['NUMERO_LINEA'])
  345. ->where('ALMA_IDAL', '=', $idWarehouse)
  346. ->update([
  347. 'ALMA_COAL' => $requestData['CODIGO_ALMACEN'],
  348. 'ALMA_NOAL' => $requestData['NOMBRE_ALMACEN'],
  349. 'ALMA_NORE' => $requestData['NOMBRE_RESPONSABLE'],
  350. 'ALMA_APRE' => $requestData['APELLIDO_PATERNO_RESPONSABLE'],
  351. 'ALMA_AMRE' => $requestData['APELLIDO_MATERNO_RESPONSABLE'],
  352. 'ALMA_COR1' => $requestData['CORREO1'],
  353. 'ALMA_COR2' => $requestData['CORREO2'],
  354. 'ALMA_LAD1' => $requestData['LADA1'],
  355. 'ALMA_TEL1' => $requestData['TELEFONO1'],
  356. 'ALMA_LAD2' => $requestData['LADA2'],
  357. 'ALMA_TEL2' => $requestData['TELEFONO2'],
  358. 'ALMA_CALL' => $requestData['CALLE'],
  359. 'ALMA_NUEX' => $requestData['NUMERO_EXTERIOR'],
  360. 'ALMA_NUIN' => $requestData['NUMERO_INTERIOR'],
  361. 'ALMA_COPO' => $requestData['CODIGO_POSTAL'],
  362. 'ALMA_COLO' => $requestData['COLONIA'],
  363. 'ALMA_LOCA' => $requestData['LOCALIDAD'],
  364. 'ALMA_MUNI' => $requestData['MUNICIPIO'],
  365. 'ALMA_ENTI' => $requestData['ENTIDAD_FEDERATIVA'],
  366. 'ALMA_PAIS' => $requestData['PAIS'],
  367. 'ALMA_USMO' => $user,
  368. 'ALMA_FEMO' => $currentDate,
  369. 'ALMA_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  370. ]);
  371. } catch (\Throwable $th) {
  372. DB::rollBack();
  373. return $this->responseController->makeResponse(
  374. true,
  375. "ERR_WAREHOUSE_UPD004: Ocurrió un error al insertar el registro del almacen.",
  376. $th->getMessage(),
  377. 500
  378. );
  379. }
  380. if (!$validateUpdate) {
  381. DB::rollBack();
  382. return $this->responseController->makeResponse(
  383. true,
  384. "ERR_WAREHOUSE_UPD005: No se pudo insertar el registro del almacen.",
  385. [],
  386. 500
  387. );
  388. }
  389. DB::commit();
  390. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  391. }
  392. // Crear Artículo en Stock
  393. public function createArtitleWithoutOrder(Request $request) {
  394. $validator = Validator::make($request->all(), [
  395. 'ARTICULO' => 'required',
  396. 'MODELO' => 'required',
  397. 'CODIGO_MODELO' => 'required',
  398. 'FAMILIA' => 'required',
  399. 'SUBFAMILIA' => 'required',
  400. 'UNIDAD' => 'required',
  401. 'CODIGO_BARRAS' => 'required',
  402. 'CANTIDAD' => 'required',
  403. 'STOCK_MINIMO' => 'required',
  404. 'STOCK_MAXIMO' => 'required',
  405. 'REPARACION' => 'required|boolean',
  406. 'CONSUMIBLE' => 'required|boolean',
  407. 'PELIGROSO' => 'required|boolean',
  408. // 'FECHA_VENCIMIENTO' => '',
  409. // 'PROVEEDOR' => 'required',
  410. 'IMAGEN' => 'required',
  411. 'NUMERO_LINEA' => 'required',
  412. 'USUARIO' => 'required',
  413. ]);
  414. if ($validator->fails()) {
  415. return $this->responseController->makeResponse(
  416. true,
  417. "ERR_STOCK_REG000: Se encontraron uno o más errores.",
  418. $this->responseController->makeErrors($validator->errors()->messages()),
  419. 401
  420. );
  421. }
  422. DB::beginTransaction();
  423. $requestData = $request->all();
  424. try {
  425. $user = $this->encController->decrypt($requestData['USUARIO']);
  426. } catch (\Throwable $th) {
  427. DB::rollBack();
  428. return $this->responseController->makeResponse(true, "ERR_STOCK_REG001: Ocurrió un error al obtener el usuario.", $th->getMessage(), 500);
  429. }
  430. try {
  431. $validateFamily = DB::table('S002V01TFAMI')
  432. ->where('FAMI_COFA','=', $requestData['FAMILIA'])
  433. ->where('FAMI_NULI','=', $requestData['NUMERO_LINEA'])
  434. ->where('FAMI_ESTA','=','Activo')
  435. ->exists();
  436. } catch (\Throwable $th) {
  437. DB::rollBack();
  438. return $this->responseController->makeResponse(true, "ERR_STOCK_REG002: Ocurrió un error al validar la familia.", $th->getMessage(), 500);
  439. }
  440. if (!$validateFamily) {
  441. DB::rollBack();
  442. return $this->responseController->makeResponse(true, "ERR_STOCK_REG003: La familia no existe.", [], 500);
  443. }
  444. try {
  445. $validateSubfamily = DB::table('S002V01TSUBF')
  446. ->where('SUBF_COSU','=', $requestData['SUBFAMILIA'])
  447. ->where('SUBF_NULI','=', $requestData['NUMERO_LINEA'])
  448. ->where('SUBF_ESTA','=','Activo')
  449. ->exists();
  450. } catch (\Throwable $th) {
  451. DB::rollBack();
  452. return $this->responseController->makeResponse(true, "ERR_STOCK_REG004: Ocurrió un error al validar la subfamilia.", $th->getMessage(), 500);
  453. }
  454. if (!$validateSubfamily) {
  455. DB::rollBack();
  456. return $this->responseController->makeResponse(true, "ERR_STOCK_REG005: La subfamilia no existe.", [], 500);
  457. }
  458. try {
  459. $validateUnit = DB::table('S002V01TUNID')
  460. ->where('UNID_IDUN','=', $requestData['UNIDAD'])
  461. ->where('UNID_NULI','=', $requestData['NUMERO_LINEA'])
  462. ->where('UNID_ESTA','=','Activo')
  463. ->exists();
  464. } catch (\Throwable $th) {
  465. DB::rollBack();
  466. return $this->responseController->makeResponse(true, "ERR_STOCK_REG006: Ocurrió un error al validar la unidad.", $th->getMessage(), 500);
  467. }
  468. if (!$validateUnit) {
  469. DB::rollBack();
  470. return $this->responseController->makeResponse(true, "ERR_STOCK_REG007: La unidad no existe.", [], 500);
  471. }
  472. $requestData['PROVEEDOR'] = $requestData['PROVEEDOR'] === '' ? null : $requestData['PROVEEDOR'];
  473. if (!is_null($requestData['PROVEEDOR'])) {
  474. try {
  475. $validateUnit = DB::table('S002V01TPROV')
  476. ->where('PROV_NUPR','=', $requestData['PROVEEDOR'])
  477. ->where('PROV_NULI','=', $requestData['NUMERO_LINEA'])
  478. ->where('PROV_ESTA','=','Activo')
  479. ->exists();
  480. } catch (\Throwable $th) {
  481. DB::rollBack();
  482. return $this->responseController->makeResponse(true, "ERR_STOCK_REG008: Ocurrió un error al validar el proveedor.", $th->getMessage(), 500);
  483. }
  484. if (!$validateUnit) {
  485. DB::rollBack();
  486. return $this->responseController->makeResponse(true, "ERR_STOCK_REG009: El proveedor no existe.", [], 500);
  487. }
  488. }
  489. $arrCodeImages = array();
  490. foreach ($requestData['IMAGEN'] as $key => $encIdFile) {
  491. $idFile = $this->encController->decrypt($encIdFile);
  492. $tempFile = DB::table('S002V01TARTE')->where([
  493. ['ARTE_NULI', '=', $requestData['NUMERO_LINEA']],
  494. ['ARTE_IDAR', '=', $idFile],
  495. ])->first();
  496. if(is_null($tempFile)){
  497. return $this->responseController->makeResponse(true, 'ERR_ARTITLE_REG006: El archivo consultado no está registrado', [], 404);
  498. }else if($tempFile->ARTE_ESTA == 'Eliminado'){
  499. return $this->responseController->makeResponse(true, 'ERR_ARTITLE_REG007: El archivo consultado está eliminado', [], 404);
  500. }
  501. $fileResponse = $this->documentManagementController->moveFinalFile(
  502. intval($requestData['NUMERO_LINEA']),
  503. 'GIST',
  504. 'FO',
  505. $tempFile,
  506. $user,
  507. );
  508. if(!$fileResponse[0]){
  509. return $this->responseController->makeResponse(true, 'ERR_ARTITLE_REG008: '.$fileResponse[1], [], 400);
  510. }
  511. $arrCodeImages[] = $this->encController->encrypt($fileResponse[1]);
  512. }
  513. $jsonImages = json_encode($arrCodeImages);
  514. $now = $this->functionsController->now();
  515. $currentDate = $now->toDateTimeString();
  516. try {
  517. $validateRegister = DB::table('S002V01TSTAR')->insert([
  518. // 'STAR_CODI' => $requestData['CODIGO_STOCK'],
  519. 'STAR_ARTI' => $requestData['ARTICULO'],
  520. 'STAR_MODE' => $requestData['MODELO'],
  521. 'STAR_COMO' => $requestData['CODIGO_MODELO'],
  522. 'STAR_IDFA' => $requestData['FAMILIA'],
  523. 'STAR_IDSU' => $requestData['SUBFAMILIA'],
  524. 'STAR_NUPR' => $requestData['PROVEEDOR'],
  525. 'STAR_IDUN' => $requestData['UNIDAD'],
  526. 'STAR_COBA' => $requestData['CODIGO_BARRAS'],
  527. 'STAR_CANT' => $requestData['CANTIDAD'],
  528. 'STAR_STMI' => $requestData['STOCK_MINIMO'],
  529. 'STAR_STMA' => $requestData['STOCK_MAXIMO'],
  530. 'STAR_REPA' => $requestData['REPARACION'],
  531. 'STAR_CONS' => $requestData['CONSUMIBLE'],
  532. 'STAR_PELI' => $requestData['PELIGROSO'],
  533. 'STAR_FEVE' => $requestData['FECHA_VENCIMIENTO'],
  534. 'STAR_IMAG' => $jsonImages,
  535. 'STAR_TIAD' => 'Sin Pedido',
  536. 'STAR_NULI' => $requestData['NUMERO_LINEA'],
  537. 'STAR_USRE' => $user,
  538. 'STAR_FERE' => $currentDate,
  539. 'STAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  540. ]);
  541. } catch (\Throwable $th) {
  542. DB::rollBack();
  543. return $this->responseController->makeResponse(true, "ERR_STOCK_REG011: Ocurrió un error al registrar el artículo.", $th->getMessage(), 500);
  544. }
  545. if (!$validateRegister) {
  546. DB::rollBack();
  547. return $this->responseController->makeResponse(true, "ERR_STOCK_REG012: No se pudo registrar el artículo.", [], 500);
  548. }
  549. DB::commit();
  550. return $this->responseController->makeResponse(false, "EXITO: Registro Exitoso");
  551. }
  552. }