responseController = new ResponseController(); $this->encController = new EncryptionController(); $this->resourcesController = new ResourcesController(); $this->documentManagementController = new DocumentManagementController(); $this->functionsController = new FunctionsController(); } // Crear Artículo en Stock public function createArtitleWithoutOrder(Request $request) { $validator = Validator::make($request->all(), [ 'ARTICULO' => 'required', 'MODELO' => 'required', 'CODIGO_MODELO' => 'required', 'FAMILIA' => 'required', 'SUBFAMILIA' => 'required', 'UNIDAD' => 'required', 'CODIGO_BARRAS' => 'required', 'CANTIDAD' => 'required', 'STOCK_MINIMO' => 'required', 'STOCK_MAXIMO' => 'required', 'REPARACION' => 'required|boolean', 'CONSUMIBLE' => 'required|boolean', 'PELIGROSO' => 'required|boolean', // 'FECHA_VENCIMIENTO' => '', // 'PROVEEDOR' => 'required', 'IMAGEN' => 'required', 'NUMERO_LINEA' => 'required', 'USUARIO' => 'required', ]); if ($validator->fails()) { return $this->responseController->makeResponse( true, "ERR_STOCK_REG000: Se encontraron uno o más errores.", $this->responseController->makeErrors($validator->errors()->messages()), 401 ); } DB::beginTransaction(); $requestData = $request->all(); try { $user = $this->encController->decrypt($requestData['USUARIO']); } catch (\Throwable $th) { DB::rollBack(); return $this->responseController->makeResponse(true, "ERR_STOCK_REG001: Ocurrió un error al obtener el usuario.", $th->getMessage(), 500); } try { $validateFamily = DB::table('S002V01TFAMI') ->where('FAMI_IDFA','=', $requestData['FAMILIA']) ->where('FAMI_NULI','=', $requestData['NUMERO_LINEA']) ->where('FAMI_ESTA','=','Activo') ->exists(); } catch (\Throwable $th) { DB::rollBack(); return $this->responseController->makeResponse(true, "ERR_STOCK_REG002: Ocurrió un error al validar la familia.", $th->getMessage(), 500); } if (!$validateFamily) { DB::rollBack(); return $this->responseController->makeResponse(true, "ERR_STOCK_REG003: La familia no existe.", [], 500); } try { $validateSubfamily = DB::table('S002V01TSUBF') ->where('SUBF_IDSU','=', $requestData['SUBFAMILIA']) ->where('SUBF_NULI','=', $requestData['NUMERO_LINEA']) ->where('SUBF_ESTA','=','Activo') ->exists(); } catch (\Throwable $th) { DB::rollBack(); return $this->responseController->makeResponse(true, "ERR_STOCK_REG004: Ocurrió un error al validar la subfamilia.", $th->getMessage(), 500); } if (!$validateSubfamily) { DB::rollBack(); return $this->responseController->makeResponse(true, "ERR_STOCK_REG005: La subfamilia no existe.", [], 500); } try { $validateUnit = DB::table('S002V01TUNID') ->where('UNID_IDUN','=', $requestData['UNIDAD']) ->where('UNID_NULI','=', $requestData['NUMERO_LINEA']) ->where('UNID_ESTA','=','Activo') ->exists(); } catch (\Throwable $th) { DB::rollBack(); return $this->responseController->makeResponse(true, "ERR_STOCK_REG006: Ocurrió un error al validar la unidad.", $th->getMessage(), 500); } if (!$validateUnit) { DB::rollBack(); return $this->responseController->makeResponse(true, "ERR_STOCK_REG007: La unidad no existe.", [], 500); } $requestData['PROVEEDOR'] = $requestData['PROVEEDOR'] === '' ? null : $requestData['PROVEEDOR']; if (!is_null($requestData['PROVEEDOR'])) { try { $validateUnit = DB::table('S002V01TPROV') ->where('PROV_NUPR','=', $requestData['PROVEEDOR']) ->where('PROV_NULI','=', $requestData['NUMERO_LINEA']) ->where('PROV_ESTA','=','Activo') ->exists(); } catch (\Throwable $th) { DB::rollBack(); return $this->responseController->makeResponse(true, "ERR_STOCK_REG008: Ocurrió un error al validar el proveedor.", $th->getMessage(), 500); } if (!$validateUnit) { DB::rollBack(); return $this->responseController->makeResponse(true, "ERR_STOCK_REG009: El proveedor no existe.", [], 500); } } $arrCodeImages = array(); foreach ($requestData['IMAGEN'] as $key => $encIdFile) { $idFile = $this->encController->decrypt($encIdFile); $tempFile = DB::table('S002V01TARTE')->where([ ['ARTE_NULI', '=', $requestData['NUMERO_LINEA']], ['ARTE_IDAR', '=', $idFile], ])->first(); if(is_null($tempFile)){ return $this->responseController->makeResponse(true, 'ERR_ARTITLE_REG006: El archivo consultado no está registrado', [], 404); }else if($tempFile->ARTE_ESTA == 'Eliminado'){ return $this->responseController->makeResponse(true, 'ERR_ARTITLE_REG007: El archivo consultado está eliminado', [], 404); } $fileResponse = $this->documentManagementController->moveFinalFile( intval($requestData['NUMERO_LINEA']), 'GIST', 'FO', $tempFile, $user, ); if(!$fileResponse[0]){ return $this->responseController->makeResponse(true, 'ERR_ARTITLE_REG008: '.$fileResponse[1], [], 400); } $arrCodeImages[] = $this->encController->encrypt($fileResponse[1]); } $jsonImages = json_encode($arrCodeImages); $now = $this->functionsController->now(); $currentDate = $now->toDateTimeString(); try { $validateRegister = DB::table('S002V01TSTAR')->insert([ // 'STAR_CODI' => $requestData['CODIGO_STOCK'], 'STAR_ARTI' => $requestData['ARTICULO'], 'STAR_MODE' => $requestData['MODELO'], 'STAR_COMO' => $requestData['CODIGO_MODELO'], 'STAR_IDFA' => $requestData['FAMILIA'], 'STAR_IDSU' => $requestData['SUBFAMILIA'], 'STAR_NUPR' => $requestData['PROVEEDOR'], 'STAR_IDUN' => $requestData['UNIDAD'], 'STAR_COBA' => $requestData['CODIGO_BARRAS'], 'STAR_CANT' => $requestData['CANTIDAD'], 'STAR_STMI' => $requestData['STOCK_MINIMO'], 'STAR_STMA' => $requestData['STOCK_MAXIMO'], 'STAR_REPA' => $requestData['REPARACION'], 'STAR_CONS' => $requestData['CONSUMIBLE'], 'STAR_PELI' => $requestData['PELIGROSO'], 'STAR_FEVE' => $requestData['FECHA_VENCIMIENTO'], 'STAR_IMAG' => $jsonImages, 'STAR_TIAD' => 'Sin Pedido', 'STAR_NULI' => $requestData['NUMERO_LINEA'], 'STAR_USRE' => $user, 'STAR_FERE' => $currentDate, 'STAR_FEAR' => DB::raw('CURRENT_TIMESTAMP'), ]); } catch (\Throwable $th) { DB::rollBack(); return $this->responseController->makeResponse(true, "ERR_STOCK_REG011: Ocurrió un error al registrar el artículo.", $th->getMessage(), 500); } if (!$validateRegister) { DB::rollBack(); return $this->responseController->makeResponse(true, "ERR_STOCK_REG012: No se pudo registrar el artículo.", [], 500); } DB::commit(); return $this->responseController->makeResponse(false, "EXITO: Registro Exitoso"); } }