IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[SPUSS_407_GUNSONU_KAYIT]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE SPUSS_407_GUNSONU_KAYIT
GO
CREATE PROCEDURE [dbo].[SPUSS_407_GUNSONU_KAYIT] 
	@HDOSYAID INT=0,
	@ID INT=0,
	@TUMUNUTEKRAR BIT = 0	 	 
AS
BEGIN
	DECLARE @SUBEID INT=CONVERT(INT,SUBSTRING(CONVERT(VARCHAR,@HDOSYAID),1,1))
	DECLARE @DAKIKAONCE INT=CONVERT(INT,SUBSTRING(CONVERT(VARCHAR,@HDOSYAID),2,LEN(CONVERT(VARCHAR,@HDOSYAID))-1))
	DECLARE @PERYOTBASLANGICTARIHI DATETIME
	DECLARE @PERYOTBITISTARIHI DATETIME
	DECLARE @USSGUNSONUGONDERIMID INT=0

	SET @PERYOTBASLANGICTARIHI = CONVERT(DATETIME,CONVERT(DATE,DATEADD(MINUTE,-1*(@DAKIKAONCE+1440),GETDATE())))
	SET @PERYOTBITISTARIHI = DATEADD(SECOND,-1,DATEADD(DAY,1,@PERYOTBASLANGICTARIHI))
	EXEC SPUSS_KLINIKKALITESAYIYAZ_GUNLUK 0,@SUBEID,@PERYOTBASLANGICTARIHI,@PERYOTBITISTARIHI


		
	
	
	DECLARE @HATA NVARCHAR(2000) = ''
	DECLARE @MESAJ NVARCHAR(2000) = ''
	DECLARE @SAYIHATA BIT = 0
	DECLARE @XMLBASLIK NVARCHAR(2000) = ''
	DECLARE @goNDERIMTABLOSU TABLE 
		(
			goNDERILECEKXML XML,
			HATA NVARCHAR(2000),
			MESAJ NVARCHAR(2000),	
			XMLBASLIK NVARCHAR(3500),
			TABLOID INT,
			MINID INT,
			MAXID INT
		)
	
	IF OBJECT_ID('tempdb..#TMPGUNLUK') IS NOT NULL 
	DROP TABLE #TMPGUNLUK
	IF OBJECT_ID('tempdb..#TMPSUBELER') IS NOT NULL 
	DROP TABLE #TMPSUBELER


	SELECT	
			CASE WHEN (SELECT TOP 1 VALUESTR FROM CONFIG WITH(NOLOCK) WHERE SECTIONSTR='Saglik-Net' AND PROGNAME='MEDISOFT' AND /* VALUESTR='1' AND*/ KEYNAME LIKE CONVERT(VARCHAR,SUBEID)+' numaral% %ubede Sa%l%k Net Online test modu aktif')='1' THEN
				'10000038' 
			ELSE
				ISNULL((SELECT TOP 1 VALUESTR FROM CONFIG WITH(NOLOCK) WHERE SECTIONSTR='Saglik-Net' AND PROGNAME='MEDISOFT' AND KEYNAME= CONVERT(VARCHAR,SUBEID)+' numaralı şubenin kurum numarası'),'') 
			END AS SAGLIKNETKURUMKODU,

				CASE WHEN (SELECT TOP 1 VALUESTR FROM CONFIG WITH(NOLOCK) WHERE SECTIONSTR='Saglik-Net' AND PROGNAME='MEDISOFT' AND /* VALUESTR='1' AND*/ KEYNAME LIKE CONVERT(VARCHAR,SUBEID)+' numaral% %ubede Sa%l%k Net Online test modu aktif')='1' THEN
				'3bc26a7d-4a19-4edf-ab9f-1413ac85e7d0' /*TEST KTS FİRMA KODU*/
			ELSE
				'C740D0288ED4C45FE0407C0A04162BDD'  /*KTS TALYA*/ 
			END AS SNOKTSFIRMAKODU,
			SUBELER.SUBEADI SUBELER_SUBEADI
	INTO #TMPSUBELER
	FROM SUBELER
	WHERE SUBELER.SUBEID=@SUBEID

	SELECT 
			GS.SUBEID AS SUBEID,
			GSK.KLINIKKALITEKOD,
			GSK.KLINIKKALITESAYI,
			GSK.ENABIZSAYI,
			TKK.KLINIKKALITEADI,
			GS.USSGUNSONUGONDERIMID
	INTO #TMPGUNLUK
	FROM USSGUNSONUgoNDERIMKLINIKKALITE GSK
	JOIN USSGUNSONUgoNDERIM GS ON GS.USSGUNSONUGONDERIMID=GSK.USSGUNSONUGONDERIMID
	JOIN TKLINIKKALITE TKK ON TKK.KLINIKKALITEKOD=GSK.KLINIKKALITEKOD	
	WHERE GS.SUBEID=@SUBEID AND GS.TARIH=@PERYOTBASLANGICTARIHI

	SET @USSGUNSONUGONDERIMID = ISNULL((SELECT MAX(USSGUNSONUGONDERIMID) FROM #TMPGUNLUK),0)

	IF (SELECT COUNT(*) FROM #TMPGUNLUK) = 0 
	BEGIN
		SET @SAYIHATA=1
		SET @MESAJ= @MESAJ + ' Günlük sayıların hesaplanmasında bir hata oluştu '
	END




	IF @HATA='' AND @SAYIHATA=0
	BEGIN		
		DECLARE @TBLXMLBASLIK TABLE (XMLBASLIK NVARCHAR(2000), HATA NVARCHAR(2000),MESAJ NVARCHAR(2000))
		INSERT INTO @TBLXMLBASLIK (XMLBASLIK, HATA, MESAJ) EXEC SPUSS_000_XML_BASLIK @SUBEID		
		SELECT @HATA=HATA, @MESAJ=MESAJ, @XMLBASLIK=XMLBASLIK FROM @TBLXMLBASLIK		
	END


	IF @HATA = '' AND @SAYIHATA=0
	BEGIN




	

			
					BEGIN TRY
						INSERT INTO @goNDERIMTABLOSU (goNDERILECEKXML,HATA,MESAJ,XMLBASLIK,TABLOID,MINID,MAXID) 
						SELECT							 
							(
							 SELECT 
							   NEWID() as 'messageGuid/@value',
							   '1' as 'messageType/@version',
							   '0a9ba485-e7e0-4abb-9c86-0a14fd364bb8' as 'messageType/@codeSystemGuid',
							   '407' as 'messageType/@code',
							   'Gün Sonu Veri Seti' as 'messageType/@value',
							   LEFT(REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(19),GETDATE(),120),' ',''),'-',''),':',''),12) as 'documentGenerationTime/@value',
									 (
						 
										SELECT	
											'1' AS 'healthcareProvider/@version',
											'c3eade04-4f91-5dab-e043-14031b0ac9f9' as 'healthcareProvider/@codeSystemGuid',
											SAGLIKNETKURUMKODU as 'healthcareProvider/@code',
											SUBELER_SUBEADI as 'healthcareProvider/@value'  
											FOR XML PATH('author'), TYPE
									  ),
										--'C740D0288ED4C45FE0407C0A04162BDD' as 'firmaKodu/@value',
										SNOKTSFIRMAKODU as 'firmaKodu/@value',
										(
										SELECT 								
											(SELECT	
											'1' AS 'healthcareProvider/@version',
											'c3eade04-4f91-5dab-e043-14031b0ac9f9' as 'healthcareProvider/@codeSystemGuid',
											SAGLIKNETKURUMKODU as 'healthcareProvider/@code',
											SUBELER_SUBEADI as 'healthcareProvider/@value'  
											FOR XML PATH('KAYIT_YERI'), TYPE
											),	
											(
												SELECT 
												(SELECT
													Convert(CHAR(8),@PERYOTBASLANGICTARIHI,112) as 'GUN_SONU_TARIH/@value',
													(SELECT 
													
													'1' AS 'KLINIK_KALITE_TANIM/@version',
													'4fa9b630-c081-44e1-8b8c-0ef0f967c17d' AS 'KLINIK_KALITE_TANIM/@codeSystemGuid',
													ISNULL(CONVERT(VARCHAR(10),KLINIKKALITEKOD),'') AS 'KLINIK_KALITE_TANIM/@code',
													ISNULL(CONVERT(VARCHAR(10),KLINIKKALITEADI),'')  AS 'KLINIK_KALITE_TANIM/@value',
													ISNULL(CONVERT(VARCHAR(10),KLINIKKALITESAYI),'') as 'KLINIK_KALITE_SAYI /@value'
													
													FROM #TMPGUNLUK
													FOR XML PATH('KLINIK_KALITE_BILGISI'),TYPE
													)
												
												FOR XML PATH('GUN_SONU_BILGISI'),TYPE)
												FOR XML PATH('GUN_SONU_VERI_SETI'),TYPE
											)
						
											FOR XML PATH('recordData'), TYPE
											
									)
		   
				FROM #TMPSUBELER

				FOR XML PATH(''), ROOT('SYSMessage')
	
	
							) AS goNDERILECEKXML,
							@HATA AS HATA, 
							@MESAJ AS MESAJ,
							@XMLBASLIK AS XMLBASLIK,
							@USSGUNSONUGONDERIMID AS TABLOID,
							0 AS MINID,
							0 AS MAXID

					END TRY
					BEGIN CATCH
						SET @HATA = ERROR_MESSAGE()
						IF @HATA = ''
							SET @HATA = 'Bilinmeyen bir hata oluştu'

						INSERT INTO @goNDERIMTABLOSU (goNDERILECEKXML,HATA,MESAJ,XMLBASLIK,TABLOID,MINID,MAXID)			
						SELECT	'' AS goNDERILECEKXML,
								@HATA AS HATA, 
								@MESAJ AS MESAJ,
								'' AS XMLBASLIK,
								@USSGUNSONUGONDERIMID AS TABLOID,
								0 AS MINID,
								0 AS MAXID
			

					END CATCH
				



		

	END
	ELSE
	BEGIN

		INSERT INTO @goNDERIMTABLOSU (goNDERILECEKXML,HATA,MESAJ,XMLBASLIK,TABLOID,MINID,MAXID)
		SELECT	'' AS goNDERILECEKXML,
				@HATA AS HATA, 
				@MESAJ AS MESAJ,
				'' AS XMLBASLIK,
				@USSGUNSONUGONDERIMID AS TABLOID,
				0 AS MINID,
				0 AS MAXID

		SET @HATA = ''			 
	END



	
	IF OBJECT_ID('tempdb..#TMPGUNLUK') IS NOT NULL 
			DROP TABLE #TMPGUNLUK
	IF OBJECT_ID('tempdb..#TMPSUBELER') IS NOT NULL 
			DROP TABLE #TMPSUBELER

	SELECT * FROM @goNDERIMTABLOSU

END
GO


