RequestLineController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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. class RequestLineController extends Controller
  15. {
  16. private $responseController;
  17. private $encController;
  18. public function __construct(){
  19. $this->responseController = new ResponseController();
  20. $this->encController = new EncryptionController();
  21. }
  22. // Se registran las líneas de solicitud
  23. public function createRequestLine(Request $request){
  24. $validator = Validator::make($request->all(), [
  25. 'line' => 'required|string',
  26. 'user' => 'required|string',
  27. // 'description' => 'required|string',
  28. 'products' => 'required|string'
  29. ]);
  30. if ($validator->fails()) {
  31. return $this->responseController->makeResponse(
  32. true,
  33. "ERR_REQUESTLINE_REG000: Se encontraron uno o más errores.",
  34. $this->responseController->makeErrors($validator->errors()->messages()),
  35. 401
  36. );
  37. }
  38. DB::beginTransaction();
  39. $requestData = $request->all();
  40. try {
  41. $user = $this->encController->decrypt($requestData['user']);
  42. } catch (\Throwable $th) {
  43. DB::rollBack();
  44. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_REG001: Ocurrió un error al obtener el usuario.", [], 500);
  45. }
  46. try {
  47. $line = $this->encController->decrypt($requestData['line']);
  48. } catch (\Throwable $th) {
  49. DB::rollBack();
  50. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_REG002: Ocurrió un error al obtener la línea.", [], 500);
  51. }
  52. /* try {
  53. $description = $this->encController->decrypt($requestData['description']);
  54. } catch (\Throwable $th) {
  55. DB::rollBack();
  56. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_REG003: Ocurrió un error al obtener el contenido de la descripción.", [], 500);
  57. } */
  58. try {
  59. $strArtitles = $this->encController->decrypt($requestData['products']);
  60. } catch (\Throwable $th) {
  61. DB::rollBack();
  62. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_REG004: Ocurrió un error al obtener los productos del artículo.", [], 500);
  63. }
  64. if (empty($strArtitles)) {
  65. DB::rollBack();
  66. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_REG005: No se encontró ningún registro de productos.", [], 500);
  67. }
  68. $arrRequestLine = json_decode($strArtitles, true);
  69. foreach ($arrRequestLine as $keyRequest => $requestLine) {
  70. try {
  71. $idRegisterLine = DB::table('S002V01TLINE')->insertGetId([
  72. 'LINE_NUPR' => $requestLine['PROVIDER'],
  73. // 'LINE_DESC' => $description,
  74. 'LINE_NULI' => $line,
  75. 'LINE_USRE' => $user,
  76. 'LINE_FERE' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  77. 'LINE_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  78. ]);
  79. } catch (\Throwable $th) {
  80. DB::rollBack();
  81. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_REG006: Ocurrió un error al hacer la inserción en la base.", $th->getMessage(), 500);
  82. }
  83. if ($idRegisterLine == null || $idRegisterLine <= 0) {
  84. DB::rollBack();
  85. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_REG007: Ocurrió un error al hacer la inserción en la base.", [], 500);
  86. }
  87. $secuence = 1;
  88. foreach ($requestLine['ARTITLES'] as $key => $artitle) {
  89. try {
  90. $validate = DB::table('S002V01TARSE')->insert([
  91. 'ARSE_IDAS' => $secuence,
  92. 'ARSE_IDLI' => $idRegisterLine,
  93. 'ARSE_IDAR' => $artitle['NUMERO_ARTICULO'],
  94. 'ARSE_NUPR' => $requestLine['PROVIDER'],
  95. 'ARSE_IDIN' => $artitle['NUMERO_INFORMACION'],
  96. 'ARSE_CANT' => $artitle['CANTIDAD'],
  97. 'ARSE_PRTO' => $artitle['CANTIDAD'] * $artitle['PRECIO'],
  98. 'ARSE_NULI' => $line,
  99. 'ARSE_USRE' => $user,
  100. 'ARSE_FERE' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  101. 'ARSE_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  102. ]);
  103. } catch (\Throwable $th) {
  104. DB::rollBack();
  105. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_REG008: Ocurrió un error al hacer la inserción en la base.", $th->getMessage(), 500);
  106. }
  107. if (!$validate) {
  108. DB::rollBack();
  109. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_REG009: Ocurrió un error al hacer la inserción en la base.", [], 500);
  110. }
  111. $secuence++;
  112. }
  113. }
  114. DB::commit();
  115. return $this->responseController->makeResponse(false, "ÉXITO: Registro correcto");
  116. }
  117. // Se obtienen todas las líneas de solicitud
  118. public function getAllRequestLinesOld($line){
  119. try {
  120. $arrRequestLines = DB::table('S002V01TLINE')
  121. ->where("LINE_NULI", "=", $line)
  122. ->get([
  123. 'LINE_IDLI AS ID_LINEA_SOLICITUD',
  124. // 'LINE_DESC AS DESCRIPCION',
  125. 'LINE_ESTA AS ESTADO',
  126. 'LINE_FERE AS FECHA_REGISTRA',
  127. 'LINE_FEMO AS FECHA_MODIFICA',
  128. 'LINE_USRE AS USUARIO_MODIFICA',
  129. 'LINE_USRE AS USUARIO_MODIFICA'
  130. ]);
  131. } catch (\Throwable $th) {
  132. return $this->responseController->makeResponse(true, "ERR_ORDER_GET000: No se pudo realizar la consulta a la base.", [], 500);
  133. }
  134. foreach ($arrRequestLines as $keyRequest => $requestLines) {
  135. $idRequestLine = $requestLines->ID_LINEA_SOLICITUD;
  136. try {
  137. $countData = DB::table('S002V01TARSE')->where('ARSE_IDLI', '=', $idRequestLine)->count();
  138. } catch (\Throwable $th) {
  139. return $this->responseController->makeResponse(true, "ERR_ORDER_GET001: No se pudo realizar la consulta a la base", [], 500);
  140. }
  141. $requestLines->CANTIDAD = $countData;
  142. }
  143. return $this->responseController->makeResponse(false, "ÉXITO", $arrRequestLines);
  144. }
  145. public function getAllRequestLines($line) {
  146. try {
  147. $getRequestLines = DB::table('S002V01TLINE')
  148. ->where("LINE_NULI", "=", $line)
  149. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'LINE_NUPR')
  150. ->get([
  151. 'LINE_IDLI',
  152. 'LINE_NUPR',
  153. 'PROV_NOCO',
  154. 'LINE_NUOR',
  155. 'LINE_OTPR',
  156. 'LINE_OTCO',
  157. 'LINE_ESTA',
  158. 'LINE_USRE',
  159. 'LINE_FERE',
  160. 'LINE_USMO',
  161. 'LINE_FEMO',
  162. ]);
  163. } catch (\Throwable $th) {
  164. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_GET000: No se pudo realizar la consulta a la base.", $th->getMessage(), 500);
  165. }
  166. $arrRequestLines = json_decode(json_encode($getRequestLines), true);
  167. foreach ($arrRequestLines as $keyRequest => $requestLine) {
  168. try {
  169. $getSelected = DB::table('S002V01TARSE')
  170. ->where('ARSE_IDLI', '=', $requestLine['LINE_IDLI'])
  171. // ->where('ARSE_ESTA', '=', 'Activo')
  172. // ->where('INAR_ESTA', '=', 'Activo')
  173. // ->where('DEAR_ESTA', '=', 'Activo')
  174. // ->where('ARTI_ESTA', '=', 'Activo')
  175. // ->where('FAMI_ESTA', '=', 'Activo')
  176. // ->where('SUBF_ESTA', '=', 'Activo')
  177. // ->where('UNID_ESTA', '=', 'Activo')
  178. ->where('ARSE_NULI', '=', $line)
  179. ->where('INAR_NULI', '=', $line)
  180. ->where('DEAR_NULI', '=', $line)
  181. ->where('ARTI_NULI', '=', $line)
  182. ->where('FAMI_NULI', '=', $line)
  183. ->where('SUBF_NULI', '=', $line)
  184. ->where('UNID_NULI', '=', $line)
  185. ->join('S002V01TINAR','INAR_IDIN','=','ARSE_IDIN')
  186. ->join('S002V01TDEAR','DEAR_IDDE','=','INAR_IDDE')
  187. ->join('S002V01TARTI','ARTI_IDAR','=','DEAR_IDAR')
  188. ->join('S002V01TFAMI','FAMI_IDFA','=','ARTI_IDFA')
  189. ->join('S002V01TSUBF','SUBF_IDSU','=','ARTI_IDSU')
  190. ->join('S002V01TUNID','UNID_IDUN','=','DEAR_IDUN')
  191. ->get([
  192. 'ARSE_IDAS',
  193. 'ARSE_IDAR',
  194. 'ARSE_NUPR',
  195. 'ARSE_IDIN',
  196. 'ARSE_CANT',
  197. 'ARSE_PRTO',
  198. 'ARSE_ESTA',
  199. 'ARSE_USRE',
  200. 'ARSE_FERE',
  201. 'ARSE_USMO',
  202. 'ARSE_FEMO',
  203. 'INAR_IDIN',
  204. 'INAR_CODI',
  205. 'INAR_MODE',
  206. 'INAR_MONE',
  207. 'INAR_PREC',
  208. 'INAR_MOMI',
  209. 'INAR_CARA',
  210. 'DEAR_IDDE',
  211. 'DEAR_IMAG',
  212. 'DEAR_DESC',
  213. 'DEAR_CARA',
  214. 'DEAR_COWE',
  215. 'ARTI_IDAR',
  216. 'ARTI_CODI',
  217. 'ARTI_NOMB',
  218. 'FAMI_IDFA',
  219. 'FAMI_NOMB',
  220. 'SUBF_IDSU',
  221. 'SUBF_NOMB',
  222. 'UNID_IDUN',
  223. 'UNID_NOMB',
  224. 'UNID_ACRO',
  225. ]);
  226. } catch (\Throwable $th) {
  227. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_GET002: No se pudo realizar la consulta a la base.", $th->getMessage(), 500);
  228. }
  229. $arrSelected = json_decode(json_encode($getSelected), true);
  230. foreach ($arrSelected as $keySelected => $selected) {
  231. $arrSelected[$keySelected]['DEAR_IMAG'] = json_decode($selected['DEAR_IMAG']);
  232. /* foreach ($arrSelected[$keySelected]['DEAR_IMAG'] as $keyimagen => $imagen) {
  233. $arrSelected[$keySelected]['DEAR_IMAG'][$keyimagen] = array( 'image' => $imagen);
  234. } */
  235. }
  236. $arrRequestLines[$keyRequest]['SELECTED'] = $arrSelected;
  237. }
  238. return $this->responseController->makeResponse(false, "ÉXITO", $arrRequestLines);
  239. }
  240. // Se obtiene una línea de solicitud en específico
  241. public function getRequestLine($encIdRequestLine, $line){
  242. try {
  243. $idRequestLine = $this->encController->decrypt($encIdRequestLine);
  244. } catch (\Throwable $th) {
  245. return $this->responseController->makeResponse(true, "ERR_ORDER_GET000: Ocurrió un error al obtener el contenido.", [], 500);
  246. }
  247. try {
  248. $arrRequestLines = DB::table('S002V01TLINE')
  249. ->where("LINE_NULI", "=", $line)
  250. ->where("LINE_IDLI", "=", $idRequestLine)
  251. ->first([
  252. 'LINE_IDLI AS ID_LINEA_SOLICITUD',
  253. // 'LINE_DESC AS DESCRIPCION',
  254. 'LINE_ESTA AS ESTADO',
  255. 'LINE_IDME AS METODO_PAGO',
  256. 'LINE_FERE AS FECHA_REGISTRA',
  257. 'LINE_FEMO AS FECHA_MODIFICA',
  258. 'LINE_USRE AS USUARIO_MODIFICA',
  259. 'LINE_USRE AS USUARIO_MODIFICA'
  260. ]);
  261. } catch (\Throwable $th) {
  262. return $this->responseController->makeResponse(true, "ERR_ORDER_GET001: No se pudo realizar la consulta a la base.", [], 500);
  263. }
  264. try {
  265. $arrProducts = DB::table('S002V01TARSE')
  266. ->where("ARSE_NULI", "=", $line)
  267. ->where('ARSE_IDLI', '=', $idRequestLine)
  268. ->where('PROV_ESTA', '=', 'Activo')
  269. ->join('S002V01TPROV', 'ARSE_NUPR', '=', 'PROV_NUPR')
  270. ->join('S002V01TINAR', 'ARSE_IDIN', '=', 'INAR_IDIN')
  271. ->get([
  272. 'ARSE_IDAS AS NUMERO_ARTI_SELE',
  273. 'ARSE_IDAR AS NUMERO_ARTICULO',
  274. 'ARSE_NUPR AS NUMERO_PRODUCTO',
  275. 'INAR_MODE AS MODELO',
  276. 'INAR_CODI AS CODIGO',
  277. 'INAR_CARA AS CARACTERISTICAS',
  278. 'ARSE_CANT AS CANTIDAD',
  279. 'INAR_PREC AS PRECIO_UNITARIO',
  280. 'INAR_MONE AS MONEDA',
  281. 'ARSE_PRTO AS PRECIO_TOTAL',
  282. 'ARSE_ESTA AS ESTADO',
  283. 'ARSE_USRE AS USUARIO_REGISTRA',
  284. 'ARSE_FERE AS FECHA_REGISTRA',
  285. 'ARSE_USMO AS USUARIO_MODIFICA',
  286. 'ARSE_FEMO AS FECHA_MODIFICA',
  287. 'PROV_NUPR AS NUMERO_PROVEEDOR',
  288. 'PROV_NOCO AS PROVEEDOR',
  289. 'PROV_ESTA AS ESTADO_PROVEEDOR'
  290. ]);
  291. foreach ($arrProducts as $productos) {
  292. if ($productos == 'Eliminado') {
  293. return $this->responseController->makeResponse(true, "ERR_ORDER_GET002: Existe un usuario en la solicitud que se encuentra eliminado", [], 500);
  294. }
  295. }
  296. $arrRequestLines->PRODUCTOS = $arrProducts;
  297. } catch (\Throwable $th) {
  298. return $this->responseController->makeResponse(true, "ERR_ORDER_GET003: No se pudo realizar la consulta a la base.", [], 500);
  299. }
  300. return $this->responseController->makeResponse(false, "ÉXITO", $arrRequestLines);
  301. }
  302. // Se cancalan la linea de solicitud de compra
  303. public function cancelRequestLine(Request $request){
  304. $validator = Validator::make($request->all(), [
  305. 'line' => 'required|string',
  306. 'user' => 'required|string',
  307. 'idRequestLine' => 'required|string',
  308. ]);
  309. if ($validator->fails()) {
  310. return $this->responseController->makeResponse(
  311. true,
  312. "ERR_REQUESTLINE_CAN000: Se encontraron uno o más errores.",
  313. $this->responseController->makeErrors($validator->errors()->messages()),
  314. 401
  315. );
  316. }
  317. DB::beginTransaction();
  318. $requestData = $request->all();
  319. try {
  320. $user = $this->encController->decrypt($requestData['user']);
  321. } catch (\Throwable $th) {
  322. DB::rollBack();
  323. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_CAN001: Ocurrió un error al obtener el contenido.", $th->getMessage(), 500);
  324. }
  325. try {
  326. $line = $this->encController->decrypt($requestData['line']);
  327. } catch (\Throwable $th) {
  328. DB::rollBack();
  329. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_CAN002: Ocurrió un error al obtener la línea.", $th->getMessage(), 500);
  330. }
  331. try {
  332. $idRequestLine = $this->encController->decrypt($requestData['idRequestLine']);
  333. } catch (\Throwable $th) {
  334. DB::rollBack();
  335. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_CAN003: Ocurrió un error al obtener el identificador de la línea de solicitud de compra.", $th->getMessage(), 500);
  336. }
  337. try {
  338. $getRequestLine = (array) DB::table('S002V01TLINE')->where('LINE_IDLI', '=', $idRequestLine)->where('LINE_NULI', '=', $line)->first();
  339. } catch (\Throwable $th) {
  340. DB::rollBack();
  341. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_CAN004: Ocurrió un error al verificar que exista la línea de solicitud de compra.", $th->getMessage(), 500);
  342. }
  343. if (empty($getRequestLine)) {
  344. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_CAN005: La línea de solicitud de compra no existe.", [], 500);
  345. }
  346. try {
  347. $response = DB::table('S002V01TLINE')
  348. ->where('LINE_IDLI', '=', $idRequestLine)
  349. ->where('LINE_NULI', '=', $line)
  350. ->update([
  351. 'LINE_ESTA' => 'Cancelado',
  352. 'LINE_USMO' => $user,
  353. 'LINE_FEMO' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  354. 'LINE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  355. ]);
  356. } catch (\Throwable $th) {
  357. DB::rollBack();
  358. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_CAN006: No se pudo realizar la modificación a la base.", $th->getMessage(), 500);
  359. }
  360. if (!$response) {
  361. DB::rollBack();
  362. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_CAN007: Ocurrió un error al hacer la inserción en la base.", [], 500);
  363. }
  364. try {
  365. $countDeleteArtitles = DB::table('S002V01TARSE')
  366. ->where('ARSE_ESTA', '=', 'Eliminado')
  367. ->where('ARSE_NULI', '=', $line)
  368. ->where('ARSE_IDLI', '=', $idRequestLine)
  369. ->count();
  370. } catch (\Throwable $th) {
  371. DB::rollBack();
  372. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_CAN008: Ocurrió un error al obtener los artículos seleccionados.", $th->getMessage(), 500);
  373. }
  374. try {
  375. $getArtitleSelected = DB::table('S002V01TARSE')
  376. ->where('ARSE_ESTA', '=', 'Activo')
  377. ->where('ARSE_NULI', '=', $line)
  378. ->where('ARSE_IDLI', '=', $idRequestLine)
  379. ->get([
  380. 'ARSE_IDAS',
  381. 'ARSE_IDLI',
  382. 'ARSE_IDAR',
  383. 'ARSE_NUPR',
  384. 'ARSE_IDIN',
  385. 'ARSE_CANT',
  386. 'ARSE_PRTO',
  387. ]);
  388. } catch (\Throwable $th) {
  389. DB::rollBack();
  390. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_CAN008: Ocurrió un error al obtener los artículos seleccionados.", $th->getMessage(), 500);
  391. }
  392. $arrArtitleSelected = json_decode(json_encode($getArtitleSelected), true);
  393. foreach ($arrArtitleSelected as $keyArtitle => $artitleSelected) {
  394. try {
  395. $validateUpdate = DB::table('S002V01TARSE')
  396. ->where('ARSE_IDAS', '=', $artitleSelected['ARSE_IDAS'])
  397. ->where('ARSE_IDLI', '=', $idRequestLine)
  398. ->update([
  399. 'ARSE_IDAS' => 999 - $countDeleteArtitles - ($keyArtitle + 1),
  400. 'ARSE_ESTA' => 'Eliminado',
  401. 'ARSE_USMO' => $user,
  402. 'ARSE_FEMO' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  403. 'ARSE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  404. ]);
  405. } catch (\Throwable $th) {
  406. DB::rollBack();
  407. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_CAN009: Ocurrió un error al eliminar los artículos seleccionados.", $th->getMessage(), 500);
  408. }
  409. if (!$validateUpdate) {
  410. DB::rollBack();
  411. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_CAN010: No se pudo modificar el estado de los artículos seleccionados", [], 500);
  412. }
  413. }
  414. /* DB::rollBack();
  415. return $arrArtitleSelected; */
  416. DB::commit();
  417. return $this->responseController->makeResponse(false, "ÉXITO: Cancelación Exitosa");
  418. }
  419. public function deleteArtitleByRequestLine(Request $request) {
  420. $validator = Validator::make($request->all(), [
  421. 'LINE_NUMBER' => 'required|string',
  422. 'USER' => 'required|string',
  423. 'ID_SELECTED' => 'required|string',
  424. 'REQUEST_LINE' => 'required|string',
  425. ]);
  426. if ($validator->fails()) {
  427. return $this->responseController->makeResponse(
  428. true,
  429. "ERR_REQUESTLINE_DEL000: Se encontraron uno o más errores.",
  430. $this->responseController->makeErrors($validator->errors()->messages()),
  431. 401
  432. );
  433. }
  434. DB::beginTransaction();
  435. $requestData = $request->all();
  436. try {
  437. $user = $this->encController->decrypt($requestData['USER']);
  438. } catch (\Throwable $th) {
  439. DB::rollBack();
  440. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL001: Ocurrió un error al obtener el usuario.", $th->getMessage(), 500);
  441. }
  442. try {
  443. $idSelected = $this->encController->decrypt($requestData['ID_SELECTED']);
  444. } catch (\Throwable $th) {
  445. DB::rollBack();
  446. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL002: Ocurrió un error al obtener el artículo seleccionado.", $th->getMessage(), 500);
  447. }
  448. try {
  449. $idRequestLine = $this->encController->decrypt($requestData['REQUEST_LINE']);
  450. } catch (\Throwable $th) {
  451. DB::rollBack();
  452. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL003: Ocurrió un error al obtener la línea de solicitud de compra.", $th->getMessage(), 500);
  453. }
  454. try {
  455. $validateRequestLine = DB::table('S002V01TLINE')
  456. ->where('LINE_IDLI', '=', $idRequestLine)
  457. ->where('LINE_ESTA', '=', 'En espera')
  458. ->where('LINE_NULI', '=', $requestData['LINE_NUMBER'])
  459. ->exists();
  460. } catch (\Throwable $th) {
  461. DB::rollBack();
  462. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL004: Ocurrió un error al verificar que exista la línea de solicitud de compra.", $th->getMessage(), 500);
  463. }
  464. if (!$validateRequestLine) {
  465. DB::rollBack();
  466. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL005: La línea de solicitud no existe o tiene un estado más avanzado.", [], 500);
  467. }
  468. try {
  469. $validateArtitleSelected = DB::table('S002V01TARSE')
  470. ->where('ARSE_IDAS', '=', $idSelected)
  471. ->where('ARSE_IDLI', '=', $idRequestLine)
  472. ->where('ARSE_ESTA', '=', 'Activo')
  473. ->where('ARSE_NULI', '=', $requestData['LINE_NUMBER'])
  474. ->exists();
  475. } catch (\Throwable $th) {
  476. DB::rollBack();
  477. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL005: Ocurrió un error al verificar que exista el artículo seleccionado.", $th->getMessage(), 500);
  478. }
  479. if (!$validateArtitleSelected) {
  480. DB::rollBack();
  481. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL006: El artículo seleccionado no existe en la línea de solicitud de compra.", [], 500);
  482. }
  483. try {
  484. $countDeletArtitle = DB::table('S002V01TARSE')
  485. ->where('ARSE_IDAS', '=', $idSelected)
  486. ->where('ARSE_IDLI', '=', $idRequestLine)
  487. ->where('ARSE_ESTA', '=', 'Eliminado')
  488. ->where('ARSE_NULI', '=', $requestData['LINE_NUMBER'])
  489. ->count();
  490. } catch (\Throwable $th) {
  491. DB::rollBack();
  492. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL007: Ocurrió un error al verificar que exista el artículo seleccionado.", $th->getMessage(), 500);
  493. }
  494. try {
  495. $validateUpdate = DB::table('S002V01TARSE')
  496. ->where('ARSE_IDAS', '=', $idSelected)
  497. ->where('ARSE_IDLI', '=', $idRequestLine)
  498. ->where('ARSE_ESTA', '=', 'Activo')
  499. ->where('ARSE_NULI', '=', $requestData['LINE_NUMBER'])
  500. ->update([
  501. 'ARSE_IDAS' => (999 - intval($countDeletArtitle + 1)),
  502. 'ARSE_ESTA' => 'Eliminado',
  503. 'ARSE_USMO' => $user,
  504. 'ARSE_FEMO' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  505. 'ARSE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  506. ]);
  507. } catch (\Throwable $th) {
  508. DB::rollBack();
  509. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL008: Ocurrió un error al eliminar el artículo.", $th->getMessage(), 500);
  510. }
  511. if (!$validateUpdate) {
  512. DB::rollBack();
  513. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL009: No se pudo eliminar el archivo seleccionado.", [], 500);
  514. }
  515. try {
  516. $getArtitleSelectedActives = DB::table('S002V01TARSE')
  517. ->where('ARSE_IDLI', '=', $idRequestLine)
  518. ->where('ARSE_ESTA', '=', 'Activo')
  519. ->where('ARSE_NULI', '=', $requestData['LINE_NUMBER'])
  520. ->get();
  521. } catch (\Throwable $th) {
  522. DB::rollBack();
  523. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL010: Ocurrió un error al verificar que exista el artículo seleccionado.", $th->getMessage(), 500);
  524. }
  525. $arrArtitleSelectedActives = json_decode(json_encode($getArtitleSelectedActives), true);
  526. if (count($arrArtitleSelectedActives) > 0) {
  527. foreach ($arrArtitleSelectedActives as $keyArtitles => $artitleSelected) {
  528. try {
  529. $validateUpdateActives = DB::table('S002V01TARSE')
  530. ->where('ARSE_IDLI', '=', $idRequestLine)
  531. ->where('ARSE_ESTA', '=', 'Activo')
  532. ->where('ARSE_NULI', '=', $requestData['LINE_NUMBER'])
  533. ->update([
  534. 'ARSE_IDAS' => $keyArtitles + 1,
  535. 'ARSE_USMO' => $user,
  536. 'ARSE_FEMO' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  537. 'ARSE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  538. ]);
  539. } catch (\Throwable $th) {
  540. DB::rollBack();
  541. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL011: Ocurrió un error al reeligir el número de línea de los artículos", $th->getMessage(), 500);
  542. }
  543. if (!$validateUpdateActives) {
  544. DB::rollBack();
  545. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL012: No se pudo reeligir el número de línea de los artículos.", [], 500);
  546. }
  547. }
  548. } else {
  549. try {
  550. $validateUpdate = DB::table('S002V01TLINE')
  551. ->where('LINE_IDLI', '=', $idRequestLine)
  552. // ->where('LINE_ESTA', '=', 'En espera')
  553. ->where('LINE_NULI', '=', $requestData['LINE_NUMBER'])
  554. ->update([
  555. 'LINE_ESTA' => 'Cancelado',
  556. 'LINE_USMO' => $user,
  557. 'LINE_FEMO' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  558. 'LINE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  559. ]);
  560. } catch (\Throwable $th) {
  561. DB::rollBack();
  562. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL013: Ocurrió un error al eliminar el artículo.", $th->getMessage(), 500);
  563. }
  564. if (!$validateUpdate) {
  565. DB::rollBack();
  566. return $this->responseController->makeResponse(true, "ERR_REQUESTLINE_DEL014: No se pudo eliminar el archivo seleccionado.", [], 500);
  567. }
  568. }
  569. DB::commit();
  570. return $this->responseController->makeResponse(false, "ÉXITO: Eliminación Correcta");
  571. }
  572. public function updateStatusRequestLine(Request $request) {
  573. $validator = Validator::make($request->all(), [
  574. 'NUMERO_SOLICITUD' => 'required|string',
  575. 'ESTADO' => 'required|string',
  576. 'USUARIO' => 'required|string',
  577. 'METODO_PAGO' => 'required|string',
  578. 'NUMERO_LINEA' => 'required|string',
  579. // 'COMENTARIOS' => 'string',
  580. ]);
  581. if ($validator->fails()) {
  582. return $this->responseController->makeResponse(
  583. true,
  584. "ERR_ORDER_UPST000: Se encontraron uno o más errores.",
  585. $this->responseController->makeErrors($validator->errors()->messages()),
  586. 401
  587. );
  588. }
  589. DB::beginTransaction();
  590. $requestData = $request->all();
  591. try {
  592. $user = $this->encController->decrypt($requestData['USUARIO']);
  593. } catch (\Throwable $th) {
  594. DB::rollBack();
  595. return $this->responseController->makeResponse(true, "ERR_ORDER_UPST001: Ocurrió un error al obtener el usuario.", [], 500);
  596. }
  597. $arrUpdate = [
  598. 'LINE_ESTA' => $requestData['ESTADO'],
  599. 'LINE_IDME' => $requestData['METODO_PAGO'],
  600. 'LINE_COME' => $requestData['COMENTARIOS'] === '' ? null : $requestData['COMENTARIOS'],
  601. 'LINE_USMO' => $user,
  602. 'LINE_FEMO' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  603. 'LINE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  604. ];
  605. try {
  606. $validateUpdate = DB::table('S002V01TLINE')
  607. ->where('LINE_IDLI', '=', $requestData['NUMERO_SOLICITUD'])
  608. ->where('LINE_NULI', '=', $requestData['NUMERO_LINEA'])
  609. ->update($arrUpdate);
  610. } catch (\Throwable $th) {
  611. DB::rollBack();
  612. return $this->responseController->makeResponse(true, "ERR_ORDER_UPST002: Ocurrió un error al modificar el estado.", $th->getMessage(), 500);
  613. }
  614. if ( !$validateUpdate ) {
  615. DB::rollBack();
  616. return $this->responseController->makeResponse(true, "ERR_ORDER_UPST003: No se pudo modificar el estado de la línea de solicitud.", [], 500);
  617. }
  618. DB::commit();
  619. return $this->responseController->makeResponse(false, "ÉXITO: Modificación de Estado Exitoso");
  620. }
  621. }