DROP PROCEDURE IF EXISTS uspGetHaziFeladatokByDateRange
GO

CREATE PROCEDURE uspGetHaziFeladatokByDateRange
   @pIntezmenyId int
  ,@pTanevId int
  ,@pDatumTol datetime = NULL
  ,@pDatumIg datetime = NULL
AS BEGIN
  SET NOCOUNT ON;

  SELECT
     hf.ID AS ID
    ,hf.C_DATUM AS Datum
    ,COALESCE(t.C_ORARENDIORAGROUPID, oo.C_ORARENDIORAGROUPID) AS OrarendiOraGroupId
    ,oo.C_ORARENDIORAID
    ,hf.C_TANITASIORAID AS TanitasiOraId
    ,hf.C_BEADASHATARIDO AS BeadasHatarido
    ,hf.C_LATHATOSAGIDOPONT LathatosagIdopont
    ,hf.C_TANTARGYID AS Tantargy
    ,hf.C_ALKALMAZOTTID AS Tanar
    ,hf.C_OSZTALYCSOPORTID AS OsztalyCSoport
    ,hf.C_ORASZAM AS Oraszam
    ,hf.C_IDOPONT AS Idopont
  FROM T_DKT_FELADAT hf
  LEFT JOIN T_TANITASIORA t ON t.ID = hf.C_TANITASIORAID AND t.TOROLT = 'F'
  LEFT JOIN T_ORARENDTELJES oo ON  oo.C_TANEVID = hf.C_TANEVID
                               AND oo.C_INTEZMENYID = hf.C_INTEZMENYID
                               AND oo.C_TANTARGYID = hf.C_TANTARGYID 
                               AND oo.C_OSZTALYCSOPORTID = hf.C_OSZTALYCSOPORTID 
                               AND oo.C_TANARID = hf.C_ALKALMAZOTTID 
                               AND oo.C_DATUM = hf.C_DATUM
                               AND (oo.C_ORASZAM = hf.C_ORASZAM OR CAST(oo.C_ORAKEZDETE AS time(0)) = CAST(hf.C_IDOPONT AS time(0)))
  WHERE hf.TOROLT = 'F'
  AND hf.C_DATUM BETWEEN @pDatumtol AND @pDatumig
  AND hf.C_INTEZMENYID = @pIntezmenyId
  AND hf.C_TANEVID = @pTanevId
  AND hf.C_FELADATTIPUSID = 0

END
GO