DELIMITER $$

/* ************************************************************************************************************************************************************************
	Numewracja oparta na `erp_obj_counters`
************************************************************************************************************************************************************************ */	

DROP PROCEDURE IF EXISTS `ht_zam_przepinanie_poz`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ht_zam_przepinanie_poz`(
	IN _MODE VARCHAR(150),
	IN _GUID_zam VARCHAR(38),
	IN _Param_A VARCHAR(255),
	IN _Param_B VARCHAR(255),
	IN _Param_C VARCHAR(255)
)
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
		Inkrementowanie licznika
		CALL ht_zam_przepinanie_poz('init', '{9234A546-1902-4971-948D-E043D53A98CB}', '', '', '');
		
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
S_ht_zam_przepinanie_poz: BEGIN
	IF _MODE = 'init' THEN
	/********************************************************************************************************************************
		CALL ht_zam_przepinanie_poz('init', '{787F3BD5-FC19-4E17-8006-6E7EEF3782B7}', '', '', '');
		_GUID_zam	to źrodlowe zam skad wezmiemy kontrahenta
	********************************************************************************************************************************/
		SELECT 
			waluta, kontrguid, IdnMagazyn, rodzaj
		FROM `zmng` z
		WHERE
			z.GUID = _GUID_zam
		INTO 
			@_waluta, @_kontrguid, @_IdnMagazyn, @_rodzaj
		;		

		DROP TEMPORARY TABLE IF EXISTS `t_ht_zam_przepinanie_poz`;
		CREATE TEMPORARY TABLE `t_ht_zam_przepinanie_poz`
			SELECT
				_VarInt(0) AS Checked,
				-- CONCAT(@LP := @LP + 1, ' - nr zam.:', z.NrZamw) AS Header,
				p.lp,
				CONCAT('nr zam.:', z.NrZamw) AS Header,
				p.GUID, p.Zrealizowano, p.TerminRealizacji, p.TowrSymb, p.TowrNazwa, p.Ilosc, _VarDouble(p.cenanet*p.Kurs) AS Cena,
				p.IdnNg, p.IdnNg AS IdnNg_NEW
			FROM `zmpo` p
			INNER JOIN `zmng` z ON z.GUID = p.IdnNg
			WHERE
				TRUE
				AND p.Ilosc > p.Zrealizowano
				AND z.KontrGUID = @_kontrguid
				AND z.Waluta = @_waluta
				AND z.IdnMagazyn = @_IdnMagazyn
				AND z.rodzaj = @_rodzaj
			ORDER BY z.TerminRealizacji
		-- ORDER BY z.Idn desc
		;
		ALTER TABLE `t_ht_zam_przepinanie_poz` ADD PRIMARY KEY GUID(GUID), ADD KEY IdnNg(IdnNg);

		TRUNCATE `t_ht_zam_przepinanie_poz`;
		INSERT INTO `t_ht_zam_przepinanie_poz`
			SELECT
				_VarInt(0) AS Checked,
				p.lp,
				-- CONCAT(@LP := @LP + 1, ' - nr zam.:', z.NrZamw) AS Header,
				CONCAT('nr zam.:', z.NrZamw) AS Header,
				p.GUID, p.Zrealizowano, p.TerminRealizacji, p.TowrSymb, p.TowrNazwa, p.Ilosc, _VarDouble(p.cenanet*p.Kurs) AS Cena,
				p.IdnNg, p.IdnNg AS IdnNg_NEW
			FROM `zmpo` p
			INNER JOIN `zmng` z ON z.GUID = p.IdnNg
			WHERE
				TRUE
				AND p.Ilosc > p.Zrealizowano
				AND z.KontrGUID = @_kontrguid
				AND z.Waluta = @_waluta
				AND z.IdnMagazyn = @_IdnMagazyn
				AND z.rodzaj = @_rodzaj				
			ORDER BY z.TerminRealizacji
		-- ORDER BY z.Idn desc
		;
		
		DROP TEMPORARY TABLE IF EXISTS `t_ht_zam_przepinanie_poz_lp`;
		CREATE TEMPORARY TABLE `t_ht_zam_przepinanie_poz_lp` SELECT _varInt(0) AS LP, IdnNg, TerminRealizacji FROM `t_ht_zam_przepinanie_poz` GROUP BY IdnNg ORDER BY TerminRealizacji;
		SET @LP := 0;
		UPDATE `t_ht_zam_przepinanie_poz_lp` SET LP := (@LP := @LP + 1);
		UPDATE `t_ht_zam_przepinanie_poz_lp` tlp, `t_ht_zam_przepinanie_poz` t SET t.Header := CONCAT(tlp.LP, ' - ', t.Header) WHERE t.IdnNg = tlp.IdnNg;
		DROP TEMPORARY TABLE IF EXISTS `t_ht_zam_przepinanie_poz_lp`;
		
		
	END IF;	--	_MODE = 'init' 
	
	IF _MODE = 'fin' THEN
		TRUNCATE `t_ht_zam_przepinanie_poz`;
		-- DROP TEMPORARY TABLE IF EXISTS `t_ht_zam_przepinanie_poz`;
	END IF;	--	_MODE = 'fin' 
	
	IF _MODE = 'select.dest.zam' THEN
	/********************************************************************************************************************************
		CALL ht_zam_przepinanie_poz('select.dest.zam', '', '', '', '');
	********************************************************************************************************************************/
		SELECT 
			z.GUID, z.NrZamw, z.TerminRealizacji
		FROM `t_ht_zam_przepinanie_poz` p
		INNER JOIN `zmng` z ON z.GUID = p.IdnNg
		GROUP BY z.GUID
		;
	END IF;	--	_MODE = 'select.dest.zam' 
	
	IF _MODE = 'przepnij' THEN
	/********************************************************************************************************************************
		CALL ht_zam_przepinanie_poz('przepnij', '{E8AB3E5E-56D6-40F7-9F13-483587BCE0FA}', '', '', '');
		_GUID_zam	to docelowe zam do ktorego dopniemy zazn. pozycje
	********************************************************************************************************************************/
		SELECT COUNT(*) FROM `t_ht_zam_przepinanie_poz` p WHERE p.Checked INTO @Count.p.Checked; 
		IF IFNULL(@Count.p.Checked, 0) != 0 THEN
			
		--	Ustaw
			UPDATE `t_ht_zam_przepinanie_poz` p
			SET
				p.IdnNg_NEW = _GUID_zam
			WHERE
				p.Checked
			;
		--	przepnij	
			UPDATE `zmpo` p, `t_ht_zam_przepinanie_poz` tp
			SET
				p.IdnNg = tp.IdnNg_NEW
			WHERE
				tp.Checked
				-- tp.IdnNg_NEW = _GUID_zam
				AND
				p.GUID = tp.GUID
			;
			DROP TEMPORARY TABLE IF EXISTS `t_ht_zam_przepinanie_zmng`;
			CREATE TEMPORARY TABLE `t_ht_zam_przepinanie_zmng` (
				`GUID` VARCHAR(38) NOT NULL DEFAULT '' COMMENT 'ID zamowienia',
				PRIMARY KEY GUID (GUID)
			);
		--	W `t_ht_zam_przepinanie_zmng` bede mial wszystkie guidy zam ktore sa przepinane (stare i nowe)	
			INSERT IGNORE INTO `t_ht_zam_przepinanie_zmng`
				SELECT _GUID_zam
			;
			INSERT IGNORE INTO `t_ht_zam_przepinanie_zmng`
				SELECT p.IdnNg FROM `t_ht_zam_przepinanie_poz` p WHERE p.Checked GROUP BY p.IdnNg
			;
		--	Teraz petla zamowien	
			BEGIN
				DECLARE done BOOL DEFAULT FALSE;
				DECLARE _GUID VARCHAR(38);
			--	3.1 Kursor	
				DECLARE c_t_ht_zam_przepinanie_zmng CURSOR FOR
					SELECT
						z.GUID
					FROM `t_ht_zam_przepinanie_zmng` z
				;	
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
							/* ---------------------------------------------------------------------------------------------------------------------------------------
								ERORR 
							--------------------------------------------------------------------------------------------------------------------------------------- */	
							DECLARE continue HANDLER FOR SQLEXCEPTION BEGIN
								SET @_Process = CONCAT("CALL ht_zam_przepinanie_poz()", ' ZM:', _GUID);
								CALL _Report_Error(@_Process, 'Exec', 'stat', '', '');
							END;
				OPEN c_t_ht_zam_przepinanie_zmng;
				loop_c_t_ht_zam_przepinanie_zmng: LOOP
					SET done = FALSE;
					FETCH c_t_ht_zam_przepinanie_zmng INTO 
						_GUID
					;
					IF done THEN
						LEAVE loop_c_t_ht_zam_przepinanie_zmng;
					END IF;
				--	Wykonaj przepiecie
					CALL ht_zam_przepinanie_poz('przepnij.jedno.zam', _GUID, '', '', '');
				END LOOP;
				CLOSE c_t_ht_zam_przepinanie_zmng;
							
			END;
			
		END IF;
						
	END IF;	--	_MODE = 'przepnij' 
	
	IF _MODE = 'przepnij.jedno.zam' THEN
	/********************************************************************************************************************************
		CALL ht_zam_przepinanie_poz('przepnij.jedno.zam', _GUID, '', '', '');
		_GUID_zam	to zamowienie ktore w swiecie E bede przeliczal
	********************************************************************************************************************************/
		/*
		UPDATE `zmpo` p, `t_ht_zam_przepinanie_poz` tp
		SET
			p.IdnNg = tp.IdnNg_NEW
		WHERE
			tp.IdnNg_NEW = _GUID_zam
			AND
			p.GUID = tp.GUID
		;
		*/
		SET @IdnOper = _GUID_zam;
		CALL _Session_Refresh_Variables('', '', '');
		
		CALL zmng_E_init('EDIT', _GUID_zam, 'jmam', '', '');
		CALL zmng_E_Run('before.run', _GUID_zam, '', '', '');
		CALL zmng_E_Run('', _GUID_zam, '', '', '');
		CALL zmng_E_Run('after.run', _GUID_zam, '', '', '');
		
		CALL zmng_E_Let('Post', _GUID_zam, '', '');
		-- CALL zmng_E_Let('Apply', _GUID_zam, '', '');
		CALL zmng_E_Let('ApplyAndClear', _GUID_zam, '', '');
		-- CALL zmng_E_Let('End', _GUID_zam, '', '');
		
	END IF;	--	_MODE = 'przepnij.jedno.zam' 
END$$


DELIMITER ;

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


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
*/

