IssueTrackingController.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\ResponseController;
  4. use App\Http\Controllers\EncryptionController;
  5. use App\Http\Controllers\ResourcesController;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Carbon;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Validator;
  10. class IssueTrackingController extends Controller
  11. {
  12. private $responseController;
  13. private $encController;
  14. private $resourcesController;
  15. public function __construct(){
  16. $this->responseController = new ResponseController();
  17. $this->encController = new EncryptionController();
  18. $this->resourcesController = new ResourcesController();
  19. }
  20. public function reportProblem(Request $request) {
  21. $validator = Validator::make($request->all(),[
  22. 'ORDER' => 'required|string',
  23. 'TIPO' => 'required|string',
  24. 'DESCRIPCION' => 'required|string',
  25. 'NOMBRE_EVIDENCIA1' => 'required|string',
  26. 'EVIDENCIA1' => 'required|string',
  27. 'NOMBRE_EVIDENCIA2' => 'string',
  28. 'EVIDENCIA2' => 'string',
  29. 'USUARIO' => 'required|string',
  30. 'NUMERO_LINEA' => 'required|integer',
  31. ]);
  32. if ($validator->fails()) {
  33. return $this->responseController->makeResponse(
  34. true,
  35. "ERR_ISSUE_TRACKING_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. // Se obtiene el usuario encriptado
  43. try {
  44. $user = $this->encController->decrypt($requestData['USUARIO']);
  45. } catch (\Throwable $th) {
  46. DB::rollBack();
  47. return $this->responseController->makeResponse(true, "ERR_ISSUE_TRACKING_REG001: Ocurrió un error al obtener el usuario.", [], 500);
  48. }
  49. // Se obtiene el número de orden enctriptado
  50. try {
  51. $orderNumber = $this->encController->decrypt($requestData['ORDER']);
  52. } catch (\Throwable $th) {
  53. DB::rollBack();
  54. return $this->responseController->makeResponse(true, "ERR_ISSUE_TRACKING_REG002: Ocurrió un error al obtener el usuario.", [], 500);
  55. }
  56. try {
  57. $validateExists = DB::table('S002V01TORCO')->where('ORCO_NUOR', '=', $orderNumber)->exists();
  58. } catch (\Throwable $th) {
  59. DB::rollBack();
  60. return $this->responseController->makeResponse(true, "ERR_ISSUE_TRACKING_REG003: Ocurrió un error al validar el número de orden de compra.", [], 500);
  61. }
  62. if (!$validateExists) {
  63. DB::rollBack();
  64. return $this->responseController->makeResponse(true, "ERR_ISSUE_TRACKING_REG004: No se encontró el número de orden de compra.", [], 500);
  65. }
  66. // Se guarda la imagen de evidencia en la base de datos documental
  67. $codeDocument1 = null;
  68. $arrResponseSave1 = $this->resourcesController->saveDocument($requestData['EVIDENCIA1'],'GEAD',$requestData['NOMBRE_EVIDENCIA1'],'FO',$requestData['NUMERO_LINEA']);
  69. if ( $arrResponseSave1['error'] ) {
  70. DB::rollBack();
  71. return $this->responseController->makeResponse(true, 'ERR_ISSUE_TRACKING_REG005:'.$arrResponseSave1['msg'], $arrResponseSave1['response'], 500);
  72. }
  73. $codeDocument1 = $arrResponseSave1['response'];
  74. // Se guarda la imagen de evidencia opcional en la base de datos documental
  75. $codeDocument2 = null;
  76. if ($requestData['EVIDENCIA1'] != '') {
  77. $arrResponseSave2 = $this->resourcesController->saveDocument($requestData['EVIDENCIA2'],'GEAD',$requestData['NOMBRE_EVIDENCIA2'],'FO',$requestData['NUMERO_LINEA']);
  78. if ( $arrResponseSave2['error'] ) {
  79. DB::rollBack();
  80. return $this->responseController->makeResponse(true, 'ERR_ISSUE_TRACKING_REG006:'.$arrResponseSave2['msg'], $arrResponseSave2['response'], 500);
  81. }
  82. $codeDocument2 = $arrResponseSave2['response'];
  83. }
  84. // Se procede a registrar el reporte en la base de datos
  85. try {
  86. $validateInsert = DB::table('S002V01TSEPR')->insert([
  87. 'SEPR_NUOR' => $orderNumber,
  88. 'SEPR_TIPO' => $requestData['TIPO'],
  89. 'SEPR_DESC' => $requestData['DESCRIPCION'],
  90. 'SEPR_EVI1' => $codeDocument1,
  91. 'SEPR_EVI2' => $codeDocument2,
  92. 'SEPR_NULI' => $requestData['NUMERO_LINEA'],
  93. 'SEPR_USRE' => $user,
  94. 'SEPR_FERE' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  95. 'SEPR_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  96. ]);
  97. } catch (\Throwable $th) {
  98. DB::rollBack();
  99. return $this->responseController->makeResponse(true, "ERR_ISSUE_TRACKING_REG007: Ocurrió un error al hacer la inserción en la base.", $th->getMessage(), 500);
  100. }
  101. // Se valida que se haya registrado correctamente
  102. if (!$validateInsert) {
  103. DB::rollBack();
  104. return $this->responseController->makeResponse(true, "ERR_ISSUE_TRACKING_REG008: No se pudo ingresar el reporte en la base.", [], 500);
  105. }
  106. // Se guardan todos los cambios
  107. DB::commit();
  108. return $this->responseController->makeResponse(false, "ÉXITO: Registro de Reporte Exitoso");
  109. }
  110. public function getIssueTracking($line) {
  111. DB::beginTransaction();
  112. try {
  113. $arrIssueTracking = DB::table('S002V01TSEPR')
  114. ->where('SEPR_NULI', '=', $line)
  115. ->where('ORCO_NULI', '=', $line)
  116. ->where('DESP_NULI', '=', $line)
  117. ->join('S002V01TORCO', 'SEPR_NUOR', '=', 'ORCO_NUOR')
  118. ->join('S002V01TDESP', 'DESP_NUDE', '=', 'ORCO_NUDE')
  119. ->get([
  120. 'SEPR_IDPR',
  121. 'SEPR_NUOR',
  122. 'DESP_NOMB',
  123. 'SEPR_TIPO',
  124. 'SEPR_DESC',
  125. 'SEPR_RESO',
  126. 'SEPR_EVI1',
  127. 'SEPR_EVI2',
  128. 'SEPR_ESTA',
  129. 'SEPR_USRE',
  130. 'SEPR_FERE',
  131. 'SEPR_USMO',
  132. 'SEPR_FEMO',
  133. ]);
  134. } catch (\Throwable $th) {
  135. DB::rollBack();
  136. return $this->responseController->makeResponse(true, "ERR_ISSUE_TRACKING_GET000: Ocurrió un error al obtener los datos.", $th->getMessage(), 500);
  137. }
  138. DB::commit();
  139. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrIssueTracking);
  140. }
  141. public function resolutionIssueTracking(Request $request) {
  142. $validator = Validator::make($request->all(),[
  143. 'NUMERO_SEGUIMIENTO' => 'required|string',
  144. 'DESCRIPCION' => 'required|string',
  145. 'NUMERO_LINEA' => 'required|string',
  146. 'USUARIO' => 'required|string',
  147. ]);
  148. if ($validator->fails()) {
  149. return $this->responseController->makeResponse(
  150. true,
  151. "ERR_ISSUE_TRACKING_RES000: Se encontraron uno o más errores.",
  152. $this->responseController->makeErrors($validator->errors()->messages()),
  153. 401
  154. );
  155. }
  156. DB::beginTransaction();
  157. $requestData = $request->all();
  158. // Se obtiene el usuario encriptado
  159. try {
  160. $user = $this->encController->decrypt($requestData['USUARIO']);
  161. } catch (\Throwable $th) {
  162. DB::rollBack();
  163. return $this->responseController->makeResponse(true, "ERR_ISSUE_TRACKING_RES001: Ocurrió un error al obtener el usuario.", [], 500);
  164. }
  165. // Se obtiene el número de orden enctriptado
  166. try {
  167. $trackingNumber = $this->encController->decrypt($requestData['NUMERO_SEGUIMIENTO']);
  168. } catch (\Throwable $th) {
  169. DB::rollBack();
  170. return $this->responseController->makeResponse(true, "ERR_ISSUE_TRACKING_RES002: Ocurrió un error al obtener el usuario.", [], 500);
  171. }
  172. // Se valida el número de seguimiento
  173. try {
  174. $validateExists = DB::table('S002V01TSEPR')
  175. ->where('SEPR_IDPR', '=', $trackingNumber)
  176. ->where('SEPR_NULI', '=', $requestData['NUMERO_LINEA'])
  177. ->exists();
  178. } catch (\Throwable $th) {
  179. DB::rollBack();
  180. return $this->responseController->makeResponse(true, "ERR_ISSUE_TRACKING_RES003: Ocurrió un error al validar el número de seguimiento.", [], 500);
  181. }
  182. if (!$validateExists) {
  183. DB::rollBack();
  184. return $this->responseController->makeResponse(true, "ERR_ISSUE_TRACKING_RES004: No se encontró el número de seguimiento seleccionado.", [], 500);
  185. }
  186. try {
  187. $validateUpdate = DB::table('S002V01TSEPR')->where('SEPR_IDPR', '=', $trackingNumber)->where('SEPR_NULI', '=', $requestData['NUMERO_LINEA'])->update([
  188. 'SEPR_ESTA' => 'Resuelto',
  189. 'SEPR_RESO' => $requestData['DESCRIPCION'],
  190. 'SEPR_USRE' => $user,
  191. 'SEPR_FEMO' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  192. 'SEPR_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  193. ]);
  194. } catch (\Throwable $th) {
  195. DB::rollBack();
  196. return $this->responseController->makeResponse(true, "ERR_ISSUE_TRACKING_RES005: Ocurrió un error al modificar los registros de seguimiento.", [], 500);
  197. }
  198. if (!$validateUpdate) {
  199. DB::rollBack();
  200. return $this->responseController->makeResponse(true, "ERR_ISSUE_TRACKING_RES006: No se pudo modificar los registros de seguimiento.", [], 500);
  201. }
  202. // Se guardan todos los cambios
  203. DB::commit();
  204. return $this->responseController->makeResponse(false, "ÉXITO: Modificación de Reporte Exitoso");
  205. }
  206. }