﻿DROP PROCEDURE IF EXISTS uspGetAllSchemaKikuldendoHazifeladat
GO

CREATE PROCEDURE uspGetAllSchemaKikuldendoHazifeladat
AS
BEGIN
  SET NOCOUNT ON;

  SELECT TOP(5000) WITH TIES
     hf.ID
    ,hf.C_XEROPANBUNDLEID AS XeropanBundleId
    ,i.C_AZONOSITO AS IntezmenyAzonosito
    ,tv.ID AS TanevId
    ,tt.C_NEV AS TantargyNev
    ,fh.ID AS TanuloId
    ,fh.C_IDPEGYEDIAZONOSITO AS IdpUniqueId
    ,fh.C_NYOMTATASINEV AS TanuloNev
    ,hf.C_FELADATTIPUSID AS FeladatTipusId
    ,hf.C_DATUM AS Datum
    ,hf.C_ORASZAM AS OraSzam
  FROM T_DKT_FELADAT hf
    INNER JOIN T_TANEV tv ON tv.ID = hf.C_TANEVID
      AND tv.C_AKTIV = 'T'
      AND tv.TOROLT = 'F'
    INNER JOIN T_INTEZMENY i ON i.ID = tv.C_INTEZMENYID
      AND i.TOROLT = 'F'
    INNER JOIN T_TANTARGY tt ON tt.ID = hf.C_TANTARGYID
      AND tt.TOROLT = 'F'
    INNER JOIN T_OSZTALYCSOPORT ocs ON ocs.ID = hf.C_OSZTALYCSOPORTID
      AND ocs.TOROLT = 'F'
    INNER JOIN T_TANULOCSOPORT tcs ON tcs.C_OSZTALYCSOPORTID = ocs.ID
      AND ((tcs.C_BELEPESDATUM <= hf.C_BEADASHATARIDO
      AND (tcs.C_KILEPESDATUM IS NULL OR tcs.C_KILEPESDATUM > hf.C_BEADASHATARIDO)) OR hf.C_BEADASHATARIDO IS NULL)
      AND tcs.C_BELEPESDATUM < GETDATE() AND (tcs.C_KILEPESDATUM IS NULL OR tcs.C_KILEPESDATUM >= GETDATE())
      AND tcs.TOROLT = 'F'
    INNER JOIN T_FELHASZNALO fh ON fh.ID = tcs.C_TANULOID
      AND fh.TOROLT = 'F'
  WHERE hf.TOROLT = 'F'
    AND hf.C_ISERTESITESKIKULDVE = 'F'
    --NOTE: A "DATEADD(hh, -7, GETDATE())"-ra azért van szükség, mert enélkül éjfélkor küldenénk ki a felhasználóknak push notification-ökat, így viszont eltoljuk reggel 7 órára, hogy ne éjfélkor bombázzuk őket push-okkal.
    AND (hf.C_LATHATOSAGIDOPONT IS NOT NULL AND CAST(hf.C_LATHATOSAGIDOPONT AS DATE) <= DATEADD(hh, -7, GETDATE()))
  ORDER BY hf.C_DATUM, hf.ID

END

GO
