IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[SPWEB_LABSONUC]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE SPWEB_LABSONUC
GO
--  SPWEB_LABSONUC 254074,339036,0
CREATE PROCEDURE [dbo].[SPWEB_LABSONUC] @kisino NVARCHAR(30), @tetkikid INT, @tumu BIT
AS
BEGIN
  declare @strtetkikid varchar(500) = convert(varchar,@tetkikid)+' numaralı tetkik detay görüldü'
  DECLARE @HKIMLIKID INT = dbo.FN_BASIT_ID_SIFRE_AC(@kisino)
  EXEC SPWEB_LOGLA 'tetkikdetay',@HKIMLIKID,@strtetkikid,''

  DECLARE @HTETKIKKARTID INT = ISNULL((select HTETKIKKARTID FROM HTETKIK WITH(NOLOCK) WHERE HTETKIKID=@tetkikid),-525)  
  
  SELECT	TOP (CASE WHEN @tumu=1 THEN 5000 ELSE 1000 END)
			TST.ITEMADI as tetkik, TST.TESTADI as test, 
			TST.OLCUM + (CASE WHEN (SELECT COUNT(HDTY.HTETKIKDETAYID) FROM HTETKIKDETAY HDTY WHERE HDTY.HTETKIKID=TST.HTETKIKID) >1 THEN ' - ' + TST.ITEMADI ELSE '' END) as olcum, 
			TST.SONUCR as sonucr, TST.BIRIM as birim, TST.SONUCY as sonucy, TST.MIND as mind, 
			TST.MIND2 as mind2, TST.MAXD as maxd, TST.MAXD2 as maxd2, TST.NUMUNETURADI as numunetur, TST.TESTORNEKTIPI as ornektipi,
			TST.TETKIKTURADI as tetkiktur, TST.SIRANO as sirano, TST.EK1 as ek1, TST.UYARIISRT as uyaristr, TST.REFD as refd, TST.REFD2 as refd2, TST.ORNEKID as ornekid,
			T.LTCKIMLIKNO as tckimlikno, T.LADISOYADI as adsoyad, T.LDTARIHI as dogumtarih, T.LHYASI as yas, T.HCINSIYETI as cinsiyet, 
			T.HDOSYAID as dosyaid, T.LUYGBOLUM as uygulayanbolum, T.LBOLUM as bolum, T.LDOKTOR as doktor, T.ITARIH as tarih, ISNULL(T.ORNEKTARIHI,T.ITARIH) as ornektarih, T.KABULTARIH as kabultarih, T.CALBITISZAMANI as bitiszamani, 
			T.ONAYZAMANI as onayzamani, T.LUYGDRADI as uygulayandoktor, T.UYGDRUNVAN as uygulayanunvan, T.UYGDRUZMANLIKADI as uygulayanuzmanlik, t.GIRISTARIH as giristarih,
			(SELECT SUBEADI FROM SUBELER WITH(NOLOCK) WHERE SUBEID=T.SUBEID) AS sube
	FROM VWTETKIKTEST TST WITH(NOLOCK) 
		JOIN VWTETKIK T WITH(NOLOCK) ON T.HTETKIKID = TST.HTETKIKID 
	WHERE T.HKIMLIKID=@HKIMLIKID AND  TST.HTETKIKKARTID = @HTETKIKKARTID AND RAPORDAgoRUNSUN = 1 AND T.WEBgoRUNSUN=1 AND TST.DRM=2
	ORDER BY TST.HTETKIKKARTID,T.HTETKIKID,TST.SIRANO
  
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_LABSONUC] TO [mediwebuser]
	END TRY
	BEGIN CATCH
	END CATCH
END
GO




