DROP PROCEDURE IF EXISTS uspFollowUpCsengetesiRend
GO

CREATE PROCEDURE uspFollowUpCsengetesiRend
   @intezmenyId int
  ,@aktTanevId int
  ,@kovetkezoTanevId int
  ,@csengetesiRendId int
AS
BEGIN
  SET NOCOUNT ON;

  IF NOT EXISTS (SELECT 1 from T_TANEV where ID = @kovetkezoTanevId and C_KOVETKEZO = 'T' and C_AKTIVALVA is null and C_ISKIVALASZTHATO = 'T')
    BEGIN
      RETURN;
    END

  DECLARE 
     @csengrendNev nvarchar(100)
    ,@kovCsengrendId int
    ,@insertColList nvarchar(max)
    ,@selectColList nvarchar(max)
    ,@updateColList nvarchar(max)
    ,@sql nvarchar(max)
  
  CREATE TABLE #columnMapping (
     insertColName nvarchar(max) COLLATE DATABASE_DEFAULT
    ,selectColName nvarchar(max)
    ,updateColName nvarchar(max)
  )
  INSERT INTO #columnMapping (
     insertColName 
    ,selectColName 
    ,updateColName 
  ) SELECT
     insertColName 
    ,selectColName 
    ,updateColName 
  FROM fnGetFollowUpColumnMapping (NULL,'T_CSENGETESIREND')

  SELECT @csengrendNev = C_NEV FROM T_CSENGETESIREND_OSSZES WHERE ID = @csengetesiRendId
  
  IF EXISTS (SELECT 1 FROM T_CSENGETESIREND_OSSZES WHERE C_TANEVID=@kovetkezoTanevId AND TOROLT='F' AND C_NEV=@csengrendNev AND ISNULL(ELOZOTANEVIREKORDID,0)<>@csengetesiRendId)
  BEGIN
    UPDATE T_CSENGETESIREND_OSSZES SET ELOZOTANEVIREKORDID = NULL WHERE ELOZOTANEVIREKORDID=@csengetesiRendId
    UPDATE T_CSENGETESIREND_OSSZES SET ELOZOTANEVIREKORDID = @csengetesiRendId WHERE C_TANEVID=@kovetkezoTanevId AND TOROLT='F' AND C_NEV=@csengrendNev AND ISNULL(ELOZOTANEVIREKORDID,0)<>@csengetesiRendId
  END

  SELECT @kovCsengrendId = ID FROM T_CSENGETESIREND_OSSZES WHERE ELOZOTANEVIREKORDID=@csengetesiRendId

  IF (SELECT TOROLT FROM T_CSENGETESIREND_OSSZES WHERE ID = @csengetesiRendId)='T' OR (SELECT TOROLT FROM T_CSENGETESIREND_OSSZES WHERE ID=@kovCsengrendId)='T'
  BEGIN
    RETURN
  END
  IF @kovCsengrendId IS NULL BEGIN
    SELECT @insertColList = ISNULL(@insertColList+','+insertColname,insertColname) FROM #columnMapping 
    SELECT @selectColList = ISNULL(@selectColList+','+selectColName,selectColName) FROM #columnMapping 
    SET @sql = N'
      INSERT INTO T_CSENGETESIREND_OSSZES (
        '+@insertColList+' 
      ) SELECT
         '+@selectColList+'
      FROM T_CSENGETESIREND_OSSZES s
        LEFT JOIN T_MUKODESIHELY_OSSZES mh ON mh.ELOZOTANEVIREKORDID = s.C_MUKODESIHELYID AND mh.TOROLT = ''F'' AND mh.C_TANEVID = @kovetkezoTanevId
      WHERE s.C_TANEVID = @aktTanevId AND s.ID = @csengetesiRendId AND s.TOROLT = ''F'''
  END
  ELSE
  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_CSENGETESIREND_OSSZES t
        INNER JOIN T_CSENGETESIREND_OSSZES s ON s.ID = t.ELOZOTANEVIREKORDID AND s.C_TANEVID = @aktTanevId AND s.ID = @csengetesiRendId
      WHERE t.TOROLT = ''F'' AND t.C_TANEVID = @kovetkezoTanevId AND t.ELOZOTANEVIREKORDID = @csengetesiRendId'
  END
  --PRINT @sql
  EXEC sp_executesql
     @sql,N'
     @intezmenyId int
    ,@aktTanevId int
    ,@kovetkezoTanevId int
    ,@csengetesiRendId int'
    ,@intezmenyId = @intezmenyId 
    ,@aktTanevId = @aktTanevId 
    ,@kovetkezoTanevId = @kovetkezoTanevId 
    ,@csengetesiRendId = @csengetesiRendId 

  IF (SELECT C_AKTIV FROM T_CSENGETESIREND_OSSZES WHERE ELOZOTANEVIREKORDID=@csengetesiRendId AND TOROLT='F')='T' BEGIN
    UPDATE T_CSENGETESIREND_OSSZES SET C_AKTIV='F' WHERE C_TANEVID=@kovetkezoTanevId AND TOROLT='F' AND ELOZOTANEVIREKORDID<>@csengetesiRendId
  END

END
GO
