IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[SPWEB_RANDEVULAR]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE SPWEB_RANDEVULAR
GO



CREATE PROCEDURE [dbo].[SPWEB_RANDEVULAR] @kisino NVARCHAR(30), @tumu BIT
AS
BEGIN
	DECLARE @HKIMLIKID INT = dbo.FN_BASIT_ID_SIFRE_AC(@kisino)
	EXEC SPWEB_LOGLA 'randevular',@HKIMLIKID,'Randevu listesini çekti',''
	SELECT TOP (CASE WHEN @tumu=1 THEN 5000 ELSE 50 END) RTARIH as rtarih, CONVERT(nvarchar(5),RSAAT+2,8) AS rsaat, BOLUMADI AS bolum, DRID AS doktorid, DRADI AS doktor, TEL AS tel, RACIKLAMA as raciklama, RANDEVUID as id, 
		CASE WHEN RANDDURUMID IN (1,7) AND HMUAYENEISLID IS NULL AND RTARIHSAAT >= GETDATE() THEN 1 ELSE 0 END AS iptaledilebilir, (SELECT SUBEADI FROM SUBELER WITH(NOLOCK) WHERE SUBEID=VWRANDEVU.SUBEID) AS sube 
	FROM VWRANDEVU WITH(NOLOCK) 
	WHERE HKIMLIKID=@HKIMLIKID AND RANDDURUMID NOT IN (5,6)
	ORDER BY RANDEVUID DESC  
END
GO

IF EXISTS (SELECT [name] FROM [sys].[database_principals] WHERE [type] = N'S' AND [name] = N'mediwebuser')
BEGIN
	BEGIN TRY
		GRANT EXECUTE ON [dbo].[SPWEB_RANDEVULAR] TO [mediwebuser]
	END TRY
	BEGIN CATCH
	END CATCH
END
GO




