 DELIMITER $$

DROP PROCEDURE IF EXISTS `hr_team_www`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `hr_team_www`(
	IN _MODE VARCHAR(150),
	IN _GUID VARCHAR(38),
	IN _YYYY_MM INTEGER,
	IN _OsobaGUID VARCHAR(38),
	IN _Param_A VARCHAR(250),	-- Rok
	IN _Param_B VARCHAR(250),	-- Miesiąc
	IN _Param_C TEXT	
)
/*
CALL hr_team_www('init', '', '', '', '', '', '');
CALL hr_team_www('szablony.init', '', '2012', '5', '', '');
CALL hr_team_www('szablony.select', '', '2012', '5', '', '');
	SELECT * FROM `t_hr_team_www_szablony` ORDER BY sort, Nazwa;
CALL hr_team_www('osoby.gantt.init', '', '2012', '5', '', '');
	CALL hr_team_www('osoby.gantt.update', '', '2012', '5', '', '');

CALL hr_team_www('osoby.gantt.select', '', '', '', '', '');
*/
S_hr_team_www:BEGIN 
	IF _MODE LIKE 'WWW.2.0.%' THEN
		IF _MODE = 'WWW.2.0.init' OR @.WWW.2.0.init IS NULL THEN
			DROP TEMPORARY TABLE IF EXISTS `t_hr_team_www_RANGE`;
			CREATE TEMPORARY TABLE `t_hr_team_www_RANGE` (
			  `i_MODE` VARCHAR(100),
			  `YYYY_MM` INTEGER,
			  `Date_From` DATE,
			  `Date_TO` DATE,
			  `GUIDTemplate` VARCHAR(38),
			  `p_GUID` VARCHAR(38),
			  `n_GUID` VARCHAR(38),
			  `OsobaGUID` VARCHAR(38) NOT NULL DEFAULT '',
			  `log_USER_GUID` VARCHAR(38) NOT NULL DEFAULT '',
			  `Funkcja_Usera` VARCHAR(150),
			  `Funkcja_dla_Grafiku` VARCHAR(150)
			)
			;
			INSERT INTO `t_hr_team_www_RANGE` SET GUIDTemplate = NULL;
			
			DROP TEMPORARY TABLE IF EXISTS `t_hr_team_www_szablony`;
			CREATE TEMPORARY TABLE `t_hr_team_www_szablony` (
			  `GUID` VARCHAR(38) NOT NULL DEFAULT '',
			  `Nazwa` VARCHAR(200) NOT NULL DEFAULT '',
			  `Symbol` VARCHAR(10) NOT NULL DEFAULT '',
			  `checked` INT(11) NOT NULL DEFAULT '0',
			  `status` INT(11) NOT NULL DEFAULT '0' COMMENT '1 nie aktywny w tym m-cu',
			  `sort` INT(11) NOT NULL DEFAULT '0' COMMENT 'do sortowania',
			  `Invalid` INT(11) NOT NULL DEFAULT '1',
			  PRIMARY KEY (`GUID`),
			  KEY Checked (`checked`)
			)
			;

			DROP TEMPORARY TABLE IF EXISTS `t_teams_po_work`;
			CREATE TEMPORARY TABLE `t_teams_po_work` (
			  `DayWork` DATE NOT NULL DEFAULT '0000-00-00' COMMENT 'Data pracy',
			  `Rec_Type` VARCHAR(20) DEFAULT '' NOT NULL COMMENT 'PLAN ABS a byc moze PLAN_CONFICT',
			  `GUID` VARCHAR(38) DEFAULT '' COMMENT 'GUID z `hr_team_po` lub z event lub sztuczny ale Unikalny ID',
			  `GUIDNg` VARCHAR(38) DEFAULT '' COMMENT 'GUID nagwka z tabeli `hr_team_ng`',
			  `GUIDStanowiska` VARCHAR(38) DEFAULT '' COMMENT 'GUID stanowiska',
			  `ID_pefix` INTEGER DEFAULT 0 COMMENT 'do zdublowanych nieprzypisanych stanowisk',  -- do zdublowanych nieprzypisanych stanowisk tego samego dnai9 np. dwa razy sprzedawca w dniu bez osoby aby byy dwie koluny a nie jedna',
			  `GUIDTemplate` VARCHAR(38) NOT NULL DEFAULT '' COMMENT 'ID szablonu',
			  `osobaguid` VARCHAR(38) DEFAULT '' COMMENT 'Pracownik (czonek zespolu)',
			  `AreaGUID` VARCHAR(38) DEFAULT NULL COMMENT 'ID miejsca pracy',
			  `Lp` INT(11) DEFAULT '0',
			  `uwagi` TEXT,
			  `eventGUID` VARCHAR(38) DEFAULT '' COMMENT 'Unikalny ID - moze pochodzic np. rcp lub z absencji lub z zadania crm',
			  -- `eventGUID_ZAST` VARCHAR(38) DEFAULT '' COMMENT 'GUID eventu zastepstwa',
			  -- `OsobaGUID_ZAST` VARCHAR(38) DEFAULT '' COMMENT 'GUID osoby zastepstwa',
			  -- `Status_ZAST` VARCHAR(50) DEFAULT '' COMMENT 'GUID osoby zastepstwa',
			  `Deleted` INTEGER DEFAULT 0,
			  `StartTime` DATETIME DEFAULT NULL,
			  `EndTime` DATETIME DEFAULT NULL,
			  `Time_Range_Str` VARCHAR(256) DEFAULT NULL,
			  `PLAN_duration_min` DOUBLE(16,4),
			  `BezPrzydzialu` INT(11) DEFAULT NULL,
			  `Is_Conflict_Team` INT(11) DEFAULT NULL,
			  `Is_Conflict_Absencja` INT(11) DEFAULT NULL,
			  `Conflict_Absencja_Inf_Short` VARCHAR(256) DEFAULT NULL,
			  `Conflict_Absencja_Inf` VARCHAR(256) DEFAULT NULL,
			  `Conflict_Team_Inf_Short` VARCHAR(256) DEFAULT NULL,
			  `Conflict_Team_Inf` VARCHAR(256) DEFAULT NULL,
			  `Paralel_Abs_Inf_Short` VARCHAR(256) DEFAULT NULL,
			  `Paralel_Abs_Inf` VARCHAR(256) DEFAULT NULL,
			  `Paralel_Team_Inf_Short` VARCHAR(256) DEFAULT NULL,
			  `Paralel_Team_Inf` VARCHAR(256) DEFAULT NULL,
			  `Legenda_ID` VARCHAR(50) DEFAULT NULL,
			  `alocaltime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
			  `NeedUpdate` INT(11) DEFAULT NULL,
			  UNIQUE KEY `GUID` (`GUID`),
			  KEY `GUIDNg` (`GUIDNg`),
			  KEY `OsobaGUID` (`osobaguid`),
			  KEY `eventGUID` (`eventGUID`),
			  KEY `GUIDTemplate` (`GUIDTemplate`),
			  KEY `GUIDStanowiska` (`GUIDStanowiska`),
			  KEY `ID_pefix` (`ID_pefix`),
			  -- KEY `eventGUID_ZAST` (`eventGUID_ZAST`),
			  -- KEY `OsobaGUID_ZAST` (`OsobaGUID_ZAST`),
			  KEY `DayWork` (`DayWork`),
			  KEY `StartTime` (`StartTime`),
			  KEY `EndTime` (`EndTime`),
			  KEY `Legenda_ID` (`Legenda_ID`),
			  KEY `alocaltime` (`alocaltime`),
			  KEY `NeedUpdate` (`NeedUpdate`)
			)
			;
			DROP TEMPORARY TABLE IF EXISTS `t_hr_team_www_Legenda`;
			CREATE TEMPORARY TABLE `t_hr_team_www_Legenda` (
			  `ID` VARCHAR(38) NOT NULL DEFAULT '',
			  `LP` INT(11) NOT NULL DEFAULT '0' COMMENT 'nr kolejny legendy',
			  `Symbol` VARCHAR(10) NOT NULL DEFAULT '',
			  `Nazwa` VARCHAR(200) NOT NULL DEFAULT '',
			  `Color` INT(11) NOT NULL DEFAULT '0',
			  PRIMARY KEY (`ID`)
			)
			;
			
			SET @.WWW.2.0.init = NOW();
		END IF;

		IF _MODE LIKE 'WWW.2.0.team_www_Legenda.%' THEN
			IF _MODE = 'WWW.2.0.team_www_Legenda.PREPARE' THEN
				TRUNCATE `t_hr_team_www_Legenda`;
				SET @LP := 0;
				INSERT IGNORE INTO `t_hr_team_www_Legenda` SET ID = 'ABS', LP = 0, Symbol = 'ABS', Nazwa = 'Absencja';
				INSERT IGNORE INTO `t_hr_team_www_Legenda`
				(ID, LP, Symbol, Nazwa)
				SELECT
					tn.Symbol AS ID,
					(@LP := @LP + 1) AS LP,
					tn.Symbol,
					tn.Nazwa
				FROM `t_teams_po_work` p, `hr_team_ng` tn 
				WHERE
					tn.GUID = p.GUIDNg
					AND IFNULL(tn.Symbol, '') <> ''
				GROUP BY tn.Symbol -- , p.DayWork
				;
			--	Teraz z eventow konfliktu
				INSERT IGNORE INTO `t_hr_team_www_Legenda`
				(ID, LP, Symbol, Nazwa)
				SELECT
					he.SymbolZespolow AS ID,
					(@LP := @LP + 1) AS LP,
					he.SymbolZespolow,
					he.NazwaZespolow
				FROM `hr_events_work_guids` he
				WHERE
					he.event_type = 'PLAN'	-- nie zastepstwa he.event_type LIKE 'PLAN%'
					AND IFNULL(he.SymbolZespolow, '') <> ''
				GROUP BY he.SymbolZespolow -- , p.DayWork
				;
				SET @LP := 0;
				UPDATE `t_hr_team_www_Legenda` tl SET tl.LP = (@LP := @LP + 1) WHERE ID != 'ABS' ORDER BY tl.Symbol;
			END IF;
			
			IF _MODE = 'WWW.2.0.team_www_Legenda.SELECT' THEN
				SELECT * FROM `t_hr_team_www_Legenda` ;
			END IF;
		END IF;
		

		IF _MODE = 'WWW.2.0.szablony.init' THEN
			TRUNCATE `t_hr_team_www_szablony`;
			INSERT INTO `t_hr_team_www_szablony`
			(GUID, Nazwa, Symbol)
				SELECT
					tn.GUID, tn.Nazwa, tn.symbol
				FROM `hr_team_ng` tn 
				WHERE
					tn.Template
			;	
			
			UPDATE `t_hr_team_www_szablony` bs
			SET
				bs.status = NOT EXISTS (
					SELECT 
						n.GUID 
					FROM `hr_team_ng` n 
					WHERE 
						n.Template = 0
						AND n.GUIDTemplate = bs.GUID
						AND YEAR(n.DayWork) = _Param_A 
						AND MONTH(n.DayWork) = _Param_B 
				),
				bs.Checked = (bs.GUID = NULLIF(_GUID, '')),
				bs.Invalid = NOT (bs.GUID = NULLIF(_GUID, ''))
			WHERE
				bs.GUID != 'ALL'
			;
			/*
			SELECT * FROM `t_hr_team_www_szablony` ORDER BY sort, Nazwa;
			*/
		END IF;

		IF _MODE = 'WWW.2.0.szablony.select' THEN
			SELECT * FROM `t_hr_team_www_szablony` ORDER BY sort, Nazwa;
		END IF;

		IF _MODE LIKE 'WWW.2.0.Range.%' THEN
			SET 
				@._GUIDTemplate = IF(_MODE IN ('WWW.2.0.Range.init', 'WWW.2.0.Range.init.Template'), NULLIF(_GUID, ''), NULL),
				@.p_GUID = IF(_MODE = 'WWW.2.0.Range.Init.1.P', NULLIF(_GUID, ''), NULL),
				@.n_GUID = IF(_MODE = 'WWW.2.0.Range.Init.1.N', NULLIF(_GUID, ''), NULL),
				@._YYYY_MM = _YYYY_MM,
				@._OsobaGUID = NULLIF(_OsobaGUID, '')
			;
			IF IFNULL(@SYSTEM.Client.Session.log_USER_GUID, '') = '' THEN
				CALL _sys_authorization('SESSION.SETUP.VAR', '', '', '', '', '', 'GO');
			END IF;
			UPDATE `t_hr_team_www_RANGE` r
			SET
				r.i_MODE = (@.i_MODE := 
					IF(@.p_GUID IS NOT NULL, 
						'1.P', 
						IF(@.n_GUID IS NOT NULL, 
							'1.N', 
							IF(@._GUIDTemplate IS NOT NULL, 
								'Template', 
								'TylkoOsoba'
							)
						)
					)),
					
				r.GUIDTemplate = NULLIF(@._GUIDTemplate, ''),
				r.YYYY_MM = (@.r.YYYY_MM := @._YYYY_MM),
				-- do 2021-04-23	Date_From = _var_YMToFirstDay(@.r.YYYY_MM) - INTERVAL 2 DAY,	--	w exe bylo -8 dni
				r.Date_From = _var_YMToFirstDay(@.r.YYYY_MM) - INTERVAL 0 DAY,	--	w exe bylo -8 dni
				-- do 2021-04-23	r.Date_To = _var_YMToLastDay(@.r.YYYY_MM) + INTERVAL 1 DAY, 	--	w exe bylo +6 dni
				r.Date_To = _var_YMToLastDay(@.r.YYYY_MM) + INTERVAL 0 DAY, 	--	w exe bylo +6 dni
				r.p_GUID = @.p_GUID,
				r.n_GUID = @.n_GUID,
				r.OsobaGUID = _OsobaGUID,
				r.log_USER_GUID = @SYSTEM.Client.Session.log_USER_GUID
			;
		--	Na wszelki wypadek załóż info -m-ca dla p.Aldony o ile jest Szablon grafiku
			IF @._GUIDTemplate IS NOT NULL THEN
				CALL hr_team_tools('hr_team_mc.NDE.CREATE', @._GUIDTemplate, @._YYYY_MM, '', '', '');			
			END IF;
			
			UPDATE `t_hr_team_www_RANGE` r
			SET
				r.Funkcja_Usera = (@.RANGE.Funkcja.Usera := 
					IFNULL(
					(
					SELECT
						htl.Rel
					FROM `hr_team_ng_links` htl
					WHERE
						htl.OsobaGuid = r.log_USER_GUID
						AND htl.Rel = 'Team->Kierownik'
					LIMIT 1
					),
					IFNULL(
					(
					SELECT
						htl.Rel
					FROM `hr_team_ng_links` htl
					WHERE
						htl.OsobaGuid = r.log_USER_GUID
						AND htl.Rel = 'Team->Z-ca'
					LIMIT 1
					),
					'pracownik'
					)
					)
				),
				r.Funkcja_dla_Grafiku = (@.RANGE.Funkcja.Usera := 
					IF(r.GUIDTemplate IS NULL,
						'pracownik',
						IFNULL(
						(
						SELECT
							htl.Rel
						FROM `hr_team_ng_links` htl
						WHERE
							htl.OsobaGuid = r.log_USER_GUID
							AND htl.Rel = 'Team->Kierownik'
							AND htl.GUIDNg = r.GUIDTemplate
						LIMIT 1
						),
						IFNULL(
						(
						SELECT
							htl.Rel
						FROM `hr_team_ng_links` htl
						WHERE
							htl.OsobaGuid = r.log_USER_GUID
							AND htl.Rel = 'Team->Z-ca'
							AND htl.GUIDNg = r.GUIDTemplate
						LIMIT 1
						),
						'pracownik'
						)
						)
					)
				)
			;
							
		END IF;
			
		IF _MODE LIKE 'WWW.2.0.grafiki.init%' THEN
SET @.X = '2.1';
			SET @.NEW_MODE = REPLACE(_MODE, 'WWW.2.0.grafiki', 'WWW.2.0.Range');
			CALL hr_team_www(@.NEW_MODE, _GUID, _YYYY_MM, _OsobaGUID, '', '', '');
SET @.X = '2.2';
			
			DROP TEMPORARY TABLE IF EXISTS `t_we_hr_team_po`;
			CREATE TEMPORARY TABLE `t_we_hr_team_po` (
			  `DayWork` DATE NOT NULL DEFAULT '0000-00-00' COMMENT 'Data pracy',
			  `GUID` VARCHAR(38) NOT NULL DEFAULT '' COMMENT 'Unikalny ID',
			  `GUIDNg` VARCHAR(38) NOT NULL DEFAULT '' COMMENT 'GUID nagłówka z tabeli `hr_team_ng`',
			  PRIMARY KEY GUID(GUID),
			  KEY GUIDNg(GUIDNg)
			)
			;
			IF @.i_MODE = '1.P' THEN
			--	czyli de facto 1 rekord - do odswiezania
				INSERT IGNORE INTO `t_we_hr_team_po`
				SELECT 
					n.DayWork,
					p.GUID,
					p.GUIDNg
				FROM `hr_team_po` p, `hr_team_ng` n, `t_hr_team_www_RANGE` r
				WHERE
					TRUE
					AND p.GUID = r.p_GUID
					AND n.GUID = p.GUIDNg
				;			
			ELSEIF @.i_MODE = '1.N' THEN
			--	czyli de facto 1 dzien N wierszy z 1 dnia
				INSERT IGNORE INTO `t_we_hr_team_po`
				SELECT 
					n.DayWork,
					p.GUID,
					p.GUIDNg
				FROM `hr_team_ng` n, `hr_team_po` p, `t_hr_team_www_RANGE` r
				WHERE
					TRUE
					AND n.GUID = r.n_GUID
					AND n.Template = 0
					AND p.GUIDNg = n.GUID
				;			
			ELSEIF @.i_MODE = 'Template' THEN
				INSERT IGNORE INTO `t_we_hr_team_po`
				SELECT 
					n.DayWork,
					p.GUID,
					p.GUIDNg
				FROM `hr_team_ng` n, `hr_team_po` p, `t_hr_team_www_RANGE` r
				WHERE
					TRUE
					AND n.GUIDTemplate = r.GUIDTemplate
					AND n.Template = 0
					AND n.DayWork BETWEEN r.Date_From AND r.Date_To
					AND p.GUIDNg = n.GUID
					AND IF(NULLIF(r.OsobaGUID, '') IS NULL, TRUE, p.OsobaGUID = r.OsobaGUID)
				;
			ELSEIF @.i_MODE = 'TylkoOsoba' THEN
				INSERT IGNORE INTO `t_we_hr_team_po`
				SELECT 
					n.DayWork,
					p.GUID,
					p.GUIDNg
				FROM `hr_team_ng` n, `hr_team_po` p, `t_hr_team_www_RANGE` r
				WHERE
					TRUE
					AND n.Template = 0
					AND n.DayWork BETWEEN r.Date_From AND r.Date_To
					AND p.GUIDNg = n.GUID
					-- AND p.OsobaGUID = r.OsobaGUID
					AND IF(NULLIF(r.OsobaGUID, '') IS NULL, TRUE, p.OsobaGUID = r.OsobaGUID)
				;
			END IF;
SET @.X = '2.3';
			
			TRUNCATE `t_teams_po_work`;
			INSERT IGNORE INTO `t_teams_po_work`
				SELECT 
					n.DayWork,
					'PLAN' AS Rec_Type,
					p.GUID,
					p.GUIDNg,
					p.GUIDStanowiska,
					_VarInt(0) AS ID_pefix,
					n.GUIDTemplate,
					p.osobaguid,
					p.AreaGUID,
					p.Lp,
					p.uwagi,
					he.GUID AS eventGUID,
					-- NULL AS eventGUID_ZAST,
					-- NULL AS OsobaGUID_ZAST,
					-- NULL AS Status_ZAST,
					he.Deleted,
					_VarDateTime(he.StartTime) AS StartTime,
					_VarDateTime(he.EndTime) AS EndTime,
					/*
					do 27 XI 2020 było tak
					ale teraz gdy ktos pracuje 0h to pokazujmy choc te 0h
					IF(he.duration_min <> 0,
						CONCAT(
							TIME_FORMAT(TIME(he.StartTime), '%H:%i'), ' do ', TIME_FORMAT(TIME(he.EndTime), '%H:%i'),
							' <b>', _DisplayDurationFormat(he.duration_min/60), '</b>'
						),
						''
					) AS Time_Range_Str,
					*/
					CONCAT_WS(' ',
						IF(he.duration_min <> 0,
							CONCAT(TIME_FORMAT(TIME(he.StartTime), '%H:%i'), ' do ', TIME_FORMAT(TIME(he.EndTime), '%H:%i')),
							NULL
						),
						CONCAT('<b>', _DisplayDurationFormat(he.duration_min/60), '</b>')
					) AS Time_Range_Str,
					he.duration_min AS PLAN_duration_min,
					_VarInt(IFNULL(p.OsobaGUID, '') = '') AS BezPrzydzialu,
					_VarInt(0) AS Is_Conflict_Team,
					_VarInt(0) AS Is_Conflict_Absencja,
					_VarStr('') AS Conflict_Absencja_Inf_Short,
					_VarStr('') AS Conflict_Absencja_Inf,
					_VarStr('') AS Conflict_Team_Inf_Short,
					_VarStr('') AS Conflict_Team_Inf,
					_VarStr('') AS Paralel_Abs_Inf_Short,
					_VarStr('') AS Paralel_Abs_Inf,
					_VarStr('') AS Paralel_Team_Inf_Short,
					_VarStr('') AS Paralel_Abs_Inf,
					n.Symbol AS Legenda_ID,
					p.alocaltime,
					_VarInt(1) AS NeedUpdate
				FROM `t_we_hr_team_po` twe
				INNER JOIN `hr_team_po` p ON p.GUID = twe.GUID
				INNER JOIN `hr_team_ng` n ON p.GUIDNg = n.GUID
				-- INNER JOIN `t_hr_team_www_RANGE` r
				LEFT JOIN `hr_events` he ON he.GUID = p.GUID -- AND he.Deleted = 0
			;
			IF _Param_A LIKE '%Crazy.grid%' THEN
			--	Dodatkowo dla Crazy grida robimy macierz gesta stad kilka zapytan i insertow
				DROP TEMPORARY TABLE IF EXISTS `t_teams_po_work_Osoba_x_stanowisko`;
				CREATE TEMPORARY TABLE `t_teams_po_work_Osoba_x_stanowisko` (
				  `osobaguid` VARCHAR(38) DEFAULT '' COMMENT 'Pracownik (czonek zespou)',
				  `GUIDStanowiska` VARCHAR(38) DEFAULT '' COMMENT 'GUID stanowiska',
				  PRIMARY KEY `osobaguid_GUIDStanowiska` (`osobaguid`, `GUIDStanowiska`)
				);
				-- CREATE TEMPORARY TABLE `t_teams_po_work_Osoba_x_stanowisko` 
				TRUNCATE `t_teams_po_work_Osoba_x_stanowisko`;
				INSERT IGNORE INTO `t_teams_po_work_Osoba_x_stanowisko`
				SELECT
					osobaguid, GUIDStanowiska
				FROM `t_teams_po_work` p
				WHERE
					TRUE
					AND p.OsobaGUID <> ''
					AND p.Rec_Type = 'PLAN'
				GROUP BY osobaguid, GUIDStanowiska
				;
							-- DELETE FROM t_teams_po_work WHERE guid IN ('62aea0a7-8593-11ea-b4ec-52540078c2f5*', 'dcf6017d-69c0-11ea-a033-52540078c2f5*');
				DROP TEMPORARY TABLE IF EXISTS `t_teams_po_work_Day_x_Osoba_x_stanowisko`;
				CREATE TEMPORARY TABLE `t_teams_po_work_Day_x_Osoba_x_stanowisko` (
				  `DayWork` DATE NOT NULL DEFAULT '0000-00-00' COMMENT 'Data pracy',
				  `osobaguid` VARCHAR(38) DEFAULT '' COMMENT 'Pracownik (czonek zespolu)',
				  `GUIDStanowiska` VARCHAR(38) DEFAULT '' COMMENT 'GUID stanowiska',
				  `IS_empty_Day` BOOLEAN DEFAULT 0 COMMENT 'Gdy tego dnia nie ma grafiku',
				  -- `Rec_Type` VARCHAR(20) DEFAULT '' NOT NULL COMMENT 'EMPTY gdy brak w tym dniu czlowieka lub PLAN ABS a byc moze PLAN_CONFICT',
				  -- `GUID` VARCHAR(38) DEFAULT '' COMMENT 'GUID z `hr_team_po` lub z event lub sztuczny ale Unikalny ID',
				  -- `ID_pefix` INTEGER DEFAULT 0 COMMENT 'do zdublowanych nieprzypisanych stanowisk',  -- do zdublowanych nieprzypisanych stanowisk tego samego dnai9 np. dwa razy sprzedawca w dniu bez osoby aby byy dwie koluny a nie jedna',
				  -- `GUIDTemplate` VARCHAR(38) NOT NULL DEFAULT '' COMMENT 'ID szablonu',
				  `StartTime` DATETIME DEFAULT NULL,
				  `EndTime` DATETIME DEFAULT NULL,
				  UNIQUE KEY `DayWork_Osoba_stanowisko` (`DayWork`, `osobaguid`, `GUIDStanowiska`),
				  KEY `IS_empty_Day` (`IS_empty_Day`),
				  KEY `StartTime` (`StartTime`),
				  KEY `EndTime` (`EndTime`)
				);  
				TRUNCATE `t_teams_po_work_Day_x_Osoba_x_stanowisko`;
				INSERT IGNORE INTO `t_teams_po_work_Day_x_Osoba_x_stanowisko`
				SELECT 
					cal.Date AS DayWork,
					p.OsobaGUID,
					p.GUIDStanowiska,
					-- (p.GUID IS NULL) OR (p.Rec_Type <> 'PLAN') as IS_empty_Day,
					1 AS IS_empty_Day,
					-- p.Rec_Type
					-- , p.GUID 
					cal.Date AS StartTime,
					(cal.Date + INTERVAL 1 DAY) - INTERVAL 1 SECOND AS EndTime
				FROM `t_hr_team_www_RANGE` r
				-- INNER JOIN `v_sys_calendar` cal ON cal.Date BETWEEN _var_YMToFirstDay(r.YYYY_MM) AND _var_YMToLastDay(r.YYYY_MM)
				INNER JOIN `v_sys_calendar` cal ON cal.Date BETWEEN r.Date_From AND r.Date_To
				LEFT JOIN `t_teams_po_work_Osoba_x_stanowisko` p ON TRUE -- ON p.OsobaGUID = o.OsobaGUID and p.DayWork = cal.Date
				WHERE
					TRUE
				GROUP BY DayWork, osobaguid, GUIDStanowiska
				;	--	180
				UPDATE `t_teams_po_work_Day_x_Osoba_x_stanowisko` t_d_x_o, `t_teams_po_work` p
				SET
					t_d_x_o.IS_empty_Day = 0
				WHERE
					p.OsobaGUID = t_d_x_o.OsobaGUID 
					AND p.DayWork = t_d_x_o.DayWork
					AND p.GUIDStanowiska = t_d_x_o.GUIDStanowiska
				;
				DELETE FROM `t_teams_po_work_Day_x_Osoba_x_stanowisko` WHERE NOT IS_empty_Day;
				-- SELECT * FROM `t_teams_po_work_Day_x_Osoba_x_stanowisko`;
			--	Dodatkowo dodaj jeszcze BEZ POWT. eventy pozostałe głownie chodzi o absemcje ABS
				INSERT IGNORE INTO `t_teams_po_work`
					SELECT 
						t_d_x_o.DayWork,
						'EMPTY' AS Rec_Type,
						_Get_ID_xxxxx('SHORT', 38, 'EMPTY:', NULL, NULL, NULL, NULL, NULL) AS GUID,
						NULL AS GUIDNg,
						t_d_x_o.GUIDStanowiska AS GUIDStanowiska,
						_VarInt(0) AS ID_pefix,
						-- NULL AS GUIDTemplate,
						NULL AS GUIDTemplate,
						t_d_x_o.osobaguid,
						NULL AS AreaGUID,
						0 AS Lp,
						NULL AS uwagi,
						NULL AS eventGUID,
						-- NULL AS eventGUID_ZAST,
						-- NULL AS OsobaGUID_ZAST,
						-- NULL AS Status_ZAST,
						NULL AS Deleted,
						t_d_x_o.StartTime,
						t_d_x_o.EndTime,
					--	było 	'Wolne' AS Time_Range_Str,
						'' AS Time_Range_Str,
						0 AS PLAN_duration_min,
						TRUE AS BezPrzydzialu,
						_VarInt(0) AS Is_Conflict_Team,
						_VarInt(0) AS Is_Conflict_Absencja,
						_VarStr('') AS Conflict_Absencja_Inf_Short,
						_VarStr('') AS Conflict_Absencja_Inf,
						_VarStr('') AS Conflict_Team_Inf_Short,
						_VarStr('') AS Conflict_Team_Inf,
						_VarStr('') AS Paralel_Abs_Inf_Short,
						_VarStr('') AS Paralel_Abs_Inf,
						_VarStr('') AS Paralel_Team_Inf_Short,
						_VarStr('') AS Paralel_Abs_Inf,
						'EMPTY' AS Legenda_ID,
						NOW() AS alocaltime,
						_VarInt(1) AS NeedUpdate
					FROM `t_teams_po_work_Day_x_Osoba_x_stanowisko` t_d_x_o
				;
				
			END IF;
			CALL hr_team_www('WWW.2.0.grafiki.update', _GUID, _YYYY_MM, _OsobaGUID, _Param_A, '', '');
		END IF;

		
		IF _MODE = 'WWW.2.0.grafiki.update' THEN
							set @.osoby.gantt.update = '1';	
			UPDATE `t_teams_po_work` w, `hr_team_po` p
			SET
				w.alocaltime = p.alocaltime,
				w.NeedUpdate = 1
			WHERE
				w.GUID = p.GUID
				AND
				w.NeedUpdate = 0
				AND
				w.alocaltime < p.alocaltime
			;
			SET @t_teams_po_work.ROW_COUNT = ROW_COUNT();
			
			DROP TEMPORARY TABLE IF EXISTS `t_teams_po_work_osoby`;
			CREATE TEMPORARY TABLE `t_teams_po_work_osoby` (
			  `OsobaGUID` VARCHAR(38) NOT NULL DEFAULT '',
			  `Sum_Prac_mc_team_h` DOUBLE(16,4),
			  `Sum_Prac_mc_all_tems_h` DOUBLE(16,4),
			  `Is_Hidden` BOOLEAN DEFAULT FALSE,
			  PRIMARY KEY OsobaGUID (OsobaGUID)
			)
			;
			
			INSERT INTO `t_teams_po_work_osoby` 
				SELECT 
					DISTINCT t.OsobaGUID,
					0, 0, FALSE
				FROM `t_teams_po_work` t
				WHERE
					NeedUpdate
			;	
			
								set @.osoby.gantt.update = '2';	
		--	Dodaj wszystkie eventy wszystkich osób
			DROP TEMPORARY TABLE IF EXISTS `hr_events_work_guids`;
			CREATE TEMPORARY TABLE `hr_events_work_guids` (
			  `GUID` VARCHAR(38) DEFAULT '' COMMENT 'Unikalny ID - moze pochodzic np. rcp lub z absencji lub z zadania crm',
			  `OsobaGUID` VARCHAR(38) DEFAULT '' COMMENT 'Pracownik (posiadacz karty)',
			  `StartTime` DATETIME DEFAULT '0000-00-00 00:00:00' COMMENT 'Rozpoczecie',
			  `EndTime` DATETIME DEFAULT NULL COMMENT 'Zakonczenie',
			  `duration_min` DOUBLE(16,4),
			  `PLAN_duration_min` DOUBLE(16,4),
			  `Curr_YYYY_MM` INTEGER,
			  `start_YYYY_MM` INTEGER,
			  `event_type` VARCHAR(20) DEFAULT '' COMMENT 'typ zdarzenia "WORK","JOB","ABS","CRM","PLAN" etc ',
			  `SrcGUID` VARCHAR(38) DEFAULT '' COMMENT 'Zrodlowy ID - na wszelki wypadek',
			  `event_source` VARCHAR(20) DEFAULT '' COMMENT 'skad pochodzi wpis "" wpis reczny, "RCP","ABS","CRM","PLAN" etc ',
			  `rodzaj` INT(11) DEFAULT '0' COMMENT 'rodzaj np. z `crm_commisions` lub `absencje_dict`',
			  `SymbolZespolow_Info` VARCHAR(150) DEFAULT NULL,
			  `SymbolZespolow` VARCHAR(150) DEFAULT NULL,
			  `NazwaZespolow` VARCHAR(150) DEFAULT NULL,
			  `DayWork` DATE,
			  `areaGUID` VARCHAR(38),
			  `GUIDTemplate` VARCHAR(38),
			  `Deleted` INTEGER DEFAULT 0,
			  PRIMARY KEY `GUID` (`GUID`),
			  KEY `OsobaGUID` (`OsobaGUID`),
			  KEY `StartTime` (`StartTime`),
			  KEY `EndTime` (`EndTime`),
			  KEY `DayWork` (`DayWork`),
			  KEY `Deleted` (`Deleted`)
			)
			;
			
			-- CREATE TEMPORARY TABLE `hr_events_work_guids` 
			INSERT IGNORE INTO `hr_events_work_guids`
				SELECT 
					he.GUID, he.OsobaGUID, he.StartTime, he.EndTime, 
					he.duration_min,
					IF(he.event_type IN ('PLAN') AND (YEAR(he.StartTime) * 100 + MONTH(he.StartTime) = r.YYYY_MM),
						he.duration_min, 
						0
					) AS PLAN_duration_min,
					r.YYYY_MM AS Curr_YYYY_MM,
					he.start_YYYY_MM,
					he.event_type, he.SrcGUID, he.event_source, he.rodzaj,
					_VarStr(
					CONCAT_WS(' ',
						tn.Symbol,
						CONCAT(
							_VarStr(TIME_FORMAT(TIME(he.StartTime), '%H:%i')),
							' - ',
							_VarStr(TIME_FORMAT(TIME(he.EndTime), '%H:%i'))
						),
						NULLIF(tn.Nazwa, '')
					)
					) AS SymbolZespolow_Info,
					tn.Symbol AS SymbolZespolow,
					tn.Nazwa AS NazwaZespolow,
					DATE(he.StartTime) AS DayWork,
					he.AreaGUID,
					tn.GUIDTemplate,
					he.Deleted
				FROM `t_teams_po_work_osoby` t
				INNER JOIN `t_hr_team_www_RANGE` r
				LEFT JOIN `hr_events` he ON he.OsobaGUID = t.OsobaGUID -- AND he.Deleted = 0 
				LEFT JOIN `hr_team_ng` tn ON tn.GUID = he.SrcGUID
				WHERE
					TRUE
					AND he.Deleted = 0 	--	dodalem bo i tak wszedzie uwzgledniam ten warunek wiec po co w ogole pobierac usuniete
					AND he.event_type IN ('ABS', 'PLAN', 'PLAN_REPLACEMENT')
					AND he.EndTime >= r.Date_From	
					AND DATE(he.StartTime) <= r.Date_To	
				GROUP BY he.GUID
			;
								set @.osoby.gantt.update = '3';	
		--	Teraz konflikty
			DROP TEMPORARY TABLE IF EXISTS `t_teams_po_work_conflict`;
			CREATE TEMPORARY TABLE `t_teams_po_work_conflict` (
			  `GUID` VARCHAR(38) DEFAULT '' COMMENT '`hr_team_po` Unikalny ID',
			  `Conflict_CC` INT(11) NOT NULL DEFAULT '0',
			  `Conflict_Info_short` VARCHAR(256),
			  `Conflict_Info` VARCHAR(256),
			--  `Conflict_Info` VARCHAR(256),
			  PRIMARY KEY GUID(GUID)
			)
			;			
		--	Konflikt z innymi teamami/brygadami
			TRUNCATE `t_teams_po_work_conflict`;
			INSERT IGNORE INTO `t_teams_po_work_conflict`
			SELECT
				p.GUID, 
				COUNT(DISTINCT(he.GUID)) AS Conflict_CC,
				GROUP_CONCAT(DISTINCT(he.SymbolZespolow)) AS Conflict_Info_short,
				GROUP_CONCAT(DISTINCT(he.SymbolZespolow_Info)) AS Conflict_Info
			FROM `t_teams_po_work` p, `hr_events_work_guids` he 
			WHERE
				-- he.GUID != p.eventGUID
				NOT (he.GUID <=> p.eventGUID)
				AND p.Rec_Type = 'PLAN'		--	EMPTY celki nie spr. konfliktów
				AND p.PLAN_duration_min > 0	--	Dla niepracujacej celki nie spr. konfliktów
				AND he.duration_min <> 0	--	gdy pracuje 0h to nie zawracaj głowy
				AND he.OsobaGUID = p.OsobaGUID  
				AND he.DayWork = p.DayWork 
				AND he.event_source IN ('PLAN', 'PLAN') 
				AND he.event_type IN ('PLAN', 'PLAN')
				AND he.Deleted = 0 
				AND 
				(
					(p.StartTime BETWEEN he.StartTime AND he.EndTime) OR 
					(p.EndTime BETWEEN he.StartTime AND he.EndTime) OR
					(he.StartTime BETWEEN p.StartTime AND p.EndTime) OR 
					(he.EndTime BETWEEN p.StartTime AND p.EndTime)
				)
			GROUP BY p.GUID
			;	
								set @.osoby.gantt.update = '4';	
			UPDATE `t_teams_po_work` p, `t_teams_po_work_conflict` t
			SET
				p.Is_Conflict_Team = t.Conflict_CC,
				p.Conflict_Team_Inf_Short = t.Conflict_Info_Short,
				p.Conflict_Team_Inf = t.Conflict_Info
			WHERE
				p.GUID = t.GUID
			;
								set @.osoby.gantt.update = '5';	
		--	zanim konflikt absencyjny stworz tabele do legendy póki masz w `t_teams_po_work_conflict` konfliktu zespołów
			CALL hr_team_www('WWW.2.0.team_www_Legenda.PREPARE', '', '', '', '', '', '');
		--	Konflikt z absencjami
			TRUNCATE `t_teams_po_work_conflict`;
			INSERT IGNORE INTO `t_teams_po_work_conflict`
			SELECT
				p.GUID, 
				COUNT(DISTINCT(he.GUID)) AS Conflict_CC,
				GROUP_CONCAT(DISTINCT CONCAT(ad.symbol, ': ', 1 + TIMESTAMPDIFF(DAY, he.StartTime, he.EndTime), ' dn')) AS Conflict_Info_Short,
				-- było GROUP_CONCAT(DISTINCT CONCAT(ad.symbol, ': ', _VarStr(_DATE_Range('dni', he.StartTime, he.EndTime, 'MMM', '')))) AS Conflict_Info
				GROUP_CONCAT(DISTINCT CONCAT(ad.symbol, ': ', _VarStr(_Fmt_DateTime_Range(he.StartTime, he.EndTime, '<MMM><dni>')))) AS Conflict_Info
			FROM `t_teams_po_work` p, `hr_events_work_guids` he
			INNER JOIN `absencje` aa ON aa.GUID = he.GUID
			LEFT JOIN `absencje_dict` ad ON ad.idn = aa.rodzaj
			WHERE
				-- he.GUID != p.eventGUID
				NOT (he.GUID <=> p.eventGUID)
				AND p.Rec_Type = 'PLAN'		--	EMPTY celki nie spr. konfliktów
				AND p.PLAN_duration_min > 0	--	Dla niepracujacej celki nie spr. konfliktów
				AND he.duration_min <> 0	--	gdy pracuje 0h to nie zawracaj głowy
				AND he.OsobaGUID = p.OsobaGUID  
				AND he.event_source IN ('ABS') 
				AND he.Deleted = 0 
				AND 
				(
					(p.StartTime BETWEEN he.StartTime AND he.EndTime) OR 
					(p.EndTime BETWEEN he.StartTime AND he.EndTime) OR
					(he.StartTime BETWEEN p.StartTime AND p.EndTime) OR 
					(he.EndTime BETWEEN p.StartTime AND p.EndTime)
				)
			GROUP BY p.GUID
			;	
								set @.osoby.gantt.update = '6';	
			UPDATE `t_teams_po_work` p, `t_teams_po_work_conflict` t
			SET
				p.Is_Conflict_Absencja = t.Conflict_CC,
				p.Conflict_Absencja_Inf_Short = t.Conflict_Info_Short,
				p.Conflict_Absencja_Inf = t.Conflict_Info
			WHERE
				p.GUID = t.GUID
			;
		/*
			do 27 XI 2020 było tak że do labelek zbieraliśmy tylko konflikty
			ale teraz mamy zawsze pokazywać wszystkie sasiednie prace na innych grafikach - oczywiście jak juz sa konflikty to omijamy (bez dubli!!!)
			Pseudo Konflik - pobieramy wszystkie sasiednie prace na innych grafikach ale tylko dla tych dla których nie ma konfliktów (konfl. zostawiamy juz)
		*/
		--	Pralelne - Pseudo-konflikt z innymi grafikami
			TRUNCATE `t_teams_po_work_conflict`;
			INSERT IGNORE INTO `t_teams_po_work_conflict`
			SELECT
				p.GUID, 
				0 AS Conflict_CC, -- 0 bo bierzemy celki które nie maja żadnego konfliktu
				GROUP_CONCAT(DISTINCT(he.SymbolZespolow)) AS Conflict_Info_short,
				GROUP_CONCAT(DISTINCT(he.SymbolZespolow_Info)) AS Conflict_Info
			FROM `t_teams_po_work` p, `hr_events_work_guids` he 
			WHERE
				-- he.GUID != p.eventGUID
				NOT (he.GUID <=> p.eventGUID)
				AND he.duration_min <> 0	--	gdy pracuje 0h to nie zawracaj głowy
				AND p.Is_Conflict_Absencja = 0 -- tylko dla celek które nie maja żadnego konfliktu
				AND p.Is_Conflict_Team = 0 -- tylko dla celek które nie maja żadnego konfliktu
				AND he.OsobaGUID = p.OsobaGUID  
				AND he.DayWork = p.DayWork 
				AND he.event_source IN ('PLAN', 'PLAN') 
				AND he.event_type IN ('PLAN', 'PLAN')
				AND he.Deleted = 0 
				AND DATE(p.StartTime) = DATE(he.StartTime)
				AND DATE(p.EndTime) = DATE(he.EndTime)
				/*
				(
					(p.StartTime BETWEEN he.StartTime AND he.EndTime) OR 
					(p.EndTime BETWEEN he.StartTime AND he.EndTime) OR
					(he.StartTime BETWEEN p.StartTime AND p.EndTime) OR 
					(he.EndTime BETWEEN p.StartTime AND p.EndTime)
				)
				*/
			GROUP BY p.GUID
			;	
																					set @.osoby.gantt.update = '6';	
			UPDATE `t_teams_po_work` p, `t_teams_po_work_conflict` t
			SET
				p.Paralel_Team_Inf_Short = t.Conflict_Info_Short,
				p.Paralel_Team_Inf = t.Conflict_Info
			WHERE
				p.GUID = t.GUID
			;
		--	Pralelne absencje Pseudo-konflikt z innymi absencjami
			TRUNCATE `t_teams_po_work_conflict`;
			INSERT IGNORE INTO `t_teams_po_work_conflict`
			SELECT
				p.GUID, 
				0 AS Conflict_CC, -- 0 bo bierzemy celki które nie maja żadnego konfliktu
				GROUP_CONCAT(DISTINCT CONCAT(ad.symbol, ': ', 1 + TIMESTAMPDIFF(DAY, he.StartTime, he.EndTime), ' dn')) AS Conflict_Info_Short,
				-- było GROUP_CONCAT(DISTINCT CONCAT(ad.symbol, ': ', _VarStr(_DATE_Range('dni', he.StartTime, he.EndTime, 'MMM', '')))) AS Conflict_Info
				GROUP_CONCAT(DISTINCT CONCAT(ad.symbol, ': ', _VarStr(_Fmt_DateTime_Range(he.StartTime, he.EndTime, '<MMM><dni>')))) AS Conflict_Info
			FROM `t_teams_po_work` p, `hr_events_work_guids` he
			INNER JOIN `absencje` aa ON aa.GUID = he.GUID
			LEFT JOIN `absencje_dict` ad ON ad.idn = aa.rodzaj
			WHERE
				-- he.GUID != p.eventGUID
				NOT (he.GUID <=> p.eventGUID)
				AND he.duration_min <> 0	--	gdy pracuje 0h to nie zawracaj głowy
				AND p.Is_Conflict_Absencja = 0 -- tylko dla celek które nie maja żadnego konfliktu
				AND p.Is_Conflict_Team = 0 -- tylko dla celek które nie maja żadnego konfliktu
				AND he.OsobaGUID = p.OsobaGUID  
				AND he.event_source IN ('ABS') 
				AND he.Deleted = 0 
				AND 
				(
					(p.StartTime BETWEEN he.StartTime AND he.EndTime) OR 
					(p.EndTime BETWEEN he.StartTime AND he.EndTime) OR
					(he.StartTime BETWEEN p.StartTime AND p.EndTime) OR 
					(he.EndTime BETWEEN p.StartTime AND p.EndTime)
				)
			GROUP BY p.GUID
			;	
								set @.osoby.gantt.update = '6';	
			UPDATE `t_teams_po_work` p, `t_teams_po_work_conflict` t
			SET
				p.Paralel_Abs_Inf_Short = t.Conflict_Info_Short,
				p.Paralel_Abs_Inf = t.Conflict_Info
			WHERE
				p.GUID = t.GUID
			;
			
			-- SELECT * FROM `t_teams_po_work_conflict`;	
			UPDATE `t_teams_po_work` p
			SET
				p.NeedUpdate = 0
			;	
			LEAVE S_hr_team_www;
		END IF;
		
		
		IF _MODE LIKE 'WWW.2.0.gantt.INIT%' THEN
			SET @.NEW_MODE = REPLACE(_MODE, 'WWW.2.0.gantt', 'WWW.2.0.grafiki');
			CALL hr_team_www(@.NEW_MODE, _GUID, _YYYY_MM, _OsobaGUID, _Param_A, _Param_B, _Param_C);
			-- CALL hr_team_www('WWW.2.0.init', '', '', '', '', '', '');
			CALL hr_team_www('WWW.2.0.team_www_Legenda.PREPARE', '', '', '', '', '', '');

		END IF;
		
		IF _MODE = 'WWW.2.0.gantt.SELECT' THEN
			SELECT 
				p.GUID,
				p.Rec_Type,
				_VarStr(_Day_Month_Year_Fmt(p.StartTime, 'DD MR')) AS DayWorkStr,
				_VarStr(CONCAT_WS(' /\n', 
					_VarStr(p.Time_Range_Str),
					(@.Event_PLAN_ZAST := 
					IF(zast.GUID IS NULL, NULL, _VarStr(CONCAT('Zastępstwo: ', IF(zast.Status = 0, 'niepotw.', IF(zast.Status = 3, 'zgoda', 'odrzucone')), ' - ', ozast.Nazwisko, ' ', ozast.Imiona)))
					),
					_VarStr(CONCAT('Konfl.pracy!!!(', NULLIF(p.Is_Conflict_Team, 0), ')')),
					_VarStr(CONCAT('Konf.absenc.!!!(', NULLIF(p.Is_Conflict_Absencja, 0), ')')),
					NULL	
				)) AS Event_STR,
				_VarStr(CONCAT_WS(' /\n', 
					CONCAT(o.Nazwisko, ' ', o.Imiona),
					_VarStr(p.Time_Range_Str),
					NULL	
				)) AS Event_Gantt_STR,
				_VarStr(CONCAT_WS(' /\n', 
					_VarStr(p.Time_Range_Str),
					@.Event_PLAN_ZAST,
					_VarStr(CONCAT('Konfl.pracy!!!(', NULLIF(IF(p.Is_Conflict_Team > 0, _VarStr(p.Conflict_Team_Inf), ''), ''), ')')),
					_VarStr(CONCAT('Konf.absenc.!!!(', NULLIF(IF(p.Is_Conflict_Absencja > 0, _VarStr(p.Conflict_Absencja_Inf), ''), ''), ')')),
					NULL	
				)) AS Event_Hint_STR,
				/*
				_VarStr(CONCAT_WS(' / ',	--	albo " /\n"
					CONCAT(o.Nazwisko, ' ', o.Imiona),
					CONCAT(
						_VarStr(TIME_FORMAT(TIME(p.StartTime), '%H:%i')), ' do ', _VarStr(TIME_FORMAT(TIME(p.EndTime), '%H:%i')),
						' <b>', _DisplayDurationFormat(p.PLAN_duration_min/60), '</b>'
					)
				)) AS Event_Gantt_STR,
				_VarStr(CONCAT_WS(' / ',	--	albo " /\n"
					IF(p.Rec_Type = 'ABS',
				było		CONCAT('<b>Absencja</b> ', _DATE_Range('', p.StartTime, p.EndTime, 'mmm', 0)),
						CONCAT(
							TIME_FORMAT(TIME(p.StartTime), '%H:%i'), ' do ', TIME_FORMAT(TIME(p.EndTime), '%H:%i'),
							' <b>', _DisplayDurationFormat(p.PLAN_duration_min/60), '</b>'
						)
					),
					CONCAT('Konfl.pracy!!!(', NULLIF(p.Is_Conflict_Team, 0), ')'),
					CONCAT('Konf.absenc.!!!(', NULLIF(p.Is_Conflict_Absencja, 0), ')')
				)) AS Event_STR,
				*/
				CONCAT(TIME_FORMAT(TIME(p.StartTime), '%H:%i'), ' do ', TIME_FORMAT(TIME(p.EndTime), '%H:%i')) AS StartEndTime_STR,
				p.OsobaGUID,
				(NULLIF(p.OsobaGUID, '') IS NULL) AS brak_osoby,
				CONCAT(o.Nazwisko, ' ', o.Imiona) AS NazwiskoImiona,
				s.Stanowisko,
				

				(p.Is_Conflict_Absencja + p.Is_Conflict_Team > 0) AS Is_Confict,
				p.Is_Conflict_Absencja,
				p.Is_Conflict_Team,
				IF(p.Is_Conflict_Absencja, p.Conflict_Absencja_Inf, '') AS Conflict_Absencja_Str,
				IF(p.Is_Conflict_Team, p.Conflict_Team_Inf, '') AS Conflict_Team_Str,
				
				-- p.StartTime,
				-- p.EndTime
				_VarStr(p.StartTime) AS StartTime,
				_VarStr(p.EndTime) AS EndTime,
				IF(cal.GOV_Free_Day,
					3,
					IF(cal.DW = 7,
						2,
						IF(cal.DW = 6,
							1,
							0
						)
					)
				) AS day_type,
				cal.Working_Day_A AS Working_Day,
				p.Legenda_ID,
				leg.LP AS Legenda_LP
				
				/*
				o.Nazwisko,
				o.Imiona,
				-- t.*,
				-- _VarStr(CONCAT(_Day_Month_Year_Fmt(t.StartTime, 'DD MR'), ', ', s.Stanowisko)) AS Nazwa_Gantt,
				StartTime AS st_conf_ABS,
				_varDateTime(IF(Is_Conflict_Absencja, EndTime, StartTime)) AS et_conf_ABS,
				StartTime AS st_conf_Team,
				_varDateTime(IF(Is_Conflict_Team, EndTime, StartTime)) AS et_conf_Team,
				StartTime AS st_brak_osoby,
				_varDateTime(IF(IFNULL(p.OsobaGUID, '') = '', p.EndTime, p.StartTime)) AS et_brak_osoby
				*/
				
			FROM `t_teams_po_work` p
			LEFT JOIN `osoby` o ON o.GUID = p.OsobaGUID
			LEFT JOIN `hr_events` zast ON zast.GUID = NULL -- _Get_ID_xxxxx('', 38, 'ZAST:', p.GUID, '', '', '', '')
			LEFT JOIN `osoby` ozast ON ozast.GUID = zast.OsobaGUID -- p.OsobaGUID_ZAST
			LEFT JOIN `stanowiska` s ON s.GUID = p.GUIDStanowiska
			LEFT JOIN `v_sys_calendar` cal ON cal.Date = p.DayWork
			LEFT JOIN `t_hr_team_www_Legenda` leg ON leg.ID = p.Legenda_ID
			WHERE
				TRUE
			ORDER BY p.StartTime, p.Lp	
			;
		END IF;		
		
		IF _MODE LIKE 'WWW.2.0.calendar.INIT%' THEN
			SET @.NEW_MODE = REPLACE(_MODE, 'WWW.2.0.calendar', 'WWW.2.0.grafiki');
			CALL hr_team_www(@.NEW_MODE, _GUID, _YYYY_MM, _OsobaGUID, _Param_A, _Param_B, _Param_C);
			-- CALL hr_team_www('WWW.2.0.init', '', '', '', '', '', '');
		--	Dodatkowo dodaj jeszcze BEZ POWT. eventy pozostałe głownie chodzi o absemcje ABS
			INSERT IGNORE INTO `t_teams_po_work`
				SELECT 
					DATE(he.StartTime) AS DayWork,
					he.event_type AS Rec_Type,
					he.GUID,
					NULL AS GUIDNg,
					NULL AS GUIDStanowiska,
					_VarInt(0) AS ID_pefix,
					-- NULL AS GUIDTemplate,
					he.SrcGUID AS GUIDTemplate,
					he.osobaguid,
					he.AreaGUID,
					0 AS Lp,
					NULL AS uwagi,
					he.GUID AS eventGUID,
					-- NULL AS eventGUID_ZAST,
					-- NULL AS OsobaGUID_ZAST,
					-- NULL AS Status_ZAST,
					he.Deleted,
					_VarDateTime(he.StartTime) AS StartTime,
					_VarDateTime(he.EndTime) AS EndTime,
					IF(he.event_type = 'ABS',
						CONCAT(
							'<b>Absencja</b> ', 
							-- było _DATE_Range('', he.StartTime, he.EndTime, 'mmm', 0)
							_Fmt_DateTime_Range(he.StartTime, he.EndTime, '<MMM>')
						),
						CONCAT(
							TIME_FORMAT(TIME(he.StartTime), '%H:%i'), ' do ', TIME_FORMAT(TIME(he.EndTime), '%H:%i'),
							' <b>', _DisplayDurationFormat(he.duration_min/60), '</b>'
						)
					) AS Time_Range_Str,
					0 AS PLAN_duration_min,
					_VarInt(IFNULL(he.OsobaGUID, '') = '') AS BezPrzydzialu,
					_VarInt(0) AS Is_Conflict_Team,
					_VarInt(0) AS Is_Conflict_Absencja,
					_VarStr('') AS Conflict_Absencja_Inf_Short,
					_VarStr('') AS Conflict_Absencja_Inf,
					_VarStr('') AS Conflict_Team_Inf_Short,
					_VarStr('') AS Conflict_Team_Inf,
					_VarStr('') AS Paralel_Abs_Inf_Short,
					_VarStr('') AS Paralel_Abs_Inf,
					_VarStr('') AS Paralel_Team_Inf_Short,
					_VarStr('') AS Paralel_Abs_Inf,
					he.event_type AS Legenda_ID,
					NOW() AS alocaltime,
					_VarInt(1) AS NeedUpdate
				FROM `hr_events_work_guids` he
				-- INNER JOIN `hr_team_po` p ON p.GUID = twe.GUID
				-- INNER JOIN `hr_team_ng` n ON p.GUIDNg = n.GUID
				-- INNER JOIN `t_hr_team_www_RANGE` r
				-- LEFT JOIN `hr_events` he ON he.GUID = p.GUID AND he.Deleted = 0
				WHERE
					TRUE
					AND he.Deleted = 0 
			;
			CALL hr_team_www('WWW.2.0.team_www_Legenda.PREPARE', '', '', '', '', '', '');

			
		END IF;

		IF _MODE = 'WWW.2.0.calendar.SELECT' THEN
			SELECT 
				p.GUID,
				p.Rec_Type,
				tpzast.GUID AS GUIDZast,
				_VarStr(_Day_Month_Year_Fmt(p.StartTime, 'DD MR')) AS DayWorkStr,
				_VarStr(CONCAT_WS(' /<br>', 
					_VarStr(p.Time_Range_Str),
					templ.Nazwa,
					(@.Event_PLAN_ZAST := 
					-- IF(zast.GUID IS NULL, NULL, _VarStr(CONCAT('Zastępstwo: ', IF(zast.Status = 0, 'niepotw.', IF(zast.Status = 3, 'zgoda', 'odrzucone')), ' - ', ozast.Nazwisko, ' ', ozast.Imiona)))
					IF(zast.GUID IS NULL, NULL, _VarStr(CONCAT('Zastępstwo: ', IF(zast.Status = 0, 'niepotw.', IF(zast.Status = 3, 'zgoda', 'odrzucone')), ' za ', ozast.Nazwisko, ' ', ozast.Imiona)))
					),
					_VarStr(CONCAT('Konfl.pracy!!!(', NULLIF(p.Is_Conflict_Team, 0), ')')),
					_VarStr(CONCAT('Konf.absenc.!!!(', NULLIF(p.Is_Conflict_Absencja, 0), ')')),
					NULL	
				)) AS Event_STR,
				_VarStr(CONCAT_WS(' /<br>', 
					CONCAT(o.Nazwisko, ' ', o.Imiona),
					_VarStr(p.Time_Range_Str),
					NULL	
				)) AS Event_Gantt_STR,
				_VarStr(CONCAT_WS(' /<br>', 
					_VarStr(p.Time_Range_Str),
					@.Event_PLAN_ZAST,
					_VarStr(CONCAT('Konfl.pracy!!!(', NULLIF(IF(p.Is_Conflict_Team > 0, _VarStr(p.Conflict_Team_Inf), ''), ''), ')')),
					_VarStr(CONCAT('Konf.absenc.!!!(', NULLIF(IF(p.Is_Conflict_Absencja > 0, _VarStr(p.Conflict_Absencja_Inf), ''), ''), ')')),
					NULL	
				)) AS Event_Hint_STR,
				CONCAT(TIME_FORMAT(TIME(p.StartTime), '%H:%i'), ' do ', TIME_FORMAT(TIME(p.EndTime), '%H:%i')) AS StartEndTime_STR,
				p.OsobaGUID,
				(NULLIF(p.OsobaGUID, '') IS NULL) AS brak_osoby,
				CONCAT(o.Nazwisko, ' ', o.Imiona) AS NazwiskoImiona,
				s.Stanowisko,

				(p.Is_Conflict_Absencja + p.Is_Conflict_Team > 0) AS Is_Confict,
				p.Is_Conflict_Absencja,
				p.Is_Conflict_Team,
				IF(p.Is_Conflict_Absencja, p.Conflict_Absencja_Inf, '') AS Conflict_Absencja_Str,
				IF(p.Is_Conflict_Team, p.Conflict_Team_Inf, '') AS Conflict_Team_Str,
				
				-- na razie p.StartTime,
				-- na razie p.EndTime
				_VarStr(p.StartTime) AS StartTime,
				_VarStr(p.EndTime) AS EndTime,
				IF(cal.GOV_Free_Day,
					3,
					IF(cal.DW = 7,
						2,
						IF(cal.DW = 6,
							1,
							0
						)
					)
				) AS day_type,
				cal.Working_Day_A AS Working_Day,
				p.Legenda_ID,
				leg.LP AS Legenda_LP
				
			FROM `t_teams_po_work` p
			LEFT JOIN `osoby` o ON o.GUID = p.OsobaGUID
			/*
			było do 17 IX 2020 
			LEFT JOIN `hr_events` zast ON zast.GUID = NULL -- _Get_ID_xxxxx('', 38, 'ZAST:', p.GUID, '', '', '', '')
			LEFT JOIN `osoby` ozast ON ozast.GUID = zast.OsobaGUID -- p.OsobaGUID_ZAST
			*/
			LEFT JOIN `hr_events` zast ON zast.GUID = p.GUID -- _Get_ID_xxxxx('', 38, 'ZAST:', p.GUID, '', '', '', '')
			LEFT JOIN `hr_team_po` tpzast ON tpzast.GUIDNg = zast.SrcGUID AND _Get_ID_xxxxx('', 38, 'ZAST:', tpzast.GUID, '', '', '', '') = zast.GUID
			LEFT JOIN `osoby` ozast ON ozast.GUID = tpzast.OsobaGUID -- zast.OsobaGUID -- p.OsobaGUID_ZAST
			LEFT JOIN `stanowiska` s ON s.GUID = p.GUIDStanowiska
			-- LEFT JOIN `hr_team_ng` tn ON tn.GUID = p.GUIDNg
			LEFT JOIN `v_sys_calendar` cal ON cal.Date = p.DayWork
			LEFT JOIN `t_hr_team_www_Legenda` leg ON leg.ID = p.Legenda_ID
			LEFT JOIN `hr_team_ng` templ ON templ.GUID = p.GUIDTemplate
			WHERE
				TRUE
			ORDER BY p.StartTime, p.Lp	
			;
		END IF;		
				
		IF _MODE LIKE 'WWW.2.0.Crazy.grid.INIT%' THEN
SET @.X = '2';
			SET @.NEW_MODE = REPLACE(_MODE, 'WWW.2.0.Crazy.grid', 'WWW.2.0.grafiki');
			CALL hr_team_www(@.NEW_MODE, _GUID, _YYYY_MM, _OsobaGUID, CONCAT_WS('.', _Param_A, 'Crazy.grid'), _Param_B, _Param_C);
SET @.X = '3';
			
			-- CALL hr_team_www('WWW.2.0.init', '', '', '', '', '', '');
			UPDATE `t_teams_po_work_osoby` t, `t_hr_team_www_RANGE` r
			SET
				t.Is_Hidden = 
					IF(IFNULL(r.Funkcja_dla_Grafiku, '') IN ('Team->Kierownik', 'Team->Z-ca'), 
						FALSE, 
						r.log_USER_GUID <> t.OsobaGUID
					),
				t.Sum_Prac_mc_team_h = 
					(
					SELECT SUM(IF(he_A.GUIDTemplate = r.GUIDTemplate, he_A.PLAN_duration_min, 0))/60 FROM `hr_events_work_guids` he_A 
					WHERE he_A.OsobaGUID = t.OsobaGUID AND he_A.Deleted = 0
					)
			;
SET @.X = '4';
			UPDATE `t_teams_po_work_osoby` t
			SET
				t.Sum_Prac_mc_all_tems_h = 
					(
					SELECT SUM(he_A.PLAN_duration_min)/60 FROM `hr_events_work_guids` he_A 
					WHERE he_A.OsobaGUID = t.OsobaGUID AND he_A.Deleted = 0
					)
			;
		--	zanim konflikt absencyjny stworz tabele do legendy póki masz w `t_teams_po_work_conflict` konfliktu zespołów	
			DROP TEMPORARY TABLE IF EXISTS `t_GUIDStanowiska`;
			CREATE TEMPORARY TABLE `t_GUIDStanowiska` 
				SELECT
					_VarInt(0) AS ID_pefix,
					p.GUIDStanowiska
					
				FROM `t_teams_po_work` p
				WHERE
					TRUE
					-- AND p.GUIDStanowiska = '{6CDD5734-2865-49EB-89CA-0069832D2E86}'
					-- AND p.`GUIDNg` = 'fe404a51-7d7e-11ea-b53a-52540078c2f5*'
				 	AND IFNULL(p.OsobaGUID, '') = ''
				 GROUP BY p.GUIDStanowiska
			;
			SET @ID_pefix := 0;
			UPDATE `t_GUIDStanowiska` ts
			SET 
				ts.ID_pefix = @ID_pefix := @ID_pefix + 1000
			ORDER BY ts.GUIDStanowiska
			;
			DROP TEMPORARY TABLE IF EXISTS `t_hr_team_po_nieprzydz`;
			CREATE TEMPORARY TABLE `t_hr_team_po_nieprzydz` 
				SELECT
					p.GUID,
					ts.ID_pefix,
					DAY(p.DayWork) AS Rec_Day,
					_VarInt(1) AS i_Next,
					p.DayWork, p.GUIDStanowiska
				FROM `t_GUIDStanowiska` ts, `t_teams_po_work` p
				WHERE
					ts.GUIDStanowiska = p.GUIDStanowiska
					AND IFNULL(p.OsobaGUID, '') = ''
			;
			-- SELECT * FROM `t_hr_team_po_nieprzydz` ;
			SET @Prev_Day := NULL, @.i_Next := NULL, @Rec_Day := NULL, @Lp = 0;
			DROP TEMPORARY TABLE IF EXISTS `t_hr_team_po_nieprzydz_final`;
			CREATE TEMPORARY TABLE `t_hr_team_po_nieprzydz_final` 
				SELECT
					tp.GUID,
					tp.ID_pefix,
					(@.i_Next := 
						IF(IFNULL(@Prev_Day, 0) < tp.Rec_Day, 
							1, 	--	gdy zmienia sie dzien to resetuj licznik na 1
							IFNULL(@.i_Next, 1) + 1
						)
					) AS I_Next,
					(@Prev_Day := tp.Rec_Day) AS Rec_Day,
					@.i_Next + tp.ID_pefix AS New_ID_pefix
					-- tp.*
				FROM `t_hr_team_po_nieprzydz` tp
				WHERE
					TRUE
					ORDER BY tp.Rec_Day, tp.ID_pefix
			;
			
			-- SELECT * FROM `t_hr_team_po_nieprzydz_final` ;
			
			UPDATE `t_teams_po_work` p, `t_hr_team_po_nieprzydz_final` tp
			SET
				p.ID_pefix = tp.New_ID_pefix MOD 1000	--	tutaj juz bez x 1000 bo to odroznuian  stanowiska a nie kolerjnosc
			WHERE
				p.GUID = tp.GUID
			;
		--	Szachownica/macierz
		/*
			_Get_ID_xxxxx('', 38, 'COL:', p.GUIDStanowiska, IFNULL(NULLIF(p.OsobaGUID, ''), p.ID_pefix), '', '', '') AS COL_ID,
			_Get_ID_xxxxx('', 38, 'COL:', p.GUIDStanowiska, IFNULL(NULLIF(p.OsobaGUID, ''), p.ID_pefix), '', '', '') AS COL_ID,
		*/
			
		--	Legenda
			CALL hr_team_www('WWW.2.0.team_www_Legenda.PREPARE', '', '', '', '', '', '');

		END IF;
		
		
		IF _MODE = 'WWW.2.0.Crazy.grid.SELECT.COL_ROW' THEN
			SET @.h.na.mc = 10000;
			DROP TEMPORARY TABLE IF EXISTS `t_COL_ROW_pre`;
			CREATE TEMPORARY TABLE `t_COL_ROW_pre` (
			  `Sum_Prac_mc_team_h_STR` VARCHAR(250) NOT NULL DEFAULT '',
			  `Sum_Prac_mc_all_tems_h_STR` VARCHAR(250) NOT NULL DEFAULT '',
			  `Sum_Prac_mc_team_h` DOUBLE DEFAULT NULL,
			  `Sum_Prac_mc_all_tems_h` DOUBLE DEFAULT NULL,
			  `Ile_etatow_na_team` VARCHAR(50),
			  `CC` INTEGER NOT NULL DEFAULT '0'
			)
			;
			/*
			było
			SELECT
				IFNULL(_DisplayDurationFormat(SUM(tos.Sum_Prac_mc_team_h)), '') AS Sum_Prac_mc_team_h_STR,
				IFNULL(_DisplayDurationFormat(SUM(tos.Sum_Prac_mc_all_tems_h)), '') AS Sum_Prac_mc_all_tems_h_STR,
				SUM(tos.Sum_Prac_mc_team_h) AS Sum_Prac_mc_team_h,
				SUM(tos.Sum_Prac_mc_all_tems_h) AS Sum_Prac_mc_all_tems_h,
				IFNULL(SUM(tos.Sum_Prac_mc_team_h)/@.h.na.mc, '') AS Ile_etatow_na_team
				,COUNT(*) AS CC,
				IFNULL(tmc.duration_h, 0) AS duration_h,
				IFNULL(tmc.duration_h_2, 0) AS duration_h_2,
				IFNULL(tmc.Edit_Status, 'NEW') AS Edit_Status,
				_VarStr(
				CASE IFNULL(tmc.Edit_Status, 'NEW') 
					WHEN 'NEW' THEN 'Nowy'
					WHEN 'EDIT.ALL' THEN 'W trakcie edycji'
					WHEN 'EDIT.MASTER' THEN 'Edycja nadzór'
					WHEN 'CLOSED' THEN 'Zamkniety do edycji'
					ELSE 'Nowy'
				END 
				) AS Edit_Status_Str
			FROM `t_teams_po_work_osoby` tos
			INNER JOIN `t_hr_team_www_RANGE` r ON IFNULL(r.Funkcja_dla_Grafiku, '') IN ('Team->Kierownik', 'Team->Z-ca')
			LEFT JOIN `hr_team_mc` tmc ON tmc.GUIDTemplate = r.GUIDTemplate AND tmc.YYYY_MM = r.YYYY_MM
			WHERE
				TRUE
			-- GROUP BY p.GUIDStanowiska, p.OsobaGUID
			;			
			*/
			INSERT INTO `t_COL_ROW_pre` 
			SELECT
				IFNULL(_DisplayDurationFormat(SUM(tos.Sum_Prac_mc_team_h)), '') AS Sum_Prac_mc_team_h_STR,
				IFNULL(_DisplayDurationFormat(SUM(tos.Sum_Prac_mc_all_tems_h)), '') AS Sum_Prac_mc_all_tems_h_STR,
				SUM(tos.Sum_Prac_mc_team_h) AS Sum_Prac_mc_team_h,
				SUM(tos.Sum_Prac_mc_all_tems_h) AS Sum_Prac_mc_all_tems_h,
				_VarStr(IFNULL(SUM(tos.Sum_Prac_mc_team_h)/@.h.na.mc, '')) AS Ile_etatow_na_team
				,COUNT(*) AS CC
			FROM `t_teams_po_work_osoby` tos
			INNER JOIN `t_hr_team_www_RANGE` r ON IFNULL(r.Funkcja_dla_Grafiku, '') IN ('Team->Kierownik', 'Team->Z-ca')
			WHERE
				TRUE
			-- GROUP BY p.GUIDStanowiska, p.OsobaGUID
			;			
			SELECT
				t.*,
				IFNULL(tmc.duration_h, 0) AS duration_h,
				IFNULL(tmc.duration_h_2, 0) AS duration_h_2,
				(@.Edit_Status := IFNULL(NULLIF(tmc.Edit_Status, ''), 'NEW')) AS Edit_Status,
				-- test (@.Edit_Status := 'EDIT.MASTER') AS Edit_Status,
				@.Edit_Status.ReadOnly := 
				NOT(
					((@.Edit_Status IN ('EDIT.MASTER')) AND (IFNULL(r.Funkcja_dla_Grafiku, '') IN ('Team->Kierownik', 'Team->Z-ca')))
					OR
					(@.Edit_Status IN ('EDIT.ALL'))
				
				) AS ReadOnly,
				_VarStr(
				CONCAT_WS(' - ', 
					CASE @.Edit_Status 
						WHEN 'NEW' THEN 'Nowy'
						WHEN 'EDIT.ALL' THEN 'W trakcie edycji'
						WHEN 'EDIT.MASTER' THEN CONCAT_WS(' - ', 'Edycja nadzór', IF(IFNULL(r.Funkcja_dla_Grafiku, '') IN ('Team->Kierownik', 'Team->Z-ca'), 'możesz edytować', NULL))
						WHEN 'CLOSED' THEN 'Zamknięty do edycji'
						WHEN 'CLOSED+WYPLATA' THEN 'Zamknięty - wypłata'
						ELSE 'Nowy'
					END,
					IF(@.Edit_Status.ReadOnly, NULL, 'możesz edytować')
				)) AS Edit_Status_Str
			FROM `t_COL_ROW_pre`  t
			INNER JOIN `t_hr_team_www_RANGE` r
			LEFT JOIN `hr_team_mc` tmc ON tmc.GUIDTemplate = r.GUIDTemplate AND tmc.YYYY_MM = r.YYYY_MM
			;			
			
		END IF;
		
		IF _MODE = 'WWW.2.0.Crazy.grid.SELECT.COL' THEN
			SELECT
				-- było _Get_ID_xxxxx('', 38, 'COL:', p.GUIDStanowiska, p.OsobaGUID, '', '', '') AS COL_ID,
				_Get_ID_xxxxx('', 38, 'COL:', p.GUIDStanowiska, IFNULL(NULLIF(p.OsobaGUID, ''), p.ID_pefix), '', '', '') AS COL_ID,
				-- było s.Stanowisko,
				CONCAT_WS(' ', 
					s.Stanowisko,
					IF(p.ID_pefix > 1, CONCAT('(', p.ID_pefix, ')'), NULL)
				) AS Stanowisko,
				IFNULL(CONCAT(o.Nazwisko, ' ', o.Imiona), 'Nie przydzielono')AS NazwiskoImiona,
				IFNULL(pr.AnalitykaFK, '') AS Nr_Prac_Str,
				tos.Sum_Prac_mc_team_h,
				tos.Sum_Prac_mc_all_tems_h,
				IF(TRUE, -- ustalenia w mailu od AS z 20 II 2020 NOT tos.Is_Hidden, 
					-- CONCAT(ROUND(Sum_Prac_mc_team_h), ' h'), 
					_DisplayDurationFormat(tos.Sum_Prac_mc_team_h),
					'-'
				) AS Sum_Prac_mc_team_h_STR,
				IF(NOT tos.Is_Hidden, 
					-- CONCAT(ROUND(Sum_Prac_mc_all_tems_h), ' h'), 
					_DisplayDurationFormat(tos.Sum_Prac_mc_all_tems_h),
					'-'
				) AS Sum_Prac_mc_all_tems_h_STR,
				p.GUIDStanowiska, p.OsobaGUID
				,COUNT(*) AS CC
			FROM `t_teams_po_work` p
			LEFT JOIN `osoby` o ON o.GUID = p.OsobaGUID
			LEFT JOIN `Pracownicy` pr ON pr.GUID = o.GUID
			LEFT JOIN `stanowiska` s ON s.GUID = p.GUIDStanowiska
			LEFT JOIN `t_teams_po_work_osoby` tos ON tos.OsobaGUID = p.OsobaGUID
			INNER JOIN `t_hr_team_www_RANGE` r
			WHERE
				TRUE
				AND p.DayWork BETWEEN _var_YMToFirstDay(r.YYYY_MM) AND _var_YMToLastDay(r.YYYY_MM)
			-- było GROUP BY p.GUIDStanowiska, p.OsobaGUID
			GROUP BY p.GUIDStanowiska, IFNULL(NULLIF(p.OsobaGUID, ''), p.ID_pefix)
			ORDER BY 
				IF(r.log_USER_GUID = IFNULL(o.GUID, ''),
					'0',
					IFNULL(CONCAT('1', o.Nazwisko), '2')
					-- p.Lp
				),
				p.ID_pefix
			;
		
		END IF;
		
		IF _MODE = 'WWW.2.0.Crazy.grid.SELECT.ROW' THEN
			SELECT 
				cal.Date AS ROW_ID,
				cal.*,
				(@.Sum_Prac_day_team_h := ((SELECT SUM(p.PLAN_duration_min) FROM `t_teams_po_work` p WHERE p.DayWork = cal.Date)/60)) AS Sum_Prac_day_team_h,
				_DisplayDurationFormat(@.Sum_Prac_day_team_h) AS Sum_Prac_day_team_h_STR,
				IF(cal.GOV_Free_Day,
					3,
					IF(cal.DW = 7,
						2,
						IF(cal.DW = 6,
							1,
							0
						)
					)
				) AS day_type,
				cal.Working_Day_A AS Working_Day
			FROM `v_sys_calendar` cal
			INNER JOIN `t_hr_team_www_RANGE` r
			WHERE
				cal.Date BETWEEN _var_YMToFirstDay(r.YYYY_MM) AND _var_YMToLastDay(r.YYYY_MM)
			GROUP BY ROW_ID
			;
		END IF;
		
		IF _MODE = 'WWW.2.0.Crazy.grid.SELECT' THEN
			SELECT 
				p.GUID,
				p.DayWork AS ROW_ID,
				-- było _Get_ID_xxxxx('', 38, 'COL:', p.GUIDStanowiska, p.OsobaGUID, '', '', '') AS COL_ID,
				_Get_ID_xxxxx('', 38, 'COL:', p.GUIDStanowiska, IFNULL(NULLIF(p.OsobaGUID, ''), p.ID_pefix), '', '', '') AS COL_ID,
				p.Rec_Type,
				_VarStr(_Fmt_DateTime_QUICK(p.StartTime, 'DDD D MR')) AS DayWorkStr,
				-- _VarStr(_Day_Month_Year_Fmt(p.StartTime, 'DD MR')) AS DayWorkStr,
				_VarStr(CONCAT_WS(' /<br>', 
					NULLIF(_VarStr(p.Time_Range_Str), ''),
					(@.Event_PLAN_ZAST := 
					IF(zast.GUID IS NULL, NULL, _VarStr(CONCAT('Zastępstwo: ', IF(zast.Status = 0, 'niepotw.', IF(zast.Status = 3, 'zgoda', 'odrzucone')), ' - ', ozast.Nazwisko, ' ', ozast.Imiona)))
					),
					/* bylo
					_VarStr(CONCAT('Konfl.pracy!!!(', NULLIF(p.Is_Conflict_Team, 0), ')')),
					_VarStr(CONCAT('Konf.absenc.!!!(', NULLIF(p.Is_Conflict_Absencja, 0), ')')),
					*/
					IF(p.Is_Conflict_Absencja > 0,
						_VarStr(CONCAT('[Abs ', NULLIF(p.Conflict_Absencja_Inf_Short, ''), ']')),
						IF(p.Is_Conflict_Team > 0,
							_VarStr(CONCAT('[Grafik:', NULLIF(p.Conflict_Team_Inf_Short, ''), ']')),
							IF(NULLIF(p.Paralel_Abs_Inf_Short, '') IS NOT NULL,
								NULLIF(_VarStr(CONCAT('(Abs:', NULLIF(p.Paralel_Abs_Inf_Short, ''), ')')), ''),
								NULLIF(_VarStr(CONCAT('(', NULLIF(p.Paralel_Team_Inf_Short, ''), ')')), '')
							)
						)
					),
					NULL	
				)) AS Event_STR,
				_VarStr(CONCAT_WS(' /<br>', 
					CONCAT(o.Nazwisko, ' ', o.Imiona),
					_VarStr(p.Time_Range_Str),
					NULL	
				)) AS Event_Gantt_STR,
				_VarStr(CONCAT_WS(' /<br>', 
					_VarStr(p.Time_Range_Str),
					@.Event_PLAN_ZAST,
					_VarStr(CONCAT(IF(p.Rec_Type = 'EMPTY', '(', 'Konfl.pracy!!!('), NULLIF(IF(p.Is_Conflict_Team > 0, _VarStr(p.Conflict_Team_Inf), ''), ''), ')')),
					_VarStr(CONCAT('Absenc.!!!(', NULLIF(IF(p.Is_Conflict_Absencja > 0, _VarStr(p.Conflict_Absencja_Inf), ''), ''), ')')),
					NULL	
				)) AS Event_Hint_STR,
				CONCAT(TIME_FORMAT(TIME(p.StartTime), '%H:%i'), ' do ', TIME_FORMAT(TIME(p.EndTime), '%H:%i')) AS StartEndTime_STR,
				p.OsobaGUID,
				(NULLIF(p.OsobaGUID, '') IS NULL) AS brak_osoby,
				CONCAT(o.Nazwisko, ' ', o.Imiona) AS NazwiskoImiona,
				s.Stanowisko,
				IFNULL(
					CONCAT('Zastępstwo: ', IF(zast.Status = 0, 'niepotw.', IF(zast.Status = 3, 'zgoda', 'odrzucone')), ' - ', ozast.Nazwisko, ' ', ozast.Imiona),
					''
				) AS Zastepstwo_Str,
				(zast.GUID IS NOT NULL) AS Is_Zastepstwo,
				(p.Is_Conflict_Absencja + p.Is_Conflict_Team > 0) AS Is_Confict,
				p.Is_Conflict_Absencja,
				p.Is_Conflict_Team,
				IF(p.Is_Conflict_Absencja, p.Conflict_Absencja_Inf, '') AS Conflict_Absencja_Str,
				IF(p.Is_Conflict_Team, p.Conflict_Team_Inf, '') AS Conflict_Team_Str,
				_VarStr(p.StartTime) AS StartTime,
				_VarStr(p.EndTime) AS EndTime,
				IF(cal.GOV_Free_Day,
					3,
					IF(cal.DW = 7,
						2,
						IF(cal.DW = 6,
							1,
							0
						)
					)
				) AS day_type,
				cal.Working_Day_A AS Working_Day,
				p.Legenda_ID,
				leg.LP AS Legenda_LP
				
			FROM `t_teams_po_work` p
			LEFT JOIN `osoby` o ON o.GUID = p.OsobaGUID
			-- LEFT JOIN `hr_events` zast on zast.GUID = p.eventGUID_ZAST
			LEFT JOIN `hr_events` zast ON zast.GUID = _Get_ID_xxxxx('', 38, 'ZAST:', p.GUID, '', '', '', '')
			LEFT JOIN `osoby` ozast ON ozast.GUID = zast.OsobaGUID -- p.OsobaGUID_ZAST
			LEFT JOIN `stanowiska` s ON s.GUID = p.GUIDStanowiska
			LEFT JOIN `v_sys_calendar` cal ON cal.Date = p.DayWork
			LEFT JOIN `t_hr_team_www_Legenda` leg ON leg.ID = p.Legenda_ID
			INNER JOIN `t_hr_team_www_RANGE` r
			WHERE
				TRUE
				AND cal.Date BETWEEN _var_YMToFirstDay(r.YYYY_MM) AND _var_YMToLastDay(r.YYYY_MM)
			ORDER BY p.StartTime, p.Lp	
			;
			

		END IF;		
				
		IF _MODE = 'WWW.2.0.hr_team_www_RANGE.SELECT' THEN
			SELECT
				r.*,
				IFNULL(CONCAT(u.Nazwisko, ' ', u.Imiona), 'Nie ustalony') AS log_User,
				IFNULL(CONCAT(o.Nazwisko, ' ', o.Imiona), 'Nie ustalona')AS Osoba
			FROM `t_hr_team_www_RANGE` r
			LEFT JOIN `osoby` u ON u.GUID = r.log_USER_GUID
			LEFT JOIN `osoby` o ON o.GUID = r.OsobaGUID
			;
			
		END IF;		
				
	END IF;
	
	
END$$

DELIMITER ;

/*
$Log$
*/


/*
====================================================================================================================================================================================
	NOTATNIK
====================================================================================================================================================================================
 
*/
