IF OBJECT_ID('sp_GetNemMegtartottOrakSzamaByHet') IS NOT NULL BEGIN
  DROP PROCEDURE sp_GetNemMegtartottOrakSzamaByHet
END
GO

CREATE PROCEDURE [sp_GetNemMegtartottOrakSzamaByHet] 
	@osztalycsoportId int
	,@intezmenyId int
	,@tanevId int
	,@hetSorszam int
AS
BEGIN

declare @aktualisHetHetirendje int = (select c_hetirend from T_NAPTARIHET where C_HETSORSZAMA=@hetSorszam and TOROLT='F' and c_tanevid=@tanevId)

SELECT 
		o.ID AS Id
		,@hetSorszam AS HetSzama
		,n.C_NAPDATUMA AS Datum
		,n.C_HETNAPJA AS Hetnapja
		,o.C_ORASZAM AS Oraszam
		,ocst.C_NEV AS OsztalyNev
		,tt.C_NEV AS TargyNev
		,felh.C_NYOMTATASINEV AS TanarNev
	FROM T_NAPTARINAP n
  		INNER JOIN T_ORARENDIORA o ON o.C_HETIREND in (@aktualisHetHetirendje, 1554) and n.C_HETNAPJA = o.C_HETNAPJA AND n.C_NAPDATUMA BETWEEN o.C_ORAERVENYESSEGKEZDETE AND o.C_ORAERVENYESSEGVEGE
  		INNER JOIN fnGetKapcsolodoOsztalycsoportok(@osztalycsoportId) ocs ON ocs.ID = o.C_OSZTALYCSOPORTID
		INNER JOIN T_OSZTALYCSOPORT ocst ON ocst.ID = ocs.ID AND ocst.TOROLT = 'F'
		INNER JOIN T_FOGLALKOZAS f ON f.ID = o.C_FOGLALKOZASID AND f.TOROLT = 'F'
		INNER JOIN T_TANTARGY tt ON tt.ID = f.C_TANTARGYID AND tt.TOROLT = 'F'
		LEFT JOIN T_FOGLALKOZASOK_TANAROK ft ON ft.C_FOGLALKOZASOKID = f.ID AND f.TOROLT='F'
		LEFT JOIN T_FELHASZNALO felh ON felh.ID = ft.C_TANAROKID
		LEFT JOIN T_TANEVRENDJE tr ON tr.C_DATUM = n.C_NAPDATUMA
		LEFT JOIN T_TANITASIORA t ON t.C_ORARENDIORAID = o.ID AND t.C_NAPTARINAPID = n.ID and t.torolt='F'
	WHERE n.C_NAPDATUMA < GETDATE()
		AND o.C_HETNAPJA IN (1408, 1409, 1410, 1411, 1412)
		AND n.C_TANEVID = @tanevId
		AND n.C_HETSORSZAMA = @hetSorszam
		AND n.C_ORARENDINAP='T'
		AND t.ID IS NULL
		and o.TOROLT='F'
		and (o.C_TANORANKIVULIFOGLALKOZAS = 'F'  or o.C_TANORANKIVULIFOGLALKOZAS is null)
		and o.C_CSENGETESIRENDORAID is not null
	ORDER BY n.C_NAPDATUMA, o.C_ORASZAM

END

