RequestLineController.php 32 KB

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