DROP PROCEDURE IF EXISTS [dbo].[uspGetFelmentesAdatok]
GO

CREATE PROCEDURE [dbo].[uspGetFelmentesAdatok]
   @pIntezmenyId INT
  ,@pTanevId INT
  ,@pFelmentesId INT = NULL
  ,@pTanuloNeve NVARCHAR(255) = NULL
  ,@pOsztalyId INT = NULL
  ,@pTantargyId INT = NULL
  ,@pFelmentesKezdeteTol DATETIME = NULL
  ,@pFelmentesKezdeteIg DATETIME = NULL
  ,@pFelmentesVegeTol DATETIME = NULL
  ,@pFelmentesVegeIg DATETIME = NULL
  ,@pIsTanoraLatogatasaAloliFelmentes BIT = NULL
  ,@pIsErtekelesAloliFelmentes BIT = NULL
  ,@pIsSzovegesenErtekelheto BIT = NULL
  ,@pFelmentesOka NVARCHAR(4000) = NULL
  ,@pRogzitesDatumaTol DATETIME = NULL
  ,@pRogzitesDatumaIg DATETIME = NULL
  ,@pFelmentesRogzitoje NVARCHAR(255) = NULL
  ,@pUtolsoModositasTol DATETIME = NULL
  ,@pUtolsoModositasIg DATETIME = NULL
  ,@pUtolsoModosito NVARCHAR(255) = NULL
  ,@pIsFelmentesTorolt BIT = NULL
AS
BEGIN
  SET NOCOUNT ON;

  DECLARE
     @evaluationDate DATETIME = GETDATE()
    ,@staticSql NVARCHAR(MAX) = ''
    ,@sql NVARCHAR(MAX) = ''
    ,@isTanoraLatogatasaAloliFelmentes CHAR(1) = IIF(@pIsTanoraLatogatasaAloliFelmentes = 1, 'T', 'F')
    ,@isErtekelesAloliFelmentes CHAR(1) = IIF(@pIsErtekelesAloliFelmentes = 1, 'T', 'F')
    ,@isSzovegesenErtekelheto CHAR(1) = IIF(@pIsSzovegesenErtekelheto = 1, 'T', 'F')
    ,@isFelmentesTorolt CHAR(1) = IIF(@pIsFelmentesTorolt = 1, 'T', 'F')

  SET @staticSql = N'
  SELECT
     tm.ID AS FelmentesId
    ,tm.TOROLT AS IsFelmentesTOROLT
    ,felt.C_NYOMTATASINEV AS TanuloNev
    ,osztaly.OsztalyNev AS OsztalyNev
    ,ttgy.ID AS TantargyId 
    ,ttgy.C_NEV AS TantargyNev
    ,tm.C_KEZDETE AS FelmentesKezdete
    ,tm.C_VEGE AS FelmentesVege
    ,tm.C_ORAMENTESITES AS IsTanoraLatogatasaAloliFelmentes
    ,tm.C_ERTEKELESMENTESITES AS IsErtekelesAloliFelmentes
    ,tm.C_SZOVEGESENERTEKELHETO AS IsSzovegesenErtekelheto
    ,tm.C_MENTESSEGOKA AS FelmentesOka
    ,tm.C_FELMENTESROGZITESIDOPONTJA AS RogzitesDatuma
    ,ISNULL(felc.C_NYOMTATASINEV, N''Nincs adat'') AS FelmentesRogzitoje
    ,tm.C_UTOLSOMODOSITASIDOPONTJA AS UtolsoModositas
    ,ISNULL(felm.C_NYOMTATASINEV, N''Nincs adat'') AS UtolsoModosito
  FROM
    T_TANULOMENTESSEG_OSSZES AS tm
    INNER JOIN T_TANULO_OSSZES AS t ON t.TOROLT = ''F'' AND tm.C_TANULOID = t.ID AND tm.C_INTEZMENYID = t.C_ALINTEZMENYID AND tm.C_TANEVID = t.C_ALTANEVID
    INNER JOIN T_FELHASZNALO_OSSZES AS felt ON felt.TOROLT = ''F'' AND t.ID = felt.ID AND t.C_ALINTEZMENYID = felt.C_INTEZMENYID AND t.C_ALTANEVID = felt.C_TANEVID
    CROSS APPLY dbo.fnGetTanuloOsztalyString(t.ID, @evaluationDate, NULL, 1, 0, @pOsztalyId) AS osztaly
    INNER JOIN T_TANTARGY_OSSZES AS ttgy ON ttgy.TOROLT = ''F'' AND tm.C_TANTARGYID = ttgy.ID AND tm.C_INTEZMENYID = ttgy.C_INTEZMENYID AND ttgy.C_TANEVID = tm.C_TANEVID
    LEFT JOIN T_FELHASZNALO_OSSZES AS felc ON felc.ID = tm.C_FELMENTESROGZITOTANARID AND tm.C_INTEZMENYID = felc.C_INTEZMENYID AND tm.C_TANEVID = felc.C_TANEVID
    LEFT JOIN T_FELHASZNALO_OSSZES AS felm ON felm.ID = tm.C_UTOLSOMODOSITOTANARID AND tm.C_INTEZMENYID = felm.C_INTEZMENYID AND tm.C_TANEVID = felm.C_TANEVID
  WHERE
    tm.C_INTEZMENYID = @pIntezmenyId
    AND tm.C_TANEVID = @pTanevId
    AND osztaly.OsztalyNev IS NOT NULL';

  IF @pFelmentesId IS NOT NULL
  BEGIN

    SET @sql = @staticSql + ' AND tm.ID = @pFelmentesId';

    EXECUTE sp_executesql
       @stmt = @sql
      ,@params = N'
         @evaluationDate DATETIME
        ,@pIntezmenyId INT
        ,@pTanevId INT
        ,@pFelmentesId INT
        ,@pOsztalyId INT'
      ,@evaluationDate = @evaluationDate
      ,@pIntezmenyId = @pIntezmenyId
      ,@pTanevId = @pTanevId
      ,@pFelmentesId = @pFelmentesId
      ,@pOsztalyId = NULL;

  END
  ELSE
  BEGIN

    SET @sql = CONCAT(
        @staticSql
      , IIF(@pTanuloNeve > N'', N' AND felt.C_NYOMTATASINEV LIKE N''%'' + @pTanuloNeve  + N''%''', NULL)
      , IIF(@pTantargyId IS NOT NULL, N' AND ttgy.ID = @pTantargyId', NULL)
      , IIF(@pFelmentesKezdeteTol IS NOT NULL, N' AND (tm.C_KEZDETE IS NULL OR tm.C_KEZDETE >= @pFelmentesKezdeteTol)', NULL)
      , IIF(@pFelmentesKezdeteIg IS NOT NULL, N' AND (tm.C_KEZDETE IS NULL OR tm.C_KEZDETE <= @pFelmentesKezdeteIg)', NULL)
      , IIF(@pFelmentesVegeTol IS NOT NULL, N' AND (tm.C_VEGE IS NULL OR tm.C_VEGE >= @pFelmentesVegeTol)', NULL)
      , IIF(@pFelmentesVegeIg IS NOT NULL, N' AND (tm.C_VEGE IS NULL OR tm.C_VEGE <= @pFelmentesVegeIg)', NULL)
      , IIF(@pIsTanoraLatogatasaAloliFelmentes IS NOT NULL, N' AND tm.C_ORAMENTESITES = @pIsTanoraLatogatasaAloliFelmentes', NULL)
      , IIF(@pIsErtekelesAloliFelmentes IS NOT NULL, N' AND tm.C_ERTEKELESMENTESITES = @pIsErtekelesAloliFelmentes', NULL)
      , IIF(@pIsSzovegesenErtekelheto IS NOT NULL, N' AND tm.C_SZOVEGESENERTEKELHETO = @pIsSzovegesenErtekelheto', NULL)
      , IIF(@pFelmentesOka > N'', N' AND tm.C_MENTESSEGOKA LIKE N''%'' + @pFelmentesOka  + N''%''', NULL)
      , IIF(@pRogzitesDatumaTol IS NOT NULL, N' AND (tm.C_FELMENTESROGZITESIDOPONTJA IS NULL OR tm.C_FELMENTESROGZITESIDOPONTJA >= @pRogzitesDatumaTol)', NULL)
      , IIF(@pRogzitesDatumaIg IS NOT NULL, N' AND (tm.C_FELMENTESROGZITESIDOPONTJA IS NULL OR tm.C_FELMENTESROGZITESIDOPONTJA < @pRogzitesDatumaIg)', NULL)
      , IIF(@pFelmentesRogzitoje > N'', N' AND felc.C_NYOMTATASINEV LIKE N''%'' + @pFelmentesRogzitoje  + N''%''', NULL)
      , IIF(@pUtolsoModositasTol IS NOT NULL, N' AND (tm.C_UTOLSOMODOSITASIDOPONTJA IS NULL OR tm.C_UTOLSOMODOSITASIDOPONTJA >= @pUtolsoModositasTol)', NULL)
      , IIF(@pUtolsoModositasIg IS NOT NULL, N' AND (tm.C_UTOLSOMODOSITASIDOPONTJA IS NULL OR tm.C_UTOLSOMODOSITASIDOPONTJA < @pUtolsoModositasIg)', NULL)
      , IIF(@pUtolsoModosito > N'', N' AND felm.C_NYOMTATASINEV LIKE N''%'' + @pUtolsoModosito  + N''%''', NULL)
      , IIF(@pIsFelmentesTorolt IS NOT NULL, N' AND tm.TOROLT = @pIsFelmentesTorolt', NULL)
    );

    EXECUTE sp_executesql
       @stmt = @sql
      ,@params = N'
         @evaluationDate DATETIME
        ,@pIntezmenyId INT
        ,@pTanevId INT
        ,@pTanuloNeve NVARCHAR(255)
        ,@pOsztalyId INT
        ,@pTantargyId INT
        ,@pFelmentesKezdeteTol DATETIME
        ,@pFelmentesKezdeteIg DATETIME
        ,@pFelmentesVegeTol DATETIME
        ,@pFelmentesVegeIg DATETIME
        ,@pIsTanoraLatogatasaAloliFelmentes CHAR(1)
        ,@pIsErtekelesAloliFelmentes CHAR(1)
        ,@pIsSzovegesenErtekelheto CHAR(1)
        ,@pFelmentesOka NVARCHAR(4000)
        ,@pRogzitesDatumaTol DATETIME
        ,@pRogzitesDatumaIg DATETIME
        ,@pFelmentesRogzitoje NVARCHAR(255)
        ,@pUtolsoModositasTol DATETIME
        ,@pUtolsoModositasIg DATETIME
        ,@pUtolsoModosito NVARCHAR(255)
        ,@pIsFelmentesTorolt CHAR(1)'
      ,@evaluationDate = @evaluationDate
      ,@pIntezmenyId = @pIntezmenyId
      ,@pTanevId = @pTanevId
      ,@pTanuloNeve = @pTanuloNeve
      ,@pOsztalyId = @pOsztalyId
      ,@pTantargyId = @pTantargyId
      ,@pFelmentesKezdeteTol = @pFelmentesKezdeteTol
      ,@pFelmentesKezdeteIg = @pFelmentesKezdeteIg
      ,@pFelmentesVegeTol = @pFelmentesVegeTol
      ,@pFelmentesVegeIg = @pFelmentesVegeIg
      ,@pIsTanoraLatogatasaAloliFelmentes = @isTanoraLatogatasaAloliFelmentes
      ,@pIsErtekelesAloliFelmentes = @isErtekelesAloliFelmentes
      ,@pIsSzovegesenErtekelheto = @isSzovegesenErtekelheto
      ,@pFelmentesOka = @pFelmentesOka
      ,@pRogzitesDatumaTol = @pRogzitesDatumaTol
      ,@pRogzitesDatumaIg = @pRogzitesDatumaIg
      ,@pFelmentesRogzitoje = @pFelmentesRogzitoje
      ,@pUtolsoModositasTol = @pUtolsoModositasTol
      ,@pUtolsoModositasIg = @pUtolsoModositasIg
      ,@pUtolsoModosito = @pUtolsoModosito
      ,@pIsFelmentesTorolt = @isFelmentesTorolt;

  END

END
GO
