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

CREATE PROCEDURE [dbo].SPUSS_405_GUNLUK_VERI_SORGULAMA    -- SPUSS_405_GUNLUK_VERI_SORGULAMA

	@SUBEID INT,
	@SORGULAMATARIHI int,
	@TUMUNUTEKRAR BIT = 0
AS
BEGIN
	--SPUSS_405_GUNLUK_VERI_SORGULAMA 1,'02.24.2026',1
	DECLARE @HATA NVARCHAR(2000) = ''
	DECLARE @MESAJ NVARCHAR(2000) = ''
	DECLARE @SAYIHATA BIT = 0
	DECLARE @XMLBASLIK NVARCHAR(2000) = ''
	--DECLARE @SUBEID INT = 1
	DECLARE @GONDERIMTABLOSU TABLE 
		(
			GONDERILECEKXML XML,
			HATA NVARCHAR(2000),
			MESAJ NVARCHAR(2000),	
			XMLBASLIK NVARCHAR(3500),
			TABLOID INT,
			MINID INT,
			MAXID INT
		)


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


    IF @SUBEID = NULL  or @SUBEID = 0 
		SET @HATA = @HATA + 'Şube tanımzsız yada girilmemiş! '

    IF @SORGULAMATARIHI= 0 or @SORGULAMATARIHI = null
		SET @HATA = @HATA + 'Sorgulama tarihi girilmemiş! '
   

	SELECT	   SUBEADI AS SUBELER_SUBEADI,			
			   SUBEID AS HDOSYA_SUBEID,
			   
			   --ISNULL((SELECT TOP 1 VALUESTR FROM CONFIG WITH(NOLOCK) WHERE SECTIONSTR='Saglik-Net' AND PROGNAME='MEDISOFT' AND KEYNAME= CONVERT(VARCHAR,HDOSYA.SUBEID)+' numaralı şubenin kurum numarası'),'') AS SAGLIKNETKURUMKODU,
	    ISNULL((SELECT TOP 1 VALUESTR FROM CONFIG WITH(NOLOCK) WHERE SECTIONSTR='Saglik-Net' AND PROGNAME='MEDISOFT' AND KEYNAME= CONVERT(VARCHAR,SUBEID)+' numarali subede Yenidoğanların Kimliksiz fatura edilebilir gün sayısı'),'') 	AS SAGLIKNETKIMLIKSIZYENIDOGANGUN,

        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,

		MDLTESISKODU AS MEDULA_TESIS_KODU	

		
		INTO #TMPSUBE
	
		from SUBELER  WITH(NOLOCK) 
		where SUBEID=@SUBEID 
		
		


	IF (SELECT COUNT(*) FROM #TMPSUBE WHERE HDOSYA_SUBEID IS NULL) > 0 
		SET @HATA = @HATA + 'İşlemin şube kaydı bulunamadı! '


	IF @HATA='' AND @SAYIHATA=0
	BEGIN		
		--SELECT @SUBEID = HDOSYA_SUBEID FROM #TMPSUBE
		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

		DECLARE @MINHESAPISLID INT = 0
		DECLARE @MAXHESAPISLID INT = 0
	

					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',
									'405' as 'messageType/@code',
									'Günlük Veri Sorgulama' 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
																		
																		  LEFT(REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(19),cast('20'+@SORGULAMATARIHI as datetime),120),' ',''),'-',''),':',''),12)  AS 'GUNLUK_VERI_SORGULAMA/SORGU_TARIHI/@value'

																		FOR XML PATH(''), TYPE
                                                                   )
																													
													FOR XML PATH('recordData'), TYPE
										)
		   
				FROM #TMPSUBE

				FOR XML PATH(''), ROOT('SYSMessage')
	
	
							) AS GONDERILECEKXML,
							@HATA AS HATA, 
							@MESAJ AS MESAJ,
							@XMLBASLIK AS XMLBASLIK,
							@SORGULAMATARIHI AS TABLOID,
							@MINHESAPISLID AS MINID,
							@MAXHESAPISLID 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,
								@SORGULAMATARIHI 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,	
				@SORGULAMATARIHI AS TABLOID,
				0 AS MINID,
				0 AS MAXID		 
	END

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


	SELECT * FROM @GONDERIMTABLOSU


END
GO


