IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[SPWEB_KVKKONAY]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE SPWEB_KVKKONAY
GO
CREATE PROCEDURE [dbo].[SPWEB_KVKKONAY] @kisino NVARCHAR(30), @onayturu NVARCHAR(30), @onay BIT, @ip NVARCHAR(20)
AS
BEGIN

  DECLARE @HKIMLIKID INT = dbo.FN_BASIT_ID_SIFRE_AC(@kisino)
  DECLARE @LOGMETIN NVARCHAR(200) = @onayturu+ (CASE WHEN @onay=1 THEN ' onaylandı' else 'onayiptaledildi' end)
  EXEC SPWEB_LOGLA 'hesapgoruldu',@HKIMLIKID,@LOGMETIN,@ip

  IF (SELECT COUNT(*) FROM HKIMLIKGIZLILIK WHERE HKIMLIKID=@HKIMLIKID)=0
  BEGIN
	INSERT INTO HKIMLIKGIZLILIK (HKIMLIKID,KVKKONAY,PAYLASIMIZNI,BILGILENDIRILSINMI,TELEFON,SMS,MAIL,FAX,POSTA,TELEFONNO1,TELEFONNO2,SMSNO,MAILADRESI,POSTAADRESI,KYTKULLANICI,KYTTARIHI,
		SGKULLANICI,SGTARIHI,PAYLASIMIZNIEVET,BILGILENDIRILSINEVET,PAYLASIMIZINDURUMU,TICARIIZINDURUMU) 
					SELECT @HKIMLIKID,0,0,0,0,0,0,0,0,ISNULL(HKIMLIK.HCEP,''),ISNULL(HKIMLIK.HCEP,''),ISNULL(HKIMLIK.HCEP,''),ISNULL(HKIMLIK.HEMAIL,''),'','WEB',GETDATE(),'WEB',GETDATE(),0,0,2,2
					FROM HKIMLIK WHERE HKIMLIKID=@HKIMLIKID
  END

  IF @onayturu='kvkk' 
  BEGIN
	UPDATE HKIMLIKGIZLILIK SET KVKKONAY=ISNULL(@onay,0) WHERE HKIMLIKID=@HKIMLIKID
  END
  
  IF @onayturu='iys'
  BEGIN
	UPDATE HKIMLIKGIZLILIK SET 
		TICARIIZINDURUMU=(CASE WHEN ISNULL(@onay,0)=0 THEN 2 ELSE 1 END),
		TELEFON = (CASE WHEN ISNULL(SMSNO,'')<>'' AND @onay=1 THEN 1 ELSE 0 END),
		SMS = (CASE WHEN ISNULL(SMSNO,'')<>'' AND @onay=1 THEN 1 ELSE 0 END),
		MAIL = (CASE WHEN ISNULL(MAILADRESI,'')<>'' AND @onay=1 THEN 1 ELSE 0 END)
	WHERE HKIMLIKID=@HKIMLIKID
  END

  SELECT 1 as result, 'Onay durumunuz değiştirilmiştir' as resultdesc
  
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_KVKKONAY] TO [mediwebuser]
	END TRY
	BEGIN CATCH
	END CATCH
END
GO




