DELIMITER $$

DROP PROCEDURE IF EXISTS `ht_kontrah_tools`$$

CREATE DEFINER=`root`@`localhost` 
PROCEDURE `ht_kontrah_tools`(IN MODE VARCHAR(38), IN _OLD VARCHAR(38), IN _NEW VARCHAR(38), IN _Param_A VARCHAR(250), IN _Param_B VARCHAR(250), IN _Param_C VARCHAR(250), IN _Param_D VARCHAR(250))
	CONTAINS SQL
	COMMENT ''
SPROC: BEGIN
	SET @result='';

	IF MODE='INIT' THEN
		CREATE TABLE IF NOT EXISTS temp_kontrah_scaleni (
			OLDGUID VARCHAR(38) NOT NULL,NEWGUID VARCHAR(38) NOT NULL,OLDSYMBOL VARCHAR(38) NOT NULL,
			OLDKONTO VARCHAR(38) NOT NULL,OLDKONTO2 VARCHAR(38) NOT NULL,
			UNIQUE OLDNEW(OLDGUID,NEWGUID)
		);
		DROP TEMPORARY TABLE IF EXISTS t_do_scalenia;
		CREATE TEMPORARY TABLE t_do_scalenia (OLDGUID VARCHAR(38) NOT NULL,NEWGUID VARCHAR(38) NOT NULL);
		SET @result='OK';
	END IF;

	IF MODE='SCAL.idn' THEN
		SELECT GUID FROM kontrah WHERE idn=_VarInt(_OLD) INTO @old;
		SELECT GUID FROM kontrah WHERE idn=_VarInt(_NEW) INTO @new;
		IF NOT ISNULL(@old) AND NOT ISNULL(@new) THEN
			CALL ht_kontrah_tools('SCAL.guid',@old,@new,_Param_A,_Param_B,_Param_C,_Param_D);
		ELSE
			SET @result='Brak kontrahenta old lub new';
		END IF;
	END IF;

	IF MODE='SCAL.guid' THEN
		INSERT IGNORE INTO temp_kontrah_scaleni 
		SELECT _OLD,_NEW,symbol,kontofk,kontofk2
		FROM kontrah 
		WHERE guid=_OLD;
		UPDATE opng SET idnattach=kontrguid WHERE kontrguid=_OLD;
		UPDATE opng n, rozrach r,kontrah k SET r.platnikguid=k.platnikguid WHERE n.kontrguid=_OLD AND k.guid=n.kontrguid AND r.idnoper=n.guid;
		UPDATE opng n,kontrah k SET n.kontrguid=_NEW,n.kontrsymbol=k.symbol,n.platnikguid=k.platnikguid WHERE n.kontrguid=_OLD AND k.guid=_NEW;
		UPDATE opng n,kontrah k SET n.katcol0=k.katcol0,n.katcol1=k.katcol1,n.katcol2=k.katcol2,n.katcol3=k.katcol3,n.katcol4=k.katcol4 WHERE n.kontrguid=_OLD AND k.guid=_NEW;
		UPDATE opng SET platnikguid=_NEW WHERE platnikguid=_OLD;
		UPDATE zmng SET kontrguid=_NEW WHERE kontrguid=_OLD;
		UPDATE kontrah SET deleted=1, Blokujsprzedaz=1, uzasadnienie_blokady=CONCAT('scalony z ',_NEW,' ',_VarStr(NOW()),' przez ',IFNULL(@session.user.UserLogin, '?')) WHERE guid=_OLD;
		IF IFNULL(@session.globals.Owner, '') = 'IZOHAN' THEN
		/********************************************************************************** 
			IZOHAN - EBI
		**********************************************************************************/
			CALL ebi.ebi_Deduplikacja('Deduplikacja', _OLD, _NEW, '', '');
		END IF;
		
		SET @result='OK';
	END IF;

	IF MODE='SCAL.list' THEN
		BEGIN
			DECLARE _aOLD VARCHAR(38);
			DECLARE _aNEW VARCHAR(38);
			
			DECLARE _done BOOL DEFAULT FALSE;
			DECLARE _c_proc CURSOR FOR
				SELECT 
					oldguid,newguid
				FROM `t_do_scalenia`
				;
			
			DECLARE CONTINUE HANDLER FOR NOT FOUND SET _done = TRUE;
			OPEN _c_proc;
				loop1: LOOP
					SET _done = FALSE;
					FETCH _c_proc INTO _aOLD,_aNEW;
					IF _done THEN	
						LEAVE loop1;
					ELSE
						SELECT _aOLD,_aNEW;
						CALL ht_kontrah_tools('SCAL.guid',_aOLD,_aNEW,_Param_A,_Param_B,_Param_C,_Param_D);
					END IF;
				END LOOP;
			CLOSE _c_proc;

			SET @result='OK';
		END;			
	END IF;

END$$

DELIMITER ;
