 /*!40101 SET NAMES utf8 */;

/*
Funkcje dat/czasu - przykĹ‚ady i opisy

_MakeDate(Y, M, D) -> DATE
    Tworzy datÄ™ z podanych roku, miesiÄ…ca i dnia.
    PrzykĹ‚ad: SELECT _MakeDate(2024, 7, 31); -- Wynik: '2024-07-31'

_varDateTime(Value) -> DATETIME
    Zwraca przekazanÄ… wartoĹ›Ä‡ typu DATETIME.
    PrzykĹ‚ad: SELECT _varDateTime('2024-07-31 12:00:00');

_var_DateToDateTime(Value, Time) -> DATETIME
    Ĺ�Ä…czy datÄ™ i czas w jeden DATETIME.
    PrzykĹ‚ad: SELECT _var_DateToDateTime('2024-07-31', '12:34:56');

_var_DateToYM(Value) -> INT
    Zwraca rok i miesiÄ…c w formacie RRRRMM.
    PrzykĹ‚ad: SELECT _var_DateToYM('2024-07-31'); -- Wynik: 202407

_var_YMToFirstDay(Value) -> DATE
    Zwraca pierwszy dzieĹ„ miesiÄ…ca na podstawie liczby RRRRMM.
    PrzykĹ‚ad: SELECT _var_YMToFirstDay(202407); -- Wynik: '2024-07-01'

_var_YMToLastDay(Value) -> DATE
    Zwraca ostatni dzieĹ„ miesiÄ…ca na podstawie liczby RRRRMM.
    PrzykĹ‚ad: SELECT _var_YMToLastDay(202407); -- Wynik: '2024-07-31'

_var_DateToYMW(Value) -> INT
    Zwraca rok, miesiÄ…c i tydzieĹ„ w formacie RRRRMMTT.
    PrzykĹ‚ad: SELECT _var_DateToYMW('2024-07-31');

_var_YM_Add(Value, Add) -> INT
    Dodaje miesiÄ…ce do liczby RRRRMM.
    PrzykĹ‚ad: SELECT _var_YM_Add(202407, 2); -- Wynik: 202409

_var_DateToYQ(Value) -> INT
    Zwraca rok i kwartaĹ‚ w formacie RRRRQ.
    PrzykĹ‚ad: SELECT _var_DateToYQ('2024-07-31'); -- Wynik: 202402

_var_DateToYW(Value) -> INT
    Zwraca rok i tydzieĹ„ w formacie RRRRTT.
    PrzykĹ‚ad: SELECT _var_DateToYW('2024-07-31');

_varDateToDateTime(Value, Time) -> DATETIME
    Ĺ�Ä…czy datÄ™ i czas w jeden DATETIME (alternatywa).
    PrzykĹ‚ad: SELECT _varDateToDateTime('2024-07-31', '12:00:00');

_DT2DateTime(Value, Time) -> DATETIME
    Ĺ�Ä…czy datÄ™ i czas w jeden DATETIME.
    PrzykĹ‚ad: SELECT _DT2DateTime('2024-07-31', '12:00:00');

_Str_to_H(We) -> DOUBLE
    Zamienia tekstowÄ… reprezentacjÄ™ czasu na godziny.
    PrzykĹ‚ad: SELECT _Str_to_H('2h'); -- Wynik: 2

_H_to_STR(HOURS, MODE) -> VARCHAR
    Zamienia liczbÄ™ godzin na tekst (np. 1.5 -> '1 godz 30 min').
    PrzykĹ‚ad: SELECT _H_to_STR(1.5, ''); -- Wynik: '1 godz 30 min'

_Month_Fmt(Month_FMT, Month) -> VARCHAR
    Zwraca nazwÄ™ miesiÄ…ca w zadanym formacie.
    PrzykĹ‚ad: SELECT _Month_Fmt('MR', 3); -- Wynik: 'III'

_Day_Month_Fmt(Month_FMT, Day, Month) -> VARCHAR
    Zwraca dzieĹ„ i miesiÄ…c w zadanym formacie.
    PrzykĹ‚ad: SELECT _Day_Month_Fmt('MR', 15, 3);

_DATE_FORMAT_Range(Start, End, Month_FMT, MODE) -> VARCHAR
    Formatuje zakres dat.
    PrzykĹ‚ad: SELECT _DATE_FORMAT_Range('2024-07-01', '2024-07-31', '', '');

_Day_Month_Year_Fmt2(Day, Month, Year) -> VARCHAR
    Zwraca sformatowanÄ… datÄ™.
    PrzykĹ‚ad: SELECT _Day_Month_Year_Fmt2(15, 3, 2024);

_Day_Month_Year_Fmt(Day, Month, Year) -> VARCHAR
    Zwraca sformatowanÄ… datÄ™.
    PrzykĹ‚ad: SELECT _Day_Month_Year_Fmt(15, 3, 2024);

_Fmt_DateTime_QUICK(DateTime, Lang) -> VARCHAR
    Szybkie formatowanie daty i czasu.
    PrzykĹ‚ad: SELECT _Fmt_DateTime_QUICK('2024-07-31 12:00:00', 'pl');

_Fmt_DateTime_QUICK2(DateTime, Lang) -> VARCHAR
    Szybkie formatowanie daty i czasu (wariant 2).
    PrzykĹ‚ad: SELECT _Fmt_DateTime_QUICK2('2024-07-31 12:00:00', 'pl');
*/

DELIMITER $$
DROP FUNCTION IF EXISTS `_DateToYM`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_DateToYM`(_D DATE) RETURNS INT
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN (DATE(_D) * 1) DIV 100;
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_YMToD1`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_YMToD1`(_YM INT) RETURNS DATE
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN DATE(100*_YM + 1);
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_YMToD2`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_YMToD2`(_YM INT) RETURNS DATE
    NO SQL
    DETERMINISTIC
BEGIN
	-- RETURN DATE(100*_YM + 1) + INTERVAL 1 MONTH - INTERVAL 1 DAY;
	RETURN LAST_DAY(DATE(100*_YM + 1));
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_MakeDate`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_MakeDate`(_Y INTEGER, _M INTEGER, _D INTEGER) RETURNS DATE
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN STR_TO_DATE(CONCAT_WS(',', _Y, _M, _D),'%Y,%m,%d');
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_varDateTime`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_varDateTime`(_Value DATETIME) RETURNS DATETIME
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN _Value;
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_var_DateToDateTime`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_var_DateToDateTime`(_Value DateTime, _Time Time) RETURNS DateTime
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN _varDateToDateTime(_Value, _Time);
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_var_DateToYM`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_var_DateToYM`(_Value DATE) RETURNS INT
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN MONTH(_Value) + 100*YEAR(_Value);
	-- lepsze?!!!! RETURN DATE_FORMAT(_Value, '%Y%m');
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_var_YMToFirstDay`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_var_YMToFirstDay`(_Value INT) RETURNS DATE
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN DATE(CONCAT_WS('-', _Value DIV 100, _Value MOD 100, 1));
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_var_YMToLastDay`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_var_YMToLastDay`(_Value INT) RETURNS DATE
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN LAST_DAY(DATE(CONCAT_WS('-', _Value DIV 100, _Value MOD 100, 1)));
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_var_DateToYMW`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_var_DateToYMW`(_Value DATE) RETURNS INT
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN (YEAR(_Value)*100 + MONTH(_Value))*100 + WEEK(_Value, 1);
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_var_YM_Add`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_var_YM_Add`(_Value INT, _Add INT) RETURNS INTEGER
    NO SQL
    DETERMINISTIC
BEGIN
	SET
		@.weYM := _Value,
		@.wePlus := _Add,
		@.weY := @.weYM DIV 100,
		@.weM := 1200 + @.weYM MOD 100,
		@.M1 := @.weM + @.wePlus,
		@.M := (@.M1 - 1) MOD 12 + 1,
		@.AddY := (@.M1 - 1) DIV 12
	;
	SET
		@.wyYM = 
			(@.weY + @.AddY - 100)*100 + @.M
			-- (@.weYM DIV 100 + (@.weYM MOD 100 + @.wePlus - 1) DIV 12)*100 + (@.weYM MOD 100 + @.wePlus - 1) MOD 12 + 1
	;

	RETURN @.wyYM;
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_var_DateToYQ`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_var_DateToYQ`(_Value DATE) RETURNS INT
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN QUARTER(_Value) + 100*YEAR(_Value);
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_var_DateToYW`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_var_DateToYW`(_Value DATE) RETURNS INT
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN WEEK(_Value) + 100*YEAR(_Value);
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_varDateToDateTime`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_varDateToDateTime`(_Value DateTime, _Time Time) RETURNS DateTime
    NO SQL
    DETERMINISTIC
BEGIN
	DECLARE D DateTime;
	SET _Time = IFNULL(_Time, MAKETIME(0, 0, 0));
	-- takie przypisanie generowalo blad `incorrect time value` SET _Time = IF(_Time = '', MAKETIME(0, 0, 0), _Time); 
	SET @Time = _Time;
	-- SET D = _varDateTime(CONCAT(D, ' ' , _Time));
	SET D = TIMESTAMP(DATE(_Value), _Time);
	RETURN D;
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_DT2DateTime`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_DT2DateTime`(_Value Date, _Time Time) RETURNS DateTime
    NO SQL
    DETERMINISTIC
BEGIN
	DECLARE D DateTime;
	SET D = TIMESTAMP(DATE(_Value), _Time);
	RETURN D;
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_Str_to_H`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Str_to_H`(
	_We VARCHAR(50)
) RETURNS DOUBLE
    NO SQL
    DETERMINISTIC
BEGIN
	SET @.n = 1*_We;
	SET 
		-- @._Unit = 
		@.H =
			IF(_We LIKE '%sec%' OR _We LIKE '%s%',
				@.n/3600,	--	'SEC',
			IF(_We LIKE '%min%' OR _We LIKE '%m%',
				@.n/60,	--	'MIN',
			IF(_We LIKE '%godz%' OR _We LIKE '%h%',
				@.n * 1,	--	'HOUR',
			IF(_We LIKE '%dn%' OR _We LIKE '%d%',
				@.n * 24,	--	'DAY',
				@.n * 1 	--	'HOUR'
			))))
	;

	RETURN @.H;
END $$
DELIMITER ;

DELIMITER $$ 
/*
select _H_to_STR(-4.5-10/3600,'');
 - 4 godz 30 min 10 sek  
 _MODE: m - zaokr. do minut, h - zaokr. do peĹ‚nych godzin
 select _H_to_STR(1.3333,'');
*/
DROP FUNCTION IF EXISTS _H_to_STR$$
CREATE DEFINER=`root`@`localhost` FUNCTION _H_to_STR(
	_HOURS DOUBLE(16,4),_MODE VARCHAR(4)
) RETURNS VARCHAR(30)
    NO SQL
    DETERMINISTIC
BEGIN
	SET @.sign=_HOURS<0,
		_HOURS=ABS(IF(_MODE='m',ROUND(_HOURS*60)/60,_HOURS)),
		_HOURS=ABS(IF(_MODE='h',ROUND(_HOURS),_HOURS));
	
	SET @.days=TRUNCATE(_HOURS/24,0),@.hours=TRUNCATE(MOD(_HOURS,24),0),
		@.mins=IF(_MODE IN ('h'),0,TRUNCATE(MOD(_HOURS*60,60),0)),
		@.secs=TRUNCATE(MOD(_HOURS*3600,60),0);
		IF _MODE IN ('h','m') THEN 
            SET @.mins=@.mins+(@.secs>29);
			SET @.secs=0; 
		END IF;
		IF _MODE IN ('h') THEN 
			SET @.hours=@.hours+(@.mins>29);
			SET @.mins=0;
		END IF;
	SET @.Str=TRIM(CONCAT(
		IF(@.sign,'- ',''),
		IF(@.days>0,CONCAT(FORMAT(@.days,0),' dni '),''),
		IF(@.hours>0,CONCAT(FORMAT(@.hours,0),' godz '),''),
		IF(@.mins>0,CONCAT(FORMAT(@.mins,0),' min '),''),
		IF(@.secs>0,CONCAT(FORMAT(@.secs,0),' sek '),'')
	));
	
	RETURN IF(@.Str='','0 godz',@.Str);
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_Month_Fmt`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Month_Fmt`(
	_Month_FMT VARCHAR(150),
	_Month INTEGER
) RETURNS VARCHAR(250)
    NO SQL
    DETERMINISTIC
BEGIN
	SET
		@M =
			IF(_Month_FMT IN ('', 'MM'), LPAD(_Month, 2, '0'),
			CASE 
				WHEN _Month = 1 THEN IF(_Month_FMT = 'MR', 'I', 'sty') 
				WHEN _Month = 2 THEN IF(_Month_FMT = 'MR', 'II', 'lut') 
				WHEN _Month = 3 THEN IF(_Month_FMT = 'MR', 'III', 'mar') 
				WHEN _Month = 4 THEN IF(_Month_FMT = 'MR', 'IV', 'kwi') 
				WHEN _Month = 5 THEN IF(_Month_FMT = 'MR', 'V', 'maj') 
				WHEN _Month = 6 THEN IF(_Month_FMT = 'MR', 'VI', 'cze') 
				WHEN _Month = 7 THEN IF(_Month_FMT = 'MR', 'VII', 'lip') 
				WHEN _Month = 8 THEN IF(_Month_FMT = 'MR', 'VIII', 'sie') 
				WHEN _Month = 9 THEN IF(_Month_FMT = 'MR', 'IX', 'wrz') 
				WHEN _Month = 10 THEN IF(_Month_FMT = 'MR', 'X', 'paz') 
				WHEN _Month = 11 THEN IF(_Month_FMT = 'MR', 'XI', 'lis') 
				WHEN _Month = 12 THEN IF(_Month_FMT = 'MR', 'XII', 'gru') 
			END 
			)
	;
	RETURN @M;
END $$

DROP FUNCTION IF EXISTS `_Day_Month_Fmt`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Day_Month_Fmt`(
	_Month_FMT VARCHAR(150),
	_Day INTEGER,
	_Month INTEGER
) RETURNS VarChar(250)
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN CONCAT(_Day, IF(_Month_FMT = 'MM', '-', ' '), _Month_Fmt(_Month_FMT, _Month));
END $$

DROP FUNCTION IF EXISTS `_DATE_FORMAT_Range`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_DATE_FORMAT_Range`(
	_MODE VARCHAR(150),
	_Start DATETIME,
	_End DATETIME,
	_Month_FMT VARCHAR(150)
) RETURNS VarChar(250)
    NO SQL
    DETERMINISTIC
BEGIN
	/*************************************************************************************************************************
		_DATE_FORMAT_Range('', );
		SPRAWDZ gdzie to wolamy i po co !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	*************************************************************************************************************************/
	SET 
		@_Result = '',
		@OneMonth = FALSE,
		@OneYear = FALSE,
		@OneDay = DATE(_Start) = DATE(_End),
		@Days = 1 + TIMESTAMPDIFF(DAY, _Start, _End)
	;
	IF NOT @OneDay THEN
		SET @OneMonth = (MONTH(_Start) = MONTH(_End)) AND (YEAR(_Start) = YEAR(_End));
	END IF;
	IF NOT @OneMonth THEN
		SET @OneYear = (YEAR(_Start) = YEAR(_End));
	END IF;
	IF @OneDay THEN
		SET @_Result = CONCAT(_Day_Month_Fmt(_Month_FMT, DAY(_Start), MONTH(_Start)), IF(_MODE LIKE '%rok%', CONCAT(' ', YEAR(_End), ' r.'), ''));
	ELSE
		IF @OneMonth THEN
			SET @_Result = CONCAT(DATE_FORMAT(_Start, '%e'), ' do ', _Day_Month_Fmt(_Month_FMT, DAY(_End), MONTH(_End)), IF(_MODE LIKE '%rok%', CONCAT(' ', YEAR(_End), ' r.'), ''));
		ELSE	
			IF @OneYear THEN
				SET @_Result = CONCAT(_Day_Month_Fmt(_Month_FMT, DAY(_Start), MONTH(_Start)), ' do ', _Day_Month_Fmt(_Month_FMT, DAY(_End), MONTH(_End)), IF(_MODE LIKE '%rok%', CONCAT(' ', YEAR(_End), ' r.'), ''));
			ELSE	
				SET @_Result = CONCAT(_Day_Month_Fmt(_Month_FMT, DAY(_Start), MONTH(_Start)), ' ', YEAR(_Start), ' do ', _Day_Month_Fmt(_Month_FMT, DAY(_End), MONTH(_End)), ' ', YEAR(_End));
			END IF;	
		END IF;	
	END IF;	
	
	SET @_Result = CONCAT(IF(_MODE LIKE '%dni%', CONCAT(@Days, 'dn '), ''), '(', @_Result, ')'); 
	RETURN @_Result;
END $$


DROP FUNCTION IF EXISTS `_Day_Month_Year_Fmt2`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Day_Month_Year_Fmt2`(
	_Date DATE,
	_Month_FMT VARCHAR(150),
	_Year_FMT VARCHAR(150)
) RETURNS VarChar(100)
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN CONCAT(DAY(_Date), IF(_Month_FMT = 'MM', '-', ' '), _Month_Fmt(_Month_FMT, MONTH((_Date))), 
	IFNULL(CASE WHEN _Year_FMT = 'YYYY' THEN YEAR(_Date) WHEN _Year_FMT = 'YY' THEN LPad(YEAR(_Date),2,'0') ELSE YEAR(_Date) END, ''));
END $$

DROP FUNCTION IF EXISTS `_Day_Month_Year_Fmt`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Day_Month_Year_Fmt`(
	_Date DATE,
	_FMT VARCHAR(150)
) RETURNS VARCHAR(100)
    NO SQL
    DETERMINISTIC
BEGIN
set @x = '1';
	IF IFNULL(_Date, '0000-00-00') = '0000-00-00' THEN
		RETURN '';
	ELSE	
		SET @_FMT = UPPER(_FMT);
		SET @S = @_FMT;
		IF (@_FMT LIKE '%@Y%') OR (@_FMT LIKE '%@M%') OR (@_FMT LIKE '%@D%') THEN
		--	np ...<SHORT.Y> albo czÄ™ste <SHORT.MY> oraz <SHORT.D> etc
			/*
			IF _FMT LIKE '%SHORT.D%' THEN 
				SET @.Short.Day = TRUE;
			ELSEIF _FMT LIKE '%SHORT.M%' THEN 
				SET @.Short.Month = TRUE;
			ELSEIF _FMT LIKE '%SHORT.Y%' THEN 
				SET @.Short.Year = TRUE
			END IF;
			*/
set @x = '1.2.1';
			SET @.Hide.Day = (@_FMT LIKE '%@D%') AND (_Date = CURRENT_DATE());
			SET @.Hide.Month = @.Hide.Day OR ((@_FMT LIKE '%@M%')  AND ((MONTH(_Date) = MONTH(CURRENT_DATE())) AND (YEAR(_Date) = YEAR(CURRENT_DATE()))));
			SET @.Hide.Year = @.Hide.Month OR ((@_FMT LIKE '%@Y%') AND ((YEAR(_Date) = YEAR(CURRENT_DATE()))));

			SET @_FMT = REPLACE(REPLACE(REPLACE(@_FMT, '@D', ''), '@M', ''), '@Y', '');
set @x = '1.2.2';
		ELSE
			SET
				@.Hide.Day = FALSE,
				@.Hide.Month = FALSE,
				@.Hide.Year = FALSE
			;
		END IF;
set @x = '2';
		IF (LOCATE('ddd', @_FMT) > 0) OR (LOCATE('DDD', @_FMT) > 0) THEN
set @x = '2.1';
			SET @DayOfWeekStr.1 = 
				ELT(WEEKDAY(_Date) + 1, 'Pn', 'Wt', 'Ĺšr', 'Cz', 'Pt', 'So', 'Nd')
			;
			/*
			-- SET @DayOfWeek = DAYOFWEEK(_Date);
			SET @DayOfWeek = WEEKDAY(_Date) + 1;
			SET @DayOfWeekStr = 
			CASE @DayOfWeek
			WHEN 1 THEN 'Pn' WHEN 2 THEN 'Wt' WHEN 3 THEN 'Ĺšr' WHEN 4 THEN 'Cz' WHEN 5 THEN 'Pt' WHEN 6 THEN 'So' WHEN 7 THEN 'Nd' 
			END;
			-- SET @_FMT = REPLACE(@_FMT, 'DDD', @DayOfWeekStr);
			*/
			SET @_FMT = REPLACE(@_FMT, 'DDD', @DayOfWeekStr.1);
			
			
			
		END IF;
		SET @_Dzisiaj = _VarStr('dzisiaj');
		SET @_FMT = _VarStr(@_FMT);
		SET @_FMT = REPLACE(@_FMT, 'DD', IF(@.Hide.Day, @_Dzisiaj, '%d'));
		SET @_FMT = REPLACE(@_FMT, 'D', IF(@.Hide.Day, @_Dzisiaj, '%e'));
		SET @_FMT = REPLACE(@_FMT, '%E', IF(@.Hide.Day, @_Dzisiaj, '%e'));
		-- -- SET @_FMT = REPLACE(@_FMT, '%XXXX', '%e');
		SET @_FMT = REPLACE(@_FMT, 'YYYY', IF(@.Hide.Year, '', '%Y'));
		SET @_FMT = REPLACE(@_FMT, 'YYY', IF(@.Hide.Year, '', '%Y'));
		SET @_FMT = _VarStr(REPLACE(@_FMT, 'YY', IF(@.Hide.Year, '', '%y')));
		SET @_FMT = REPLACE(_VarStr(@_FMT), 'MR', IF(@.Hide.Month, '', _VarStr(_Month_Fmt('MR', _VarStr(MONTH(_Date))))));
		SET @_FMT = REPLACE(@_FMT, 'MMM', IF(@.Hide.Month, '', _VarStr(_Month_Fmt('MMM', MONTH(_Date)))));
		SET @_FMT = REPLACE(@_FMT, 'MM', IF(@.Hide.Month, '', _VarStr(_Month_Fmt('MM', MONTH(_Date)))));
		SET @S = TRIM(DATE_FORMAT(_Date, @_FMT));
		RETURN @S;
	END IF;
END $$

/*
TODO: nie działa
*/

DROP FUNCTION IF EXISTS `_Fmt_DateTime_QUICK`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Fmt_DateTime_QUICK`(
	_DateTime DATETIME,
	_FMT VARCHAR(150)
) RETURNS VARCHAR(150)
    NO SQL
    DETERMINISTIC
BEGIN
		SET @_FMT = _FMT;
		SET @_FMT = REPLACE(@_FMT, 'YYYY', '%Y');
		SET @_FMT = REPLACE(@_FMT, 'YY', '%y');
		SET @_FMT = REPLACE(@_FMT, 'MMMM', '%M');
		SET @_FMT = REPLACE(@_FMT, 'MMM', '%b');
		SET @_FMT = REPLACE(@_FMT, 'MM', '%m');
		SET @_FMT = REPLACE(@_FMT, 'DDD', '%a');	--	%a	Abbreviated weekday name (Sun..Sat)
		SET @_FMT = REPLACE(@_FMT, 'D', '%e');		--	%e	Day of the month, numeric (0..31)
		SET @_FMT = REPLACE(@_FMT, 'DD', '%d');		--	%d	Day of the month, numeric (00..31)
		SET @_FMT = REPLACE(@_FMT, 'MR', ELT(MONTH(_DateTime), 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII'));
		SET @S = TRIM(DATE_FORMAT(_DateTime, @_FMT));
		RETURN @S;
END $$

/*
TODO: nie działa
*/

DROP FUNCTION IF EXISTS `_Fmt_DateTime_QUICK2`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Fmt_DateTime_QUICK2`(
	_DateTime DATETIME,
	_FMT VARCHAR(150)
) RETURNS VARCHAR(150)
    NO SQL
    DETERMINISTIC
BEGIN
		SET @_FMT = _FMT;
		SET @_FMT = REPLACE(@_FMT, 'MR', ELT(MONTH(_DateTime), 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII'));
		SET @S = TRIM(DATE_FORMAT(_DateTime, @_FMT));
		RETURN @S;
END $$

/*
	_Fmt_DateTime
	to nowe funkcje formatujace
*/
DROP FUNCTION IF EXISTS `_Fmt_DateTime`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Fmt_DateTime`(
	_DateTime DATETIME,
	_FMT VARCHAR(150)
) RETURNS VARCHAR(150)
    NO SQL
    DETERMINISTIC
BEGIN
	DECLARE _Date DATE;
/*
	Oznaczenia
	dla 2020-01-06 08:45:02	
	YY			20
	YYYY		2020
	MM			01
	M			1
	MMM			sty
	MMMM		stycznia
	MR			I  ( II, III, IV, ...)
	D			6
	DD			06
	DDD			pon
	DDDD		poniedzialek
	H			8  godzina
	HH			08  godzina
	N			45  minuty
	NN			45  minuty
	S			2  sekundy
	SS			02  sekundy
*/	
	SET lc_time_names = 'pl_PL';
	IF 
	(IFNULL(_DateTime, '0000-00-00 00:00:00') = '0000-00-00 00:00:00')
	OR
	(IFNULL(DATE(_DateTime), '0000-00-00') = '0000-00-00')
	OR
	NULLIF(_FMT, '') IS NULL
	THEN
		RETURN '';
	ELSE	
		SET _Date = DATE(_DateTime);
		SET @_FMT = UPPER(_FMT);
		SET @S = @_FMT;
		IF (@_FMT LIKE '%@Y%') OR (@_FMT LIKE '%@M%') OR (@_FMT LIKE '%@D%') THEN
		--	np ...<SHORT.Y> albo czÄ™ste <SHORT.MY> oraz <SHORT.D> etc
			/*
			IF _FMT LIKE '%SHORT.D%' THEN 
				SET @.Short.Day = TRUE;
			ELSEIF _FMT LIKE '%SHORT.M%' THEN 
				SET @.Short.Month = TRUE;
			ELSEIF _FMT LIKE '%SHORT.Y%' THEN 
				SET @.Short.Year = TRUE
			END IF;
			*/
			SET @.Hide.Day = (@_FMT LIKE '%@D%') AND (_Date = CURRENT_DATE());
			SET @.Hide.Month = @.Hide.Day OR ((@_FMT LIKE '%@M%')  AND ((MONTH(_Date) = MONTH(CURRENT_DATE())) AND (YEAR(_Date) = YEAR(CURRENT_DATE()))));
			SET @.Hide.Year = @.Hide.Month OR ((@_FMT LIKE '%@Y%') AND ((YEAR(_Date) = YEAR(CURRENT_DATE()))));
			IF (@_FMT LIKE '%@YL%') AND @.Hide.Year AND NOT @.Hide.Month THEN
			--	zadanie ukrycia roku ale tylko dla daty nie wiekszej nic 1 m-c w przod
			--	UWAGA gdy jest to ten sam rok ALE jesli jest to ten sam dzien lub miesiac to nie zawracaj glowy
			--	ustaw nowa wartosc @.Hide.Year tylko dla odp. dat.
				SET @.Hide.Year = (MONTH(_Date) - MONTH(CURRENT_DATE()) <= 1);
			END IF;

			SET @_FMT = _VarStr(REPLACE(REPLACE(REPLACE(REPLACE(@_FMT, '@D', ''), '@M', ''), '@YL', ''), '@Y', ''));
		ELSE
			SET
				@.Hide.Day = FALSE,
				@.Hide.Month = FALSE,
				@.Hide.Year = FALSE
			;
		END IF;
		SET @_Dzisiaj = _VarStr('dzisiaj');
		-- SET @_FMT = REPLACE(@_FMT, '%a', _VarStr(ELT(WEEKDAY(_Date) + 1, 'Pn', 'Wt', 'Ĺšr', 'Cz', 'Pt', 'So', 'Nd')));
		
		set @_FMT.tmp = @_FMT;
		/*
		IF (LOCATE('ddd', @_FMT) > 0) OR (LOCATE('DDD', @_FMT) > 0) THEN
			-- SET @DayOfWeek = DAYOFWEEK(_Date);
			SET @DayOfWeek = WEEKDAY(_Date) + 1;
			SET @DayOfWeekStr = _VarStr(
			CASE @DayOfWeek
			WHEN 1 THEN 'Pn' WHEN 2 THEN 'Wt' WHEN 3 THEN 'Ĺšr' WHEN 4 THEN 'Cz' WHEN 5 THEN 'Pt' WHEN 6 THEN 'So' WHEN 7 THEN 'Nd' 
			END
			);
			SET @_FMT = REPLACE(@_FMT, 'DDD', @DayOfWeekStr);
		END IF;
		*/
	--	Rok	
		-- -- SET @_FMT = REPLACE(@_FMT, '%XXXX', '%e');
		SET @_FMT = REPLACE(@_FMT, 'YYYY', IF(@.Hide.Year, '', '%Y'));		--	2020
		SET @_FMT = REPLACE(@_FMT, 'YYY', IF(@.Hide.Year, '', '%Y'));		--	2020
		SET @_FMT = REPLACE(@_FMT, 'YY', IF(@.Hide.Year, '', '%y'));		--	20
		
	--	Miesiac
		-- bylo SET @_FMT = REPLACE(@_FMT, 'MR', IF(@.Hide.Month, '', _VarStr(_Month_Fmt('MR', MONTH(_Date)))));
		SET @_FMT = REPLACE(@_FMT, 'MR', _VarStr(IF(@.Hide.Month, '', _VarStr(ELT(MONTH(_Date), 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII')))));
		-- bylo SET @_FMT = REPLACE(@_FMT, 'MMM', IF(@.Hide.Month, '', _VarStr(_Month_Fmt('MMM', MONTH(_Date)))));
		SET @_FMT = REPLACE(@_FMT, 'MMM', IF(@.Hide.Month, '', '%b'));	--	sty
		-- bylo SET @_FMT = REPLACE(@_FMT, 'MM', IF(@.Hide.Month, '', _VarStr(_Month_Fmt('MM', MONTH(_Date)))));
		SET @_FMT = REPLACE(@_FMT, 'MM', IF(@.Hide.Month, '', '%m'));
	--	godziny
		SET @_FMT = REPLACE(REPLACE(@_FMT, 'HH', 'H'), 'H', '%H');
	--	minuty
		SET @_FMT = REPLACE(REPLACE(@_FMT, 'NN', 'N'), 'N', '%i');
	--	sekundy
		SET @_FMT = REPLACE(REPLACE(@_FMT, 'SS', 'S'), 'S', '%S');
	--	Dni	
	--	Dni	ROWOJ
		SET @_FMT = REPLACE(@_FMT, 'DDD', IF(@.Hide.Day, '', 'UUU'));
		IF @_FMT LIKE '%UUU%' THEN
		/*
			SET @DayOfWeek.Str.2 = 
				CASE WEEKDAY(_Date) + 1
				WHEN 1 THEN 'Pn' WHEN 2 THEN 'Wt' WHEN 3 THEN 'Ĺšr' WHEN 4 THEN 'Cz' WHEN 5 THEN 'Pt' WHEN 6 THEN 'So' WHEN 7 THEN 'Nd' 
				END
			;
		*/
			SET @DayOfWeek.Str.1 = ELT(WEEKDAY(_Date) + 1, 'Pn', 'Wt', 'Ĺšr', 'Cz', 'Pt', 'So', 'Nd');
		END IF;
		SET @_FMT = REPLACE(@_FMT, 'DD', IF(@.Hide.Day, @_Dzisiaj, '%d'));	--	08 sty
		SET @_FMT = REPLACE(@_FMT, 'D', IF(@.Hide.Day, @_Dzisiaj, '%e'));	--	8 sty
		SET @_FMT = REPLACE(@_FMT, '%E', IF(@.Hide.Day, @_Dzisiaj, '%e'));	--	8 sty
		SET @_FMT = REPLACE(@_FMT, 'UUU', @DayOfWeek.Str.1);
		
		SET @.Fmt_DateTime = REPLACE(REPLACE(TRIM(DATE_FORMAT(_DateTime, @_FMT)), '  ', ' '), '  ', ' ');
		RETURN @.Fmt_DateTime;
	END IF;

END $$



DROP FUNCTION IF EXISTS `_DATE_Range`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_DATE_Range`(
	_MODE VARCHAR(150),
	_Start DATE,
	_End DATE,
	_Month_FMT VARCHAR(150),
	_Year_Invisible INTEGER
) RETURNS VarChar(250)
    NO SQL
    DETERMINISTIC
BEGIN
	/*
	
	*/
	SET
		_Start = IFNULL(_Start, '0000-00-00'),
		@_Start = IFNULL(_Start, '0000-00-00'),
		_End = IFNULL(_End, '0000-00-00')
	;	
	SET 
		@_Result = '',
		@_StartIs_0 = (_Start IS NULL) OR (_Start = '0000-00-00'),
		@_EndIs_0 = (_End = '0000-00-00'),
		@OneMonth = FALSE,
		@OneYear = FALSE,
		@OneDay = DATE(_Start) = DATE(_End),
		@Days = 1 + TIMESTAMPDIFF(DAY, _Start, _End)
	;
	SET @Days = IFNULL(@Days, 0);
	
	IF NOT @_StartIs_0 AND NOT @_EndIs_0 THEN
		IF NOT @OneDay THEN
			SET @OneMonth = (MONTH(_Start) = MONTH(_End)) AND (YEAR(_Start) = YEAR(_End));
		END IF;
		IF NOT @OneMonth THEN
			SET @OneYear = (YEAR(_Start) = YEAR(_End));
		END IF;
	END IF;
	
	SET @FmtDay = IF(_Month_FMT = 'MM', 'D-', 'D ');
	
	IF @_StartIs_0 AND @_EndIs_0 THEN
		SET @_Result = '';
	ELSE
	IF @_StartIs_0 THEN
		SET @_Result = CONCAT('do ', 
			_Day_Month_Year_Fmt(_End, CONCAT(@FmtDay, _Month_FMT, IF(_Year_Invisible, '', ' YYYY')))
		)	
		;
	ELSE	
	IF @_EndIs_0 THEN
		SET @_Result = CONCAT('od ', 
			_Day_Month_Year_Fmt(_Start, CONCAT(@FmtDay, _Month_FMT, IF(_Year_Invisible, '', ' YYYY')))
		)	
		;
	ELSE
	IF @OneDay THEN
		SET @_Result = 
			_Day_Month_Year_Fmt(_Start, CONCAT(@FmtDay, _Month_FMT, IF(_Year_Invisible, '', ' YYYY')))
		;
	ELSE
		IF @OneMonth THEN
			SET @_Result = CONCAT(
				_Day_Month_Year_Fmt(_Start, '%e'),
				' do ',
				_Day_Month_Year_Fmt(_End, CONCAT(@FmtDay, _Month_FMT, IF(_Year_Invisible, '', ' YYYY')))
			)	
			;
		ELSE	
			IF @OneYear THEN
				SET @_Result = CONCAT(
					_Day_Month_Year_Fmt(_Start, CONCAT(@FmtDay, _Month_FMT, '')),
					' do ',
					_Day_Month_Year_Fmt(_End, CONCAT(@FmtDay, _Month_FMT, IF(_Year_Invisible, '', ' YYYY')))
				)	
				;
			ELSE	
				SET @_Result = CONCAT(
					_Day_Month_Year_Fmt(_Start, CONCAT(@FmtDay, _Month_FMT, IF(_Year_Invisible, '', ' YYYY'))),
					' do ',
					_Day_Month_Year_Fmt(_End, CONCAT(@FmtDay, _Month_FMT, IF(_Year_Invisible, '', ' YYYY')))
				)	
				;
			END IF;	
		END IF;	
	END IF;	
	END IF;
	END IF;
	END IF;
	
	SET @_Result = CONCAT(IF((_MODE LIKE '%dni%') AND (@Days > 1), CONCAT(_VarStr(@Days), ' dni '), ''), '(', @_Result, ')'); 
	RETURN @_Result;
END $$


DROP FUNCTION IF EXISTS `_Fmt_DATE_Range`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Fmt_DATE_Range`(
	_MODE VARCHAR(150),
	_Start DATE,
	_End DATE,
	_Month_FMT VARCHAR(150),
	_Year_Invisible INTEGER
) RETURNS VarChar(250)
    NO SQL
    DETERMINISTIC
BEGIN
	/*
	
	*/
	SET
		_Start = IFNULL(_Start, '0000-00-00'),
		@_Start = IFNULL(_Start, '0000-00-00'),
		_End = IFNULL(_End, '0000-00-00')
	;	
	SET 
		@_Result = '',
		@_StartIs_0 = (_Start = '0000-00-00'),
		@_EndIs_0 = (_End = '0000-00-00'),
		@OneMonth = FALSE,
		@OneYear = FALSE,
		@OneDay = DATE(_Start) = DATE(_End),
		@Days = 1 + TIMESTAMPDIFF(DAY, _Start, _End)
	;
	SET @Days = IFNULL(@Days, 0);
	
	IF NOT @_StartIs_0 AND NOT @_EndIs_0 THEN
		IF NOT @OneDay THEN
			SET @OneMonth = (MONTH(_Start) = MONTH(_End)) AND (YEAR(_Start) = YEAR(_End));
		END IF;
		IF NOT @OneMonth THEN
			SET @OneYear = (YEAR(_Start) = YEAR(_End));
		END IF;
	END IF;
	
	SET @FmtDay = IF(_Month_FMT = 'MM', 'D-', 'D ');
	SET @FmtDefaultYYYY = CONCAT(@FmtDay, _Month_FMT, IF(_Year_Invisible, '', ' YYYY'));
	SET @FmtDefault = CONCAT(@FmtDefaultYYYY, '@YL');	--	@Y oznacza ze gdy wtstapi rok i jest to rok biezacy to zniknie
	
	IF @_StartIs_0 AND @_EndIs_0 THEN
		SET @_Result = '';
	ELSEIF @_StartIs_0 THEN
		SET @_Result = CONCAT('do ', 
			_Fmt_DateTime(_End, @FmtDefault)
		)	
		;
	ELSEIF @_EndIs_0 THEN
		SET @_Result = CONCAT('od ', 
			_Fmt_DateTime(_Start, @FmtDefault)
		)	
		;
	ELSEIF @OneDay THEN
	--	oba dni wystepuja ale ....
	--	jest to tylko 1 dzien !!!
		SET @_Result = 
			_Fmt_DateTime(_Start, @FmtDefault)
		;
	ELSEIF @OneMonth THEN
	--	oba dni wystepuja ale ....
	--	w obrebie 1 m-ca
			SET @_Result = CONCAT(
				_Fmt_DateTime(_Start, 'D'),
				' do ',
				_Fmt_DateTime(_End, @FmtDefault)
			)	
			;
	ELSEIF @OneYear THEN
			SET @_Result = CONCAT(
				_Fmt_DateTime(_Start, CONCAT(@FmtDay, _Month_FMT, '')),
				' do ',
				_Fmt_DateTime(_End, @FmtDefault)
			)	
			;
	ELSE	
			SET @_Result = CONCAT(
				_Fmt_DateTime(_Start, @FmtDefault),
				' do ',
				_Fmt_DateTime(_End, @FmtDefaultYYYY)
			)	
			;
	END IF;
	
	SET @_Result = CONCAT(IF((_MODE LIKE '%dni%') AND (@Days > 1), CONCAT(_VarStr(@Days), ' dni '), ''), '(', @_Result, ')'); 
	RETURN @_Result;
END $$

DROP FUNCTION IF EXISTS `_Fmt_DateTime_Range`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Fmt_DateTime_Range`(
	_Start DATETIME,
	_End  DATETIME,
	_FMT VARCHAR(150)
) RETURNS VarChar(250)
    NO SQL
    DETERMINISTIC
BEGIN
	/*
	Najnowsza - odwolujaca sie to takze najnowsze `_Fmt_DateTime`
	*/
	-- SET @_Start = _Start, @_End = _End;	
	SET _FMT = REPLACE(REPLACE(REPLACE(_FMT, '<MR>', 'D MR YYYY @Y@D'), '<MMM>', 'D MMM YYYY @Y@D'), '<dni>', 'dni');
	SET 
		@_Result = '',
		@_StartIs_0 = DATE(IFNULL(_Start, '0000-00-00 00:00:00')) = '0000-00-00',
		@_EndIs_0 = DATE(IFNULL(_End, '0000-00-00 00:00:00')) = '0000-00-00',
		@OneDay = DATE(_Start) <=> DATE(_End),
		@Show_Dni = _FMT LIKE '%dni%',
		@Days = 1 + TIMESTAMPDIFF(DAY, _Start, _End)
	;
	set @.fmt.x = _FMT;
	SET _FMT = REPLACE(_FMT, 'dni', '');

	IF @_StartIs_0 AND @_EndIs_0 THEN
		SET @_Result = '';
	ELSEIF @_StartIs_0 THEN
		SET @_Result = CONCAT('do ', 
			_Fmt_DateTime(_End, _FMT)
		)	
		;
	ELSEIF @_EndIs_0 THEN
		SET @_Result = CONCAT('od ', 
			_Fmt_DateTime(_Start, _FMT)
		)	
		;
	ELSE
	--	mamy obie daty - spr czy 1 dzien
		SET @OneMonth = NOT @OneDay AND (MONTH(_Start) = MONTH(_End)) AND (YEAR(_Start) = YEAR(_End));
		SET @OneYear = NOT @OneDay AND NOT @OneMonth AND (YEAR(_Start) = YEAR(_End));
		IF @OneDay THEN
		--	oba dni wystepuja ale .... jest to tylko 1 dzien !!!
			SET @_Result = 
				_Fmt_DateTime(_Start, _FMT)
			;
		ELSE
			SET @_FMT_End = _FMT;
			IF @OneMonth THEN
				SET @_FMT_Start = REPLACE(REPLACE(REPLACE(_FMT, 'MR', ''), 'MMM', ''), 'MM', '');
			ELSEIF @OneYear THEN
				SET @_FMT_Start = REPLACE(REPLACE(_FMT, 'YY', ''), 'YYYY', '');
			ELSE
				SET @_FMT_Start = _FMT;
			END IF;
		--	zwracamy	
			SET @_Result = CONCAT(
				_Fmt_DateTime(_Start, @_FMT_Start),
				' do ',
				_Fmt_DateTime(_End, @_FMT_End)
			)	
			;
		/*	
		ELSEIF @OneMonth THEN
		--	oba dni wystepuja ale ....
		--	w obrebie 1 m-ca
				SET @_Result = CONCAT(
					_Fmt_DateTime(_Start, REPLACE(REPLACE(REPLACE(_FMT, 'MR', ''), 'MMM', ''), 'MM', '')),
					' do ',
					_Fmt_DateTime(_End, _FMT)
				)	
				;
		ELSEIF @OneYear THEN
				SET @_Result = CONCAT(
					_Fmt_DateTime(_Start, REPLACE(REPLACE(_FMT, 'YY', ''), 'YYYY', '')),
					' do ',
					_Fmt_DateTime(_End, _FMT)
				)	
				;
		ELSE	
				-- set @_FMT.1 = _FMT;
				SET @_Result = CONCAT(
					_Fmt_DateTime(_Start, _FMT),
					' do ',
					_Fmt_DateTime(_End, _FMT)
				)	
				;
		*/
		END IF;
	END IF;
	
	SET
		@_Result = 
			CONCAT_WS('',
				IF(@Show_Dni AND @Days > 0, 
					CONCAT(NULLIF(@Days, 1), ' dni '), 
					NULL
				),
				-- IF(@BezNaw, NULL, '('),
				'(',
				NULLIF(@_Result, ''),
				-- IF(@BezNaw, NULL, ')')
				')'
			)
	;
	RETURN @_Result;
END $$

DROP FUNCTION IF EXISTS `_Get_Next_WorkDay`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Get_Next_WorkDay`(_From DateTime) RETURNS DateTime
    NO SQL
    DETERMINISTIC
BEGIN
	DECLARE _D DateTime;
	DECLARE _I Integer;
	
	SET _I = 0;
	-- SET _D = DATE_ADD(_From INTERVAL 1 DAY);
	-- SET _D = _From + INTERVAL 1 DAY;
	SET _D = _From;
	
	WHILE (DayOfWeek(_D) = 1) AND (_I < 100) DO	
	  -- SET _D = DATE_ADD(_D INTERVAL 1 DAY);
	  SET _D = _D + INTERVAL 1 DAY;
	  SET _I = _I + 1;
	END WHILE;
	
   /* while not CzyDzienRoboczy(Result) and (I < 100) do
    begin
      Result := Result + 1;
      inc(I);
    end; */
	
	RETURN _D;
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_varDATE`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_varDATE`(_Value DATE) RETURNS DATE
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN _Value;
END $$
DELIMITER ;

DELIMITER $$ 
/**** 1 dzieĹ„ m-ca daty wej. ****/
DROP FUNCTION IF EXISTS `_FirstDayM`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_FirstDayM`(_D DATE) RETURNS DATE
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN LAST_DAY(_D - INTERVAL 1 MONTH) + INTERVAL 1 DAY;
END $$
DELIMITER ;

DELIMITER $$ 
/**** ost. dzieĹ„ m-ca daty wej. ****/
DROP FUNCTION IF EXISTS `_LastDayM`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_LastDayM`(_D DATE) RETURNS DATE
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN LAST_DAY(_D);
END $$
DELIMITER ;

DELIMITER $$ 
/**** 1 dzieĹ„ roku daty wej. ****/
DROP FUNCTION IF EXISTS `_FirstDayY`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_FirstDayY`(_D DATE) RETURNS DATE
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN MAKEDATE(YEAR(_D), 1);
END $$
DELIMITER ;

DELIMITER $$ 
/**** ost. dzieĹ„ roku daty wej. ****/
DROP FUNCTION IF EXISTS `_LastDayY`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_LastDayY`(_D DATE) RETURNS DATE
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN LAST_DAY(MAKEDATE(YEAR(_D), 360));
END $$
DELIMITER ;

DELIMITER $$ 
/**** 1 dzieĹ„ roku wej. ****/
DROP FUNCTION IF EXISTS `_FirstDayYYYY`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_FirstDayYYYY`(_YYYY INTEGER) RETURNS DATE
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN MAKEDATE(_YYYY, 1);
END $$
DELIMITER ;

DELIMITER $$ 
/**** ost. dzieĹ„ roku wej. ****/
DROP FUNCTION IF EXISTS `_LastDayYYYY`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_LastDayYYYY`(_YYYY INTEGER) RETURNS DATE
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN LAST_DAY(MAKEDATE(_YYYY, 360));
END $$
DELIMITER ;

DELIMITER $$ 
/**** BEĹ�KOT - powt. NIE UĹ»YWANA NIGDZIE  ****/
DROP FUNCTION IF EXISTS `_FirstDay`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_FirstDay`(_D DATE) RETURNS DATE
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN STR_TO_DATE(Concat(YEAR(_D),'-',month(_D),'-01'),'%Y-%m-%d');
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_MonthStart`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_MonthStart`(_shift int) RETURNS DATE
    NO SQL
    DETERMINISTIC
BEGIN
	DECLARE _D Date;

	SET _D=DATE_ADD(CURRENT_DATE,interval _shift MONTH);
	RETURN STR_TO_DATE(Concat(YEAR(_D),'-',month(_D),'-01'),'%Y-%m-%d');
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_MonthEnd`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_MonthEnd`(_shift int) RETURNS DATE
    NO SQL
    DETERMINISTIC
BEGIN
	RETURN LAST_DAY(DATE_ADD(CURRENT_DATE,interval _shift MONTH));
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `Get_Status_For_Valid_From_To`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `Get_Status_For_Valid_From_To`(
	_MODE VARCHAR(255),
	_IN_Date DATE,
	_Current_Status VARCHAR(100),
	_Deleted BOOLEAN,
	_Valid_From DATETIME,
	_Valid_To DATETIME,
	_ParamA VARCHAR(250),
	_ParamB VARCHAR(250),
	_ParamC VARCHAR(250)
) 
RETURNS VARCHAR(256)
    NO SQL
    DETERMINISTIC
S_Get_Status_For_Valid_From_To: BEGIN
	/*************************************************************************************************************************
		Get_Status_For_Valid_From_To('', NULL, '', go.Deleted, go.Valid_From, go.Valid_To, '', '', '') = 'OK'
	*************************************************************************************************************************/
	IF _MODE IN ('', 'GET', 'GET.FULL') THEN
		SET 
			_IN_Date = IFNULL(NULLIF(NULLIF(_IN_Date, ''), '0000-00-00'), CURRENT_DATE()),
			_Valid_From = IFNULL(_Valid_From, '2000-01-01'),
			_Valid_To = IFNULL(_Valid_To, '2100-12-31'),
			@.Valid_From_To.From = NULL,
			@.Valid_From_To.To = NULL,
			@.Valid_From_To.Any = NULL,
			@.Valid_From_To._Msg = NULL
		;
		-- SET @_IN_Date =_IN_Date;
		SET @.OUT.Status = 
			IF(_Deleted, 
				'deleted',
				IF(_IN_Date BETWEEN IFNULL(_Valid_From, '2000-01-01') AND IFNULL(_Valid_To, '2100-12-31'),
					'OK',
					IF(_IN_Date < IFNULL(_Valid_From, '2000-01-01'),
						'wait',
						'old'
					)
				)
			)
		;
		IF _MODE = 'GET.FULL' AND @.OUT.Status <> 'OK' THEN
		--	w pewnych wypadkach chcemy mieÄ‡ po spr. w zmiennych opis 	
			SET 
				@.Valid_From_To.Format = IFNULL(NULLIF(_ParamA, ''), '#Date##Date#'),	-- "cos #TextDate#"
				_ParamB = IFNULL(NULLIF(_ParamB, ''), '#Date##Date#'),	-- "cos #TextDate#"
				@.Valid_From_To.From = _Fmt_DateTime(_Valid_From, 'DDD D MMM YYYY'),
				@.Valid_From_To.To = _Fmt_DateTime(_Valid_To, 'DDD D MMM YYYY')
			;
			SET 
				@.Valid_From_To.Any = IF(@.OUT.Status = 'wait', @.Valid_From_To.From, IF(@.OUT.Status = 'old', @.Valid_From_To.To, '')),
				@.Valid_From_To.Status.Str = IF(@.OUT.Status = 'wait', IFNULL(NULLIF(_ParamB, ''), 'waĹĽne od '), IF(@.OUT.Status = 'old', IFNULL(NULLIF(_ParamC, ''), 'waĹĽne do '), ''))
			;
			
			SET	
				@.Valid_From_To._Msg = REPLACE(REPLACE(REPLACE(REPLACE(_ParamA,
					'#Delete#', IF(@.OUT.Status = 'deleted', 'usuniÄ™to', '')),
					'#Delete2#', IF(@.OUT.Status = 'deleted', 'zostaĹ‚ usuniÄ™ty', '')),
					'#Date#', IF(@.OUT.Status IN ('wait', 'old'), @.Valid_From_To.Any, '')),
					'#Text#', @.Valid_From_To.Status.Str)
			;	
			SET @.OUT.Status = CONCAT_WS('::', @.OUT.Status, @.Valid_From_To._Msg);
		END IF;
		RETURN @.OUT.Status;
	END IF;
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `Get_Status_For_Valid_From_To_Str`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `Get_Status_For_Valid_From_To_Str`(
	_MODE VARCHAR(255),
	_IN_Date DATE,
	_Current_Status VARCHAR(100),
	_Deleted BOOLEAN,
	_Valid_From DATETIME,
	_Valid_To DATETIME,
	_IF_OK VARCHAR(250),
	_IF_Deleted VARCHAR(250),
	_IF_Canceled VARCHAR(250),
	_IF_Wait VARCHAR(250),
	_IF_Old VARCHAR(250),
	_IF_Infinity VARCHAR(250),
	_ParamC VARCHAR(250)
) 
RETURNS VARCHAR(256)
    NO SQL
    DETERMINISTIC
S_Get_Status_For_Valid_From_To: BEGIN
	/*************************************************************************************************************************
		Get_Status_For_Valid_From_To_Str('', NULL, '', go.Deleted, go.Valid_From, go.Valid_To, '', '', '') = 'OK'
	*************************************************************************************************************************/
	IF _MODE IN ('', 'GET', 'GET.FULL') THEN
		SET 
			_IN_Date = IFNULL(NULLIF(NULLIF(_IN_Date, ''), '0000-00-00'), CURRENT_DATE()),
			_Valid_From = IFNULL(_Valid_From, '2000-01-01'),
			_Valid_To = IFNULL(_Valid_To, '2100-12-31')
		;
		-- SET @_IN_Date =_IN_Date;
		SET @.OUT.Status = 
			IF(_Deleted = 1 OR _Current_Status = 1, 
				'deleted',
				IF(_Current_Status = 2, 
					'Canceled',
					IF(_Valid_From = '0000-00-00' AND _Valid_To = '2100-12-31',
						'Infinity',
						IF(_IN_Date BETWEEN IFNULL(_Valid_From, '2000-01-01') AND IFNULL(_Valid_To, '2100-12-31'),
							'OK',
							IF(_IN_Date < IFNULL(_Valid_From, '2000-01-01'),
								'wait',
								'old'
							)
						)
					)
				)
			)
		;
		SET @.OUT._Msg_Fmt =
		CASE @.OUT.Status 
		WHEN 'OK' THEN _IF_OK
		WHEN 'deleted' THEN _IF_Deleted
		WHEN 'Canceled' THEN _IF_Canceled
		WHEN 'Wait' THEN _IF_Wait
		WHEN 'Old' THEN _IF_Old
		WHEN 'Infinity' THEN _IF_Infinity
		END;
		SET @.OUT.Msg =
			REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@.OUT._Msg_Fmt, 
				'#OK#', _Fmt_DateTime_Range(_Valid_From, NULLIF(_Valid_To, '2100-12-31 00:00:00'), '<MMM>')),
				'#Delete#', ''),
				'#Canceled#', ''),
				'#Wait#', _Fmt_DateTime(_Valid_From, 'D MMM YYYY @Y@D')),  --   _Fmt_DateTime_Range(_Valid_From, NULLIF(_Valid_To, '2100-12-31 00:00:00'), '<MMM>')),
				'#Old#', _Fmt_DateTime(_Valid_To, 'D MMM YYYY @Y@D')),  --   _Fmt_DateTime_Range(_Valid_From, NULLIF(_Valid_To, '2100-12-31 00:00:00'), '<MMM>')),
				'#Range#', _Fmt_DateTime_Range(_Valid_From, NULLIF(_Valid_To, '2100-12-31 00:00:00'), '<MMM>')),
				'#Infinity#', '')
		;
		RETURN @.OUT.Msg;
	END IF;
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `Get_StartEndDuration`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `Get_StartEndDuration`(
	_MODE VARCHAR(255),
	_IN_StartTime DATETIME,
	_IN_EndTime DATETIME,
	_IN_Duration DOUBLE(16, 2),
	_DurationInterval VARCHAR(250),	--	HOUR (domyslnie), MINUTE, etc 
	_Param_A VARCHAR(250),
	_Param_B VARCHAR(250),
	_Param_C VARCHAR(250)
) 
RETURNS VARCHAR(256)
    NO SQL
    DETERMINISTIC
BEGIN
	SET @.Set_StartEndDuration.Result = NULL;
	SET _DurationInterval = IFNULL(NULLIF(_DurationInterval, ''), 'HOUR');
	IF _MODE = 'Duration<-Start,End' THEN
		SET @.Set_StartEndDuration.Result = 
			IF(_DurationInterval IN ('HOUR', 'H'), 
				ROUND(TIMESTAMPDIFF(MINUTE, _IN_StartTime, _IN_EndTime)/60, 4),
				IF(_DurationInterval IN ('MIN', 'MINUTE', 'M'), 
					ROUND(TIMESTAMPDIFF(SECOND, _IN_StartTime, _IN_EndTime)/60, 4),
					IF(_DurationInterval IN ('SEC', 'SECOND', 'S'), 
						ROUND(TIMESTAMPDIFF(SECOND, _IN_StartTime, _IN_EndTime), 4),
						NULL
					)
				)	
			)
		;
		RETURN 
			IF(_Param_A = 'not null',
				IFNULL(@.Set_StartEndDuration.Result, '0'),
				@.Set_StartEndDuration.Result
			)
		;
	ELSEIF _MODE = 'End<-Start,Duration' THEN
		SET @.Set_StartEndDuration.Result = 
			IF(_DurationInterval IN ('HOUR', 'H'), 
				DATE_ADD(_IN_StartTime, INTERVAL _IN_Duration*60 MINUTE),
				IF(_DurationInterval IN ('MIN', 'MINUTE', 'M'), 
					DATE_ADD(_IN_StartTime, INTERVAL _IN_Duration*60 SECOND),
					IF(_DurationInterval IN ('SEC', 'SECOND', 'S'), 
						DATE_ADD(_IN_StartTime, INTERVAL _IN_Duration SECOND),
						NULL
					)
				)	
			)
		;
		RETURN 
			IF(_Param_A = 'not null',
				IFNULL(@.Set_StartEndDuration.Result, '0000-00-00 00:00:00'),
				@.Set_StartEndDuration.Result
			)
		;
	ELSEIF _MODE = 'Start<-End,Duration' THEN
		SET @.Set_StartEndDuration.Result = 
			IF(_DurationInterval IN ('HOUR', 'H'), 
				DATE_ADD(_IN_EndTime, INTERVAL -_IN_Duration*60 MINUTE),
				IF(_DurationInterval IN ('MIN', 'MINUTE', 'M'), 
					DATE_ADD(_IN_EndTime, INTERVAL -_IN_Duration*60 SECOND),
					IF(_DurationInterval IN ('SEC', 'SECOND', 'S'), 
						DATE_ADD(_IN_EndTime, INTERVAL -_IN_Duration SECOND),
						NULL
					)
				)	
			)
		;
		RETURN 
			IF(_Param_A = 'not null',
				IFNULL(@.Set_StartEndDuration.Result, '0000-00-00 00:00:00'),
				@.Set_StartEndDuration.Result
			)
		;
	END IF;
END $$
DELIMITER ;

DELIMITER $$ 
DROP PROCEDURE IF EXISTS `Set_Var_StartEndDuration`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `Set_Var_StartEndDuration`(
	IN _MODE VARCHAR(150),
	INOUT _IN_StartTime DATETIME,
	INOUT _IN_EndTime DATETIME,
	INOUT _IN_Duration DOUBLE(16, 2),
	IN _IN_StartTime_Changed BOOLEAN,
	IN _IN_EndTime_Changed BOOLEAN,
	IN _IN_Duration_Changed BOOLEAN,
	IN _DurationInterval VARCHAR(250),	--	HOUR (domyslnie), MINUTE, etc 
	IN _Param_A VARCHAR(255),	--	waĹĽne bo tu moĹĽesz dac "not null" i wtedy daty/time = null zwraca jako '0000-00-00 00:00:00'
	IN _Param_B VARCHAR(255)
)
S_frekw_po_Before_Post: BEGIN 
	IF _MODE = '' THEN
		/*
			Zmiana dotyczy:					Zmieniasz
			Start	End		Duration
			  X		  0		  0					End 
			  X		  X		  0					Duration
			  X		  X		  X					Gdy Rodzaj <> Zadanie Duration	Gdy Rodzaj = Zadanie to End - ALE dla bezpieczenstwa EXE na razie NIE
			  X		  0		  X					End
			  
			  0		  X		  0					Duration
			  0		  X		  X					Start
			  0		  0		  X					End
			  
			  0		  0		  0					NIC czli niczego nie zmieniam
		*/
		SET
			@.DO.Change.NEW.Duration = 
				-- na razie nie (_IN_StartTime_Changed AND _IN_EndTime_Changed)	--	X X 0  i  X X X
				(_IN_StartTime_Changed AND _IN_EndTime_Changed AND NOT _IN_Duration_Changed)	--	X X 0  
				OR
				(NOT _IN_StartTime_Changed AND _IN_EndTime_Changed AND NOT _IN_Duration_Changed)	--	0 X 0
			,
			@.DO.Change.NEW.EndTime = 
				(_IN_StartTime_Changed AND NOT _IN_EndTime_Changed)	--	X 0 0  i  X 0 X
				OR
				(NOT _IN_StartTime_Changed AND NOT _IN_EndTime_Changed AND _IN_Duration_Changed)	--	0 0 X
			,
			@.DO.Change.NEW.StartTime = 
				(NOT _IN_StartTime_Changed AND _IN_EndTime_Changed AND _IN_Duration_Changed)	--	0 X X
		;
	--	Gdy X X X  to 	
		IF (_IN_StartTime_Changed AND _IN_EndTime_Changed AND _IN_Duration_Changed) THEN	--	X X X
			SET
				@.DO.Change.NEW.Duration = 
					TRUE -- BLOK (@.NEW.Rodzaj != 8),	--	dla wszystkich innych niz ZADANIE
				-- BLOK @.DO.Change.NEW.EndTime =
					-- BLOK (@.NEW.Rodzaj = 8)	--	Tylko gdy ZADANIE
			;
		END IF;
		
		IF @.DO.Change.NEW.Duration THEN
		--	zmien trwanie
			SET _IN_Duration = Get_StartEndDuration('Duration<-Start,End', _IN_StartTime, _IN_EndTime, _IN_Duration, _DurationInterval, _Param_A, '', '');
		ELSEIF @.DO.Change.NEW.EndTime THEN
		--	zmien koniec
			SET _IN_EndTime = Get_StartEndDuration('End<-Start,Duration', _IN_StartTime, _IN_EndTime, _IN_Duration, _DurationInterval, _Param_A, '', '');
		ELSEIF @.DO.Change.NEW.StartTime THEN
		--	zmien poczatek
			SET _IN_StartTime = Get_StartEndDuration('Start<-End,Duration', _IN_StartTime, _IN_EndTime, _IN_Duration, _DurationInterval, _Param_A, '', '');
		END IF;
		
	END IF;
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_Format_DT`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Format_DT`(
	_DateTime DATETIME,
	_FMT VARCHAR(150)
	
) RETURNS VARCHAR(150)
    NO SQL
    DETERMINISTIC
/******************************************************************************************************************************************
	NAJNOWSZA 16 kwi 2021 
*******************************************************************************************************************************************/	
BEGIN
	DECLARE _Date DATE;
/*
	Oznaczenia
	dla 2021-04-09 12:25:02	
	%YY			20
	%YYYY		2020
	%YYCY		gdy aktualny rok to: b.r. w przeciw. 2020
		gdy @YYYY/@YY etc to chowanie
	%MM			01
	%M			1
	%MMM			sty
	%MMMM		stycznia
	%MR			I  ( II, III, IV, ...)
		gdy @MMM/@MM etc to chowanie
	%D			6
	%DD			06
	%DDD			pon
	%DDDD		poniedzialek
		gdy @DDD/@DD etc to chowanie
	%H			8  godzina
	%HH			08  godzina
	%N			45  minuty
	%NN			45  minuty
	%S			2  sekundy
	%SS			02  sekundy
*/	
	set @.dt.fmt = 'pocz';
	set @.dt.fmt = concat_WS('/', @.dt.fmt, 'ust');
	SET lc_time_names = 'pl_PL';
	IF 
		(IFNULL(_DateTime, '0000-00-00 00:00:00') = '0000-00-00 00:00:00')
		OR
		(IFNULL(DATE(_DateTime), '0000-00-00') = '0000-00-00')
		OR
		NULLIF(_FMT, '') IS NULL
	THEN
		RETURN '';
	ELSE
		SET _Date = (@._Date := DATE(_DateTime));
		-- SET @_FMT = UPPER(_FMT); -- , @_Params = _Params;
		SET @_FMT = _FMT;
		IF 
			(@.Dzisiaj := (_Date = CURRENT_DATE()))
			AND (@_FMT LIKE '%[DZ]%') 
		THEN
			RETURN 'dzisiaj';
		ELSEIF 
			(@.Wczoraj := (_Date = (CURRENT_DATE() - INTERVAL 1 DAY)))
			AND (@_FMT LIKE '%[DW]%') 
		THEN
			RETURN 'wczoraj';
		ELSEIF 
			(@.Jutro := (_Date = (CURRENT_DATE() + INTERVAL 1 DAY)))
			AND (@_FMT LIKE '%[DJ]%') 
		THEN
			RETURN 'jutro';
		ELSE	
			-- SET @_FMT = _VarStr(REPLACE(REPLACE(REPLACE(@_FMT, '[D]', ''), '[W]', ''), '[J]', ''));
			SET
				@.Year := YEAR(_Date),
				@.MONTH := MONTH(_Date)
			;
			SET
				@.Aktualny.Rok := (@.Dzisiaj OR (@.Year = YEAR(CURRENT_DATE()))),
				@.Aktualny.Miesiac := (@.Dzisiaj OR (@.Aktualny.Rok AND (@.MONTH = MONTH(CURRENT_DATE())))),
				@.2DayName := _VarStr(ELT(WEEKDAY(_Date) + 1, 'Pn', 'Wt', 'SSrr', 'Cz', 'Pt', 'So', 'Nd'))
			;	
			-- SET @.Current.Year = (@_FMT LIKE '%YYCY%') AND (YEAR(_Date) = YEAR(CURRENT_DATE()));
			
	set @.dt.fmt = concat_WS('/', @.dt.fmt, 'REPLACE duzy');
			SET @_FMT = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@_FMT, 
				'[DZ]', ''),
				'[DW]', ''),
				'[DJ]', ''),
		--	Rok	
				'%YC', IF(@.Aktualny.Rok, 'b.r.', '%YYYY')),	--	2020/"b.r."
				'%YH', IF(@.Aktualny.Rok, '', '%YYYY')),	--	2021/""
				'%YYYY', '%Y'),			--	2020
				'%YY', '%y'),			--	20
		--	Miesiac
				'%mmmmH', IF(@.Aktualny.Miesiac, '', '%M')),	--	styczeĹ„
				'%mmmm', '%M'),	--	styczeĹ„
				'%mmmH', IF(@.Aktualny.Miesiac, '', '%b')),	--	styczeĹ„
				'%mmm', '%b'),	--	sty
				'%mH', IF(@.Aktualny.Miesiac, '', '%m')),	--	sty
				'%MR', _VarStr(ELT(@.MONTH, 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII'))),
				'%MM', '%m'),	-- 01 02 .. 11 12
		--	Dni	
				'%DW', IF(@.Wczoraj, 'wczoraj', '')),	
				'%DZ', IF(@.Dzisiaj, 'dzisiaj', '')),	
				'%DJ', IF(@.Jutro, 'jutro', '')),	
				-- '%DNN', '%W'),--	sobota
				'%ddddH', IF(@.Dzisiaj , '', '%W')),	--	sobota
				'%dddd', '%W'),--	sobota
				-- '%DN3', '%a'),	--	sob
				'%dddH', IF(@.Dzisiaj , '', '%a')),	--	sob
				'%ddd', '%a'),	--	sob
				-- '%DN2', _VarStr(ELT(WEEKDAY(_Date) + 1, 'Pn', 'Wt', 'SSrr', 'Cz', 'Pt', 'So', 'Nd'))),
				'%ddH', IF(@.Dzisiaj , '', @.2DayName)),
				-- '%dd', _VarStr(ELT(WEEKDAY(_Date) + 1, 'Pn', 'Wt', 'SSrr', 'Cz', 'Pt', 'So', 'Nd'))),
				'%dd', _VarStr(@.2DayName)),
				'%DDH', IF(@.Dzisiaj , '', '%d')),	--	08 sty
				'%DD', '%d'),	--	08 sty
				'%DH', IF(@.Dzisiaj , '', '%e')),	--	8 sty
				'%D', '%e'),	--	8 sty
				'%dH', IF(@.Dzisiaj , '', '%d')),	--	8 sty
		--	godziny
				'%HH', '%H'),	-- 01 02 ..11 12
				'%H', '%H'),	-- 01 02 ..11 12
		--	minuty
				'%NN', '%i'),	-- 01 02 ..11 12
				'%N', '%i'),	-- 01 02 ..11 12
		--	sekundy
				'%SS', '%S'),	-- 01 02 ..11 12
			-- skracamy kazda milisec wazna SET @_FMT = REPLACE(REPLACE(@_FMT, '%SS', '%S'), '%S', '%S');
				'  ', ' '),
				'  ', ' ')
			;
	set @.dt.fmt = concat_WS('/', @.dt.fmt, 'REPLACE duzy.end');
			-- trzeba uwazac by nie wstwiac spacji bo po co zwiekszac czas serwera SET @.Fmt_DateTime = REPLACE(REPLACE(TRIM(DATE_FORMAT(_DateTime, @_FMT)), '  ', ' '), '  ', ' ');
			SET @.Fmt_DateTime = TRIM(REPLACE(DATE_FORMAT(_DateTime, @_FMT), 'SSrr', _VarStr('Ĺšr'))); -- musze tutaj zmieniaÄ‡ srode bo lc_time_names = 'pl_PL' wymusza polskie znaki i zamniast Ĺšr mam krzaki
	set @.dt.fmt = concat_WS('/', @.dt.fmt, 'koniec');
			RETURN @.Fmt_DateTime;
		END IF;
	END IF;
	
/*	
	IF 
		(IFNULL(_DateTime, '0000-00-00 00:00:00') = '0000-00-00 00:00:00')
		OR
		(IFNULL(DATE(_DateTime), '0000-00-00') = '0000-00-00')
		OR
		NULLIF(_FMT, '') IS NULL
	THEN
		RETURN '';
	ELSE
		SET _Date = DATE(_DateTime);
		SET @_FMT = UPPER(_FMT); -- , @_Params = _Params;
		IF 
			(@_FMT LIKE '%[D]%') 
			AND (_Date = CURRENT_DATE())
		THEN
			RETURN 'dzisiaj';
		ELSEIF 
			(@_FMT LIKE '%[W]%') 
			AND (_Date = (CURRENT_DATE() - INTERVAL 1 DAY))
		THEN
			RETURN 'wczoraj';
		ELSEIF 
			(@_FMT LIKE '%[J]%') 
			AND (_Date = (CURRENT_DATE() + INTERVAL 1 DAY))
		THEN
			RETURN 'jutro';
		ELSE	
			SET @_FMT = _VarStr(REPLACE(REPLACE(REPLACE(@_FMT, '[D]', ''), '[W]', ''), '[J]', ''));
			-- SET @.Current.Year = (@_FMT LIKE '%YYCY%') AND (YEAR(_Date) = YEAR(CURRENT_DATE()));
			-- set @_FMT.tmp = @_FMT;
		--	Rok	
			SET @_FMT = REPLACE(REPLACE(REPLACE(@_FMT, 
				-- '%YYCY', IF(@.Current.Year, 'b.r.', '%Y')),	--	2020
				'%YYCY', IF(YEAR(_Date) = YEAR(CURRENT_DATE()), 'b.r.', '%Y')),	--	2020
				'%YYYY', '%Y'),			--	2020
				-- bez przesady '%YYY', '%Y'),			--	2020
				'%YY', '%y')			--	20
			;
		--	Miesiac
			SET @_FMT = REPLACE(REPLACE(REPLACE(@_FMT, 
				'%MR', _VarStr(ELT(MONTH(_Date), 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII'))),
				'%MMM', '%b'),	--	sty
				'%MM', '%m')	-- 01 02 ..11 12
			;
		--	godziny
			SET @_FMT = REPLACE(REPLACE(@_FMT, '%HH', '%H'), '%H', '%H');
		--	minuty
			SET @_FMT = REPLACE(REPLACE(@_FMT, '%NN', '%N'), '%N', '%i');
		--	sekundy
			-- skracamy kazda milisec wazna SET @_FMT = REPLACE(REPLACE(@_FMT, '%SS', '%S'), '%S', '%S');
			SET @_FMT = REPLACE(@_FMT, '%SS', '%S');
		--	Dni	
			SET @_FMT = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@_FMT, 
				-- '%DDD', _VarStr(ELT(WEEKDAY(_Date) + 1, 'Pn', 'Wt', 'Ĺšr', 'Cz', 'Pt', 'So', 'Nd'))),
				'%DDD', _VarStr(ELT(WEEKDAY(_Date) + 1, 'Pn', 'Wt', 'SSrr', 'Cz', 'Pt', 'So', 'Nd'))),
				'%DD', '%d'),	--	08 sty
				'%D', '%e'),	--	8 sty
				'%E', '%e'),	--	8 sty
				'  ', ' ')
			;
			-- trzeba uwazac by nie wstwiac spacji bo po co zwiekszac czas serwera SET @.Fmt_DateTime = REPLACE(REPLACE(TRIM(DATE_FORMAT(_DateTime, @_FMT)), '  ', ' '), '  ', ' ');
			SET @.Fmt_DateTime = TRIM(REPLACE(DATE_FORMAT(_DateTime, @_FMT), 'SSrr', 'Ĺšr')); -- musze tutaj zmieniaÄ‡ srode bo lc_time_names = 'pl_PL' wymusza polskie znaki i zamniast Ĺšr mam krzaki
			RETURN @.Fmt_DateTime;
		END IF;
	END IF;
*/
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `Format_DT_REG`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `Format_DT_REG`(
	_DateTime DATETIME,
	_FMT VARCHAR(150)
	
) RETURNS VARCHAR(150)
    NO SQL
    DETERMINISTIC
/******************************************************************************************************************************************
	REGEXP
	NAJNOWSZA 13 XI 2021 
*******************************************************************************************************************************************/	
BEGIN
	DECLARE _Date DATE;
/*
	Oznaczenia
	dla 2021-04-09 12:25:02	
	%YY			20
	%YYYY		2020
	%YYCY		gdy aktualny rok to: b.r. w przeciw. 2020
		gdy @YYYY/@YY etc to chowanie
	%MM			01
	%M			1
	%MMM			sty
	%MMMM		stycznia
	%MR			I  ( II, III, IV, ...)
		gdy @MMM/@MM etc to chowanie
	%D			6
	%DD			06
	%DDD			pon
	%DDDD		poniedzialek
		gdy @DDD/@DD etc to chowanie
	%H			8  godzina
	%HH			08  godzina
	%N			45  minuty
	%NN			45  minuty
	%S			2  sekundy
	%SS			02  sekundy
*/	
	SET lc_time_names = 'pl_PL';
	IF 
		(IFNULL(_DateTime, '0000-00-00 00:00:00') = '0000-00-00 00:00:00')
		OR
		(IFNULL(DATE(_DateTime), '0000-00-00') = '0000-00-00')
		OR
		NULLIF(_FMT, '') IS NULL
	THEN
		RETURN '';
	ELSE
		SET _Date = DATE(_DateTime);
		-- SET @_FMT = UPPER(_FMT); -- , @_Params = _Params;
		SET @_FMT = _FMT;
		IF 
			(@.Dzisiaj := (_Date = CURRENT_DATE()))
			AND (@_FMT LIKE '%[DZ]%') 
		THEN
			RETURN 'dzisiaj';
		ELSEIF 
			(@.Wczoraj := (_Date = (CURRENT_DATE() - INTERVAL 1 DAY)))
			AND (@_FMT LIKE '%[DW]%') 
		THEN
			RETURN 'wczoraj';
		ELSEIF 
			(@.Jutro := (_Date = (CURRENT_DATE() + INTERVAL 1 DAY)))
			AND (@_FMT LIKE '%[DJ]%') 
		THEN
			RETURN 'jutro';
		ELSE	
			-- SET @_FMT = _VarStr(REPLACE(REPLACE(REPLACE(@_FMT, '[D]', ''), '[W]', ''), '[J]', ''));
			SET
				@.Year := YEAR(_Date),
				@.MONTH := MONTH(_Date)
			;
			SET
				@.Aktualny.Rok := (@.Dzisiaj OR (@.Year = YEAR(CURRENT_DATE()))),
				@.Aktualny.Miesiac := (@.Dzisiaj OR (@.Aktualny.Rok AND (@.MONTH = MONTH(CURRENT_DATE())))),
				@.2DayName := _VarStr(ELT(WEEKDAY(_Date) + 1, 'Pn', 'Wt', 'SSrr', 'Cz', 'Pt', 'So', 'Nd'))
			;	
			SET
				@.Szukaj.DWJ = @_FMT RLIKE '%[a-zA-z]+#H[DWJ][DWJ]?[DWJ]?#'
			;
			IF @.Szukaj.DWJ THEN
				IF @.Dzisiaj THEN
				--	dla dzisiaj
					SET @_FMT =
						REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(@_FMT, 
						--	dni
							'%[D]+#H[D][WJ]?[J]?#', '_'),
						--	m-ce
							'%[M]+#H[D][WJ]?[J]?#', '_'),
						--	rok
							'%[Y]+#H[D][WJ]?[J]?#', '_')
					;
				ELSEIF @.Wczoraj THEN
				--	dla wczoraj
					SET @_FMT =
						REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(@_FMT, 
						--	dni
							'%[D]+#H[D]?[W][J]?#', '_'),
						--	m-ce
							'%[M]+#H[D]?[W][J]?#', '_'),
						--	rok
							'%[Y]+#H[D]?[W][J]?#', '_')
						;
				ELSEIF @.Jutro THEN
				--	dla jutra
					SET @_FMT =
						REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(@_FMT, 
						--	dni
							'%[D]+#H[D]?[W]?[J]#', '_'),
						--	m-ce
							'%[M]+#H[D]?[W]?[J]#', '_'),
						--	rok
							'%[Y]+#H[D]?[W]?[J]#', '_')
						;
				END IF;
			--	wyczyĹ›c opcje DWJ
				SET @_FMT = REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(@_FMT, '#H[WJD]?[WJD]?[WJD]?#', ''), '  ', ' '), '_', '');
			END IF;
			-- SET @.Current.Year = (@_FMT LIKE '%YYCY%') AND (YEAR(_Date) = YEAR(CURRENT_DATE()));
			SET @_FMT = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@_FMT, 
				'[DZ]', ''),
				'[DW]', ''),
				'[DJ]', ''),
		--	Rok	
				'%YC', IF(@.Aktualny.Rok, 'b.r.', '%YYYY')),	--	2020/"b.r."
				'%YH', IF(@.Aktualny.Rok, '', '%YYYY')),	--	2021/""
				'%YYYY', '%Y'),			--	2020
				'%YY', '%y'),			--	20
		--	Miesiac
				'%mmmmH', IF(@.Aktualny.Miesiac, '', '%M')),	--	styczeĹ„
				'%mmmm', '%M'),	--	styczeĹ„
				'%mmmH', IF(@.Aktualny.Miesiac, '', '%b')),	--	styczeĹ„
				'%mmm', '%b'),	--	sty
				'%mH', IF(@.Aktualny.Miesiac, '', '%m')),	--	sty
				'%MR', _VarStr(ELT(@.MONTH, 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII'))),
				'%MM', '%m'),	-- 01 02 .. 11 12
		--	Dni	
				'%DW', IF(@.Wczoraj, 'wczoraj', '')),	
				'%DZ', IF(@.Dzisiaj, 'dzisiaj', '')),	
				'%DJ', IF(@.Jutro, 'jutro', '')),	
				-- '%DNN', '%W'),--	sobota
				'%ddddH', IF(@.Dzisiaj , '', '%W')),	--	sobota
				'%dddd', '%W'),--	sobota
				-- '%DN3', '%a'),	--	sob
				'%dddH', IF(@.Dzisiaj , '', '%a')),	--	sob
				'%ddd', '%a'),	--	sob
				-- '%DN2', _VarStr(ELT(WEEKDAY(_Date) + 1, 'Pn', 'Wt', 'SSrr', 'Cz', 'Pt', 'So', 'Nd'))),
				'%ddH', IF(@.Dzisiaj , '', @.2DayName)),
				-- '%dd', _VarStr(ELT(WEEKDAY(_Date) + 1, 'Pn', 'Wt', 'SSrr', 'Cz', 'Pt', 'So', 'Nd'))),
				'%dd', _VarStr(@.2DayName)),
				'%DDH', IF(@.Dzisiaj , '', '%d')),	--	08 sty
				'%DD', '%d'),	--	08 sty
				'%DH', IF(@.Dzisiaj , '', '%e')),	--	8 sty
				'%D', '%e'),	--	8 sty
				'%dH', IF(@.Dzisiaj , '', '%d')),	--	8 sty
		--	godziny
				'%HH', '%H'),	-- 01 02 ..11 12
				'%H', '%H'),	-- 01 02 ..11 12
		--	minuty
				'%NN', '%i'),	-- 01 02 ..11 12
				'%N', '%i'),	-- 01 02 ..11 12
		--	sekundy
				'%SS', '%S'),	-- 01 02 ..11 12
			-- skracamy kazda milisec wazna SET @_FMT = REPLACE(REPLACE(@_FMT, '%SS', '%S'), '%S', '%S');
				'  ', ' '),
				'  ', ' ')
			;
			-- trzeba uwazac by nie wstwiac spacji bo po co zwiekszac czas serwera SET @.Fmt_DateTime = REPLACE(REPLACE(TRIM(DATE_FORMAT(_DateTime, @_FMT)), '  ', ' '), '  ', ' ');
			SET @.Fmt_DateTime = TRIM(REPLACE(DATE_FORMAT(_DateTime, @_FMT), 'SSrr', 'Ĺšr')); -- musze tutaj zmieniaÄ‡ srode bo lc_time_names = 'pl_PL' wymusza polskie znaki i zamniast Ĺšr mam krzaki
			RETURN @.Fmt_DateTime;
		END IF;
	END IF;
	
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `Get_Status_For_From_To`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `Get_Status_For_From_To`(
	_MODE VARCHAR(255),
	_IN_Date DATETIME,
	_IN_From DATETIME,
	_IN_To DATETIME,
	_IN_Status VARCHAR(100),
	_IN_Deleted BOOLEAN
	-- _FMT VARCHAR(150)
-- 	_ParamA VARCHAR(250),
--	_ParamB VARCHAR(250),
--	_ParamC VARCHAR(250)
) 
RETURNS VARCHAR(256)
    NO SQL
    DETERMINISTIC
S_Get_Status_For_From_To: BEGIN
	/*************************************************************************************************************************
		Get_Status_For_From_To('', NULL/Current_DATE(), go.Valid_From, go.Valid_To, '', go.Deleted, '') = 'OK'
	*************************************************************************************************************************/
	IF _MODE IN ('', 'GET', 'GET.FULL') THEN
		SET 
			-- _IN_Date = IFNULL(NULLIF(NULLIF(_IN_Date, ''), '0000-00-00'), CURRENT_DATE()),
			_IN_Date = IFNULL(NULLIF(NULLIF(_IN_Date, ''), '0000-00-00'), NOW()),
			_IN_Deleted = IFNULL(NULLIF(_IN_Deleted, ''), 0),
			_IN_Status = IFNULL(NULLIF(_IN_Status, ''), 0),
			_IN_From = IFNULL(_IN_From, '2000-01-01'),
			_IN_To = IFNULL(_IN_To, '2100-12-31'),
			@.Valid_From_To.From = NULL,
			@.Valid_From_To.To = NULL,
			@.Valid_From_To.Any = NULL,
			@.Valid_From_To._Msg = NULL
		;
		-- SET @_IN_Date =_IN_Date;
		/*------------------------------------------------------------------------------------
			Statusy dla zastepstw
			0	- OK / Akceptuj
			-1	- Propozycja (Offer) - wstÄ™pny status 
			1	- Usuniety (Deleted) 
			2	- Anulowany (Canceled)
			3	- Odrzucony (Reject)
			Generalnie 0 OK ale teĹĽ awaryjnie akcepujemy wszystkie NOT (Status > 0) 
		------------------------------------------------------------------------------------*/
		
		SET @.OUT.Status = 
			IF(_IN_Deleted = 1 OR _IN_Status = 1, 
				'Deleted',
				IF(_IN_Status = -1, 
					'Offer',
					IF(_IN_Status = 2, 
						'Canceled',
						IF(_IN_Status = 3, 
							'Reject',
							IF(_IN_From = '0000-00-00' AND _IN_To = '2100-12-31',
								'OK.Infinity',
								IF(_IN_Date BETWEEN _IN_From AND _IN_To,
									'OK',
									IF(_IN_Date < IFNULL(_IN_From, '2000-01-01'),
										'wait',
										'old'
									)
								)
							)
						)
					)
				)
			)
		;
		SET 
			@.OUT.From_To.OK.From = IF(@.OUT.Status LIKE 'OK%', _IN_From, NULL),
			@.OUT.From_To.OK.To = IF(@.OUT.Status LIKE 'OK%', _IN_To, NULL),
			@.OUT.From_To.wait.From = NULL,	
			@.OUT.From_To.wait.To = IF(@.OUT.Status LIKE 'wait%', _IN_From, NULL),	-- gdy Wait to piszem ze nie OK do
			@.OUT.From_To.old.From = IF(@.OUT.Status LIKE 'old%', _IN_To, NULL),	-- gdy Wait to piszem ze nie OK do
			@.OUT.From_To.old.To = NULL
		;	
		RETURN @.OUT.Status;
	END IF;
	
	/*
	IF _MODE = 'Last.@.Range' THEN
		SET
			@.OUT.Text = 
			CASE 
			WHEN @.OUT.Status LIKE 'OK%' THEN
				_Fmt_DateTime_Range(@.OUT.From_To.OK.From, @.OUT.From_To.OK.To, _FMT)
			WHEN @.OUT.Status LIKE 'wait%' THEN
				_Fmt_DateTime_Range(@.OUT.From_To.wait.From, @.OUT.From_To.wait.To, _FMT)
			WHEN @.OUT.Status LIKE 'old%' THEN
				_Fmt_DateTime_Range(@.OUT.From_To.old.From, @.OUT.From_To.old.To, _FMT)
			ELSE
				NULL
			END
		;
		SET 
			@.OUT.From_To.OK.From = NULL,
			@.OUT.From_To.OK.To = NULL,
			@.OUT.From_To.wait.From = NULL,	
			@.OUT.From_To.wait.To = NULL,	
			@.OUT.From_To.old.From = NULL,	
			@.OUT.From_To.old.To = NULL
		;	
		RETURN @.OUT.Text;
	END IF;
	*/
	
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_Format_DT_Range_Mask`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Format_DT_Range_Mask`(
	_Start DATETIME,
	_End  DATETIME,
	_FMT VARCHAR(150),
 	_Mask VARCHAR(250)
) RETURNS VarChar(250)
    NO SQL
    DETERMINISTIC
BEGIN
	/*
	Najnowsza - odwolujaca sie to takze najnowsze `_Format_DT`
	*/
	-- SET @_Start = _Start, @_End = _End;	
	-- SET _FMT = REPLACE(REPLACE(REPLACE((@._FMT := _FMT), '<MR>', 'D MR YYYY @Y@D'), '<MMM>', 'D MMM YYYY @Y@D'), '<dni>', 'dni');
	-- SET @.jmam.fmt= _FMT;
	-- set @.jmam.fmt.Show.Dni = (@.jmam.fmt LIKE '%dni%') OR (@.jmam.fmt LIKE '%<dni>%');
	SET @.fmt = _FMT;
	SET @.FMT = NULLIF(@.fmt, '');
	SET @.fmt.Start = _mid_sep(@.FMT, ';', 1);
	SET @.fmt.End = IFNULL(NULLIF(_mid_sep(@.FMT, ';', 2), ''), @.fmt.Start);
	SET @.fmt.Spec = IFNULL(NULLIF(_mid_sep(@.FMT, ';', 3), ''), @.fmt.End);
	-- SET @.fmt.Params = IFNULL(NULLIF(_mid_sep(@.FMT, ';', 3), ''), _Params);
	
	SET 
		@.range.Result = '',
		@.No.Start = DATE(IFNULL(_Start, '0000-00-00 00:00:00')) = '0000-00-00',
		@.No.End = DATE(IFNULL(_End, '0000-00-00 00:00:00')) = '0000-00-00' OR _End >= '2099-12-31'
	;
	SET
		@.Is.Start.End = NOT @.No.Start AND NOT @.No.End
	;

	SET @.fmt.Start = REPLACE(@.fmt.Start, '@', '%');
	
	set @.fmt.x = _FMT;
	
	SET @.fmt.Zastap.Bezterminowo.Text = 'bezterninowo';
	IF @.No.Start AND @.No.End THEN
	--	... - bez ograniczeĹ„ od pocz do konca swiata
		SET 
			@.range.Result = 
				REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(_Mask,
					'[od]', ''),
					'[#from#]', ''),
					'[#fromSpec#]', ''),
					'[do]', ''),
					'[#to#]', ''),
					'[#toSpec#]', ''),
					'[#to.bt#]', IFNULL(@.fmt.Zastap.Bezterminowo.Text, '')),
					'[#dni#]', ''),
					'[#hour#]', ''),
					'[#hour.minute#]', '')
		;
		/*
		SET 
			@.range.Result = 
				IF(@.fmt.Zastap.Bezterminowo, 
					IFNULL(@.fmt.Zastap.Bezterminowo.Text, ''), 
					''
			)
		;
		*/
	ELSEIF @.No.Start THEN
	--	... do DATY - od pocz swiata do daty
		SET 
			@.range.Result = 
				REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(_Mask,
					'[od]', ''),
					'[#fromSpec#]', ''),
					'[#from#]', ''),
					'[do]', ' do '),
					'[#to#]', _Format_DT(_End, @.fmt.End)),
					'[#toSpec#]', _Format_DT(_End, @.fmt.Spec)),
					'[#to.bt#]', _Format_DT(_End, @.fmt.End)),
					'[#dni#]', ''),
					'[#hour#]', ''),
					'[#hour.minute#]', '')
		;
		/*
		SET
			@.range.Result = CONCAT_WS(' ', 
				'do', _Format_DT(_End, @.fmt.Start)
			)
		;
		*/
	ELSEIF @.No.End THEN
	--	od DATY ... - od daty do konca swiata bez ograniczeĹ„
		SET 
			@.range.Result = 
				REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(_Mask,
					'[od]', 'od '),
					'[#from#]', _Format_DT(_Start, @.fmt.Start)),
					'[#fromSpec#]', _Format_DT(_Start, @.fmt.Spec)),
					'[do]', ''),
					'[#to#]', ''),
					'[#toSpec#]', ''),
					'[#to.bt#]', IFNULL(CONCAT(' - ', @.fmt.Zastap.Bezterminowo.Text), '')),
					'[#dni#]', ''),
					'[#hour#]', ''),
					'[#hour.minute#]', '')
		;
		/*
		SET 
			@.range.Result = CONCAT_WS(' ', 
				'od', _Format_DT(_Start, @.fmt.Start), 
				IF(@.fmt.Zastap.Bezterminowo, NULLIF(@.fmt.Zastap.Bezterminowo.Text, ''), NULL)
			)
		;
		*/
	ELSE
	--	Dwie daty - mamy obie daty - spr czy 1 dzien
		-- SET @.Is.1.Day = DATE(_Start) <=> DATE(_End);
		-- IF @.Is.1.Day THEN
		IF (@.Is.1.Day := (DATE(_Start) <=> DATE(_End))) THEN
		--	1 dzieĹ„!!! oba dni wystepuja ale .... jest to tylko 1 dzien !!!
			SET @.fmt.Hour = TIMESTAMPDIFF(HOUR, _Start, _End);
			SET @.fmt.Minute = TIMESTAMPDIFF(MINUTE, _Start, _End);
			SET 
				@.range.Result = 
					REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(_Mask,
						'[od]', ''),
						'[#from#]', _Format_DT(_Start, @.fmt.Start)),
						'[#fromSpec#]', _Format_DT(_Start, @.fmt.Spec)),
						'[do]', ''),
						'[#to#]', ''),
						'[#toSpec#]', ''),
						'[#to.bt#]', ''),
						'[#dni#]', IF(@.fmt.Hour > 0, '', '1 dzieĹ„')),
						-- '[#hour#]', IF(@.fmt.Hour > 0, CONCAT(@.fmt.Hour, ' h '), ''))
						-- '[#hour#]', IF(@.fmt.Hour > 0, _DisplayDurationFormat(@.fmt.Hour), '')),
						-- '[#minute#]', IF(@.fmt.Minute > 0, _DisplayDurationFormat(@.fmt.Minute), '')),
						-- '[#hour.minute#]', IF(@.fmt.Hour > 0, _DisplayDurationFormat(@.fmt.Minute/60), ''))
						'[#hour#]', IF(@.fmt.Hour > 0, _DisplayDurationH('H', @.fmt.Hour, '', ''), '')),
						'[#minute#]', IF(@.fmt.Minute > 0, _DisplayDurationH('M', @.fmt.Minute/60, '', ''), '')),
						'[#hour.minute#]', IF(@.fmt.Minute > 0, _DisplayDurationH('H/M', @.fmt.Minute/60, '', ''), ''))
			;
			/*
			SET @.range.Result = 
				_Format_DT(_Start, @.fmt.Start)
			;
			*/
		ELSE
		--	dwie rĂłĹĽne  daty / dwa rozne dni	
			SET @.fmt.Days = 1 + TIMESTAMPDIFF(DAY, DATE(_Start), DATE(_End));
			SET @.fmt.Hour = TIMESTAMPDIFF(HOUR, _Start, _End);
			SET 
				@.Is.1.Month = 
					@.Is.Start.End AND NOT @.Is.1.Day AND (MONTH(_Start) = MONTH(_End)) AND (YEAR(_Start) = YEAR(_End))
			;
			SET 
				@.Is.1.Year = 
					@.Is.Start.End AND NOT @.Is.1.Day AND NOT @.Is.1.Month AND (YEAR(_Start) = YEAR(_End))
			;
			
			IF @.Is.1.Month THEN
			--	zakres 1 m-ca
			--	wywal m-ce i ...
				-- SET @.fmt.Start = REPLACE(REPLACE(@.fmt.Start, '%M', 'M'), '%Y', 'Y');
				-- miesiecy cyfrowych nie blokujemy!!! SET @.fmt.Start = REPLACE(REPLACE(REPLACE(REPLACE(@.fmt.Start, '%MR', ''), '%mmmm', ''), '%mmm', ''), '%MM', '');
				SET @.fmt.Start = REPLACE(REPLACE(REPLACE(REPLACE(@.fmt.Start, '%MR', ''), '%mmmm', ''), '%mmm', ''), '%blablaMM', '');
			--	wywal lata
				SET @.fmt.Start = REPLACE(REPLACE(REPLACE(REPLACE(@.fmt.Start, '%YC', ''), '%YH', ''), '%YYYY', ''), '%YY', '');	-- YYCY, YYYY, YY
			ELSEIF @.Is.1.Year THEN
			--	zakres 1 roku
			--	wywal lata
				SET @.fmt.Start = REPLACE(REPLACE(REPLACE(REPLACE(@.fmt.Start, '%YC', ''), '%YH', ''), '%YYYY', ''), '%YY', '');	-- YYCY, YYYY, YY
			END IF;
			SET 
				@.range.Result = 
					REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(_Mask,
						'[od]', 'od '),
						'[#from#]', _Format_DT(_Start, @.fmt.Start)),
						'[#fromSpec#]', _Format_DT(_Start, @.fmt.Spec)),
						'[do]', ' do '),
						'[#to#]', _Format_DT(_End, @.fmt.End)),
						'[#toSpec#]', _Format_DT(_End, @.fmt.Spec)),
						'[#to.bt#]', _Format_DT(_End, @.fmt.End)),
						'[#dni#]', IF(@.fmt.Days > 0, CONCAT(@.fmt.Days, ' dni '), '')),
						'[#hour#]', IF(@.fmt.Days = 0 AND @.fmt.Hour > 0, CONCAT(@.fmt.Hour, ' h '), '')),
						'[#hour.minute#]', '')
			;
			/*
			SET @.range.Result = CONCAT_WS('',
				IF(@.fmt.put.OD, 'od ', NULL),
				_Format_DT(_Start, @.fmt.Start),
				' do ',
				_Format_DT(_End, @.fmt.End)
			)	
			;
			*/
		END IF;
	END IF;
	
	SET
		@.range.Result = 
			TRIM(REPLACE(REPLACE(REPLACE(@.range.Result, 'dnia dzisiaj', 'dzisiaj'), '   ', '  '), '  ', ' '))
	;
	/*
		@.range.Result = 
			REPLACE(
			CONCAT_WS('',
				-- jmam NULLIF(@.fmt.Lead.Text, ''),
				IF(@.fmt.Show.Dni AND @.fmt.Days > 0, 
					CONCAT(NULLIF(@.fmt.Days, 1), ' dni '), 
					NULL
				),
				IF(@.fmt.Bez.Nawiasow, NULL, '('),
				NULLIF(@.range.Result, ''),
				IF(@.fmt.Bez.Nawiasow, NULL, ')')
			), 'dnia dzisiaj', 'dzisiaj'
			)
	;
	*/
	-- SET @.DT.Range.Bez.Termin = NULL;
	RETURN @.range.Result;
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `_D_to_Wczoraj_Jutro`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_D_to_Wczoraj_Jutro`(
	_At DATE,
 	_Mask VARCHAR(250),
 	_ParamA VARCHAR(250)
) RETURNS VarChar(250)
    NO SQL
    DETERMINISTIC
BEGIN
	/*
		SELECT _D_to_Wczoraj_Jutro('2024-07-31', '%dd %D %mmm %YH', '');
	*/
	SET @.WJ.Result :=
		IF(CURRENT_DATE() = _At,
			'DziĹ› ',
			IF(CURRENT_DATE() > _At,
			--	minÄ™Ĺ‚o
				IF(CURRENT_DATE() - INTERVAL 1 DAY = _At,
					'Wczoraj ',
					CONCAT(
						IF((CURRENT_DATE() - INTERVAL 2 DAY) = _At,
							'Przedwczoraj w ',
							'W '
						),
						_Format_DT(_At, _Mask), 
						' '
					)
				),
			--	nastÄ…pi
				CONCAT(
					IF((CURRENT_DATE() + INTERVAL 1 DAY) = _At,
						'Jutro ',
						CONCAT('Za ', DATEDIFF(_At, CURRENT_DATE()), ' dni ')
					),
					'(', _Format_DT(_At, _Mask), ') '
				)
			)
		);
	RETURN @.WJ.Result;
END $$
DELIMITER ;

DELIMITER $$ 
DROP FUNCTION IF EXISTS `Get_Status_For_From_To_Str`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `Get_Status_For_From_To_Str`(
	_MODE VARCHAR(255),
	_IN_Date DATETIME,
	_IN_From DATETIME,
	_IN_To DATETIME,
	_IN_Status VARCHAR(100),
	_IN_Deleted BOOLEAN,
	_FMT VARCHAR(150),
 	_Params VARCHAR(250)
--	_ParamB VARCHAR(250),
--	_ParamC VARCHAR(250)
) RETURNS VarChar(250)
    NO SQL
    DETERMINISTIC
/******************************************************************************************************************************************
	NOWA do info o stanie i waĹĽnoĹ›ci od... do...
	UWAGA uĹĽywa do statusĂłw
		Get_Status_For_From_To()
	dla formatu
		_Format_DT_Range_Mask()
*******************************************************************************************************************************************/	
BEGIN
	IF _MODE LIKE 'GET.Info.%' THEN
	--	Status
		SET @.OUT.Info.Full = (_MODE LIKE 'GET.Info.%.Full%');
		SET _FMT = IFNULL(NULLIF(_FMT, ''), '%D %mmm %Y;%d %mmm %YYYY;<BNaw><BT>- bezterminowo<#BT><OD>'); -- MMM albo MR
		SET 
			@.OUT.Status = IF(_Params LIKE '%NULL%', 
				'Null', 
				Get_Status_For_From_To('', _IN_Date, _IN_From, _IN_To, _IN_Status, _IN_Deleted)
				);
	--	ew. Format domyslny
		-- SET _FMT = IF(_FMT LIKE '%<LEAD>%<#LEAD>%', _FMT, CONCAT(_FMT, '<LEAD>#LEAD#<#LEAD>'));

		IF _MODE LIKE 'GET.Info.Konto%' THEN
			SET 
				@.OUT.Status = IF(_Params LIKE '%NULL%', 
					'Null', 
					Get_Status_For_From_To('', _IN_Date, _IN_From, _IN_To, _IN_Status, _IN_Deleted)
				),
				_IN_From = IF(_IN_From <= '2000-01-01', NULL, _IN_From), 
				_IN_To = IF(_IN_To >= '2100-01-01', NULL, _IN_To)
				;
			SET
				@.OUT.Text = 
				CASE 
				WHEN @.OUT.Status IN ('Null') THEN	-- dla nie znalezienia grupy i tak woĹ‚amy 
					'Brak dostÄ™pu do konta'
				WHEN @.OUT.Status IN ('Canceled', 'Deleted') THEN
					'konto nieaktywne'
				WHEN @.OUT.Status LIKE 'OK%' THEN
					IF(@.OUT.Info.Full,
						_Format_DT_Range_Mask(_IN_From, _IN_To, '%D %mmm %YYYY[DZ];%D %mmm %YYYY[DZ];', 
							'konto aktywne [od][#from#][do][#to.bt#]' 
						),
						''
					)
				WHEN @.OUT.Status LIKE 'wait%' THEN
						_Format_DT_Range_Mask(_IN_From, _IN_To, '%D %mmm %YYYY[DZ];%D %mmm %YYYY[DZ][DJ];%DJ w %dd %D %mmm %YH[DZ][DJ];', 
							'konto bÄ™dzie aktywne [#fromSpec#] ([od][#from#][do][#to.bt#])'
							-- 'konto bÄ™dzie aktywne [#fromSpec#] ([od][#from#][do][#to.bt#])'
						)
				WHEN @.OUT.Status LIKE 'old%' THEN
						_Format_DT_Range_Mask(_IN_From, _IN_To, '%D %mmm %YYYY[DZ];%D %mmm %YYYY[DZ][DW];%DW w %dd %D %mmm %YH[DZ][DW];', 
							-- 'dostÄ™p do konta wygasĹ‚ dnia [#toSpec#] ([od][#from#][do][#to#])'
							'dostÄ™p do konta wygasĹ‚ [#toSpec#] ([od][#from#][do][#to#])'
						)
				ELSE
					NULL
				END
			;		
		END IF;		
		
		IF (_MODE LIKE 'GET.Info.%GRP%') OR (_MODE LIKE 'GET.Info.%Grupy%') THEN
			SET 
				@.OUT.Status = IF(_Params LIKE '%NULL%', 
					'Null', 
					Get_Status_For_From_To('', _IN_Date, _IN_From, _IN_To, _IN_Status, _IN_Deleted)
					);
			SET
				@.OUT.Text = 
				CASE 
				WHEN @.OUT.Status IN ('Null') THEN	-- dla nie znalezienia grupy i tak woĹ‚amy 
					'Brak dostÄ™pu do moduĹ‚u/grupy "#Grupa#"'
				WHEN @.OUT.Status IN ('Canceled') THEN
					IF(@.OUT.Info.Full, 'DostÄ™p do moduĹ‚u/grupy "#Grupa#" zostaĹ‚ cofniÄ™ty', 'dostÄ™p cofniÄ™ty')
				WHEN @.OUT.Status IN ('Deleted') THEN
					IF(@.OUT.Info.Full, 'DostÄ™p do moduĹ‚u/grupy "#Grupa#" zostaĹ‚ usuniÄ™ty', 'dostÄ™p usuniÄ™ty')
				WHEN @.OUT.Status LIKE 'OK%' THEN
					IF(@.OUT.Info.Full,
						_Format_DT_Range_Mask(_IN_From, _IN_To, '%D %mmm %YYYY[DZ];%D %mmm %YYYY[DZ];', 
							IF(_Params LIKE '%<Dostep>%', 
								'DostÄ™p z moduĹ‚u/grupy "#Grupa#" [od][#from#][do][#to.bt#]', 
								'DostÄ™p do moduĹ‚u/grupy "#Grupa#" [od][#from#][do][#to.bt#]'
							)
						),
						''
					)
				WHEN @.OUT.Status LIKE 'wait%' THEN
						_Format_DT_Range_Mask(_IN_From, _IN_To, '%D %mmm %YYYY[DZ];%D %mmm %YYYY[DZ];', 
							'DostÄ™p do moduĹ‚u/grupy "#Grupa#" nastÄ…pi [#from#] ([od][#from#][do][#to.bt#])'
						)
				WHEN @.OUT.Status LIKE 'old%' THEN
						_Format_DT_Range_Mask(_IN_From, _IN_To, '%D %mmm %YYYY[DZ];%D %mmm %YYYY[DZ];', 
							'DostÄ™p do moduĹ‚u/grupy "#Grupa#" wygasĹ‚ [#to#] ([od][#from#][do][#to#])'
						)
				ELSE
					NULL
				END
			;
		END IF;
		
		IF _MODE LIKE 'GET.Info.Zastepstwo%' THEN
			/*------------------------------------------------------------------------------------
				0	- OK / Akceptuj
				-1	- Propozycja (Offer) - wstÄ™pny status 
				1	- Usuniety (Deleted) 
				2	- Anulowany (Canceled)
				3	- Odrzucony (Reject)
				Generalnie 0 OK ale teĹĽ awaryjnie akcepujemy wszystkie NOT (Status > 0) 
			------------------------------------------------------------------------------------*/
			SET 
				@.OUT.Status = IF(_Params LIKE '%NULL%', 
					'Null', 
					Get_Status_For_From_To('', _IN_Date, _IN_From, _IN_To, _IN_Status, _IN_Deleted)
				),
				_IN_From = IF(_IN_From <= '2000-01-01', NULL, _IN_From), 
				_IN_To = IF(_IN_To >= '2100-01-01', NULL, _IN_To)
				;
			SET
				@.OUT.Text.Inf = 
					IF(@.OUT.Info.Full,
						_Format_DT_Range_Mask(_IN_From, _IN_To, '%dd %D %mmm %YYYY[DZ];%dd %D %mmm %YYYY[DZ];', 
							'[od][#from#][do][#to.bt#]' 
						),
						''
					)
			;
			SET
				@.OUT.Text = 
				CASE 
				WHEN @.OUT.Status IN ('Null') THEN	-- dla nie znalezienia  i tak woĹ‚amy 
					'Brak'
				WHEN @.OUT.Status IN ('Offer') THEN
					CONCAT_WS(' ', 'do akceptacji.', @.OUT.Text.Inf)
				WHEN @.OUT.Status IN ('Canceled', 'Deleted') THEN
					CONCAT_WS(' ', 'UniewaĹĽnione', @.OUT.Text.Inf)
				WHEN @.OUT.Status IN ('Reject') THEN
					CONCAT_WS(' ', 'Odrzucone', @.OUT.Text.Inf)
				WHEN @.OUT.Status LIKE 'OK%' THEN
					IF(@.OUT.Info.Full,
						CONCAT_WS(' ', 'zastÄ™p.', @.OUT.Text.Inf),
						''
					)
				WHEN @.OUT.Status LIKE 'wait%' THEN
						_Format_DT_Range_Mask(_IN_From, _IN_To, '%D %mmm %YYYY[DZ];%D %mmm %YYYY[DZ][DJ];%DJ w %dd %D %mmm %YH[DZ][DJ];', 
							'zastÄ™p. rozp. [#fromSpec#] ([od][#from#][do][#to.bt#])'
							-- 'konto bÄ™dzie aktywne [#fromSpec#] ([od][#from#][do][#to.bt#])'
						)
				WHEN @.OUT.Status LIKE 'old%' THEN
						_Format_DT_Range_Mask(_IN_From, _IN_To, '%D %mmm %YYYY[DZ];%D %mmm %YYYY[DZ][DW];%DW w %dd %D %mmm %YH[DZ][DW];', 
							-- 'dostÄ™p do konta wygasĹ‚ dnia [#toSpec#] ([od][#from#][do][#to#])'
							'zastÄ™p. wygasĹ‚o [#toSpec#] ([od][#from#][do][#to#])'
						)
				ELSE
					NULL
				END
			;		
		END IF;		
		
		SET @.OUT.Text = 
			IF(_Params LIKE '%@%', CONCAT_WS('::', @.OUT.Status, @.OUT.Text), @.OUT.Text)
		;
		RETURN @.OUT.Text;
	END IF;
	
END $$

DELIMITER ;

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

*/

/*  Zmiany CVS
$Log: _varDATETIME.sql,v $
Revision 1.68  2026/02/02 14:27:52  jmam
Uwzgl. calej daty w RANGE a nie date time $send$

Revision 1.67  2026/01/16 09:37:48  jmam
Inicjacja + $send$

Revision 1.66  2026/01/16 09:37:08  jmam
Inicjacja

*/
