﻿-- ==============================================================================
-- Author: Szekeres András
-- Create date:  2018.04.24.
-- Description:  Egy tanuló felmentéseinek frissítése a következõ tanévre
-- ==============================================================================

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

CREATE PROCEDURE sp_FollowUpTanuloFelmentes
  @intezmenyId int,
  @aktTanevId int,
  @kovTanevId int,
  @tanuloId int
AS
BEGIN
  DECLARE @kovTanuloId int;
  
  SELECT @kovTanuloId = kov.ID
  FROM T_FELHASZNALO_OSSZES akt
  INNER JOIN T_FELHASZNALO_OSSZES kov ON akt.C_OKTATASIAZONOSITO = kov.C_OKTATASIAZONOSITO AND kov.C_TANEVID = @kovTanevId AND kov.C_INTEZMENYID = @intezmenyId
  WHERE akt.ID = @tanuloId
    AND akt.C_INTEZMENYID = @intezmenyId
    AND akt.C_TANEVID = @aktTanevId
    AND kov.TOROLT = 'F'
    AND akt.TOROLT = 'F'

  IF @kovTanuloId IS NOT NULL BEGIN
    DELETE FROM T_TANULOMENTESSEG_OSSZES
    WHERE C_TANULOID = @kovTanuloId
      AND C_INTEZMENYID = @intezmenyId
      AND C_TANEVID = @kovTanevId;

    INSERT INTO T_TANULOMENTESSEG_OSSZES (
       C_ERTEKELESMENTESITES
      ,C_KEZDETE
      ,C_MENTESSEGOKA
      ,C_ORAMENTESITES
      ,C_VEGE
      ,C_SZOVEGESENERTEKELHETO
      ,C_TANTARGYID
      ,C_TANULOID
      ,C_INTEZMENYID
      ,C_TANEVID
      ,TOROLT
      ,SERIAL
      ,LASTCHANGED
      ,CREATED
      ,MODIFIER
      ,CREATOR
      ,NNID
      ,ELOZOTANEVIREKORDID
    )  SELECT
       C_ERTEKELESMENTESITES
      ,C_KEZDETE
      ,C_MENTESSEGOKA
      ,C_ORAMENTESITES
      ,C_VEGE
      ,C_SZOVEGESENERTEKELHETO
      ,C_TANTARGYID
      ,@kovTanuloId
      ,C_INTEZMENYID
      ,@kovTanevId
      ,TOROLT
      ,0
      ,GETDATE()
      ,GETDATE()
      ,MODIFIER
      ,MODIFIER
      ,NULL
      ,ID
    FROM T_TANULOMENTESSEG_OSSZES
    WHERE C_TANULOID = @tanuloId
      AND C_INTEZMENYID = @intezmenyId
      AND C_TANEVID = @aktTanevId
      AND TOROLT = 'F';
  END
END
GO
