 /*!40101 SET NAMES utf8 */;
DELIMITER $$

DROP PROCEDURE IF EXISTS `ht_KompensTool_Doc_WZ_ZW`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ht_KompensTool_Doc_WZ_ZW`(
	IN _MODE INT,
	IN _GUIDFin VARCHAR(38),
	IN _DateOd DATE, 
	IN _DateDo DATE, 
	IN _LIMIT INTEGER
	)
	READS SQL DATA
BEGIN
/*
	_MODE:
	0:	Dziala na dokumentach z data od _DateOd do _DateDo limit _LIMIT 
	
	1:	Dziala na konkretnjym dokumencie GUID = _GUIDFin
	
	
	DECLARE SqlText TEXT;
	DECLARE NonText VARCHAR(20) DEFAULT '*** poufne ***';
	DECLARE _GUIDFin VARCHAR(38);
	DECLARE _MaxCheck INT; 
	DECLARE _IsOK BOOLEAN;
*/
	
/******************************************************************************************************************************************************/
--	Dziala na dokumentach z data od _DateOd do _DateDo limit _LIMIT 
/******************************************************************************************************************************************************/
	IF _MODE = 0 THEN
	--	Dziala na dokumentach z data od _DateOd do _DateDo limit _LIMIT 
		DROP TEMPORARY TABLE IF EXISTS `t_AllDoc`;
		CREATE TEMPORARY TABLE `t_AllDoc`
			SELECT
				distinct o.idnoperfin as GUID
			FROM `oppo` p 
			WHERE
				p.DataWystaw between _DateOd AND _DateDo 
				AND 
				p.IdnOperFin <> p.idnoper 
				AND 
				p.Rodzaj = 70	-- ZWROTY!!!			
/*
			SELECT
				n.GUID 
			FROM `opng` n
			INNER JOIN `oppo` p ON p.IdnOperFin = n.GUID AND p.Rodzaj = 70	-- ZWROTY!!!
			WHERE
				n.DataWystaw between _DateOd AND _DateDo
				AND n.Rodzaj IN (0,1,2)
			GROUP BY n.GUID
		--	LIMIT 1
*/		
		;
		
		BEGIN
			DECLARE done BOOL DEFAULT FALSE;
			DECLARE _LOCAL_GUID VARCHAR(38);
			DECLARE c_proc CURSOR FOR
				SELECT 
					n.GUID
				FROM `t_AllDoc` n
				;
			DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
			OPEN c_proc;
			loop1: LOOP
				FETCH c_proc INTO _LOCAL_GUID;
				IF done THEN
					LEAVE loop1;
				END IF;
				
			--	Wolaj dzialanie do siebie samej czyli z _MODE = 1 i Guid-em dokumentu
				CALL ht_KompensTool_Doc_WZ_ZW(1, _LOCAL_GUID, '', '', 0);
			END LOOP;
			CLOSE c_proc;
		END;
	END IF;

/******************************************************************************************************************************************************/
--	Dziala na konkretnjym dokumencie GUID = _GUIDFin
/******************************************************************************************************************************************************/
	IF _MODE = 1 THEN
	--	Kreuj pozycje dokumentu do `tdoc_oppo_WZZW`
		DROP TEMPORARY TABLE IF EXISTS `tdoc_oppo_WZZW`;
		CREATE TEMPORARY TABLE `tdoc_oppo_WZZW`
			SELECT
				p.* 
				-- _VarInt(0) AS Deleted 
			FROM `oppo` p
			WHERE
				p.IdnOperFin = _GUIDFin
		;
	--	Kreuj liste towarow tego dokumentu do `tdoc_oppo_WZZW_towr`
		DROP TEMPORARY TABLE IF EXISTS `tdoc_oppo_WZZW_towr`;
		CREATE TEMPORARY TABLE `tdoc_oppo_WZZW_towr`
			SELECT
				distinct p.IdnTowr AS IdnTowr 
			FROM `tdoc_oppo_WZZW` p
		;
		
		BEGIN
			DECLARE done2 BOOL DEFAULT FALSE;
			DECLARE _LOCAL_IdnTowr VARCHAR(38);
			DECLARE c_proc2 CURSOR FOR
				SELECT 
					t.IdnTowr
				FROM `tdoc_oppo_WZZW_towr` t
				;
			DECLARE CONTINUE HANDLER FOR NOT FOUND SET done2 = TRUE;
			OPEN c_proc2;
			loop2: LOOP
				FETCH c_proc2 INTO _LOCAL_IdnTowr;
				IF done2 THEN
					LEAVE loop2;
				END IF;
				
			--	Wolaj dzialanie do siebie samej czyli z _MODE = 1 i Guid-em dokumentu
				CALL ht_KompensTool_WZ_ZW(0, _GUIDFin, _LOCAL_IdnTowr);
			END LOOP;
			CLOSE c_proc2;
		END;
	END IF;
	
END$$

DELIMITER ;
