﻿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 @kovAlkalmazottId int
  
  SELECT @kovAlkalmazottId = ID
  FROM T_FELHASZNALO_OSSZES 
  WHERE ELOZOTANEVIREKORDID = @alkalmazottId 
    AND C_TANEVID = @kovetkezoTanevId
  
  IF (SELECT TOROLT FROM T_FELHASZNALO_OSSZES WHERE ID = @alkalmazottId) = 'T' OR (SELECT TOROLT FROM T_FELHASZNALO_OSSZES WHERE ID = @kovAlkalmazottId) = 'T' BEGIN
    RETURN
  END

  UPDATE kov set
     kov.C_FOKOZAT = akt.C_FOKOZAT
		,kov.C_MINOSITESDATUM = akt.C_MINOSITESDATUM
		,kov.C_PORTFOLIOSTATUSZ = akt.C_PORTFOLIOSTATUSZ
		,kov.C_INTEZMENYID = @intezmenyId
		,kov.C_TANEVID = @kovetkezoTanevId
		,kov.SERIAL += 1
		,kov.LASTCHANGED = GETDATE()
    ,kov.MODIFIER = akt.MODIFIER
  FROM T_PEDAGOGUSELETPALYAMODELL_OSSZES kov
    INNER JOIN T_PEDAGOGUSELETPALYAMODELL_OSSZES akt ON kov.ELOZOTANEVIREKORDID = akt.ID
  WHERE akt.C_TANEVID = @aktTanevId 
    AND akt.C_INTEZMENYID = @intezmenyId 
    AND akt.C_TANARID = @alkalmazottId
    AND akt.TOROLT = 'F' 
    AND kov.C_TANARID = @kovAlkalmazottId 
    AND kov.C_TANEVID = @kovetkezoTanevId 
    AND kov.C_INTEZMENYID = @intezmenyId 
    AND kov.TOROLT = 'F'


	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 
     akt.C_FOKOZAT AS C_FOKOZAT
	  ,akt.C_MINOSITESDATUM AS C_MINOSITESDATUM
	  ,akt.C_PORTFOLIOSTATUSZ AS C_PORTFOLIOSTATUSZ
	  ,@kovAlkalmazottId  AS C_TANARID
	  ,@intezmenyId AS C_INTEZMENYID
	  ,@kovetkezoTanevId AS C_TANEVID
	  ,akt.TOROLT AS TOROLT
	  ,0 AS SERIAL
	  ,GETDATE() AS LASTCHANGED
		,GETDATE() AS CREATED
		,akt.MODIFIER AS MODIFIER
		,akt.MODIFIER AS CREATOR
	  ,akt.NNID AS NNID
	  ,akt.ID AS ELOZOTANEVIREKORDID
  FROM T_PEDAGOGUSELETPALYAMODELL_OSSZES akt
  WHERE akt.C_TANEVID = @aktTanevId 
    AND akt.C_INTEZMENYID = @intezmenyId 
    AND akt.C_TANARID = @alkalmazottId 
    AND akt.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1  FROM T_PEDAGOGUSELETPALYAMODELL_OSSZES kov WHERE kov.ELOZOTANEVIREKORDID = akt.ID AND kov.C_TANEVID = @kovetkezoTanevId AND kov.C_TANARID = @kovAlkalmazottId)

END
GO

