ALTER PROCEDURE [sAdmission].[pAdmissionStudentDocumentGet]
	 
    @rAdmissionFormId bigint,
	@rOrgId bigint,
	@rClassId bigint,
	@rBatchId bigint

WITH EXECUTE AS CALLER
AS
SET NOCOUNT ON
 
	BEGIN

	Select 
		ISNULL(D.Id,0) AS DocumentId,
		ISNULL(D.DocumentName,'')AS DocumentName,
		A.DocumentFileName ,
		A.AdmissionDocumentId
		from sCommon.tSubmitDocumentMaster D 
		INNER JOIN sCommon.tSubmitDocumentScope DS ON DS.SubmitDocumentId = D.Id
		INNER JOIN 
		(Select D1.DocumentName,D1.Id AS AdmissionDocumentId,S.DocumentName AS DocumentFileName FROM sCommon.tSubmitDocumentMaster D1
		INNER JOIN sAdmission.tAdmissionStudentDocument  S ON S.SubmitDocumentMasterId=D1.Id AND (S.AdmissionFormId= @rAdmissionFormId  OR @rAdmissionFormId=0)
		WHERE  D1.SubmitDocumentFunctionalityId='3' AND D1.OrgId=@rOrgId 
		AND D1.Id IN (select SubmitDocumentMasterId from sAdmission.tAdmissionCriteriaDocument WHERE ClassId=@rClassId AND BatchId = @rBatchId  ))A ON  A.DocumentName = D.DocumentName

		Where OrgId=@rOrgId AND SubmitDocumentFunctionalityId='1' AND DS.SourceTypeId = 2016 AND DS.SourceId = @rClassId
			
END