ALTER PROCEDURE [sFee].[pReceiptAdd]	
	@rId bigint,
	@rSourceTypeId smallint,
	@rSourceId bigint,
	@rReceiptNumber int,
	@rReceiptDate date,
	@rReceiptAmount decimal(12,2),
	@rTotalReceiptAmount decimal(12,2),
	@rDiscountAmount decimal(12,2),
	@rPayTypeId tinyint,
	@rChequeNo nvarchar(40),
	@rBankName nvarchar(200),
	@rReceiptBookId int,
	@rIsObsolete bit,
	@rCreatedUserId bigint,
	@rOrgId int,
	@rBatchId int,
	@rOrgGroupBatchId int,
	@rRemark nvarchar(200),
	@rReceiptDetailXml [nvarchar](max),
	@rChequeDate date,
	@rCharge decimal(12,2)=0,
	@rFeeInvoiceId bigint = NULL,
	@rCollectionGroupId int = 0,
	@rDepositedLedgerId int = 0,
	@rPaymentTransactionId bigint = 0,
	@rReceiptPrintGroupId nvarchar(100),
	@rIsDiscountCoupon bit = 0

WITH EXECUTE AS CALLER
AS
SET NOCOUNT ON

DECLARE @loDoc INT
DECLARE @lIsCreated INT
EXEC sp_xml_preparedocument @loDoc OUTPUT, @rReceiptDetailXml

DECLARE @lIsEnableLog bit = 0 -- Set to 1 If want to add log for receipt add process

BEGIN
	BEGIN TRY
		
		IF(@lIsEnableLog = 1)
		BEGIN
		 INSERT INTO [sFee].[tFeeReceiptLog]
		 VALUES (@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
			    ,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
			    ,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
			    ,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),'Receipt SP Main Process Start')
		END

		SELECT @rOrgGroupBatchId = OrgGroupBatchId FROM sSetup.tBatchMaster where Id = @rBatchId

		DECLARE @lConfigValue nvarchar(50)=''
		SELECT @lConfigValue = ItemValue FROM sConfig.tConfigurationSetup WHERE ConfigurationId = 339 AND SourceTypeId = 2010 AND SourceId = LEFT(@rOrgGroupBatchId,4)

		IF @lConfigValue = '' 
		    SET @lConfigValue = 'ReceiptBookWise'

		IF @rChequeDate = '1900-01-01'
			SET @rChequeDate = NULL

		-- Mantis - 0030695
		 DECLARE @lConfigValue1 nvarchar(50)=''
		 SELECT @lConfigValue1 = ItemValue FROM sConfig.tConfigurationSetup WHERE ConfigurationId = 543 AND SourceTypeId = 2010 AND SourceId = LEFT(@rOrgGroupBatchId,4)
		-- Mantis - 0030695	 

		IF(@rReceiptNumber <> 0)
		BEGIN
			IF(@rId = 0)--ID Condition Added by dhruv for issue no.0013266	
			BEGIN
				IF(@lConfigValue='ReceiptBookWise')
				BEGIN
					IF EXISTS(SELECT 1 from sFee.tReceipt WITH (NOLOCK) where ReceiptNumber=@rReceiptNumber AND ReceiptBookId=@rReceiptBookId AND OrgGroupBatchId = @rOrgGroupBatchId)
					BEGIN
						SELECT -102 AS Result
						RETURN	
					END
				END
				ELSE IF(@lConfigValue='PayTypeWise')
				BEGIN
					IF EXISTS(SELECT 1 from sFee.tReceipt WITH (NOLOCK) where ReceiptNumber=@rReceiptNumber AND ReceiptBookId=@rReceiptBookId AND  PayTypeId=@rPayTypeId AND OrgGroupBatchId = @rOrgGroupBatchId)
					BEGIN
						SELECT -102 AS Result
						RETURN	
					END
				END
				-- #Start Mantis Id : 0036287
				ELSE IF(@lConfigValue='Continuous Batch')
				BEGIN
					IF EXISTS(SELECT 1 from sFee.tReceipt WITH (NOLOCK) where ReceiptNumber=@rReceiptNumber AND ReceiptBookId=@rReceiptBookId)
					BEGIN
						SELECT -102 AS Result
						RETURN	
					END
				END
				-- #End Mantis Id : 0036287

				-- Mantis - 0030695
				IF(@rChequeNo <> '' AND @lConfigValue1 = 'True')
				BEGIN
					IF EXISTS(SELECT 1 from sFee.tReceipt WITH (NOLOCK) where ChequeNo=@rChequeNo)
					BEGIN
						SELECT -103 AS Result
						RETURN	
					END
				END
				-- Mantis - 0030695

				-- For Mantis 0038951, By Foram
				IF EXISTS(SELECT 1 FROM sFee.tPostDatePayment WITH (NOLOCK) WHERE BatchId = @rBatchId AND OrgGroupBatchId = @rOrgGroupBatchId 
														AND SourceTypeId = @rSourceTypeId AND SourceId = @rSourceId AND ReceiptBookId = @rReceiptBookId 
														AND PayTypeId = @rPayTypeId AND ChequeNo = @rChequeNo AND BankName = @rBankName AND Amount = @rTotalReceiptAmount AND ReceiptId > 0)
				BEGIN
					SELECT -104 AS Result
					RETURN	
				END
				-- For Mantis 0038951, By Foram
			END
			ELSE
			BEGIN
				IF(@lConfigValue='ReceiptBookWise')
				BEGIN
					IF EXISTS(SELECT 1 from sFee.tReceipt WITH (NOLOCK) where ReceiptNumber=@rReceiptNumber AND ReceiptBookId=@rReceiptBookId AND OrgGroupBatchId = @rOrgGroupBatchId AND Id != @rId)
					BEGIN
						SELECT -102 AS Result
						RETURN	
					END
				END
				ELSE IF(@lConfigValue='PayTypeWise')
				BEGIN
					IF EXISTS(SELECT 1 from sFee.tReceipt WITH (NOLOCK) where ReceiptNumber=@rReceiptNumber AND ReceiptBookId=@rReceiptBookId AND  PayTypeId=@rPayTypeId AND OrgGroupBatchId = @rOrgGroupBatchId AND Id != @rId)
					BEGIN
						SELECT -102 AS Result
						RETURN	
					END
				END
				-- #Start Mantis Id : 0036287
				ELSE IF(@lConfigValue='Continuous Batch')
				BEGIN
					IF EXISTS(SELECT 1 from sFee.tReceipt WITH (NOLOCK) where ReceiptNumber=@rReceiptNumber AND ReceiptBookId=@rReceiptBookId AND Id != @rId)
					BEGIN
						SELECT -102 AS Result
						RETURN	
					END
				END
				-- #End Mantis Id : 0036287

				-- Mantis - 0030695
				IF(@rChequeNo <> '' AND @lConfigValue1 = 'True')
				BEGIN
					IF EXISTS(SELECT 1 from sFee.tReceipt WITH (NOLOCK) where ChequeNo=@rChequeNo AND Id != @rId)
					BEGIN
						SELECT -103 AS Result
						RETURN	
					END
				END
				-- Mantis - 0030695
			END 
		END
		ELSE
		BEGIN
			SET @rReceiptNumber = sFee.fReceiptNumberGet(@rReceiptBookId,@rOrgGroupBatchId,@rPayTypeId,@lConfigValue)
		END	

		IF(@lIsEnableLog = 1)
		BEGIN
			INSERT INTO [sFee].[tFeeReceiptLog]
			 VALUES
			 (@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
				,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
				,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
				,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),'Receipt No Generated and pReceiptAddValidate SP Start')
		END

		EXECUTE [sFee].[pReceiptAddValidate] @rSourceId,'',@rReceiptDetailXml,@rPayTypeId,@rPaymentTransactionId,@rReceiptBookId,@rChequeNo,@rIsCreated = @lIsCreated OUTPUT 
		-- Added ,@rPayTypeId,@rPaymentTransactionId for mantis 0031290 By Foram	-- Added @rChequeNo for mantis 0035484 By Foram

		IF(@lIsEnableLog = 1)
		BEGIN
			INSERT INTO [sFee].[tFeeReceiptLog]
			 VALUES
			 (@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
				,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
				,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
				,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),'pReceiptAddValidate SP End : ' + CAST(@lIsCreated AS NVARCHAR(10)))
		END

		IF(@lIsCreated = 1)
 		BEGIN
			SELECT -501 AS Result
			RETURN
		END
		IF(@lIsCreated = -1)	-- For Mantis 0032370 by Foram
 		BEGIN
			SELECT -502 AS Result
			RETURN
		END
		IF(@lIsCreated = -2)	-- For Mantis 0035484 by Foram
 		BEGIN
			SELECT -503 AS Result
			RETURN
		END
	END TRY
	BEGIN CATCH 
		SELECT -1 AS Result, ERROR_Message()
	END CATCH	

	BEGIN TRY
	
	IF(@rId = 0)	
	BEGIN
		
		--Added By Dhruv
		IF(@lConfigValue='ReceiptBookWise')
		BEGIN
			IF EXISTS(SELECT 1 from sFee.tReceipt WITH (NOLOCK) where ReceiptNumber=@rReceiptNumber AND ReceiptBookId=@rReceiptBookId AND OrgGroupBatchId = @rOrgGroupBatchId)
			BEGIN
				SELECT -102 AS Result
				RETURN	
			END
		END
		ELSE IF(@lConfigValue='PayTypeWise')
		BEGIN
			IF EXISTS(SELECT 1 from sFee.tReceipt WITH (NOLOCK) where ReceiptNumber=@rReceiptNumber AND ReceiptBookId=@rReceiptBookId AND  PayTypeId=@rPayTypeId AND OrgGroupBatchId = @rOrgGroupBatchId)
			BEGIN
				SELECT -102 AS Result
				RETURN	
			END
		END
		--Added By Dhruv

		-- #Start Mantis Id : 0036287
			ELSE IF(@lConfigValue='Continuous Batch')
			BEGIN
			IF EXISTS(SELECT 1 from sFee.tReceipt WITH (NOLOCK) where ReceiptNumber=@rReceiptNumber AND ReceiptBookId=@rReceiptBookId)
			BEGIN
				SELECT -102 AS Result
				RETURN	
			END
		END
			-- #End Mantis Id : 0036287

		-- Mantis - 0030695
		IF(@rChequeNo <> '' AND @lConfigValue1 = 'True')
		BEGIN
			IF EXISTS(SELECT 1 from sFee.tReceipt WITH (NOLOCK) where ChequeNo=@rChequeNo)
			BEGIN
				SELECT -103 AS Result
				RETURN	
			END
		END
		-- Mantis - 0030695

		-- For Mantis 0038951, By Foram
		IF EXISTS(SELECT 1 FROM sFee.tPostDatePayment WITH (NOLOCK) WHERE BatchId = @rBatchId AND OrgGroupBatchId = @rOrgGroupBatchId 
												AND SourceTypeId = @rSourceTypeId AND SourceId = @rSourceId AND ReceiptBookId = @rReceiptBookId 
												AND PayTypeId = @rPayTypeId AND ChequeNo = @rChequeNo AND BankName = @rBankName AND Amount = @rTotalReceiptAmount AND ReceiptId > 0)
		BEGIN
			SELECT -104 AS Result
			RETURN	
		END
		-- For Mantis 0038951, By Foram

		Declare @lSourceTypeId int 
		SET @lSourceTypeId =(select ISNULL(SourceTypeId,0) from sSecurity.tUser WITH (NOLOCK) where Id = @rCreatedUserId)

		DECLARE @lCreatedUserId bigint
		IF(@lSourceTypeId = 2001 OR @lSourceTypeId = 2003 OR @rCreatedUserId = 3)
			SET @lCreatedUserId=(Select [sCommon].[fGetBigIntIdForOrg](4,LEFT(@rOrgGroupBatchId,4)))
		ELSE
			SET @lCreatedUserId = @rCreatedUserId 

		

		BEGIN TRANSACTION

		DECLARE @lNextNumber BIGINT  , @lMaxReceiptId bigint = 0  
		--EXECUTE sCommon.pTableRowIdGet @rOrgId, 'sFee.tReceipt', 0, 1, 1,@outNextNumber = @lNextNumber output
		SELECT @lMaxReceiptId = ISNULL(MAX(R.Id), 0) FROM sFee.tReceipt R WITH (NOLOCK) INNER JOIN sSetup.tBatchMaster BM WITH (NOLOCK)  ON BM.id = R.BatchId WHERE Bm.OrgId = @rOrgId

		IF EXISTS(SELECT 1 FROM sAudit.tReceiptDelete RD WITH (NOLOCK) INNER JOIN sSetup.tBatchMaster BM WITH (NOLOCK)  ON BM.Id = RD.BatchId WHERE BM.OrgId = @rOrgId AND RD.Id > @lMaxReceiptId)
		BEGIN
			SELECT @lMaxReceiptId = ISNULL(MAX(RD.Id), 0) FROM sAudit.tReceiptDelete RD WITH (NOLOCK) INNER JOIN sSetup.tBatchMaster BM WITH (NOLOCK)  ON BM.Id = RD.BatchId WHERE Bm.OrgId = @rOrgId
		END

		IF(@lMaxReceiptId = 0)
		BEGIN
			IF(@lIsEnableLog = 1)
			BEGIN
				INSERT INTO [sFee].[tFeeReceiptLog]
				VALUES
				(@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
					,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
					,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
					,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),'pTableRowIdGet SP Start')
			END

			EXECUTE sCommon.pTableRowIdGet @rOrgId, 'sFee.tReceipt', 0, 1, 1,@outNextNumber = @lNextNumber output

			IF(@lIsEnableLog = 1)
			BEGIN
				INSERT INTO [sFee].[tFeeReceiptLog]
				 VALUES
				 (@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
					,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
					,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
					,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),CAST(@lNextNumber AS NVARCHAR(100))+'-pTableRowIdGet SP End & Receipt Add Start')
			END
		END
		ELSE
		BEGIN
			SET @lNextNumber =  @lMaxReceiptId + 1 
		END
		
	
		IF(@lIsEnableLog = 1)
		BEGIN
			INSERT INTO [sFee].[tFeeReceiptLog]
			 VALUES
			 (@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
				,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
				,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
				,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),CAST(@lNextNumber AS NVARCHAR(100))+'- ReceiptId - Receipt Add Start')
		END

		INSERT INTO sFee.tReceipt
		(
		   Id,
		   SourceTypeId,
		   SourceId,
		   ReceiptNumber,
		   ReceiptDate,
		   ReceiptAmount,
		   TotalReceiptAmount,
		   DiscountAmount,
		   PayTypeId,
		   ChequeNo,
		   BankName,
		   ReceiptBookId,
		   Remark,
		   IsObsolete,
		   BatchId,
		   CreatedUserId,
		   CreatedDateTime,
		   OrgGroupBatchId,
		   ChequeDate,
		   FeeInvoiceId,
		   CollectionGroupId,
		   DepositedLedgerId,
		   PaymentTransactionId,
		   ReceiptPrintGroupId,
		   IsDiscountCoupon  ---Added By Dhruv for Issue . 0011150
		   --Added for default value
		    ,IsChequeCleared
			,ChequeClearDate
			,AmountAfterDiscount
			,CGSTPercentage
			,SGSTPercentage
			,CGSTAmount
			,SGSTAmount
			,CancelStatusId
			,CancelStatusRemark
			,FirmReceiptBookId
			,TaxPercentage
			,CancelDateTime
        )
		VALUES
		(
			@lNextNumber,
			@rSourceTypeId,
			@rSourceId,
		    @rReceiptNumber,
		    @rReceiptDate,
		    @rReceiptAmount,
		    @rTotalReceiptAmount,
		    @rDiscountAmount,
		    @rPayTypeId,
		    @rChequeNo,
		    @rBankName,
		    @rReceiptBookId,
		    @rRemark,
		    @rIsObsolete,
		    @rBatchId,
		    @lCreatedUserId,
		    sCommon.fGetDate(),
		    @rOrgGroupBatchId,
		    @rChequeDate,
		    @rFeeInvoiceId,
		    @rCollectionGroupId,
		    @rDepositedLedgerId,
		    @rPaymentTransactionId,
			@rReceiptPrintGroupId,
			@rIsDiscountCoupon
			,0,NULL,0,0,0,0,0,0,NULL,0,0,NULL
		)
		
		IF(@lIsEnableLog = 1)
		BEGIN
			INSERT INTO [sFee].[tFeeReceiptLog]
			VALUES
			(@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
			,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
			,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
			,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),'Receipt Add End and Receipt Detail Start') 
		END

		INSERT INTO sFee.tReceiptDetail
		(
		   ReceiptId,
		   FeeSetupId,
		   Amount
        )
        SELECT  	
		  @lNextNumber,
		  FeeSetupId,
		  Amount
		FROM OPENXML (@loDoc, '/Table/ReceiptDetail', 1)
		WITH ( 
				FeeSetupId int,
				Amount decimal(12,2)
			  ) 
		
		 IF(@lIsEnableLog = 1)
		         BEGIN
		         	INSERT INTO [sFee].[tFeeReceiptLog]
		         	VALUES
		         	(@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
		         	,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
		         	,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
		         	,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),'Receipt Detail End') 
		         END

		SELECT @lNextNumber AS Result
		EXEC sp_xml_removedocument @loDoc
		
	    IF (@rCharge > 0)
		BEGIN
		    IF(@lIsEnableLog = 1)
		    BEGIN
		    	INSERT INTO [sFee].[tFeeReceiptLog]
		    	VALUES
		    	(@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
		    	,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
		    	,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
		    	,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),'Charge Add Start') 
		    END
			
			EXEC [sFee].[pReceiptChargeAdd] @rSourceTypeId,@rSourceId,@lNextNumber,@lCreatedUserId,@rOrgId,@rBatchId,@rCharge,@rReceiptBookId
			
			IF(@lIsEnableLog = 1)
		    BEGIN
		    	INSERT INTO [sFee].[tFeeReceiptLog]
		    	VALUES
		    	(@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
		    	,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
		    	,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
		    	,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),'Charge Add End') 
		    END
		END
			
		DECLARE @lIsServiceTaxApplicable bit
	    DECLARE @lServiceTaxPercentage decimal(6,2)
	
	    SELECT @lIsServiceTaxApplicable = ISNULL(IsServiceTaxApplicable,0),@lServiceTaxPercentage = ISNULL(ServiceTaxPercentage,0) 
		FROM sFee.tReceiptBookMaster WITH (NOLOCK) where Id = @rReceiptBookId
		
		IF (@lIsServiceTaxApplicable > 0 AND @lServiceTaxPercentage > 0)
		BEGIN
			IF(@lIsEnableLog = 1)
			BEGIN
				INSERT INTO [sFee].[tFeeReceiptLog]
				VALUES
				(@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
				,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
				,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
				,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),'pReceiptServiceTaxAdd SP Add Start')
			END

			EXEC [sFee].[pReceiptServiceTaxAdd] @rSourceTypeId,@rSourceId,@lNextNumber,@lCreatedUserId,@rOrgId,@rBatchId,@lServiceTaxPercentage
			
			IF(@lIsEnableLog = 1)
			BEGIN
				INSERT INTO [sFee].[tFeeReceiptLog]
				VALUES
				(@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
				,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
				,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
				,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),'pReceiptServiceTaxAdd SP Add End')
			END
		END

	    IF(@lIsEnableLog = 1)
		BEGIN
			INSERT INTO [sFee].[tFeeReceiptLog]
			VALUES
			(@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
			,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
			,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
			,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),'pFeeProcessGeneratePaidFeeAdd SP Start')
		END
		
		EXEC [sFee].[pFeeProcessGeneratePaidFeeAdd] @rOrgId,@rBatchId,@rSourceId,@lNextNumber,@lCreatedUserId
		
		IF(@lIsEnableLog = 1)
		BEGIN
			INSERT INTO [sFee].[tFeeReceiptLog]
			VALUES
			(@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
			,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
			,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
			,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),'pFeeProcessGeneratePaidFeeAdd SP End')
		END
			
		SET @rId = @lNextNumber

		-- For Mantis 0023321 By Foram on 14/03/2024 START
		DECLARE @lSourceId bigint, @lSourceTypeId1 smallint, @lBatchId int, @lOrgGroupBatchId int, @lReceiptBookId int, 
				@lPayTypeId smallint, @lChequeNo nvarchar(40), @lBankName nvarchar(200), @lTotalReceiptAmount decimal(12,2)

		SELECT
			@lSourceId = SourceId, 
			@lSourceTypeId1 = SourceTypeId, 
			@lBatchId = BatchId, 
			@lOrgGroupBatchId = OrgGroupBatchId, 
			@lReceiptBookId = ReceiptBookId, 
			@lPayTypeId = PayTypeId, 
			@lChequeNo = ChequeNo, 
			@lBankName = BankName, 
			@lTotalReceiptAmount = TotalReceiptAmount
		FROM sFee.tReceipt WHERE Id = @rId

		IF EXISTS(SELECT 1 FROM sFee.tPostDatePayment WITH (NOLOCK) WHERE BatchId = @lBatchId AND OrgGroupBatchId = @lOrgGroupBatchId 
														AND SourceTypeId = @lSourceTypeId1 AND SourceId = @lSourceId AND ReceiptBookId = @lReceiptBookId 
														AND PayTypeId = @lPayTypeId AND ChequeNo = @lChequeNo AND BankName = @lBankName AND Amount = @lTotalReceiptAmount)
		BEGIN
			UPDATE sFee.tPostDatePayment 
			SET ReceiptId = @rId, ReceiptDate = @rReceiptDate, StatusId = 752 
			WHERE BatchId = @lBatchId AND OrgGroupBatchId = @lOrgGroupBatchId 
				AND SourceTypeId = @lSourceTypeId1 AND SourceId = @lSourceId AND ReceiptBookId = @lReceiptBookId 
				AND PayTypeId = @lPayTypeId AND ChequeNo = @lChequeNo AND BankName = @lBankName AND Amount = @lTotalReceiptAmount
		END
		ELSE
		BEGIN
			UPDATE sFee.tPostDatePayment SET ReceiptId = 0, StatusId = CASE WHEN ISNULL(DepositedDate, '') <> '' THEN 753 ELSE 751 END WHERE ReceiptId = @rId
		END
		-- For Mantis 0023321 By Foram on 14/03/2024 END

		IF(@lIsEnableLog = 1)
		BEGIN
			INSERT INTO [sFee].[tFeeReceiptLog]
			VALUES
			(@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
			,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
			,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
			,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),'PDC Entry Done')
		END

		COMMIT TRANSACTION
	END 
	ELSE
	BEGIN
		IF(@lIsEnableLog = 1)
		BEGIN
			INSERT INTO [sFee].[tFeeReceiptLog]
			VALUES
			(@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
			,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
			,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
			,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),'Fee Receipt Update Start')
		END

		IF EXISTS(SELECT 1 FROM sFee.tReceipt WITH (NOLOCK) where ReceiptNumber=@rReceiptNumber AND Id = @rId)
		BEGIN
			BEGIN TRANSACTION
				UPDATE sFee.tReceipt SET
					ReceiptDate=@rReceiptDate,
					PayTypeId=@rPayTypeId,
					ChequeDate=@rChequeDate,
					ChequeNo=@rChequeNo,
					BankName=@rBankName,
					Remark=@rRemark
				WHERE Id=@rId

				-- For Mantis 0023321 By Foram on 14/03/2024 START
				DECLARE @lSourceId_1 bigint, @lSourceTypeId1_1 smallint, @lBatchId_1 int, @lOrgGroupBatchId_1 int, @lReceiptBookId_1 int, 
						@lPayTypeId_1 smallint, @lChequeNo_1 nvarchar(40), @lBankName_1 nvarchar(200), @lTotalReceiptAmount_1 decimal(12,2)

				SELECT
					@lSourceId_1 = SourceId, 
					@lSourceTypeId1_1 = SourceTypeId, 
					@lBatchId_1 = BatchId, 
					@lOrgGroupBatchId_1 = OrgGroupBatchId, 
					@lReceiptBookId_1 = ReceiptBookId, 
					@lPayTypeId_1 = PayTypeId, 
					@lChequeNo_1 = ChequeNo, 
					@lBankName_1 = BankName, 
					@lTotalReceiptAmount_1 = TotalReceiptAmount
				FROM sFee.tReceipt WHERE Id = @rId

				IF EXISTS(SELECT 1 FROM sFee.tPostDatePayment WITH (NOLOCK) WHERE BatchId = @lBatchId_1 AND OrgGroupBatchId = @lOrgGroupBatchId_1
																AND SourceTypeId = @lSourceTypeId1_1 AND SourceId = @lSourceId_1 AND ReceiptBookId = @lReceiptBookId_1 
																AND PayTypeId = @lPayTypeId_1 AND ChequeNo = @lChequeNo_1 AND BankName = @lBankName_1 AND Amount = @lTotalReceiptAmount_1)
				BEGIN
					UPDATE sFee.tPostDatePayment 
					SET ReceiptId = @rId, ReceiptDate = @rReceiptDate, StatusId = 752 
					WHERE BatchId = @lBatchId_1 AND OrgGroupBatchId = @lOrgGroupBatchId_1 
						AND SourceTypeId = @lSourceTypeId1_1 AND SourceId = @lSourceId_1 AND ReceiptBookId = @lReceiptBookId_1 
						AND PayTypeId = @lPayTypeId_1 AND ChequeNo = @lChequeNo_1 AND BankName = @lBankName_1 AND Amount = @lTotalReceiptAmount_1
				END
				ELSE
				BEGIN
					UPDATE sFee.tPostDatePayment SET ReceiptId = 0, StatusId = CASE WHEN ISNULL(DepositedDate, '') <> '' THEN 753 ELSE 751 END WHERE ReceiptId = @rId
				END
				-- For Mantis 0023321 By Foram on 14/03/2024 END
								
			COMMIT TRANSACTION
		
			SELECT @rId AS RESULT
		END
		ELSE
		IF(@rReceiptNumber <> 0)
		BEGIN
			IF(@lConfigValue='ReceiptBookWise')
			BEGIN
				IF EXISTS(SELECT 1 from sFee.tReceipt WITH (NOLOCK) where ReceiptNumber=@rReceiptNumber AND ReceiptBookId=@rReceiptBookId AND OrgGroupBatchId = @rOrgGroupBatchId)
				BEGIN
					SELECT -102 AS Result
					RETURN	
				END
			END
			ELSE IF(@lConfigValue='PayTypeWise')
			BEGIN
				IF EXISTS(SELECT 1 from sFee.tReceipt WITH (NOLOCK) where ReceiptNumber=@rReceiptNumber AND ReceiptBookId=@rReceiptBookId AND  PayTypeId=@rPayTypeId AND OrgGroupBatchId = @rOrgGroupBatchId)
				BEGIN
					SELECT -102 AS Result
					RETURN	
				END
			END

		-- #Start Mantis Id : 0036287
			ELSE IF(@lConfigValue='Continuous Batch')
			BEGIN
				IF EXISTS(SELECT 1 from sFee.tReceipt WITH (NOLOCK) where ReceiptNumber=@rReceiptNumber AND ReceiptBookId=@rReceiptBookId)
				BEGIN
					SELECT -102 AS Result
					RETURN	
				END
			END
			-- #End Mantis Id : 0036287

		-- Mantis - 0030695
			IF(@rChequeNo <> '' AND @lConfigValue1 = 'True')
			BEGIN
				IF EXISTS(SELECT 1 from sFee.tReceipt WITH (NOLOCK) where ChequeNo=@rChequeNo AND Id != @rId)
				BEGIN
					SELECT -103 AS Result
					RETURN	
				END
			END
			-- Mantis - 0030695

			BEGIN TRANSACTION

				UPDATE sFee.tReceipt SET
					ReceiptNumber = @rReceiptNumber,
					ReceiptDate=@rReceiptDate,
					PayTypeId=@rPayTypeId,
					ChequeDate=@rChequeDate,
					ChequeNo=@rChequeNo,
					BankName=@rBankName,
					Remark=@rRemark
				WHERE Id=@rId
		
				-- For Mantis 0023321 By Foram on 14/03/2024 START
				DECLARE @lSourceId_2 bigint, @lSourceTypeId1_2 smallint, @lBatchId_2 int, @lOrgGroupBatchId_2 int, @lReceiptBookId_2 int, 
						@lPayTypeId_2 smallint, @lChequeNo_2 nvarchar(40), @lBankName_2 nvarchar(200), @lTotalReceiptAmount_2 decimal(12,2)

				SELECT
					@lSourceId_2 = SourceId, 
					@lSourceTypeId1_2 = SourceTypeId, 
					@lBatchId_2 = BatchId, 
					@lOrgGroupBatchId_2 = OrgGroupBatchId, 
					@lReceiptBookId_2 = ReceiptBookId, 
					@lPayTypeId_2 = PayTypeId, 
					@lChequeNo_2 = ChequeNo, 
					@lBankName_2 = BankName, 
					@lTotalReceiptAmount_2 = TotalReceiptAmount
				FROM sFee.tReceipt WHERE Id = @rId

				IF EXISTS(SELECT 1 FROM sFee.tPostDatePayment WITH (NOLOCK) WHERE BatchId = @lBatchId_2 AND OrgGroupBatchId = @lOrgGroupBatchId_2
																AND SourceTypeId = @lSourceTypeId1_2 AND SourceId = @lSourceId_2 AND ReceiptBookId = @lReceiptBookId_2 
																AND PayTypeId = @lPayTypeId_2 AND ChequeNo = @lChequeNo_2 AND BankName = @lBankName_2 AND Amount = @lTotalReceiptAmount_2)
				BEGIN
					UPDATE sFee.tPostDatePayment 
					SET ReceiptId = @rId, ReceiptDate = @rReceiptDate, StatusId = 752 
					WHERE BatchId = @lBatchId_2 AND OrgGroupBatchId = @lOrgGroupBatchId_2
						AND SourceTypeId = @lSourceTypeId1_2 AND SourceId = @lSourceId_2 AND ReceiptBookId = @lReceiptBookId_2 
						AND PayTypeId = @lPayTypeId_2 AND ChequeNo = @lChequeNo_2 AND BankName = @lBankName_2 AND Amount = @lTotalReceiptAmount_2
				END
				ELSE
				BEGIN
					UPDATE sFee.tPostDatePayment SET ReceiptId = 0, StatusId = CASE WHEN ISNULL(DepositedDate, '') <> '' THEN 753 ELSE 751 END WHERE ReceiptId = @rId
				END
				-- For Mantis 0023321 By Foram on 14/03/2024 END
								
				SELECT @rId AS RESULT
			COMMIT TRANSACTION 
		END
	
			IF(@lIsEnableLog = 1)
			BEGIN
				INSERT INTO [sFee].[tFeeReceiptLog]
				VALUES
				(@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
				,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
				,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
				,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),'Fee Receipt Update End')
			END
		END
	
	IF(@lIsEnableLog = 1)
	BEGIN
		INSERT INTO [sFee].[tFeeReceiptLog]
		VALUES
		(@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
		,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
		,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
		,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),'Receipt SP Main Process End')
	END
	END TRY
	BEGIN CATCH 
		ROLLBACK TRANSACTION
		IF(@lIsEnableLog = 1)
		BEGIN
			INSERT INTO [sFee].[tFeeReceiptLog]
			VALUES
			(@rOrgId,@rId,@rSourceTypeId,@rSourceId,@rReceiptNumber,@rReceiptDate,@rReceiptAmount
			,@rTotalReceiptAmount,@rDiscountAmount,@rPayTypeId,@rChequeNo,@rBankName,@rReceiptBookId,@rIsObsolete,@rCreatedUserId
			,@rBatchId,@rOrgGroupBatchId,@rRemark,@rReceiptDetailXml,@rChequeDate,@rCharge,@rFeeInvoiceId,@rCollectionGroupId
			,@rDepositedLedgerId,@rPaymentTransactionId,@rReceiptPrintGroupId,@rIsDiscountCoupon,GETDATE(),Error_Message())
			SELECT -1 AS Result, ERROR_Message()
		END
	END CATCH	
		
END
