﻿DROP PROCEDURE IF EXISTS uspGetFogadooraIdopont
GO

CREATE PROCEDURE uspGetFogadooraIdopont
 @tanevid				INT,
 @rendszeressegTipusId	INT,
 @datum					DATE,
 @tanevUtolsoNapja		DATE

AS
BEGIN

DECLARE @hetnapjaId DATETIME, @hetirendId INT
SELECT  @hetnapjaId = C_HETNAPJA, @hetirendId = C_HETIREND FROM T_NAPTARINAP WHERE C_NAPDATUMA = @datum AND C_TANEVID = @tanevid AND TOROLT = 'F'

SELECT 
	C_NAPDATUMA		Datum
FROM T_NAPTARINAP Naptarinap
	LEFT JOIN T_TANEVRENDJE Tanevrendje ON Naptarinap.C_NAPDATUMA = Tanevrendje.C_DATUM 
					AND Tanevrendje.TOROLT = 'F' 
					AND Tanevrendje.C_OSSZESCSOPORTRAVONATKOZIK = 'T'
					AND Tanevrendje.C_TANEVID = @tanevid
WHERE	
	C_NAPDATUMA BETWEEN @datum AND @tanevUtolsoNapja 
	AND (@rendszeressegTipusId <> 2 /*Hetirendenként*/ OR Naptarinap.C_HETIREND = @hetirendId)
	AND Naptarinap.C_HETNAPJA = @hetnapjaId 
	AND Naptarinap.C_TANEVID = @tanevId 
	AND Naptarinap.TOROLT = 'F'
	AND (Tanevrendje.C_NAPTIPUSA IS NULL OR Tanevrendje.C_NAPTIPUSA NOT IN (1386, 1391, 1396, 1397, 1398, 1399))
END

GO