 DELIMITER $$

DROP FUNCTION IF EXISTS `_Rnd2_`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Rnd2_`(
    _Value DECIMAL(24,6)
    ) RETURNS DECIMAL(16,2)
    NO SQL
    DETERMINISTIC
BEGIN
    RETURN ROUND(_Value, 2);
    

END $$


DROP FUNCTION IF EXISTS `_Rnd`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Rnd`(
    _Value DECIMAL(36,12),
    _N INTEGER
    ) RETURNS DECIMAL(36,12)
    NO SQL
    DETERMINISTIC
/*
SELECT BENCHMARK(100000, (SELECT _Rnd(-@V, 4)));    
*/    
BEGIN
    DECLARE _Value_mul_1E, _Result, _DIFF_Calk  DECIMAL(36,12);
    DECLARE _Value_mul_1E_Calk, _DIFF_Calk_10, _Value_mul_1E_Calk_SUM  DECIMAL(46,0);
    DECLARE _Pow BIGINT;
    
    -- SET _Value = TRUNCATE(_Value, _N + 2);
    -- SET @_Value = _Value;
    SET @IsMinus = (_Value < 0);
    SET _Value = ABS(_Value);
    SET _Pow = IF(_N = 0, 1, IF(_N = 1, 10, IF(_N = 2, 100, IF(_N = 3, 1000, IF(_N = 4, 10000, POW(10, _N))))));
    SET _Value_mul_1E = (_Value*_Pow);
    SET _Value_mul_1E_Calk = TRUNCATE(_Value_mul_1E, 0);    --     calkowita czesc
    SET _DIFF_Calk = (_Value_mul_1E - _Value_mul_1E_Calk);        -- roznica ulamkowa 
    SET _DIFF_Calk_10 = TRUNCATE((_DIFF_Calk)*10, 0);        -- roznica ulamkowa powiekszona razy 10 i zaokr. czyli wchodzi w jednosci
    SET _Value_mul_1E_Calk_SUM = _Value_mul_1E_Calk + (_DIFF_Calk_10 > 4);    --    gdy _DIFF_Calk_10 > 4 czyli 5, 6, .. to dodaje jedynke : "1"
/*
    SET _Result = 
        IF(@IsMinus, 
            -(_Value_mul_1E_Calk_SUM/_Pow),
            (_Value_mul_1E_Calk_SUM/_Pow)
        )    
    ;
*/    
    RETURN 
        IF(@IsMinus, 
            -(_Value_mul_1E_Calk_SUM/_Pow),
            (_Value_mul_1E_Calk_SUM/_Pow)
        )    
    ;

END $$

            DROP PROCEDURE IF EXISTS `_RndProc`$$
            CREATE DEFINER=`root`@`localhost` PROCEDURE `_RndProc`(
                _Value DECIMAL(36,12),
                _N INTEGER
                )
            /*
            SELECT BENCHMARK(100000, (SELECT _Rnd(-@V, 4)));    
            */    
            BEGIN
                DECLARE _Value_mul_1E, _Result, _DIFF_Calk  DECIMAL(36,12);
                DECLARE _Value_mul_1E_Calk, _DIFF_Calk_10, _Value_mul_1E_Calk_SUM  DECIMAL(46,0);
                DECLARE _Pow BIGINT;
                
                SET _Value = TRUNCATE(_Value, _N + 2);
                -- SET @_Value = _Value;
                SET @IsMinus = (_Value < 0);
                SET _Value = ABS(_Value);
                SET _Pow = IF(_N = 0, 0, IF(_N = 1, 10, IF(_N = 2, 100, IF(_N = 3, 1000, IF(_N = 4, 10000, POW(10, _N))))));
                SET _Value_mul_1E = (_Value*_Pow);
                SET _Value_mul_1E_Calk = TRUNCATE(_Value_mul_1E, 0);
                SET _DIFF_Calk = (_Value_mul_1E - _Value_mul_1E_Calk);
                SET _DIFF_Calk_10 = TRUNCATE((_DIFF_Calk)*10, 0);
                SET _Value_mul_1E_Calk_SUM = _Value_mul_1E_Calk + (_DIFF_Calk_10 > 4);    --    gdy _DIFF_Calk_10 > 4 czyli 5, 6, .. to dodaje jedynke : "1"

                SET _Result = 
                    IF(@IsMinus, 
                        -(_Value_mul_1E_Calk_SUM/_Pow),
                        (_Value_mul_1E_Calk_SUM/_Pow)
                    )    
                ;
                
                SELECT
                    _Value, _Pow, 
                    _Value_mul_1E, _Value_mul_1E_Calk,
                    _DIFF_Calk, _DIFF_Calk_10,
                    _Value_mul_1E_Calk_SUM,
                    _Result
                    
                
                ;

            END $$


DROP FUNCTION IF EXISTS `_Rnd2`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Rnd2`(
    _Value DECIMAL(24,6)
    ) RETURNS DECIMAL(18,2)
    NO SQL
    DETERMINISTIC
BEGIN
    DECLARE _Value_16_6  DECIMAL(16,6);
    SET _Value_16_6 = TRUNCATE(_Value, 6);
    -- SET @_Value_16_2 = _Value_16_2;
    -- SET _Value_16_6 = _Value;
    RETURN _Rnd(_Value, 2);
    -- RETURN ROUND( TRUNCATE(_Value_16_6, 2) + REPLACE(  ((_Value_16_6*1000) - ( TRUNCATE(_Value_16_6,2) *1000)) / 1000, '5', '6'), 2);
END $$

DROP FUNCTION IF EXISTS `_DisplayFmt_PL`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_DisplayFmt_PL`(
    _Input DOUBLE(16,8),
    _decimal_point INTEGER,
    _no_zeros BOOLEAN,
    _tousand_sep VARCHAR(1)
) RETURNS VARCHAR(50)
    NO SQL
    DETERMINISTIC
/*  przyk�ady
    _DisplayFmt_PL(182.8, 2, TRUE, '');  182,8
    _DisplayFmt_PL(182.8, 4, TRUE, '');  182,8
    _DisplayFmt_PL(182.8123, 3, TRUE, '');  182,812
*/    
BEGIN
    SET @_DisplayFmt_PL := FORMAT(_Input, IFNULL(_decimal_point, @session.HT.IR.CenyPrecision),'de_DE');
    SET @_DisplayFmt_PL := 
        IF(_no_zeros,
            REPLACE(RTRIM(REPLACE(REPLACE(RTRIM(REPLACE(@_DisplayFmt_PL, '0', ' ')), ' ', '0'), ',', ' ')), ' ', ','),
            @_DisplayFmt_PL
        )
    ;
    SET @_DisplayFmt_PL := REPLACE(@_DisplayFmt_PL, '.', IFNULL(_tousand_sep, ''));
    RETURN @_DisplayFmt_PL;
END $$

DROP FUNCTION IF EXISTS `_varIs_Number`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_varIs_Number`(_Value VARCHAR(256)) RETURNS BOOLEAN
    NO SQL
    DETERMINISTIC
BEGIN
    RETURN NOT(_Value regexp '[^0-9]');
END $$

DROP FUNCTION IF EXISTS `_varINT`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_varINT`(_Value VARCHAR(256)) RETURNS INT
    NO SQL
    DETERMINISTIC
BEGIN
    SET @Value = _Value;
    IF @Value IS NOT NULL THEN
        SET _Value = TRIM(REPLACE(_Value,',','.'));
        IF _Value REGEXP '^-?[0-9]+(\.[0-9]+)?$' THEN
            SET @Value = TRUNCATE(_Value+0, 0);
        ELSE
            SET @Value = 0;
        END IF;
    END IF;
    RETURN @Value;
END $$


DROP FUNCTION IF EXISTS `_varINT_NOTNULL`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_varINT_NOTNULL`(_Value INT) RETURNS INT
    NO SQL
    DETERMINISTIC
BEGIN
    /*  aktualnie  */
    RETURN IFNULL(_Value, 0);
    /*  propozycja  */
    SET _Value = TRIM(REPLACE(_Value,',','.'));
    IF _Value REGEXP '^-?[0-9]+(\.[0-9]+)?$' THEN
        SET @Value = TRUNCATE(_Value+0, 0);
    ELSE
        SET @Value = 0;
    END IF;
    RETURN @Value;
    
END $$

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


DROP FUNCTION IF EXISTS `_varBOOL`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_varBOOL`(_Value VARCHAR(256)) RETURNS BOOLEAN
    NO SQL
    DETERMINISTIC
BEGIN
    RETURN _Value OR (_Value = 'TRUE');
END $$


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

DROP FUNCTION IF EXISTS `_varDOUBLE_NOTNULL`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_varDOUBLE_NOTNULL`(_Value DOUBLE) RETURNS DOUBLE
    NO SQL
    DETERMINISTIC
BEGIN
    RETURN IFNULL(_Value, 0.0);
END $$

DROP FUNCTION IF EXISTS `_varDECIMAL`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_varDECIMAL`(_Value DECIMAL(18,8)) RETURNS DECIMAL(18,8)
    NO SQL
    DETERMINISTIC
BEGIN
    RETURN _Value;
END $$

DROP FUNCTION IF EXISTS `_varDECIMAL_NOTNULL`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_varDECIMAL_NOTNULL`(_Value DECIMAL(18,8)) RETURNS DECIMAL(18,8)
    NO SQL
    DETERMINISTIC
BEGIN
    RETURN IFNULL(_Value, 0.0);
END $$

DROP FUNCTION IF EXISTS `_CurrencyDiv`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_CurrencyDiv`(_X DECIMAL(16,4), _Y DECIMAL(16,4)) RETURNS DECIMAL(18,8)
    NO SQL
    DETERMINISTIC
BEGIN
    RETURN IFNULL(TRUNCATE(_X / _Y, 0), 0.0);  --  Result := Trunc(x/y);
END $$

DROP FUNCTION IF EXISTS `_CurrencyMod`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_CurrencyMod`(_X DECIMAL(16,4), _Y DECIMAL(16,4)) RETURNS DECIMAL(18,8)
    NO SQL
    DETERMINISTIC
BEGIN
    RETURN IFNULL(_X - _Y * TRUNCATE(_X / _Y, 0), 0.0);  --  Result := x - y * Trunc(x/y);
END $$

DROP FUNCTION IF EXISTS `_DisplFmt_`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_DisplFmt_`(
    _Value DECIMAL(24,6),
    DecimalPlaces INTEGER
    ) RETURNS VARCHAR(50)
    NO SQL
    DETERMINISTIC
BEGIN
    IF DecimalPlaces > 0 THEN
        RETURN FORMAT(_Value, IF((@vd := (ABS(_Value) - ABS(TRUNCATE(_Value, 0)))) = 0, 0, IF(@vd > 0.0999, 1, LEAST(DecimalPlaces, 2))), 'pl_PL');
    ELSE
        RETURN FORMAT(_Value, 0, 'pl_PL');
    END IF;

END $$

DROP FUNCTION IF EXISTS `_Get_Frac`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_Get_Frac`(
    _Kwota DOUBLE(16, 4),
    _FracDecimal INTEGER
) RETURNS INTEGER
    NO SQL
    DETERMINISTIC
BEGIN
    DECLARE _FracPower, _KwotaFrac INTEGER;
    DECLARE _Kwota_WE, _Kwota_POW DOUBLE(16, 4);
    SET    
        _FracDecimal = IFNULL(_FracDecimal, 2),
        _Kwota_WE = ABS(_Kwota)
    ;
    SET    _FracPower = ROUND(POWER(10, _FracDecimal));
    SET _Kwota_POW = _Kwota_WE * _FracPower;
    SET _KwotaFrac = FLOOR(_Kwota_POW % _FracPower);
        set @_Kwota_POW = _Kwota_POW, @_KwotaFrac = _KwotaFrac, @_Kwota = _Kwota, @_FracPower = _FracPower, @_FracDecimal = _FracDecimal;
    RETURN _KwotaFrac;
END $$




DELIMITER ;

/*
$Log: _varNumeric.sql,v $
Revision 1.20  2025/11/03 09:15:50  apro
$SEND$

Revision 1.19  2025/11/03 09:15:30  apro
varint

Revision 1.18  2025/11/03 08:06:57  jmam
Poprawka w _varINT aby przepuszczal NULL $send$

Revision 1.17  2025/09/16 16:01:46  apro
$SEND$
c.d. zabezpieczenie _VarInt

Revision 1.16  2025/09/16 15:54:05  apro
$SEND$
zabezpieczenie _VarINT

Revision 1.15  2025/08/10 12:21:42  apro
reformat - polskie i taby

*/

