﻿DROP PROCEDURE IF EXISTS uspGetHazifeladatCsatolmanyData
GO

CREATE PROCEDURE uspGetHazifeladatCsatolmanyData
   @pTanevId int
  ,@pOraDatuma datetime = NULL
  ,@pOsztalyCsoportId int = NULL
  ,@pTanarId int = NULL
  ,@pTantargyId int = NULL
  ,@pFajlNeve nvarchar(max) = NULL
  ,@pFajlMeretTol int = NULL
  ,@pFajlMeretIg int = NULL
  ,@pFeltoltesDatumaTol datetime = NULL
  ,@pFeltoltesDatumaIg datetime = NULL
  ,@pHataridoTol datetime = NULL
  ,@pHataridoIg datetime = NULL
  ,@pFeladatKategoriaId int = NULL
  ,@pFeladatEllatasiHelyId int = NULL
  ,@pIsOnlineOra bit = NULL
AS BEGIN
  SET NOCOUNT ON;
  
  DECLARE @sql nvarchar(max)

  SET @sql = N'
  SELECT
     cs.ID AS ID
    ,ISNULL(tOra.C_DATUM, hf.C_DATUM) AS OraDatuma
    ,hf.C_ORASZAM AS OraSorszama
    ,ocs.C_NEV AS OsztalyCsoport
    ,ocs.ID AS OsztalyCsoportId
    ,f.C_NYOMTATASINEV AS Tanar
    ,f.ID AS TanarId
    ,ta.C_NEV AS Tantargy
    ,ta.ID AS TantargyId
    ,cs.C_FILENEV AS FajlNev
    ,cs.C_FILESIZEBYTE AS FajlMeret
    ,cs.C_FELTOLTESDATUM AS FeltoltesDatum
    ,hf.C_BEADASHATARIDO AS HaziFeladatHataridoDatuma
    ,cs.TOROLT AS Torolt
    ,torlo.C_NYOMTATASINEV AS TorloUser
    ,hfcs.DELETED AS TorlesDatum
    ,hf.ID AS HazifeladatId
    ,ISNULL(totOnlineOra.C_BOOLERTEK, ottOnlineOra.C_BOOLDEFAULT) AS IsOnlineOra
  FROM T_DKT_FILE_OSSZES cs
    INNER JOIN T_DKT_FELADATFILE_OSSZES hfcs ON hfcs.C_FILEID = cs.ID
    INNER JOIN T_DKT_FELADAT_OSSZES hf ON hf.ID = hfcs.C_FELADATID AND hf.C_FELADATTIPUSID = 0
    INNER JOIN T_TANTARGY_OSSZES ta ON ta.ID = hf.C_TANTARGYID
    INNER JOIN T_OSZTALYCSOPORT_OSSZES ocs ON ocs.ID = hf.C_OSZTALYCSOPORTID
    INNER JOIN T_FELHASZNALO_OSSZES f ON f.ID = hf.C_ROGZITOALKALMAZOTTID
    LEFT JOIN (
      T_TANITASIORA_OSSZES tOra
      INNER JOIN T_ORATULAJDONSAGTIPUS_OSSZES ottOnlineOra ON ottOnlineOra.TOROLT = ''F'' AND ottOnlineOra.C_ALTANEVID = tOra.C_TANEVID AND ottOnlineOra.ID = 8615
      LEFT JOIN T_TANITASIORATULAJDONSAG_OSSZES totOnlineOra ON totOnlineOra.C_ORATULAJDONSAGID = ottOnlineOra.ID AND totOnlineOra.C_TANEVID = ottOnlineOra.C_ALTANEVID AND totOnlineOra.TOROLT = ''F'' AND totOnlineOra.C_TANITASIORAID = tOra.ID
    ) ON tOra.ID = hf.C_TANITASIORAID
    LEFT JOIN T_FELHASZNALO_OSSZES torlo ON torlo.ID = hfcs.DELETER
  WHERE cs.C_TANEVID = @pTanevId
  '

  SET @sql += IIF(@pOraDatuma IS NOT NULL, N'
    AND ISNULL(tOra.C_DATUM, hf.C_ROGZITESIDOPONT) = @pOraDatuma', '')
  SET @sql += IIF(@pTantargyId IS NOT NULL, N'
    AND hf.C_TANTARGYID = @pTantargyId', '')
  SET @sql += IIF(@pOsztalyCsoportId IS NOT NULL, N'
    AND hf.C_OSZTALYCSOPORTID = @pOsztalyCsoportId', '')
  SET @sql += IIF(@pFeladatKategoriaId IS NULL, N'', N' 
    AND ocs.C_FELADATKATEGORIAID = @pFeladatKategoriaId')
  SET @sql += IIF(@pFeladatEllatasiHelyId IS NULL, N'', N' 
    AND ocs.C_FELADATELLATASIHELYID = @pFeladatEllatasiHelyId')
  SET @sql += IIF(@pTanarId IS NOT NULL, N'
    AND hf.C_ROGZITOALKALMAZOTTID = @pTanarId', '')
  SET @sql += IIF(@pFajlNeve IS NOT NULL, N'
    AND cs.C_FILENEV LIKE ''%'' + @pFajlNeve + ''%''', '')
  SET @sql += IIF(@pFajlMeretTol IS NOT NULL, N'
    AND cs.C_FILESIZEBYTE >= @pFajlMeretTol', '')
  SET @sql += IIF(@pFajlMeretIg IS NOT NULL, N'
    AND cs.C_FILESIZEBYTE <= @pFajlMeretIg', '')
  SET @sql += IIF(@pFeltoltesDatumaTol IS NOT NULL, N'
    AND cs.C_FELTOLTESDATUM >= @pFeltoltesDatumaTol', '')
  SET @sql += IIF(@pFeltoltesDatumaIg IS NOT NULL, N'
    AND CONVERT(date, cs.C_FELTOLTESDATUM) <= @pFeltoltesDatumaIg', '')
  SET @sql += IIF(@pHataridoTol IS NOT NULL, N'
    AND hf.C_BEADASHATARIDO >= @pHataridoTol', '')
  SET @sql += IIF(@pHataridoIg IS NOT NULL, N'
    AND hf.C_BEADASHATARIDO <= @pHataridoIg', '')
  SET @sql += IIF(@pIsOnlineOra IS NOT NULL, N'
    AND ISNULL(totOnlineOra.C_BOOLERTEK, ottOnlineOra.C_BOOLDEFAULT) = ' + IIF(@pIsOnlineOra = 1, '''T''', '''F'''), '')

  EXEC sp_executesql @sql
    ,N'
     @pTanevId int
    ,@pOraDatuma datetime
    ,@pOsztalyCsoportId int
    ,@pTanarId int
    ,@pTantargyId int
    ,@pFajlNeve nvarchar(max)
    ,@pFajlMeretTol int
    ,@pFajlMeretIg int
    ,@pFeltoltesDatumaTol datetime
    ,@pFeltoltesDatumaIg datetime
    ,@pHataridoTol datetime
    ,@pHataridoIg datetime
    ,@pFeladatKategoriaId int
    ,@pFeladatEllatasiHelyId int
    '
    ,@pTanevId = @pTanevId
    ,@pOraDatuma = @pOraDatuma
    ,@pOsztalyCsoportId = @pOsztalyCsoportId
    ,@pTanarId = @pTanarId
    ,@pTantargyId = @pTantargyId
    ,@pFajlNeve = @pFajlNeve
    ,@pFajlMeretTol = @pFajlMeretTol
    ,@pFajlMeretIg = @pFajlMeretig
    ,@pFeltoltesDatumaTol = @pFeltoltesDatumaTol
    ,@pFeltoltesDatumaIg = @pFeltoltesDatumaIg
    ,@pHataridoTol = @pHataridoTol
    ,@pHataridoIg = @pHataridoIg
    ,@pFeladatKategoriaId = @pFeladatKategoriaId
    ,@pFeladatEllatasiHelyId = @pFeladatEllatasiHelyId

END

GO

