﻿SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF OBJECT_ID('[dbo].[sp_GetAdminDashboardAdminisztracio]') IS NOT NULL 
BEGIN
  DROP PROCEDURE [dbo].[sp_GetAdminDashboardAdminisztracio]
END  
GO

CREATE PROCEDURE [dbo].[sp_GetAdminDashboardAdminisztracio]

	 @pNaFeladatellatasihelyTipus INT
    ,@pNaEvfolyamTipusa INT
    ,@pNaCsoportTipusa INT
    ,@pNaNemTipusa INT
    ,@pNaAlkalmazottMunkakor INT
    ,@pNaAlkalmazottMunkaviszony INT
    ,@pNaBesorolasiFokozat INT
    ,@pNaTargyKategoria INT
    ,@pNincsAnyanyelv INT
    ,@pNincsAllampolgarsag INT
    ,@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 TOP 1 ISNULL(C_DATE, '20201231') FROM T_LICENCE_OSSZES) 										    LicenceDatum,
                (SELECT COUNT(1) FROM T_FELADATELLATASIHELY_OSSZES adat
	                WHERE adat.C_TANEVID = @pTanevId AND adat.TOROLT = 'F'
	                AND adat.C_OKTATASINEVELESIFELADATTIPUS =  @pNaFeladatellatasihelyTipus)								NincsFeladatellatasiHelyTipus,
                (SELECT COUNT(1) FROM T_MUKODESIHELY_OSSZES adat
	                WHERE adat.C_TANEVID = @pTanevId AND adat.TOROLT = 'F'
	                AND 0 = (SELECT COUNT(1) FROM T_FELADATELLATASIHELY_OSSZES fh WHERE fh.C_MUKODESIHELYID = adat.ID))	    NincsFeladatellatasiHelyMukodesiHely,
			
				(select count(1)  from (SELECT 
					T_OSZTALY_OSSZES.ID
				FROM T_OSZTALY_OSSZES
					LEFT JOIN T_TANEV_OSSZES on T_TANEV_OSSZES.ID = T_OSZTALY_OSSZES.C_ALTANEVID
					LEFT JOIN T_TANULOCSOPORT_OSSZES on T_TANULOCSOPORT_OSSZES.C_OSZTALYCSOPORTID = T_OSZTALY_OSSZES.ID  AND T_TANULOCSOPORT_OSSZES.TOROLT='F' 
					AND ((T_TANULOCSOPORT_OSSZES.C_BELEPESDATUM <=  GETDATE() AND (T_TANULOCSOPORT_OSSZES.C_KILEPESDATUM IS NULL or T_TANULOCSOPORT_OSSZES.C_KILEPESDATUM >= GETDATE())))
				WHERE C_ALTANEVID = @pTanevId AND T_OSZTALY_OSSZES.TOROLT = 'F'
				GROUP BY T_OSZTALY_OSSZES.ID
				HAVING COUNT(T_TANULOCSOPORT_OSSZES.id) = 0)  adat)
                    UresOsztaly,

				(select count(1)  from (SELECT 
					T_CSOPORT_OSSZES.ID
				FROM T_CSOPORT_OSSZES
					LEFT JOIN T_TANEV_OSSZES on T_TANEV_OSSZES.ID = T_CSOPORT_OSSZES.C_ALTANEVID
					LEFT JOIN T_TANULOCSOPORT_OSSZES on T_TANULOCSOPORT_OSSZES.C_OSZTALYCSOPORTID = T_CSOPORT_OSSZES.ID  AND T_TANULOCSOPORT_OSSZES.TOROLT='F' 
					AND ((T_TANULOCSOPORT_OSSZES.C_BELEPESDATUM <=  GETDATE() AND (T_TANULOCSOPORT_OSSZES.C_KILEPESDATUM IS NULL or T_TANULOCSOPORT_OSSZES.C_KILEPESDATUM >= GETDATE())))
				WHERE C_ALTANEVID = @pTanevId AND T_CSOPORT_OSSZES.TOROLT = 'F'
				GROUP BY T_CSOPORT_OSSZES.ID
				HAVING COUNT(T_TANULOCSOPORT_OSSZES.id) = 0)  adat) 
                    UresCsoport,

                (SELECT COUNT(1) FROM T_OSZTALYCSOPORT_OSSZES adat
	                INNER JOIN T_OSZTALY_OSSZES o ON o.ID = adat.ID AND o.C_ALTANEVID = adat.C_TANEVID
	                WHERE adat.C_TANEVID = @pTanevId AND adat.TOROLT = 'F' AND adat.C_EVFOLYAMTIPUSA = @pNaEvfolyamTipusa or adat.C_EVFOLYAMTIPUSA IS NULL )	
                        NincsEvfolyamOsztaly,

				(SELECT COUNT(1) FROM T_OSZTALYCSOPORT_OSSZES adat
	                INNER JOIN T_CSOPORT_OSSZES csoport ON csoport.ID = adat.ID AND csoport.C_ALTANEVID = adat.C_TANEVID
	                WHERE adat.C_TANEVID = @pTanevId AND adat.TOROLT = 'F' AND adat.C_EVFOLYAMTIPUSA = @pNaEvfolyamTipusa or adat.C_EVFOLYAMTIPUSA IS NULL )	
                        NincsEvfolyamCsoport,

                (SELECT COUNT(1) FROM T_OSZTALYCSOPORT_OSSZES adat
	                INNER JOIN T_CSOPORT_OSSZES csoport ON csoport.ID = adat.ID AND csoport.C_ALTANEVID = adat.C_TANEVID
	                WHERE adat.C_TANEVID = @pTanevId AND adat.TOROLT = 'F' AND csoport.C_TIPUSA = @pNaCsoportTipusa)				
                        NincsTipusCsoport,

                (SELECT COUNT(1) FROM T_OSZTALYCSOPORT_OSSZES adat
	                INNER JOIN T_OSZTALY_OSSZES o ON o.ID = adat.ID AND o.C_ALTANEVID = adat.C_TANEVID
	                WHERE adat.C_TANEVID = @pTanevId AND adat.TOROLT = 'F' AND o.C_OSZTALYFONOKID IS NULL)					
                        NincsOsztalyOsztalyfonok,

                (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
					INNER JOIN T_MUNKAUGYIADATOK_OSSZES ON T_MUNKAUGYIADATOK_OSSZES.C_ALKALMAZOTTID = a.ID
	                WHERE adat.C_TANEVID = @pTanevId AND adat.TOROLT = 'F'
	                AND (adat.C_NEME = @pNaNemTipusa OR adat.C_NEME IS NULL)
					AND adat.C_NYOMTATASINEV NOT LIKE '[[]HO%'			
					AND adat.C_NYOMTATASINEV NOT LIKE '[[]BTA%'	
					AND T_MUNKAUGYIADATOK_OSSZES.C_MUNKAKORTIPUSA NOT IN 
                        (548,552,553,555,597,598,599,600,594,595,557,558,559,560,561,562,563,564,565
						,566,567,568,569,570,571,572,573,574,576,577,578,579,580,581,582,583,584,585
						,586,587,588,589,590,591,592,633,635,636,637,638,639,640,641,642,1762,1763
						,3026,3027,3028,3029,3030,6329,6330,6331,6332,6333,6334,6335,6336,6337,6338
						,6339,6340,6341,6342,6343,6344,6345,6346,6347,6348,6349,6350,6351,6352,6353
						,6354,6355,6356,6357,6358,6359,6360,6361,6362,6363,6364,6365,6366,6367,6368
						,6369,6370,6371,6372,6373,6374,6375,6376,6377,6378,6379,6380,6381,6382,6383
						,6384,6385,6386,6387,6388,6389,6390,6392,6393,6394,6395,6396,6397,6398,6399
						,6400,6401,6402,6403,6404,6405,6406,6407,6408,6409,6410,6411,6412,6413,6414
						,6415,6416,6417,6418,6419,6420,6421,6422,6423,6424,6425,6426,6427,6428,6429
						,6430,6431,6432,6433,6434,6435,6436,6437,6438,6439,6440,6441,6442,6443,6445
						,6446,6447,6448,6449,6450,6451,6452,6453,6454,6455,6456,6457,6458,6459,6460
						,6461,6565,6709,6710,6725,6812,6813,6914,6915,6968,6969,7356,556,622,621,634
						,554,620,619,626,625,628,629,627,615,616,617,551,606,608,607,631,632,593,601
						,603,602,596,624,623,630,618,550,549,604,605,612,613,611,609,610,614,575))		
                            NincsNemAlkalmazott,

                (SELECT COUNT(1) FROM T_FELHASZNALO_OSSZES adat
	                INNER JOIN T_TANULO_OSSZES a ON a.ID = adat.ID AND a.C_ALTANEVID = adat.C_TANEVID
	                WHERE adat.C_TANEVID = @pTanevId AND adat.TOROLT = 'F'
	                AND (adat.C_NEME = @pNaNemTipusa  OR adat.C_NEME IS NULL))							
                            NincsNemTanulo,

                (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
	                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'
	                AND m.C_MUNKAKORTIPUSA = @pNaAlkalmazottMunkakor 
                    AND m.C_MUNKAKORTIPUSA NOT IN 
                         (548,552,553,555,597,598,599,600,594,595,557,558,559,560,561,562,563,564,565
						,566,567,568,569,570,571,572,573,574,576,577,578,579,580,581,582,583,584,585
						,586,587,588,589,590,591,592,633,635,636,637,638,639,640,641,642,1762,1763
						,3026,3027,3028,3029,3030,6329,6330,6331,6332,6333,6334,6335,6336,6337,6338
						,6339,6340,6341,6342,6343,6344,6345,6346,6347,6348,6349,6350,6351,6352,6353
						,6354,6355,6356,6357,6358,6359,6360,6361,6362,6363,6364,6365,6366,6367,6368
						,6369,6370,6371,6372,6373,6374,6375,6376,6377,6378,6379,6380,6381,6382,6383
						,6384,6385,6386,6387,6388,6389,6390,6392,6393,6394,6395,6396,6397,6398,6399
						,6400,6401,6402,6403,6404,6405,6406,6407,6408,6409,6410,6411,6412,6413,6414
						,6415,6416,6417,6418,6419,6420,6421,6422,6423,6424,6425,6426,6427,6428,6429
						,6430,6431,6432,6433,6434,6435,6436,6437,6438,6439,6440,6441,6442,6443,6445
						,6446,6447,6448,6449,6450,6451,6452,6453,6454,6455,6456,6457,6458,6459,6460
						,6461,6565,6709,6710,6725,6812,6813,6914,6915,6968,6969,7356,556,622,621,634
						,554,620,619,626,625,628,629,627,615,616,617,551,606,608,607,631,632,593,601
						,603,602,596,624,623,630,618,550,549,604,605,612,613,611,609,610,614,575))		
                            NincsMunkakorAlkalmazott,

                (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
	                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'
	                AND m.C_MUNKAVISZONYTIPUSA = @pNaAlkalmazottMunkaviszony
					AND m.C_MUNKAKORTIPUSA NOT IN 
                        (548,552,553,555,597,598,599,600,594,595,557,558,559,560,561,562,563,564,565
						,566,567,568,569,570,571,572,573,574,576,577,578,579,580,581,582,583,584,585
						,586,587,588,589,590,591,592,633,635,636,637,638,639,640,641,642,1762,1763
						,3026,3027,3028,3029,3030,6329,6330,6331,6332,6333,6334,6335,6336,6337,6338
						,6339,6340,6341,6342,6343,6344,6345,6346,6347,6348,6349,6350,6351,6352,6353
						,6354,6355,6356,6357,6358,6359,6360,6361,6362,6363,6364,6365,6366,6367,6368
						,6369,6370,6371,6372,6373,6374,6375,6376,6377,6378,6379,6380,6381,6382,6383
						,6384,6385,6386,6387,6388,6389,6390,6392,6393,6394,6395,6396,6397,6398,6399
						,6400,6401,6402,6403,6404,6405,6406,6407,6408,6409,6410,6411,6412,6413,6414
						,6415,6416,6417,6418,6419,6420,6421,6422,6423,6424,6425,6426,6427,6428,6429
						,6430,6431,6432,6433,6434,6435,6436,6437,6438,6439,6440,6441,6442,6443,6445
						,6446,6447,6448,6449,6450,6451,6452,6453,6454,6455,6456,6457,6458,6459,6460
						,6461,6565,6709,6710,6725,6812,6813,6914,6915,6968,6969,7356,556,622,621,634
						,554,620,619,626,625,628,629,627,615,616,617,551,606,608,607,631,632,593,601
						,603,602,596,624,623,630,618,550,549,604,605,612,613,611,609,610,614,575))			
                            NincsMunkaviszonyTipusAlkalmazott,


                (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
	                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'
					AND m.C_MUNKAKORTIPUSA NOT IN 
                         (548,552,553,555,597,598,599,600,594,595,557,558,559,560,561,562,563,564,565
						,566,567,568,569,570,571,572,573,574,576,577,578,579,580,581,582,583,584,585
						,586,587,588,589,590,591,592,633,635,636,637,638,639,640,641,642,1762,1763
						,3026,3027,3028,3029,3030,6329,6330,6331,6332,6333,6334,6335,6336,6337,6338
						,6339,6340,6341,6342,6343,6344,6345,6346,6347,6348,6349,6350,6351,6352,6353
						,6354,6355,6356,6357,6358,6359,6360,6361,6362,6363,6364,6365,6366,6367,6368
						,6369,6370,6371,6372,6373,6374,6375,6376,6377,6378,6379,6380,6381,6382,6383
						,6384,6385,6386,6387,6388,6389,6390,6392,6393,6394,6395,6396,6397,6398,6399
						,6400,6401,6402,6403,6404,6405,6406,6407,6408,6409,6410,6411,6412,6413,6414
						,6415,6416,6417,6418,6419,6420,6421,6422,6423,6424,6425,6426,6427,6428,6429
						,6430,6431,6432,6433,6434,6435,6436,6437,6438,6439,6440,6441,6442,6443,6445
						,6446,6447,6448,6449,6450,6451,6452,6453,6454,6455,6456,6457,6458,6459,6460
						,6461,6565,6709,6710,6725,6812,6813,6914,6915,6968,6969,7356,556,622,621,634
						,554,620,619,626,625,628,629,627,615,616,617,551,606,608,607,631,632,593,601
						,603,602,596,624,623,630,618,550,549,604,605,612,613,611,609,610,614,575)
	                AND m.C_BESOROLASIFOKOZAT =@pNaBesorolasiFokozat )														
                        NincsBesorolasiFokozatAlkalmazott,

                (SELECT COUNT(1) FROM T_FELHASZNALO_OSSZES adat
	                INNER JOIN T_TANULO_OSSZES a ON a.ID = adat.ID AND a.C_ALTANEVID = adat.C_TANEVID
	                WHERE adat.C_TANEVID = @pTanevId AND adat.TOROLT = 'F' AND (adat.C_ANYANYELVE = @pNincsAnyanyelv OR adat.C_ANYANYELVE IS NULL))					
                        NincsAnyanyelvTanulo,

                (SELECT COUNT(1) FROM T_FELHASZNALO_OSSZES adat
	                INNER JOIN T_TANULO_OSSZES a ON a.ID = adat.ID AND a.C_ALTANEVID = adat.C_TANEVID
	                WHERE adat.C_TANEVID = @pTanevId AND adat.TOROLT = 'F' AND (adat.C_ALLAMPOLGARSAGA = @pNincsAllampolgarsag OR adat.C_ALLAMPOLGARSAGA IS NULL) )
                        NincsAllampolgarsagaTanulo,

                (SELECT COUNT(1) FROM T_CSENGETESIREND_OSSZES adat
	                WHERE adat.C_TANEVID = @pTanevId AND adat.TOROLT = 'F' AND adat.C_AKTIV = 'T')						
                        NincsAktivCsengetesiRend,

                (SELECT COUNT(1) FROM T_TANTARGY_OSSZES adat
	                WHERE adat.C_TANEVID = @pTanevId AND adat.TOROLT = 'F' AND adat.C_TARGYKATEGORIA = @pNaTargyKategoria )	
                        NincsKategoriaTantargy,

                (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
	                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'
	                AND m.C_MUNKAKORTIPUSA NOT IN 
                        (548,552,553,555,597,598,599,600,594,595,557,558,559,560,561,562,563,564,565
						,566,567,568,569,570,571,572,573,574,576,577,578,579,580,581,582,583,584,585
						,586,587,588,589,590,591,592,633,635,636,637,638,639,640,641,642,1762,1763
						,3026,3027,3028,3029,3030,6329,6330,6331,6332,6333,6334,6335,6336,6337,6338
						,6339,6340,6341,6342,6343,6344,6345,6346,6347,6348,6349,6350,6351,6352,6353
						,6354,6355,6356,6357,6358,6359,6360,6361,6362,6363,6364,6365,6366,6367,6368
						,6369,6370,6371,6372,6373,6374,6375,6376,6377,6378,6379,6380,6381,6382,6383
						,6384,6385,6386,6387,6388,6389,6390,6392,6393,6394,6395,6396,6397,6398,6399
						,6400,6401,6402,6403,6404,6405,6406,6407,6408,6409,6410,6411,6412,6413,6414
						,6415,6416,6417,6418,6419,6420,6421,6422,6423,6424,6425,6426,6427,6428,6429
						,6430,6431,6432,6433,6434,6435,6436,6437,6438,6439,6440,6441,6442,6443,6445
						,6446,6447,6448,6449,6450,6451,6452,6453,6454,6455,6456,6457,6458,6459,6460
						,6461,6565,6709,6710,6725,6812,6813,6914,6915,6968,6969,7356,556,622,621,634
						,554,620,619,626,625,628,629,627,615,616,617,551,606,608,607,631,632,593,601
						,603,602,596,624,623,630,618,550,549,604,605,612,613,611,609,610,614,575))		
							 NSzFHRosszMunkakor,

                (SELECT COUNT(1) FROM T_ALKALMAZOTT_OSSZES a
				 LEFT JOIN
                        T_EMAIL_OSSZES ON T_EMAIL_OSSZES.C_FELHASZNALOID = a.ID AND T_EMAIL_OSSZES.TOROLT = 'F' AND T_EMAIL_OSSZES.C_ALAPERTELMEZETT = 'T'
	                WHERE a.C_ALTANEVID = @pTanevId AND a.TOROLT = 'F' and T_EMAIL_OSSZES.ID is null)			NincsEmailAlkalmazott

                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

