 /*!40101 SET NAMES cp1250 */;
DELIMITER $$

DROP PROCEDURE IF EXISTS `fr_pl_rocz_kart_ob`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `fr_pl_rocz_kart_ob`(
	IN _MODE VARCHAR(38), 
	IN _GUIDPrac VARCHAR(38),
	IN _WHERE_SQL TEXT, 
	IN _Param_A VARCHAR(50),
	IN _Param_B VARCHAR(50))
SPROC: BEGIN 

	DECLARE _symbol VARCHAR(100);
/*
  CALL fr_pl_rocz_kart_ob('GetMainData', 'Prac61', '', '2010', '');
  
  Wydruk "karta ewidencji czasu pracy"
	1) GetMainData
	2) GetMainTable
	3) GetFrekwTable
	4) GetAbsTable
	
	W przypadku gdy jest to standardowy wydruk "pojedynczy" (RepNo in [117, 118, 119]), 
	to w _GUIDPrac jest przekazywany GUID konkretnego pracownika;
	w przypadku wydruku "grupowego" (RepNo in [128, 129]), GUIDy rekordów frekwencji są w tabeli t_frekw,
	i na tej podstawie należy utworzyć tabelę t_prac z GUIDami pracowników
	  
*/
	/*
	DROP TABLE IF EXISTS t_prac;
	IF _GUIDPrac != '' THEN
		CREATE TEMPORARY TABLE t_prac AS
		SELECT _GUIDPrac AS GUID; 
	ELSE
		CREATE TEMPORARY TABLE t_prac AS
		SELECT DISTINCT PracGUID AS GUID
		FROM frekw f WHERE f.GUID IN (SELECT t.GUID FROM t_frekw t);
	END IF;*/

	SET _symbol = '';
	
	IF (_MODE IN ('GetMainData','GetMainDataGrp')) THEN
	
		DROP TABLE IF EXISTS t_prac;
		IF _GUIDPrac != '' THEN
			CREATE TEMPORARY TABLE t_prac AS
			SELECT _GUIDPrac AS GUID; 
		ELSE
			CREATE TEMPORARY TABLE t_prac AS
			SELECT DISTINCT PracGUID AS GUID
			FROM frekw f WHERE f.GUID IN (SELECT t.GUID FROM t_frekw t);
		END IF;
		
		DROP TABLE IF EXISTS t_prac_kopia;
		CREATE TEMPORARY TABLE t_prac_kopia AS
		SELECT * FROM t_prac; 					
						
		SELECT 
			'fr_pl_rocz_kart_ob' AS fr_pl_rocz_kart_ob,
			_Param_A AS rok,
			u.DodGodzUrlop, P.GUID,
			o.Nazwisko, o.Imiona,
			-- ROUND((SELECT MAX(F.DostepnyUrlWypoczGodz / 60) FROM frekw F WHERE F.deleted=0 AND F.rok=_Param_A AND F.pracGUID=_GUIDPrac GROUP BY F.pracGUID),0) AS DostepnyUrlWypoczGodz,
			-- ROUND((SELECT MAX(F.DostepnyUrlWypoczDni) FROM frekw F WHERE F.deleted=0 AND F.rok=_Param_A AND F.pracGUID=_GUIDPrac GROUP BY F.pracGUID),0) AS DostepnyUrlWypoczDni,
			
			(u.WymiarUrlopuGodz + u.IleDniZaleglegoUrlopuGodz - u.UrlopZaliczkowyGodz)/60 AS DostepnyUrlWypoczGodz,
			u.WymiarUrlopuDni + u.IleDniZaleglegoUrlopuDni - u.UrlopZaliczkowyDni AS DostepnyUrlWypoczDni,
			
			u.IleDniZaleglegoUrlopu AS IleDniZaleglegoUrlopu,
			u.WymiarUrlopuGodz/60  AS WymiarUrlopuGodz,
			u.WymiarUrlopuDni  AS WymiarUrlopuDni,
			u.WymiarUrlopu  AS WymiarUrlopu,

			ROUND((SELECT MIN(F.PozostUrlopWypoczGodz /60) FROM frekw F WHERE F.deleted=0 AND F.rok=_Param_A AND F.pracGUID=o.GUID),0)  AS PozostUrlopWypoczGodz,
			ROUND((SELECT MIN(F.PozostUrlopWypoczDni) FROM frekw F WHERE F.deleted=0 AND F.rok=_Param_A AND F.pracGUID=o.GUID),0)  AS PozostUrlopWypoczDni,
			(SELECT SUM(F.PracowalDni) FROM frekw F WHERE F.deleted=0 AND F.rok=_Param_A AND F.pracGUID=o.GUID)  AS PracowalDni,
			IFNULL((SELECT SUM(F.UrlopWypoczEkwiwGodz) FROM frekw F WHERE F.deleted=0 AND F.rok=_Param_A AND F.pracGUID=o.GUID),0) as UrlopWypoczEkwiwGodz, --  /60
			IFNULL((SELECT SUM(F.UrlopWypoczEkwiw) FROM frekw F WHERE F.deleted=0 AND F.rok=_Param_A AND F.pracGUID=o.GUID),0) as UrlopWypoczEkwiw,

			(SELECT GROUP_CONCAT(distinct CONCAT(symbol,' - ',definicja) ORDER BY symbol SEPARATOR ' ') FROM absencje_dict) as absencje
		FROM 
			osoby o
		JOIN Pracownicy P ON  P.GUID = o.GUID
		INNER JOIN urlopy u ON o.GUID = u.GUIDPr AND u.rok = _Param_A
		WHERE 
			-- o.GUID = _GUIDPrac
			IF(_GUIDPrac = '', o.GUID IN (SELECT GUID FROM t_prac), o.GUID = _GUIDPrac)
		;

	END IF;
	
/*
  CALL fr_pl_rocz_kart_ob('GetMainTable', 'Prac61', '', '2010', '');
*/

	IF (_MODE = 'GetMainTable') THEN
		
		DROP TABLE IF EXISTS t_pl_symb;
		CREATE TEMPORARY TABLE t_pl_symb
		SELECT GUIDPr AS GUID, symbolkalendarza 
		FROM historia_pracy 
		WHERE IF(_GUIDPrac = '', GUIDPr IN (SELECT GUID FROM t_prac), GUIDPr = _GUIDPrac) 
			AND YEAR(OdDnia) <= _Param_A
		ORDER BY OdDnia DESC 
		LIMIT 1;
	
		SELECT DISTINCT spie.displayValue AS MiesiacStr,
			-- f.GUID, f.rok, f.miesiac,
			kpo.data, P.GUID AS pracGUID, 
			YEAR(kpo.data) AS rok,  MONTH(kpo.data) AS miesiac,  DAY(kpo.data) AS dzien,
			fp.godziny/60 AS godziny, fp.kp_info, fp.kp_godziny/60 AS kp_godziny, fp.absencja
		FROM 
			t_pl_symb P
		LEFT JOIN kalendpracy kp ON P.SymbolKalendarza = kp.symbol
		LEFT JOIN kalendpracy_po kpo ON kpo.GUIDNg = kp.GUID
		LEFT JOIN frekw f ON f.PracGUID = P.GUID AND f.rok=YEAR(kpo.data) AND f.miesiac = MONTH(kpo.data) AND f.deleted=0 
		LEFT JOIN frekw_po fp ON  fp.rok_msc_dzien = kpo.data AND fp.FrekwGUID = f.GUID 
		LEFT JOIN slowniki_pierwotne spie ON spie.Slownik_GUID='pl_miesiace' AND spie.`value` = MONTH(kpo.data)
		WHERE YEAR(kpo.data)=_Param_A 
			AND IF(_GUIDPrac = '', P.GUID IN (SELECT GUID FROM t_prac), P.GUID = _GUIDPrac)  -- and P.GUID = _GUIDPrac -- '0081'
		ORDER BY P.GUID, kpo.data
		;
	END IF;	--	IF _MODE = 'GetZaswiadczenie'


	IF (_MODE = 'GetMainTableGrp') THEN
		
		DROP TABLE IF EXISTS t_pl_symb;
		CREATE TEMPORARY TABLE t_pl_symb
		SELECT GUIDPr AS GUID, symbolkalendarza AS symbol 
		FROM historia_pracy 
		WHERE IF(_GUIDPrac = '', GUIDPr IN (SELECT GUID FROM t_prac), GUIDPr = _GUIDPrac) 
			AND YEAR(OdDnia) <= _Param_A
		ORDER BY OdDnia DESC 
		LIMIT 1;
				
		DROP TABLE IF EXISTS t_pl_mt1;
		DROP TABLE IF EXISTS t_pl_mt2;

		CREATE TEMPORARY TABLE t_pl_mt1
		SELECT DISTINCT spie.displayValue AS MiesiacStr,
			-- f.GUID, f.rok, f.miesiac,
			kpo.data, P.GUID AS pracGUID, 
			YEAR(kpo.data) AS rok,  MONTH(kpo.data) AS miesiac,  DAY(kpo.data) AS dzien,
			fp.godziny/60 AS godziny, fp.kp_info, fp.kp_godziny/60 AS kp_godziny, fp.absencja
		FROM 
			t_pl_symb P
		LEFT JOIN kalendpracy kp ON P.SymbolKalendarza = kp.symbol
		LEFT JOIN kalendpracy_po kpo ON kpo.GUIDNg = kp.GUID
		LEFT JOIN frekw f ON f.PracGUID = P.GUID AND f.rok=YEAR(kpo.data) AND f.miesiac = MONTH(kpo.data) AND f.deleted=0 
		LEFT JOIN frekw_po fp ON  fp.rok_msc_dzien = kpo.data AND fp.FrekwGUID = f.GUID 
		LEFT JOIN slowniki_pierwotne spie ON spie.Slownik_GUID='pl_miesiace' AND spie.`value` = MONTH(kpo.data)
		WHERE YEAR(kpo.data)=_Param_A 
			AND IF(_GUIDPrac = '', P.GUID IN (SELECT GUID FROM t_prac), P.GUID = _GUIDPrac) -- P.GUID = _GUIDPrac -- '0081'
		ORDER BY P.GUID, kpo.data
		;



		CREATE TEMPORARY TABLE t_pl_mt2
		SELECT
		pracguid, rok, miesiac, miesiacstr,
		max(IF(dzien=1,absencja,'')) AS a01,
		max(IF(dzien=1,godziny,'')) AS g01,
		max(IF(dzien=2,absencja,'')) AS a02,
		max(IF(dzien=2,godziny,'')) AS g02,
		max(IF(dzien=3,absencja,'')) AS a03,
		max(IF(dzien=3,godziny,'')) AS g03,
		max(IF(dzien=4,absencja,'')) AS a04,
		max(IF(dzien=4,godziny,'')) AS g04,
		max(IF(dzien=5,absencja,'')) AS a05,
		max(IF(dzien=5,godziny,'')) AS g05,
		max(IF(dzien=6,absencja,'')) AS a06,
		max(IF(dzien=6,godziny,'')) AS g06,
		max(IF(dzien=7,absencja,'')) AS a07,
		max(IF(dzien=7,godziny,'')) AS g07,
		max(IF(dzien=8,absencja,'')) AS a08,
		max(IF(dzien=8,godziny,'')) AS g08,
		max(IF(dzien=9,absencja,'')) AS a09,
		max(IF(dzien=9,godziny,'')) AS g09,
		max(IF(dzien=10,absencja,'')) AS a10,
		max(IF(dzien=10,godziny,'')) AS g10,
		max(IF(dzien=11,absencja,'')) AS a11,
		max(IF(dzien=11,godziny,'')) AS g11,
		max(IF(dzien=12,absencja,'')) AS a12,
		max(IF(dzien=12,godziny,'')) AS g12,
		max(IF(dzien=13,absencja,'')) AS a13,
		max(IF(dzien=13,godziny,'')) AS g13,
		max(IF(dzien=14,absencja,'')) AS a14,
		max(IF(dzien=14,godziny,'')) AS g14,
		max(IF(dzien=15,absencja,'')) AS a15,
		max(IF(dzien=15,godziny,'')) AS g15,
		max(IF(dzien=16,absencja,'')) AS a16,
		max(IF(dzien=16,godziny,'')) AS g16,
		max(IF(dzien=17,absencja,'')) AS a17,
		max(IF(dzien=17,godziny,'')) AS g17,
		max(IF(dzien=18,absencja,'')) AS a18,
		max(IF(dzien=18,godziny,'')) AS g18,
		max(IF(dzien=19,absencja,'')) AS a19,
		max(IF(dzien=19,godziny,'')) AS g19,
		max(IF(dzien=20,absencja,'')) AS a20,
		max(IF(dzien=20,godziny,'')) AS g20,
		max(IF(dzien=21,absencja,'')) AS a21,
		max(IF(dzien=21,godziny,'')) AS g21,
		max(IF(dzien=22,absencja,'')) AS a22,
		max(IF(dzien=22,godziny,'')) AS g22,
		max(IF(dzien=23,absencja,'')) AS a23,
		max(IF(dzien=23,godziny,'')) AS g23,
		max(IF(dzien=24,absencja,'')) AS a24,
		max(IF(dzien=24,godziny,'')) AS g24,
		max(IF(dzien=25,absencja,'')) AS a25,
		max(IF(dzien=25,godziny,'')) AS g25,
		max(IF(dzien=26,absencja,'')) AS a26,
		max(IF(dzien=26,godziny,'')) AS g26,
		max(IF(dzien=27,absencja,'')) AS a27,
		max(IF(dzien=27,godziny,'')) AS g27,
		max(IF(dzien=28,absencja,'')) AS a28,
		max(IF(dzien=28,godziny,'')) AS g28,
		max(IF(dzien=29,absencja,'')) AS a29,
		max(IF(dzien=29,godziny,'')) AS g29,
		max(IF(dzien=30,absencja,'')) AS a30,
		max(IF(dzien=30,godziny,'')) AS g30,
		max(IF(dzien=31,absencja,'')) AS a31,
		max(IF(dzien=31,godziny,'')) AS g31

		FROM t_pl_mt1 m1
		GROUP BY pracguid, rok, miesiac;

		select * from t_pl_mt2 order by pracguid, miesiac;

	END IF;	--	IF _MODE = 'GetMainTableGrp'

/*
  CALL fr_pl_rocz_kart_ob('GetFrekwTable', 'Prac61', '', '2011', '');
*/
	IF (_MODE IN ('GetFrekwTable','GetFrekwTableGrp')) THEN
	
	(
		SELECT 
			(spie.value+1-1) AS miesiac,
			spie.displayValue AS MiesiacStr,
			0 AS NomCzasPracy,  
			0 AS RzeczCzasPrac, 
			0 AS CzasPracNoc,
			0 AS CzasPracNiedzSwiet,
			0 AS CzasNadliczb,
			0 AS CzasPracDniWolne,
			0 AS WCzasieUrlopow,
			0 AS NieobUspr,
			0 AS NieobNieUspr	
		FROM 
			slowniki_pierwotne spie
		WHERE spie.Slownik_GUID='pl_miesiace'
		AND spie.value NOT IN (SELECT miesiac FROM frekw ng WHERE ng.deleted=0 AND ng.rok = _Param_A 
			AND IF(_GUIDPrac = '', ng.PracGUID IN (SELECT GUID FROM t_prac), ng.PracGUID = _GUIDPrac) -- AND ng.PracGUID = _GUIDPrac
		)	
	)
	UNION
	(
	SELECT 
			ng.miesiac, spie.displayValue AS MiesiacStr, 
			SUM(po.kp_godziny/60) AS NomCzasPracy,  
			-- SUM(IF(po.absencja IN ("","E"),po.kp_godziny,0)/60) AS RzeczCzasPrac,
			SUM(po.godziny/60) AS RzeczCzasPrac,
			SUM(IF(po.absencja IN ("","E"),po.noc,0)/60) AS CzasPracNoc,
			SUM(IF(po.absencja IN ("X"),po.kp_godziny,0)/60) AS CzasPracNiedzSwiet,
			
			IF( _Param_B = 'ROWNOWAZNY_CP',
				IF(	(SELECT valuei FROM IReg WHERE idkey='nadgodziny_jako_100' limit 1)=0,
					ng.godz_plus100wolne,
					ng.godz_plus50prac
					)
			,
			SUM(IF(po.godziny>po.kp_godziny, po.godziny-po.kp_godziny, 0)/60 )
			)	AS CzasNadliczb,
			SUM(IF(po.absencja IN ("D"),po.kp_godziny,0)/60) AS CzasPracDniWolne,
			SUM(IF(po.absencja IN ("W","G"), po.kp_godziny, 0 )/60) AS WCzasieUrlopow,
			SUM(IF(po.absencja IN ("T", "A", "B"), po.kp_godziny, 0 )/60) AS NieobUspr,
			SUM(IF(po.absencja IN ("N"), po.kp_godziny, 0 )/60) AS NieobNieUspr
		FROM 
			frekw_po po 
		LEFT JOIN frekw ng ON ng.GUID = po.FrekwGUID AND ng.deleted=0 
		LEFT JOIN slowniki_pierwotne spie ON spie.Slownik_GUID='pl_miesiace' AND ng.miesiac=spie.`value`
		WHERE 
			ng.rok = _Param_A 
			AND IF(_GUIDPrac = '', ng.PracGUID IN (SELECT GUID FROM t_prac_kopia), ng.PracGUID = _GUIDPrac) -- AND ng.PracGUID = _GUIDPrac -- 'Prac61' 
			AND ng.miesiac>0 AND ng.deleted=0 
		GROUP BY ng.PracGUID, ng.miesiac 
	)		
		ORDER BY miesiac 
		;
	END IF; --	IF _MODE = 'GetFrekwTable'
	
/*
  CALL fr_pl_rocz_kart_ob('GetAbsTable', 'Prac61', '', '2011', '');
*/
	IF (_MODE IN ('GetAbsTable','GetAbsTableGrp')) THEN
		DROP TEMPORARY TABLE IF EXISTS t_pl_ddp1;
		DROP TEMPORARY TABLE IF EXISTS t_pl_ddp2;
		
		CREATE TEMPORARY TABLE t_pl_ddp1
			SELECT 
				COUNT(*) AS DDP,  kp.miesiac AS miesiac, f.pracGUID 
			FROM 
				kalendpracy_po kp 
			INNER JOIN kalendpracy k ON kp.GUIDNg = k.GUID 	AND k.rok = _Param_A 
			INNER JOIN Pracownicy p ON k.symbol = p.symbolkalendarza 
			INNER JOIN frekw f ON f.PracGUID = p.GUID AND f.rok = _Param_A AND f.miesiac = kp.miesiac  AND f.deleted=0 
			WHERE 
				f.deleted=0 
				AND IF(_GUIDPrac = '', f.PracGUID IN (SELECT GUID FROM t_prac), f.PracGUID = _GUIDPrac) -- AND f.PracGUID = _GUIDPrac 
				AND NOT (kp.info IN ('N','D','X') ) 
			GROUP BY f.PracGUID, kp.miesiac
			ORDER BY miesiac ;
     
		CREATE TEMPORARY TABLE t_pl_ddp2     
			SELECT 
				f.PracGUID,
				COUNT(fp.godziny) AS DP, 
				MAX(f.miesiac) AS miesiac 
			FROM 
				frekw_po fp 
			INNER JOIN frekw f ON f.GUID = fp.FrekwGUID  AND f.deleted=0 
			WHERE fp.godziny>0 AND f.rok=_Param_A 
				AND IF(_GUIDPrac = '', f.PracGUID IN (SELECT GUID FROM t_prac), f.PracGUID = _GUIDPrac) -- f.PracGUID = _GUIDPrac 
			GROUP BY f.PracGUID, f.miesiac 
			ORDER BY miesiac;     
		
	(
		SELECT 
		    '' AS pracGUID,
			(spie.value+1-1) AS miesiac,
			spie.displayValue AS MiesiacStr,
			0 AS DDP, 
			0 AS DP,
			0 AS DniLeczSzpit, 
			0 AS DniLeczDom, 
			0 AS DniOpieka, 
			0 AS DniUrlopMac, 
			0 AS DniUrlopOkol, 
			0 AS DniUrlopRehab, 
			0 AS DniUrlopBezpl, 
			0 AS DniNN, 
			0 AS DniUrlopWyp, 
			0 AS DniOpiekaZdroweD, 
			0 AS DniWypadCh, 
			0 AS DniUsprPlat, 
			0 AS DniUsprNPlat, 
			0 AS DniUrlopWych, 
			0 AS DniWojsko, 
			0 AS DniUrlopNaZadanie, 
			0 AS DniPostojNieplatny, 
			0 AS DniNaprWad,
			0 AS mo,
			0 AS mr,
			0 AS mt,
			0 AS eo,
			0 AS ez,
			0 AS kb,
			0 AS wd,
			0 AS x
		FROM 
			slowniki_pierwotne spie
		WHERE spie.Slownik_GUID = 'pl_miesiace'
		AND spie.value NOT IN (SELECT DISTINCT miesiac FROM frekw ng WHERE ng.deleted=0 AND ng.rok = _Param_A 
		    AND IF(_GUIDPrac = '', ng.PracGUID IN (SELECT GUID FROM t_prac), ng.PracGUID = _GUIDPrac) -- AND ng.PracGUID = _GUIDPrac
			)	

	)
	UNION
	(
		SELECT ng.PracGUID,
			MAX(ng.miesiac) AS miesiac,
			spie.displayValue AS MiesiacStr,
			IFNULL((SELECT d.DDP FROM t_pl_ddp1 d WHERE d.miesiac=MAX(ng.miesiac) and d.pracGUID = ng.PracGUID),0) AS DDP,
			IFNULL((SELECT d.DP FROM t_pl_ddp2 d WHERE d.miesiac=MAX(ng.miesiac) and d.pracGUID = ng.PracGUID),0) AS DP,
			SUM(IF(po.absencja IN ('L'), 1, 0 )) AS DniLeczSzpit, 
			SUM(IF(po.absencja IN ('C'), 1, 0 )) AS DniLeczDom, 
			SUM(IF(po.absencja IN ('K'), 1, 0 )) AS DniOpieka, 
			SUM(IF(po.absencja IN ('M'), 1, 0 )) AS DniUrlopMac, 
			SUM(IF(po.absencja IN ('O'), 1, 0 )) AS DniUrlopOkol, 
			SUM(IF(po.absencja IN ('R'), 1, 0 )) AS DniUrlopRehab, 
			SUM(IF(po.absencja IN ('B'), 1, 0 )) AS DniUrlopBezpl, 
			SUM(IF(po.absencja IN ('N'), 1, 0 )) AS DniNN, 
			SUM(IF(po.absencja IN ('W'), 1, 0 )) AS DniUrlopWyp, 
			SUM(IF(po.absencja IN ('D'), 1, 0 )) AS DniOpiekaZdroweD, 
			SUM(IF(po.absencja IN ('Z'), 1, 0 )) AS DniWypadCh, 
			SUM(IF(po.absencja IN ('T'), 1, 0 )) AS DniUsprPlat, 
			SUM(IF(po.absencja IN ('A'), 1, 0 )) AS DniUsprNPlat, 
			SUM(IF(po.absencja IN ('Y'), 1, 0 )) AS DniUrlopWych, 
			SUM(IF(po.absencja IN ('J'), 1, 0 )) AS DniWojsko, 
			SUM(IF(po.absencja IN ('G'), 1, 0 )) AS DniUrlopNaZadanie, 
			SUM(IF(po.absencja IN ('Q'), 1, 0 )) AS DniPostojNieplatny, 
			SUM(IF(po.absencja IN ('V'), 1, 0 )) AS DniNaprWad,
			SUM(IF(po.absencja IN ('MO'), 1, 0 )) AS mo,
			SUM(IF(po.absencja IN ('MR'), 1, 0 )) AS mr,
			SUM(IF(po.absencja IN ('MT'), 1, 0 )) AS mt,
			SUM(IF(po.absencja IN ('EO'), 1, 0 )) AS eo,
			SUM(IF(po.absencja IN ('EZ'), 1, 0 )) AS ez,
			SUM(IF(po.absencja IN ('KB'), 1, 0 )) AS kb,
			SUM(IF(po.absencja IN ('WD'), 1, 0 )) AS wd,
			SUM(IF(po.absencja IN ('X'), 1, 0 )) AS x
		FROM 
			frekw_po po 
		LEFT JOIN frekw ng ON ng.GUID = po.FrekwGUID  AND ng.deleted=0 
		LEFT JOIN slowniki_pierwotne spie ON spie.Slownik_GUID='pl_miesiace' AND ng.miesiac=spie.value
		WHERE 
			ng.deleted=0 AND ng.rok = _Param_A  AND ng.miesiac>0 
			AND IF(_GUIDPrac = '', ng.PracGUID IN (SELECT GUID FROM t_prac_kopia), ng.PracGUID = _GUIDPrac) -- ng.PracGUID = _GUIDPrac
		GROUP BY ng.PracGUID, ng.miesiac
	)
		ORDER BY miesiac 
		;
	END IF; --	IF _MODE = 'GetAbsTable'
	

/*
USE sec;

CREATE TEMPORARY TABLE `t_frekw` (lp INT(11) NOT NULL AUTO_INCREMENT,GUID VARCHAR(38) ,  PRIMARY KEY (`lp`),  UNIQUE GUID (GUID)  ) ;

INSERT IGNORE INTO `t_frekw` (GUID)  VALUES ('{0559719B-E1F4-4361-9C3A-D794A42D8E38}');
INSERT IGNORE INTO `t_frekw` (GUID)  VALUES ('{5048D981-A3E5-47C2-AAA3-8145789BFAFC}') ;

CALL fr_pl_rocz_kart_ob('GetMainDataGrp', '', '', '2017', '') ;
CALL fr_pl_rocz_kart_ob('GetMainTableGrp', '', '', '2017', '');
CALL fr_pl_rocz_kart_ob('GetFrekwTableGrp', '', '', '2017', 'ROWNOWAZNY_CP');
CALL fr_pl_rocz_kart_ob('GetAbsTableGrp', '', '', '2017', ''); 

*/

	
END$$

DELIMITER ;
