IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[AKTARIM_EPIKRIZ_PUSULA]') AND OBJECTPROPERTY(id,N'IsVIew') = 1)
BEGIN
    INSERT INTO SCRIPT_BACKUPS (SCRIPTNAME,SCRIPT,SCRIPTDATE) SELECT 'AKTARIM_EPIKRIZ_PUSULA' SCRIPTNAME,OBJECT_DEFINITION(OBJECT_ID('AKTARIM_EPIKRIZ_PUSULA')) AS SCRIPT,GETDATE() AS SCRIPTDATE
    DROP VIEW AKTARIM_EPIKRIZ_PUSULA
END
go

CREATE VIEW AKTARIM_EPIKRIZ_PUSULA
AS
Select --Id,ProtokolId,DoktorId,Endikasyon,OzgecmisiDiger,OzetEpikriz,Epikriz,CONVERT(varchar, ISNULL(MuayeneBaslangicTarihi, CreatedDate), 120) AS EPIKRIZ_ZAMANI,MuayeneBaslangicTarihi,
   *,  
					  (Select Adi + ' ' + Soyadi From MetCer.IK.Personel(NOLOCK) Where Id = gm.DoktorId) DoktorAdi,  
					  (Select B.Adi From MetCer.IK.Personel(NOLOCK) p INNER JOIN MetCer.Ortak.Bolum(NOLOCK) B ON  B.Id=BolumId Where p.Id = gm.DoktorId) BolumAdi  
from 	metcer.Tedavi.GenelMuayene gm  where  (State <> 0)  

--ORDER BY ProtokolId,Id
GO

IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[AKTARIM_YATISGOZEM]') AND OBJECTPROPERTY(id,N'IsVIew') = 1)
BEGIN
    INSERT INTO SCRIPT_BACKUPS (SCRIPTNAME,SCRIPT,SCRIPTDATE) SELECT 'AKTARIM_YATISGOZEM' SCRIPTNAME,OBJECT_DEFINITION(OBJECT_ID('AKTARIM_YATISGOZEM')) AS SCRIPT,GETDATE() AS SCRIPTDATE
    DROP VIEW AKTARIM_YATISGOZEM
END
go
CREATE VIEW AKTARIM_YATISGOZEM
AS
 SELECT Id,ProtokolId,Aciklama,DoktorId,GozlemTarihi FROM metcer.Tedavi.GozlemNotu where State>0 
 /*AND  ProtokolId= 1829*/

GO

IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[AKTARIM_AMELIYATRAPOR]') AND OBJECTPROPERTY(id,N'IsVIew') = 1)
BEGIN
    INSERT INTO SCRIPT_BACKUPS (SCRIPTNAME,SCRIPT,SCRIPTDATE) SELECT 'AKTARIM_AMELIYATRAPOR' SCRIPTNAME,OBJECT_DEFINITION(OBJECT_ID('AKTARIM_AMELIYATRAPOR')) AS SCRIPT,GETDATE() AS SCRIPTDATE
    DROP VIEW AKTARIM_AMELIYATRAPOR
END
go
CREATE VIEW AKTARIM_AMELIYATRAPOR
AS
select Id,ProtokolId,RaporText,RaporRtf,ISNULL(RaporTarihi,CreatedDate) RaporTarihi,DoktorId from metcer.Tedavi.AmeliyatRapor where State>0 
/*AND ProtokolId= 1829*/
GO

IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[AKTARIM_MUAYENEOZGECMIS]') AND OBJECTPROPERTY(id,N'IsVIew') = 1)
BEGIN
    INSERT INTO SCRIPT_BACKUPS (SCRIPTNAME,SCRIPT,SCRIPTDATE) SELECT 'AKTARIM_MUAYENEOZGECMIS' SCRIPTNAME,OBJECT_DEFINITION(OBJECT_ID('AKTARIM_MUAYENEOZGECMIS')) AS SCRIPT,GETDATE() AS SCRIPTDATE
    DROP VIEW AKTARIM_MUAYENEOZGECMIS
END
go
CREATE VIEW AKTARIM_MUAYENEOZGECMIS
AS
  select * from metcer.Tedavi.MuayeneOzGecmis where State>0 
  /*AND  ProtokolId= 10502  /*1 alerji, 3 ilaç, 4 operason 7 alışkanlık */*/
GO



IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[AKTARIM_HASTA_NOTLARI]') AND OBJECTPROPERTY(id,N'IsVIew') = 1)
BEGIN
    INSERT INTO SCRIPT_BACKUPS (SCRIPTNAME,SCRIPT,SCRIPTDATE) SELECT 'AKTARIM_HASTA_NOTLARI' SCRIPTNAME,OBJECT_DEFINITION(OBJECT_ID('AKTARIM_HASTA_NOTLARI')) AS SCRIPT,GETDATE() AS SCRIPTDATE
    DROP VIEW AKTARIM_HASTA_NOTLARI
END
go
CREATE VIEW AKTARIM_HASTA_NOTLARI
AS
select * from  metcer.Ortak.[Not] /*where ALANLARDAN BİRİ HASTAID Sİ ONA BAKALIM*/
GO



IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[AKTARIM_ANESTEZINOTLARI]') AND OBJECTPROPERTY(id,N'IsVIew') = 1)
BEGIN
    INSERT INTO SCRIPT_BACKUPS (SCRIPTNAME,SCRIPT,SCRIPTDATE) SELECT 'AKTARIM_ANESTEZINOTLARI' SCRIPTNAME,OBJECT_DEFINITION(OBJECT_ID('AKTARIM_ANESTEZINOTLARI')) AS SCRIPT,GETDATE() AS SCRIPTDATE
    DROP VIEW AKTARIM_ANESTEZINOTLARI
END
go

CREATE VIEW AKTARIM_ANESTEZINOTLARI
AS
  SELECT ISNULL(TA.AmeliyatBaslamaTarihi,TA.CreatedDate) AMELIYATTARIHI, TA.AnestezistNotu /*,TA.STATE,TY.State*/
	FROM metcer.Tedavi.Ameliyat TA(NOLOCK)
	INNER JOIN metcer.Tedavi.Yatis TY ON TA.YatisId = TY.Id
	WHERE TA.STATE<>0 AND TY.State<>0 AND AnestezistNotu IS NOT NULL 
	/*AND TY.ProtokolId = 1829*/


	
