StockController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 App\Http\Controllers\DocumentManagementController;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Validator;
  16. use App\Http\Controllers\FunctionsController;
  17. class StockController extends Controller
  18. {
  19. private $responseController;
  20. private $encController;
  21. private $resourcesController;
  22. private $documentManagementController;
  23. private $functionsController;
  24. public function __construct(){
  25. $this->responseController = new ResponseController();
  26. $this->encController = new EncryptionController();
  27. $this->resourcesController = new ResourcesController();
  28. $this->documentManagementController = new DocumentManagementController();
  29. $this->functionsController = new FunctionsController();
  30. }
  31. // Crear Artículo en Stock
  32. public function createArtitleWithoutOrder(Request $request) {
  33. $validator = Validator::make($request->all(), [
  34. 'ARTICULO' => 'required',
  35. 'MODELO' => 'required',
  36. 'CODIGO_MODELO' => 'required',
  37. 'FAMILIA' => 'required',
  38. 'SUBFAMILIA' => 'required',
  39. 'UNIDAD' => 'required',
  40. 'CODIGO_BARRAS' => 'required',
  41. 'CANTIDAD' => 'required',
  42. 'STOCK_MINIMO' => 'required',
  43. 'STOCK_MAXIMO' => 'required',
  44. 'REPARACION' => 'required|boolean',
  45. 'CONSUMIBLE' => 'required|boolean',
  46. 'PELIGROSO' => 'required|boolean',
  47. // 'FECHA_VENCIMIENTO' => '',
  48. // 'PROVEEDOR' => 'required',
  49. 'IMAGEN' => 'required',
  50. 'NUMERO_LINEA' => 'required',
  51. 'USUARIO' => 'required',
  52. ]);
  53. if ($validator->fails()) {
  54. return $this->responseController->makeResponse(
  55. true,
  56. "ERR_STOCK_REG000: Se encontraron uno o más errores.",
  57. $this->responseController->makeErrors($validator->errors()->messages()),
  58. 401
  59. );
  60. }
  61. DB::beginTransaction();
  62. $requestData = $request->all();
  63. try {
  64. $user = $this->encController->decrypt($requestData['USUARIO']);
  65. } catch (\Throwable $th) {
  66. DB::rollBack();
  67. return $this->responseController->makeResponse(true, "ERR_STOCK_REG001: Ocurrió un error al obtener el usuario.", $th->getMessage(), 500);
  68. }
  69. try {
  70. $validateFamily = DB::table('S002V01TFAMI')
  71. ->where('FAMI_IDFA','=', $requestData['FAMILIA'])
  72. ->where('FAMI_NULI','=', $requestData['NUMERO_LINEA'])
  73. ->where('FAMI_ESTA','=','Activo')
  74. ->exists();
  75. } catch (\Throwable $th) {
  76. DB::rollBack();
  77. return $this->responseController->makeResponse(true, "ERR_STOCK_REG002: Ocurrió un error al validar la familia.", $th->getMessage(), 500);
  78. }
  79. if (!$validateFamily) {
  80. DB::rollBack();
  81. return $this->responseController->makeResponse(true, "ERR_STOCK_REG003: La familia no existe.", [], 500);
  82. }
  83. try {
  84. $validateSubfamily = DB::table('S002V01TSUBF')
  85. ->where('SUBF_IDSU','=', $requestData['SUBFAMILIA'])
  86. ->where('SUBF_NULI','=', $requestData['NUMERO_LINEA'])
  87. ->where('SUBF_ESTA','=','Activo')
  88. ->exists();
  89. } catch (\Throwable $th) {
  90. DB::rollBack();
  91. return $this->responseController->makeResponse(true, "ERR_STOCK_REG004: Ocurrió un error al validar la subfamilia.", $th->getMessage(), 500);
  92. }
  93. if (!$validateSubfamily) {
  94. DB::rollBack();
  95. return $this->responseController->makeResponse(true, "ERR_STOCK_REG005: La subfamilia no existe.", [], 500);
  96. }
  97. try {
  98. $validateUnit = DB::table('S002V01TUNID')
  99. ->where('UNID_IDUN','=', $requestData['UNIDAD'])
  100. ->where('UNID_NULI','=', $requestData['NUMERO_LINEA'])
  101. ->where('UNID_ESTA','=','Activo')
  102. ->exists();
  103. } catch (\Throwable $th) {
  104. DB::rollBack();
  105. return $this->responseController->makeResponse(true, "ERR_STOCK_REG006: Ocurrió un error al validar la unidad.", $th->getMessage(), 500);
  106. }
  107. if (!$validateUnit) {
  108. DB::rollBack();
  109. return $this->responseController->makeResponse(true, "ERR_STOCK_REG007: La unidad no existe.", [], 500);
  110. }
  111. $requestData['PROVEEDOR'] = $requestData['PROVEEDOR'] === '' ? null : $requestData['PROVEEDOR'];
  112. if (!is_null($requestData['PROVEEDOR'])) {
  113. try {
  114. $validateUnit = DB::table('S002V01TPROV')
  115. ->where('PROV_NUPR','=', $requestData['PROVEEDOR'])
  116. ->where('PROV_NULI','=', $requestData['NUMERO_LINEA'])
  117. ->where('PROV_ESTA','=','Activo')
  118. ->exists();
  119. } catch (\Throwable $th) {
  120. DB::rollBack();
  121. return $this->responseController->makeResponse(true, "ERR_STOCK_REG008: Ocurrió un error al validar el proveedor.", $th->getMessage(), 500);
  122. }
  123. if (!$validateUnit) {
  124. DB::rollBack();
  125. return $this->responseController->makeResponse(true, "ERR_STOCK_REG009: El proveedor no existe.", [], 500);
  126. }
  127. }
  128. $arrCodeImages = array();
  129. foreach ($requestData['IMAGEN'] as $key => $encIdFile) {
  130. $idFile = $this->encController->decrypt($encIdFile);
  131. $tempFile = DB::table('S002V01TARTE')->where([
  132. ['ARTE_NULI', '=', $requestData['NUMERO_LINEA']],
  133. ['ARTE_IDAR', '=', $idFile],
  134. ])->first();
  135. if(is_null($tempFile)){
  136. return $this->responseController->makeResponse(true, 'ERR_ARTITLE_REG006: El archivo consultado no está registrado', [], 404);
  137. }else if($tempFile->ARTE_ESTA == 'Eliminado'){
  138. return $this->responseController->makeResponse(true, 'ERR_ARTITLE_REG007: El archivo consultado está eliminado', [], 404);
  139. }
  140. $fileResponse = $this->documentManagementController->moveFinalFile(
  141. intval($requestData['NUMERO_LINEA']),
  142. 'GIST',
  143. 'FO',
  144. $tempFile,
  145. $user,
  146. );
  147. if(!$fileResponse[0]){
  148. return $this->responseController->makeResponse(true, 'ERR_ARTITLE_REG008: '.$fileResponse[1], [], 400);
  149. }
  150. $arrCodeImages[] = $this->encController->encrypt($fileResponse[1]);
  151. }
  152. $jsonImages = json_encode($arrCodeImages);
  153. $now = $this->functionsController->now();
  154. $currentDate = $now->toDateTimeString();
  155. try {
  156. $validateRegister = DB::table('S002V01TSTAR')->insert([
  157. // 'STAR_CODI' => $requestData['CODIGO_STOCK'],
  158. 'STAR_ARTI' => $requestData['ARTICULO'],
  159. 'STAR_MODE' => $requestData['MODELO'],
  160. 'STAR_COMO' => $requestData['CODIGO_MODELO'],
  161. 'STAR_IDFA' => $requestData['FAMILIA'],
  162. 'STAR_IDSU' => $requestData['SUBFAMILIA'],
  163. 'STAR_NUPR' => $requestData['PROVEEDOR'],
  164. 'STAR_IDUN' => $requestData['UNIDAD'],
  165. 'STAR_COBA' => $requestData['CODIGO_BARRAS'],
  166. 'STAR_CANT' => $requestData['CANTIDAD'],
  167. 'STAR_STMI' => $requestData['STOCK_MINIMO'],
  168. 'STAR_STMA' => $requestData['STOCK_MAXIMO'],
  169. 'STAR_REPA' => $requestData['REPARACION'],
  170. 'STAR_CONS' => $requestData['CONSUMIBLE'],
  171. 'STAR_PELI' => $requestData['PELIGROSO'],
  172. 'STAR_FEVE' => $requestData['FECHA_VENCIMIENTO'],
  173. 'STAR_IMAG' => $jsonImages,
  174. 'STAR_TIAD' => 'Sin Pedido',
  175. 'STAR_NULI' => $requestData['NUMERO_LINEA'],
  176. 'STAR_USRE' => $user,
  177. 'STAR_FERE' => $currentDate,
  178. 'STAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  179. ]);
  180. } catch (\Throwable $th) {
  181. DB::rollBack();
  182. return $this->responseController->makeResponse(true, "ERR_STOCK_REG011: Ocurrió un error al registrar el artículo.", $th->getMessage(), 500);
  183. }
  184. if (!$validateRegister) {
  185. DB::rollBack();
  186. return $this->responseController->makeResponse(true, "ERR_STOCK_REG012: No se pudo registrar el artículo.", [], 500);
  187. }
  188. DB::commit();
  189. return $this->responseController->makeResponse(false, "EXITO: Registro Exitoso");
  190. }
  191. }