StockController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /*
  3. Desarrollador: Ing. Jean Jairo Benitez Meza
  4. Ultima Modificación: 11/04/2023
  5. Módulo: Gestión de Inventario y/o Stock
  6. */
  7. namespace App\Http\Controllers;
  8. use App\Http\Controllers\Controller;
  9. use App\Http\Controllers\ResourcesController;
  10. use App\Http\Controllers\ResponseController;
  11. use App\Http\Controllers\EncryptionController;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Carbon;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Validator;
  16. class StockController extends Controller
  17. {
  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. // Crear Artículo en Stock
  27. public function createArtitleWithoutOrder(Request $request) {
  28. $validator = Validator::make($request->all(), [
  29. 'ARTICULO' => 'required',
  30. 'MODELO' => 'required',
  31. 'CODIGO_MODELO' => 'required',
  32. 'FAMILIA' => 'required',
  33. 'SUBFAMILIA' => 'required',
  34. 'UNIDAD' => 'required',
  35. 'CODIGO_BARRAS' => 'required',
  36. 'CANTIDAD' => 'required',
  37. 'STOCK_MINIMO' => 'required',
  38. 'STOCK_MAXIMO' => 'required',
  39. 'REPARACION' => 'required|boolean',
  40. 'CONSUMIBLE' => 'required|boolean',
  41. 'PELIGROSO' => 'required|boolean',
  42. // 'FECHA_VENCIMIENTO' => '',
  43. 'PROVEEDOR' => 'required',
  44. 'IMAGEN' => 'required',
  45. 'NUMERO_LINEA' => 'required',
  46. 'USUARIO' => 'required',
  47. ]);
  48. if ($validator->fails()) {
  49. return $this->responseController->makeResponse(
  50. true,
  51. "ERR_STOCK_REG000: Se encontraron uno o más errores.",
  52. $this->responseController->makeErrors($validator->errors()->messages()),
  53. 401
  54. );
  55. }
  56. $requestData = $request->all();
  57. try {
  58. $user = $this->encController->decrypt($requestData['USUARIO']);
  59. } catch (\Throwable $th) {
  60. DB::rollBack();
  61. return $this->responseController->makeResponse(true, "ERR_STOCK_REG001: Ocurrió un error al obtener el usuario.", $th->getMessage(), 500);
  62. }
  63. try {
  64. $validateFamily = DB::table('S002V01TFAMI')
  65. ->where('FAMI_IDFA','=', $requestData['FAMILIA'])
  66. ->where('FAMI_NULI','=', $requestData['NUMERO_LINEA'])
  67. ->where('FAMI_ESTA','=','Activo')
  68. ->exists();
  69. } catch (\Throwable $th) {
  70. DB::rollBack();
  71. return $this->responseController->makeResponse(true, "ERR_STOCK_REG002: Ocurrió un error al validar la familia.", $th->getMessage(), 500);
  72. }
  73. if (!$validateFamily) {
  74. DB::rollBack();
  75. return $this->responseController->makeResponse(true, "ERR_STOCK_REG003: La familia no existe.", [], 500);
  76. }
  77. try {
  78. $validateSubfamily = DB::table('S002V01TSUBF')
  79. ->where('SUBF_IDSU','=', $requestData['SUBFAMILIA'])
  80. ->where('SUBF_NULI','=', $requestData['NUMERO_LINEA'])
  81. ->where('SUBF_ESTA','=','Activo')
  82. ->exists();
  83. } catch (\Throwable $th) {
  84. DB::rollBack();
  85. return $this->responseController->makeResponse(true, "ERR_STOCK_REG004: Ocurrió un error al validar la subfamilia.", $th->getMessage(), 500);
  86. }
  87. if (!$validateSubfamily) {
  88. DB::rollBack();
  89. return $this->responseController->makeResponse(true, "ERR_STOCK_REG005: La subfamilia no existe.", [], 500);
  90. }
  91. try {
  92. $validateUnit = DB::table('S002V01TUNID')
  93. ->where('UNID_IDUN','=', $requestData['UNIDAD'])
  94. ->where('UNID_NULI','=', $requestData['NUMERO_LINEA'])
  95. ->where('UNID_ESTA','=','Activo')
  96. ->exists();
  97. } catch (\Throwable $th) {
  98. DB::rollBack();
  99. return $this->responseController->makeResponse(true, "ERR_STOCK_REG006: Ocurrió un error al validar la unidad.", $th->getMessage(), 500);
  100. }
  101. if (!$validateUnit) {
  102. DB::rollBack();
  103. return $this->responseController->makeResponse(true, "ERR_STOCK_REG007: La unidad no existe.", [], 500);
  104. }
  105. try {
  106. $validateUnit = DB::table('S002V01TPROV')
  107. ->where('PROV_NUPR','=', $requestData['PROVEEDOR'])
  108. ->where('PROV_NULI','=', $requestData['NUMERO_LINEA'])
  109. ->where('PROV_ESTA','=','Activo')
  110. ->exists();
  111. } catch (\Throwable $th) {
  112. DB::rollBack();
  113. return $this->responseController->makeResponse(true, "ERR_STOCK_REG008: Ocurrió un error al validar el proveedor.", $th->getMessage(), 500);
  114. }
  115. if (!$validateUnit) {
  116. DB::rollBack();
  117. return $this->responseController->makeResponse(true, "ERR_STOCK_REG009: El proveedor no existe.", [], 500);
  118. }
  119. $jsonCodeImages = array();
  120. foreach ($requestData['IMAGEN'] as $imagen) {
  121. $arrResponseDocument = $this->resourcesController->saveDocument($imagen['base64File'],'GEAD',$imagen['nameFile'],'FO',$requestData['NUMERO_LINEA']);
  122. if ($arrResponseDocument['error']) {
  123. DB::rollBack();
  124. return $this->responseController->makeResponse(true, 'ERR_STOCK_REG010:'.$arrResponseDocument['msg'], $arrResponseDocument['response'], 500);
  125. }
  126. $jsonCodeImages[] = $arrResponseDocument['response'];
  127. }
  128. try {
  129. $validateRegister = DB::table('S002V01TSTAR')->insert([
  130. // 'STAR_CODI' => $requestData['CODIGO_STOCK'],
  131. 'STAR_ARTI' => $requestData['ARTICULO'],
  132. 'STAR_MODE' => $requestData['MODELO'],
  133. 'STAR_COMO' => $requestData['CODIGO_MODELO'],
  134. 'STAR_IDFA' => $requestData['FAMILIA'],
  135. 'STAR_IDSU' => $requestData['SUBFAMILIA'],
  136. 'STAR_NUPR' => $requestData['PROVEEDOR'],
  137. 'STAR_IDUN' => $requestData['UNIDAD'],
  138. 'STAR_COBA' => $requestData['CODIGO_BARRAS'],
  139. 'STAR_CANT' => $requestData['CANTIDAD'],
  140. 'STAR_STMI' => $requestData['STOCK_MINIMO'],
  141. 'STAR_STMA' => $requestData['STOCK_MAXIMO'],
  142. 'STAR_REPA' => $requestData['REPARACION'],
  143. 'STAR_CONS' => $requestData['CONSUMIBLE'],
  144. 'STAR_PELI' => $requestData['PELIGROSO'],
  145. 'STAR_FEVE' => $requestData['FECHA_VENCIMIENTO'],
  146. 'STAR_IMAG' => json_encode($jsonCodeImages),
  147. 'STAR_TIAD' => 'Sin Pedido',
  148. 'STAR_NULI' => $requestData['NUMERO_LINEA'],
  149. 'STAR_USRE' => $user,
  150. 'STAR_FERE' => Carbon::now()->timezone('America/Mazatlan')->toDateTimeString(),
  151. 'STAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  152. ]);
  153. } catch (\Throwable $th) {
  154. DB::rollBack();
  155. return $this->responseController->makeResponse(true, "ERR_STOCK_REG011: Ocurrió un error al registrar el artículo.", $th->getMessage(), 500);
  156. }
  157. if (!$validateRegister) {
  158. DB::rollBack();
  159. return $this->responseController->makeResponse(true, "ERR_STOCK_REG012: No se pudo registrar el artículo.", [], 500);
  160. }
  161. DB::commit();
  162. return $this->responseController->makeResponse(false, "EXITO: Registro Exitoso");
  163. }
  164. }