DROP PROCEDURE IF EXISTS `CALCTOTALGPV`;
delimiter ;;
CREATE PROCEDURE `CALCTOTALGPV`()
BEGIN
	DECLARE
		MLAYER,
		MMINLAYER,
		MUSERTYPE INT DEFAULT 0;
	DECLARE
		MUSERID,
		MMAXUSERID VARCHAR ( 50 );
	DECLARE
		MGPV DECIMAL ( 10, 2 );
	SELECT
		MAX( LAYER ) INTO MLAYER 
	FROM
		AR_BS_BONUS_103_CALC_NET 
	WHERE
		GPV > 0;
	SELECT
		MIN( LAYER ) INTO MMINLAYER 
	FROM
		AR_BS_BONUS_103_CALC_NET 
	WHERE
		USER_TYPE = 30;
	UPDATE AR_BS_BONUS_103_CALC_NET T1
	INNER JOIN AR_BS_BONUS_103_CALC_NET T2 ON IFNULL( T1.INTRODUCER_ID, '0' ) = T2.USER_ID 
	AND T1.USER_TYPE = 30 
	AND T2.USER_TYPE = 30 
	SET T1.INTRODUCER_ID30 = T2.USER_ID;
	WHILE
			MLAYER > MMINLAYER DO
			CALL CALCTOTALGPV_ ( MLAYER, MMINLAYER );
		
		SET MLAYER = MLAYER - 1;
		
	END WHILE;

END
;;