OrderController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. <?php
  2. /*
  3. Desarrollador: Ing. Jean Jairo Benitez Meza
  4. Ultima Modificación: 11/04/2023
  5. Módulo: Gestión de Adquisiciones
  6. */
  7. namespace App\Http\Controllers;
  8. use App\Http\Controllers\Controller;
  9. use App\Http\Controllers\ResponseController;
  10. use App\Http\Controllers\EncryptionController;
  11. use App\Http\Controllers\ResourcesController;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Carbon;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Validator;
  16. use Illuminate\Database\Query\JoinClause;
  17. class OrderController extends Controller {
  18. private $responseController;
  19. private $encController;
  20. private $resourcesController;
  21. public function __construct(){
  22. $this->responseController = new ResponseController();
  23. $this->encController = new EncryptionController();
  24. $this->resourcesController = new ResourcesController();
  25. }
  26. public function getOrders($line) {
  27. try {
  28. $arrOrders = DB::table('S002V01TORCO')
  29. ->where('ORCO_NULI', '=', $line)
  30. ->join('S002V01TDESP', 'DESP_NUDE', '=', 'ORCO_NUDE')
  31. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'ORCO_NUPR')
  32. ->join('S002V01TLINE', 'LINE_IDLI', '=', 'ORCO_IDLI')
  33. ->join('S002V01TMEPA', 'MEPA_IDME', '=', 'LINE_IDME')
  34. ->get([
  35. 'ORCO_NUOR', // Número de Orden
  36. 'PROV_NUPR', // Número de Proveedor
  37. 'PROV_NOCO', // Nombre Comercial del Proveedor
  38. 'DESP_NUDE', // Número de Despacho
  39. 'DESP_NOMB', // Nombre del Despacho
  40. 'LINE_IDLI', // Identificador de línea de solicitud de compra
  41. 'LINE_DESC', // Descripción de la línea de solicitud de compra
  42. 'LINE_IDME', // Métodos de Pago
  43. 'MEPA_NOMB', // Nombre del método de pago
  44. 'ORCO_COST', // Costo Total
  45. 'ORCO_MONE', // Moneda
  46. 'ORCO_PROP', // Proposito
  47. 'ORCO_ACTI', // Actividad
  48. 'ORCO_PROY', // Proyecto
  49. 'ORCO_ESTA', // Estado
  50. 'ORCO_USRE', // Usuario Registra
  51. 'ORCO_FERE', // Fecha Registra
  52. 'ORCO_USMO', // Usuario Modifica
  53. 'ORCO_FEMO', // Fecha Modifica
  54. ]);
  55. } catch ( \Throwable $th ) {
  56. return $this->responseController->makeResponse(true, "ERR_ORDER_GET000: Ocurrió un error al momento de obtener las órdenes de compra.", $th->getMessage(), 500);
  57. }
  58. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrOrders);
  59. }
  60. // Se crea una orden de compra
  61. public function createOrder(Request $request){
  62. $validator = Validator::make($request->all(), [
  63. 'NUMERO_SOLICITUD' => 'required|string',
  64. 'NUMERO_LINEA' => 'required|string',
  65. 'USUARIO' => 'required|string',
  66. 'ARR_ORDENES' => 'required|array',
  67. ]);
  68. if ($validator->fails()) {
  69. return $this->responseController->makeResponse(
  70. true,
  71. "ERR_ORDER_REG000: Se encontraron uno o más errores.",
  72. $this->responseController->makeErrors($validator->errors()->messages()),
  73. 401
  74. );
  75. }
  76. DB::beginTransaction();
  77. $requestData = $request->all();
  78. try {
  79. $user = $this->encController->decrypt($requestData['USUARIO']);
  80. } catch (\Throwable $th) {
  81. DB::rollBack();
  82. return $this->responseController->makeResponse(true, "ERR_ORDER_REG001: Ocurrió un error al obtener el usuario.", [], 500);
  83. }
  84. foreach ($requestData['ARR_ORDENES'] as $keyOrders => $orders) {
  85. $validator = Validator::make($orders, [
  86. 'NUMERO_PROVEEDOR' => 'required|string',
  87. 'PROVEEDOR' => 'required|string',
  88. 'PRECIO_TOTAL' => 'required|integer',
  89. 'MONEDA' => 'required|string',
  90. 'DESPACHO' => 'required|string',
  91. 'PROPOSITO' => 'required|string',
  92. // 'ACTIVIDAD' => '',
  93. // 'PROYECTO' => '',
  94. ]);
  95. if ($validator->fails()) {
  96. return $this->responseController->makeResponse(
  97. true,
  98. "ERR_ORDER_REG002: Se encontraron uno o más errores.",
  99. $this->responseController->makeErrors($validator->errors()->messages()),
  100. 401
  101. );
  102. }
  103. $arrInsert = [
  104. 'ORCO_NUPR' => $orders['NUMERO_PROVEEDOR'],
  105. 'ORCO_NUDE' => $orders['DESPACHO'],
  106. 'ORCO_IDLI' => $requestData['NUMERO_SOLICITUD'],
  107. 'ORCO_COST' => $orders['PRECIO_TOTAL'],
  108. 'ORCO_MONE' => $orders['MONEDA'],
  109. 'ORCO_PROP' => $orders['PROPOSITO'],
  110. 'ORCO_ACTI' => $orders['ACTIVIDAD'] === '' ? null : $orders['ACTIVIDAD'],
  111. 'ORCO_PROY' => $orders['PROYECTO'] === '' ? null : $orders['PROYECTO'],
  112. 'ORCO_NULI' => $requestData['NUMERO_LINEA'],
  113. 'ORCO_USRE' => $user,
  114. 'ORCO_FERE' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  115. 'ORCO_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  116. ];
  117. try {
  118. $orderNumber = DB::table('S002V01TORCO')->insertGetId($arrInsert);
  119. } catch (\Throwable $th) {
  120. DB::rollBack();
  121. return $this->responseController->makeResponse(true, "ERR_ORDER_REG003: Ocurrió un error al momento de insertar los datos en la base de datos.", $th->getMessage(), 500);
  122. }
  123. if ( !$orderNumber ) {
  124. DB::rollBack();
  125. return $this->responseController->makeResponse(true, "ERR_ORDER_REG004: Ocurrió un error al insertar los datos en la base de datos.", [], 500);
  126. }
  127. $arrUpdate = [
  128. 'LINE_ESTA' => 'En OC',
  129. 'LINE_USMO' => $user,
  130. 'LINE_FEMO' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  131. 'LINE_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  132. ];
  133. try {
  134. $validateInsert = DB::table('S002V01TLINE')
  135. ->where('LINE_IDLI', '=', $requestData['NUMERO_SOLICITUD'])
  136. ->where('LINE_NULI', '=', $requestData['NUMERO_LINEA'])
  137. ->update($arrUpdate);
  138. } catch (\Throwable $th) {
  139. DB::rollBack();
  140. return $this->responseController->makeResponse(true, "ERR_ORDER_REG005: Ocurrió un error al momento de insertar los datos en la base de datos.", [], 500);
  141. }
  142. if ( !$validateInsert ) {
  143. DB::rollBack();
  144. return $this->responseController->makeResponse(true, "ERR_ORDER_REG006: Ocurrió un error al insertar los datos en la base de datos.", [], 500);
  145. }
  146. $arrInsertState = [
  147. 'HIOR_NUOR' => $orderNumber,
  148. 'HIOR_POEN' => 10,
  149. 'HIOR_NULI' => $requestData['NUMERO_LINEA'],
  150. 'HIOR_USRE' => $user,
  151. 'HIOR_FERE' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  152. 'HIOR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  153. ];
  154. try {
  155. $validateInsertUpdate = DB::table('S002V01THIOR')->insert($arrInsertState);
  156. } catch (\Throwable $th) {
  157. DB::rollBack();
  158. return $this->responseController->makeResponse(true, "ERR_ORDER_REG007: Ocurrió un error al momento de insertar los datos en el historial de estados.", [], 500);
  159. }
  160. if ( !$validateInsertUpdate ) {
  161. DB::rollBack();
  162. return $this->responseController->makeResponse(true, "ERR_ORDER_REG008: Ocurrió un error al insertar los datos del historial en la base de datos.", [], 500);
  163. }
  164. }
  165. DB::commit();
  166. return $this->responseController->makeResponse(false, "ÉXITO: Generación de Órdenes Exitosas");
  167. }
  168. public function getOrderByNumber($order, $provider, $line){
  169. try {
  170. $order = $this->encController->decrypt($order);
  171. } catch (\Throwable $th) {
  172. DB::rollBack();
  173. return $this->responseController->makeResponse(true, "ERR_ORDER_GET000: Ocurrió un error al obtener el usuario.", [], 500);
  174. }
  175. try {
  176. $provider = $this->encController->decrypt($provider);
  177. } catch (\Throwable $th) {
  178. DB::rollBack();
  179. return $this->responseController->makeResponse(true, "ERR_ORDER_GET001: Ocurrió un error al obtener el usuario.", [], 500);
  180. }
  181. try {
  182. $orders = DB::table('S002V01TORCO')
  183. ->where('ORCO_NULI', '=', $line)
  184. ->where('ORCO_NUOR', '=', $order)
  185. ->where('ORCO_NUPR', '=', $provider)
  186. ->join('S002V01TDESP', 'DESP_NUDE', '=', 'ORCO_NUDE')
  187. ->join('S002V01TPROV', 'PROV_NUPR', '=', 'ORCO_NUPR')
  188. ->join('S002V01TLINE', 'LINE_IDLI', '=', 'ORCO_IDLI')
  189. ->join('S002V01TMEPA', 'MEPA_IDME', '=', 'LINE_IDME')
  190. ->first([
  191. 'ORCO_NUOR', // Número de Orden
  192. 'PROV_NUPR', // Número de Proveedor
  193. 'PROV_NOCO', // Nombre Comercial del Proveedor
  194. 'DESP_NUDE', // Número de Despacho
  195. 'DESP_NOMB', // Nombre del Despacho
  196. 'DESP_CALL', // Calle
  197. 'DESP_NUEX', // Número Exterior
  198. 'DESP_NUIN', // Número Interior
  199. 'DESP_COPO', // Código Postal
  200. 'DESP_COLO', // Colonia
  201. 'DESP_MUNI', // Municipio
  202. 'DESP_ENTI', // Entidad
  203. 'DESP_PAIS', // Pais
  204. 'LINE_IDLI', // Identificador de línea de solicitud de compra
  205. 'LINE_DESC', // Descripción de la línea de solicitud de compra
  206. 'LINE_IDME', // Métodos de Pago
  207. 'MEPA_NOMB', // Nombre del método de pago
  208. 'ORCO_COST', // Costo Total
  209. 'ORCO_MONE', // Moneda
  210. 'ORCO_PROP', // Proposito
  211. 'ORCO_ACTI', // Actividad
  212. 'ORCO_PROY', // Proyecto
  213. 'ORCO_ESTA', // Estado
  214. 'ORCO_USRE', // Usuario Registra
  215. 'ORCO_FERE', // Fecha Registra
  216. 'ORCO_USMO', // Usuario Modifica
  217. 'ORCO_FEMO', // Fecha Modifica
  218. ]);
  219. } catch ( \Throwable $th ) {
  220. return $this->responseController->makeResponse(true, "ERR_ORDER_GET002: Ocurrió un error al momento de obtener las órdenes de compra.", $th->getMessage(), 500);
  221. }
  222. if ( !empty($orders) ) {
  223. try {
  224. $arrArtitleSelected = DB::table('S002V01TARSE')
  225. ->where('ARSE_IDLI', '=', $orders->LINE_IDLI)
  226. ->where('ARSE_NUPR', '=', $provider)
  227. ->where('ARSE_NULI', '=', $line)
  228. ->where('ARSE_ESTA', '=', 'Activo')
  229. ->join('S002V01TARTI', 'ARTI_IDAR', '=', 'ARSE_IDAR')
  230. ->join('S002V01TINAR', 'INAR_IDIN', '=', 'ARSE_IDIN')
  231. ->join('S002V01TDEAR', 'DEAR_IDDE', '=', 'INAR_IDDE')
  232. ->get([
  233. 'ARSE_IDAS', // Identificador de artículos seleccionados
  234. 'ARTI_IDAR', // Identificador del artículo
  235. 'ARTI_IDFA', // Identificador de la familia
  236. 'ARTI_IDSU', // Identificador de la subfamilia
  237. 'ARTI_CODI', // Código del artículo
  238. 'ARTI_NOMB', // Nombre del artículo
  239. 'ARSE_NUPR', // Número de Identificador del Proveedor
  240. 'ARSE_IDIN', // Número de Identificador de Información
  241. 'ARSE_CANT', // Cantidad seleccionada
  242. 'ARSE_PRTO', // Precio total
  243. 'ARSE_USRE', // Usuario registra
  244. 'ARSE_FERE', // Fecha registra
  245. 'ARSE_USMO', // Usuario modifica
  246. 'ARSE_FEMO', // Fecha modifica
  247. 'INAR_CODI', // Código del artículo
  248. 'INAR_MODE', // Modelo del artículo
  249. 'INAR_MONE', // Moneda
  250. 'INAR_PREC', // Precio
  251. 'INAR_CARA', // Características
  252. 'DEAR_IDDE', // Identificador de la descripción del artículo
  253. 'DEAR_DESC', // Descripción del artículo
  254. 'DEAR_CARA', // Características del proveedor
  255. 'DEAR_COWE', // Compra Web
  256. 'DEAR_IMAG', // Imágen del artículo
  257. 'DEAR_IDUN', // Identificador de la unidad
  258. 'DEAR_NUPR', // Número del Proveedor
  259. 'DEAR_IDAR', // Identificador del artícul
  260. ]);
  261. } catch (\Throwable $th) {
  262. return $this->responseController->makeResponse(true, "ERR_ORDER_GET003: Ocurrió un error al momento de obtener la información de la línea de solicitud de compra", $th->getMessage(), 500);
  263. }
  264. $orders->PRODUCTS = $arrArtitleSelected;
  265. try {
  266. $arrStateOrder = DB::table('S002V01THIOR')
  267. ->where('HIOR_NUOR', '=', $order)
  268. ->where('HIOR_ESTA', '=', 'Activo')
  269. ->where('HIOR_NULI', '=', $line)
  270. ->get([
  271. 'HIOR_IDHO', // Identificador del historial de órdenes
  272. 'HIOR_NUOR', // Número de orden de compra
  273. 'HIOR_ESOR', // Estado de la orden
  274. 'HIOR_EVI1', // Evidencia 1
  275. 'HIOR_EVI2', // Evidencia 2
  276. 'HIOR_DESC', // Descripción
  277. 'HIOR_POEN', // Porcentaje de entrega
  278. 'HIOR_USRE', // Usuario entrega
  279. 'HIOR_FERE', // Fecha entrega
  280. 'HIOR_USMO', // Usuario modifica
  281. 'HIOR_FEMO', // Fecha modifica
  282. ]);
  283. } catch (\Throwable $th) {
  284. return $this->responseController->makeResponse(true, "ERR_ORDER_GET004: Ocurrió un error al momento de obtener la información del historial de orden de compra", $th->getMessage(), 500);
  285. }
  286. $orders->STATES = $arrStateOrder;
  287. }
  288. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $orders);
  289. }
  290. public function getHistoryStateOrder($order, $line) {
  291. try {
  292. $objHistoryState = DB::table('S002V01THIOR')
  293. ->where('HIOR_NUOR', '=', $order)
  294. ->where('HIOR_NULI', '=', $line)
  295. ->where('HIOR_ESTA', '=', 'Activo')
  296. ->first([
  297. 'HIOR_IDHO', // Identificador de la historia de la orden
  298. 'HIOR_ESOR', // Estado de la orden
  299. 'HIOR_DESC', // Descripción del estado de la orden
  300. 'HIOR_EVI1', // Evidencia 1
  301. 'HIOR_EVI2', // Evidencia 2
  302. 'HIOR_POEN', // Porcentaje de entrega
  303. ]);
  304. } catch (\Throwable $th) {
  305. //throw $th;
  306. }
  307. }
  308. public function updateStateHistory(Request $request) {
  309. $validator = Validator::make($request->all(), [
  310. 'ESTADO' => 'required|string',
  311. 'DESCRIPCION' => 'required|string',
  312. 'EVIDENCIA1' => 'required|string',
  313. 'NOMBRE_IMAGEN1' => 'string',
  314. // 'EVIDENCIA2' => 'string',
  315. // 'NOMBRE_IMAGEN2' => 'string',
  316. 'PORCENTAJE' => 'required|integer',
  317. 'NUMERO_ORDEN' => 'required|string',
  318. 'NUMERO_LINEA' => 'required|string',
  319. 'USUARIO' => 'required|string',
  320. ]);
  321. if ($validator->fails()) {
  322. return $this->responseController->makeResponse(
  323. true,
  324. "ERR_ORDERSTATE_REG000: Se encontraron uno o más errores.",
  325. $this->responseController->makeErrors($validator->errors()->messages()),
  326. 401
  327. );
  328. }
  329. DB::beginTransaction();
  330. $requestData = $request->all();
  331. $imageEvidence1 = null;
  332. $arrResponseImage = $this->resourcesController->saveImage( $requestData['EVIDENCIA1'], '/app/public/GEAD/FO', $requestData['NOMBRE_IMAGEN1'], $requestData['NUMERO_LINEA'] );
  333. if ( $arrResponseImage['error'] ) {
  334. DB::rollBack();
  335. return $this->responseController->makeResponse(true, $arrResponseImage['msg'], $arrResponseImage['response'], 500);
  336. }
  337. $imageEvidence1 = $arrResponseImage['response'];
  338. $imageEvidence2 = null;
  339. if ( $requestData['EVIDENCIA2'] != '' && $requestData['EVIDENCIA2'] != null ) {
  340. $arrResponseImage = $this->resourcesController->saveImage( $requestData['EVIDENCIA2'], '/app/public/GEAD/FO', $requestData['NOMBRE_IMAGEN2'], $requestData['NUMERO_LINEA'] );
  341. if ( $arrResponseImage['error'] ) {
  342. DB::rollBack();
  343. return $this->responseController->makeResponse(true, $arrResponseImage['msg'], $arrResponseImage['response'], 500);
  344. }
  345. $imageEvidence2 = $arrResponseImage['response'];
  346. }
  347. try {
  348. $user = $this->encController->decrypt($requestData['USUARIO']);
  349. } catch (\Throwable $th) {
  350. DB::rollBack();
  351. return $this->responseController->makeResponse(true, "ERR_ORDERSTATE_REG001: Ocurrió un error al obtener el usuario.", [], 500);
  352. }
  353. $arrInsert = [
  354. 'HIOR_NUOR' => $requestData['NUMERO_ORDEN'],
  355. 'HIOR_ESOR' => $requestData['ESTADO'],
  356. 'HIOR_DESC' => $requestData['DESCRIPCION'],
  357. 'HIOR_EVI1' => $imageEvidence1,
  358. 'HIOR_EVI2' => $imageEvidence2,
  359. 'HIOR_POEN' => $requestData['PORCENTAJE'],
  360. 'HIOR_NULI' => $requestData['NUMERO_LINEA'],
  361. 'HIOR_USRE' => $user,
  362. 'HIOR_FERE' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  363. 'HIOR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  364. ];
  365. try {
  366. $validateInsert = DB::table('S002V01THIOR')->insert($arrInsert);
  367. } catch (\Throwable $th) {
  368. DB::rollBack();
  369. return $this->responseController->makeResponse(true, "ERR_ORDERSTATE_REG002: Ocurrió un error al momento de insertar los datos.", [], 500);
  370. }
  371. if ( !$validateInsert ) {
  372. DB::rollBack();
  373. return $this->responseController->makeResponse(true, "ERR_ORDERSTATE_REG003: No se pudo insertar los datos a la tabla.", [], 500);
  374. }
  375. $arrUpdate = [
  376. 'ORCO_ESTA' => $requestData['ESTADO'],
  377. 'ORCO_USMO' => $user,
  378. 'ORCO_FEMO' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  379. ];
  380. try {
  381. $validateUpdate = DB::table('S002V01TORCO')
  382. ->where('ORCO_NUOR', '=', $requestData['NUMERO_ORDEN'])
  383. ->where('ORCO_NULI', '=', $requestData['NUMERO_LINEA'])
  384. ->update($arrUpdate);
  385. } catch (\Throwable $th) {
  386. DB::rollBack();
  387. return $this->responseController->makeResponse(true, "ERR_ORDERSTATE_REG004: Ocurrió un error al momento de modificar los datos.", [], 500);
  388. }
  389. if ( !$validateUpdate ) {
  390. DB::rollBack();
  391. return $this->responseController->makeResponse(true, "ERR_ORDERSTATE_REG005: No se pudo modificar los datos a la tabla.", [], 500);
  392. }
  393. DB::commit();
  394. return $this->responseController->makeResponse(false, "ÉXITO: Modificación de Estado Exitoso");
  395. }
  396. public function reorderArtitles(Request $request) {
  397. $validator = Validator::make($request->all(), [
  398. 'ORDEN' => 'required|string',
  399. 'DESCRIPCION' => 'required|string',
  400. 'USUARIO' => 'required|string',
  401. 'NUMERO_LINEA' => 'required|string',
  402. ]);
  403. if ($validator->fails()) {
  404. return $this->responseController->makeResponse(
  405. true,
  406. "ERR_REORDER_REG000: Se encontraron uno o más errores.",
  407. $this->responseController->makeErrors($validator->errors()->messages()),
  408. 401
  409. );
  410. }
  411. DB::beginTransaction();
  412. $requestData = $request->all();
  413. try {
  414. $user = $this->encController->decrypt($requestData['USUARIO']);
  415. } catch (\Throwable $th) {
  416. DB::rollBack();
  417. return $this->responseController->makeResponse(true, "ERR_REORDER_REG001: Ocurrió un error al obtener el usuario.", [], 500);
  418. }
  419. try {
  420. $order = $this->encController->decrypt($requestData['ORDEN']);
  421. } catch (\Throwable $th) {
  422. DB::rollBack();
  423. return $this->responseController->makeResponse(true, "ERR_REORDER_REG002: Ocurrió un error al obtener el número de orden.", [], 500);
  424. }
  425. try {
  426. $idRegisterLine = DB::table('S002V01TLINE')->insertGetId([
  427. 'LINE_DESC' => $request['DESCRIPCION'],
  428. 'LINE_NULI' => $request['NUMERO_LINEA'],
  429. 'LINE_USRE' => $user,
  430. 'LINE_FERE' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  431. 'LINE_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  432. ]);
  433. } catch (\Throwable $th) {
  434. DB::rollBack();
  435. return $this->responseController->makeResponse(true, "ERR_REORDER_REG003: Ocurrió un error al hacer la inserción en la base.", $th->getMessage(), 500);
  436. }
  437. if ( !$idRegisterLine ) {
  438. DB::rollBack();
  439. return $this->responseController->makeResponse(true, "ERR_REORDER_REG004: Ocurrió un error al hacer la inserción en la base.", [], 500);
  440. }
  441. try {
  442. $arrArtitles = DB::table('S002V01TORCO')
  443. ->where('ORCO_NUOR', '=', $order)
  444. ->join('S002V01TARSE', function (JoinClause $join) {
  445. $join->on('ARSE_IDLI', '=', 'ORCO_IDLI')
  446. ->on('ARSE_NUPR', '=', 'ORCO_NUPR');
  447. })
  448. ->get([
  449. 'ARSE_IDAR', // Identificador del artículo
  450. 'ARSE_NUPR', // Número del proveedor
  451. 'ARSE_IDIN', // Identificador de la información del artículo
  452. 'ARSE_CANT', // Cantidad
  453. 'ARSE_PRTO', // Precio total
  454. ]);
  455. } catch (\Throwable $th) {
  456. DB::rollBack();
  457. return $this->responseController->makeResponse(true, "ERR_REORDER_REG005: No se pudo obtener la información de la orden.", $th->getMessage(), 500);
  458. }
  459. if ( $arrArtitles->isEmpty() ) {
  460. DB::rollBack();
  461. return $this->responseController->makeResponse(true, "ERR_REORDER_REG006: La orden no tiene artículos.", [], 500);
  462. }
  463. foreach ($arrArtitles as $keyArtitle => $artitle) {
  464. try {
  465. $validateInsert = DB::table('S002V01TARSE')->insert([
  466. 'ARSE_IDLI' => $idRegisterLine,
  467. 'ARSE_IDAR' => $artitle->ARSE_IDAR,
  468. 'ARSE_NUPR' => $artitle->ARSE_NUPR,
  469. 'ARSE_IDIN' => $artitle->ARSE_IDIN,
  470. 'ARSE_CANT' => $artitle->ARSE_CANT,
  471. 'ARSE_PRTO' => $artitle->ARSE_PRTO,
  472. 'ARSE_NULI' => $request['NUMERO_LINEA'],
  473. 'ARSE_USRE' => $user,
  474. 'ARSE_FERE' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  475. 'ARSE_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  476. ]);
  477. } catch (\Throwable $th) {
  478. DB::rollBack();
  479. return $this->responseController->makeResponse(true, "ERR_REORDER_REG007: Ocurrió un error al hacer la inserción en la base.", $th->getMessage(), 500);
  480. }
  481. if ( !$validateInsert ) {
  482. DB::rollBack();
  483. return $this->responseController->makeResponse(true, "ERR_REORDER_REG008: Ocurrió un error al hacer la inserción en la base.", [], 500);
  484. }
  485. }
  486. DB::commit();
  487. return $this->responseController->makeResponse(false, "ÉXITO: Artículos Reordenados Exitosamente");
  488. }
  489. }