RequestLineController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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_ORDER_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_ORDER_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_ORDER_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_ORDER_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_ORDER_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_ORDER_REG005: No se encontró ningún registro de productos.", [], 500);
  67. }
  68. $arrResponseArti = [
  69. 'LINE_DESC' => $description,
  70. 'LINE_NULI' => $line,
  71. 'LINE_USRE' => $user,
  72. 'LINE_FERE' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  73. 'LINE_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  74. ];
  75. try {
  76. $idRegisterLine = DB::table('S002V01TLINE')->insertGetId($arrResponseArti);
  77. if ($idRegisterLine == null || $idRegisterLine <= 0) {
  78. DB::rollBack();
  79. return $this->responseController->makeResponse(true, "ERR_ORDER_REG006: Ocurrió un error al hacer la inserción en la base.", [], 500);
  80. }
  81. } catch (\Throwable $th) {
  82. DB::rollBack();
  83. return $this->responseController->makeResponse(true, "ERR_ORDER_REG007: Ocurrió un error al hacer la inserción en la base.", $th->getMessage(), 500);
  84. }
  85. $arrArtitles = json_decode($strArtitles);
  86. foreach ($arrArtitles as $keyArtitles => $artitles) {
  87. $idArtitle = $artitles->idArtitle;
  88. $arrProducts = $artitles->content;
  89. $idProvider = $artitles->idProvider;
  90. foreach ($arrProducts as $keyProducts => $products) {
  91. $arrResponseProd = [
  92. 'ARSE_IDLI' => $idRegisterLine,
  93. 'ARSE_IDAR' => $idArtitle,
  94. 'ARSE_NUPR' => $idProvider,
  95. 'ARSE_IDIN' => $products->NUMERO_INFORMACION,
  96. 'ARSE_CANT' => $products->CANTIDAD,
  97. 'ARSE_PRTO' => $products->CANTIDAD * $products->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. try {
  104. $idRegisterArtitle = DB::table('S002V01TARSE')->insertGetId($arrResponseProd);
  105. if ($idRegisterArtitle == null || $idRegisterArtitle <= 0) {
  106. DB::rollBack();
  107. return $this->responseController->makeResponse(true, "ERR_ORDER_REG008: Ocurrió un error al hacer la inserción en la base.", [], 500);
  108. }
  109. } catch (\Throwable $th) {
  110. DB::rollBack();
  111. return $this->responseController->makeResponse(true, "ERR_ORDER_REG009: Ocurrió un error al hacer la inserción en la base.", $th->getMessage(), 500);
  112. }
  113. }
  114. }
  115. DB::commit();
  116. return $this->responseController->makeResponse(false, "ÉXITO: Registro correcto");
  117. }
  118. // Se obtienen todas las líneas de solicitud
  119. public function getAllRequestLines($line){
  120. try {
  121. $arrRequestLines = DB::table('S002V01TLINE')
  122. ->where("LINE_NULI", "=", $line)
  123. ->get([
  124. 'LINE_IDLI AS ID_LINEA_SOLICITUD',
  125. 'LINE_DESC AS DESCRIPCION',
  126. 'LINE_ESTA AS ESTADO',
  127. 'LINE_FERE AS FECHA_REGISTRA',
  128. 'LINE_FEMO AS FECHA_MODIFICA',
  129. 'LINE_USRE AS USUARIO_MODIFICA',
  130. 'LINE_USRE AS USUARIO_MODIFICA'
  131. ]);
  132. } catch (\Throwable $th) {
  133. return $this->responseController->makeResponse(true, "ERR_ORDER_GET000: No se pudo realizar la consulta a la base.", [], 500);
  134. }
  135. foreach ($arrRequestLines as $keyRequest => $requestLines) {
  136. $idRequestLine = $requestLines->ID_LINEA_SOLICITUD;
  137. try {
  138. $countData = DB::table('S002V01TARSE')->where('ARSE_IDLI', '=', $idRequestLine)->count();
  139. } catch (\Throwable $th) {
  140. return $this->responseController->makeResponse(true, "ERR_ORDER_GET001: No se pudo realizar la consulta a la base", [], 500);
  141. }
  142. $requestLines->CANTIDAD = $countData;
  143. }
  144. return $this->responseController->makeResponse(false, "ÉXITO", $arrRequestLines);
  145. }
  146. // Se obtiene una línea de solicitud en específico
  147. public function getRequestLine($encIdRequestLine, $line){
  148. try {
  149. $idRequestLine = $this->encController->decrypt($encIdRequestLine);
  150. } catch (\Throwable $th) {
  151. return $this->responseController->makeResponse(true, "ERR_ORDER_GET000: Ocurrió un error al obtener el contenido.", [], 500);
  152. }
  153. try {
  154. $arrRequestLines = DB::table('S002V01TLINE')
  155. ->where("LINE_NULI", "=", $line)
  156. ->where("LINE_IDLI", "=", $idRequestLine)
  157. ->first([
  158. 'LINE_IDLI AS ID_LINEA_SOLICITUD',
  159. 'LINE_DESC AS DESCRIPCION',
  160. 'LINE_ESTA AS ESTADO',
  161. 'LINE_FERE AS FECHA_REGISTRA',
  162. 'LINE_FEMO AS FECHA_MODIFICA',
  163. 'LINE_USRE AS USUARIO_MODIFICA',
  164. 'LINE_USRE AS USUARIO_MODIFICA'
  165. ]);
  166. } catch (\Throwable $th) {
  167. return $this->responseController->makeResponse(true, "ERR_ORDER_GET001: No se pudo realizar la consulta a la base.", [], 500);
  168. }
  169. try {
  170. $arrProducts = DB::table('S002V01TARSE')
  171. ->where("ARSE_NULI", "=", $line)
  172. ->where('ARSE_IDLI', '=', $idRequestLine)
  173. ->where('PROV_ESTA', '=', 'Activo')
  174. ->join('S002V01TPROV', 'ARSE_NUPR', '=', 'PROV_NUPR')
  175. ->join('S002V01TINAR', 'ARSE_IDIN', '=', 'INAR_IDIN')
  176. ->get([
  177. 'ARSE_IDAS AS NUMERO_ARTI_SELE',
  178. 'ARSE_IDAR AS NUMERO_ARTICULO',
  179. 'ARSE_NUPR AS NUMERO_PRODUCTO',
  180. 'INAR_MODE AS MODELO',
  181. 'INAR_CODI AS CODIGO',
  182. 'INAR_CARA AS CARACTERISTICAS',
  183. 'ARSE_CANT AS CANTIDAD',
  184. 'INAR_PREC AS PRECIO_UNITARIO',
  185. 'INAR_MONE AS MONEDA',
  186. 'ARSE_PRTO AS PRECIO_TOTAL',
  187. 'ARSE_ESTA AS ESTADO',
  188. 'ARSE_USRE AS USUARIO_REGISTRA',
  189. 'ARSE_FERE AS FECHA_REGISTRA',
  190. 'ARSE_USMO AS USUARIO_MODIFICA',
  191. 'ARSE_FEMO AS FECHA_MODIFICA',
  192. 'PROV_NUPR AS NUMERO_PROVEEDOR',
  193. 'PROV_NOCO AS PROVEEDOR',
  194. 'PROV_ESTA AS ESTADO_PROVEEDOR'
  195. ]);
  196. foreach ($arrProducts as $productos) {
  197. if ($productos == 'Eliminado') {
  198. return $this->responseController->makeResponse(true, "ERR_ORDER_GET002: Existe un usuario en la solicitud que se encuentra eliminado", [], 500);
  199. }
  200. }
  201. $arrRequestLines->PRODUCTOS = $arrProducts;
  202. } catch (\Throwable $th) {
  203. return $this->responseController->makeResponse(true, "ERR_ORDER_GET003: No se pudo realizar la consulta a la base.", [], 500);
  204. }
  205. return $this->responseController->makeResponse(false, "ÉXITO", $arrRequestLines);
  206. }
  207. // Se cancalan la linea de solicitud de compra
  208. public function cancelRequestLine(Request $request){
  209. $validator = Validator::make($request->all(), [
  210. 'line' => 'required|string',
  211. 'user' => 'required|string',
  212. 'idRequestLine' => 'required|string',
  213. ]);
  214. if ($validator->fails()) {
  215. return $this->responseController->makeResponse(
  216. true,
  217. "ERR_ORDER_CAN000: Se encontraron uno o más errores.",
  218. $this->responseController->makeErrors($validator->errors()->messages()),
  219. 401
  220. );
  221. }
  222. DB::beginTransaction();
  223. $requestData = $request->all();
  224. try {
  225. $user = $this->encController->decrypt($requestData['user']);
  226. } catch (\Throwable $th) {
  227. DB::rollBack();
  228. return $this->responseController->makeResponse(true, "ERR_ORDER_CAN001: Ocurrió un error al obtener el contenido.", [], 500);
  229. }
  230. try {
  231. $line = $this->encController->decrypt($requestData['line']);
  232. } catch (\Throwable $th) {
  233. DB::rollBack();
  234. return $this->responseController->makeResponse(true, "ERR_ORDER_CAN002: Ocurrió un error al obtener la línea.", [], 500);
  235. }
  236. try {
  237. $idRequestLine = $this->encController->decrypt($requestData['idRequestLine']);
  238. } catch (\Throwable $th) {
  239. DB::rollBack();
  240. return $this->responseController->makeResponse(true, "ERR_ORDER_CAN003: Ocurrió un error al obtener el identificador de la línea de solicitud de compra.", [], 500);
  241. }
  242. $currentDate = Carbon::now()->timezone('America/Mazatlan')->toDateTimeString();
  243. $objRequest = [
  244. 'LINE_ESTA' => 'Cancelado',
  245. 'LINE_USMO' => $user,
  246. 'LINE_FEMO' => $currentDate,
  247. 'LINE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  248. ];
  249. try {
  250. $response = DB::table('S002V01TLINE')->where('LINE_IDLI', '=', $idRequestLine)->update($objRequest);
  251. if ($response == null || $response <= 0) {
  252. DB::rollBack();
  253. return $this->responseController->makeResponse(true, "ERR_ORDER_CAN004: Ocurrió un error al hacer la inserción en la base.", [], 500);
  254. }
  255. } catch (\Throwable $th) {
  256. DB::rollBack();
  257. return $this->responseController->makeResponse(true, "ERR_ORDER_CAN005: No se pudo realizar la modificación a la base.", [], 500);
  258. }
  259. DB::commit();
  260. return $this->responseController->makeResponse(false, "ÉXITO: Cancelación Exitosa");
  261. }
  262. public function updateStatusRequestLine(Request $request) {
  263. $validator = Validator::make($request->all(), [
  264. 'NUMERO_SOLICITUD' => 'required|string',
  265. 'ESTADO' => 'required|string',
  266. 'USUARIO' => 'required|string',
  267. 'METODO_PAGO' => 'required|string',
  268. 'NUMERO_LINEA' => 'required|string',
  269. // 'COMENTARIOS' => 'string',
  270. ]);
  271. if ($validator->fails()) {
  272. return $this->responseController->makeResponse(
  273. true,
  274. "ERR_ORDER_UPST000: Se encontraron uno o más errores.",
  275. $this->responseController->makeErrors($validator->errors()->messages()),
  276. 401
  277. );
  278. }
  279. DB::beginTransaction();
  280. $requestData = $request->all();
  281. try {
  282. $user = $this->encController->decrypt($requestData['USUARIO']);
  283. } catch (\Throwable $th) {
  284. DB::rollBack();
  285. return $this->responseController->makeResponse(true, "ERR_ORDER_UPST001: Ocurrió un error al obtener el usuario.", [], 500);
  286. }
  287. $arrUpdate = [
  288. 'LINE_ESTA' => $requestData['ESTADO'],
  289. 'LINE_IDME' => $requestData['METODO_PAGO'],
  290. 'LINE_COME' => $requestData['COMENTARIOS'] === '' ? null : $requestData['COMENTARIOS'],
  291. 'LINE_USMO' => $user,
  292. 'LINE_FEMO' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  293. 'LINE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  294. ];
  295. try {
  296. $validateUpdate = DB::table('S002V01TLINE')
  297. ->where('LINE_IDLI', '=', $requestData['NUMERO_SOLICITUD'])
  298. ->where('LINE_NULI', '=', $requestData['NUMERO_LINEA'])
  299. ->update($arrUpdate);
  300. } catch (\Throwable $th) {
  301. DB::rollBack();
  302. return $this->responseController->makeResponse(true, "ERR_ORDER_UPST002: Ocurrió un error al modificar el estado.", $th->getMessage(), 500);
  303. }
  304. if ( !$validateUpdate ) {
  305. DB::rollBack();
  306. return $this->responseController->makeResponse(true, "ERR_ORDER_UPST003: No se pudo modificar el estado de la línea de solicitud.", [], 500);
  307. }
  308. DB::commit();
  309. return $this->responseController->makeResponse(false, "ÉXITO: Modificación de Estado Exitoso");
  310. }
  311. }