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

CREATE PROCEDURE sp_FollowUpAlkalmazottPEP
  @intezmenyId int,
  @aktTanevId int,
  @kovetkezoTanevId int,
  @alkalmazottId int
AS
BEGIN

	DECLARE @KovTanevAlkalmazottId INT;
		SET @KovTanevAlkalmazottId = (SELECT ID FROM T_ALKALMAZOTT_OSSZES WHERE ELOZOTANEVIREKORDIDA = @alkalmazottId AND C_ALTANEVID = @kovetkezoTanevId AND TOROLT = 'F')
		
	DELETE FROM T_PEDAGOGUSELETPALYAMODELL_OSSZES WHERE C_TANARID = @KovTanevAlkalmazottId

	INSERT INTO T_PEDAGOGUSELETPALYAMODELL_OSSZES (
			C_FOKOZAT
			,C_MINOSITESDATUM
			,C_PORTFOLIOSTATUSZ
			,C_TANARID
			,C_INTEZMENYID
			,C_TANEVID
			,TOROLT
			,SERIAL
			,LASTCHANGED
			,CREATED
			,MODIFIER
			,CREATOR
			,NNID
			,ELOZOTANEVIREKORDID
			) SELECT 
			  IIF((SELECT COUNT(1) FROM T_ELETPALYAMODELLFOKOZATTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_FOKOZAT) = 0,6462,t.C_FOKOZAT) AS C_FOKOZAT
			  ,C_MINOSITESDATUM AS C_MINOSITESDATUM
			  ,IIF((SELECT COUNT(1) FROM T_ELETPALYAMODELLSTATUSZOK_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_PORTFOLIOSTATUSZ) = 0,6521,t.C_PORTFOLIOSTATUSZ) AS C_PORTFOLIOSTATUSZ
			  ,@KovTanevAlkalmazottId AS C_TANARID
			  ,@intezmenyID AS C_INTEZMENYID
			  ,@kovetkezoTanevId AS C_TANEVID
			  ,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_PEDAGOGUSELETPALYAMODELL_OSSZES t WHERE C_TANEVID = @aktTanevId AND C_INTEZMENYID = @intezmenyId 
				AND 0 = (SELECT COUNT(1) FROM T_PEDAGOGUSELETPALYAMODELL_OSSZES WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyId AND ELOZOTANEVIREKORDID = t.ID)

END
GO
