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

CREATE PROCEDURE sp_FollowUpOraterv
  @intezmenyId int,
  @aktTanevId int,
  @kovetkezoTanevId int,
  @oratervId int,
  @tantervId int
AS
BEGIN

IF (@oratervId = 0)
	BEGIN
		INSERT INTO T_ORATERV_OSSZES (
	 C_EVFOLYAM
	,C_INTEZMENYID
	,C_NEV
	,C_TANEVID
	,C_TANTERVID
	,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
	,NNID
	,ELOZOTANEVIREKORDID
    ) SELECT 
	  IIF((SELECT COUNT(1) FROM T_EVFOLYAMTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_EVFOLYAM) = 0, 1296, t.C_EVFOLYAM) AS C_EVFOLYAM
      ,@intezmenyID AS C_INTEZMENYID
      ,C_NEV AS C_NEV
	  ,@kovetkezoTanevId AS C_TANEVID
	  ,(SELECT ID FROM T_TANTERV_OSSZES WHERE ELOZOTANEVIREKORDID = @tantervId AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID) AS C_TANTERVID
      ,TOROLT AS TOROLT
      ,0 AS SERIAL
      ,GETDATE() AS LASTCHANGED
      ,GETDATE() AS CREATED
      ,MODIFIER AS MODIFIER
      ,MODIFIER AS CREATOR
      ,NULL AS NNID
	  ,t.ID AS ELOZOTANEVIREKORDID
    FROM T_ORATERV_OSSZES t WHERE C_TANEVID = @aktTanevId AND C_INTEZMENYID = @intezmenyId AND TOROLT='F'
		AND 0 = (SELECT COUNT(1) FROM T_ORATERV_OSSZES WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyId AND ELOZOTANEVIREKORDID = t.ID)
 
		SET @oratervId = (SELECT ID FROM T_ORATERV_OSSZES t WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyId AND ELOZOTANEVIREKORDID = t.ID)

	END
ELSE
	BEGIN
		UPDATE kovetkezo SET
	 kovetkezo.C_EVFOLYAM = IIF((SELECT COUNT(1) FROM T_EVFOLYAMTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = aktiv.C_EVFOLYAM) = 0, 1296, aktiv.C_EVFOLYAM)
	,kovetkezo.C_NEV = aktiv.C_NEV
	,kovetkezo.TOROLT = aktiv.TOROLT
	FROM T_ORATERV_OSSZES kovetkezo
	INNER JOIN T_ORATERV_OSSZES aktiv ON aktiv.C_INTEZMENYID = kovetkezo.C_INTEZMENYID AND aktiv.C_TANEVID = @aktTanevId AND aktiv.ID = @oratervId AND aktiv.TOROLT='F'
	WHERE kovetkezo.C_INTEZMENYID = @intezmenyId AND kovetkezo.C_TANEVID = @kovetkezoTanevId AND kovetkezo.ELOZOTANEVIREKORDID = @oratervId AND kovetkezo.TOROLT='F'
	END


DELETE T_ORATERVTARGY_OSSZES WHERE C_INTEZMENYID = @intezmenyId AND C_TANEVID = @kovetkezoTanevId AND TOROLT='F'

INSERT INTO T_ORATERVTARGY (
	 C_EVESORASZAM
	,C_INTEZMENYID
	,C_ORATERVID
	,C_TANEVID
	,C_TANTARGYID
	,TOROLT
  ,SERIAL
  ,LASTCHANGED
  ,CREATED
  ,MODIFIER
  ,CREATOR
	,NNID
	,ELOZOTANEVIREKORDID
    ) SELECT 
       C_EVESORASZAM AS C_EVESORASZAM
      ,@intezmenyID AS C_INTEZMENYID
      ,(SELECT ID FROM T_ORATERV_OSSZES WHERE ELOZOTANEVIREKORDID = t.C_ORATERVID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID) AS C_ORATERVID
	  ,@kovetkezoTanevId AS C_TANEVID
	  ,(SELECT ID FROM T_TANTARGY_OSSZES WHERE ELOZOTANEVIREKORDID = t.C_TANTARGYID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID) AS C_TANTARGYID
      ,t.TOROLT AS TOROLT
      ,0 AS SERIAL
      ,GETDATE() AS LASTCHANGED
      ,GETDATE() AS CREATED
      ,t.MODIFIER AS MODIFIER
      ,t.MODIFIER AS CREATOR
      ,NULL AS NNID
	  ,t.ID AS ELOZOTANEVIREKORDID
    FROM T_ORATERVTARGY_OSSZES t 
      INNER JOIN T_ORATERV ot ON ot.ID=t.C_ORATERVID AND ot.TOROLT='F'
    WHERE t.C_TANEVID = @aktTanevId AND t.C_INTEZMENYID = @intezmenyId AND C_ORATERVID = @oratervId AND t.TOROLT='F'
      AND NOT EXISTS(SELECT TOP 1 ID FROM T_ORATERVTARGY_OSSZES WHERE ELOZOTANEVIREKORDID=t.ID)

END
GO
