﻿DROP PROCEDURE IF EXISTS sp_FizikaiTorlesOrarendek
GO

-- =============================================
-- Description:	<Fizikailag töröljük az intézmény adott tanévére az órarendeket>
-- =============================================
CREATE PROCEDURE [dbo].[sp_FizikaiTorlesOrarendek]
	 @pIntezmenyId	INT
	,@pTanevId		INT
	,@pUserId		INT

AS
BEGIN

	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.
	SET NOCOUNT ON;

	UPDATE T_TANITASIORA_OSSZES SET
		 C_ORARENDIORAID = NULL
		,SERIAL += 1
		,LASTCHANGED = GETDATE()
		,MODIFIER = @pUserId
	FROM
		T_TANITASIORA_OSSZES
	WHERE
			C_INTEZMENYID = @pIntezmenyId
		AND C_TANEVID = @pTanevId
		AND C_ORARENDIORAID IS NOT NULL
	
	UPDATE T_HAZIFELADATOK_OSSZES SET
		 TOROLT = 'T'
		,SERIAL += 1
		,LASTCHANGED = GETDATE()
		,MODIFIER = @pUserId
	FROM
		T_HAZIFELADATOK_OSSZES
	WHERE
			C_INTEZMENYID = @pIntezmenyId
		AND C_TANEVID = @pTanevId
		AND C_TANITASIORAID IS NULL



	DELETE FROM
		T_HELYETTESITESIIDOSZAK_OSSZES
	WHERE
			C_HELYETTESITETTORARENDID IN (
				SELECT ID
				FROM
					T_ORARENDIORA_OSSZES
				WHERE
						C_INTEZMENYID = @pIntezmenyId
					AND C_TANEVID = @pTanevId
			)
		AND C_INTEZMENYID = @pIntezmenyId
		AND C_TANEVID = @pTanevId

	CREATE TABLE #TempFajlIDs (ID int)
  DELETE fajlorarend 
    OUTPUT DELETED.C_FELTOLTOTTFAJLID INTO #TempFajlIDs
    FROM T_FELTOLTOTTFAJL_ORARENDIORA fajlorarend 
        INNER JOIN T_FELTOLTOTTFAJL fajl ON fajl.ID = fajlorarend.C_FELTOLTOTTFAJLID 
  WHERE C_INTEZMENYID = @pIntezmenyId
          AND C_TANEVID = @pTanevId
  DELETE fajl FROM T_FELTOLTOTTFAJL fajl
      INNER JOIN #TempFajlIDs ids ON ids.ID = fajl.ID
	DELETE FROM
		T_ORARENDIORA_OSSZES
	WHERE
			C_INTEZMENYID = @pIntezmenyId
		AND C_TANEVID = @pTanevId

END


GO

