GO
ALTER PROCEDURE [sCommon].[pMaxAdmissionInquiryNumberWithPrefixGet]
	 
	 @rPrefixFunctionalityId smallint,
	 @rOrgId smallint,
	 @rOrgGroupId int,
	 @rOrgGroupBatchId int=0,
	 @rBatchId int=0
	 
	WITH EXECUTE AS CALLER
AS
SET NOCOUNT ON
			 
BEGIN 
BEGIN TRY
	
	DECLARE @lSourceTypeId int
	DECLARE @lOutCode nvarchar(20)
	DECLARE @lPrefix nvarchar(10)
	DECLARE @lNumberLength tinyint
	
	DECLARE @lOrgGroupBatchId int=0
	
	SELECT @lOrgGroupBatchId=OrgGroupBatchId FROM sSetup.tBatchMaster BM WHERE BM.Id = @rBatchId
	
	DECLARE @lConfigValue nvarchar(50)=''
	SELECT @lConfigValue = ItemValue FROM sConfig.tConfigurationSetup WHERE ConfigurationId = 341 AND SourceTypeId = 2010 AND SourceId = @rOrgGroupId
	
	IF @rPrefixFunctionalityId=2013 -- Admission Inquiry
	BEGIN
		SELECT @lSourceTypeId=SourceTypeId FROM sCommon.tPrefixConfig WHERE PrefixFunctionalityId = @rPrefixFunctionalityId AND OrgId =@rOrgId
		IF @lSourceTypeId IS NOT NULL
		BEGIN
			IF @lSourceTypeId = 2021 -- Organization
			BEGIN
				SELECT @lPrefix=Prefix,@lNumberLength=NumberLength FROM sCommon.tPrefixConfig WHERE PrefixFunctionalityId = @rPrefixFunctionalityId AND SourceId = @rOrgId
			END

			IF(@lConfigValue='OrgGroupBatch')
			BEGIN
				SELECT @lOutCode = (MAX(Number)) FROM sAdmission.tAdmissionInquiry AI
				INNER JOIN sSetup.tBatchMaster BM ON BM.Id = AI.ForbatchId 
				WHERE BM.OrgGroupBatchId = @lOrgGroupBatchId AND Number LIKE @lPrefix+'%'
			END
			ELSE IF(@lConfigValue='Batch')
			BEGIN
				SELECT @lOutCode = MAX(Number) FROM sAdmission.tAdmissionInquiry AI
				WHERE AI.ForBatchId = @rBatchId AND Number LIKE @lPrefix+'%'
			END
			ELSE IF (@lConfigValue = 'Continuous' OR @lConfigValue = '' OR @lConfigValue IS NULL) --changes for mantis id:0045586
			BEGIN
				SELECT @lOutCode =MAX(Number) FROM sAdmission.tAdmissionInquiry  
				WHERE OrgGroupId = @rOrgGroupId AND Number LIKE @lPrefix+'%'
			END

			IF CHARINDEX(@lPrefix,@lOutCode) > 0
			BEGIN
					SELECT @lOutCode = RIGHT(@lOutCode, LEN(@lOutCode)-LEN(@lPrefix))
					SELECT @lOutCode = (CONVERT(bigint, @lOutCode)+1)
					SELECT @lOutCode = RIGHT(REPLICATE('0', @lNumberLength), @lNumberLength-LEN(@lPrefix)-LEN(@lOutCode))+@lOutCode
					SELECT @lOutCode = @lPrefix + @lOutCode
				END
			ELSE
			BEGIN
				SELECT @lOutCode = 1
				IF @lNumberLength != 0
				BEGIN
					SELECT @lOutCode = RIGHT(REPLICATE('0', @lNumberLength), @lNumberLength-LEN(@lPrefix)-LEN(@lOutCode))+@lOutCode
				END
				SELECT @lOutCode = @lPrefix + @lOutCode
			END
		END
		ELSE 
		BEGIN
			IF(@lConfigValue='OrgGroupBatch')
			BEGIN
				SELECT @lOutCode = (ISNULL(MAX(InquiryNumber),0) + 1) FROM sAdmission.tAdmissionInquiry AI
				INNER JOIN sSetup.tBatchMaster BM ON BM.Id = AI.ForbatchId 
				WHERE BM.OrgGroupBatchId = @lOrgGroupBatchId 
			END
			ELSE IF(@lConfigValue='Batch')
			BEGIN
				SELECT @lOutCode = (ISNULL(MAX(InquiryNumber),0) + 1) FROM sAdmission.tAdmissionInquiry AI
				WHERE AI.ForBatchId = @rBatchId
			END
			ELSE IF (@lConfigValue = 'Continuous' OR @lConfigValue = '' OR @lConfigValue IS NULL)  --changes for mantis id:0045586
			BEGIN
				SELECT @lOutCode = (ISNULL(MAX(InquiryNumber),0) + 1) FROM sAdmission.tAdmissionInquiry AI 
				WHERE OrgGroupId = @rOrgGroupId 
			END
		END
	END
	
	END TRY
	BEGIN CATCH
		SET @lOutCode = ''
	END CATCH
	
	SELECT @lOutCode Result
END
GO