﻿DROP PROCEDURE IF EXISTS uspGetFelhasznaloBelepesData
GO

CREATE PROCEDURE uspGetFelhasznaloBelepesData
  @pTanevId int

AS
BEGIN
  SET NOCOUNT ON;

  DECLARE @kovTanev bit = (SELECT IIF(C_KOVETKEZO = 'T', 1, 0) FROM T_TANEV_OSSZES WHERE ID = @pTanevId AND TOROLT = 'F')
  --NOTE: Alkalmazott
  SELECT
     fb.ID AS Id
    ,fb.C_BEJELENTKEZESINEV AS BejelentkezesiNev
    ,IIF(fb.C_JELSZO IS NULL, 'F', 'T') AS HasJelszo
    ,fb.C_IMPORTALT AS Importalt
    ,fh.ID AS FelhasznaloId
    ,fh.C_NEVSORREND AS FelhasznaloNevsorrend
    ,fh.C_ELOTAG AS FelhasznaloElotag
    ,fh.C_VEZETEKNEV AS FelhasznaloVezeteknev
    ,fh.C_UTONEV AS FelhasznaloKeresztnev
    ,fh.C_SZULETESIHELY AS FelhasznaloSzuletesiHely
    ,fh.C_SZULETESIDATUM AS FelhasznaloSzuletesiIdo
    ,fh.C_ANYJANEVESORREND AS AnyjaNeveNevsorrend
    ,fh.C_ANYJANEVEELOTAG AS AnyjaNeveElotag
    ,fh.C_ANYJAVEZETEKNEVE AS AnyjaVezetekneve
    ,fh.C_ANYJAUTONEVE AS AnyjaUtoneve
    ,fh.C_OKTATASIAZONOSITO AS FelhasznaloOktatasiAzonosito
    ,a.ID AS AlkalmazottId
    ,NULL AS TanuloId
    ,NULL AS GondviseloId
    ,NULL AS GondviseloNev
    ,NULL AS GondviseloRokonsagiFokId
    ,NULL AS Osztalynev
  FROM T_ALKALMAZOTT_OSSZES a
    INNER JOIN T_FELHASZNALO_OSSZES fh ON fh.ID = a.ID AND fh.C_TANEVID = a.C_ALTANEVID AND fh.TOROLT = 'F'
    LEFT JOIN T_FELHASZNALOBELEPES_OSSZES fb ON fb.C_FELHASZNALOID = fh.ID AND fb.C_GONDVISELOID IS NULL AND fb.C_TANEVID = a.C_ALTANEVID AND fb.TOROLT = 'F'
  WHERE a.C_ALTANEVID = @pTanevId
    AND a.TOROLT = 'F'
  UNION
  --NOTE: Tanuló
  SELECT
     fb.ID AS Id
    ,fb.C_BEJELENTKEZESINEV AS BejelentkezesiNev
    ,IIF(fb.C_JELSZO IS NULL, 'F', 'T') AS HasJelszo
    ,fb.C_IMPORTALT AS Importalt
    ,fh.ID AS FelhasznaloId
    ,fh.C_NEVSORREND AS FelhasznaloNevsorrend
    ,fh.C_ELOTAG AS FelhasznaloElotag
    ,fh.C_VEZETEKNEV AS FelhasznaloVezeteknev
    ,fh.C_UTONEV AS FelhasznaloKeresztnev
    ,fh.C_SZULETESIHELY AS FelhasznaloSzuletesiHely
    ,fh.C_SZULETESIDATUM AS FelhasznaloSzuletesiIdo
    ,fh.C_ANYJANEVESORREND AS AnyjaNeveNevsorrend
    ,fh.C_ANYJANEVEELOTAG AS AnyjaNeveElotag
    ,fh.C_ANYJAVEZETEKNEVE AS AnyjaVezetekneve
    ,fh.C_ANYJAUTONEVE AS AnyjaUtoneve
    ,fh.C_OKTATASIAZONOSITO AS FelhasznaloOktatasiAzonosito
    ,NULL AS AlkalmazottId
    ,dk.ID AS TanuloId
    ,NULL AS GondviseloId
    ,NULL AS GondviseloNev
    ,NULL AS GondviseloRokonsagiFokId
    ,NULL AS Osztalynev
  FROM T_TANULO_OSSZES dk
    INNER JOIN T_FELHASZNALO_OSSZES fh ON fh.ID = dk.ID AND fh.C_TANEVID = dk.C_ALTANEVID AND fh.TOROLT = 'F'
    LEFT JOIN T_FELHASZNALOBELEPES_OSSZES fb ON fb.C_FELHASZNALOID = fh.ID AND fb.C_GONDVISELOID IS NULL AND fb.C_TANEVID = dk.C_ALTANEVID AND fb.TOROLT = 'F'
  WHERE dk.C_ALTANEVID = @pTanevId
    AND dk.TOROLT = 'F'
  UNION
  --NOTE: Gondviselő
  SELECT
     fb.ID AS Id
    ,fb.C_BEJELENTKEZESINEV AS BejelentkezesiNev
    ,IIF(fb.C_JELSZO IS NULL, 'F', 'T') AS HasJelszo
    ,fb.C_IMPORTALT AS Importalt
    ,fh.ID AS FelhasznaloId
    ,fh.C_NEVSORREND AS FelhasznaloNevsorrend
    ,fh.C_ELOTAG AS FelhasznaloElotag
    ,fh.C_VEZETEKNEV AS FelhasznaloVezeteknev
    ,fh.C_UTONEV AS FelhasznaloKeresztnev
    ,fh.C_SZULETESIHELY AS FelhasznaloSzuletesiHely
    ,fh.C_SZULETESIDATUM AS FelhasznaloSzuletesiIdo
    ,fh.C_ANYJANEVESORREND AS AnyjaNeveNevsorrend
    ,fh.C_ANYJANEVEELOTAG AS AnyjaNeveElotag
    ,fh.C_ANYJAVEZETEKNEVE AS AnyjaVezetekneve
    ,fh.C_ANYJAUTONEVE AS AnyjaUtoneve
    ,fh.C_OKTATASIAZONOSITO AS FelhasznaloOktatasiAzonosito
    ,NULL AS AlkalmazottId
    ,dk.ID AS TanuloId
    ,g.ID AS GondviseloId
    ,g.C_NEV AS GondviseloNev
    ,g.C_ROKONSAGFOKA AS GondviseloRokonsagiFokId
    ,Osztaly.OsztalyNev AS Osztalynev
  FROM T_GONDVISELO_OSSZES g
    INNER JOIN T_FELHASZNALO_OSSZES fh ON fh.ID = g.C_TANULOID AND fh.C_TANEVID = g.C_TANEVID AND fh.TOROLT = 'F'
    INNER JOIN T_TANULO_OSSZES dk ON dk.ID = g.C_TANULOID AND dk.C_ALTANEVID = g.C_TANEVID AND dk.TOROLT = 'F'
    LEFT JOIN T_FELHASZNALOBELEPES_OSSZES fb ON fb.C_FELHASZNALOID = fh.ID AND fb.C_GONDVISELOID = g.ID AND fb.C_TANEVID = g.C_TANEVID AND fb.TOROLT = 'F'
    CROSS APPLY dbo.fnGetTanuloOsztalyString(g.C_TANULOID, NULL, NULL, 1, @kovTanev, NULL) AS osztaly
  WHERE g.C_TANEVID = @pTanevId
    AND g.TOROLT = 'F'
  UNION
  --NOTE: Egyéb(fenntartó, admin, stb...)
  SELECT
     fb.ID AS Id
    ,fb.C_BEJELENTKEZESINEV AS BejelentkezesiNev
    ,IIF(fb.C_JELSZO IS NULL, 'F', 'T') AS HasJelszo
    ,fb.C_IMPORTALT AS Importalt
    ,fh.ID AS FelhasznaloId
    ,fh.C_NEVSORREND AS FelhasznaloNevsorrend
    ,fh.C_ELOTAG AS FelhasznaloElotag
    ,fh.C_VEZETEKNEV AS FelhasznaloVezeteknev
    ,fh.C_UTONEV AS FelhasznaloKeresztnev
    ,fh.C_SZULETESIHELY AS FelhasznaloSzuletesiHely
    ,fh.C_SZULETESIDATUM AS FelhasznaloSzuletesiIdo
    ,fh.C_ANYJANEVESORREND AS AnyjaNeveNevsorrend
    ,fh.C_ANYJANEVEELOTAG AS AnyjaNeveElotag
    ,fh.C_ANYJAVEZETEKNEVE AS AnyjaVezetekneve
    ,fh.C_ANYJAUTONEVE AS AnyjaUtoneve
    ,fh.C_OKTATASIAZONOSITO AS FelhasznaloOktatasiAzonosito
    ,NULL AS AlkalmazottId
    ,NULL AS TanuloId
    ,NULL AS GondviseloId
    ,NULL AS GondviseloNev
    ,NULL AS GondviseloRokonsagiFokId
    ,NULL AS Osztalynev
  FROM T_FELHASZNALO_OSSZES fh
    LEFT JOIN T_FELHASZNALOBELEPES_OSSZES fb ON fb.C_FELHASZNALOID = fh.ID AND fb.C_GONDVISELOID IS NULL AND fb.C_TANEVID = fh.C_TANEVID AND fb.TOROLT = 'F'
      WHERE NOT EXISTS (SELECT ID FROM T_TANULO_OSSZES dk WHERE dk.ID = fh.ID AND dk.C_ALTANEVID = fh.C_TANEVID AND dk.TOROLT = 'F')
        AND NOT EXISTS (SELECT ID FROM T_ALKALMAZOTT_OSSZES a WHERE a.ID = fh.ID AND a.C_ALTANEVID = fh.C_TANEVID AND a.TOROLT = 'F')
        AND fh.C_TANEVID = @pTanevId
        AND fh.TOROLT = 'F'
END

GO
