RequestLineController.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. <?php
  2. /*
  3. Desarrollador: Ing. Jean Jairo Benitez Meza
  4. Ultima Modificación: 27/04/2023
  5. Módulo: Gestión de Adquisiciones
  6. */
  7. namespace App\Http\Controllers;
  8. use App\Http\Controllers\ResponseController;
  9. use App\Http\Controllers\EncryptionController;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Carbon;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Validator;
  14. use App\Http\Controllers\FunctionsController;
  15. class RequestLineController extends Controller
  16. {
  17. private $responseController;
  18. private $encController;
  19. private $documentManagementController;
  20. private $functionsController;
  21. private $resourcesController;
  22. public function __construct(){
  23. $this->responseController = new ResponseController();
  24. $this->encController = new EncryptionController();
  25. $this->documentManagementController = new DocumentManagementController();
  26. $this->functionsController = new FunctionsController();
  27. $this->resourcesController = new ResourcesController();
  28. }
  29. // Se registran las líneas de solicitud
  30. public function createRequestLine(Request $request){
  31. $validator = Validator::make($request->all(), [
  32. 'LINE' => 'required|string',
  33. 'USER' => 'required|string',
  34. 'PRODUCTS' => 'required|array',
  35. 'PURPOSE' => 'required',
  36. ]);
  37. if ($validator->fails()) {
  38. return $this->responseController->makeResponse(
  39. true,
  40. "ERR_REQUESTLINE_REG000: Se encontraron uno o más errores.",
  41. $this->responseController->makeErrors($validator->errors()->messages()),
  42. 401
  43. );
  44. }
  45. DB::beginTransaction();
  46. $requestData = $request->all();
  47. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USER'], $requestData['LINE']);
  48. if ($arrResponseCheckUser['error']) {
  49. return $this->responseController->makeResponse(true, 'ERR_REQUESTLINE_REG001:'.$arrResponseCheckUser['msg'], [], 401);
  50. }
  51. $user = $arrResponseCheckUser['response'];
  52. $idOrderPreventive = null;
  53. if ($requestData['PURPOSE']['PROPOSITO'] == 'MANT-PREV') {
  54. $idOrderPreventive = $requestData['PURPOSE']['ID'];
  55. try {
  56. $validateExistsPreventive = DB::table('S002V01TOTPR')
  57. ->where('OTPR_NULI', '=', $requestData['LINE'])
  58. ->where('OTPR_IDOT', '=', $idOrderPreventive )
  59. ->exists();
  60. } catch (\Throwable $th) {
  61. DB::rollBack();
  62. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_REG002: Ocurrió un error al validar la orden de mantenimiento preventivo.", $th->getMessage(), 500);
  63. }
  64. if (!$validateExistsPreventive) {
  65. DB::rollBack();
  66. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_REG003: La orden de mantenimiento preventivo no existe", [], 500);
  67. }
  68. }
  69. $idOrderCorrective = null;
  70. if ($requestData['PURPOSE']['PROPOSITO'] == 'MANT-CORR') {
  71. $idOrderCorrective = $requestData['PURPOSE']['ID'];
  72. try {
  73. $validateExistsCorrective = DB::table('S002V01TOTCO')
  74. ->where('OTCO_NULI', '=', $requestData['LINE'])
  75. ->where('OTCO_IDOT', '=', $idOrderCorrective )
  76. ->exists();
  77. } catch (\Throwable $th) {
  78. DB::rollBack();
  79. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_REG004: Ocurrió un error al validar la orden de mantenimiento correctivo.", $th->getMessage(), 500);
  80. }
  81. if (!$validateExistsCorrective) {
  82. DB::rollBack();
  83. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_REG005: La orden de mantenimiento correctivo no existe", [], 500);
  84. }
  85. }
  86. $now = $this->functionsController->now();
  87. $currentDate = $now->toDateTimeString();
  88. $arrProviders = array_unique(array_column($requestData['PRODUCTS'], 'NUMERO_PROVEEDOR'));
  89. foreach ($arrProviders as $provider) {
  90. try {
  91. $idRegisterLine = DB::table('S002V01TLINE')->insertGetId([
  92. 'LINE_NUPR' => $provider,
  93. 'LINE_OTPR' => $idOrderPreventive,
  94. 'LINE_OTCO' => $idOrderCorrective,
  95. 'LINE_NULI' => $requestData['LINE'],
  96. 'LINE_USRE' => $user,
  97. 'LINE_FERE' => $currentDate,
  98. 'LINE_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  99. ]);
  100. } catch (\Throwable $th) {
  101. DB::rollBack();
  102. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_REG006: Ocurrió un error al hacer la inserción en la base.", $th->getMessage(), 500);
  103. }
  104. if ($idRegisterLine == null || $idRegisterLine <= 0) {
  105. DB::rollBack();
  106. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_REG007: Ocurrió un error al hacer la inserción en la base.", [], 500);
  107. }
  108. $nuar = 0;
  109. foreach ($requestData['PRODUCTS'] as $products) {
  110. if ($products['NUMERO_PROVEEDOR'] === $provider) {
  111. try {
  112. $validateInsert = DB::table('S002V01TARSE')->insert([
  113. 'ARSE_IDAS' => $nuar,
  114. 'ARSE_NULI' => $requestData['LINE'],
  115. 'ARSE_IDLI' => $idRegisterLine,
  116. 'ARSE_IDAR' => $products['NUMERO_ARTICULO'],
  117. 'ARSE_NUPR' => $products['NUMERO_PROVEEDOR'],
  118. 'ARSE_IDIN' => $products['NUMERO_INFORMACION'],
  119. 'ARSE_USRE' => $user,
  120. 'ARSE_FERE' => $currentDate,
  121. 'ARSE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  122. ]);
  123. } catch (\Throwable $th) {
  124. DB::rollBack();
  125. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_REG008: Ocurrió un error al insertar los artículos en la base de datos.", $th->getMessage(), 500);
  126. }
  127. if (!$validateInsert) {
  128. DB::rollBack();
  129. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_REG009: No se pudo insertar los datos en la base de datos.", [], 500);
  130. }
  131. $nuar = $nuar + 1;
  132. }
  133. }
  134. }
  135. DB::commit();
  136. return $this->responseController->makeResponse(false, "ÉXITO: Registro correcto");
  137. }
  138. public function getAllRequestLines($user, $line) {
  139. try {
  140. $arrRequestLines = DB::table('S002V01TLINE')
  141. ->where("LINE_NULI", "=", $line)
  142. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'LINE_NUPR')
  143. ->get([
  144. 'LINE_IDLI AS ID_LINEA_SOLICITUD',
  145. 'LINE_NUPR AS NUMERO_PROVEEDOR',
  146. 'PROV_NOCO AS NOMBRE_COMERCIAL',
  147. 'LINE_OTPR AS ORDEN_TRABAJO_PREVENTIVO',
  148. 'LINE_OTCO AS ORDEN_TRABAJO_CORRECTIVO',
  149. 'LINE_ESTA AS ESTADO',
  150. 'LINE_USRE AS USUARIO_REGISTRA',
  151. 'LINE_FERE AS FECHA_REGISTRA',
  152. 'LINE_USMO AS USUARIO_MODIFICA',
  153. 'LINE_FEMO AS FECHA_MODIFICA',
  154. ]);
  155. } catch (\Throwable $th) {
  156. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_GET000: No se pudo realizar la consulta a la base.", $th->getMessage(), 500);
  157. }
  158. $arrRequestLines = json_decode(json_encode($arrRequestLines), true);
  159. try {
  160. $arrOrders= DB::table('S002V01TORCO')
  161. ->where("ORCO_NULI", "=", $line)
  162. ->get([
  163. 'ORCO_NUOR AS NUMERO_ORDEN',
  164. 'ORCO_IDLI AS ID_LINEA_SOLICITUD',
  165. 'ORCO_IDDE AS ID_DESPACHO',
  166. 'ORCO_ESTA AS ESTADO',
  167. ]);
  168. } catch (\Throwable $th) {
  169. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_GET001: No se pudo realizar la consulta a la base.", $th->getMessage(), 500);
  170. }
  171. $arrOrders = json_decode(json_encode($arrOrders), true);
  172. foreach ($arrRequestLines as $keyRequest => $requestLine) {
  173. try {
  174. $getSelected = DB::table('S002V01TARSE')
  175. ->where('ARSE_IDLI', '=', $requestLine['ID_LINEA_SOLICITUD'])
  176. ->where('ARSE_NULI', '=', $line)
  177. ->where('INAR_NULI', '=', $line)
  178. ->where('DEAR_NULI', '=', $line)
  179. ->where('ARTI_NULI', '=', $line)
  180. ->where('FAMI_NULI', '=', $line)
  181. ->where('SUBF_NULI', '=', $line)
  182. ->where('UNID_NULI', '=', $line)
  183. ->join('S002V01TINAR','INAR_IDIN','=','ARSE_IDIN')
  184. ->join('S002V01TDEAR','DEAR_IDDE','=','INAR_IDDE')
  185. ->join('S002V01TARTI','ARTI_IDAR','=','DEAR_IDAR')
  186. ->join('S002V01TFAMI','FAMI_COFA','=','ARTI_COFA')
  187. ->join('S002V01TSUBF','SUBF_COSU','=','ARTI_COSU')
  188. ->join('S002V01TUNID','UNID_IDUN','=','DEAR_IDUN')
  189. ->join('S002V01TCAMO','CAMO_COMO','=','INAR_COMO')
  190. ->get([
  191. 'ARSE_IDAS AS ID_ARTICULO_SELECCIONADO',
  192. 'ARSE_NUPR AS NUMERO_PROVEEDOR',
  193. 'ARSE_ESTA AS ESTADO',
  194. 'INAR_IDIN AS NUMERO_INFORMACION',
  195. 'INAR_CODI AS CODIGO_INFORMACION',
  196. 'INAR_MODE AS MODELO_INFORMACION',
  197. 'CAMO_COMO AS CODIGO_MONEDA',
  198. 'CAMO_DESC AS MONEDA_DESCRIPCION',
  199. 'INAR_PREC AS PRECIO_UNITARIO',
  200. 'INAR_MOMI AS MONTO_MINIMO',
  201. 'INAR_CARA AS CARACTERISTICAS_INFORMACION',
  202. 'DEAR_IDDE AS NUMERO_DESCRIPCION',
  203. 'DEAR_IMAG AS IMAGENES',
  204. 'DEAR_DESC AS DESCRIPCION',
  205. 'DEAR_CARA AS CARACTERISTICAS',
  206. 'DEAR_COWE AS COMPRA_WEB',
  207. 'ARTI_IDAR AS NUMERO_ARTICULO',
  208. 'ARTI_CODI AS CODIGO',
  209. 'ARTI_NOMB AS ARTICULO',
  210. 'FAMI_COFA AS CODIGO_FAMILIA',
  211. 'FAMI_NOFA AS NOMBRE_FAMILIA',
  212. 'SUBF_COSU AS CODIGO_SUBFAMILIA',
  213. 'SUBF_NOSU AS NOMBRE_SUBFAMILIA',
  214. 'UNID_IDUN AS ID_UNIDAD',
  215. 'UNID_NOMB AS NOMBRE_UNIDAD',
  216. 'UNID_ACRO AS ACRONIMO_UNIDAD',
  217. ]);
  218. } catch (\Throwable $th) {
  219. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_GET002: No se pudo realizar la consulta a la base.", $th->getMessage(), 500);
  220. }
  221. $arrSelected = json_decode(json_encode($getSelected), true);
  222. $index = 0;
  223. $arrArtitles = array();
  224. foreach ($arrSelected as $keyArtitle => $artitles) {
  225. if ($keyArtitle === 0) {
  226. $arrArtitles[$index] = $artitles;
  227. $arrArtitles[$index]['CANTIDAD'] = 1;
  228. } else {
  229. if (
  230. in_array($artitles['NUMERO_INFORMACION'], array_column($arrArtitles, 'NUMERO_INFORMACION'))
  231. ) {
  232. $indexArtitle = array_search($artitles['NUMERO_INFORMACION'], array_column($arrArtitles, 'NUMERO_INFORMACION'));
  233. $arrArtitles[$indexArtitle]['CANTIDAD'] = $arrArtitles[$indexArtitle]['CANTIDAD'] + 1;
  234. } else {
  235. $index = $index + 1;
  236. $arrArtitles[$index] = $artitles;
  237. $arrArtitles[$index]['CANTIDAD'] = 1;
  238. }
  239. }
  240. }
  241. foreach ($arrArtitles as $keySelected => $selected) {
  242. $arrImagen = json_decode($selected['IMAGENES']);
  243. $arrUrlImage = array();
  244. foreach ($arrImagen as $key => $imagen) {
  245. $responseDocument = $this->documentManagementController->getPublicDocumentURL($imagen, $user, $line);
  246. $arrResponseDocument = json_decode($responseDocument->original, true);
  247. if ($arrResponseDocument['error']) {
  248. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_GET003: Ocurrió un error al obtener la URL de la imágen.", [], 500);
  249. }
  250. $arrUrlImage[] = $arrResponseDocument['response']['public_uri'];
  251. }
  252. $arrArtitles[$keySelected]['IMAGENES'] = $arrUrlImage;
  253. }
  254. foreach ($arrOrders as $order) {
  255. if ($requestLine['ID_LINEA_SOLICITUD'] == $order['ID_LINEA_SOLICITUD']) {
  256. $requestLine['NUMERO_ORDEN'] = $order['NUMERO_ORDEN'];
  257. }
  258. }
  259. if (!array_key_exists('LINE_NUOR', $requestLine)) {
  260. $requestLine['NUMERO_ORDEN'] = null;
  261. }
  262. $arrRequestLines[$keyRequest] = $requestLine;
  263. $arrRequestLines[$keyRequest]['SELECTED'] = $arrArtitles;
  264. }
  265. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrRequestLines);
  266. }
  267. // Se obtiene una línea de solicitud en específico
  268. public function getRequestLine($encIdRequestLine, $user, $line){
  269. try {
  270. $idRequestLine = $this->encController->decrypt($encIdRequestLine);
  271. } catch (\Throwable $th) {
  272. return $this->responseController->makeResponse(true, "ERR_ORDER_GET000: Ocurrió un error al obtener el contenido.", $th->getMessage(), 500);
  273. }
  274. try {
  275. $arrRequestLines = DB::table('S002V01TLINE')
  276. ->where("LINE_NULI", "=", $line)
  277. ->where("LINE_IDLI", "=", $idRequestLine)
  278. ->first([
  279. 'LINE_IDLI AS ID_LINEA_SOLICITUD',
  280. 'LINE_ESTA AS ESTADO',
  281. 'LINE_IDME AS METODO_PAGO',
  282. 'LINE_FERE AS FECHA_REGISTRA',
  283. 'LINE_FEMO AS FECHA_MODIFICA',
  284. 'LINE_USRE AS USUARIO_REGISTRA',
  285. 'LINE_USMO AS USUARIO_MODIFICA'
  286. ]);
  287. } catch (\Throwable $th) {
  288. return $this->responseController->makeResponse(true, "ERR_ORDER_GET001: No se pudo realizar la consulta a la base.", $th->getMessage(), 500);
  289. }
  290. try {
  291. $arrProducts = DB::table('S002V01TARSE')
  292. ->where("ARSE_NULI", "=", $line)
  293. ->where('ARSE_IDLI', '=', $idRequestLine)
  294. ->where('PROV_ESTA', '=', 'Activo')
  295. ->join('S002V01TPROV', 'ARSE_NUPR', '=', 'PROV_NUPR')
  296. ->join('S002V01TINAR', 'ARSE_IDIN', '=', 'INAR_IDIN')
  297. ->join('S002V01TCAMO', 'CAMO_COMO', '=', 'INAR_COMO')
  298. ->get([
  299. 'ARSE_IDAS AS NUMERO_ARTI_SELE',
  300. 'ARSE_IDAR AS NUMERO_ARTICULO',
  301. 'ARSE_NUPR AS NUMERO_PRODUCTO',
  302. 'INAR_MODE AS MODELO',
  303. 'INAR_CODI AS CODIGO',
  304. 'INAR_CARA AS CARACTERISTICAS',
  305. 'ARSE_CANT AS CANTIDAD',
  306. 'INAR_PREC AS PRECIO_UNITARIO',
  307. 'CAMO_COMO AS MONEDA',
  308. 'CAMO_DESC AS MONEDA_DESCRIPCION',
  309. // 'ARSE_PRTO AS PRECIO_TOTAL',
  310. 'ARSE_ESTA AS ESTADO',
  311. 'ARSE_USRE AS USUARIO_REGISTRA',
  312. 'ARSE_FERE AS FECHA_REGISTRA',
  313. 'ARSE_USMO AS USUARIO_MODIFICA',
  314. 'ARSE_FEMO AS FECHA_MODIFICA',
  315. 'PROV_NUPR AS NUMERO_PROVEEDOR',
  316. 'PROV_NOCO AS PROVEEDOR',
  317. 'PROV_ESTA AS ESTADO_PROVEEDOR'
  318. ]);
  319. foreach ($arrProducts as $productos) {
  320. if ($productos == 'Eliminado') {
  321. return $this->responseController->makeResponse(true, "ERR_ORDER_GET002: Existe un usuario en la solicitud que se encuentra eliminado", [], 500);
  322. }
  323. }
  324. $arrRequestLines->PRODUCTOS = $arrProducts;
  325. } catch (\Throwable $th) {
  326. return $this->responseController->makeResponse(true, "ERR_ORDER_GET003: No se pudo realizar la consulta a la base.", $th->getMessage(), 500);
  327. }
  328. return $this->responseController->makeResponse(false, "ÉXITO", $arrRequestLines);
  329. }
  330. // Se cancalan la linea de solicitud de compra
  331. public function cancelRequestLine(Request $request){
  332. $validator = Validator::make($request->all(), [
  333. 'ID_LINEA_SOLICITUD' => 'required|string',
  334. 'NUMERO_PROVEEDOR' => 'required|string',
  335. 'NUMERO_LINEA' => 'required|string',
  336. 'USUARIO' => 'required|string',
  337. ]);
  338. if ($validator->fails()) {
  339. return $this->responseController->makeResponse(
  340. true,
  341. "ERR_REQUESTLINE_CAN000: Se encontraron uno o más errores.",
  342. $this->responseController->makeErrors($validator->errors()->messages()),
  343. 401
  344. );
  345. }
  346. DB::beginTransaction();
  347. $requestData = $request->all();
  348. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  349. if ($arrResponseCheckUser['error']) {
  350. DB::rollBack();
  351. return $this->responseController->makeResponse(true, 'ERR_REQUESTLINE_CAN001:'.$arrResponseCheckUser['msg'], [], 401);
  352. }
  353. $user = $arrResponseCheckUser['response'];
  354. try {
  355. $requestData['ID_LINEA_SOLICITUD'] = $this->encController->decrypt($requestData['ID_LINEA_SOLICITUD']);
  356. } catch (\Throwable $th) {
  357. DB::rollBack();
  358. return $this->responseController->makeResponse(true, 'ERR_REQUESTLINE_CAN002: Ocurrió un error al desencriptar la línea de solicitud de compra.', $th->getMessage(), 500);
  359. }
  360. try {
  361. $requestData['NUMERO_PROVEEDOR'] = $this->encController->decrypt($requestData['NUMERO_PROVEEDOR']);
  362. } catch (\Throwable $th) {
  363. DB::rollBack();
  364. return $this->responseController->makeResponse(true, 'ERR_REQUESTLINE_CAN003: Ocurrió un error al desencriptar el número de proveedor.', $th->getMessage(), 500);
  365. }
  366. $now = $this->functionsController->now();
  367. $currentDate = $now->toDateTimeString();
  368. try {
  369. $validateExists = DB::table('S002V01TPROV')
  370. ->where('PROV_NUPR', '=', $requestData['NUMERO_PROVEEDOR'])
  371. ->where('PROV_NULI', '=', $requestData['NUMERO_LINEA'])
  372. ->where('PROV_ESTA', '=', 'Activo')
  373. ->exists();
  374. } catch (\Throwable $th) {
  375. DB::rollBack();
  376. return $this->responseController->makeResponse(true, 'ERR_REQUESTLINE_CAN004: Ocurrió un error al verificar el número del proveedor.', $th->getMessage(), 500);
  377. }
  378. if (!$validateExists) {
  379. DB::rollBack();
  380. return $this->responseController->makeResponse(true, 'ERR_REQUESTLINE_CAN005: El número del proveedor no existe.', [], 500);
  381. }
  382. try {
  383. $validateExists = DB::table('S002V01TLINE')
  384. ->where('LINE_IDLI', '=', $requestData['ID_LINEA_SOLICITUD'])
  385. ->where('LINE_NUPR', '=', $requestData['NUMERO_PROVEEDOR'])
  386. ->where('LINE_NULI', '=', $requestData['NUMERO_LINEA'])
  387. ->exists();
  388. } catch (\Throwable $th) {
  389. DB::rollBack();
  390. return $this->responseController->makeResponse(true, 'ERR_REQUESTLINE_CAN006: Ocurrió un error al verificar la línea de solicitud de compra.', $th->getMessage(), 500);
  391. }
  392. if (!$validateExists) {
  393. DB::rollBack();
  394. return $this->responseController->makeResponse(true, 'ERR_REQUESTLINE_CAN007: La linea de solicitud de compra no existe.', [], 500);
  395. }
  396. try {
  397. $validateUpdate = DB::table('S002V01TLINE')
  398. ->where('LINE_IDLI', '=', $requestData['ID_LINEA_SOLICITUD'])
  399. ->where('LINE_NULI', '=', $requestData['NUMERO_LINEA'])
  400. ->where('LINE_NUPR', '=', $requestData['NUMERO_PROVEEDOR'])
  401. ->update([
  402. 'LINE_ESTA' => 'Cancelado',
  403. 'LINE_USMO' => $user,
  404. 'LINE_FEMO' => $currentDate,
  405. 'LINE_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  406. ]);
  407. } catch (\Throwable $th) {
  408. DB::rollBack();
  409. return $this->responseController->makeResponse(true, 'ERR_REQUESTLINE_CAN008: Ocurrió un error al modificar la línea de solicitud de compra.', $th->getMessage(), 500);
  410. }
  411. if (!$validateUpdate) {
  412. DB::rollBack();
  413. return $this->responseController->makeResponse(true, 'ERR_REQUESTLINE_CAN009: No se pudo cancelar la solicitud de compra.', [], 500);
  414. }
  415. try {
  416. $validateUpdate = DB::table('S002V01TARSE')
  417. ->where('ARSE_NULI', '=', $requestData['NUMERO_LINEA'])
  418. ->where('ARSE_IDLI', '=', $requestData['ID_LINEA_SOLICITUD'])
  419. ->where('ARSE_NUPR', '=', $requestData['NUMERO_PROVEEDOR'])
  420. ->update([
  421. 'ARSE_ESTA' => 'Eliminado',
  422. 'ARSE_USMO' => $user,
  423. 'ARSE_FEMO' => $currentDate,
  424. 'ARSE_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  425. ]);
  426. } catch (\Throwable $th) {
  427. DB::rollBack();
  428. return $this->responseController->makeResponse(true, 'ERR_REQUESTLINE_CAN010: Ocurrió un error al modificar la línea de solicitud de compra.', $th->getMessage(), 500);
  429. }
  430. if (!$validateUpdate) {
  431. DB::rollBack();
  432. return $this->responseController->makeResponse(true, 'ERR_REQUESTLINE_CAN011: No se pudo eliminar los artículos seleccionados.', [], 500);
  433. }
  434. DB::commit();
  435. return $this->responseController->makeResponse(false, "ÉXITO: Cancelación Exitosa");
  436. }
  437. public function deleteArtitleByRequestLine(Request $request) {
  438. $validator = Validator::make($request->all(), [
  439. 'USUARIO' => 'required|string',
  440. 'NUMERO_LINEA' => 'required|string',
  441. 'NUMERO_INFORMACION' => 'required|string',
  442. 'NUMERO_PROVEEDOR' => 'required|string',
  443. 'NUMERO_ARTICULO' => 'required|string',
  444. 'ID_LINEA_SOLICITUD' => 'required|string',
  445. ]);
  446. if ($validator->fails()) {
  447. return $this->responseController->makeResponse(
  448. true,
  449. "ERR_REQUESTLINE_DEL000: Se encontraron uno o más errores.",
  450. $this->responseController->makeErrors($validator->errors()->messages()),
  451. 401
  452. );
  453. }
  454. DB::beginTransaction();
  455. $requestData = $request->all();
  456. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($requestData['USUARIO'], $requestData['NUMERO_LINEA']);
  457. if ($arrResponseCheckUser['error']) {
  458. return $this->responseController->makeResponse(true, 'ERR_LEVEL_UPD004:'.$arrResponseCheckUser['msg'], [], 401);
  459. }
  460. $user = $arrResponseCheckUser['response'];
  461. try {
  462. $requestData['NUMERO_INFORMACION'] = $this->encController->decrypt($requestData['NUMERO_INFORMACION']);
  463. } catch (\Throwable $th) {
  464. DB::rollBack();
  465. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL002: Ocurrió un error al obtener el número de información.", $th->getMessage(), 500);
  466. }
  467. try {
  468. $requestData['ID_LINEA_SOLICITUD'] = $this->encController->decrypt($requestData['ID_LINEA_SOLICITUD']);
  469. } catch (\Throwable $th) {
  470. DB::rollBack();
  471. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL003: Ocurrió un error al obtener la línea de solicitud de compra.", $th->getMessage(), 500);
  472. }
  473. try {
  474. $requestData['NUMERO_PROVEEDOR'] = $this->encController->decrypt($requestData['NUMERO_PROVEEDOR']);
  475. } catch (\Throwable $th) {
  476. DB::rollBack();
  477. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL004: Ocurrió un error al obtener el número del proveedor.", $th->getMessage(), 500);
  478. }
  479. try {
  480. $requestData['NUMERO_ARTICULO'] = $this->encController->decrypt($requestData['NUMERO_ARTICULO']);
  481. } catch (\Throwable $th) {
  482. DB::rollBack();
  483. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL005: Ocurrió un error al obtener el número del artículo.", $th->getMessage(), 500);
  484. }
  485. $now = $this->functionsController->now();
  486. $currentDate = $now->toDateTimeString();
  487. try {
  488. $validateExists = DB::table('S002V01TLINE')
  489. ->where('LINE_IDLI', '=', $requestData['ID_LINEA_SOLICITUD'])
  490. ->where('LINE_NULI', '=', $requestData['NUMERO_LINEA'])
  491. ->exists();
  492. } catch (\Throwable $th) {
  493. DB::rollBack();
  494. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL006: Ocurrió un error al verificar si existe la línea de solicitud de comrpa.", $th->getMessage(), 500);
  495. }
  496. if (!$validateExists) {
  497. DB::rollBack();
  498. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL007: El número de línea de solicitud de compra no existe.", [], 500);
  499. }
  500. try {
  501. $validateExists = DB::table('S002V01TARTI')
  502. ->where('ARTI_IDAR', '=', $requestData['NUMERO_ARTICULO'])
  503. ->where('ARTI_NULI', '=', $requestData['NUMERO_LINEA'])
  504. ->where('ARTI_ESTA', '=', 'Activo')
  505. ->exists();
  506. } catch (\Throwable $th) {
  507. DB::rollBack();
  508. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL008: Ocurrió un error al verificar si existe el número del artículo.", $th->getMessage(), 500);
  509. }
  510. if (!$validateExists) {
  511. DB::rollBack();
  512. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL009: El número del artículo no existe.", [], 500);
  513. }
  514. try {
  515. $validateExists = DB::table('S002V01TPROV')
  516. ->where('PROV_NUPR', '=', $requestData['NUMERO_PROVEEDOR'])
  517. ->where('PROV_NULI', '=', $requestData['NUMERO_LINEA'])
  518. ->where('PROV_ESTA', '=', 'Activo')
  519. ->exists();
  520. } catch (\Throwable $th) {
  521. DB::rollBack();
  522. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL010: Ocurrió un error al verificar si existe el número de proveedor.", $th->getMessage(), 500);
  523. }
  524. if (!$validateExists) {
  525. DB::rollBack();
  526. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL011: El número de proveedor no existe.", [], 500);
  527. }
  528. try {
  529. $validateExists = DB::table('S002V01TINAR')
  530. ->where('INAR_IDIN', '=', $requestData['NUMERO_INFORMACION'])
  531. ->where('INAR_NULI', '=', $requestData['NUMERO_LINEA'])
  532. ->where('INAR_ESTA', '=', 'Activo')
  533. ->exists();
  534. } catch (\Throwable $th) {
  535. DB::rollBack();
  536. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL012: Ocurrió un error al verificar si existe el numero de información del artículo.", $th->getMessage(), 500);
  537. }
  538. if (!$validateExists) {
  539. DB::rollBack();
  540. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL013: El número de información del artículo no existe.", [], 500);
  541. }
  542. try {
  543. $existsSelected = DB::table('S002V01TARSE')
  544. ->where('ARSE_NULI', '=', $requestData['NUMERO_LINEA'])
  545. ->where('ARSE_IDLI', '=', $requestData['ID_LINEA_SOLICITUD'])
  546. ->where('ARSE_IDAR', '=', $requestData['NUMERO_ARTICULO'])
  547. ->where('ARSE_NUPR', '=', $requestData['NUMERO_PROVEEDOR'])
  548. ->where('ARSE_IDIN', '=', $requestData['NUMERO_INFORMACION'])
  549. ->exists();
  550. } catch (\Throwable $th) {
  551. DB::rollBack();
  552. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL014: Ocurrió un error al modificar los artículos seleccionados.", $th->getMessage(), 500);
  553. }
  554. if (!$existsSelected) {
  555. DB::rollBack();
  556. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL015: No existen artículos asociados a la información obtenida.", [], 500);
  557. }
  558. try {
  559. $validateUpdate = DB::table('S002V01TARSE')
  560. ->where('ARSE_NULI', '=', $requestData['NUMERO_LINEA'])
  561. ->where('ARSE_IDLI', '=', $requestData['ID_LINEA_SOLICITUD'])
  562. ->where('ARSE_IDAR', '=', $requestData['NUMERO_ARTICULO'])
  563. ->where('ARSE_NUPR', '=', $requestData['NUMERO_PROVEEDOR'])
  564. ->where('ARSE_IDIN', '=', $requestData['NUMERO_INFORMACION'])
  565. ->update([
  566. 'ARSE_ESTA' => 'Eliminado',
  567. 'ARSE_USMO' => $user,
  568. 'ARSE_FEMO' => $currentDate,
  569. 'ARSE_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  570. ]);
  571. } catch (\Throwable $th) {
  572. DB::rollBack();
  573. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL016: Ocurrió un error al modificar los artículos seleccionados.", $th->getMessage(), 500);
  574. }
  575. if (!$validateUpdate) {
  576. DB::rollBack();
  577. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL017: Ocurrió un error al eliminar el artículo seleccionado.", [], 500);
  578. }
  579. try {
  580. $existsArtitles = DB::table('S002V01TARSE')
  581. ->where('ARSE_NULI', '=', $requestData['NUMERO_LINEA'])
  582. ->where('ARSE_IDLI', '=', $requestData['ID_LINEA_SOLICITUD'])
  583. ->where('ARSE_ESTA', '=', 'Activo')
  584. ->exists();
  585. } catch (\Throwable $th) {
  586. DB::rollBack();
  587. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL018: Ocurrió un error al verificar los artículos disponibles.", $th->getMessage(), 500);
  588. }
  589. if (!$existsArtitles) {
  590. try {
  591. $validateUpdate = DB::table('S002V01TLINE')
  592. ->where('LINE_IDLI', '=', $requestData['ID_LINEA_SOLICITUD'])
  593. ->where('LINE_NULI', '=', $requestData['NUMERO_LINEA'])
  594. ->where('LINE_NUPR', '=', $requestData['NUMERO_PROVEEDOR'])
  595. ->update([
  596. 'LINE_ESTA' => 'Cancelado',
  597. 'LINE_USMO' => $user,
  598. 'LINE_FEMO' => $currentDate,
  599. 'LINE_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  600. ]);
  601. } catch (\Throwable $th) {
  602. DB::rollBack();
  603. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL019: Ocurrió un error al cancelar la línea de solicitud de compra.", $th->getMessage(), 500);
  604. }
  605. if (!$validateUpdate) {
  606. DB::rollBack();
  607. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL020: No se pudo cancelar la línea de solicitud de compra.", [], 500);
  608. }
  609. }
  610. DB::commit();
  611. return $this->responseController->makeResponse(false, "ÉXITO: Eliminación Correcta");
  612. }
  613. public function updateStatusRequestLine(Request $request) {
  614. $validator = Validator::make($request->all(), [
  615. 'NUMERO_SOLICITUD' => 'required|string',
  616. 'ESTADO' => 'required|string',
  617. 'USUARIO' => 'required|string',
  618. 'METODO_PAGO' => 'required|string',
  619. 'NUMERO_LINEA' => 'required|string',
  620. // 'COMENTARIOS' => 'string',
  621. ]);
  622. if ($validator->fails()) {
  623. return $this->responseController->makeResponse(
  624. true,
  625. "ERR_ORDER_UPST000: Se encontraron uno o más errores.",
  626. $this->responseController->makeErrors($validator->errors()->messages()),
  627. 401
  628. );
  629. }
  630. DB::beginTransaction();
  631. $requestData = $request->all();
  632. try {
  633. $user = $this->encController->decrypt($requestData['USUARIO']);
  634. } catch (\Throwable $th) {
  635. DB::rollBack();
  636. return $this->responseController->makeResponse(true, "ERR_ORDER_UPST001: Ocurrió un error al obtener el usuario.", [], 500);
  637. }
  638. $now = $this->functionsController->now();
  639. $currentDate = $now->toDateTimeString();
  640. try {
  641. $validateUpdate = DB::table('S002V01TLINE')
  642. ->where('LINE_IDLI', '=', $requestData['NUMERO_SOLICITUD'])
  643. ->where('LINE_NULI', '=', $requestData['NUMERO_LINEA'])
  644. ->update([
  645. 'LINE_ESTA' => $requestData['ESTADO'],
  646. 'LINE_IDME' => $requestData['METODO_PAGO'],
  647. 'LINE_COME' => $requestData['COMENTARIOS'] === '' ? null : $requestData['COMENTARIOS'],
  648. 'LINE_USMO' => $user,
  649. 'LINE_FEMO' => $currentDate,
  650. 'LINE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  651. ]);
  652. } catch (\Throwable $th) {
  653. DB::rollBack();
  654. return $this->responseController->makeResponse(true, "ERR_ORDER_UPST002: Ocurrió un error al modificar el estado.", $th->getMessage(), 500);
  655. }
  656. if ( !$validateUpdate ) {
  657. DB::rollBack();
  658. return $this->responseController->makeResponse(true, "ERR_ORDER_UPST003: No se pudo modificar el estado de la línea de solicitud.", [], 500);
  659. }
  660. DB::commit();
  661. return $this->responseController->makeResponse(false, "ÉXITO: Modificación de Estado Exitoso");
  662. }
  663. public function getArtitlesByRequestLine($requestLine, $user, $line) {
  664. try {
  665. $requestLine = $this->encController->decrypt($requestLine);
  666. } catch (\Throwable $th) {
  667. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_GETBY000: Ocurrió un error al obtener la línea de solicitud.", $th->getMessage(), 500);
  668. }
  669. $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
  670. if ($arrResponseCheckUser['error']) {
  671. return $this->responseController->makeResponse(true, 'ERR_REQUESTLINE_GETBY001:'.$arrResponseCheckUser['msg'], [], 401);
  672. }
  673. try {
  674. $getArtitleSelected = DB::table('S002V01TARSE')
  675. ->where('ARSE_IDLI', '=', $requestLine)
  676. ->where('ARSE_NULI', '=', $line)
  677. ->where('INAR_NULI', '=', $line)
  678. ->where('DEAR_NULI', '=', $line)
  679. ->where('ARTI_NULI', '=', $line)
  680. ->where('FAMI_NULI', '=', $line)
  681. ->where('SUBF_NULI', '=', $line)
  682. ->where('UNID_NULI', '=', $line)
  683. ->where('ARSE_ESTA', '=', 'Activo')
  684. ->where('INAR_ESTA', '=', 'Activo')
  685. ->where('DEAR_ESTA', '=', 'Activo')
  686. ->where('ARTI_ESTA', '=', 'Activo')
  687. ->where('FAMI_ESTA', '=', 'Activo')
  688. ->where('SUBF_ESTA', '=', 'Activo')
  689. ->where('UNID_ESTA', '=', 'Activo')
  690. ->join('S002V01TINAR', 'INAR_IDIN', '=', 'ARSE_IDIN')
  691. ->join('S002V01TDEAR', 'DEAR_IDDE', '=', 'INAR_IDDE')
  692. ->join('S002V01TARTI', 'ARTI_IDAR', '=', 'DEAR_IDAR')
  693. ->join('S002V01TFAMI', 'FAMI_COFA', '=', 'ARTI_COFA')
  694. ->join('S002V01TSUBF', 'SUBF_COSU', '=', 'ARTI_COSU')
  695. ->join('S002V01TUNID', 'UNID_IDUN', '=', 'DEAR_IDUN')
  696. ->join('S002V01TCAMO', 'CAMO_COMO', '=', 'INAR_COMO')
  697. ->get([
  698. 'ARTI_IDAR AS NUMERO_ARTICULO',
  699. 'DEAR_IDDE AS NUMERO_DESCRIPCION',
  700. 'DEAR_NUPR AS NUMERO_PROVEEDOR',
  701. 'DEAR_IMAG AS IMAGENES',
  702. 'ARTI_NOMB AS ARTICULO',
  703. 'INAR_CARA AS CARACTERISTICAS',
  704. 'INAR_CODI AS CODIGO',
  705. 'INAR_MODE AS MODELO',
  706. 'CAMO_COMO AS MONEDA',
  707. 'CAMO_DESC AS MONEDA_DESCRIPCION',
  708. 'INAR_MOMI AS MONTO_MINIMO',
  709. 'INAR_IDIN AS NUMERO_INFORMACION',
  710. 'INAR_PREC AS PRECIO',
  711. ]);
  712. } catch (\Throwable $th) {
  713. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_GETBY002: Ocurrió un error al obtener la información.", $th->getMessage(), 500);
  714. }
  715. $arrArtitleSelected = json_decode(json_encode($getArtitleSelected), true);
  716. $index = 0;
  717. $arrArtitles = array();
  718. foreach ($arrArtitleSelected as $keyArtitle => $artitles) {
  719. if ($keyArtitle === 0) {
  720. $arrArtitles[$index] = $artitles;
  721. $arrArtitles[$index]['CANTIDAD'] = 1;
  722. } else {
  723. if (
  724. in_array($artitles['NUMERO_INFORMACION'], array_column($arrArtitles, 'NUMERO_INFORMACION'))
  725. ) {
  726. $indexArtitle = array_search($artitles['NUMERO_INFORMACION'], array_column($arrArtitles, 'NUMERO_INFORMACION'));
  727. $arrArtitles[$indexArtitle]['CANTIDAD'] = $arrArtitles[$indexArtitle]['CANTIDAD'] + 1;
  728. } else {
  729. $index = $index + 1;
  730. $arrArtitles[$index] = $artitles;
  731. $arrArtitles[$index]['CANTIDAD'] = 1;
  732. }
  733. }
  734. }
  735. $arrArtitleSelected = $arrArtitles;
  736. foreach ($arrArtitleSelected as $keyArtitleSelected => $artitleSelected) {
  737. $arrImage = json_decode($artitleSelected['IMAGENES']);
  738. $arrUrlImage = array();
  739. foreach ($arrImage as $key => $encImage) {
  740. $responseDocument = $this->documentManagementController->getPublicDocumentURL($encImage, $user, $line);
  741. $arrResponseDocument = json_decode($responseDocument->original, true);
  742. if ($arrResponseDocument['error']) {
  743. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_GETBY003: Ocurrió un error al obtener la URL de la imágen.", [], 500);
  744. }
  745. $arrUrlImage[] = $arrResponseDocument['response']['public_uri'];
  746. }
  747. $artitleSelected['IMAGENES'] = $arrUrlImage;
  748. $arrArtitleSelected[$keyArtitleSelected] = $artitleSelected;
  749. }
  750. return $this->responseController->makeResponse(false, "ÉXITO", $arrArtitleSelected);
  751. }
  752. }