﻿--Migration
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


IF OBJECT_ID('[dbo].[sp_GetAdminDashboardNaplo]') IS NOT NULL 
BEGIN
  DROP PROCEDURE [dbo].[sp_GetAdminDashboardNaplo]
END  
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;
	
SELECT
                intezmenyAdatok.C_NEV																						    IntezmenyNev,
                intezmeny.C_AZONOSITO																					        IntezmenyAzonosito,
                intezmeny.C_ALTERNATIVAZONOSITO																		            IntezmenyAlternativAzonosito,
                tanev.C_NEV																									    TanevNev,
                (SELECT IIF(
                    (SELECT CAST(REPLACE(SUBSTRING(rb.C_ERTEK,CHARINDEX('Date',rb.C_ERTEK)+7,10), '-', '') AS DATETIME)
                        FROM T_RENDSZERBEALLITAS_OSSZES rb WHERE rb.C_ERTEK LIKE '%Naplozaras_eves_datuma%'
                        AND rb.C_TANEVID = @pTanevId AND rb.TOROLT = 'F')
                        > GETDATE(),1,0)
                    )       ZarasiGond,
               
                    (SELECT SUM(IIF(fb.ID IS NULL,1,0)) FROM T_FELHASZNALO_OSSZES adat
	                INNER JOIN T_ALKALMAZOTT_OSSZES a ON a.ID = adat.ID AND a.C_ALTANEVID = adat.C_TANEVID
	                LEFT JOIN T_FELHASZNALOBELEPES_OSSZES fb ON fb.TOROLT = 'F' AND fb.C_FELHASZNALOID = adat.ID AND fb.C_TANEVID = @pTanevId
                    INNER JOIN T_MUNKAUGYIADATOK_OSSZES m ON m.C_ALKALMAZOTTID = adat.ID AND m.C_TANEVID = adat.C_TANEVID AND m.TOROLT = 'F'
	                WHERE adat.C_TANEVID = @pTanevId AND adat.TOROLT = 'F' 							
					)	    NincsBelepesAlkalmazott,

                (SELECT SUM(IIF(fb.ID IS NULL,1,0)) FROM T_FELHASZNALO_OSSZES adat
	                INNER JOIN T_TANULO_OSSZES a ON a.ID = adat.ID AND a.C_ALTANEVID = adat.C_TANEVID
	                LEFT JOIN T_FELHASZNALOBELEPES_OSSZES fb ON fb.TOROLT = 'F' AND fb.C_FELHASZNALOID = adat.ID AND fb.C_TANEVID = @pTanevId AND fb.C_GONDVISELOID IS NULL
	                WHERE adat.C_TANEVID = @pTanevId AND adat.TOROLT = 'F' 
                    )       NincsBelepesTanulo,


                (SELECT SUM(IIF(fb.ID IS NULL,1,0)) FROM T_FELHASZNALO_OSSZES adat
	                INNER JOIN T_GONDVISELO_OSSZES g ON g.C_TANULOID = adat.ID AND g.C_TANEVID = adat.C_TANEVID AND g.TOROLT = 'F'
	                LEFT JOIN T_FELHASZNALOBELEPES_OSSZES fb ON fb.TOROLT = 'F' AND fb.C_TANEVID = adat.C_TANEVID AND fb.C_GONDVISELOID = g.ID
	                WHERE adat.C_TANEVID = @pTanevId AND adat.TOROLT = 'F'
                    )       NincsBelepesGondviselo,

                (SELECT COUNT(1)
	                FROM T_FELHASZNALO_OSSZES adat
	                INNER JOIN T_ALKALMAZOTT_OSSZES a ON a.ID = adat.ID AND a.C_ALTANEVID = adat.C_TANEVID
                    LEFT JOIN T_FELHASZNALOBELEPES_OSSZES fb ON fb.C_FELHASZNALOID = adat.ID AND fb.TOROLT = 'F'
                    INNER JOIN T_MUNKAUGYIADATOK_OSSZES m ON m.C_ALKALMAZOTTID = adat.ID AND m.C_TANEVID = adat.C_TANEVID AND m.TOROLT = 'F'
	                WHERE (adat.TOROLT = 'F' AND adat.C_TANEVID = @pTanevId AND (fb.C_UTOLSOBELEPES <= DATEADD(DAY,-28,GETDATE()) OR fb.C_UTOLSOBELEPES IS NULL) AND m.C_TARTOSHELYETTESITES = 'F' AND (m.C_ALKALMAZASMEGSZUNESE IS NULL or m.C_ALKALMAZASMEGSZUNESE >=getdate()))
							AND fb.C_BEJELENTKEZESINEV NOT IN ('fenntarto', 'KRETA_TECHNICAL_FORI')
                    )       NemLepettBeHonapAlkalmazott,

                (SELECT COUNT(1)
	                FROM T_FELHASZNALO_OSSZES adat
	                INNER JOIN T_ALKALMAZOTT_OSSZES a ON a.ID = adat.ID AND a.C_ALTANEVID = adat.C_TANEVID
                    LEFT JOIN T_FELHASZNALOBELEPES_OSSZES fb ON fb.C_FELHASZNALOID = adat.ID AND fb.TOROLT = 'F'
                    INNER JOIN T_MUNKAUGYIADATOK_OSSZES m ON m.C_ALKALMAZOTTID = adat.ID AND m.C_TANEVID = adat.C_TANEVID AND m.TOROLT = 'F'
	                WHERE (adat.TOROLT = 'F' AND adat.C_TANEVID = @pTanevId AND (fb.C_UTOLSOBELEPES <= DATEADD(DAY,-14,GETDATE()) OR fb.C_UTOLSOBELEPES IS NULL) AND m.C_TARTOSHELYETTESITES = 'F' AND (m.C_ALKALMAZASMEGSZUNESE IS NULL or m.C_ALKALMAZASMEGSZUNESE >=getdate()))
							AND fb.C_BEJELENTKEZESINEV NOT IN ('fenntarto', 'KRETA_TECHNICAL_FORI')
                    )       NemLepettBeKetHetAlkalmazott

                FROM T_TANEV_OSSZES tanev
                INNER JOIN T_INTEZMENY_OSSZES  intezmeny ON intezmeny.ID = tanev.C_INTEZMENYID AND intezmeny.TOROLT = 'F'
                INNER JOIN T_INTEZMENYADATOK_OSSZES  intezmenyAdatok ON intezmenyAdatok.C_INTEZMENYID = tanev.C_INTEZMENYID AND intezmenyAdatok.C_TANEVID = tanev.ID AND intezmenyAdatok.TOROLT = 'F'
                WHERE tanev.TOROLT = 'F' AND tanev.ID = @pTanevId

END
GO

