﻿DROP PROCEDURE IF EXISTS uspFollowUpAlkalmazottTovabbiMunkaugy
GO

CREATE PROCEDURE uspFollowUpAlkalmazottTovabbiMunkaugy
   @intezmenyId int
  ,@aktTanevId int
  ,@kovetkezoTanevId int
  ,@tovabbiMunkaugyiAdatId int
AS
BEGIN
  SET NOCOUNT ON;
  DECLARE 
     @kovTovabbiMunkaugyiAdatId int
    ,@selectColList nvarchar(max)
    ,@insertColList nvarchar(max)
    ,@updateColList nvarchar(max)
    ,@sql nvarchar(max)

  SELECT 
    @kovTovabbiMunkaugyiAdatId = ID
  FROM T_TOVABBIMUNKAUGYIADATOK_OSSZES WHERE ELOZOTANEVIREKORDID = @tovabbiMunkaugyiAdatId  
  
  IF (SELECT TOROLT FROM T_TOVABBIMUNKAUGYIADATOK_OSSZES WHERE ID = @tovabbiMunkaugyiAdatId) = 'T' OR (SELECT TOROLT FROM T_TOVABBIMUNKAUGYIADATOK_OSSZES WHERE ID = @kovTovabbiMunkaugyiAdatId) = 'T' BEGIN
    RETURN
  END

  CREATE TABLE #ColumnMapping (selectColName nvarchar(128),insertColName nvarchar(128),updateColName nvarchar(128))
  INSERT INTO #ColumnMapping(
     selectColName
    ,insertColname
    ,updateColname
  )
  SELECT 
     selectColName
    ,insertColName
    ,updateColName 
  FROM fnGetFollowUpColumnMapping (NULL,'T_TOVABBIMUNKAUGYIADATOK')
  
  DECLARE @ujAlkalmazottId int = (
    SELECT a.ID 
    FROM T_ALKALMAZOTT_OSSZES a 
      INNER JOIN T_TOVABBIMUNKAUGYIADATOK_OSSZES ma ON ma.C_ALKALMAZOTTID = a.ELOZOTANEVIREKORDID AND ma.ID = @tovabbiMunkaugyiAdatId)

  IF @kovTovabbiMunkaugyiAdatId IS NOT NULL BEGIN
    SELECT 
      @updateColList = ISNULL(@updateColList+','+insertColname+' = '+updateColname,insertColName+' = '+updateColname) 
    FROM #columnMapping WHERE updateColName IS NOT NULL
    SET @sql = N'
      UPDATE t SET
        '+@updateColList+'
      FROM T_TOVABBIMUNKAUGYIADATOK_OSSZES t
        INNER JOIN T_TOVABBIMUNKAUGYIADATOK_OSSZES s ON t.ELOZOTANEVIREKORDID = s.ID AND s.ID=@tovabbiMunkaugyiAdatId
        INNER JOIN T_FELADATELLATASIHELY_OSSZES f ON f.ELOZOTANEVIREKORDID=s.C_FELADATELLATASIHELYID AND f.TOROLT=''F''
      WHERE t.TOROLT=''F'' AND t.ID = @kovTovabbiMunkaugyiAdatId'    
  END
  ELSE
  BEGIN
    SELECT @insertColList = ISNULL(@insertColList+','+insertColname,insertColname) FROM #columnMapping
    SELECT @selectColList = ISNULL(@selectColList+','+selectColName,selectColName) FROM #columnMapping
    SET @sql = N'
	  INSERT INTO T_TOVABBIMUNKAUGYIADATOK(
		   '+@insertColList+'
	  ) SELECT 
		   '+@selectColList+'
	  FROM T_TOVABBIMUNKAUGYIADATOK_OSSZES s
	  	INNER JOIN T_FELADATELLATASIHELY_OSSZES f ON f.ELOZOTANEVIREKORDID = s.C_FELADATELLATASIHELYID AND f.TOROLT=''F''
	  	INNER JOIN T_FELHASZNALO_OSSZES fh ON fh.ELOZOTANEVIREKORDID = s.C_ALKALMAZOTTID
	  WHERE s.ID=@tovabbiMunkaugyiAdatId'
	END

  --PRINT @sql
  EXEC sp_executesql
     @sql,N'
     @intezmenyId int
    ,@aktTanevId int
    ,@kovetkezoTanevId int
    ,@tovabbiMunkaugyiAdatId int
    ,@kovTovabbiMunkaugyiAdatId int
    ,@ujAlkalmazottId int'
    ,@intezmenyId = @intezmenyId 
    ,@aktTanevId = @aktTanevId
    ,@kovetkezoTanevId = @kovetkezoTanevId
    ,@tovabbiMunkaugyiAdatId = @tovabbiMunkaugyiAdatId
    ,@kovTovabbiMunkaugyiAdatId = @kovTovabbiMunkaugyiAdatId
    ,@ujAlkalmazottId = @ujAlkalmazottId

END
GO
