 /* ****************************************************
REWIZJE
*******************************************************
$Log: _varTime.sql,v $
Revision 1.17  2026/01/16 10:06:47  jmam
stan na 16 I 2026

Revision 1.16  2025/02/14 15:12:33  jmam
no message

Revision 1.15  2023/01/18 14:11:42  jmam
$SEND$

Revision 1.14  2023/01/18 14:08:19  jmam
$SEND$

Revision 1.13  2022/11/08 08:37:12  jmam
$SEND$

Revision 1.12  2021/10/07 13:15:36  jmam
$SEND$

Revision 1.11  2021/06/21 14:41:46  jmam
$SEND$

Revision 1.10  2021/05/17 13:19:44  jmam
$SEND$

Revision 1.9  2017/03/29 13:15:31  jmam
$SEND$

Revision 1.8  2016/05/25 12:30:17  apro
charset

Revision 1.7  2016/05/25 11:32:17  apro
utf, delimiter

Revision 1.6  2016/05/24 09:22:57  apro
names, kosmetyka
$SEND$

*/


DELIMITER $$

DROP FUNCTION IF EXISTS `_RndTIME`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_RndTIME`(
	_MODE VARCHAR(150),
	_ValueSeconds INTEGER,	--	sekundy
	_Range INTEGER,
	_Param_A VARCHAR(150),
	_Param_B VARCHAR(150)
	) RETURNS INTEGER
    NO SQL
    DETERMINISTIC
BEGIN
	DECLARE _Bool BOOLEAN;
	DECLARE _Minutes INTEGER DEFAULT 0;
	DECLARE _Seconds INTEGER DEFAULT 0;

	-- 	Minuty
	IF _MODE = 'MIN' THEN
		IF _ValueSeconds > 0 THEN
		--	UWAGA aby 15 zaokr. do 0  a nie 30 musimy odejmowac 1 seknde
			SET _ValueSeconds = _ValueSeconds - 1; 
		END IF;	
		SET @_ValuePartHour = _ValueSeconds div 3600;
		SET @_ValueSeconds = _ValueSeconds MOD 3600;
		SET @_ValueSeconds = CONCAT('2000-01-01 ', SEC_TO_TIME(@_ValueSeconds));
		SET @_C_MIN = IF(_Range = 0, 15, _Range);
		SET @_C_MIN = @_C_MIN*60;
		
		SET _Minutes = ((TIME_TO_SEC(DATE_ADD(@_ValueSeconds, INTERVAL @_C_MIN DIV 2 SECOND)) DIV (@_C_MIN))*@_C_MIN) DIV 60;
		SET _Minutes = IFNULL(_Minutes, 0) + @_ValuePartHour*60;
		RETURN _Minutes;
	END IF;
END$$

DROP FUNCTION IF EXISTS `_RndDATETIME`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_RndDATETIME`(
	_MODE VARCHAR(150),
	_Value DATETIME
	) RETURNS DATETIME
    NO SQL
    DETERMINISTIC
BEGIN
	-- 	Minuty
	IF _MODE = 'MIN TRUNC' THEN
		SET @_RndDATETIME = SUBSTR(_Value, 1, 16);
		SET _Value = @_RndDATETIME;
		RETURN _Value;
	END IF;
	
	IF _MODE = 'MIN' THEN
		SET @_RndDATETIME_Is_Up = (SECOND(_Value) >= 30);
		SET _Value = TIMESTAMP(DATE(_Value), MAKETIME(HOUR(_Value), MINUTE(_Value), 0));
		IF @_RndDATETIME_Is_Up THEN
			SET _Value = DATE_ADD(_Value, INTERVAL 1 MINUTE);
		END IF;		
		RETURN _Value;
	END IF;
	
	-- 	Godz
	IF _MODE = 'HOUR TRUNC' THEN
		SET @_RndDATETIME = SUBSTR(_Value, 1, 13);
		SET _Value = @_RndDATETIME;
		RETURN _Value;
	END IF;
	
	IF _MODE = 'MIN' THEN   --  stare nie wykona się? Ale i ak nigdzie nie wołamy
		SET @_RndDATETIME_Is_Up = (MINUTE(_Value) >= 30);
		SET _Value = TIMESTAMP(DATE(_Value), MAKETIME(HOUR(_Value), 0, 0));
		IF @_RndDATETIME_Is_Up THEN
			SET _Value = DATE_ADD(_Value, INTERVAL 1 HOUR);
		END IF;		
		RETURN _Value;
	END IF;
	
END$$

DROP FUNCTION IF EXISTS `_TimeRange`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_TimeRange`(
	_MODE VARCHAR(150),
	_A_Start DATETIME,
	_A_End DATETIME,
	_B_Start DATETIME,
	_B_End DATETIME,
	_C VARCHAR(150),
	_D VARCHAR(150)
) RETURNS INTEGER
    NO SQL
    DETERMINISTIC
BEGIN

	DECLARE _Bool BOOLEAN;
	DECLARE _Minutes INTEGER DEFAULT 0;
	DECLARE _Seconds INTEGER DEFAULT 0;

/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'conflict'
		Zwraca 1 gdy jest konflikt czyli zakresy czasowe maja czesc wspolna
		Parametry
		SELECT _TimeRange('conflict', '2011-06-21 08:45', '2011-06-21 11:00',	'2011-06-21 09:45', '2011-06-21 12:00', '', '');
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
	IF _MODE = 'conflict' THEN
		SET _Bool =
				(_A_Start BETWEEN _B_Start AND _B_End) 
				OR 
				(_A_End BETWEEN _B_Start AND _B_End)
				OR 
				(_B_Start BETWEEN _A_Start AND _A_End) 
				OR 
				(_B_End BETWEEN _A_Start AND _A_End) 
		;		
		RETURN _Bool;
	END IF;	--	IF _MODE = 'conflict'
	
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'Czas.Diff.Minuty'
		Zwraca czas w minutach miêdzy _A_Start i _A_End
		Parametry
		SELECT _TimeRange('Czas.Diff.Minuty', '2011-06-21 08:45', '2011-06-21 11:00',	'', '', '', '');
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
	IF _MODE = 'Czas.Diff.Minuty' THEN
		SET _Minutes = TIMESTAMPDIFF(MINUTE, _A_Start, _A_End);
		RETURN _Minutes;
	END IF;	--	IF _MODE = 'Czas.Diff.Minuty'
	
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'Czas.Pracy.A'
		Zwraca czas w minutach miêdzy _A_Start i _A_End
		Parametry
		SELECT _TimeRange('Czas.Pracy.A', '2011-06-21 08:45', '2011-06-21 11:00',	'', '', '', '');
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
	IF _MODE = 'Czas.Pracy.A' THEN
		SET _Minutes = TIMESTAMPDIFF(MINUTE, _A_Start, _A_End);
		RETURN IFNULL(_Minutes, 0);
	END IF;	--	IF _MODE = 'Czas.Pracy.A'
	
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'Czas.Pracy.B'
		Zwraca czas w minutach miêdzy _A_Start i _A_End ale czasy sa kwadransowo sciagane" w dol od 0 do 7,5 min i w góre od 7,5 do 15)
		Parametry
		SELECT _TimeRange('Czas.Pracy.B', '2011-06-21 08:45', '2011-06-21 11:00',	'', '', '', '');
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
	IF _MODE = 'Czas.Pracy.B' THEN
		SET @_A_Start = SEC_TO_TIME((TIME_TO_SEC(DATE_ADD(_A_Start,INTERVAL 450 SECOND)) DIV (900))*900);
		SET @_A_End = SEC_TO_TIME((TIME_TO_SEC(DATE_ADD(_A_End,INTERVAL 450 SECOND)) DIV (900))*900);
		SET @_A_Start = CONCAT('2000-01-01 ', @_A_Start);
		SET @_A_End = CONCAT('2000-01-01 ', @_A_End);
		SET _Minutes = TIMESTAMPDIFF(MINUTE, @_A_Start, @_A_End);
		RETURN IFNULL(_Minutes, 0);
	END IF;	--	IF _MODE = 'Czas.Pracy.B'
	
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'Czas.Pracy.C'
		Zwraca czas w minutach miedzy _A_Start i _A_End a wynik zaokraglany "kwadransowo" w dol od 0 do 7,5 min i w gore od 7,5 do 15)
		Parametry
		SELECT _TimeRange('Czas.Pracy.C', '2011-06-21 08:45', '2011-06-21 11:00',	'', '', '', '');
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
	IF _MODE = 'Czas.Pracy.C' THEN
		SET _Seconds = TIMESTAMPDIFF(SECOND, _A_Start, _A_End);
		SET @_Seconds = CONCAT('2000-01-01 ', SEC_TO_TIME(_Seconds));
		SET _Minutes = ((TIME_TO_SEC(DATE_ADD(@_Seconds, INTERVAL 450 SECOND)) DIV (900))*900) DIV 60;
		SET _Minutes = IFNULL(_Minutes, 0);
		RETURN _Minutes;
	END IF;	--	IF _MODE = 'Czas.Pracy.C'
	
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'Czas.Pracy.X'
		TO SAMO CO 'Czas.Pracy.C' dla 15 minut
		Zwraca czas w minutach miedzy _A_Start i _A_End a wynik zaokraglany "kwadransowo" w dol od 0 do 7,5 min i w gore od 7,5 do 15)
		Parametry
		_C  co ile minut (30 15 5)
		SELECT _TimeRange('Czas.Pracy.X', '2011-06-21 08:45', '2011-06-21 11:00',	'', '', '15', '');
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
	IF _MODE = 'Czas.Pracy.X' THEN
		SET _Seconds = TIMESTAMPDIFF(SECOND, _A_Start, _A_End);
		SET @_Seconds = CONCAT('2000-01-01 ', SEC_TO_TIME(_Seconds));
		SET @_C_MIN = IF(_C = '', 15, _C);
		SET @_C_MIN = @_C_MIN*60;
		
		SET _Minutes = ((TIME_TO_SEC(DATE_ADD(@_Seconds, INTERVAL @_C_MIN DIV 2 SECOND)) DIV (@_C_MIN))*@_C_MIN) DIV 60;
		SET _Minutes = IFNULL(_Minutes, 0);
		RETURN _Minutes;
	END IF;	--	IF _MODE = 'Czas.Pracy.X'
	
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'Czas.czesc.wspolna.min'
		czesc wspolan zakresu _A_Start _A_End oraz zakresu _B_Start _B_End i z tego minuty
		Gdy zakresy sie nie pokrywaja to 0
		SELECT _TimeRange('Czas.czesc.wspolna.min', '2011-06-21 08:45', '2011-06-21 11:00',	'', '', '15', '');
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
	IF _MODE = 'Czas.czesc.wspolna.min' THEN
		SET 
			_Minutes = 0,
			@Czas.czesc.wspolna_Start = (GREATEST(_A_Start, _B_Start)),
			@Czas.czesc.wspolna_End = (LEAST(_A_End, _B_End))
		;
		IF @Czas.czesc.wspolna_Start < @Czas.czesc.wspolna_End THEN
			SET _Minutes = TIMESTAMPDIFF(MINUTE, @Czas.czesc.wspolna_Start, @Czas.czesc.wspolna_End);
		END IF;	
		RETURN _Minutes;
	END IF;	--	IF _MODE = 'Czas.czesc.wspolna.min'
	
END$$

DROP FUNCTION IF EXISTS `_TimeRangeNight`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_TimeRangeNight`(
	_MODE VARCHAR(150),
	_A_Start DATETIME,
	_A_End DATETIME
) RETURNS INTEGER
    NO SQL
    DETERMINISTIC
BEGIN

	DECLARE _Bool BOOLEAN;
	DECLARE _Minutes INTEGER DEFAULT 0;
	DECLARE _Seconds INTEGER DEFAULT 0;

	
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'get.min.night'
		czesc wspolan zakresu _A_Start _A_End oraz zakresu _B_Start _B_End i z tego minuty
		Gdy zakresy sie nie pokrywaja to 0
		SELECT _TimeRangeNight('get.min.night', '2012-02-21 22:00', '2012-02-22 11:00');
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
	IF _MODE = 'get.min.night' THEN
		SET 
			_Minutes = 0,
			@_t._Day = DATE(_A_Start),
			@_t._DayNext = DATE_ADD(DATE(_A_Start), INTERVAL 1 DAY)
		;	
		SET
			@_t._D0000 = _varDateToDateTime(@_t._Day, '00:00:00'),
			@_t._D2200 = _varDateToDateTime(@_t._Day, '22:00:00'),
			@_t._D0600 = _varDateToDateTime(@_t._Day, '06:00:00'),
			@_t._ND0600 = _varDateToDateTime(@_t._DayNext, '06:00:00'),
			@_t._ND2200 = _varDateToDateTime(@_t._DayNext, '22:00:00'),
			@_t._ND2359 = _varDateToDateTime(@_t._DayNext, '23:59:59')
		;	
		
		SET
			_Minutes = 
				_TimeRange('Czas.czesc.wspolna.min', _A_Start, _A_End, @_t._D0000, @_t._D0600, '', '')
		;	
		SET
			_Minutes = _Minutes +
				_TimeRange('Czas.czesc.wspolna.min', _A_Start, _A_End, @_t._D2200, @_t._ND0600, '', '')
		;	
		SET
			_Minutes = _Minutes +
				_TimeRange('Czas.czesc.wspolna.min', _A_Start, _A_End, @_t._ND2200, @_t._ND2359, '', '')
		;	
		RETURN _Minutes;
	END IF;	--	IF _MODE = 'get.min.night'
	
END$$

DROP FUNCTION IF EXISTS `_pl_Min2Godz`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_pl_Min2Godz`(_Value DOUBLE) RETURNS varchar(256)
    NO SQL
    DETERMINISTIC
BEGIN
	SET	_Value = ROUND(_Value, 0);
	RETURN CONCAT(IF(length(_Value div 60)=1,'0',''),_Value div 60, ':', IF(length(floor(_Value) mod 60)=1,'0',''),floor(_Value) mod 60);
END$$

DROP FUNCTION IF EXISTS `_pl_Min2Godz_Full`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_pl_Min2Godz_Full`(_Value DOUBLE) RETURNS varchar(256)
    NO SQL
    DETERMINISTIC
BEGIN
	SET	_Value = ROUND(_Value, 0);
	RETURN CONCAT(IF(length(_Value div 60)=1,'0',''),_Value div 60, ' h ', IF(length(floor(_Value) mod 60)=1,'0',''),floor(_Value) mod 60,' min');
END$$

DROP FUNCTION IF EXISTS `_DisplayDurationFormat`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_DisplayDurationFormat`(_Value DOUBLE) RETURNS VARCHAR(256)
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN  CONCAT(FLOOR(_Value),'h ',IF((ROUND(((_Value-FLOOR(_Value))*30)/0.5))>0 ,CONCAT(ROUND(((_Value-FLOOR(_Value))*30)/0.5),'min'),''));

END$$

DROP FUNCTION IF EXISTS `_DisplayDurationFormatPL`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_DisplayDurationFormatPL`(_Value DOUBLE) RETURNS VARCHAR(256)
    NO SQL
    DETERMINISTIC
BEGIN
    DECLARE _Min VARCHAR(2);
	SET _Min = IF((ROUND(((_Value-FLOOR(_Value))*30)/0.5))>0 ,CONCAT(ROUND(((_Value-FLOOR(_Value))*30)/0.5),''),'0');
	
	RETURN  CONCAT(FLOOR(_Value),'.',CONCAT(IF(LENGTH(_Min)=1,'0',''),_Min));
END$$

DROP FUNCTION IF EXISTS `_DurationH`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_DurationH`(
	_MODE VARCHAR(255),	--	'D/H/M/S'	'H/M'	'H/M/S'	'H' 	'M' 	'S'
	_Value_H DOUBLE,		--	czas w h
	_EmptyReturn VARCHAR(20)		--	czas w h
) RETURNS VARCHAR(256)
    NO SQL
    DETERMINISTIC
S_EXIT: BEGIN
/*
	_DurationH('#H #M', 234.5);
	Nie bawimy się we wszystkie kombinacje - ma byc szybko!!!
	Mozliwe rzeczywiste mody
		#D#H#
		#H#M#S
		#H#M
		#M#S
*/
	SET @.Result = NULL;
	IF NULLIF(_Value_H, 0) IS NULL THEN
		RETURN _EmptyReturn;	
		LEAVE S_EXIT;
	END IF;
	
	IF _MODE LIKE 'D%H%' THEN
		SET 
			@.T.d = (@.T.h := ROUND(_Value_H)) DIV 24,
			@.T.h = @.T.h MOD 24
		;
		SET 
			@.Result =
				CONCAT_WS(' ',
					CONCAT(
						NULLIF(@.T.d, 0),
						IF(LOCATE('Dd' , _MODE) > 0, ' dn.', ' d')
					),
					CONCAT(
						NULLIF(@.T.h, 0),
						IF(LOCATE('Hh' , _MODE) > 0, ' godz.', ' h')
					),
					NULL
				)
		;
		/*
		SET 
			@.Result =
				CONCAT_WS(' ',
					IF(@.T.d = 0,
						NULL,
						CONCAT(
							@.T.d,
							IF(LOCATE('Dd' , _MODE) > 0, ' dn.', ' d')
						)
					),
					IF(@.T.h = 0,
						NULL,
						CONCAT(
							@.T.h,
							IF(LOCATE('Hh' , _MODE) > 0, ' godz.', ' h')
						)
					),
					NULL
				)
		;
		*/
		RETURN IFNULL(NULLIF(@.Result, ''), _EmptyReturn);	
	END IF;
	
	IF _MODE LIKE 'H%M%S%' THEN	-- 13h 25 min 34 sec
		SET
				@.T.s := (@.T.sec := ROUND(_Value_H*3600)) MOD 60,
				@.T.m := (@.T.min := (@.T.sec DIV 60)) MOD 60,
				@.T.h := (@.T.min DIV 60)
		;
		SET 
			@.Result =
				CONCAT_WS(' ',
					CONCAT(
						NULLIF(@.T.h, 0),
						IF(LOCATE('Hh' , _MODE) > 0, ' godz.', ' h')
					),
					CONCAT(
						NULLIF(@.T.m, 0),
						IF(LOCATE('Mm' , _MODE) > 0, ' min', ' m')
					),
					CONCAT(
						NULLIF(@.T.s, 0),
						IF(LOCATE('Ss' , _MODE) > 0, ' sec', ' s')
					),
					NULL
				)
		;
		RETURN IFNULL(NULLIF(@.Result, ''), _EmptyReturn);	
	END IF;
	
	IF _MODE LIKE 'H%Kxxx%' THEN	-- 13h 25 min
		SET
				@.T.m := (@.T.min := ROUND(_Value_H*60)) MOD 60,
				@.T.h := (@.T.min DIV 60)
		;
		SET 
			@.Result =
				CONCAT_WS(' ',
					IF(@.T.h = 0,
						NULL,
						CONCAT(
							@.T.h,
							IF(LOCATE('Hh' , _MODE) > 0, ' godz.', ' h')
						)
					),
					IF(@.T.m = 0,
						NULL,
						CONCAT(
							@.T.m,
							IF(LOCATE('Mm' , _MODE) > 0, ' min', ' m')
						)
					),
					NULL
				)
		;
		RETURN IFNULL(NULLIF(@.Result, ''), _EmptyReturn);	
	END IF;
	
	IF _MODE LIKE 'H%M%' THEN	-- 13h 25 min
		SET
				@.T.m := (@.T.min := ROUND(_Value_H*60)) MOD 60,
				@.T.h := (@.T.min DIV 60)
		;
		SET 
			@.Result =
				CONCAT_WS(' ',
					CONCAT(
						NULLIF(@.T.h, 0),
						IF(LOCATE('Hh' , _MODE) > 0, ' godz.', ' h')
					),
					CONCAT(
						NULLIF(@.T.m, 0),
						IF(LOCATE('Mm' , _MODE) > 0, ' min', ' m')
					),
					NULL
				)
		;
		RETURN IFNULL(NULLIF(@.Result, ''), _EmptyReturn);	
	END IF;
	
	IF _MODE LIKE 'M%S%' THEN	-- 1225 min 34 sec
		SET
				@.T.s := (@.T.sec := ROUND(_Value_H*3600)) MOD 60,
				@.T.m := (@.T.min := (@.T.sec DIV 60))
		;
		SET 
			@.Result =
				CONCAT_WS(' ',
					CONCAT(
						NULLIF(@.T.m, 0),
						IF(LOCATE('Mm' , _MODE) > 0, ' min', ' m')
					),
					CONCAT(
						NULLIF(@.T.s, 0),
						IF(LOCATE('Ss' , _MODE) > 0, ' sec', ' s')
					),
					NULL
				)
		;
		/*
				CONCAT_WS(' ',
					IF(@.T.m = 0,
						NULL,
						CONCAT(
							@.T.m,
							IF(LOCATE('Mm' , _MODE) > 0, ' min', ' m')
						)
					),
					IF(@.T.s = 0,
						NULL,
						CONCAT(
							@.T.s,
							IF(LOCATE('Ss' , _MODE) > 0, ' sec', ' s')
						)
					),
					NULL
				)
		;
		*/
		RETURN IFNULL(NULLIF(@.Result, ''), _EmptyReturn);	
	END IF;
	
	
END$$

DROP FUNCTION IF EXISTS `_DisplayDurationH`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_DisplayDurationH`(
	_MODE VARCHAR(255),	--	'D/H/M/S'	'H/M'	'H/M/S'	'H' 	'M' 	'S'
	_Value_H DOUBLE,		--	czas w H
	_PrzedTxt VARCHAR(50),
	_PoTxt VARCHAR(50)
) RETURNS VARCHAR(256)
    NO SQL
    DETERMINISTIC
S_EXIT: BEGIN
    DECLARE _Min VARCHAR(2);
	IF _Value_H IS NULL THEN
		RETURN NULL;
		LEAVE S_EXIT;
	END IF;
	SET 
		@.Przed = _Value_H > 0, 
	--	Dalej godziny nie ujemne
		@.H := ABS(_Value_H), 
	--	Godziny obciete to 24h	
		@.H.24 := @.H MOD 24,
	--	Godziny do naliczanie typu TIME - 27:15:10 dla H... i 3:15:10 dla D/H...
		@.H.Calc := IF(_MODE LIKE '%/H%', @.H.24, @.H)
	;
	IF _MODE LIKE '%/H%' THEN
	--	Godziny obciete to 24h
		SET 
			@.H = @.H -- @.H.24
		;
	END IF;
	SET 
		@.T.in.H = SEC_TO_TIME(ROUND(@.H.Calc)*3600),
		@.T.in.M = SEC_TO_TIME(ROUND(@.H.Calc*60)*60),
		@.T.in.S = SEC_TO_TIME(ROUND(@.H.Calc*3600)),
		@.f.D = NULL,
		@.f.H = NULL,
		@.f.M = NULL,
		@.f.S = NULL,
		@.f.T = SEC_TO_TIME(ROUND(@.H*3600))
	;
	SET 
		@.f.D = IF(_MODE LIKE '%D%', @.H DIV 24, NULL),
		@.f.H = 
		IF(_MODE = 'H', 
			@.H, 
			NULLIF(			--	ale jesli całośc ponizej da 0 to zwroc NULL
				IF(_MODE LIKE '%H%S%', 
					HOUR(@.T.in.S), 
					IF(_MODE LIKE '%H%M%', 
						HOUR(@.T.in.M), 
						IF(_MODE LIKE '%H%', 
							HOUR(@.T.in.H), 
							NULL
				)))
			, 0)
		),
		@.f.M = 
		IF(_MODE = 'M', 
			FLOOR(ROUND(@.H *60)),
			IF(_MODE LIKE 'M%', -- zaczynamy od minut wtedy nie ma co brac minut z czasu bo te sa zawsze obciete do 60
				FLOOR((@.H *60)),
				IF(_MODE LIKE '%M%S%', 
					MINUTE(@.T.in.S), 
					IF(_MODE LIKE '%M%', 
						MINUTE(@.T.in.M), 
						NULL
		)))),
		@.f.S = 
		IF(_MODE = 'S', 
			FLOOR(@.H *3600), 
				IF(_MODE LIKE '%S%', 
					SECOND(@.T.in.S), 
					NULL
		)),
		@.f.T = IF(_MODE = 'T', @.f.T, NULL)
	;
	
	SET @.Result = CONCAT_WS(' ',
		IF(@.Przed, NULLIF(_VarStr(_PrzedTxt), ''), NULLIF(_VarStr(_PoTxt), '')),
		_ConcatPre(NULLIF(@.f.D, 0), ' dn'),
		-- _ConcatPre(NULLIF(@.f.H, 0), ' h'),
		_ConcatPre(@.f.H, ' h'),
		_ConcatPre(NULLIF(@.f.M, 0), ' min'),
		_ConcatPre(NULLIF(@.f.S, 0), ' s'),
		_VarStr(@.f.T)
	);
	
	-- RETURN  CONCAT(FLOOR(_Value),'h ',IF((ROUND(((_Value-FLOOR(_Value))*30)/0.5))>0 ,CONCAT(ROUND(((_Value-FLOOR(_Value))*30)/0.5),'min'),''));
	RETURN @.Result;
END$$

DROP FUNCTION IF EXISTS `_Time_From`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Time_From`(
	_From VARCHAR(150),
	_Value DOUBLE
) RETURNS TIME
    NO SQL
    DETERMINISTIC
BEGIN
	IF _FROM = 'SECOND' THEN
		RETURN  MAKETIME((_Value*1 DIV 3600), (_Value*1 DIV 60) MOD 60, ROUND((_Value*1) MOD 60));
	END IF;

	IF _FROM = 'MINUTE' THEN
		RETURN  MAKETIME((_Value*60 DIV 3600), (_Value*60 DIV 60) MOD 60, (_Value*60) MOD 60);
	END IF;

	IF _FROM = 'HOUR' THEN
		RETURN  MAKETIME((_Value*3600 DIV 3600), (_Value*3600 DIV 60) MOD 60, ROUND((_Value*3600) MOD 60));
	END IF;

END$$

DROP FUNCTION IF EXISTS `_Time_From_AS_DateTime`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Time_From_AS_DateTime`(
	_From VARCHAR(150),
	_Value DOUBLE
) RETURNS DATETIME
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN _varDateToDateTime('2000-01-00', _Time_From(_From, _Value));
END$$

DROP FUNCTION IF EXISTS `_Time_From_AS_TimeStamp`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Time_From_AS_TimeStamp`(
	_From VARCHAR(150),
	_Value DOUBLE
) RETURNS TIMESTAMP
    NO SQL
    DETERMINISTIC
BEGIN
    DECLARE _TS DATETIME;
	DECLARE _T TIME;
	-- RETURN TIMESTAMP('1001-01-00', _Time_From(_From, _Value));
	SET _T = _Time_From(_From, _Value);
	SET _TS = _varDateToDateTime('0001-01-00', _Time_From(_From, _Value));
	-- SET _TS = DATETIME(_T);
	SET
		@_T = _T, @_TS = _TS
	;
	RETURN TIMESTAMP(DATE(_TS), TIME(_TS));
END$$

DROP FUNCTION IF EXISTS `_PL_WeWy_Night`$$	
CREATE DEFINER=`root`@`localhost` FUNCTION `_PL_WeWy_Night`(
	_MODE VARCHAR(150),
	_A_Start DATETIME,	--	UWAGA NULL obsługuję i wychodzę z zerem ALE "" NIE!!! "" jest jako ... 00:00:00 
	_A_End DATETIME
) RETURNS INTEGER
    NO SQL
    DETERMINISTIC
BEGIN
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'get.min.night'
		_A_Start	jest wejsciem
		_A_End		jest wyjsciem z pracy
		czesc wspolan zakresu _A_Start _A_End oraz zakresu _B_Start _B_End i z tego minuty
		Gdy zakresy sie nie pokrywaja to 0
		SELECT _PL_WeWy_Night('get.min.night', '2012-02-21 22:00', '2012-02-22 11:00');
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
	-- IF _MODE IN ('', 'get.min.night') THEN
		SET
			@._Minutes := 0			
			-- , @._A_Start = _A_Start
		;
                                set @._A_Start := _A_Start, @._A_End := _A_End;
                                set @.jmam = 'jmam1';
		-- IF IFNULL(_A_Start, '') <> '' THEN
		IF _A_Start IS NOT NULL THEN
                                set @.jmam = 'jmam2';
			SET
				@._Minutes := 0,
				@_A_Start := ADDTIME(@.n.DT, TIME(_A_Start)),
				@_A_End := ADDTIME(@.n.DT, TIME(_A_End)),
				@_A_End := IF(@_A_End < @_A_Start, @_A_End + INTERVAL 1 DAY, @_A_End)
			;
					-- set @.night.dt = concat_WS(' - ', _A_Start, _A_End);
			SET	
				@._Minutes = IFNULL(
					GREATEST(0, TIMESTAMPDIFF(MINUTE, GREATEST(@_A_Start, @.n.D_00_00), LEAST(@_A_End, @.n.D_06_00))) +
					GREATEST(0, TIMESTAMPDIFF(MINUTE, GREATEST(@_A_Start, @.n.D_22_00), LEAST(@_A_End, @.n.Dn_06_00))) +
					GREATEST(0, TIMESTAMPDIFF(MINUTE, GREATEST(@_A_Start, @.n.Dn_22_00), LEAST(@_A_End, @.n.Dn_23_59))),
					0
				)
				;
		END IF;
		RETURN @._Minutes;
	-- END IF;
END$$

DROP FUNCTION IF EXISTS `_PL_WeWy_NightOLD`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_PL_WeWy_NightOLD`(
	_MODE VARCHAR(150),
	_A_Start DATETIME,
	_A_End DATETIME
) RETURNS INTEGER
    NO SQL
    DETERMINISTIC
BEGIN
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'get.min.night'
		_A_Start	jest wejsciem
		_A_End		jest wyjsciem z pracy
		czesc wspolan zakresu _A_Start _A_End oraz zakresu _B_Start _B_End i z tego minuty
		Gdy zakresy sie nie pokrywaja to 0
		SELECT _PL_WeWy_Night('get.min.night', '2012-02-21 22:00', '2012-02-22 11:00');
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
	IF _MODE IN ('', 'get.min.night') THEN
		SET @.Start.Date = ADDTIME(DATE(_A_Start), '00:00');
		SET 
			@.D_00_00 := ADDTIME(@.Start.Date, '00:00'),	-- + 0h - dany dzień 00:00
			@.D_06_00 := ADDTIME(@.Start.Date, '06:00'),	-- + 6h - dany dzień 06:00
			@.D_22_00 := ADDTIME(@.Start.Date, '22:00'),	-- +22h - dany dzień 22:00 - relatywne jutro od dziś 22:00 do...  
			@.Dn_06_00 := ADDTIME(@.Start.Date, '30:00'),	-- +30h - nast. dzień 06:00 - relatywne jutro do 06:00 do...  
			@.Dn_22_00 := ADDTIME(@.Start.Date, '46:00'),	-- +46h - nast. dzień 22:00 - relatywne jutro do 22:00 do...  
			@.Dn_23_59 := ADDTIME(@.Start.Date, '47:59')	-- +30h - nast. dzień 23_59 - relatywne jutro do 23_59 do...  
		;
		SET	
			@._Minutes =
				GREATEST(0, TIMESTAMPDIFF(MINUTE, GREATEST(_A_Start, @.D_00_00), LEAST(_A_End, @.D_06_00))) +
				GREATEST(0, TIMESTAMPDIFF(MINUTE, GREATEST(_A_Start, @.D_22_00), LEAST(_A_End, @.Dn_06_00))) +
				GREATEST(0, TIMESTAMPDIFF(MINUTE, GREATEST(_A_Start, @.Dn_22_00), LEAST(_A_End, @.Dn_23_59)))
			;
		RETURN @._Minutes;
	END IF;
END$$

DROP FUNCTION IF EXISTS `_PL_OdDo`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_PL_OdDo`(
	_MODE VARCHAR(150),
	_Param_A VARCHAR(150),
	_Param_B VARCHAR(150),
	_Param_C VARCHAR(150),
	_Param_D VARCHAR(150),
	_Param_E VARCHAR(150)
) RETURNS VARCHAR(150)
    NO SQL
    DETERMINISTIC
BEGIN
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'Zmien_Na_od'
		_A_Start	jest wejsciem
		_A_End		jest wyjsciem z pracy
		czesc wspolan zakresu _A_Start _A_End oraz zakresu _B_Start _B_End i z tego minuty
		Gdy zakresy sie nie pokrywaja to 0
		SELECT _PL_OdDo('Zmien_Na_od', '2012-02-21 22:00', '2012-02-21 22:00', '2012-02-22 11:00', '', '');
------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	IF _MODE IN ('Zmien_Na_od') THEN
		IF _A_We BETWEEN _A_Start AND _A_End THEN
			SET 
				@.PL_OdDo.Start = _A_We,
				@.PL_OdDo.End = _A_End
			;
		ELSEIF _A_We < _A_Start THEN
			SET 
				@.PL_OdDo.Start = _A_Start,
				@.PL_OdDo.End = _A_End
			;
		ELSE
			SET 
				@.PL_OdDo.Start = NULL,
				@.PL_OdDo.End = NULL
			;
		END IF;	
			
		SET 
			@.e.we_wy_list = 
				CONCAT(TIME_FORMAT(@.PL_OdDo.Start, @.e.format_H), ' - ', TIME_FORMAT(@.PL_OdDo.End, @.e.format_H))
		;
		RETURN @.e.we_wy_list;
	END IF;
*/	

	IF _MODE LIKE '@->%Spec.od.do.ponad.NCP%' THEN
	/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
		mając w _Param_A  od do od do ... i datę konca pracy NCP zbieramy serie od do ale tylko starszą od NcP
		w _Param_B pula godzin wstępnyc
		SELECT _PL_OdDo('Zmien_Na_od', '2012-02-21 22:00', '', '', 'od:do', 'minuty');
	------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
		-- DO _PL_WeWy('_Params->@;Calc.tP;Calc.tP_Noc;->tP', _A_We, _Param_A, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
		SET 
			@.DatePocz = _Param_A,
			@.czas.Pula = _Param_B,
			-- @.czas.p.log = NULL,
			@.e.stop = FALSE
		;
	--	UWAGA w zmiennych @.e.dt, @.e.dt2, @.e.dt3, ewn. @.e.dt4 są czasy tych przedziałów	
		SET
					/*
					@.czas.p.log := CONCAT_WS('', @.czas.p.log, '/0: ', CONCAT_WS(',',
						CONCAT('P=', @.czas.Pula), CONCAT('dt=', @.e.dt))),
					*/
		--	1. przedział badam czy @.e.dt miesci pulę
			@.e.we := 
				IF(@.e.stop := (@.e.dt > (@.czas.Pula := (@.czas.Pula - 0))),
					@.e.we := @.e.we  + INTERVAL @.czas.Pula MINUTE,  -- Nowe Od jako stare Od + pula czasowa
					NULL
				),
			@.e.wy := IF(@.e.stop, @.e.wy, NULL),
					/*
					@.czas.p.log := CONCAT_WS('', @.czas.p.log, '/1: ', CONCAT_WS(',', 
						CONCAT('P=', @.czas.Pula), CONCAT('dt=', @.e.dt2), CONCAT('S=', @.e.stop), CONCAT(TIME(@.e.we),':', TIME(@.e.wy)))),
					*/
		--	2. przedział badam czy @.e.dt2 miesci reszte puli 
			@.e.we2	:= 
				IF(@.e.stop,
					@.e.we2,
					IF(@.e.stop := (@.e.dt2 > (@.czas.Pula := GREATEST(@.czas.Pula - @.e.dt, 0))),
						@.e.we2 := @.e.we2  + INTERVAL @.czas.Pula MINUTE,  -- Nowe Od jako stare Od + pula czasowa
						NULL
					)
				),
			@.e.wy2 := IF(@.e.stop, @.e.wy2, NULL),
					/*
					@.czas.p.log := CONCAT_WS('/2: ', @.czas.p.log, CONCAT_WS(',', 
						CONCAT('P=', @.czas.Pula), CONCAT('dt3=', @.e.dt), CONCAT('S=', @.e.stop), CONCAT(TIME(@.e.we2),':', TIME(@.e.wy2)))),
					*/
		--	3. przedział badam czy @.e.dt2 miesci reszte puli 
			@.e.we3	:= 
				IF(@.e.stop,
					@.e.we3,
					IF(@.e.stop := (@.e.stop OR (@.e.dt3 > (@.czas.Pula := GREATEST(@.czas.Pula - @.e.dt2, 0)))),
						@.e.we3 := @.e.we3  + INTERVAL @.czas.Pula MINUTE,  -- Nowe Od jako stare Od + pula czasowa
						NULL
					)
				),
			@.e.wy3 := IF(@.e.stop, @.e.wy3, NULL),
		--	4. przedział badam czy @.e.dt2 miesci reszte puli 
					/*
					@.czas.p.log := CONCAT_WS('/3: ', @.czas.p.log, CONCAT_WS(',', 
						CONCAT('P=', @.czas.Pula), CONCAT('dt=', @.e.dt3), CONCAT('S=', @.e.stop), CONCAT(TIME(@.e.we3),':', TIME(@.e.wy3)))),
					*/
			@.e.we4	:= 
				IF(@.e.stop,
					@.e.we4,
					IF(@.e.stop := (@.e.stop OR (@.e.dt4 > (@.czas.Pula := GREATEST(@.czas.Pula - @.e.dt3, 0)))),
						@.e.we4 := @.e.we3  + INTERVAL @.czas.Pula MINUTE,  -- Nowe Od jako stare Od + pula czasowa
						NULL
					)
				),
			@.e.wy4 := IF(@.e.stop, @.e.wy4, NULL)
		;	
		DO _PL_WeWy('Calc.tP;Calc.tP_Noc;->tP', @.DatePocz, '', @.e.we, @.e.wy, @.e.we2, @.e.wy2, @.e.we3, @.e.wy3, @.e.we4, @.e.wy4);
					/*
					set
						@.x.e.we := @.e.we, @.x.e.wy = @.e.wy,
						@.x.e.we2 := @.e.we2, @.x.e.wy2 = @.e.wy2,
						@.x.e.we3 := @.e.we3, @.x.e.wy3 = @.e.wy3,
						@.x.e.we4 := @.e.we4, @.x.e.wy4 = @.e.wy4
					;
					*/
					-- SET @.e.X.we_wy_list = @.e.we_wy_list;
		SET 
			@.oddo.RETURN =
			IF(_MODE LIKE '%->tP_Noc%',
				@.e.tP_Noc,
				IF(_MODE LIKE '%->tP%',
					@.e.tP,
					IF(_MODE LIKE '%->we_wy%',
						@.e.we_wy_list,
						@.e.RETURN
					)
				)	
			)
		;
		RETURN @.oddo.RETURN;
	END IF;
	
END$$

DROP FUNCTION IF EXISTS `_PL_OdDoOLD`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_PL_OdDoOLD`(
	_MODE VARCHAR(150),
	_A_We DATETIME,
	_A_Start DATETIME,
	_A_End DATETIME,
	_Param_A VARCHAR(150),
	_Param_B VARCHAR(150)
) RETURNS VARCHAR(150)
    NO SQL
    DETERMINISTIC
BEGIN
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'Zmien_Na_od'
		_A_Start	jest wejsciem
		_A_End		jest wyjsciem z pracy
		czesc wspolan zakresu _A_Start _A_End oraz zakresu _B_Start _B_End i z tego minuty
		Gdy zakresy sie nie pokrywaja to 0
		SELECT _PL_OdDo('Zmien_Na_od', '2012-02-21 22:00', '2012-02-21 22:00', '2012-02-22 11:00', '', '');
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
	IF _MODE IN ('Zmien_Na_od') THEN
		IF _A_We BETWEEN _A_Start AND _A_End THEN
			SET 
				@.PL_OdDo.Start = _A_We,
				@.PL_OdDo.End = _A_End
			;
		ELSEIF _A_We < _A_Start THEN
			SET 
				@.PL_OdDo.Start = _A_Start,
				@.PL_OdDo.End = _A_End
			;
		ELSE
			SET 
				@.PL_OdDo.Start = NULL,
				@.PL_OdDo.End = NULL
			;
		END IF;	
			
		SET 
			@.e.we_wy_list = 
				CONCAT(TIME_FORMAT(@.PL_OdDo.Start, @.e.format_H), ' - ', TIME_FORMAT(@.PL_OdDo.End, @.e.format_H))
		;
		RETURN @.e.we_wy_list;
	END IF;
	
	IF _MODE LIKE '%Spec.od.do.ponad.NCP%' THEN
	/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
		mając w _Param_A  od do od do ... i datę konca pracy NCP zbieramy serie od do ale tylko starszą od NcP
		w _Param_B pula godzin wstępnyc
		SELECT _PL_OdDo('Zmien_Na_od', '2012-02-21 22:00', '', '', 'od:do', 'minuty');
	------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
		DO _PL_WeWy('_Params->@;Calc.tP;Calc.tP_Noc;->tP', _A_We, _Param_A, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
		SET 
			@.oddo.RETURN = NULL,	-- @.odod.i = 0,
			@.ne.we = @.e.we, @.ne.wy = @.e.wy,
			@.ne.we2 = @.e.we2, @.ne.wy2 = @.e.wy2,
			@.ne.we3 = @.e.we3, @.ne.wy3 = @.e.wy3,
			@.ne.we4 = @.e.we4, @.ne.wy4 = @.e.wy4,
			@.czas.Pula = _Param_B
		;
	--	UWAGA w zmiennych @.e.dt, @.e.dt2, @.e.dt3, ewn. @.e.dt4 są czasy tych przedziałów	
	--	1. przedział
		IF @.e.dt > @.czas.Pula THEN
		--	Tutaj koncze
			SET 			-- @.odod.i = 1,
				@.ne.we = @.ne.we  + INTERVAL @.czas.Pula MINUTE  -- Nowe Od jako stare Od + pula czasowa
				-- @.ne.wy = @.ne.wy,	-- Nowe Do bez zmian itd
			;
		ELSE
			SET 
				@.czas.Pula = @.czas.Pula - @.e.dt,
				@.ne.we = NULL, @.ne.wy = NULL
			;
		--	2. przedział
			IF @.e.dt2 > @.czas.Pula THEN
				SET 			-- @.odod.i = 2,
					@.ne.we2 = @.ne.we2  + INTERVAL @.czas.Pula MINUTE  -- Nowe Od jako stare Od + pula czasowa
					-- @.ne.wy2 = @.e.wy2	-- Nowe Do bez zmian itd
				;
			ELSE
				SET 
					@.czas.Pula = @.czas.Pula - @.e.dt2,
					@.ne.we2 = NULL, @.ne.wy2 = NULL
				;
			--	3. przedział
				IF @.e.dt3 > @.czas.Pula THEN
					SET 			-- @.odod.i = 3,
						@.ne.we3 = @.ne.we3  + INTERVAL @.czas.Pula MINUTE  -- Nowe Od jako stare Od + pula czasowa
						-- @.ne.wy3 = @.e.wy3	-- Nowe Do bez zmian
					;
				ELSE
					SET 
						@.czas.Pula = @.czas.Pula - @.e.dt3,
						@.ne.we3 = NULL, @.ne.wy3 = NULL
					;
				--	4. przedział
					IF @.e.dt4 > @.czas.Pula THEN
						SET 			-- @.odod.i = 4,
							@.ne.we4 = @.ne.we4  + INTERVAL @.czas.Pula MINUTE  -- Nowe Od jako stare Od + pula czasowa
							-- @.ne.wy4 = @.e.wy4	-- Nowe Do bez zmian
						;
					ELSE
						SET
							@.ne.we4 = NULL, @.ne.wy4 = NULL
						;
					END IF;
				END IF;
			END IF;
		END IF;
	--	Końcowe	
		DO _PL_WeWy('WeWy->@;Calc.tP;Calc.tP_Noc;->tP', _A_We, '', @.ne.we, @.ne.wy, @.ne.we2, @.ne.wy2, @.ne.we3, @.ne.wy3, @.ne.we4, @.ne.wy4);
		SET 
			@.oddo.RETURN =
			IF(_MODE LIKE '%->tP_Noc%',
				@.e.tP_Noc,
				IF(_MODE LIKE '%->tP%',
					@.e.tP,
					IF(_MODE LIKE '%->we_wy%',
						@.e.we_wy_list,
						@.e.RETURN
					)
				)	
			)
		;
		RETURN @.oddo.RETURN;
	END IF;
	
END$$

DROP FUNCTION IF EXISTS `_PL_WeWy`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_PL_WeWy`(
	_MODE VARCHAR(150),
	_DatePocz DateTime,
	_WeWyStr VARCHAR(150),
	_We DATETIME,
	_Wy DATETIME,
	_We2 DATETIME,
	_Wy2 DATETIME,
	_We3 DATETIME,
	_Wy3 DATETIME,
	_We4 DATETIME,
	_Wy4 DATETIME
) RETURNS VARCHAR(150)
    NO SQL
    DETERMINISTIC
BEGIN
	-- DECLARE _dt1, _dt2, _dt3, _dt4 INTEGER DEFAULT 0;
	-- DECLARE _Start, _End, _we, _wy, _we2, _wy2, _we3, _wy3, _we4, _wy4 DATETIME DEFAULT NULL;
	-- DECLARE _Start1, _End1, _Start2, _End2, _Start3, _End3, _Start4, _End4 DATETIME DEFAULT NULL;
	SET 
		-- @.e.format_H = IFNULL(@.e.format_H, IFNULL(@.e.format_H.z.rega, '%H:%i'))	-- '%H:%i:%s'
		@.e.format_H = '%H:%i'
	;

	IF IFNULL(NULLIF(_WeWyStr, ''), '') <> '' THEN	
	/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
		Wchodzimy z _WeWyStr
	------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
		SET
			@.e.dt = 0, @.e.dt2 = 0, @.e.dt3 = 0, @.e.dt4 = 0,
			@.e.we = NULL, @.e.wy = NULL, 
			@.e.we2 = NULL, @.e.wy2 = NULL,
			@.e.we3 = NULL, @.e.wy3 = NULL,
			@.e.we4 = NULL, @.e.wy4 = NULL,
			@._We.Params = CONCAT(_WeWyStr, ';')
		;
	--	1. cz. we wy
		SET @.Part = SUBSTR(@._We.Params, 1, (@.Part.P := LOCATE(';', @._We.Params)) - 1);
		SET @._We.Params = SUBSTR(@._We.Params, @.Part.P + 1, 255);
	--	W danej cz. Start
		SET @.Start.S = SUBSTR(@.Part, 1, (@.Start.S.P := LOCATE('-', @.Part)) - 1);
		SET @.End.S = SUBSTR(@.Part, @.Start.S.P + 1, 255);
		SET 
			@.e.we = ADDTIME(DATE(_DatePocz), TIME(@.Start.S)),
			@.e.wy = ADDTIME(DATE(_DatePocz), TIME(@.End.S))
		;
		IF @.e.we IS NOT NULL THEN
			IF @.e.we > @.e.wy THEN
				SET @.e.wy = @.e.wy + INTERVAL 1 DAY;
			END IF;
			SET 
				-- @.e.we = @.e.we, @.e.wy = @.e.wy,
				@.e.dt = IFNULL(TIMESTAMPDIFF(MINUTE,  @.e.we, @.e.wy), 0)
			;
		--	2. cz. we wy
			SET @.Part = SUBSTR(@._We.Params, 1, (@.Part.P := LOCATE(';', @._We.Params)) - 1);
			SET @._We.Params = SUBSTR(@._We.Params, @.Part.P + 1, 255);
		--	W danej cz. Start
			SET @.Start.S = SUBSTR(@.Part, 1, (@.Start.S.P := LOCATE('-', @.Part)) - 1);
			SET @.End.S = SUBSTR(@.Part, @.Start.S.P + 1, 255);
			SET 
				@.e.we2 = ADDTIME(DATE(_DatePocz), TIME(@.Start.S)),
				@.e.wy2 = ADDTIME(DATE(_DatePocz), TIME(@.End.S))
			;
			IF @.e.we2 IS NOT NULL THEN
				IF @.e.we2 > @.e.wy2 THEN
					SET @.e.wy2 = @.e.wy2 + INTERVAL 1 DAY;
				END IF;
				SET 
					-- @.e.we2 = @.Start, @.e.wy2 = @.End,
					@.e.dt2 = IFNULL(TIMESTAMPDIFF(MINUTE,  @.e.we2, @.e.wy2), 0)
				;
			--	3. cz. we wy
				SET @.Part = SUBSTR(@._We.Params, 1, (@.Part.P := LOCATE(';', @._We.Params)) - 1);
				SET @._We.Params = SUBSTR(@._We.Params, @.Part.P + 1, 255);
			--	W danej cz. Start
				SET @.Start.S = SUBSTR(@.Part, 1, (@.Start.S.P := LOCATE('-', @.Part)) - 1);
				SET @.End.S = SUBSTR(@.Part, @.Start.S.P + 1, 255);
				SET 
					@.e.we3 = ADDTIME(DATE(_DatePocz), TIME(@.Start.S)),
					@.e.wy3 = ADDTIME(DATE(_DatePocz), TIME(@.End.S))
				;
				IF @.e.we3 IS NOT NULL THEN
					IF @.e.we3 > @.e.wy3 THEN
						SET @.e.wy3 = @.e.wy3 + INTERVAL 1 DAY;
					END IF;
					SET 
						-- @.e.we3 = @.Start, @.e.wy3 = @.End,
						@.e.dt3 = IFNULL(TIMESTAMPDIFF(MINUTE,  @.e.we3, @.e.wy3), 0)
					;
				--	4. cz. we wy
				/*
					SET @.Part = SUBSTR(@._We.Params, 1, (@.Part.P := LOCATE(';', @._We.Params)) - 1);
					SET @._We.Params = SUBSTR(@._We.Params, @.Part.P + 1, 255);
				--	W danej cz. Start
					SET @.Start.S = SUBSTR(@.Part, 1, (@.Start.S.P := LOCATE('-', @.Part)) - 1);
					SET @.End.S = SUBSTR(@.Part, @.Start.S.P + 1, 255);
					SET 
						@.e.we4 = ADDTIME(DATE(_DatePocz), TIME(@.Start.S)),
						@.e.wy4 = ADDTIME(DATE(_DatePocz), TIME(@.End.S))
					;
					IF @.e.we4 IS NOT NULL THEN
						IF @.e.we4 > @.e.wy4 THEN
							SET @.e.wy4 = @.e.wy4 + INTERVAL 1 DAY;
						END IF;
						SET 
							-- @.e.we4 = @.Start, @.e.wy4 = @.End,
							@.e.dt4 = IFNULL(TIMESTAMPDIFF(MINUTE,  @.e.we4, @.e.wy4), 0)
						;
					END IF;
				*/
				END IF;
			END IF;
		END IF;
	ELSE	
	/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
		Wchodzimy z czasami
	------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
		SET
			-- @.e.dt = 0, @.e.dt2 = 0, @.e.dt3 = 0, @.e.dt4 = 0,
			@.e.we = NULLIF(_We, 0), @.e.wy = NULLIF(_Wy, 0), 
			@.e.we2 = NULLIF(_We2, 0), @.e.wy2 = NULLIF(_Wy2, 0),
			@.e.we3 = NULLIF(_We3, 0), @.e.wy3 = NULLIF(_Wy3, 0),
			@.e.we4 = NULLIF(_We4, 0), @.e.wy4 = NULLIF(_Wy4, 0)
		;
		SET
			@.e.wy := IF(@.e.wy >= @.e.we, @.e.wy, @.e.wy + INTERVAL 1 DAY),
			@.e.wy2 := IF(@.e.wy2 >= @.e.we2, @.e.wy2, @.e.wy2 + INTERVAL 1 DAY),
			@.e.wy3 := IF(@.e.wy3 >= @.e.we3, @.e.wy3, @.e.wy3 + INTERVAL 1 DAY),
			@.e.wy4 := IF(@.e.wy4 >= @.e.we4, @.e.wy4, @.e.wy4 + INTERVAL 1 DAY),
			@.e.dt = IFNULL(TIMESTAMPDIFF(MINUTE,  @.e.we, @.e.wy), 0),
			@.e.dt2 = IFNULL(TIMESTAMPDIFF(MINUTE,  @.e.we2, @.e.wy2), 0),
			@.e.dt3 = IFNULL(TIMESTAMPDIFF(MINUTE,  @.e.we3, @.e.wy3), 0),
			@.e.dt4 = IFNULL(TIMESTAMPDIFF(MINUTE,  @.e.we4, @.e.wy4), 0)
		;
	END IF;
	
	IF _MODE LIKE '%Calc.tP%' THEN
					-- SET @.x.log = CONCAT_WS('/', 'tP', @.x.log);
		SET
			@.e.tP = 0
				+ @.e.dt + @.e.dt2 + @.e.dt3 + @.e.dt4
		;
	END IF;
	
	IF _MODE LIKE '%Calc.tP_Noc%' THEN
					-- SET @.x.log = CONCAT_WS('/', 'tNoc', @.x.log);
		SET
			@.e.tP_Noc = 0 
				+ _PL_WeWy_Night('', @.e.we, @.e.wy)
				+ _PL_WeWy_Night('', @.e.we2, @.e.wy2)
				+ _PL_WeWy_Night('', @.e.we3, @.e.wy3)
			/*
				+ IFNULL(_PL_WeWy_Night('', @.e.we, @.e.wy), 0)
				+ IFNULL(_PL_WeWy_Night('', @.e.we2, @.e.wy2), 0)
				+ IFNULL(_PL_WeWy_Night('', @.e.we3, @.e.wy3), 0)
				-- + + _PL_WeWy_Night('', @.e.we4, @.e.wy4), 0)
			*/
		;
	END IF;
	
	SET 
		@.e.we_wy_list = CONCAT_WS('; ',
			CONCAT(TIME_FORMAT(@.e.we, @.e.format_H), ' - ', TIME_FORMAT(@.e.wy, @.e.format_H)),
			CONCAT(TIME_FORMAT(@.e.we2, @.e.format_H), ' - ', TIME_FORMAT(@.e.wy2, @.e.format_H)),
			CONCAT(TIME_FORMAT(@.e.we3, @.e.format_H), ' - ', TIME_FORMAT(@.e.wy3, @.e.format_H)),
			CONCAT(TIME_FORMAT(@.e.we4, @.e.format_H), ' - ', TIME_FORMAT(@.e.wy4, @.e.format_H))
		),
		@.e.we.Min = NULLIF(LEAST(IFNULL(@.e.we, '2100-12-31 23:59:59'), IFNULL(@.e.we2, '2100-12-31 23:59:59'), IFNULL(@.e.we3, '2100-12-31 23:59:59'), IFNULL(@.e.we4, '2100-12-31 23:59:59')), '2100-12-31 23:59:59'),
		@.e.wy.Max = NULLIF(GREATEST(IFNULL(@.e.wy, '0000-00-00 00:00:00'), IFNULL(@.e.wy2, '0000-00-00 00:00:00'), IFNULL(@.e.wy3, '0000-00-00 00:00:00'), IFNULL(@.e.wy4, '0000-00-00 00:00:00')), 0)
	;
	
	SET 
		@.e.RETURN =
		IF(_MODE LIKE '%->tP_Noc%',
			@.e.tP_Noc,
			IF(_MODE LIKE '%->tP%',
				@.e.tP,
				IF(_MODE LIKE '%->we_wy%',
					@.e.we_wy_list,
					@.e.RETURN
				)
			)	
		)
	;
	
	RETURN @.e.RETURN;
END$$

DROP FUNCTION IF EXISTS `_PL_WeWyOLD`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_PL_WeWyOLD`(
	_MODE VARCHAR(150),
	_DatePocz DateTime,
	_Params VARCHAR(150),
	_We DATETIME,
	_Wy DATETIME,
	_We2 DATETIME,
	_Wy2 DATETIME,
	_We3 DATETIME,
	_Wy3 DATETIME,
	_We4 DATETIME,
	_Wy4 DATETIME
) RETURNS VARCHAR(150)
    NO SQL
    DETERMINISTIC
BEGIN
	SET
		@.e.RETURN = '',
		@.e.tP = 0,
		@.e.tP_Noc = 0,
		@.e.format_H = IFNULL(@.e.format_H, IFNULL(@.e.format_H.z.rega, '%H:%i'))	-- '%H:%i:%s'
		, @.part.log = NULL
		, @.x.log = NULL
	;
	/*
	IF _MODE LIKE '%tP->@%' THEN
		aaaaa
	END IF;
	*/
	IF _MODE LIKE '%Czas.na.WeWy%' THEN
	/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
		W _Params mamy Czas w min
	------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
		SET
			@.e.tP = _Params
		;	
		SET
			@.e.we = _DatePocz,
			@.e.wy = _DatePocz + INTERVAL @.e.tP MINUTE,
			@.e.we2 = NULL, @.e.wy2 = NULL,
			@.e.we3 = NULL, @.e.wy3 = NULL,
			@.e.we4 = NULL, @.e.wy4 = NULL
		;
	END IF;
	
	IF _MODE LIKE '%_Params->@%' THEN
	/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
		Wchodzimy zapisami typu	
			08:23-09:44;10:13-16:55
			a zawaramu We/wy w _A,_B, ...
			_A_Start	jest wejsciem
			_A_End		jest wyjsciem z pracy
	------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
		SET
			@.Parts = 0,
			@._We.Params = CONCAT(_Params, ';'),
			@.e.we = NULL, @.e.wy = NULL, 
			@.e.we2 = NULL, @.e.wy2 = NULL,
			@.e.we3 = NULL, @.e.wy3 = NULL,
			@.e.we4 = NULL, @.e.wy4 = NULL
		;
		IF @._We.Params IS NOT NULL THEN
			WHILE @.Parts <= 2 DO
			--	Kolejna cz.
				SET @.Part = SUBSTR(@._We.Params, 1, (@.Part.P := LOCATE(';', @._We.Params)) - 1);
				SET @._We.Params = SUBSTR(@._We.Params, @.Part.P + 1, 255);
			--	W danej cz. Start
				SET @.Start.S = SUBSTR(@.Part, 1, (@.Start.S.P := LOCATE('-', @.Part)) - 1);
				SET @.End.S = SUBSTR(@.Part, @.Start.S.P + 1, 255);
				
				SET 
					@.Start = ADDTIME(DATE(_DatePocz), TIME(@.Start.S)),
					@.End = ADDTIME(DATE(_DatePocz), TIME(@.End.S))
				;
				IF @.Start > @.End THEN
					SET @.End = @.End + INTERVAL 1 DAY;
				END IF;
				IF @.Parts = 0 THEN
					SET @.e.we = @.Start, @.e.wy = @.End;
				ELSEIF @.Parts = 1 THEN
					SET @.e.we2 = @.Start, @.e.wy2 = @.End;
				ELSEIF @.Parts = 2 THEN
					SET @.e.we3 = @.Start, @.e.wy3 = @.End;
				END IF;	

				SET @.Parts = @.Parts + 1;
					SET @.part.log = CONCAT_WS('/', @.part.log, concat('part:', @.Part), concat(',start:', @.Start.S), concat(',end:', @.End.S), null); 
			END WHILE;
		END IF;
	END IF;
	
	IF _MODE LIKE '%WeWy->@%' THEN
	/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
		Wchodzimy z czasami
	------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
		SET
			@.e.we = NULLIF(_We, 0), @.e.wy = NULLIF(_Wy, 0), 
			@.e.we2 = NULLIF(_We2, 0), @.e.wy2 = NULLIF(_Wy2, 0),
			@.e.we3 = NULLIF(_We3, 0), @.e.wy3 = NULLIF(_Wy3, 0),
			@.e.we4 = NULLIF(_We4, 0), @.e.wy4 = NULLIF(_Wy4, 0)
		;
		SET
			@.e.wy = IF(@.e.wy >= @.e.we, @.e.wy, @.e.wy + INTERVAL 1 DAY),
			@.e.wy2 = IF(@.e.wy2 >= @.e.we2, @.e.wy2, @.e.wy2 + INTERVAL 1 DAY),
			@.e.wy3 = IF(@.e.wy3 >= @.e.we3, @.e.wy3, @.e.wy3 + INTERVAL 1 DAY),
			@.e.wy4 = IF(@.e.wy4 >= @.e.we4, @.e.wy4, @.e.wy4 + INTERVAL 1 DAY)
		;
		
	END IF;
	
	IF _MODE LIKE '%Spec.od.do.NCP._Params->@%' THEN
	/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
		CHWILOWO
		mając w _Params  od do od do ... i datę konca pracy NCP zbieramy serie od do ale tylko starszą od NcP
	------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
		SET
			@.e.we = NULLIF(_We, 0), @.e.wy = NULLIF(_Wy, 0), 
			@.e.we2 = NULLIF(_We2, 0), @.e.wy2 = NULLIF(_Wy2, 0),
			@.e.we3 = NULLIF(_We3, 0), @.e.wy3 = NULLIF(_Wy3, 0),
			@.e.we4 = NULLIF(_We4, 0), @.e.wy4 = NULLIF(_Wy4, 0)
		;
		SET
			@.e.wy = IF(@.e.wy >= @.e.we, @.e.wy, @.e.wy + INTERVAL 1 DAY),
			@.e.wy2 = IF(@.e.wy2 >= @.e.we2, @.e.wy2, @.e.wy2 + INTERVAL 1 DAY),
			@.e.wy3 = IF(@.e.wy3 >= @.e.we3, @.e.wy3, @.e.wy3 + INTERVAL 1 DAY),
			@.e.wy4 = IF(@.e.wy4 >= @.e.we4, @.e.wy4, @.e.wy4 + INTERVAL 1 DAY)
		;
		
	END IF;
	
	IF _MODE LIKE '%->@%' THEN
		SET 
			@.e.we_wy_list = CONCAT_WS('; ',
				CONCAT(TIME_FORMAT(@.e.we, @.e.format_H), ' - ', TIME_FORMAT(@.e.wy, @.e.format_H)),
				CONCAT(TIME_FORMAT(@.e.we2, @.e.format_H), ' - ', TIME_FORMAT(@.e.wy2, @.e.format_H)),
				CONCAT(TIME_FORMAT(@.e.we3, @.e.format_H), ' - ', TIME_FORMAT(@.e.wy3, @.e.format_H)),
				CONCAT(TIME_FORMAT(@.e.we4, @.e.format_H), ' - ', TIME_FORMAT(@.e.wy4, @.e.format_H))
			),
			-- @.e.we.Min = LEAST(@.e.we, @.e.we2, @.e.we3, @.e.we4),
			@.e.we.Min = NULLIF(LEAST(IFNULL(@.e.we, '2100-12-31 23:59:59'), IFNULL(@.e.we2, '2100-12-31 23:59:59'), IFNULL(@.e.we3, '2100-12-31 23:59:59'), IFNULL(@.e.we4, '2100-12-31 23:59:59')), '2100-12-31 23:59:59'),
			-- @.e.wy.Max = GREATEST(@.e.wy, @.e.wy2, @.e.wy3, @.e.wy4)
			@.e.wy.Max = NULLIF(GREATEST(IFNULL(@.e.wy, 0), IFNULL(@.e.wy2, 0), IFNULL(@.e.wy3, 0), IFNULL(@.e.wy4, 0)), 0)
		;
	END IF;
	
	IF _MODE LIKE '%Calc.tP%' THEN
					SET @.x.log = CONCAT_WS('/', 'tP', @.x.log);
		SET
			@.e.tP = 0
				+ IFNULL(@.e.dt := TIMESTAMPDIFF(MINUTE, @.e.we, @.e.wy), 0)
				+ IFNULL(@.e.dt2 := TIMESTAMPDIFF(MINUTE, @.e.we2, @.e.wy2), 0)
				+ IFNULL(@.e.dt3 := TIMESTAMPDIFF(MINUTE, @.e.we3, @.e.wy3), 0)
				-- + IFNULL(@.e.dt4 := TIMESTAMPDIFF(MINUTE, @.e.we4, @.e.wy4), 0)
		;
	END IF;
	
	IF _MODE LIKE '%Calc.tP_Noc%' THEN
					SET @.x.log = CONCAT_WS('/', 'tNoc', @.x.log);
		SET
			@.e.tP_Noc = 0
				+ IFNULL(_PL_WeWy_Night('', @.e.we, @.e.wy), 0)
				+ IFNULL(_PL_WeWy_Night('', @.e.we2, @.e.wy2), 0)
				+ IFNULL(_PL_WeWy_Night('', @.e.we3, @.e.wy3), 0)
				-- + + _PL_WeWy_Night('', @.e.we4, @.e.wy4), 0)
		;
	END IF;

	SET 
		@.e.RETURN =
		IF(_MODE LIKE '%->tP_Noc%',
			@.e.tP_Noc,
			IF(_MODE LIKE '%->tP%',
				@.e.tP,
				IF(_MODE LIKE '%->we_wy%',
					@.e.we_wy_list,
					@.e.RETURN
				)
			)	
		)
	;
	
	RETURN @.e.RETURN;
END$$

DROP FUNCTION IF EXISTS `_var_Time_PL_init`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_var_Time_PL_init`(
	_MODE VARCHAR(150)
) RETURNS INTEGER
    NO SQL
    DETERMINISTIC
BEGIN
	SET
		@.ti.RETURN = '',
	--	Format polski	
		lc_time_names = 'pl_PL',
		@.e.format_H = IFNULL(@.e.format_H, IFNULL(@.e.format_H.z.rega, '%H:%i')),	-- '%H:%i:%s'
	--	zmienne do nalicz. nocy	
		@.n.D := DATE('2023-01-01'),
		@.n.DT := ADDTIME(DATE(@.n.D), '00:00'),
		@.n.D_00_00 := ADDTIME(@.n.DT, '00:00'),	-- + 0h - dany dzień 00:00
		@.n.D_06_00 := ADDTIME(@.n.DT, '06:00'),	-- + 6h - dany dzień 06:00
		@.n.D_22_00 := ADDTIME(@.n.DT, '22:00'),	-- +22h - dany dzień 22:00 - relatywne jutro od dziś 22:00 do...  
		@.n.Dn_06_00 := ADDTIME(@.n.DT, '30:00'),	-- +30h - nast. dzień 06:00 - relatywne jutro do 06:00 do...  
		@.n.Dn_22_00 := ADDTIME(@.n.DT, '46:00'),	-- +46h - nast. dzień 22:00 - relatywne jutro do 22:00 do...  
		@.n.Dn_23_59 := ADDTIME(@.n.DT, '47:59')	-- +30h - nast. dzień 23_59 - relatywne jutro do 23_59 do...  
	;	
	SET @.pl.ValidDate = 
		IFNULL(@.pl.ValidDate,
			(SELECT t.ValidDate FROM `_pl_today` t)
		)
	;
	RETURN @.ti.RETURN;
END$$



DROP FUNCTION IF EXISTS `_PL_WeWy_list`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_PL_WeWy_list`(
	_MODE VARCHAR(150),
	_DatePocz DateTime,
	_abStr VARCHAR(150),
	_abExStr VARCHAR(150),
	_a0 DATETIME,
	_b0 DATETIME,
	_a1 DATETIME,
	_b1 DATETIME,
	_a2 DATETIME,
	_b2 DATETIME,
	_a3 DATETIME,
	_b4 DATETIME,
	_aEx DATETIME,
	_bEx DATETIME
) RETURNS VARCHAR(150)
    NO SQL
    DETERMINISTIC
BEGIN
	-- DECLARE _dt1, _dt2, _dt3, _dt4 INTEGER DEFAULT 0;
	-- DECLARE _Start, _End, _we, _wy, _we2, _wy2, _we3, _wy3, _we4, _wy4 DATETIME DEFAULT NULL;
	-- DECLARE _Start1, _End1, _Start2, _End2, _Start3, _End3, _Start4, _End4 DATETIME DEFAULT NULL;
	SET 
		-- @.e.format_H = IFNULL(@.e.format_H, IFNULL(@.e.format_H.z.rega, '%H:%i'))	-- '%H:%i:%s'
		@.e.format_H = '%H:%i'
	;

	IF IFNULL(NULLIF(_abStr, ''), '') <> '' THEN	
	/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
		Wchodzimy z _abStr
	------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
		SET
			@.e.dt = 0, @.e.dt2 = 0, @.e.dt3 = 0, @.e.dt4 = 0,
			@.e.we = NULL, @.e.wy = NULL, 
			@.e.we2 = NULL, @.e.wy2 = NULL,
			@.e.we3 = NULL, @.e.wy3 = NULL,
			@.e.we4 = NULL, @.e.wy4 = NULL,
			@._We.Params = CONCAT(_abStr, ';')
		;
	--	1. cz. we wy
		SET @.Part = SUBSTR(@._We.Params, 1, (@.Part.P := LOCATE(';', @._We.Params)) - 1);
		SET @._We.Params = SUBSTR(@._We.Params, @.Part.P + 1, 255);
	--	W danej cz. Start
		SET @.Start.S = SUBSTR(@.Part, 1, (@.Start.S.P := LOCATE('-', @.Part)) - 1);
		SET @.End.S = SUBSTR(@.Part, @.Start.S.P + 1, 255);
		SET 
			@.e.we = ADDTIME(DATE(_DatePocz), TIME(@.Start.S)),
			@.e.wy = ADDTIME(DATE(_DatePocz), TIME(@.End.S))
		;
		IF @.e.we IS NOT NULL THEN
			IF @.e.we > @.e.wy THEN
				SET @.e.wy = @.e.wy + INTERVAL 1 DAY;
			END IF;
			SET 
				-- @.e.we = @.e.we, @.e.wy = @.e.wy,
				@.e.dt = IFNULL(TIMESTAMPDIFF(MINUTE,  @.e.we, @.e.wy), 0)
			;
		--	2. cz. we wy
			SET @.Part = SUBSTR(@._We.Params, 1, (@.Part.P := LOCATE(';', @._We.Params)) - 1);
			SET @._We.Params = SUBSTR(@._We.Params, @.Part.P + 1, 255);
		--	W danej cz. Start
			SET @.Start.S = SUBSTR(@.Part, 1, (@.Start.S.P := LOCATE('-', @.Part)) - 1);
			SET @.End.S = SUBSTR(@.Part, @.Start.S.P + 1, 255);
			SET 
				@.e.we2 = ADDTIME(DATE(_DatePocz), TIME(@.Start.S)),
				@.e.wy2 = ADDTIME(DATE(_DatePocz), TIME(@.End.S))
			;
			IF @.e.we2 IS NOT NULL THEN
				IF @.e.we2 > @.e.wy2 THEN
					SET @.e.wy2 = @.e.wy2 + INTERVAL 1 DAY;
				END IF;
				SET 
					-- @.e.we2 = @.Start, @.e.wy2 = @.End,
					@.e.dt2 = IFNULL(TIMESTAMPDIFF(MINUTE,  @.e.we2, @.e.wy2), 0)
				;
			--	3. cz. we wy
				SET @.Part = SUBSTR(@._We.Params, 1, (@.Part.P := LOCATE(';', @._We.Params)) - 1);
				SET @._We.Params = SUBSTR(@._We.Params, @.Part.P + 1, 255);
			--	W danej cz. Start
				SET @.Start.S = SUBSTR(@.Part, 1, (@.Start.S.P := LOCATE('-', @.Part)) - 1);
				SET @.End.S = SUBSTR(@.Part, @.Start.S.P + 1, 255);
				SET 
					@.e.we3 = ADDTIME(DATE(_DatePocz), TIME(@.Start.S)),
					@.e.wy3 = ADDTIME(DATE(_DatePocz), TIME(@.End.S))
				;
				IF @.e.we3 IS NOT NULL THEN
					IF @.e.we3 > @.e.wy3 THEN
						SET @.e.wy3 = @.e.wy3 + INTERVAL 1 DAY;
					END IF;
					SET 
						-- @.e.we3 = @.Start, @.e.wy3 = @.End,
						@.e.dt3 = IFNULL(TIMESTAMPDIFF(MINUTE,  @.e.we3, @.e.wy3), 0)
					;
				--	4. cz. we wy
				END IF;
			END IF;
		END IF;
	ELSE	
	/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
		Wchodzimy z czasami
	------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
		SET
			-- @.e.dt = 0, @.e.dt2 = 0, @.e.dt3 = 0, @.e.dt4 = 0,
			@.e.we = NULLIF(_We, 0), @.e.wy = NULLIF(_Wy, 0), 
			@.e.we2 = NULLIF(_We2, 0), @.e.wy2 = NULLIF(_Wy2, 0),
			@.e.we3 = NULLIF(_We3, 0), @.e.wy3 = NULLIF(_Wy3, 0),
			@.e.we4 = NULLIF(_We4, 0), @.e.wy4 = NULLIF(_Wy4, 0)
		;
		SET
			@.e.wy := IF(@.e.wy >= @.e.we, @.e.wy, @.e.wy + INTERVAL 1 DAY),
			@.e.wy2 := IF(@.e.wy2 >= @.e.we2, @.e.wy2, @.e.wy2 + INTERVAL 1 DAY),
			@.e.wy3 := IF(@.e.wy3 >= @.e.we3, @.e.wy3, @.e.wy3 + INTERVAL 1 DAY),
			@.e.wy4 := IF(@.e.wy4 >= @.e.we4, @.e.wy4, @.e.wy4 + INTERVAL 1 DAY),
			@.e.dt = IFNULL(TIMESTAMPDIFF(MINUTE,  @.e.we, @.e.wy), 0),
			@.e.dt2 = IFNULL(TIMESTAMPDIFF(MINUTE,  @.e.we2, @.e.wy2), 0),
			@.e.dt3 = IFNULL(TIMESTAMPDIFF(MINUTE,  @.e.we3, @.e.wy3), 0),
			@.e.dt4 = IFNULL(TIMESTAMPDIFF(MINUTE,  @.e.we4, @.e.wy4), 0)
		;
	END IF;
	
	IF _MODE LIKE '%Calc.tP%' THEN
					-- SET @.x.log = CONCAT_WS('/', 'tP', @.x.log);
		SET
			@.e.tP = 0
				+ @.e.dt + @.e.dt2 + @.e.dt3 + @.e.dt4
		;
	END IF;
	
	IF _MODE LIKE '%Calc.tP_Noc%' THEN
					-- SET @.x.log = CONCAT_WS('/', 'tNoc', @.x.log);
		SET
			@.e.tP_Noc = 0 
				+ _PL_WeWy_Night('', @.e.we, @.e.wy)
				+ _PL_WeWy_Night('', @.e.we2, @.e.wy2)
				+ _PL_WeWy_Night('', @.e.we3, @.e.wy3)
			/*
				+ IFNULL(_PL_WeWy_Night('', @.e.we, @.e.wy), 0)
				+ IFNULL(_PL_WeWy_Night('', @.e.we2, @.e.wy2), 0)
				+ IFNULL(_PL_WeWy_Night('', @.e.we3, @.e.wy3), 0)
				-- + + _PL_WeWy_Night('', @.e.we4, @.e.wy4), 0)
			*/
		;
	END IF;
	
	SET 
		@.e.we_wy_list = CONCAT_WS('; ',
			CONCAT(TIME_FORMAT(@.e.we, @.e.format_H), ' - ', TIME_FORMAT(@.e.wy, @.e.format_H)),
			CONCAT(TIME_FORMAT(@.e.we2, @.e.format_H), ' - ', TIME_FORMAT(@.e.wy2, @.e.format_H)),
			CONCAT(TIME_FORMAT(@.e.we3, @.e.format_H), ' - ', TIME_FORMAT(@.e.wy3, @.e.format_H)),
			CONCAT(TIME_FORMAT(@.e.we4, @.e.format_H), ' - ', TIME_FORMAT(@.e.wy4, @.e.format_H))
		),
		@.e.we.Min = NULLIF(LEAST(IFNULL(@.e.we, '2100-12-31 23:59:59'), IFNULL(@.e.we2, '2100-12-31 23:59:59'), IFNULL(@.e.we3, '2100-12-31 23:59:59'), IFNULL(@.e.we4, '2100-12-31 23:59:59')), '2100-12-31 23:59:59'),
		@.e.wy.Max = NULLIF(GREATEST(IFNULL(@.e.wy, '0000-00-00 00:00:00'), IFNULL(@.e.wy2, '0000-00-00 00:00:00'), IFNULL(@.e.wy3, '0000-00-00 00:00:00'), IFNULL(@.e.wy4, '0000-00-00 00:00:00')), 0)
	;
	
	SET 
		@.e.RETURN =
		IF(_MODE LIKE '%->tP_Noc%',
			@.e.tP_Noc,
			IF(_MODE LIKE '%->tP%',
				@.e.tP,
				IF(_MODE LIKE '%->we_wy%',
					@.e.we_wy_list,
					@.e.RETURN
				)
			)	
		)
	;
	RETURN @.e.RETURN;
END$$

DELIMITER ;

/*
====================================================================================================================================================================================
	NOTATNIK
====================================================================================================================================================================================
	Round_Min !!!!!!!!!!!
	SET	
		@.min.round.30@.min.round.30 = 30 
	dla @.minWe round 30 min
	SET
		@.R.0.5 := (@.R := 30) DIV 2
	;
	RETURN 
		@.R*(@.minWe DIV @.R) + IF(@.minWe MOD @.R < @.R.0.5, 0, @.R)
	;
*/