﻿SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

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


CREATE PROCEDURE [dbo].[sp_GetAdminDashboardNaplo]
  @pTanevId INT
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.
	SET NOCOUNT ON;
	CREATE TABLE #AKTIV_ALKALMAZOTT (ID int,INDEX IX1 CLUSTERED(ID));  
	INSERT INTO #AKTIV_ALKALMAZOTT (ID)
	SELECT f.ID
	FROM T_FELHASZNALO f
	inner join T_MUNKAUGYIADATOK munkaugyiadat ON munkaugyiadat.C_ALKALMAZOTTID = f.ID AND munkaugyiadat.C_TARTOSHELYETTESITES = 'F' AND ISNULL(munkaugyiadat.C_ALKALMAZASKEZDETE,getdate()) <= getdate() AND ISNULL(munkaugyiadat.C_ALKALMAZASMEGSZUNESE,getdate()) >= getdate() AND munkaugyiadat.TOROLT = 'F'
	WHERE f.C_TANEVID = @pTanevId
  SELECT
     ia.C_NEV AS IntezmenyNev
    ,i.C_AZONOSITO AS IntezmenyAzonosito
    ,i.C_ALTERNATIVAZONOSITO AS IntezmenyAlternativAzonosito
    ,tv.C_NEV AS TanevNev
    ,(SELECT IIF(LEFT(REPLACE(JSON_VALUE(C_ERTEK, '$.Date'),'-',''), 8) > GETDATE(), 1, 0)
        FROM T_RENDSZERBEALLITAS_OSSZES rb 
        WHERE C_BEALLITASTIPUS = 3082 
          AND rb.C_TANEVID = @pTanevId 
          AND rb.TOROLT = 'F'
     ) AS ZarasiGond
    ,(SELECT COUNT(1) 
        FROM T_ALKALMAZOTT adat 
		INNER JOIN #AKTIV_ALKALMAZOTT aa ON aa.ID = adat.ID
        WHERE NOT EXISTS (SELECT 1 FROM T_FELHASZNALOBELEPES fb WHERE adat.ID = fb.C_FELHASZNALOID AND fb.TOROLT = 'F')
	        AND adat.C_ALTANEVID = @pTanevId 
          AND adat.TOROLT = 'F' 							
		 ) AS NincsBelepesAlkalmazott
    ,(SELECT COUNT(1) 
        FROM T_TANULO adat 
        WHERE NOT EXISTS (SELECT 1 FROM T_FELHASZNALOBELEPES fb WHERE adat.ID = fb.C_FELHASZNALOID AND fb.TOROLT = 'F' AND fb.C_GONDVISELOID IS NULL)
	        AND adat.C_ALTANEVID = @pTanevId 
          AND adat.TOROLT = 'F' 							
		 ) AS NincsBelepesTanulo
    ,(SELECT COUNT(1) 
        FROM T_GONDVISELO adat 
        WHERE NOT EXISTS (SELECT 1 FROM T_FELHASZNALOBELEPES fb WHERE adat.ID = fb.C_GONDVISELOID AND fb.TOROLT = 'F')
	        AND adat.C_TANEVID = @pTanevId 
          AND adat.TOROLT = 'F' 							
		 ) AS NincsBelepesGondviselo
    ,(SELECT COUNT(1)
	      FROM T_ALKALMAZOTT_OSSZES adat
		  INNER JOIN #AKTIV_ALKALMAZOTT aa ON aa.ID = adat.ID
          LEFT JOIN T_FELHASZNALOBELEPES_OSSZES fb ON fb.C_FELHASZNALOID = adat.ID AND fb.TOROLT = 'F'
		  WHERE adat.TOROLT = 'F' 
          AND (fb.C_UTOLSOBELEPES <= DATEADD(DAY,-28,GETDATE()) OR fb.C_UTOLSOBELEPES IS NULL) 
      ) AS NemLepettBeHonapAlkalmazott
     ,(SELECT COUNT(1)
	      FROM T_ALKALMAZOTT_OSSZES adat
		  INNER JOIN #AKTIV_ALKALMAZOTT aa ON aa.ID = adat.ID
          LEFT JOIN T_FELHASZNALOBELEPES_OSSZES fb ON fb.C_FELHASZNALOID = adat.ID AND fb.TOROLT = 'F'
        WHERE adat.TOROLT = 'F' 
          AND (fb.C_UTOLSOBELEPES <= DATEADD(DAY,-14,GETDATE()) OR fb.C_UTOLSOBELEPES IS NULL) 
      ) AS NemLepettBeKetHetAlkalmazott
  FROM T_TANEV_OSSZES tv
    INNER JOIN T_INTEZMENY_OSSZES i ON i.ID = tv.C_INTEZMENYID AND i.TOROLT = 'F'
    INNER JOIN T_INTEZMENYADATOK_OSSZES  ia ON ia.C_INTEZMENYID = tv.C_INTEZMENYID AND ia.C_TANEVID = tv.ID AND ia.TOROLT = 'F'
  WHERE tv.TOROLT = 'F' AND tv.ID = @pTanevId

END
GO

